Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - KyleTheCarpenter

Pages: [1]
1
Hello! i have made a classic tic-tac-toe Game from scratch! (well using SFML)

the only classes i didnt make were the TextureList and FontList from a book.

Im seeking advice on game structure. this took me about 10 hours in total.

the code is here.
https://github.com/KyleTheCarpenter/Tic-Tac-Toe

heres a little preview:



2
Hello i am refrencing Chapter 3 in the book Sfml Game Development.. ive been doing well until now. i cant get this to work i even copied and pasted all the git hub stuff to replace what i wrote and i still get the same errors..
https://github.com/SFML/SFML-Game-Development-Book/tree/master/03_World is the source i copy and pasted and still no work. my error messages are:

Build started...
1>------ Build started: Project: realBook, Configuration: Debug Win32 ------
1>Game.cpp
1>C:\SFML\realBook\SceneNode.h(11,17): error C2011: 'SceneNode': 'class' type redefinition
1>C:\SFML\realBook\SceneNode.h(11): message : see declaration of 'SceneNode'
1>C:\SFML\realBook\SpriteNode.h(7,1): error C2504: 'SceneNode': base class undefined
1>C:\SFML\realBook\Entity.h(6,1): error C2504: 'SceneNode': base class undefined
1>C:\SFML\realBook\ResourceIdentifier.hpp(10,2): error C2011: 'Textures::ID': 'enum' type redefinition
1>C:\SFML\realBook\ResourceIdentifier.hpp(9): message : see declaration of 'Textures::ID'
1>C:\SFML\realBook\World.h(46,18): error C2079: 'World::mSceneGraph' uses undefined class 'SceneNode'
1>C:\SFML\realBook\Game.cpp(89,27): error C3861: 'toString': identifier not found
1>C:\SFML\realBook\Game.cpp(90,25): error C3861: 'toString': identifier not found
1>Main.cpp
1>C:\SFML\realBook\SceneNode.h(11,17): error C2011: 'SceneNode': 'class' type redefinition
1>C:\SFML\realBook\SceneNode.h(11): message : see declaration of 'SceneNode'
1>C:\SFML\realBook\SpriteNode.h(7,1): error C2504: 'SceneNode': base class undefined
1>C:\SFML\realBook\Entity.h(6,1): error C2504: 'SceneNode': base class undefined
1>C:\SFML\realBook\ResourceIdentifier.hpp(10,2): error C2011: 'Textures::ID': 'enum' type redefinition
1>C:\SFML\realBook\ResourceIdentifier.hpp(9): message : see declaration of 'Textures::ID'
1>C:\SFML\realBook\World.h(46,18): error C2079: 'World::mSceneGraph' uses undefined class 'SceneNode'
1>World.cpp
1>C:\SFML\realBook\SceneNode.h(11,17): error C2011: 'SceneNode': 'class' type redefinition
1>C:\SFML\realBook\SceneNode.h(11): message : see declaration of 'SceneNode'
1>C:\SFML\realBook\SpriteNode.h(7,1): error C2504: 'SceneNode': base class undefined
1>C:\SFML\realBook\Entity.h(6,1): error C2504: 'SceneNode': base class undefined
1>C:\SFML\realBook\ResourceIdentifier.hpp(10,2): error C2011: 'Textures::ID': 'enum' type redefinition
1>C:\SFML\realBook\ResourceIdentifier.hpp(9): message : see declaration of 'Textures::ID'
1>C:\SFML\realBook\World.h(46,18): error C2079: 'World::mSceneGraph' uses undefined class 'SceneNode'
1>C:\SFML\realBook\World.cpp(30,43): error C2039: 'getPosition': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(48,26): error C2664: 'void sf::RenderTarget::draw(const sf::Drawable &,const sf::RenderStates &)': cannot convert argument 1 from 'int' to 'const sf::Drawable &'
1>C:\SFML\realBook\World.cpp(48,26): message : Reason: cannot convert from 'int' to 'const sf::Drawable'
1>C:\SFML\realBook\World.cpp(48,26): message : No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\SFML\sfml\include\SFML\Graphics\RenderTarget.hpp(237,10): message : see declaration of 'sf::RenderTarget::draw'
1>C:\SFML\realBook\World.cpp(53,31): error C2027: use of undefined type 'Textures::ID'
1>C:\SFML\realBook\ResourceIdentifier.hpp(9): message : see declaration of 'Textures::ID'
1>C:\SFML\realBook\World.cpp(53,36): error C2065: 'Eagle': undeclared identifier
1>C:\SFML\realBook\World.cpp(54,31): error C2027: use of undefined type 'Textures::ID'
1>C:\SFML\realBook\ResourceIdentifier.hpp(9): message : see declaration of 'Textures::ID'
1>C:\SFML\realBook\World.cpp(54,37): error C2065: 'Raptor': undeclared identifier
1>C:\SFML\realBook\World.cpp(55,31): error C2027: use of undefined type 'Textures::ID'
1>C:\SFML\realBook\ResourceIdentifier.hpp(9): message : see declaration of 'Textures::ID'
1>C:\SFML\realBook\World.cpp(55,37): error C2065: 'Desert': undeclared identifier
1>C:\SFML\realBook\World.cpp(63,14): error C2027: use of undefined type 'SceneNode'
1>C:\SFML\realBook\SceneNode.h(11): message : see declaration of 'SceneNode'
1>C:\SFML\realBook\World.cpp(63,18): error C2065: 'Ptr': undeclared identifier
1>C:\SFML\realBook\World.cpp(63,18): error C2146: syntax error: missing ';' before identifier 'layer'
1>C:\SFML\realBook\World.cpp(63,39): error C2027: use of undefined type 'SceneNode'
1>C:\SFML\realBook\SceneNode.h(11): message : see declaration of 'SceneNode'
1>C:\SFML\realBook\World.cpp(63,18): error C3861: 'layer': identifier not found
1>C:\SFML\realBook\World.cpp(64,21): error C2065: 'layer': undeclared identifier
1>C:\SFML\realBook\World.cpp(66,37): error C2065: 'layer': undeclared identifier
1>C:\SFML\realBook\World.cpp(70,53): error C2027: use of undefined type 'Textures::ID'
1>C:\SFML\realBook\ResourceIdentifier.hpp(9): message : see declaration of 'Textures::ID'
1>C:\SFML\realBook\World.cpp(70,59): error C2065: 'Desert': undeclared identifier
1>C:\SFML\realBook\World.cpp(70,23): error C2530: 'texture': references must be initialized
1>C:\SFML\realBook\World.cpp(76,20): error C2039: 'setPosition': is not a member of 'SpriteNode'
1>C:\SFML\realBook\SpriteNode.h(6): message : see declaration of 'SpriteNode'
1>C:\SFML\realBook\World.cpp(77,26): error C2027: use of undefined type 'SceneNode'
1>C:\SFML\realBook\SceneNode.h(11): message : see declaration of 'SceneNode'
1>C:\SFML\realBook\World.cpp(80,58): error C2039: 'Eagle': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(80,63): error C2065: 'Eagle': undeclared identifier
1>C:\SFML\realBook\World.cpp(82,19): error C2039: 'setPosition': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(84,19): error C2027: use of undefined type 'SceneNode'
1>C:\SFML\realBook\SceneNode.h(11): message : see declaration of 'SceneNode'
1>C:\SFML\realBook\World.cpp(87,62): error C2039: 'Raptor': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(87,68): error C2065: 'Raptor': undeclared identifier
1>C:\SFML\realBook\World.cpp(88,14): error C2039: 'setPosition': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(89,19): error C2039: 'attachChild': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(91,63): error C2039: 'Raptor': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(91,69): error C2065: 'Raptor': undeclared identifier
1>C:\SFML\realBook\World.cpp(92,15): error C2039: 'setPosition': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>C:\SFML\realBook\World.cpp(93,19): error C2039: 'attachChild': is not a member of 'AirCraft'
1>C:\SFML\realBook\AirCraft.h(6): message : see declaration of 'AirCraft'
1>Generating Code...
1>Done building project "realBook.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
Severity        Code    Description     Project File    Line    Suppression State
Message         No constructor could take the source type, or constructor overload resolution was ambiguous     realBook        C:\SFML\realBook\World.cpp      48     
Error   C2039   'attachChild': is not a member of 'AirCraft'    realBook        C:\SFML\realBook\World.cpp      89     
Error   C2039   'attachChild': is not a member of 'AirCraft'    realBook        C:\SFML\realBook\World.cpp      93     
Error   C2065   'Desert': undeclared identifier realBook        C:\SFML\realBook\World.cpp      55     
Error   C2065   'Desert': undeclared identifier realBook        C:\SFML\realBook\World.cpp      70     
Error   C2065   'Eagle': undeclared identifier  realBook        C:\SFML\realBook\World.cpp      53     
Error   C2039   'getPosition': is not a member of 'AirCraft'    realBook        C:\SFML\realBook\World.cpp      30     
Error   C3861   'layer': identifier not found   realBook        C:\SFML\realBook\World.cpp      63     
Error   C2065   'layer': undeclared identifier  realBook        C:\SFML\realBook\World.cpp      64     
Error   C2065   'layer': undeclared identifier  realBook        C:\SFML\realBook\World.cpp      66     
Error   C2065   'Ptr': undeclared identifier    realBook        C:\SFML\realBook\World.cpp      63     
Error   C2065   'Raptor': undeclared identifier realBook        C:\SFML\realBook\World.cpp      54     
Error   C2011   'SceneNode': 'class' type redefinition  realBook        C:\SFML\realBook\SceneNode.h    11     
Error   C2504   'SceneNode': base class undefined       realBook        C:\SFML\realBook\SpriteNode.h   7      
Error   C2504   'SceneNode': base class undefined       realBook        C:\SFML\realBook\Entity.h       6      
Error   C2039   'setPosition': is not a member of 'AirCraft'    realBook        C:\SFML\realBook\World.cpp      82     
Error   C2039   'setPosition': is not a member of 'AirCraft'    realBook        C:\SFML\realBook\World.cpp      88     
Error   C2039   'setPosition': is not a member of 'AirCraft'    realBook        C:\SFML\realBook\World.cpp      92     
Error   C2039   'setPosition': is not a member of 'SpriteNode'  realBook        C:\SFML\realBook\World.cpp      76     
Error   C2530   'texture': references must be initialized       realBook        C:\SFML\realBook\World.cpp      70     
Error   C2011   'Textures::ID': 'enum' type redefinition        realBook        C:\SFML\realBook\ResourceIdentifier.hpp 10     
Error   C2664   'void sf::RenderTarget::draw(const sf::Drawable &,const sf::RenderStates &)': cannot convert argument 1 from 'int' to 'const sf::Drawable &'    realBook        C:\SFML\realBook\World.cpp      48     
Error   C2079   'World::mSceneGraph' uses undefined class 'SceneNode'   realBook        C:\SFML\realBook\World.h        46     
Message         Reason: cannot convert from 'int' to 'const sf::Drawable'       realBook        C:\SFML\realBook\World.cpp      48     
Message         see declaration of 'AirCraft'   realBook        C:\SFML\realBook\AirCraft.h     6      
Message         see declaration of 'AirCraft'   realBook        C:\SFML\realBook\AirCraft.h     6      
Message         see declaration of 'AirCraft'   realBook        C:\SFML\realBook\AirCraft.h     6      
Message         see declaration of 'AirCraft'   realBook        C:\SFML\realBook\AirCraft.h     6      
Message         see declaration of 'AirCraft'   realBook        C:\SFML\realBook\AirCraft.h     6      
Message         see declaration of 'AirCraft'   realBook        C:\SFML\realBook\AirCraft.h     6      
Message         see declaration of 'SceneNode'  realBook        C:\SFML\realBook\SceneNode.h    11     
Message         see declaration of 'SceneNode'  realBook        C:\SFML\realBook\SceneNode.h    11     
Message         see declaration of 'SceneNode'  realBook        C:\SFML\realBook\SceneNode.h    11     
Message         see declaration of 'SceneNode'  realBook        C:\SFML\realBook\SceneNode.h    11     
Message         see declaration of 'SceneNode'  realBook        C:\SFML\realBook\SceneNode.h    11     
Message         see declaration of 'sf::RenderTarget::draw'     realBook        C:\SFML\sfml\include\SFML\Graphics\RenderTarget.hpp     237    
Message         see declaration of 'SpriteNode' realBook        C:\SFML\realBook\SpriteNode.h   6      
Message         see declaration of 'Textures::ID'       realBook        C:\SFML\realBook\ResourceIdentifier.hpp 9      
Message         see declaration of 'Textures::ID'       realBook        C:\SFML\realBook\ResourceIdentifier.hpp 9      
Message         see declaration of 'Textures::ID'       realBook        C:\SFML\realBook\ResourceIdentifier.hpp 9      
Message         see declaration of 'Textures::ID'       realBook        C:\SFML\realBook\ResourceIdentifier.hpp 9      
Message         see declaration of 'Textures::ID'       realBook        C:\SFML\realBook\ResourceIdentifier.hpp 9      
Error   C2146   syntax error: missing ';' before identifier 'layer'     realBook        C:\SFML\realBook\World.cpp      63     
Error   C2027   use of undefined type 'SceneNode'       realBook        C:\SFML\realBook\World.cpp      63     
Error   C2027   use of undefined type 'SceneNode'       realBook        C:\SFML\realBook\World.cpp      63     
Error   C2027   use of undefined type 'SceneNode'       realBook        C:\SFML\realBook\World.cpp      77     
Error   C2027   use of undefined type 'SceneNode'       realBook        C:\SFML\realBook\World.cpp      84     
Error   C2027   use of undefined type 'Textures::ID'    realBook        C:\SFML\realBook\World.cpp      53     
Error   C2027   use of undefined type 'Textures::ID'    realBook        C:\SFML\realBook\World.cpp      54     
Error   C2027   use of undefined type 'Textures::ID'    realBook        C:\SFML\realBook\World.cpp      55     
Error   C2027   use of undefined type 'Textures::ID'    realBook        C:\SFML\realBook\World.cpp      70     
 


