SFML community forums

Help => Window => Topic started by: Flash619 on October 27, 2012, 05:58:11 pm

Title: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 27, 2012, 05:58:11 pm
Hello,

     I was wondering if it is possible to declare the sf::Window as a static variable for easier access from other functions? I tried it but I get a nice exception...

Quote
First-chance exception at 0x776AF592 (ntdll.dll) in Genesis.exe: 0xC0000005: Access violation writing location 0x00000004.

         So.. Does this mean that, that is not possible then? Or does it just mean that I dun goofed and broke something?

If you wish to see my code:
WindowManagment.h
#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H
#include "stdafx.h"
class WindowManager{

public:
        static sf::Window renderWindow;

        sf::Window& LaunchRenderWindow();
        sf::Window& SetResolution(short int x,short int y);
        sf::Window& SetFullscreen(bool a);
        sf::Window& GetRenderWindow();
        static void SetVerticalSync(bool a);
        static void ShowCourser(bool a);
        static void SetFramerateLimit(short int a);

private:
        void GetDesktopResolution(short int&X,short int&Y);

};
#endif
 

WindowManagment.cpp
#include "stdafx.h"
#include "WindowManagment.h"
#include "Genesis.h"

sf::Window WindowManager::renderWindow;
/**
Launches the render window.
*/

sf::Window& WindowManager::LaunchRenderWindow()
{
        if(Genesis::Windowed)
        {
                renderWindow.create(sf::VideoMode(Genesis::WindowX,Genesis::WindowY,32),"Genesis");
        }else{
                short int X;
                short int Y;
                GetDesktopResolution(X,Y);
                renderWindow.create(sf::VideoMode(X,Y,32),"Genesis",sf::Style::Fullscreen);
        }
        return renderWindow;
}
/**
Detects desktop resolution and adjusts the X and Y variables accordingly.
*/

void WindowManager::GetDesktopResolution(short int&X,short int&Y)
{
        RECT desktop;
        const HWND hDesktop = GetDesktopWindow();
        GetWindowRect(hDesktop, &desktop);
    X = desktop.right;
    Y = desktop.bottom;
}
/**
Recieves a true or false and enables/disables vertical sync accordingly.
*/

void WindowManager::SetVerticalSync(bool a)
{
        if(a)
                renderWindow.setVerticalSyncEnabled(true);
        else
                renderWindow.setVerticalSyncEnabled(false);
}
/**
Recieves a true or false and enables/disables showing the mouse curser.
*/

void WindowManager::ShowCourser(bool a)
{
        if(a)
                renderWindow.setMouseCursorVisible(true);
        else
                renderWindow.setMouseCursorVisible(false);
}
/**
Recieves a short int and sets the framerate limit to it. Must be between 15 - 60,
*/

void WindowManager::SetFramerateLimit(short int a)
{
        if(a>60)
                a=60;

        if(a<15)
                a=15;

        renderWindow.setFramerateLimit(a);
}
 

kk Thanks a bunch! :D
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: eXpl0it3r on October 27, 2012, 06:11:09 pm
Instead of reimplementing every function you could simply derive from sf::Window and then add your custom functions. Also keep in mind that sf::Window != sf::RenderWindow...

Why do you think accessing the window member would get easier with the static keyword, or better said why doesn't it work with just having it public?
Static class member basically means that multiple instance of the class will have the same window, but you shouldn't have multiple instances of your 'manager' in the first place, so I don't really see where the advantage is. ;)
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 27, 2012, 06:29:33 pm
Instead of reimplementing every function you could simply derive from sf::Window and then add your custom functions. Also keep in mind that sf::Window != sf::RenderWindow...

Why do you think accessing the window member would get easier with the static keyword, or better said why doesn't it work with just having it public?
Static class member basically means that multiple instance of the class will have the same window, but you shouldn't have multiple instances of your 'manager' in the first place, so I don't really see where the advantage is. ;)

Well I cant access the manager from a static function in another class without copying it first because otherwise it causes errors. So I need my functions in my window manager to be static, for those to be static and access the sf::window the window needs to be static as well. At least, that's what it looks like to me. If you know a better way of doing it, I'm all ears.

I mean say for example I have
#include "WindowManagment.h"

void MyOtherClass::MyOtherFunction()//Static function.
{
      sf::Window& Renderwindow= WindowManager.LaunchRenderWindow(); //ERROR Non static member reference must be relative to a specific object.
}
 
It will have that error if the function in WindowManager is not static, and if the sf::window variable is not static, it will practically do the exact same thing with that. Know what I mean? Idealy I want only ONE WindowManagment that stores one render window that is accessible by everything without making duplicates everywhere.

Also, I am not aware that SFML2 has a sf::RenderWindow.

Quote
Instead of reimplementing every function you could simply derive from sf::Window and then add your custom functions.

...???
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 27, 2012, 06:35:26 pm
This borders on bad design patterns like singletons and quasi-global variables.
Quote
Well I cant access the manager from a static function in another class without copying it first because otherwise it causes errors.
Pass pointers to your manager? If it has renderwindow as member then default copy c-tor will fail.
Quote
...???
class Yay : public sf::RenderWindow
{
public:
void SetMagicalFramerate(short gfps);
void blablabla();
//ect.
}
 
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 27, 2012, 06:49:19 pm
This borders on bad design patterns like singletons and quasi-global variables.
......I'm not sure what you are referring to with this.  Then show me what parts are bad, why they are considered bad design patterns, and how they should be "properly" done. Otherwise I'm not going to fully understand it and will likely make the same mistake twice.

Quote
Well I cant access the manager from a static function in another class without copying it first because otherwise it causes errors.
Pass pointers to your manager? If it has renderwindow as member then default copy c-tor will fail.
I have no clue what a "c-tor" is. Pointers? Is that similar to references?

EDIT

I googled pointers in C++. They seem very similar "if not the same thing" of what I did for the "GetDesktopResolution" and the "sf::Window& WindowManager::LaunchRenderWindow()"

