Scrolling Game Development Kit Forum

SGDK Version 2 => Help, Errors, FAQ => Topic started by: Vincent on 2009-03-03, 10:44:55 AM

Title: Zooming, Window resizing
Post by: Vincent on 2009-03-03, 10:44:55 AM
Hi everyone,

Just wondering, is there a way to change the size of a windowed game at runtime, without changing the resolution of the game I mean?  Would it be possible to allow the game to be resized?  This "resize", rather than showing more or less of the current map, would zoom in or out.  So small graphic would appear bigger, etc?  If it is not possible right now, what king of efforts would be necessary to change the source code to allow that?  What would be the general steps to follow?

Thanks
Title: Re: Zooming, Window resizing
Post by: Jam0864 on 2009-03-03, 11:38:53 PM
good idea.
I assume zooming out will be fine, but zooming in will result in pixelation.
Title: Re: Zooming, Window resizing
Post by: bluemonkmn on 2009-03-04, 05:13:48 PM
Actually I almost implemented this because it looked so easy.  I can't remember why I didn't.  It's built right in to the OpenGL framework and the video hardware, I assume, the ability to set the viewport size and view window size independently (or something like that).  Actually I think I just remembered why I didn't.  It worked, but in some cases you could see the spaces between the tiles when they got stretched.  I don't know if I'm using the wrong coordinates (if I need to be offsetting things by a half a pixel in some direction) or if that's the nature of drawing independent tiles.  But I didn't want to have to deal with that issue at the time so I didn't.  I think you could try a couple of things to enable that though (I can't remember exactly what I did).  First change "FormBorderStyle.FixedSingle" to "FormBorderStyle.Sizable" in GameForm.cs.  See how that behaves.  If that didn't do the trick, then also try changing "private bool scaleNativeSize = false;" to "private bool scaleNativeSize = true;" in Display.cs.  I think scaleNativeSize controls whether making the window larger will expose more of the map versus stretching the existing view.  So you see it's very close to already done -- it just has some issues when you turn it on.  Please tell us how it turns out.
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-05, 07:51:18 AM
Neat!!!

Thanks bluemonkmn,  I'm probably gonna try this tonight.  Oh, I can't wait to try it!
I'll keep you informed. :)
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-05, 11:07:52 PM
Well, I tried a couple of things... 

If I change the size of the window with "private bool scaleNativeSize = false;", I see more or less of the map.  When "private bool scaleNativeSize = true;", the whole thing zooms in or out.  The main problem is, when the display zone changes size, it would seem that the scrolling is not working fine.  My scripts don't scroll the character sprite into view.  It's also strange when I, change the size in the "zooming" mode, the aspect ratio changes, making the graphics become very thin or very wide.  I tried to add an event on the resize of the window to make sure the aspect ratio is always respected, but it does not seem to work so well.

I tried all this stuff in the project files of the game I am currently working on.  I'd like to post it here, but it's a little too big.  I'll try to make an example without all the extra stuff.  I could post it here when it's done, so if you're interested bluemonkmn, you could take a look and maybe take a step further.  I'm not sure what to do about the scrolling issue.

I didn't see the problem about the spaces between the tiles.  It never happened to me.

I'll post an example here, probably tomorrow night.

Thanks! :)
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-06, 11:59:16 AM
Allright here is the first test. 

I think I solved all of the problems for the "show more map option".  (Meaning that you can resize a window and it shows more or less of the map without zooming.) 

It behaves correctly I suppose.  If some of you guys want to test it and make it better, be my guest. :) 

I'm attaching the sgdk2 file to this post.  It's a very simple map with black and white tiles with lines accross them.  Those lines are useful to see if there is space between tiles or if the aspect ratio changes (tiles do not seem square if the ratio changes). 
There is a little stick man you can move around with the keyboard arrows to check the scrolling.  As far as I can see, it works fine, but I didn't test a lot.

Oups!  It would seem that the upload folder is full.  The two files I want to provide are not big.  100k each.  Can you help me out Bluemonkmn?
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-06, 12:08:50 PM
Okay, I'm writing explanations for the second file, even if I can't attach it for the moment.

The second file is for a zoom in, zoom out effect when the window is resized.  It does not seem to behave correctly.  When I resize the window to make it larger, the display of the game gets real big and seems to go over the bounds of the window.  But when I use a MessageBox.Show to get the size of the window and the size of the display, they seem equal.

The opposite is also true.  If I resize the window to make it smaller, the display gets real tiny, well under the size of the window.  But when I use a MessageBox.Show to get the size of the window and the size of the display, they seem equal.

By the way, I changed the color of the tiles, thinking it would help to see spaces between tiles as the window gets resized, so I know it's kinda ugly.  Hey, it's only for a test! ;)  I couldn't see spaces between tiles.