im in desperate need thanks so much

World.cpp
#include "World.h"

#include <SFML/Graphics/RenderWindow.hpp>


World::World(sf::RenderWindow& window)
        : mWindow(window)
        , mWorldView(window.getDefaultView())
        , mTextures()
        , mSceneGraph()
        , mSceneLayers()
        , mWorldBounds(0.f, 0.f, mWorldView.getSize().x, 2000.f)
        , mSpawnPosition(mWorldView.getSize().x / 2.f, mWorldBounds.height - mWorldView.getSize().y / 2.f)
        , mScrollSpeed(-50.f)
        , mPlayerAircraft(nullptr)
{
        loadTextures();
        buildScene();

        // Prepare the view
        mWorldView.setCenter(mSpawnPosition);
}

void World::update(sf::Time dt)
{
        // Scroll the world
        mWorldView.move(0.f, mScrollSpeed * dt.asSeconds());

        // Move the player sidewards (plane scouts follow the main aircraft)
        sf::Vector2f position = mPlayerAircraft->getPosition();
        sf::Vector2f velocity = mPlayerAircraft->getVelocity();

        // If player touches borders, flip its X velocity
        if (position.x <= mWorldBounds.left + 150.f
                || position.x >= mWorldBounds.left + mWorldBounds.width - 150.f)
        {
                velocity.x = -velocity.x;
                mPlayerAircraft->setVelocity(velocity);
        }

        // Apply movements
        mSceneGraph.update(dt);
}

