Quick pointer question.+ some game design ones.

Started by
2 comments, last by nhold 12 years, 6 months ago
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 here 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)


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:

};



and my cpp init()

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");
}



the way i tried to create the renderwindow there fails , it should just be sf::RenderWindow window(ect , ect);
but i cant do sf::RenderWindow * window(ect); or sf::RenderWindow window(ect); 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++.
Advertisement

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++.


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 http://www.cprogramming.com/tutorial/initialization-lists-c++.html.


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

Engineering Manager at Deloitte Australia

Creating a function to return a reference or the pointer to renderwindow would be pretty much the exact same as what im currently doing game->getWindow().display(); vs game->window->display(); 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.

Creating a function to return a reference or the pointer to renderwindow would be pretty much the exact same as what im currently doing game->getWindow().display(); vs game->window->display(); 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 ?


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.


Engineering Manager at Deloitte Australia

This topic is closed to new replies.

Advertisement