But I'm still googling so don't expect me to be right on that.

EDIT

But then... I'm confused -_-' So for every pointer I would pass, I would have to mirror that off of a variable that stores a memory location? That seems like a lot of added variables for passing everything around.

...I'm still looking for a tutorial on passing pointers between functions...

EDIT

So I've come to a halt on pointers because I've come to the realization that I don't even know when I should use one. That and lack of good examples for what I'm actually doing.

Quote
...???
class Yay : public sf::RenderWindow
{
public:
void SetMagicalFramerate(short gfps);
void blablabla();
//ect.
}
 
Yea I see the code, I don't understand how it works. :/ Or how to change it for my needs for that matter.  If I'm going to use code, I would like to know how it works.  *I don't want to just brainlessly copy and paste.*

Just try to understand that my goal of this whole project is to learn. So I may not pick up on all terminology, or understand concepts by just their names.

I am in this whole C++ language to learn it. How it works and everything, but reading books doesn't teach me anything, I learn best with hands on applications and actually seeing progress. I'm not asking to be given anything, I'm just saying that it would help me if you could be as specific as possible because otherwise, I honestly don't know. I want to know, but I don't know.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 27, 2012, 07:21:06 pm
Sorry, I thought you'er passing copies of your manager.
Pointers and inheritance are very powerfull but hard to understand at first.
Basically, inheritance(without anything virtual) is gluing a class or few together with few new functions and variables. Inherited class(without anything virtual) acts exactly like the base class would so can use it just like base. ie. use Yay class as normal render window.
Pointers are used when you don't want to duplicate resources but acess and/or modify them from few places. Ie. sf::Sprites take reference of sf::Texture and store a pointer to it so 100 sprites can use 1 texture.
Quote
First-chance exception at 0x776AF592 (ntdll.dll) in Genesis.exe: 0xC0000005: Access violation writing location 0x00000004.
Which line exactly does this happen on?
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 27, 2012, 07:35:40 pm
Which line exactly does this happen on?

Visual Studio just takes me to a line in "MutexImpl.cpp"
void MutexImpl::lock()
{
    EnterCriticalSection(&m_mutex);
}
 