void World::draw()
{
        mWindow.setView(mWorldView);
        mWindow.draw(mSceneGraph);
}

void World::loadTextures()
{
        mTextures.load(Textures::ID::Eagle, "files\\eagle.png");
        mTextures.load(Textures::ID::Raptor, "files\\raptor.png");
        mTextures.load(Textures::ID::Desert, "files\\desert.png");
}

void World::buildScene()
{
        // Initialize the different layers
        for (std::size_t i = 0; i < LayerCount; ++i)
        {
                SceneNode::Ptr layer(new SceneNode());
                mSceneLayers[i] = layer.get();

                mSceneGraph.attachChild(std::move(layer));
        }

        // Prepare the tiled background
        sf::Texture& texture = mTextures.get(Textures::ID::Desert);
        sf::IntRect textureRect(mWorldBounds);
        texture.setRepeated(true);

        // Add the background sprite to the scene
        std::unique_ptr<SpriteNode> backgroundSprite(new SpriteNode(texture, textureRect));
        backgroundSprite->setPosition(mWorldBounds.left, mWorldBounds.top);
        mSceneLayers[Background]->attachChild(std::move(backgroundSprite));

        // Add player's aircraft
        std::unique_ptr<AirCraft> leader(new AirCraft(AirCraft::Eagle, mTextures));
        mPlayerAircraft = leader.get();
        mPlayerAircraft->setPosition(mSpawnPosition);
        mPlayerAircraft->setVelocity(40.f, mScrollSpeed);
        mSceneLayers[Air]->attachChild(std::move(leader));

        // Add two escorting aircrafts, placed relatively to the main plane
        std::unique_ptr<AirCraft> leftEscort(new AirCraft(AirCraft::Raptor, mTextures));
        leftEscort->setPosition(-80.f, 50.f);
        mPlayerAircraft->attachChild(std::move(leftEscort));

        std::unique_ptr<AirCraft> rightEscort(new AirCraft(AirCraft::Raptor, mTextures));
        rightEscort->setPosition(80.f, 50.f);
        mPlayerAircraft->attachChild(std::move(rightEscort));
}

