Resizable windows

I heard resizable windows (and the corresponding messaging system
additions) mentioned recently, and I wondered just how remote a
possibility it is. It would be a very useful feature to have.

Cheers,

Darrell Johnson

I heard resizable windows (and the corresponding messaging system
additions) mentioned recently, and I wondered just how remote a
possibility it is. It would be a very useful feature to have.

I’m considering it for SDL 0.11

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

Hi everyone,

I have created an opengl application for both windows and linux.
The application uses a resizable window by passing the SDL_RESIZABLE flag
to the SDL_SetVideoMode.

The problem is that with SDL 1.2.6 under Mandrake 9.2 eveything worked
fine. With SDL 1.2.7 under Mandrake 10 CE, everything works but the window
is not resizable any more (as if the SDL_RESIZABLE is not passed). Under
windows everything works fine even with SDL 1.2.7.

Can someone verify that 1.2.7 is OK? Could it be an issue with the
X-Server of Mandrake?

p.s. I made a test with an non-OpenGL application and the problem still
remains…

Hello,

this is my first message here, and also my first try to make a SDL program.
Here is what I made so far :

Code:
#ifdef __cplusplus
#include
#else
#include <stdlib.h>
#endif
#ifdef APPLE
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

int main (int argc, char** argv)
{
// initialize SDL video
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
fprintf (stderr, “Unable to init SDL: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

// make sure SDL cleans up before exit
atexit(SDL_Quit);


// create a new window
int width = 400;
int height = 400;
SDL_Surface* screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
if ( !screen )
{
    fprintf (stderr, "Unable to set video: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

SDL_WM_SetCaption("Position Editor", NULL);

/*

// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
    fprintf(stderr, "Unable to load bitmap: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;

*/

SDL_Surface* light_square = SDL_CreateRGBSurface(SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect(light_square, NULL, SDL_MapRGB(screen->format, 255, 206, 158));

SDL_Surface* dark_square = SDL_CreateRGBSurface(SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect(dark_square, NULL, SDL_MapRGB(screen->format, 209, 139, 71));




// program main loop
bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {
				// exit if the window is closed
			case SDL_QUIT:
				done = true;
				break;

			case SDL_VIDEORESIZE:
				width = event.resize.w;
				height = event.resize.h;
				break;

				// check for keypresses
			case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                break;
            }
        } // end switch
    } // end of message processing

    // DRAWING STARTS HERE
	screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);

	SDL_Surface* light_square = SDL_CreateRGBSurface (SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
	SDL_FillRect (light_square, NULL, SDL_MapRGB (screen->format, 255, 206, 158));

	SDL_Surface* dark_square = SDL_CreateRGBSurface (SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
	SDL_FillRect (dark_square, NULL, SDL_MapRGB(screen->format, 209, 139, 71));


    //clear screen
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

/*
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
/
int i,j;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position;
position.x = i
width/8;
position.y = j*height/8;
if ( (i + j) % 2 == 0)
SDL_BlitSurface(light_square, 0, screen, &position);
else
SDL_BlitSurface(dark_square, 0, screen, &position);
}
}

    // DRAWING ENDS HERE

    // finally, update the screen :)
    SDL_Flip(screen);
} // end main loop

/*
// free loaded bitmap
SDL_FreeSurface(bmp);
*/
// all is well :wink:
printf(“Exited cleanly\n”);
return EXIT_SUCCESS;
}

It can be run, but the result isn’t what I expected. I don’t want the squares to become rectangles when the window is resized. I want to keep a fixed aspect ratio. How can I achieve that ?

I also noticed that the window size isn’t updated until I remove my finger from the mouse click button. Is it possible to update the window size automatically, as it is for other system’s windows ?

Finally, I don’t have advanced softwares to see how my cpu/gpu handles my program, but it seems to me that my computer slows down a lot when opening this program. Did I do something wrong ?

I’ll be really greatful for any ameliorations you can help me to make.

Thanks.[/quote]

The slowdown: You need to throttle your frame rate. You should look further
into it for a better method, but a fair start is to add SDL_Delay(1) after
SDL_Flip.

Jonny DOn Sun, Feb 28, 2010 at 12:54 AM, Lilly wrote:

Hello,

this is my first message here, and also my first try to make a SDL program.
Here is what I made so far :

Code:

#ifdef __cplusplus
#include
#else
#include
#endif
#ifdef APPLE
#include
#else
#include
#endif

int main (int argc, char** argv)
{
// initialize SDL video
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
fprintf (stderr, “Unable to init SDL: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

// make sure SDL cleans up before exit
atexit(SDL_Quit);


// create a new window

int width = 400;
int height = 400;
SDL_Surface* screen = SDL_SetVideoMode(width, height, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
if ( !screen )
{
fprintf (stderr, “Unable to set video: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

SDL_WM_SetCaption(“Position Editor”, NULL);

/*

// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
    fprintf(stderr, "Unable to load bitmap: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;

*/

SDL_Surface* light_square = SDL_CreateRGBSurface(SDL_HWSURFACE,

width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect(light_square, NULL, SDL_MapRGB(screen->format, 255, 206,
158));

SDL_Surface* dark_square = SDL_CreateRGBSurface(SDL_HWSURFACE, width/8,

height/8, 32, 0, 0, 0, 0);
SDL_FillRect(dark_square, NULL, SDL_MapRGB(screen->format, 209, 139,
71));

// program main loop
bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {
           // exit if the window is closed
        case SDL_QUIT:
           done = true;
           break;

        case SDL_VIDEORESIZE:
           width = event.resize.w;
           height = event.resize.h;
           break;

           // check for keypresses
        case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                break;
            }
        } // end switch
    } // end of message processing

    // DRAWING STARTS HERE
  screen = SDL_SetVideoMode(width, height, 32,

SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);

  SDL_Surface* light_square = SDL_CreateRGBSurface (SDL_HWSURFACE,

width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect (light_square, NULL, SDL_MapRGB (screen->format, 255,
206, 158));

  SDL_Surface* dark_square = SDL_CreateRGBSurface (SDL_HWSURFACE,

width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect (dark_square, NULL, SDL_MapRGB(screen->format, 209, 139,
71));

    //clear screen
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

/*
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
/
int i,j;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position;
position.x = i
width/8;
position.y = j*height/8;
if ( (i + j) % 2 == 0)
SDL_BlitSurface(light_square, 0, screen, &position);
else
SDL_BlitSurface(dark_square, 0, screen, &position);
}
}

    // DRAWING ENDS HERE

    // finally, update the screen :)
    SDL_Flip(screen);
} // end main loop

/*
// free loaded bitmap
SDL_FreeSurface(bmp);
*/
// all is well :wink:
printf(“Exited cleanly\n”);
return EXIT_SUCCESS;
}

It can be run, but the result isn’t what I expected. I don’t want the
squares to become rectangles when the window is resized. I want to keep a
fixed aspect ratio. How can I achieve that ?

I also noticed that the window size isn’t updated until I remove my finger
from the mouse click button. Is it possible to update the window size
automatically, as it is for other system’s windows ?

Finally, I don’t have advanced softwares to see how my cpu/gpu handles my
program, but it seems to me that my computer slows down a lot when opening
this program. Did I do something wrong ?

I’ll be really greatful for any ameliorations you can help me to make.

Thanks.[/quote]


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Another performance problem: Don’t use SDL_SetVideoMode or
SDL_CreateRGBSurface in your loop. SDL_SetVideoMode creates the display
surface. You only need to call it once (before your loop) and again any
time you change the screen size (in response to a resize event).
SDL_CreateRGBSurface creates another surface. Surfaces are image data.
You don’t want to recreate the same image data each frame, especially if
you don’t free the memory (with SDL_FreeSurface) like you do. The display
surface (screen) should not be freed with SDL_FreeSurface, but the others
must be.

Jonny DOn Sun, Feb 28, 2010 at 11:28 AM, Jonathan Dearborn <@Jonathan_Dearborn>wrote:

The slowdown: You need to throttle your frame rate. You should look
further into it for a better method, but a fair start is to add SDL_Delay(1)
after SDL_Flip.

Jonny D

On Sun, Feb 28, 2010 at 12:54 AM, Lilly wrote:

Hello,

this is my first message here, and also my first try to make a SDL
program.
Here is what I made so far :

Code:

#ifdef __cplusplus
#include
#else
#include
#endif
#ifdef APPLE
#include
#else
#include
#endif

int main (int argc, char** argv)
{
// initialize SDL video
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
fprintf (stderr, “Unable to init SDL: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

// make sure SDL cleans up before exit
atexit(SDL_Quit);


// create a new window

int width = 400;
int height = 400;
SDL_Surface* screen = SDL_SetVideoMode(width, height, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
if ( !screen )
{
fprintf (stderr, “Unable to set video: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

SDL_WM_SetCaption(“Position Editor”, NULL);

/*

// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
    fprintf(stderr, "Unable to load bitmap: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;

*/

SDL_Surface* light_square = SDL_CreateRGBSurface(SDL_HWSURFACE,

width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect(light_square, NULL, SDL_MapRGB(screen->format, 255, 206,
158));

SDL_Surface* dark_square = SDL_CreateRGBSurface(SDL_HWSURFACE,

width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect(dark_square, NULL, SDL_MapRGB(screen->format, 209, 139,
71));

// program main loop
bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {
           // exit if the window is closed
        case SDL_QUIT:
           done = true;
           break;

        case SDL_VIDEORESIZE:
           width = event.resize.w;
           height = event.resize.h;
           break;

           // check for keypresses
        case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                break;
            }
        } // end switch
    } // end of message processing

    // DRAWING STARTS HERE
  screen = SDL_SetVideoMode(width, height, 32,

SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);

  SDL_Surface* light_square = SDL_CreateRGBSurface (SDL_HWSURFACE,

width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect (light_square, NULL, SDL_MapRGB (screen->format, 255,
206, 158));

  SDL_Surface* dark_square = SDL_CreateRGBSurface (SDL_HWSURFACE,

width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect (dark_square, NULL, SDL_MapRGB(screen->format, 209,
139, 71));

    //clear screen
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

/*
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
/
int i,j;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position;
position.x = i
width/8;
position.y = j*height/8;
if ( (i + j) % 2 == 0)
SDL_BlitSurface(light_square, 0, screen, &position);
else
SDL_BlitSurface(dark_square, 0, screen, &position);
}
}

    // DRAWING ENDS HERE

    // finally, update the screen :)
    SDL_Flip(screen);
} // end main loop

/*
// free loaded bitmap
SDL_FreeSurface(bmp);
*/
// all is well :wink:
printf(“Exited cleanly\n”);
return EXIT_SUCCESS;
}

It can be run, but the result isn’t what I expected. I don’t want the
squares to become rectangles when the window is resized. I want to keep a
fixed aspect ratio. How can I achieve that ?

I also noticed that the window size isn’t updated until I remove my finger
from the mouse click button. Is it possible to update the window size
automatically, as it is for other system’s windows ?

Finally, I don’t have advanced softwares to see how my cpu/gpu handles my
program, but it seems to me that my computer slows down a lot when opening
this program. Did I do something wrong ?

I’ll be really greatful for any ameliorations you can help me to make.

Thanks.[/quote]


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Ok, thanks for the answer. I changed some things, according to your observations. I hope this is what you meant.
As far as how my computer handles the program, it seems to be less laggy. When I press ctrl alt suppr, I find that it still uses about 3Mo of ram. Why so much ?
The following problems are still not solved :

  • I don’t want the squares to become rectangles when the window is resized. I want to keep a fixed aspect ratio. How can I achieve that ?

  • I also noticed that the window size isn’t updated until I remove my finger from the mouse click button. Is it possible to update the window size automatically, as it is for other system’s windows ?

Code:

#ifdef __cplusplus
#include
#else
#include <stdlib.h>
#endif
#ifdef APPLE
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

int main (int argc, char** argv)
{
// initialize SDL video
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
fprintf (stderr, “Unable to init SDL: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

// make sure SDL cleans up before exit
atexit(SDL_Quit);


// create a new window
int width = 400;
int height = 400;
SDL_Surface* screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
if ( !screen )
{
    fprintf (stderr, "Unable to set video: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

SDL_WM_SetCaption("Position Editor", NULL);

/*

// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
    fprintf(stderr, "Unable to load bitmap: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;

*/

SDL_Surface* light_square = SDL_CreateRGBSurface(SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect(light_square, NULL, SDL_MapRGB(screen->format, 255, 206, 158));

SDL_Surface* dark_square = SDL_CreateRGBSurface(SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
SDL_FillRect(dark_square, NULL, SDL_MapRGB(screen->format, 209, 139, 71));

int i,j;
for (i = 0 ; i < 8 ; i++)
{
	for (j = 0 ; j < 8 ; j++)
	{
		SDL_Rect position;
		position.x = i*width/8;
		position.y = j*height/8;
		if ( (i + j) % 2 == 0)
			SDL_BlitSurface(light_square, 0, screen, &position);
		else
			SDL_BlitSurface(dark_square, 0, screen, &position);
	}
}

SDL_FreeSurface(light_square);
SDL_FreeSurface(dark_square);

// program main loop
bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {
				// exit if the window is closed
			case SDL_QUIT:
				done = true;
				break;

			case SDL_VIDEORESIZE:
			{
				width = event.resize.w;
				height = event.resize.h;

				//clear screen
				SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

				//Drawing starts here
				screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);

				SDL_Surface* light_square = SDL_CreateRGBSurface (SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
				SDL_FillRect (light_square, NULL, SDL_MapRGB (screen->format, 255, 206, 158));

				SDL_Surface* dark_square = SDL_CreateRGBSurface (SDL_HWSURFACE, width/8, height/8, 32, 0, 0, 0, 0);
				SDL_FillRect (dark_square, NULL, SDL_MapRGB(screen->format, 209, 139, 71));

		        int i,j;
				for (i = 0 ; i < 8 ; i++)
				{
					for (j = 0 ; j < 8 ; j++)
					{
						SDL_Rect position;
						position.x = i*width/8;
						position.y = j*height/8;
						if ( (i + j) % 2 == 0)
							SDL_BlitSurface(light_square, 0, screen, &position);
						else
							SDL_BlitSurface(dark_square, 0, screen, &position);
					}
				}

				SDL_FreeSurface(light_square);
				SDL_FreeSurface(dark_square);

				// DRAWING ENDS HERE

				break;
			}

				// check for keypresses
			case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                break;
            }
        } // end switch
    } // end of message processing

    // finally, update the screen :)
    SDL_Flip(screen);
    SDL_Delay(1);

} // end main loop

/*
// free loaded bitmap
SDL_FreeSurface(bmp);
*/

// all is well ;)
printf("Exited cleanly\n");
return EXIT_SUCCESS;

}

First off, you’re only drawing the squares when resizing now. It seems like this is not what you want.

To fix what you want with the way you have it now, though:

Code:

           SDL_Surface* light_square = SDL_CreateRGBSurface (SDL_HWSURFACE, min(width, height)/8, min(width, height)/8, 32, 0, 0, 0, 0);
           SDL_FillRect (light_square, NULL, SDL_MapRGB (screen->format, 255, 206, 158));

           SDL_Surface* dark_square = SDL_CreateRGBSurface (SDL_HWSURFACE, min(width, height)/8, min(width, height)/8, 32, 0, 0, 0, 0);
           SDL_FillRect (dark_square, NULL, SDL_MapRGB(screen->format, 209, 139, 71));

Here are some more suggestions:

It is easy to get surfaces which have differing internal formats, especially
when you load them from files. Don’t assume that the new ones have the same
format as the display surface (screen). Use the appropriate format in the
SDL_MapRGB call:
SDL_Surface* light_square = SDL_CreateRGBSurface(SDL_HWSURFACE, width/8,
height/8, 32, 0, 0, 0, 0);
SDL_FillRect(light_square, NULL, SDL_MapRGB(light_square->format, 255,
206, 158));

SDL_Surface* dark_square = SDL_CreateRGBSurface(SDL_HWSURFACE, width/8,

height/8, 32, 0, 0, 0, 0);
SDL_FillRect(dark_square, NULL, SDL_MapRGB(dark_square->format, 209,
139, 71));

If you’re really using images, then creating new surfaces is fine. If
you’re actually just going to use rectangles, then it might be easier (and
definitely faster) if you perform the drawing right on the display surface:

int i,j;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position = {iwidth/8, jheight/8, width/8, height/8};
if ( (i + j) % 2 == 0)
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 255,
206, 158)); // light_square
else
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 209,
139, 71)); // Dark square
}
}

Using this and moving your drawing back into the proper loop, I get:

// program main loop
bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {
           // exit if the window is closed
        case SDL_QUIT:
           done = true;
           break;

        case SDL_VIDEORESIZE:
        {
           width = event.resize.w;
           height = event.resize.h;

           screen = SDL_SetVideoMode(width, height, 32,

SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);

           break;
        }

           // check for keypresses
        case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                break;
            }
        } // end switch
    } // end of message processing

      //Drawing starts here

           //clear screen
           SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

      int i,j;

for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position = {iwidth/8, jheight/8, width/8, height/8};
if ( (i + j) % 2 == 0)
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 255,
206, 158)); // light_square
else
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 209,
139, 71)); // Dark square
}
}

      // DRAWING ENDS HERE

    // finally, update the screen :)
    SDL_Flip(screen);
    SDL_Delay(1);


} // end main loop

/*
// free loaded bitmap
SDL_FreeSurface(bmp);
*/

// all is well ;)
printf("Exited cleanly\n");
return EXIT_SUCCESS;

}

Now, if you want your rects to stay the same size, then you can’t initialize
them with the screen size (which changes). That is, don’t use ‘width’ and
’height’ to determine your rect dimensions if you’re also setting 'width’
and ‘height’ to be equal to the screen dimensions:

      int i,j;

Uint32 rectW = 50;
Uint32 rectH = 50;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position = {irectW, jrectH, rectW, rectH};
if ( (i + j) % 2 == 0)
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 255,
206, 158)); // light_square
else
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 209,
139, 71)); // Dark square
}
}