Oh, I successfully implemented an event to make sure the aspect ratio is always respected when the window gets resized.  So, graphics don't look too wide or too thin anymore.

I made modifications in GameForm.cs and Display.cs to do modifications for both tests.  These alterations can be found by searching for "vincent" in the source code.  My modifications are quite explicit I believe.

There is a little work to do here to finish the display size issue.  Can you help me out Bluemonkmn?  I'm clueless on this one.

As soon as bluemonkmn allows me to upload the test files, I'll provide them.

Thanks a lot! :)
Title: Re: Zooming, Window resizing
Post by: SmartBoy16 on 2009-03-07, 12:31:27 AM
Oups!  It would seem that the upload folder is full.  The two files I want to provide are not big.  100k each.  Can you help me out Bluemonkmn?

box.net
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-07, 08:20:48 AM
Thanks for your info SmartBoy16, :)

Here are the links to download the example files.  They are going to be removed from the storage in two weeks I believe.  If anyone wants them after that, send me a message.

For the "show more or less map test":    http://www.adrive.com/public/7f0d5679c0dfce5a5bb1d1d895c99a9cd6a735dce3aec637eaac37e4554f4073.html (http://www.adrive.com/public/7f0d5679c0dfce5a5bb1d1d895c99a9cd6a735dce3aec637eaac37e4554f4073.html)

For the "zoom in or out test":     http://www.adrive.com/public/03c0aa1a710778e3b98eda6ae28390846185ef228dd2b5a7bcf8a09d558b3065.html (http://www.adrive.com/public/03c0aa1a710778e3b98eda6ae28390846185ef228dd2b5a7bcf8a09d558b3065.html)