World.h
#include "ResourceHolder.hpp"
#include "SceneNode.h"
#include "SpriteNode.h"
#include "AirCraft.h"
#include "ResourceIdentifier.hpp"
#include <SFML/System/NonCopyable.hpp>
#include <SFML/Graphics/View.hpp>
#include <SFML/Graphics/Texture.hpp>

#include <array>


// Forward declaration
namespace sf
{
        class RenderWindow;
}

class World : private sf::NonCopyable
{
public:
        explicit                                                        World(sf::RenderWindow& window);
        void                                                            update(sf::Time dt);
        void                                                            draw();


private:
        void                                                            loadTextures();
        void                                                            buildScene();


private:
        enum Layer
        {
                Background,
                Air,
                LayerCount
        };


private:
        sf::RenderWindow& mWindow;
        sf::View                                                        mWorldView;
        TextureHolder                                           mTextures;

        SceneNode                                                       mSceneGraph;
        std::array<SceneNode*, LayerCount>      mSceneLayers;

        sf::FloatRect                                           mWorldBounds;
        sf::Vector2f                                            mSpawnPosition;
        float                                                           mScrollSpeed;
        AirCraft* mPlayerAircraft;
};
 

AirCraft h
#include "Entity.h"
#include "ResourceIdentifier.hpp"
#include <SFML/Graphics/Sprite.hpp>