If you actually want the rects to scale with the screen but stay square (for
instance), one way is to use only one screen dimension to calculate the
dimensions of your rects. You might use SDL_gfx or Sprig to scale
SDL_Surfaces if you’re using images.

Hopefully that fixes your problems. Let us know,
Jonny DOn Mon, Mar 1, 2010 at 7:43 AM, nfries88 wrote:

First off, you’re only drawing the squares when resizing now. It seems
like this is not what you want.

To fix what you want with the way you have it now, though:

Code:

           SDL_Surface* light_square = SDL_CreateRGBSurface

(SDL_HWSURFACE, min(width, height)/8, min(width, height)/8, 32, 0, 0, 0, 0);

           SDL_FillRect (light_square, NULL, SDL_MapRGB

(screen->format, 255, 206, 158));

           SDL_Surface* dark_square = SDL_CreateRGBSurface

(SDL_HWSURFACE, min(width, height)/8, min(width, height)/8, 32, 0, 0, 0, 0);

           SDL_FillRect (dark_square, NULL, SDL_MapRGB(screen->format,

209, 139, 71));


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hello nfries, thanks for trying to help me.
I’m a little bit lost. You’re saying that “you’re only drawing the squares when resizing now”, but from what I understood, Jonny D explained to me that I had to draw it only everytime I change the screen size. So in the end, what do I need to do ?

