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

Author Topic: Static Window? "static sf::Window renderWindow;"  (Read 28118 times)

0 Members and 1 Guest are viewing this topic.

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #15 on: October 27, 2012, 09:41:26 pm »
Do you not know what new and delete are too?
New allocates item on heap or 'freestore' that means that you must later get rid of it, application doesn't care about deleting things you requested with new, delete removes something constructed by new. They are equivalents to malloc and free from C but should not be mixed with them, because new and delete will call constructors and destructors properly and C functions won't.

And SManager is accessible by SManager:: because it has static function, and this static function will return reference to WindowManager. It's still not good idea to use static things but if you have to then do that or use the cire's function.

I recognized "new" from java and was just making an assumption.

Well then in example. I'm trying to convert all the static in my program to non static *because its better I suppose....* So I'm trying to find a way to do this but have a lot of issues leaving my "main" function.

I try to do something like....

int main(int argc, char *argv[])
{
     Genesis::flags = argv;
     Genesis::Initialize();
     return 0;
}
//Both declared in a header file in the Genesis class under public as
char flags; //Ok now this I know is not a proper array but this is just an example.
void Initialize();
 

Both of those throw "A non static member reference must be relative to a specific object"

So doesn't that make it impossible to leave the "main" without declaring them static? And then if they are static, doesn't everything else have to be in order to avoid that same error? How would you go about doing this, whats the proper way?

I'm fairly sure I would have to make it a object first. But....... Then that to me makes no sense on how it would be "better".
« Last Edit: October 27, 2012, 09:49:13 pm by Flash619 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #16 on: October 27, 2012, 09:51:21 pm »
Quote
I recognized "new" from java and was just making an assumption.
That's kind of ok, I don't know Java, read this:
http://www.stroustrup.com/bs_faq2.html#new-java

Do you not know you have to have instances of objects to use them?
int main(int argc, char *argv[])
{
Genesis myInstanceOf;
myInstanceOf.flags=argv;
myInstanceOf.Initialize();
return 0;
}
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #17 on: October 27, 2012, 09:57:02 pm »
Quote
I recognized "new" from java and was just making an assumption.
That's kind of ok, I don't know Java, read this:
http://www.stroustrup.com/bs_faq2.html#new-java

Do you not know you have to have instances of objects to use them?
int main(int argc, char *argv[])
{
Genesis myInstanceOf;
myInstanceOf.flags=argv;
myInstanceOf.Initialize();
return 0;
}

But then if everything is non static will I have to make a object for EVERY class in my program like WindowManager, and everything else!?! I mean. You say not to have it static, but it seems like everything has to have a instance re made if its not static. >_< So then how do you know when something should be static and when it shouldn't?

It seems as if I'll have to re make ever class as an object before I am able to actually use it, ....doesn't that defeat some purpose of something?

Also, if I re make everything as a object, that makes it impossible to get variables from that object because every time it makes a new one, wouldn't it have the default variable values only?
« Last Edit: October 27, 2012, 10:01:03 pm by Flash619 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #18 on: October 27, 2012, 10:00:53 pm »
What do you mean 'remake', it's not like having a class that is not static and making one instance of it costs more than static class does.
Statics/singletons/globals are bad because their initialization order is not known, they live untill application quits, they introduce global state into the application, they overly couple things together, make reuse and testing specific parts harder ect.
Back to C++ gamedev with SFML in May 2023

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #19 on: October 27, 2012, 10:02:08 pm »
Quote
It seems as if I'll have to re make ever class as an object before I am able to actually use it, ....doesn't that defeat some purpose of something?

You have to have an object to use an object.  If all I have is the idea of a ball, it's going to be hard to play catch.

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #20 on: October 27, 2012, 10:06:09 pm »
What do you mean 'remake', it's not like having a class that is not static and making one instance of it costs more than static class does.
Statics/singletons/globals are bad because their initialization order is not known, they live untill application quits, they introduce global state into the application, they overly couple things together, make reuse and testing specific parts harder ect.

But of I re make everything as a object, that makes it impossible to get variables from that object because every time it makes a new one, wouldn't it have the default variable values only?

Say Genesis changed a variable when it loaded inside of its class. Then WindowManager re makes Genesis into a object to access that variable. Wouldn't get the default value and not the updated one since it re made the class into an object? Or do objects store the updated values as well and not the blank ones?