class AirCraft : public Entity
{
public:
        enum Type
        {
                Eagle,
                Raptor,
        };


public:
        AirCraft(Type type, const TextureHolder& textures);


private:
        virtual void            drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;


private:
        Type                            mType;
        sf::Sprite                      mSprite;
};
 

AirCraft cpp
#include "Aircraft.h"
#include "ResourceHolder.hpp"

#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderStates.hpp>


Textures::ID toTextureID(AirCraft::Type type)
{
        switch (type)
        {
        case AirCraft::Eagle:
                return Textures::Eagle;

        case AirCraft::Raptor:
                return Textures::Raptor;
        }
        return Textures::Eagle;
}

AirCraft::AirCraft(Type type, const TextureHolder& textures)
        : mType(type)
        , mSprite(textures.get(toTextureID(type)))
{
        sf::FloatRect bounds = mSprite.getLocalBounds();
        mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f);
}

void AirCraft::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
        target.draw(mSprite, states);
}

Scenenode cpp
#include "SceneNode.h"
#include "Foreach.hpp"

#include <algorithm>
#include <cassert>


SceneNode::SceneNode()
: mChildren()
, mParent(nullptr)
{
}

void SceneNode::attachChild(Ptr child)
{
        child->mParent = this;
        mChildren.push_back(std::move(child));
}