Your min idea is interesting, but it’s not what I want. I’d like to have a chessboard. If I insert the mins, the squares are not next to each other anymore.
I think there are (at least) 2 solutions to force the squares not to become rectangles (none of which I know how to program) :

  • forbid the user from resizing in any other way than in ‘diagonal’ (with the same aspect ratio).
  • Append a black rectangle on the bottom or the right part of the board, where the dimensions are too long.

Lilly wrote:

Hello nfries, thanks for trying to help me.
I’m a little bit lost. You’re saying that “you’re only drawing the squares when resizing now”, but from what I understood, Jonny D explained to me that I had to draw it only everytime I change the screen size. So in the end, what do I need to do ?

Your min idea is interesting, but it’s not what I want. I’d like to have a chessboard. If I insert the mins, the squares are not next to each other anymore.
I think there are (at least) 2 solutions to force the squares not to become rectangles (none of which I know how to program) :

  • forbid the user from resizing in any other way than in ‘diagonal’ (with the same aspect ratio).
  • Append a black rectangle on the bottom or the right part of the board, where the dimensions are too long.

You don’t need to recreate all surfaces when resizing, only the screen surface (because that’s the only one that changed).

and, to fix the code given by johnny to render rectangles and simultaneously fix your issue with my changes:

Code:
int i,j;
int size = min(width, height)/8;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position = {isize, jsize, size, size};
if ( (i + j) % 2 == 0)
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 255, 206, 158)); // light_square
else
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 209, 139, 71)); // Dark square
}
}

