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

Author Topic: Can't save and load ContextSettings and WindowHandle  (Read 1329 times)

0 Members and 1 Guest are viewing this topic.

Ardeshir81

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Can't save and load ContextSettings and WindowHandle
« on: October 22, 2013, 12:51:21 am »
HI !
I'm beginner and trying to write a simple app which creates a basic window based on properties the user enters .
Now I want to try to save the current state and load it later , thus I'm using a struct :
struct SLInfo
{
    sf::ContextSettings winMainSettings ; //winMain.getSettings ()
    sf::WindowHandle winMainSystemHandle ; //winMain.getSystemHandle ()
}

and a function to save :
int SaveCurrentState (sf::RenderWindow & winMain)
{
    SLInfo Info ;
    ofstream SaveFile ("some_file" , ios::binary) ;
    Info.winMainSettings = winMain.getSettings () ;
    Info.winMainSystemHandle = winMain.getSystemHandle () ;
    SaveFile.write (reinterpret_cast <char *> (& Info) , sizeof (Info)) ;
    return 1 ;
}
(is that refrence sign (&) before winMain correct?)
(Most of the code is omitted so that it looks simple and easier for you to check it)


and a function to load :
SLInfo LoadFile (string FileName)
{
    ifstream LoadLoc (FileName.c_str () , ios::binary) ;
    SLInfo Loader ;
    LoadLoc.read (reinterpret_cast <char *> (& Loader) , sizeof (Loader)) ;
    return Loader ;
}

Then in another function I open the window like this :
SLInfo winMainInfo ;
    string FileName ;
    cout << "\nEnter file name :\n" ;
    cin >> FileName ;
    winMainInfo = LoadFile (FileName.c_str ()) ;
    sf::RenderWindow winMain (winMainInfo.winMainSystemHandle , winMainInfo.winMainSettings) ;
(+ while window.isOpen () and event handlings , I omitted those parts too)

But when I run it (using Code::Blocks 12.11) I see these messages in console :
Failed to set pixel format for device context -- cannot creat OpenGL context
Failed to activate the window's context
Failed to activate the window's context
 
And I see no window at all .
Thanks in advanced for helping.
And if there is any additional info on my code needs to be provided let me know .
Thanks .
« Last Edit: October 22, 2013, 12:53:35 am by Ardeshir81 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Can't save and load ContextSettings and WindowHandle
« Reply #1 on: October 22, 2013, 01:05:08 am »
A window handle is little more than a pointer to a window.  If the window gets destroyed and you try to use its handle later, that's essentially the same problem as dereferencing a null pointer: there's nothing there anymore, so of course anyone trying to use it will get garbage information or maybe even seg fault.  The sf::Window constructor that takes a handle is intended for using SFML inside something you've already created using some other means (both the tutorials and documentation tell you this).

If you really need to store window information for later, either save the actual window, or save the information needed to construct a new window (ie, what the other sf::Window constructors take).  The handle by itself is neither of those things.

Btw, if you have to ask whether the & is correct, you should probably back away from real graphics for a while and go brush up on basic C++.  Trying to use a graphics library when you don't know the language it's written in is never a good idea.
« Last Edit: October 22, 2013, 01:08:51 am by Ixrec »

Ardeshir81

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Can't save and load ContextSettings and WindowHandle
« Reply #2 on: October 22, 2013, 01:49:19 am »
Thanks Ixrec !

I saved the window's properties one by one (size , position , title + settings) and it worked .

By the way , about that reference .

First I tried to send the object itself , but my "SaveCurrentState ()" caused a problem .
It opened a .hpp file : noncopyable.hpp
I searched in Google and I found  out a reference sign will solve the problem , so I used it .

I still don't know why .
The reason I asked it is that I'm looking for a simpler description of what the problem was and how it was solved .
Can you help me again ? Thanks !

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Can't save and load ContextSettings and WindowHandle
« Reply #3 on: October 22, 2013, 01:59:47 am »
It's simpler than you think.  sf::Window is a subclass of sf::Noncopyable, which (go figure) cannot be copied!  There are many different kinds of resources that can't be copied for various reasons, typically because either you have to ask the OS whenever you want a new one (like a window), or it's a piece of hardware (like a keyboard).

When you pass arguments to a function, normally they get copied in what's called "pass-by-value."  When you pass by pointer or by reference (and a reference is really just a pointer with different syntax/semantics that make it a lot safer), you pass the address of the object, rather than making a full copy of it (this can also saves a lot of memory).  In that particular context, the & indicates a reference (in other places it may indicate "retrieve the address of").  This is basic C++ stuff that you should go back and review.