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

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

0 Members and 1 Guest are viewing this topic.

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Static Window? "static sf::Window renderWindow;"
« 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
« Last Edit: October 27, 2012, 06:02:10 pm by Flash619 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Static Window? "static sf::Window renderWindow;"
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #2 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.

...???
« Last Edit: October 27, 2012, 06:31:29 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 #3 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.
}
 
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #4 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.
« Last Edit: October 27, 2012, 07:18:50 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 #5 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?
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #6 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. :)
« Last Edit: October 27, 2012, 07:43:44 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 #7 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);
}
};
 
« Last Edit: October 27, 2012, 07:54:56 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #8 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.
« Last Edit: October 27, 2012, 08:41:49 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 #9 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.
Back to C++ gamedev with SFML in May 2023

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #10 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.

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #11 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?

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 #12 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()
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Static Window? "static sf::Window renderWindow;"
« Reply #13 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.

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 #14 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.
Back to C++ gamedev with SFML in May 2023