this should work perfectly.

Like he said, there’s no reason to use surfaces for the rectangles if you’re only using a solid color.

I’m a little bit lost as to how to put the pieces together, I’m not sure if it has to be inside or outside of the loops. Is there any chance one of you could copy paste the full code ?
Thanks in advance.

Lilly wrote:

I’m a little bit lost as to how to put the pieces together, I’m not sure if it has to be inside or outside of the loops. Is there any chance one of you could copy paste the full code ?
Thanks in advance.

It replaces his code in both places.

Yes, I understood that part. But what about the rest ? Does it need to be written before the loop as well, like this ? :

Code:

#ifdef __cplusplus
#include
#else
#include <stdlib.h>
#endif
#ifdef APPLE
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#define min(X, Y) ({ typeof (X) __x = (X), __y = (Y); (__x < __y) ? __x : __y; })

int main (int argc, char** argv)
{
// initialize SDL video
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
fprintf (stderr, “Unable to init SDL: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

// make sure SDL cleans up before exit
atexit(SDL_Quit);


// create a new window

int width = 400;
int height = 400;
SDL_Surface* screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
if ( !screen )
{
fprintf (stderr, “Unable to set video: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}

SDL_WM_SetCaption(“Position Editor”, NULL);

/*

// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
    fprintf(stderr, "Unable to load bitmap: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;

*/

int i,j;
int size = min(width, height)/8;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position = {isize, jsize, size, size};
if ( (i + j) % 2 == 0)
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 255, 206, 158)); // light_square
else
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 209, 139, 71)); // Dark square
}
}

