Sign in to follow this  
rooski358

Quick pointer question.+ some game design ones.

Recommended Posts

so ive been trying to get some structure to my game im working on since last one was such a tangled mess, so im starting with game states. ive ripped(not as bad as you think) most the code from [url="http://gamedevgeek.com/tutorials/managing-game-states-in-c/"]here[/url] but started converting it to use SFML instead of SDL . Ive never done gamestates before so that is why i didnt want to start from scratch, i figured id find someones source and modify it to work with SFML so i would better understand states and how they are implemented + get it in to my project.

my problem is ive scrapped all the sdl code and replaced it wil similar SFML code just to get things rolling , but im having issues with the pointer to the main renderwindow in the gameEngine class , specifically how would i declare it as public in the header file and initialize it in the cpp file ?

my header looks like this(simplified)
[code]

class CGameState;

class CGameEngine
{
public:

void Init(const char* title, int width=800, int height=600, bool fullscreen=false);
void Cleanup();

void ChangeState(CGameState* state);
void PushState(CGameState* state);
void PopState();

void HandleEvents();
void Update();
void Draw();

bool Running() { return m_running; }
void Quit() { m_running = false; }

sf::RenderWindow* window;

private:

};
[/code]


and my cpp init()
[code]
void CGameEngine::Init(const char* title, int width, int height, bool fullscreen)
{
// initialize
window->Create(sf::VideoMode(width, height),title);
window->SetFramerateLimit(60);

m_fullscreen = fullscreen;
m_running = true;

printf("CGameEngine Init\n");
}[/code]


the way i tried to create the renderwindow there fails , it should just be[code] sf::RenderWindow window(ect , ect);[/code]
but i cant do [code]sf::RenderWindow * window(ect);[/code] or [code]sf::RenderWindow window(ect);[/code] because those give errors as well . so im wondering how could i initialize this in the cpp while keeping the simple declaration in the .h so i can access it from other classes by doing game->window ? or is there a better way ? as you can tell i haven't used pointers very much .

anyway thanks for reading.

EDIT-----
is there even a need for the renderwindow to be a pointer , i don't mean now specifically but later on ?

i would just do window = new sf::RenderWindow(ect); wouldnt i :\ , and reason for pointer would be so the object doesn't get deleted ? damn its been a while since ive touched c++.

Share this post


Link to post
Share on other sites
[quote name='JohnnyR' timestamp='1318821508' post='4873304']
EDIT-----
is there even a need for the renderwindow to be a pointer , i don't mean now specifically but later on ?

i would just do window = new sf::RenderWindow(ect); wouldnt i :\ , and reason for pointer would be so the object doesn't get deleted ? damn its been a while since ive touched c++.
[/quote]

Kind of, I mean, it depends on what you need. A pointer is faster to pass through or return (In a function) than by value.

In my opinion the RenderWindow needs to be private and a GetWindow() function should be there that returns a pointer to a constant RenderWindow.

Oh, that reminds me if there is no possibly of creating a CGameEngine without a window it should probably be just a normal member, which you initialise with [url="http://www.cprogramming.com/tutorial/initialization-lists-c++.html"]http://www.cprogramming.com/tutorial/initialization-lists-c++.html[/url].


Yes, you do need to assign the pointer to some data, watch this epic video on pointers: [url="http://www.youtube.com/watch?v=UvoHwFvAvQE"]http://www.youtube.c...h?v=UvoHwFvAvQE[/url]

Share this post


Link to post
Share on other sites
Creating a function to return a reference or the pointer to renderwindow would be pretty much the exact same as what im currently doing [code] game->getWindow().display(); vs game->window->display(); [/code] is there any benefit to creating a function to access it , i get that it would be private and not directly accessible from other parts of the code , but does it really matter ? or is there a risk in accessing it the way i am ?

and your right, that was epic.

Share this post


Link to post
Share on other sites
[quote name='JohnnyR' timestamp='1318828254' post='4873324']
Creating a function to return a reference or the pointer to renderwindow would be pretty much the exact same as what im currently doing [code] game->getWindow().display(); vs game->window->display(); [/code] is there any benefit to creating a function to access it , i get that it would be private and not directly accessible from other parts of the code , but does it really matter ? or is there a risk in accessing it the way i am ?[/quote]

The only reason to have a get function would be to return it such that the thing getting it can't change your variable, or mess with it in ways you don't want it messed with.

Otherwise there is no real reason for it to be private.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this