Like say it we declared

bool Windowed = false;

But then a method changed it:

Windowed = true;

Then after that the WindowManager re creates the class into an object to access it. Does Windowed in that object equal false, or true?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #21 on: October 27, 2012, 10:17:32 pm »
Pass around and store references or pointers to your objects. So if anything needs your Genesis object it must be given pointer or reference to it in constructor or some function or something.
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #22 on: October 27, 2012, 10:32:37 pm »
Pass around and store references or pointers to your objects. So if anything needs your Genesis object it must be given pointer or reference to it in constructor or some function or something.

..........I'm confuse...Pass around and store references or pointers to your objects.  ???  Genesis object it must be given pointer or reference to it in constructor or some function or something. ??? .....i'm lost. but... then what holds the variable?

Quote
So if anything needs your Genesis object

Well wont everything need it since it's the main class and not static? I mean. Everything that accesses anything from it will have to make a object of it.... >_> at least... I think that's right.
« Last Edit: October 27, 2012, 10:37:18 pm by Flash619 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #23 on: October 27, 2012, 10:37:11 pm »
If something would before use Genesis:: now it has to have a member variable Genesis * mGenesis; and must have a function like GiveGenesis(Genesis& instance){mGenesis=&instance;} or take it as parameter in it's constructor.
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #24 on: October 27, 2012, 10:41:02 pm »
If something would before use Genesis:: now it has to have a member variable Genesis * mGenesis; and must have a function like GiveGenesis(Genesis& instance){mGenesis=&instance;} or take it as parameter in it's constructor.

Well wont everything need it since it's the main class and not static? I mean. Everything that accesses anything from it will have to make a object of it.... >_> at least... I think that's right.

Quote
and must have a function like GiveGenesis(Genesis& instance){mGenesis=&instance;} or take it as parameter in it's constructor.

The thing accessing Genesis needs that? Or.............. Genesis itself does? Also. Really rudimentary dumb question of the day.......... I'm not sure if I have any constructors...... anywhere.

What does "GiveGenesis" Do? Are we talking about accessing a variable from genesis or giving it one? Also what do you mean by "(Genesis& instance){mGenesis=&instance;}" I see it but once again, I don't really know whats going on...

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #25 on: October 27, 2012, 10:53:56 pm »
GiveGenesis tells a class that needs genesis, that if it needs an instance of genesis it can use this particular one at this particular memory adress.
If there are no constructors or destructors declared then the compiler creates default ones itself.
You probably shouldn't make class that is needed by everything else.
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #26 on: October 27, 2012, 10:59:07 pm »
GiveGenesis tells a class that needs genesis, that if it needs an instance of genesis it can use this particular one at this particular memory adress.
If there are no constructors or destructors declared then the compiler creates default ones itself.
You probably shouldn't make class that is needed by everything else.

So.... now your saying I shouldn't make classes. This is not making any sense the way you are putting things. How can I not have a "class that is used by everything else". If I have a class called Genesis, and that class controls what is going on in the whole program, and many things access it for variables........thats wrong is what your saying and I shouldn't have that?!

So today so far I have.... more or less made no progress what so ever and am now completely confused and not even knowing what to do at this point. So if you could please tell me exactly what I should be doing, that would be awfully helpful. I've been trying to re do all, ALL, of my code to switch things to not use statics, and on top of that am trying to fix things, and trying to figure out how to make things work between objects, and while trying to learn how to do all of this am basically getting confused at every left turn because it seems another "you shouldn't do this but should do that" comes along. Not that I'm against doing things the right way. But It just gets a bit overwhelming when you had a working program, that is now reduced to a lump of unstable code because you are trying to re do things so it works better, and in the mists of that, loose track of how your even going to do it.

Know what I spent the last 45min on and still have no clue how to get working?