// program main loop
bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {
           // exit if the window is closed
        case SDL_QUIT:
           done = true;
           break;


        case SDL_VIDEORESIZE:
        {
           width = event.resize.w;
           height = event.resize.h;
           
           screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
           break;
        }


           // check for keypresses
        case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                break;
            }
        } // end switch
    } // end of message processing
    
      //Drawing starts here
           //clear screen
           SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

int i,j;
int size = min(width, height)/8;
for (i = 0 ; i < 8 ; i++)
{
for (j = 0 ; j < 8 ; j++)
{
SDL_Rect position = {isize, jsize, size, size};
if ( (i + j) % 2 == 0)
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 255, 206, 158)); // light_square
else
SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 209, 139, 71)); // Dark square
}
}

      // DRAWING ENDS HERE


    // finally, update the screen Smile
    SDL_Flip(screen);
    SDL_Delay(1);




} // end main loop

/*
// free loaded bitmap
SDL_FreeSurface(bmp);
*/

// all is well Wink
printf("Exited cleanly\n");
return EXIT_SUCCESS;

}

I added some questions to the previous post.
Can someone try to answer them please ?

Many of us are on the mailing list, not the forum. I guess we can’t see
edits :confused:

"I find it weird that I have to type the same thing (the drawing) twice in
my code.
The first time before the loop, and the second time inside the loop. "
You don’t need to write the drawing code twice. You only need it in the
main loop. Our main points were that you need to be more careful about
where you create new surfaces (don’t do it in a loop unless you need to and
you clean up correctly) and you need to draw in the main loop, not before it
and not in the event-handling loop.

