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

Author Topic: .net change from Close to Fullscreen  (Read 4410 times)

0 Members and 1 Guest are viewing this topic.

dalhome3

  • Newbie
  • *
  • Posts: 3
    • View Profile
.net change from Close to Fullscreen
« on: January 21, 2016, 01:04:34 pm »
I'm trying to move from a window to fullscreen my game I can do this successfully using the code
if (e.Code == Keyboard.Key.F11)
{
      fullscreen = !fullscreen
      if (fullscreen)
      {
            renderWindow = new new RenderWindow(new VideoMode(PrimaryScreen.X, PrimaryScreen.Y), "RogueLike 2D " + context.MajorVersion + "." + context.MinorVersion, Styles.Fullscreen, context);
      }
      else
      {
      renderWindow = new new RenderWindow(new VideoMode(1280, 720), "RogueLike 2D " + context.MajorVersion + "." + context.MinorVersion, Styles.Close, context);
      }
}

But I lose all of my setup events when I do this. Is there any other way then re declaring my events?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: .net change from Close to Fullscreen
« Reply #1 on: January 21, 2016, 03:12:56 pm »
No there isn't, but its easy to solve, just put all your event hookups in a function that takes a reference to the window.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

dalhome3

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: .net change from Close to Fullscreen
« Reply #2 on: January 21, 2016, 07:21:26 pm »
How would I make it so it knows what events to set up because I have different events depending on what screen I'm on?

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: .net change from Close to Fullscreen
« Reply #3 on: January 22, 2016, 03:15:15 pm »
first step you need to unsubscribe all events from previous render window,
second step you need to dispose previous render window
third step you need to create new render window and subscribe for events for it.

So, you're doing it in the wrong way, because you didn't unsubscribed from events, didn't disposed old render window. It means that it still exists, still works and consuming memory and may raise events
« Last Edit: January 22, 2016, 03:19:32 pm by mkalex777 »

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: .net change from Close to Fullscreen
« Reply #4 on: January 22, 2016, 03:18:50 pm »
How would I make it so it knows what events to set up because I have different events depending on what screen I'm on?

your program is only one who knows what events is needed for you.

You can do it in the following way:
if (e.Code == Keyboard.Key.F11)
{
      _isFullscreenChangeRequest = true;
}
...
...
public void Main()
{
   CreateWindow();
   while (window.IsOpen)
   {
       window.DispatchMessages();
       Update();
       Render(window);

       if (_isFullscreenChangeRequest)
       {
           _isFullscreenChangeRequest = false;
           _isFullscreen = !_isFullscreen;
           CreateWindow();
       }
   }
}

private void CreateWindow()
{
        // first we need unsubscribe from all events
        window.Resize -= Window_OnResize;
        window.KeyPressed -= Window_OnKeyPressed;
       
        // then we need to dispose window
        window.Dispose();
        window = null;

        // and now we can create new one
        window = new new RenderWindow(
            _isFullscreen ? new VideoMode(PrimaryScreen.X, PrimaryScreen.Y) : new VideoMode(1280, 720),
            "RogueLike 2D " + context.MajorVersion + "." + context.MinorVersion,
            _isFullscreen ? Styles.Fullscreen : Styles.Default,
            context);
       
         // and now you need to subscribe for events on a new window
        window.Closed += (s,e) => window.Close();
        window.Resize += Window_OnResize;
        window.KeyPressed += Window_OnKeyPressed;
        window.SetVisible(true);
        window.SetFrameLimit(0);
        window.SetVerticalSyncEnabled(true);
}
 

Unfortunately you cannot do all thins inside keypressed event due to the bug in SFML. (If you will try to dispose window from keypressed event it will leads to access violation error).
So, you need to notify the main loop about the change you need and then you can do all things safely from the main loop.

This sample is a little simplified and has some minor issues. Look for my topic here http://en.sfml-dev.org/forums/index.php?topic=19026.0
You will find good working example source code at the end of this topic.
« Last Edit: January 22, 2016, 03:39:24 pm by mkalex777 »

 

anything