Simple, I'm trying to receive my arguments, "char *argv[]), and convert that char to something that I can then forward to another class. To you, that would probably take about 5 seconds and extremely easy and basic but do understand that I am NEW to this. I'm trying my best to learn all of this, but I have very little experience with these items to go off of. I still have no clue how to actually store variables with objects and have it retain its info. >_< This day in my eyes, has been a successful failure. Successful in the fact that I know the right ways of doing things, a failure in the way that nothing has been accomplished and I am more lost in C++ now than I ever was.
« Last Edit: October 27, 2012, 11:05:20 pm by Flash619 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #27 on: October 27, 2012, 11:16:16 pm »
There shouldn't be one class that does everything and is needed by everyone. I myself have a core class that has a stack of polymorphic game state classes and calls virtual methods run and render on the class that is on top of the stack. Game states have pointers to core so they can call for their own removal and they keep their own resources, textures, whatnot with themselves.
Quote
Simple, I'm trying to receive my arguments, "char *argv[]), and convert that char to something that I can then forward to another class. To you, that would probably take about 5 seconds and extremely easy and basic but do understand that I am NEW to this. I'm trying my best to learn all of this, but I have very little experience with these items to go off of. I still have no clue how to actually store variables with objects and have it retain its info. >_< This day in my eyes, has been a successful failure. Successful in the fact that I know the right ways of doing things, a failure in the way that nothing has been accomplished and I am more lost in C++ now than I ever was.
What do you want them to convert to? argv is something that came from C. :)
Can you provide example of what you want to do? "store variables with objects and have it retain its info" doesn't say much.
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #28 on: October 27, 2012, 11:27:44 pm »
There shouldn't be one class that does everything and is needed by everyone. I myself have a core class that has a stack of polymorphic game state classes and calls virtual methods run and render on the class that is on top of the stack. Game states have pointers to core so they can call for their own removal and they keep their own resources, textures, whatnot with themselves.
Quote
Simple, I'm trying to receive my arguments, "char *argv[]), and convert that char to something that I can then forward to another class. To you, that would probably take about 5 seconds and extremely easy and basic but do understand that I am NEW to this. I'm trying my best to learn all of this, but I have very little experience with these items to go off of. I still have no clue how to actually store variables with objects and have it retain its info. >_< This day in my eyes, has been a successful failure. Successful in the fact that I know the right ways of doing things, a failure in the way that nothing has been accomplished and I am more lost in C++ now than I ever was.
What do you want them to convert to? argv is something that came from C. :)
Can you provide example of what you want to do? "store variables with objects and have it retain its info" doesn't say much.

Well first, I have a question about the static stuff. You say to keep from using static functions/variables, correct?

Well a few months back I was following this tutorial:
http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition-Part-2.aspx

And I always used static since then because it was what I was taught to use in the tutorial.... Is what he did wrong? I'm basically making my engine off of what I learned from his, so while its a bit different, it generally has similar things. So I'm trying to do it without the static stuff, and am running into troubles because I've never done it this way. ^^;;

I basically have a main class called Genesis, that runs the backbone of the program. It handles command line arguments, sets global flags for other things to access, and has a main loop with a switch statement based off of the engine core state, that then, based on the state, goes to other functions that open different classes.

for instance if the state is EngineCoreState ShowingSplash; then it goes through the loop, catches in the correct part of the switch statement, then goes to a method called "ShowSplash" that then turns the SplashScreen class into a object "SplashScreen splashScreen" and runs it "splashScreen.ShowSplash(sf::RenderWindow& renderWindow);

Is that correct? I just worry that things that need to access global variables such as WindowManager wont be able to. Window manager has to check to see if the Fullscreen flag is set. Fullscreen is a bool in Genesis. So if it makes a object out of Genesis, it wont get the updated value, it will get the default, which is false.

See what I mean? I don't know how I would properly set that.

As for argv..... I imagine I could just change the Initialize function to accept int argc and char *argv[]
After all, I already have a function in Genesis that handles the arguments and iterates through the list of them setting the proper variables. ^^

Also, do... I really have to make a object of Genesis while in Genesis.cpp? That seems kinda... odd. xD Or do I only have to make a object in the "main" and then run something like genesis.Initialize(); ?

« Last Edit: October 27, 2012, 11:29:32 pm by Flash619 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #29 on: October 28, 2012, 12:33:17 am »
Quote
Also, do... I really have to make a object of Genesis while in Genesis.cpp? That seems kinda... odd. xD Or do I only have to make a object in the "main" and then run something like genesis.Initialize(); ?
What does seem 'odd'?
Back to C++ gamedev with SFML in May 2023