"I have some other questions as well, would you be kind enough to answer
them please ? "
You do ask nicely :wink:

"- Why doesn’t it work if I replace PollEvent by WaitEvent ? I thought the
only difference is that PollEvent is better suited for windows using time
controls, so that some things can happen even when no event occur. But I get
a black screen… "
SDL_WaitEvent causes the program to wait for an event. In the meantime,
your loop won’t execute and the drawing code won’t be run. SDL_PollEvent
reads the event queue and then keeps going.

"- When I start playing with the resize tool, I find that sometimes the
board is neither touching the bottom of the window nor its right side.
Shouldn’t it get filled in to the minimum length between width and height
? "
You have to do this yourself. You can center the board by adding offsets to
the drawing positions or you can scale the squares as mentioned previously.
Scaling them will still leave a border on one side if you want to keep the
rects square.

"- I also noticed that the window size isn’t updated until I remove my
finger from the mouse click button. Is it possible to update the window size
automatically, as it is for other system’s windows ? "
I dunno. What OS did you say you’re on?

"- I’m using codeblocks and windows xp. When I run my program, there’s
cmd.exe in the background. Isn’t there a way to make it not appear like that
? "
Oh, you’re on WinXP :slight_smile:
In Code::Blocks, go to the menu item Projects->Properties…
Choose the ‘Build targets’ tab and change the ‘Type’ to ‘Gui application’.