SceneNode::Ptr SceneNode::detachChild(const SceneNode& node)
{
        auto found = std::find_if(mChildren.begin(), mChildren.end(), [&] (Ptr& p) { return p.get() == &node; });
        assert(found != mChildren.end());

        Ptr result = std::move(*found);
        result->mParent = nullptr;
        mChildren.erase(found);
        return result;
}

void SceneNode::update(sf::Time dt)
{
        updateCurrent(dt);
        updateChildren(dt);
}

void SceneNode::updateCurrent(sf::Time)
{
        // Do nothing by default
}

void SceneNode::updateChildren(sf::Time dt)
{
        FOREACH(Ptr& child, mChildren)
                child->update(dt);
}

void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        // Apply transform of current node
        states.transform *= getTransform();

        // Draw node and children with changed transform
        drawCurrent(target, states);
        drawChildren(target, states);
}

void SceneNode::drawCurrent(sf::RenderTarget&, sf::RenderStates) const
{
        // Do nothing by default
}

void SceneNode::drawChildren(sf::RenderTarget& target, sf::RenderStates states) const
{
        FOREACH(const Ptr& child, mChildren)
                child->draw(target, states);
}

sf::Vector2f SceneNode::getWorldPosition() const
{
        return getWorldTransform() * sf::Vector2f();
}

sf::Transform SceneNode::getWorldTransform() const
{
        sf::Transform transform = sf::Transform::Identity;

        for (const SceneNode* node = this; node != nullptr; node = node->mParent)
                transform = node->getTransform() * transform;

        return transform;
}

scenenode
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Graphics/Transformable.hpp>
#include <SFML/Graphics/Drawable.hpp>

#include <vector>
#include <memory>


class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable
{
public:
        typedef std::unique_ptr<SceneNode> Ptr;


public:
        SceneNode();

        void                                    attachChild(Ptr child);
        Ptr                                             detachChild(const SceneNode& node);

        void                                    update(sf::Time dt);

        sf::Vector2f                    getWorldPosition() const;
        sf::Transform                   getWorldTransform() const;


private:
        virtual void                    updateCurrent(sf::Time dt);
        void                                    updateChildren(sf::Time dt);

        virtual void                    draw(sf::RenderTarget& target, sf::RenderStates states) const;
        virtual void                    drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
        void                                    drawChildren(sf::RenderTarget& target, sf::RenderStates states) const;


private:
        std::vector<Ptr>                mChildren;
        SceneNode* mParent;
};

3
General / Re: Would like tips on my Button class.
« on: June 28, 2021, 08:57:50 pm »
My only issue with it is i have to Draw everything by refrencing the sprite and text. id like to be able to just draw(button);.
thanks to anyone that reads this

4
General / Would like tips on my Button class.
« on: June 28, 2021, 08:54:48 pm »
Hello i am newish to Sfml. finished a c++ book thats what i know.. i made a class Button i like it.
im impressed with it but i wonder if i could make it better. its just a standard button you click.

#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>

/**************************************************************************
                Title: Simple Button
                        Features: clickable button with a label. thats centerd shades when cursor is over.
                        is lockable and position can be moved

                Application:
                        ky::Button test1;   //default no data
                        ky::Button test2("eat my"); //name. position will be moved around after..
                        ky::Button test3("Butt",30,30); //name,locationX,LocationY
                        ky::Button test4("test3",30,30,1);//name,locationX,LocationY,scale

                        for scale 1small 6 big..
                inLoop:
                        if (event.type == sf::Event::MouseButtonPressed) {

                                if (event.mouseButton.button == sf::Mouse::Left) {
                                        if (test1.CheckState())
                                                std::cout << "clicked";//call function
                                                }
                                                }
                Display:
                test1.Update(MainWindow);
                MainWindow.draw(test1.Draw());
                MainWindow.draw(test1.Drawtext());

                Features:        #include "Button.hpp"
                                                .Locked() to lock
                                                .Unlock() to unlock
                                                .setPos(x,y) to move position
                                                .setSize(size) set size of button
                                                .CheckState() to check state
                                                .DisplayData() to display all credentials
**************************************************************************/


