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

Author Topic: binding window with texture in different classes  (Read 1216 times)

0 Members and 1 Guest are viewing this topic.

aslo

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
binding window with texture in different classes
« on: August 15, 2015, 03:11:03 pm »
Hey guys,
if my english is not so good sry and if i say something wrong its because im a newbie in c++ and sfml
so my question is how can i put the SpriteTexture in the draw function that is in a other class


main.cpp
#include <iostream>

#include <SFML\Graphics.hpp>

#include "Player.h"
#include "GameWindow.h"


int main()
{

        GameWindow runWindow;
        runWindow.RunGameWindow();

        return 0;
}

Player.h
#ifndef PLAYER_HPP
#define PLAYER_HPP

#include <iostream>
#include <SFML\Graphics.hpp>

#include "GameWindow.h"

class Player
{
public:
       
        void CreatTexture();
        void PlayerMovement();

       


private:

        sf::Texture PlayerTexture;
        sf::Sprite  PlayerSprite;
};

#endif
 

Player.cpp
#include "Player.h"

void Player::CreatTexture()
{
        if (!PlayerTexture.loadFromFile("Picture//rgs.png"))
        {/*----------*/ }

        PlayerSprite.setTextureRect(sf::IntRect(40, 0, 20, 18));
       
}



void Player::PlayerMovement()
{
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
                PlayerSprite.move(0.1 , 0);
                PlayerSprite.setTextureRect(sf::IntRect(140, 0, 40, 40));
        }


        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
                PlayerSprite.move(-0.1 , 0);
                PlayerSprite.setTextureRect(sf::IntRect(100, 0, 20, 30));
        }
}
 

GameWindow.h
#ifndef GAMEWINDOW_HPP
#define GAMEWINDOW_HPP

#include <SFML\Graphics.hpp>
#include "Player.h"


class GameWindow
{
public:
        GameWindow();
        ~GameWindow();

       
       
        void RunGameWindow();
        void Draw();
        void Render();
       


private:
        sf::RenderWindow *mainWindow;
        sf::Event        *mainEvent;
};

#endif

GameWindow.cpp
#include "GameWindow.h"


GameWindow::GameWindow()
{
        mainWindow = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Red Ghost", sf::Style::Titlebar);
        mainEvent  = new sf::Event;
}


GameWindow::~GameWindow()
{
        mainWindow = NULL;
        mainEvent = NULL;

        delete mainWindow;
        delete mainEvent;
}


void GameWindow::RunGameWindow()
{

        while (mainWindow->isOpen())
        {

                while (mainWindow->pollEvent(*mainEvent))
                {/*----------*/
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                {
                        mainWindow->close();
                }


                Render();
        }

}

void GameWindow::Draw()
{
        /*I want put the PlayerSprite in this function or draw the PlayerSprite
        on the window*/

}

void GameWindow::Render()
{
        mainWindow->clear(sf::Color::White);
        Draw();
        mainWindow->display();
}
 

you guys can give me a other example too if this is to much
thanks

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: binding window with texture in different classes
« Reply #1 on: August 15, 2015, 03:52:55 pm »
You'll need to pass the player or just the player sprite to the game window object.
Either do this whenever you call game window's draw, or set it up in advance and hold a reference/pointer to it.

Speaking of pointers, not sure why you are using so much of the raw pointer.

Firstly, the heap is usually only used for larger pieces of data, so that "sf::Event" makes no sense over there. Chuck it on the stack with the game window object (hoping you didn't also put this on the heap). The render window's a bit larger, sure, but the stack can probably handle that too.

Secondly, if you do need to use the heap, don't use raw pointers and new to allocate the space.
There is much better - and very clear - information about this here.


Also, generally, it's better to keep larger resources (such as textures) outside of the objects themselves.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*