"- My next step will be to resize images. How can I resize bitmap images ? "
Use SDL_gfx’s rotozoom or Sprig’s SPG_Scale.

"- Would it be possible to use vector (svg) images so that the image quality
is not altered while resizing ? "
Yes. I don’t know the state of projects to bring SVG to SDL, but try
Googling for ‘sdl svg’. Maybe SDL_image can load SVG, but of course it’d be
rasterized.

Jonny DOn Wed, Mar 3, 2010 at 11:40 AM, Lilly wrote:

I added some questions to the previous post.
Can someone try to answer them please ?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks for the answers, it helped me a lot.
As you noticed, I said that I’m using windows xp. But you didn’t answer my question : how to automatically update the window’s size as the mouse moves, instead of just updating it when the left button is released ?

Lilly wrote:

Yes, I understood that part. But what about the rest ? Does it need to be written before the loop as well, like this (see the code below) ?
I find it weird that I have to type the same thing (the drawing) twice in my code.
The first time before the loop, and the second time inside the loop.

You don’t actually have to do that.

Lilly wrote:

  • Why doesn’t it work if I replace PollEvent by WaitEvent ? I thought the only difference is that PollEvent is better suited for windows using time controls, so that some things can happen even when no event occur. But I get a black screen…

Call SDL_Flip(screen) before the loop as well, it should fix that.
However if you plan on implementing an AI opponent for your checkers/chess game, then stick with PollEvent.

Lilly wrote:

When I start playing with the resize tool, I find that sometimes the board is neither touching the bottom of the window nor its right side. Shouldn’t it get filled in to the minimum length between width and height ?

I also noticed that the window size isn’t updated until I remove my finger from the mouse click button. Is it possible to update the window size automatically, as it is for other system’s windows ?

SDL doesn’t seem to put out “SDL_VIDEORESIZE” events during the resize operation, only at the end. It’s probably better this way for advanced UI apps (constantly resizing might cause choppiness in rendering as things are shifted).
You can parse system window manager events for resizing events if you want, but this will be different for every system.

Lilly wrote:

I’m using codeblocks and windows xp. When I run my program, there’s cmd.exe in the background. Isn’t there a way to make it not appear like that ?

Tell Code::Blocks that your program is a GUI program. It thinks it’s a Console program.

Lilly wrote:

My next step will be to resize images. How can I resize bitmap images ?

SDL 1.2 does not provide this functionality. You will need to use a separate library; Sprig, SGE, or SDL_gfx.

I may have missed it, but I don’t recall seeing anyone else answer this. Have
you tried catching the SDL_MOUSEMOTION event?

You’ll get a boatload of them, so you may want to resize the window only if,
say, 1/10 second has passed since having done the last window resize.

JeffOn Wednesday 03 March 2010 13:43, Lilly wrote:

Thanks for the answers, it helped me a lot.
As you noticed, I said that I’m using windows xp. But you didn’t answer my
question : how to automatically update the window’s size as the mouse
moves, instead of just updating it when the left button is released ?

Thanks for the answers, they’re very helpful and appreciated.
I have yet another question : I sometimes get this warning while initializing a SDL_rect :
warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

Do you have any idea why ?