Quote
   ntdll.dll!776af592()   Unknown
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]   
>   Genesis.exe!sf::priv::MutexImpl::lock() Line 52   C++
    Genesis.exe!sf::Mutex::lock() Line 57   C++
    Genesis.exe!sf::Lock::Lock(sf::Mutex & mutex) Line 39   C++
    Genesis.exe!sf::GlResource::GlResource() Line 49   C++
    Genesis.exe!sf::Window::Window() Line 48   C++
    Genesis.exe!`dynamic initializer for 'WindowManager::renderWindow''() Line 5   C++
    Genesis.exe!_initterm(void (void) * * pfbegin, void (void) * * pfend) Line 894   C
    Genesis.exe!_cinit(int initFloatingPrecision) Line 290   C
    Genesis.exe!__tmainCRTStartup() Line 226   C
    Genesis.exe!mainCRTStartup() Line 164   C
    kernel32.dll!75008543()   Unknown
    ntdll.dll!776bac69()   Unknown
    ntdll.dll!776bac3c()   Unknown

Sorry, I thought you'er passing copies of your manager.

It's ok. :) All I want the manager to do, is store the Window, and then have everything else in the program reference to that variable that is being stored in it. Would that work alright? Or would there be a better way?

The reason I placed all those functions in WindowManager is because I figured if WindowManager was storing the Window itself, it should also be the class that interacts with it.


Basically, inheritance(without anything virtual) is gluing a class or few together with few new functions and variables. Inherited class(without anything virtual) acts exactly like the base class would so can use it just like base. ie. use Yay class as normal render window.
Pointers are used when you don't want to duplicate resources but acess and/or modify them from few places. Ie. sf::Sprites take reference of sf::Texture and store a pointer to it so 100 sprites can use 1 texture.

I understand inheritance somewhat. To what class in your example are things being glues to? Yay? I'm just a little confused as to what really makes it different from just having a class with methods inside of it. *I understand that it is probably a more efficient method but for someone who never used it, ....its just a little confusing. *  ;D

So I was poking around playing with inheritance a little... is this code proper?

void WindowManager::SetFramerateLimit(short int a)
{
        if(a>60)
                a=60;

        if(a<15)
                a=15;
        WindowManager::SetFramerateLimit(a); //I think this means I setup inheritance right.
}
 
^Yes I understand that that is completely useless having that in a function like that but I was just practicing..... If It works I can delete all those excess functions I suppose. But How does it know what to adjust? I mean, sure I can do "WindowManager::SetFramerateLimit(a);" but how does it know what window its setting the framerate limit on?


Pointers make a lot of sense to me, more so than inheritance at this point, but they just seem slightly confusing at first as to how to actually implement them and use them properly. Especially if you start talking about moving pointers between classes.

Also back to Window Managment..... If the sf::Window can't be static... how would I make everything non static and still be able to access it? I seem to always run into this issue, where I have to make one thing static, and then because of that, everything else has to be static because its accessing something that's static, or referencing from something static.... Kinda forcing me to keep using static functions and variables. >_<

Thanks a ton for your help so far by the way. :)
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 27, 2012, 07:50:46 pm
I mean on what line in your code do things crash? At return 0 in main or at start or where?
Also, why do you use a sf::Window instead of sf::RenderWindow, do you plan on using opengl(probably not)?

Quote
I understand inheritance somewhat. To what class in your example are things being glues to? Yay? I'm just a little confused as to what really makes it different from just having a class with methods inside of it.
It's complicated, consider:
class B
{
public:
void SomeB();
};
class C
{
public:
void SomeC();
};
class A : public B, public C
{
public:
void SomeA();
};
Now when you create an A, it has SomeB and SomeC functions in itself and it will suffice in any case where pointer or reference to B or C is required. Inheritance is 'is-a' relationship A is a B(has SomeB method), it's also a C(has someC method), but it's also itself(has own method).
You should read faq to understand c++ http://www.parashift.com/c++-faq/
SFML makes use of inheritance and it'll be much easier to implement own sf::Drawables once you understand the inheritance, virtual methods and read the sfml source code.

void WindowManager::SetFramerateLimit(short int a)
{
    if(a>60)
        a=60;

    if(a<15)
        a=15;
    WindowManager::SetFramerateLimit(a); //I think this means I setup inheritance right.
}
 
This is not right. Without complicating things with covering sfml framerate setter:
class MyWindow : public sf::RenderWindow
{
public:
void SetFramerateLimit(short int a)
{
if(a>60)a=60;
if(a<15)a=15;
setFramerateLimit(a);//use one of methods we inherited from renderwindow, can also write this->setFramerateLimit(a);
}
};
 
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 27, 2012, 08:25:36 pm
I mean on what line in your code do things crash? At return 0 in main or at start or where?
Also, why do you use a sf::Window instead of sf::RenderWindow, do you plan on using opengl(probably not)?

I don't know, it doesn't tell me.
Quote
First-chance exception at 0x776AF592 (ntdll.dll) in Genesis.exe: 0xC0000005: Access violation writing location 0x00000004.
Unhandled exception at 0x776AF592 (ntdll.dll) in Genesis.exe: 0xC0000005: Access violation writing location 0x00000004.
The program '[7120] Genesis.exe' has exited with code 0 (0x0

SFML 2.0 *as far as I know.* Does not contain a sf::Renderwindow.

I mean on what line in your code do things crash? At return 0 in main or at start or where?
Also, why do you use a sf::Window instead of sf::RenderWindow, do you plan on using opengl(probably not)?

class MyWindow : public sf::RenderWindow
{
public:
void SetFramerateLimit(short int a)
{
if(a>60)a=60;
if(a<15)a=15;
setFramerateLimit(a);//use one of methods we inherited from renderwindow, can also write this->setFramerateLimit(a);
}
};
 

But how does "setFramerateLimit" Know which window that it is adjusting the framerate on? We never told it what sf::window it is altering. Does it just guess? That doesn't seem right... I mean sure "MyWindow" has some functions from sf::Window but that doesn't make it a window,..... does it?

Also then how would I go about using a non static function such as......... "void SomeA();" from a static function in another class such as... "static void Initialize();" Do I re create it as an object since I cant directly access it? Thats the only reason why everything was static xD So that I could access it.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 27, 2012, 08:47:54 pm
sfml 2.0 has a render window http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderWindow.php
Quote
I don't know, it doesn't tell me.
Use step over or place breakpoints and trace it?
It might be caused by static window.
Quote
But how does "setFramerateLimit" Know which window that it is adjusting the framerate on? We never told it what sf::window it is altering. Does it just guess? That doesn't seem right...

It's adjusting own framrate because MyWindow is a Window.
Quote
Also then how would I go about using a non static function such as......... "void SomeA();" from a static function in another class such as... "static void Initialize();" Do I re create it as an object since I cant directly access it? Thats the only reason why everything was static xD So that I could access it.
Why do you have to have static things?
You can try making a non static windowmanager and doing that:
class SManager
{
private:
static WindowManager * mptr;
public:
SManager(){mptr = new WindowManager;}
~SManager(){delete mptr;}
static WindowManager& Get(){return *mptr;}
};
and then creating instance of SManager at start of main so there wouldn't be static window.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: cire on October 27, 2012, 09:01:51 pm
One can guess from the stack trace posted earlier that this happens prior to main being invoked, which is when things of static and external storage duration are constructed, so it is very likely some sort of dependency issue on the order of things being constructed (which is supported by the OP's assertion that it only happened after he made the variable static.)  Unfortunately, there is no portable way to order the construction of such objects which is one reason these type of variables should be avoided when possible.

One possible solution is to:

sf::Window& GetWindowInstance()
{
    static sf::Window instance ;
    return instance ;
}
 

And just use that to access your window instance.  The instance isn't constructed until the function is called the first time.  However, any object of static duration which calls this function directly or indirectly during construction could cause the problem to recur.

A better solution is to refactor the code so this isn't necessary.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 27, 2012, 09:06:23 pm
sfml 2.0 has a render window http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderWindow.php

"Namespace "sf" has no member "RenderWindow""

Quote
I don't know, it doesn't tell me.
Use step over or place breakpoints and trace it?
It might be caused by static window.

It WAS caused by the static window. But Now I don't think my return syntax in right. >_<
sf::Window& WindowManager::LaunchRenderWindow()
{
        if(Genesis::Windowed)
        {
                create(sf::VideoMode(Genesis::WindowX,Genesis::WindowY,32),"Genesis");
        }else{
                short int X;
                short int Y;
                GetDesktopResolution(X,Y);
                create(sf::VideoMode(X,Y,32),"Genesis",sf::Style::Fullscreen);
        }
        return * this;
}
 

Quote
But how does "setFramerateLimit" Know which window that it is adjusting the framerate on? We never told it what sf::window it is altering. Does it just guess? That doesn't seem right...

It's adjusting own framrate because MyWindow is a Window.

Oh wow...... that makes it make a lot more sense. xD

Quote
Also then how would I go about using a non static function such as......... "void SomeA();" from a static function in another class such as... "static void Initialize();" Do I re create it as an object since I cant directly access it? Thats the only reason why everything was static xD So that I could access it.
Why do you have to have static things?
You can try making a non static windowmanager and doing that:
class SManager
{
private:
static WindowManager * mptr;
public:
SManager(){mptr = new WindowManager;}
~SManager(){delete mptr;}
static WindowManager& Get(){return *mptr;}
};
and then creating instance of SManager at start of main so there wouldn't be static window.

Because a thing in a static function cant access a non static function without re creating it statically as an object. So I have no clue how to avoid that and fix that, and eventually had to make everything static. >_< I still am not sure how to properly go from static, to regular, and back to static again. Making a new object of everything seemed a bit more tasking on memory than just directly accessing static functions but that may be wrong or backwards, or both.... probably both.

.....What does that code do exactly?
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 27, 2012, 09:10:51 pm
Quote
"Namespace "sf" has no member "RenderWindow""
You didn't include SFML/Graphics.hpp but only Window.hpp...

That code constructs manager in it's constructor and destruct it when it goes out of scope so that your class doesn't get created too soon or destroyed too late and you can access your manger from anywhere by SManager::Get()
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 27, 2012, 09:19:53 pm
Quote
"Namespace "sf" has no member "RenderWindow""
You didn't include SFML/Graphics.hpp but only Window.hpp...
DOH!

That code constructs manager in it's constructor and destruct it when it goes out of scope so that your class doesn't get created too soon or destroyed too late and you can access your manger from anywhere by SManager::Get()

OH wow that makes sense now!!!

So "mptr" is a pointer that points to the SManager class!?

So let me see if I know this code right...
class SManager //Declares a class Smanager
{
private:
static WindowManager * mptr;  //Makes a static WindowManager pointer named mptr
public:
SManager(){mptr = new WindowManager;} //Making a copy of WindowManager and storing it inside of mptr??
~SManager(){delete mptr;}                    //Deconstructing SManager and deleting the pointer?
static WindowManager& Get(){return *mptr;} //Returning the pointer to any function or class that needs to get a hold of SManager
};
 

The question marks show the things I'm slightly confused by.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 27, 2012, 09:29:38 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 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".
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex 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;
}
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 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?
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: cire 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 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?
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 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...
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex 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.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 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(); ?

Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex 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'?
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 12:36:13 am

Also not sure if I'm right but this is my

WindowManagment.cpp
#include "stdafx.h"
#include "WindowManagment.h"
#include "Genesis.h"

/**
Launches the render window.
*/

sf::RenderWindow& WindowManager::LaunchRenderWindow()
{
        Genesis genesis;
        if(genesis.Windowed)
        {
                create(sf::VideoMode(genesis.WindowX,genesis.WindowY,32),"Genesis");
        }else{
                short int X;
                short int Y;
                GetDesktopResolution(X,Y);
                create(sf::VideoMode(X,Y,32),"Genesis",sf::Style::Fullscreen);
        }
        return * this;
}
/**
Detects desktop resolution and adjusts the X and Y variables accordingly.
*/

void WindowManager::GetDesktopResolution(short int&X,short int&Y)
{
        RECT desktop;
        const HWND hDesktop = GetDesktopWindow();
        GetWindowRect(hDesktop, &desktop);
    X = desktop.right;
    Y = desktop.bottom;
}
/**
Recieves a true or false and enables/disables vertical sync accordingly.
*/

void WindowManager::SetVerticalSync(bool a)
{
        if(a)
                setVerticalSyncEnabled(true);
        else
                setVerticalSyncEnabled(false);
}
/**
Recieves a true or false and enables/disables showing the mouse curser.
*/

void WindowManager::ShowCourser(bool a)
{
        if(a)
                setMouseCursorVisible(true);
        else
                setMouseCursorVisible(false);
}
/**
Recieves a short int and sets the framerate limit to it. Must be between 15 - 60,
*/

void WindowManager::SetFramerateLimit(short int a)
{
        if(a>60) a=60;
        if(a<15) a=15;

        setFramerateLimit(a);
}
 

and this is it's header
#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H
#include "stdafx.h"
class WindowManager : sf::RenderWindow{

public:
        sf::RenderWindow& LaunchRenderWindow();
        sf::RenderWindow& GetRenderWindow();

        void SetResolution(short int x,short int y);
        void SetFullscreen(bool a);
        void SetVerticalSync(bool a);
        void ShowCourser(bool a);
    void SetFramerateLimit(short int a);

private:
        void GetDesktopResolution(short int&X,short int&Y);

};
#endif
 

But when I try to launch a window with:

        WindowManager windowManager;
        sf::RenderWindow & renderWindow = windowManager.LaunchRenderWindow();
 

from my Genesis.cpp, I get the following:

Quote
Error   14   error LNK1120: 2 unresolved externals   C:\Users\Travis\Documents\Visual Studio 2012\Projects\Genesis\Debug\Genesis.exe   Genesis
Error   13   error LNK2019: unresolved external symbol __imp__sscanf referenced in function _jinit_memory_mgr   C:\Users\Travis\Documents\Visual Studio 2012\Projects\Genesis\Genesis\sfml-graphics-s-d.lib(jmemmgr.obj)   Genesis
Error   12   error LNK2019: unresolved external symbol __imp__fprintf referenced in function _output_message   C:\Users\Travis\Documents\Visual Studio 2012\Projects\Genesis\Genesis\sfml-graphics-s-d.lib(jerror.obj)   Genesis

....Not sure what I did wrong...
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 12:47:02 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'?

Oh it's ok, I think I figured out a lot of it. XD But now I'm stuck on that unresolved external symbol error, and have no idea whats causing it. >_< 
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 28, 2012, 01:08:56 am
Did you recompile sfml yourself? Did you try linking to dynamic libraries, (get the dlls to your directory and remove -s from names in linker settings). I never ran into that error.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 01:28:47 am
Did you recompile sfml yourself? Did you try linking to dynamic libraries, (get the dlls to your directory and remove -s from names in linker settings). I never ran into that error.

I did compile SFML myself with CMake and Visual Studio 2012.

Oddly enough, I get no error if I use regular sf::Window. But then again, that doesn't really "work"
 It does open a window, but the window is not on the screen anywhere and you cant ever see it. >_> Perhaps something in the code is not right? Maybe Its being deleted or something? Idk I'm just coming up with ideas.

Hm.... I'll go dig out the dynamic libraries and see what happens.....

So it compiles with the non static libraries. o.O; So why wont the static libraries work. ???

Also, my window isn't there. I see it in the task bar, but it never actually pops up, well, at least I never actually see it. I still am not sure why this works only with the dynamic libraries...... >_> Or why my window doesn't actually work, work. Hmm Any ideas for testing out and finding why? I'm all ears. :)
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 28, 2012, 01:35:04 am
Please read the documentation of sfml and tutorials before use..
to display a window you have to call display() on it(very unintuitive, I know..)
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 02:06:26 am
Please read the documentation of sfml and tutorials before use..
to display a window you have to call display() on it(very unintuitive, I know..)

Mhm I even tried:

        WindowManager windowManager;
        sf::RenderWindow& renderWindow = windowManager.LaunchRenderWindow();

        while(!isClosing())
        {
                renderWindow.display();
        }
 

No visible window. It opens, but nothing is actually there, no window. I see it in the task bar, that's all.

I still am also concerned as to why the static libraries don't work. I think whatever caused those libraries not to work, and my window to glitch out, is related.

I've started to trace the cause of the static library error so far if I remove

WindowManager windowManager;
 

I can compile without error. I cant open a window mind you, but I can compile without error.

Also I found that if I don't remove the code, but empty out the WindowManager class completely, I still get the error.

The error is that

WindowManager windowManager;
 
Is making a new object of a "sf::RenderWindow" "because WindowManager was declared with " : sf::Renderwindow"" and that is whats throwing the error.  Now the question, ....why is it throwing the error? *goes back to digging through code*

So I still don't know why but I've found that it IS the combination of

class WindowManager : sf::RenderWindow

and

WindowManager windowManager;

If I change either of them the problem goes away. But that doesnt actually fix the problem. hmm..

Please tell me if I'm wrong but if I remember right the sf::RenderWindow is non copyable and wouldn't turning it into a object.....be making a copy of it since that object is "glued" to the sf::RenderWindow?

It may also be worth mentioning that sf::Window also does the same thing, and doesn't actually show up when Display is called. "Thats right sf::window doesn't throw the error :D .....still doesn't work, but it doesnt throw a error with the static libraries, but I still am clueless as to why my window wont show...."
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: masskiller on October 28, 2012, 02:35:00 am
If you are having that much trouble due to trying to make a static window class handle everything when you don't have a good understanding on how to use the window normally then start with some basic window handling and then move on to your fancy class, wrappers around that kind of stuff tend to be very tricky to use (I am making an openGL with shaders wrapper that is taking far longer than I thought it would take)...

Not to mention that using a window in SFML is quite easy, you just need to know it's correct syntax and it should go smooth.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 02:59:51 am
If you are having that much trouble due to trying to make a static window class handle everything when you don't have a good understanding on how to use the window normally then start with some basic window handling and then move on to your fancy class, wrappers around that kind of stuff tend to be very tricky to use (I am making an openGL with shaders wrapper that is taking far longer than I thought it would take)...

Not to mention that using a window in SFML is quite easy, you just need to know it's correct syntax and it should go smooth.

But this should work the way I have it setup though. *also the window class is not static, I went away from static stuff because it seemed unstable. ;)

In  fact. I'll try making a quickie window in the main class.

*5min later*

As I thought, normally it works just fine...

So It would appear my issue is the way in which I'm referencing from my WindowManager class..... but I took 70% of that code directly from a working example I made a few months back >_< Anything you see that looks kinda wrong, or "ify" ? To me it seems like the window may be being created, but immediately destroyed when it leaves that class. But if I make it static ..... *sigh
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: masskiller on October 28, 2012, 03:24:23 pm
Quote
So It would appear my issue is the way in which I'm referencing from my WindowManager class..... but I took 70% of that code directly from a working example I made a few months back >_< Anything you see that looks kinda wrong, or "ify" ? To me it seems like the window may be being created, but immediately destroyed when it leaves that class. But if I make it static ..... *sigh

So you actually made it work in the past, but now it doesn't work?
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 03:31:53 pm
Quote
So It would appear my issue is the way in which I'm referencing from my WindowManager class..... but I took 70% of that code directly from a working example I made a few months back >_< Anything you see that looks kinda wrong, or "ify" ? To me it seems like the window may be being created, but immediately destroyed when it leaves that class. But if I make it static ..... *sigh

So you actually made it work in the past, but now it doesn't work?

Well yea, only this time I inherited the whole WindowManager with sf::Window.

So I wouldn't have to use references when making the window, to make the window I could just use:

eWindow.Launch();

Which uses

//Header file
#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H
#include "stdafx.h"
class EngineWindow : public sf::Window
{
public:
        void Launch();
};
#endif

//Class File
void EngineWindow::Launch()
{
        Log consoleLog;
        consoleLog.info("Launching Window...");
        Genesis genesis;
        if(genesis.Windowed)
        {
                create(sf::VideoMode(genesis.WindowX,genesis.WindowY,32),"Genesis");
        }else{
                short int X;
                short int Y;
                GetDesktopResolution(X,Y);
                create(sf::VideoMode(X,Y,32),"Genesis",sf::Style::Fullscreen);
        }
}
 

Inside the EngineWindow class *I renamed WindowManager to EngineWindow*

Then display it with:

       
while(true)
        {
        eWindow.display();
        }

But it never displayed because I think its being de allocated and destroyed for whatever reason, its not persisting. :/ I mean, what I have should work, shouldn't it?
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 04:49:30 pm
And now I'm back to the

Quote
First-chance exception at 0x776AF592 (ntdll.dll) in Genesis.exe: 0xC0000005: Access violation writing location 0x00000004.
The program '[8284] Genesis.exe' has exited with code 0 (0x0).

Error. I even reverted back ALL of my code to when it at least started up this morning, and I STILL have that error after undoing everything. So it should work, everything should work, but it doesn't. Amd oh, I'm sure it will probably work with the dynamic libraries, but I want to know why the static ones throw such a fit over everything when they were working two hours ago.

Oh, and ONCE again, the error is caused by making my WindowManager into a object like:

WindowManager windowManager;

and BAM exception.

This to me indicates for whatever reason:

class EngineWindow : public sf::Window

Does not want to be made into a object, specifically the " : public sf::Window" part.

Ok and now, figure out why this works.

        EngineWindow eWindow;
        eWindow.Launch();
 

I literally deleted, the re wrote that code, and now it works. I still don't have a window, but it doesn't throw an error.

HAHAHAha

And now visual studio is telling me

Quote
Error   1   error LNK1168: cannot open C:\Users\Travis\documents\visual studio 2012\Projects\Genesis\Debug\Genesis.exe for writing   C:\Users\Travis\documents\visual studio 2012\Projects\Genesis\Genesis\LINK   Genesis

Now I just tried it again *after getting that error 5 times in a row* and it worked without me changing anything.

Any ideas on that one? Or on just how unstable everything is all together?

ALSO, I am having an issue. I can define "Genesis::Windowed=false;" and everything in the Genesis class will read it as false "most of the time" but all other classes that make it a object read it as true. ???
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 28, 2012, 05:02:27 pm
That error means that you have your app open.
We can't know why things are unstable because you post one hundredth of your code every time..
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 05:19:22 pm
That error means that you have your app open.
We can't know why things are unstable because you post one hundredth of your code every time..

Ok, Here is some code. :P *you could have asked for more....*

I got my window working.

Turns out that the value wasn't being carried between objects for

   
 bool Windowed; //Located in Genesis.h

So I changed it to:

   
 static bool Windowed; //Located in Genesis.h

and it worked but now I have a issue........ it launches in fullscreen........... all of the time. XD

Though, I would love to have a second person look at some of this code. >.>

WindowManagment.h
#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H
#include "stdafx.h"
class EngineWindow : public sf::Window
{
public:
        void Launch();
        sf::Window& GetRenderWindow();

        void SetResolution(short int x,short int y);
        void SetFullscreen(bool a);
        void SetVerticalSync(bool a);
        void ShowCourser(bool a);
    void SetFramerateLimit(short int a);

private:
        void GetDesktopResolution(short int&X,short int&Y);
       
};
#endif
 

WindowManagment.cpp

#include "stdafx.h"
#include "WindowManagment.h"
#include "Genesis.h"
#include "ConsoleUtils.h"
/**
Launches the render window.
*/

void EngineWindow::Launch()
{
        Log consoleLog;
        consoleLog.info("Launching Window...");
        Genesis genesis;
        if(genesis.Windowed)
        {
                consoleLog.info("Windowed Mode Detected");
                create(sf::VideoMode(genesis.WindowX,genesis.WindowY,32),"Genesis");
        }else{
                consoleLog.info("Going Fullscreen");
                short int X;
                short int Y;
                GetDesktopResolution(X,Y);
                create(sf::VideoMode(X,Y,32),"Genesis",sf::Style::Fullscreen);
        }
}
/**
Detects desktop resolution and adjusts the X and Y variables accordingly.
*/

void EngineWindow::GetDesktopResolution(short int&X,short int&Y)
{
        RECT desktop;
        const HWND hDesktop = GetDesktopWindow();
        GetWindowRect(hDesktop, &desktop);
    X = desktop.right;
    Y = desktop.bottom;
}
/**
Recieves a true or false and enables/disables vertical sync accordingly.
*/

void EngineWindow::SetVerticalSync(bool a)
{
        if(a)
                setVerticalSyncEnabled(true);
        else
                setVerticalSyncEnabled(false);
}
/**
Recieves a true or false and enables/disables showing the mouse curser.
*/

void EngineWindow::ShowCourser(bool a)
{
        if(a)
                setMouseCursorVisible(true);
        else
                setMouseCursorVisible(false);
}
/**
Recieves a short int and sets the framerate limit to it. Must be between 15 - 60,
*/

void EngineWindow::SetFramerateLimit(short int a)
{
        if(a>60) a=60;
        if(a<15) a=15;

        setFramerateLimit(a);
}
 

Genesis.h
#ifndef GENESIS_H
#define GENESIS_H
#include <string>
class Genesis{

public:
        //Command Line Arguments
         std::string flags;
         static bool SkipSplash;
         static bool Windowed;
         static bool Debug;
         static bool NoConsoleTags;
         bool WaitBeforeExit;

        short int WindowX;
        short int WindowY;

        //Core Engine States
        enum CoreEngineState{Initializing,ShowingSplash,ShowingMenuScreen,Playing,Reloading,Closing};
         CoreEngineState engineCoreState;

    //Public Functions
        void Initialize(int argc,char *argv[]);
    void ArgumentHandler(int argsl,char *args[]);

    std::string GetVersion(void);
        bool isClosing();

private:
        void ValidateFlags();
        void ClassDistributor();
};
#endif
 

Genesis.cpp
/*
Genesis and its resources are copyright of PredawnStudios LLC (C)2012 PredawnStudios.
SFML - Copyright (c) 2007-2008 Laurent Gomila
*/


// Genesis.cpp : main project file.

#include "stdafx.h"
#include "Genesis.h"
#include "ConsoleUtils.h"
#include "WindowManagment.h"

using namespace std;

//Make used objects
Font font;
Log ConsoleLog;
Validate validate;

bool Genesis::Windowed;
bool Genesis::SkipSplash;
bool Genesis::Debug;
bool Genesis::NoConsoleTags;

//Declaring our version.
const std::string GenesisVersion = "2.0.0 Pre Alpha";

/**
The main will recieve our command line arguments and bee the starting point for
the engine.
*/

int main(int argc,char *argv[])
{
        Genesis::CoreEngineState engineCoreState = Genesis::Initializing;

        const int argcc = argc;
        Genesis genesis;

        genesis.Initialize(argc,argv);
    return 0;
}
/**
This function will be our main tree for controling core engine specific paths.
*/

void Genesis::Initialize(int argc,char *argv[])
{
        //Print Genesis Command Line Header And Version
        cout << "  @@@@@@@  @@@@@@@@ @@@  @@@ @@@@@@@@  @@@@@@ @@@  @@@@@@" << endl;
        cout << " !@@       @@!      @@!@!@@@ @@!      !@@     @@! !@@    " << endl;
        cout << " !@! @!@!@ @!!!:!   @!@@!!@! @!!!:!    !@@!!  !!@  !@@!! " << endl;
        cout << " :!!   !!: !!:      !!:  !!! !!:          !:! !!:     !:!" << endl;
        cout << "  :: :: :  : :: ::: ::    :  : :: ::: ::.: :  :   ::.: : " << endl;
        cout << endl;
        font.set_color(15);
    cout <<"Version " << Genesis::GetVersion() << endl;
    font.set_color(7);
        cout << endl;
        //End Header

        //Set Flag Defaults
        Genesis::SkipSplash=false;
        Genesis::Windowed=false;
        Genesis::Debug=false;
        Genesis::NoConsoleTags=false;
        Genesis::WaitBeforeExit=false;

        short int WindowX=0;
        short int WindowY=0;

       
        if(argc>=2)
        {
                ConsoleLog.info("Command Line Arguments Detected...");
                Genesis::ArgumentHandler(argc,argv);           
        }else{
                ConsoleLog.info("No arguments found. Launching engine.");
        }

        //Validate Command Line Flags
        ValidateFlags();

        //We will make a render window but not automatically display it.
        ConsoleLog.info("Initializing Window System");
        EngineWindow eWindow;
        eWindow.Launch();

        //Ask for input so they can see what all occured if flag -wait set.
        if(Genesis::WaitBeforeExit)
        {
                cout << "Press enter to close..." << endl;
                cin.get();
        }
}
void Genesis::ClassDistributor()
{
        /*switch(CoreEngineState){
        case Genesis::Initializing:
                Genesis::CoreEngineState = Genesis::ShowingSplash;
                break;
        };*/

}
/**
Argument Handler will recieve the arguments all at once, and flip the proper
switches based on the argument. It also returns false if the argument was not
found in its database.
@arg int The Ammount of arguments being recieved.
@arg char* The array of arguments to be recieved.
*/

void Genesis::ArgumentHandler(int argsl,char *args[])
{
        for(int i=1;i<argsl;i++)
        {
                int j = 0;
                bool ProperArgument=false;
                std::string arg = args[i];

                std::string at = "Attempting to parse: \"" + arg + "\"";
                ConsoleLog.info(at);

                if(arg=="-SI")
                {
                        Genesis::SkipSplash = true;
                        ProperArgument = true;
                }
                if(arg=="-WM")
                {
                        Genesis::Windowed = true;
                        ProperArgument = true;
                }
                if(arg=="-DB")
                {
                        Genesis::Debug = true;
                        ProperArgument = true;
                }
                if(arg=="-nct")
                {
                        Genesis::NoConsoleTags = true;
                        ProperArgument = true;
                }
                if(arg=="-wait")
                {
                        Genesis::WaitBeforeExit = true;
                        ProperArgument = true;
                }
                if(arg=="-X")
                {
                        j = i;
                        j++;
                        if(validate.Digits(args[j]))
                        {
                                std::string k = args[j];
                                Genesis::WindowX = atoi(k.c_str());
                                ProperArgument = true;
                                i=j;
                                std::string m = "Windows \"-X\" value set to: " + k;
                                ConsoleLog.info(m);
                        }else{
                                ConsoleLog.error("Inproper argument for -X only INT is allowed!");
                                Genesis::WindowX = 0;
                                ProperArgument = false;
                                i=j;
                        }
                }
                if(arg=="-Y")
                {
                        j = i;
                        j++;
                        if(validate.Digits(args[j]))
                        {
                                std::string k = args[j];
                                Genesis::WindowY = atoi(k.c_str());
                                ProperArgument = true;
                                i=j;
                                std::string m = "Windows \"-Y\" value set to: " + k;
                                ConsoleLog.info(m);
                        }else{
                                ConsoleLog.error("Inproper argument for -Y only INT is allowed!");
                                Genesis::WindowY = 0;
                                ProperArgument = false;
                                i=j;
                        }
                }
                if(ProperArgument==true){
                        std::string a = "Succesfully Recognized: \""+arg+"\"";
                        ConsoleLog.info(a);
                }else{
                        std::string b = "Succesfully Recognized: "+arg;
                        ConsoleLog.error("Not a valid argument! \""+arg+"\"");
                }
        }

}
/**
If flags are dependant on other flags, checks should be added here!
*/

void Genesis::ValidateFlags()
{
        if((Genesis::WindowX > 0 && Genesis::WindowY <= 0) || (Genesis::WindowY > 0 && Genesis::WindowX <= 0)) //Checks to make sure both X and Y are correct.
        {
                ConsoleLog.error("\"-X\" and \"-Y\" must be used at the same time, have a value greater than 0, and be proper integers!");
                ConsoleLog.error("\"-X\" and \"-Y\" will be ignored.");
                if(Windowed)
                {
                        ConsoleLog.error("\"-WM\" Was dected but due to argument errors will be disabled.");
                        Windowed = false;
                }
        }
        if((Genesis::WindowX>0)&&(Genesis::WindowY>0)&&(Genesis::Windowed=false)) //Makes sure that windowed mode is enabled when X and Y are correct.
        {
                ConsoleLog.error("\"-X\" and \"-Y\" were declared but windowed mode was never set! Did you forget a \"-WM\" ?");
                ConsoleLog.error("\"-X\" and \"-Y\" will be ignored.");
        }
}
/**
Returns the defined version of Genesis running.
*/

std::string Genesis::GetVersion()
{
        return GenesisVersion;
}
bool Genesis::isClosing()
{
        if(Genesis::engineCoreState==Closing)
                return true;
        else
                return false;
}
 

There, that should be enough code for a while. ^^

Let me know your input tho. I want to do things PROPERLY. XD If someone seems very wrong, its likely I just didn't know how to do it right. ;) Just know, some parts are purposely torn apart right now in attempt to locate problems.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 28, 2012, 05:35:50 pm
Globals, statics, what's going on ???
..you realize that classes can have other classes as members of themselves?
You are supposed to add genesis to engine as member, not keep recreating it.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 05:45:36 pm
Globals, statics, what's going on ???
..you realize that classes can have other classes as members of themselves?
You are supposed to add genesis to engine as member, not keep recreating it.

Well see, that's something I honestly didn't know! XD How do you make something a member of something else then? Sounds like it would be easy. >.>

Globals? Oh you are refering to....
bool Genesis::Windowed;
bool Genesis::SkipSplash;
bool Genesis::Debug;
bool Genesis::NoConsoleTags;
 
I assume?

I have to declare them in the file otherwise I will get unresolved externals. They also have to be on the global scope because otherwise you get "blah blah blah cannot be defined in the current scope." Basically they are just flags for command line parameters.

I made those static in attempt to fix my window error, but now its just full screen 100% of the time >_< Even when -WM is defined.

But I am interested in hearing more about this member thing. I don't think I remember that in java exactly.... So I look forward to learning. ^^ Sounds kinda similar to Inheritance.... I'm also going to take a wild guess and say that it not being a member is why nothing is getting the proper values stored in those flags unless they are static.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 28, 2012, 06:03:36 pm
You can treat your classes like normal types and just put variable of your class type into your other class somewhere.
#include "B.h"
#include "C.h"
class A
{
private:
B my_b;
public:
C myPublicC;
}
That's the complete basics of c++ that you're missing.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 06:05:30 pm
You can treat your classes like normal types and just put variable of your class type into your other class somewhere.
#include "B.h"
#include "C.h"
class A
{
private:
B my_b;
public:
C myPublicC;
}
That's the complete basics of c++ that you're missing.
So then.... Is B a class, and my_b refers to that class?

Aslo, what would be the difference between having that reference in private or public? It seems kinda odd I mean, another class wouldn't go to that class to get a reference member of the other class would it?

Also I just tried to change it in my ConsoleUtils..... It didn't work. It compiles. But it still does not get the proper variable... -nct is set to True, but it reads it as false.....

ConsoleUtils.h
#ifndef CONSOLEUTILS_H
#define CONSOLEUTILS_H
#include <string>
#include "Genesis.h"
//Debug Console font Modifications
class Font{
public:
    void set_color(short int a);
};
//A reference class for modifying, and adding things to the debuc console.
class Log{
public:
        Genesis genesis;
        void info(std::string a);
        void error(std::string a);
    void critical(std::string a);
};
class Validate{
public:
    bool Digits(const std::string& a);
};
#endif
 

Would it be possible, for you to show me a simple example of two files, with two different classes, say.... class A needs to get a bool variable from class B, but its in another file. How does it get that? Because apparently something I'm doing is wrong because I never can get the right data from my Genesis class.
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: FRex on October 28, 2012, 06:11:14 pm
B is the CLASS, my_B is object or instance of that class, it's independent of other objects of class B apart for static variables that all instances of one class share. Declared like this it will be constructed just before instance of A and live as long as that instance of A does. Go read some c++ tutorial, really.
http://www.parashift.com/c++-faq/overview-class.html
http://www.parashift.com/c++-faq/overview-object.html
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 06:14:38 pm
B is the CLASS, my_B is object or instance of that class, it's independent of other objects of class B apart for static variables that all instances of one class share. Declared like this it will be constructed just before instance of A and live as long as that instance of A does. Go read some c++ tutorial, really.
http://www.parashift.com/c++-faq/overview-class.html
http://www.parashift.com/c++-faq/overview-object.html

So you're saying if I want console utils to get

"NoConsoleTags"

From Genesis, that "NoConsoleTags" will have to be static?

Also thank you for the tutorial links...... I unfortunettely don't think I will pick up any knowledge from them. I mean sure I can read them, but whether I actually retain anything I'm reading is another story. I learn best from practical application. I mean, unless it actually says "This is how you send variables to other classes that are in other files" It's likely I wont know that that is what its for.

Its just frustrating right now because what I want, is really really simple, but I feel like you might be miss understanding what I want. Can it really be, that complicated to do this:

//MyFile
// .h
MyClass{

     bool MyVariable;

}
// .cpp
bool MyClass::MyVariable=false;
/*something in the class would happen and change that variable to true*/
/*It would then go to MyFile2 to do something else*/
//MyFile2
// .h
MyClass2{
#include MyClass;

MyClass myClass;

}
//.cpp
/*Some stuff happens in here, that gets that bool from MyClass, but it STILL RETAINS ITS VALUE of being true, and not its default of being false

It seems like it should be extremely easy stuff..... In fact... I'm pretty sure every program ever made does this at some point... >.>
Title: Re: Static Window? "static sf::Window renderWindow;"
Post by: Flash619 on October 28, 2012, 06:57:45 pm
Just thought I would say, I have everything fixed! :D

The problem had nothing to do with how I was referencing my instance well it did. But here were the issues

1, I needed my variables to be static so they would share data across instances.

2,
        if((Genesis::WindowX>0)&&(Genesis::WindowY>0)&&(Genesis::Windowed=false))

Should have been

        if((Genesis::WindowX>0)&&(Genesis::WindowY>0)&&(Genesis::Windowed==false))

^^ That explains why it always appeared at default.

And my X/Y short ints also had to be made static.

Thank you for your generous help. It is greatly appreciated. Kudos to you!