namespace ky{
        class Button {
        public:
                Button();  
                Button(std::string s);
                Button(std::string s, int localX, int localY);
                Button(std::string s, int localX, int localY, int Size);
                void Setup();
                void CenterTxt();
                void Update(sf::RenderWindow& MainWindow);
                sf::Sprite Draw() { return mySprite; }
                sf::Text Drawtext() { return myLabel; }
                void DisplayData();
                void setSize(int size) { scale = size; Setup(); }
                void setPos(int px, int py) {
                        mySprite.setPosition(px, py); CenterTxt();
               
               
                 }
                bool CheckState() { return State; }
                void Lock() { isLocked = true; }
                void Unlock() { isLocked = false; }
        private:
                sf::Vector2i mySize;
                sf::Font myFont;
                sf::Text myLabel;
                sf::Texture myTexture;
                sf::Sprite mySprite;
            bool State;
                bool isLocked;
                std::string myString;
                int scale;
        };
        void ky::Button::DisplayData()
        {
                std::string temp;
                if (isLocked)
                        temp = "Locked";
                if (!isLocked)
                        temp = "Unlocked";
                using namespace std;
                cout << "\n\tButton id: '" << myString << "' size is:\t width=" <<
                        mySprite.getLocalBounds().width*scale << "\theight= "<<
                        mySprite.getLocalBounds().height*scale;
                cout << "\n\tButton id: '" << myString << "' Position is:\t x:" <<
                        mySprite.getPosition().x << "\t y: " <<
                        mySprite.getPosition().y;
                cout << "\n\tButton id: '" << myString << "' Current State is:\t" <<
                        temp << endl << endl;
        }
        void ky::Button::Update(sf::RenderWindow& MainWindow) {
                auto mouse_pos = sf::Mouse::getPosition(MainWindow);
                auto translated_pos = MainWindow.mapPixelToCoords(mouse_pos);
                if (!isLocked) {
                        if (mySprite.getGlobalBounds().contains(translated_pos)) {
                                mySprite.setTextureRect(sf::IntRect(0, 15, 30, 15));
                                State = true;

                        }
                        else {
                                mySprite.setTextureRect(sf::IntRect(0, 0, 30, 15));
                                State = false;
                        }
                }
                if (isLocked) {
                        mySprite.setTextureRect(sf::IntRect(0, 0, 30, 15));
                        State = false;
                }
        }
       
        ky::Button::Button():scale(3), mySize(40,20),myString("test"),State(false)
        {
                Setup();
               
        }


        ky::Button::Button(std::string s) :mySize(40, 20), scale(3), myString(s), State(false)
        {
                Setup();
               
               
        }
        ky::Button::Button(std::string s, int localX, int localY) :
                scale(3),mySize(localX, localY), myString(s), State(false)
        {
                setPos(localX,localY);
                Setup();
                CenterTxt();
               
        }
        ky::Button::Button(std::string s, int localX, int localY,int size) :
                scale(size), mySize(localX, localY), myString(s), State(false)
        {
                setPos(localX, localY);
                Setup();
                CenterTxt();

        }
        void Button::CenterTxt() {
                int x = (myTexture.getSize().x / 2)*  scale;
                int y = (myTexture.getSize().y / 4) * scale;
                sf::FloatRect textRect = myLabel.getLocalBounds();
                myLabel.setOrigin(textRect.left + textRect.width / 2.0f,
                        textRect.top + textRect.height / 2.0f);
                myLabel.setPosition(mySprite.getPosition().x + (x), mySprite.getPosition().y + y);
        }
        void Button::Setup()
        {
                myTexture.loadFromFile("files\\kybutton.png");
                mySprite.setTexture(myTexture);
                mySprite.setScale(scale, scale);
                myFont.loadFromFile("files\\kyfont.otf");
                mySprite.setTextureRect(sf::IntRect(0, 0, 30, 15));
                myLabel.setFont(myFont);
                myLabel.setString(myString);
                isLocked = false;
                myLabel.setCharacterSize(6*scale);
                myLabel.setFillColor(sf::Color::Black);
                //myLabel.setPosition(mySprite.getGlobalBounds().width / 2, mySprite.getGlobalBounds().height / 2);
                //mySprite.setOrigin(myTexture.getSize().x / 2, myTexture.getSize().y / 2);
                CenterTxt();
        }
}

Pages: [1]