That's it! :)
Title: Re: Zooming, Window resizing
Post by: bluemonkmn on 2009-03-07, 09:07:46 AM
I doubled the available space for attachments so it should be possible to attach files on posts again now.
Title: Re: Zooming, Window resizing
Post by: bluemonkmn on 2009-03-07, 09:14:06 AM
Wow, you changed a lot more code than I was suggesting.  Really I thought you only needed to change 2 lines of code to get the zooming working.  Now I think you're maybe double-zooming, which is why the effect is exaggerated.  Did you try just changing the 2 lines (allowing the form to be resized, and setting the native scaling to true)?
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-07, 09:36:21 AM
I changed a little more code, because I wanted the size of the display to fit the size of the window.  (otherwise, the display size, wasn't changing).

I have a theory:  (I'm most probably mistaken)
I looked around in a couple of OpenGL forums, (I must admit that I understand almost nothing of it), but I saw in the display.cs class that the MatrixMode is in Projection.  I've found a couple of graphics that describe this mode, and, as I understand it, it's like projecting an image on a cinema screen.  I have the feeling that, even if the display control has the right size, the "camera" looking at the graphics is getting very near the graphics (when you resize the graphics to make them bigger), and the camera gets very far when the ersize is becoming smaller.  That would explain that even if the display fits the size of the screen, the actual graphics are much too small or much too big.

Does this help?  I completely lost.  I've been looking at the Gl.Ortho function, wondering if the last 2 parameters had something to do with the operation, but I don't understand what they mean.
Title: Re: Zooming, Window resizing
Post by: bluemonkmn on 2009-03-07, 09:45:21 AM
I changed a little more code, because I wanted the size of the display to fit the size of the window.  (otherwise, the display size, wasn't changing).

I tried myself and saw the same thing.  The simplest way to make the display size match the window size is by changing this line of code in GameForm:
Code: [Select]
GameDisplay.Size = Display.GetScreenSize(mode);

with this:
Code: [Select]
GameDisplay.Dock = DockStyle.Fill;
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-07, 09:55:55 AM
Ok, here is the "exact" report on what I changed! ;)

First of all, for the zooming, I only changed the native scaling to true in Display.cs

Then, in GameForm.cs I changed these things:
Changed the borderStyle to sizable (as you were suggesting)
Changed the start position of the gameform to "centerscreen" (just for fun, it doesn't change anything really, it just makes the gameform appear in the middle of the screen when yuo launch the game)
Added an event that is launched when the window gets resized.

The event makes sure that the display iis always respecting the aspect ratio and fits the clientsize of the window

The code of the event:
Code: [Select]
 
private void GameForm_Resize(object sender, System.EventArgs e)
   {

      GameForm myForm = (GameForm)sender;
      int baseHeight = Display.GetScreenSize(myForm.GameDisplay.GameDisplayMode).Height;
      int baseWidth = Display.GetScreenSize(myForm.GameDisplay.GameDisplayMode).Width;
      double OriginalRatio = ((double)baseWidth / (double)baseHeight);
      double NewRatio = ((double)myForm.ClientSize.Width / (double)myForm.ClientSize.Height);
      if(OriginalRatio != NewRatio)
      {
         int newHeight = myForm.ClientSize.Width * baseHeight / baseWidth;
         myForm.ClientSize = new System.Drawing.Size(myForm.ClientSize.Width, newHeight);
      }
      GameDisplay.Size = myForm.ClientSize;

     //This part under is only used for debugging, making sure that the display size = clientsize of the game form
      Random num = new Random();
      if(num.Next(0, 1000) > 900)
      {    
         MessageBox.Show("FormWidth: " + myForm.ClientSize.Width.ToString() + " FormHeight: " + myForm.ClientSize.Height.ToString() + " GameDisplayWidth: " + GameDisplay.Size.Width.ToString() + " GameDisplayHeight: " + GameDisplay.Size.Height.ToString());
      }
   }

Okay, just seen yuor new post with the dockstyle.  It works well and makes a good part of the resize event obsolete.  But the aspect ratio isn't respected and I still have the problem with the zooming being too far or too near...  So the code of the event could be reduced to this:
Code: [Select]
   private void GameForm_Resize(object sender, System.EventArgs e)
   {

      GameForm myForm = (GameForm)sender;
      int baseHeight = Display.GetScreenSize(myForm.GameDisplay.GameDisplayMode).Height;
      int baseWidth = Display.GetScreenSize(myForm.GameDisplay.GameDisplayMode).Width;
      double OriginalRatio = ((double)baseWidth / (double)baseHeight);
      double NewRatio = ((double)myForm.ClientSize.Width / (double)myForm.ClientSize.Height);
      if(OriginalRatio != NewRatio)
      {
         int newHeight = myForm.ClientSize.Width * baseHeight / baseWidth;
         myForm.ClientSize = new System.Drawing.Size(myForm.ClientSize.Width, newHeight);
      }
}
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-07, 09:58:19 AM
I'm posting the "showmoremap" example here.  (this one is functional)
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-07, 09:59:44 AM
And an updated version of the "zooming" example.  Still not working, but with less changes to the source code and using bluemonkmn DockStyle.Fill line of code.
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-07, 09:14:33 PM
Hi guys!

Well, I worked a lot and through a combination of intuition, openGl forums reading, luck, trials and errors, not to mention a good reading of the SGDK2 sourcecode, I succeeded!!!  :nerd:

As far as I can see, the zooming now works well for window resizing.  I wasn't far off with my idea of a camera.  In fact, not only must the Display size change but there is a Rectangle within it that must be resized too.  This Rectangle is used to draw the graphics on, like the screen of a cinema (as I understood it). 

To achieve this, I modified another line of code in the Display.cs class.  In the function "Scissor" I changed this line
Code: [Select]
GL.Scissor(rect.X, ClientRectangle.Height - rect.Y - rect.Height, rect.Width, rect.Height);
for this line
Code: [Select]
GL.Scissor(rect.X, rect.Y, ClientSize.Width, ClientSize.Height);

And in the MapBase.cs class, for the TotalView property, I changed this line
Code: [Select]
return m_Display.DisplayRectangle;for these lines
Code: [Select]
int rectWidth = Display.GetScreenSize(m_Display.GameDisplayMode).Width;
int rectHeight = Display.GetScreenSize(m_Display.GameDisplayMode).Height;
return new Rectangle(0, 0, rectWidth, rectHeight);

I would like to explain exactly what I did and how that came to work, but I'm really not sure myself... :-[
My biggest hints were bluemonkmn comments on these parts of code.

I didn't test if it works for games where the display is splitted for 2 players for example.  Would someone like to test it?
I'm providing my example here.  As always, look for modifications in the code by searching for "vincent".  in these sections, the original code is commented out and my lines of codes are clearly identified.

Thanks a lot for your help bluemonkmn! :)  I really appreciate it! :D
Title: Re: Zooming, Window resizing
Post by: bluemonkmn on 2009-03-07, 11:43:27 PM
I don't have an SGDK2 system handy at the moment.  What did your additional changes accomplish this time?  Did they make the scrolling margins work properly?  Did they eliminate the lines between tiles?
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-08, 07:50:58 AM
The scrolling margins work properly and I don't see lines between tiles. 
But, maybe the tiles I use do not help me seeing lines between tiles.  So, for the lines, I'm not 100% sure.
Title: Re: Zooming, Window resizing
Post by: bluemonkmn on 2009-03-08, 08:37:20 AM
Did the changes you made accomplish that, or did that already work with just the 3 line changes?
Title: Re: Zooming, Window resizing
Post by: Vincent on 2009-03-08, 08:40:08 AM
No, the changes I made were necessary.  Without them the scrolling margins do not work properly.