Resizable windows

It’s be easier to answer if you provided the line of code that is causing
the warning. This is a warning about a feature that is not yet in the core
language (C++0x is the next C++ standard). Without seeing your code, I’d
suspect you’re trying something like so:
SDL_Rect r{something, something, something, something};

The solution: Don’t do it. Try:
SDL_Rect r = {something, something, something, something};

Jonny DOn Fri, Mar 5, 2010 at 11:28 AM, Lilly wrote:

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 ?


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

That indicates you are using {} to initialise the SDL_Rect. Can you
show us an example of code that produces this warning? It might be a
false positive on the compilers part, but more likely you are using {}
somewhere the standard doesn’t allow.On 5 March 2010 16:28, Lilly wrote:

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 ?


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

Wow you were really fast this time, I didn’t even had time to edit properly :D. Thanks I love you guys.

Here is my code so far. The first warning appears line 121 (if you don’t have a line count, use ctrl+f : position)

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; })
#define max(X, Y) ({ typeof (X) __x = (X), __y = (Y); (__x > __y) ? __x : __y; })

#define INIT_WIDTH 400
#define INIT_HEIGHT 400

#define MIN_WIDTH 150
#define MIN_HEIGHT 150

#define FRAME_TICKNESS 1
#define VLINE_TICKNESS 2

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 = INIT_WIDTH;
int height = INIT_HEIGHT;
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("Chess 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;

*/

// 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 = max (MIN_WIDTH,event.resize.w);
           height = max (MIN_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, 0xC0, 0xC0, 0xC0));


	int i,j;
	SDL_Rect position;
	int square_size = min ( (width - 2 * FRAME_TICKNESS - VLINE_TICKNESS) / 10, (height - 2 * FRAME_TICKNESS) / 8);

	//8*8 squares
	for (i = 0 ; i < 8 ; i++)
	{
		for (j = 0 ; j < 8 ; j++)
		{
			position = {FRAME_TICKNESS+i*square_size, FRAME_TICKNESS+j*square_size, square_size, square_size};
			if ( (i + j) % 2 == 0)
				SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0xCC, 0xBD, 0xA4));  // light square
			else
				SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0xA8, 0x89, 0x64));  // dark square
		}
	}

	//vertical line separation between board and menu
	position = {FRAME_TICKNESS+8*square_size,FRAME_TICKNESS,VLINE_TICKNESS,8*square_size};
	SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0, 0, 0));

	//menu
	position = {8*square_size+VLINE_TICKNESS+FRAME_TICKNESS,FRAME_TICKNESS,2*square_size,8*square_size};
	SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0xCC, 0xBD, 0xA4));

	//right border
	position = {10*square_size+VLINE_TICKNESS+FRAME_TICKNESS,0,FRAME_TICKNESS,8*square_size+2*FRAME_TICKNESS};
	SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0, 0, 0));

	//bottom border
	position = {0,8*square_size+FRAME_TICKNESS,10*square_size+VLINE_TICKNESS+2*FRAME_TICKNESS,FRAME_TICKNESS};
	SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0, 0, 0));

	//upper border
	position = {0,0,10*square_size+VLINE_TICKNESS+2*FRAME_TICKNESS,FRAME_TICKNESS};
	SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0, 0, 0));

	//left border
	position = {0,0,FRAME_TICKNESS,8*square_size+2*FRAME_TICKNESS};
	SDL_FillRect(screen, &position, SDL_MapRGB(screen->format, 0, 0, 0));

	// 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;

}

typedef struct {int x; int y;} Coord;

Coord SquareClicked(Coord p, int width, int height)
{
Coord square;
int x = p.x, y = p.y;
int size = min((width-4)/10, (height-2)/8);
if (x <= 0 || x >= 10size+4 || y <= 0 || y >= 8size+2 || x == 8size+1 || x == 8size+2)
{
square.x = -1;
square.y = -1;
return square;
}
if(x < 8*size+1)
square.x = x/size;
else
square.x = (x-2)/size;
square.y = y/size;
return square ;
}

Pre C++0x, that syntax is illegal. You can only use brace initialisation
during variable initialisation. You can use a helper function, like so:

SDL_Rect make_rect(int x, int y, int w, int h) {
SDL_Rect rect = {x, y, w, h};
return rect;
}

// later
position = make_rect(FRAME_TICKNESS+isquare_size,
FRAME_TICKNESS+j
square_size, square_size, square_size);On 5 March 2010 18:17, Lilly wrote:

Wow you were really fast this time, I didn’t even had time to edit
properly [image: Very Happy]. Thanks I love you guys.

Here is my code so far. The first warning appears line 121 (if you don’t
have a line count, use ctrl+f : position)

Ok Brian, thanks.

I’m sorry to you all, I’m used to editing my posts in forums. I’ll try to do it less often. Due to that, there’s an unanswered question regarding gfx.

Jeff Post, I’m so sorry I just noticed your answer. It’s probably because it’s the first post of page 2.
Your idea seems very interesting. I don’t really understand how to put together SDL_MOUSEMOTION and SDL_VIDEORESIZE. I made a test sample that is probably close to the solution, so that you can edit it and show me how to implement it. Sorry for the indentation problems that occured while copy/pasting. I hope it’s still readable.

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 | SDL_INIT_TIMER) < 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 = 600;
int height = 400;
SDL_Surface* screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);



bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {

        case SDL_MOUSEMOTION:
        {
	currentTime = SDL_GetTicks();
	if (currentTime - PreviousTime > 10) 
	{
	      width = event.resize.w;
              height = event.resize.h;
                  screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
	      PreviousTime = currentTime ;
	}

           break;
        }


        } // end switch
    } // end of message processing

	


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

} // end main loop


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

}

Jeff Post, I’m so sorry I just noticed your answer. It’s probably because
it’s the first post of page 2. Your idea seems very interesting. I don’t
really understand how to put together SDL_MOUSEMOTION and SDL_VIDEORESIZE.
I made a test sample that is probably close to the solution, so that you
can edit it and show me how to implement it. Sorry for the indentation
problems that occured while copy/pasting. I hope it’s still readable.

My needs were a little simpler than what I assume yours are, so I didn’t use a
timer. Rather I just sucked up MOUSEMOTION events until some other event
arrived, and then pushed that other event back on the event stack:

    case SDL_MOUSEMOTION:
       processCB(&event);

       while (SDL_PollEvent(&event))
       {
          if (event.type != SDL_MOUSEMOTION)
          {
             SDL_PushEvent(&event);
             break;
          }
       }
       break;

    case SDL_VIDEORESIZE:
       w = event.resize.w;
       h = event.resize.h;
       screen = SDL_SetVideoMode(w, h, SCREEN_PIXEL_DEPTH, SDL_RESIZABLE);
       pitch = screen->pitch;
       resizeGUI();
       updateScreen();
       SDL_UpdateRect(screen, 0, 0, 0, 0);
       break;

I assume you might want to do some processing on MOUSEMOTION events, and you’d
want your screen updates to be smoother than what I needed, that’s why I
suggested using a timer.

The MOUSEMOTION event doesn’t have event.resize entries (at least in the 1.2.x
version I was using at the time–don’t know about 1.3), so you will have to
catch the VIDEORESIZE event.

You’ll need some booleans to coordinate your responses to both events. For
example, resizePending and motionTimerExpired so that you do the visible
resizing in a smooth manner. (It didn’t matter in my application if the
resizing was a bit jerky, but that might be unacceptable for other
applications.)

HTH,
JeffOn Friday 05 March 2010 10:55, Lilly wrote:

Code:

bool done = false;
while (!done)
{
    // message processing loop
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {

        case SDL_MOUSEMOTION:
        {

currentTime = SDL_GetTicks();
if (currentTime - PreviousTime > 10)
{
width = event.resize.w; // ****** Doesn’t exist in motion event,
height = event.resize.h; // ****** nor does this
screen = SDL_SetVideoMode(width, height, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); PreviousTime = currentTime ;
}

           break;
        }


        } // end switch
    } // end of message processing

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

} // end main loop

// all is well ;)

printf(“Exited cleanly\n”);
return EXIT_SUCCESS;
}

Ok, thanks.

I have one question that was left behind :

I’ve downloaded SDL_gfx as you advised me. Where am I supposed to extract it so that it works ?
Currently, my C:\Program Files\CodeBlocks contains codeblock.exe, a folder called mingw, a folder called SDL-devel-1.2.14-mingw32, and some other files and folders.

I was also wondering if it is possible to give a string that can be pasted in another software (either put it directly in the clipboard or open a message box containing the sentence to be selected and then copied or in another way I’m not thinking of).

Hello,

I come to you for yet another question :).

I have a problem at runtime and I think I found out where the problem is, but I’m not sure, so if everything I’m going to describe to you seems normal, maybe the problem comes from somewhere else. If so, please tell me.
So I’m trying to deal with images now, and in order to make them easily available within the code, I’d like to associate an int to each of them (a constant defined with #define). In order to make this association, I created a function SDL_Surface* image(int) with a big ‘switch’ so that image(KING) returns the image of the king.
The problem is that these images are not defined inside the image function, so I declared them as global images (prior to ‘main’). Thus they are declared as global, but they are only initialized in the ‘main’ function.
The program compiles but fails at runtime and tells me that the function ‘image’ actually returns a NULL pointer.

I hope the explanation of what I tried to do was clear enough. Here is the code (the problem at runtime appears line 277 according to the debugger)

(if you want to try it tell me so and I’ll find a way to send you the images so that you can compile it).

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; })
#define max(X, Y) ({ typeof (X) __x = (X), __y = (Y); (__x > __y) ? __x : __y; })

#define INIT_WIDTH 400
#define INIT_HEIGHT 400

#define MIN_WIDTH 150
#define MIN_HEIGHT 150

#define FRAME_TICKNESS 1
#define VLINE_TICKNESS 2

#define EMPTY 0
#define WKING 1
#define WQUEEN 2
#define WROOK 3
#define WBISHOP 4
#define WKNIGHT 5
#define WPAWN 6
#define BKING 7
#define BQUEEN 8
#define BROOK 9
#define BBISHOP 10
#define BKNIGHT 11
#define BPAWN 12

typedef struct {int x; int y;} Coord;

Coord SquareClicked(Coord p, int width, int height);
void make_rect(SDL_Rect* rect, int x, int y, int w, int h);
SDL_Surface* image(int PieceInHand);

SDL_Surface *wking_img, *wqueen_img, *wrook_img, *wbishop_img, *wknight_img, *wpawn_img;
SDL_Surface *bking_img, *bqueen_img, *brook_img, *bbishop_img, *bknight_img, *bpawn_img;

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 = INIT_WIDTH;
int height = INIT_HEIGHT;
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("Chess Position Editor", NULL);

// load all images
SDL_Surface* wking_img = SDL_LoadBMP(“wking.bmp”);
if (!wking_img)
{
fprintf(stderr, “Unable to load wking: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}
SDL_SetColorKey(wking_img, SDL_SRCCOLORKEY, SDL_MapRGB(wking_img->format, 255, 255, 0));

SDL_Surface* wqueen_img = SDL_LoadBMP("wqueen.bmp");
if (!wqueen_img)
{
    fprintf(stderr, "Unable to load bqueen: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(wqueen_img, SDL_SRCCOLORKEY, SDL_MapRGB(wqueen_img->format, 255, 255, 0));

SDL_Surface* wrook_img = SDL_LoadBMP("wrook.bmp");
if (!wrook_img)
{
    fprintf(stderr, "Unable to load wrook: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(wrook_img, SDL_SRCCOLORKEY, SDL_MapRGB(wrook_img->format, 255, 255, 0));

SDL_Surface* wbishop_img = SDL_LoadBMP("wbishop.bmp");
if (!wbishop_img)
{
    fprintf(stderr, "Unable to load wbishop: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(wbishop_img, SDL_SRCCOLORKEY, SDL_MapRGB(wbishop_img->format, 255, 255, 0));

SDL_Surface* wknight_img = SDL_LoadBMP("wknight.bmp");
if (!wknight_img)
{
    fprintf(stderr, "Unable to load wknight: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(wknight_img, SDL_SRCCOLORKEY, SDL_MapRGB(wknight_img->format, 255, 255, 0));

SDL_Surface* wpawn_img = SDL_LoadBMP("wpawn.bmp");
if (!wpawn_img)
{
    fprintf(stderr, "Unable to load wpawn: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(wpawn_img, SDL_SRCCOLORKEY, SDL_MapRGB(wpawn_img->format, 255, 255, 0));

SDL_Surface* bking_img = SDL_LoadBMP("bking.bmp");
if (!bking_img)
{
    fprintf(stderr, "Unable to load bking: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(bking_img, SDL_SRCCOLORKEY, SDL_MapRGB(bking_img->format, 255, 255, 0));

SDL_Surface* bqueen_img = SDL_LoadBMP("bqueen.bmp");
if (!bqueen_img)
{
    fprintf(stderr, "Unable to load bqueen: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(bqueen_img, SDL_SRCCOLORKEY, SDL_MapRGB(bqueen_img->format, 255, 255, 0));

SDL_Surface* brook_img = SDL_LoadBMP("brook.bmp");
if (!brook_img)
{
    fprintf(stderr, "Unable to load brook: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(brook_img, SDL_SRCCOLORKEY, SDL_MapRGB(brook_img->format, 255, 255, 0));

SDL_Surface* bbishop_img = SDL_LoadBMP("bbishop.bmp");
if (!bbishop_img)
{
    fprintf(stderr, "Unable to load bbishop: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(bbishop_img, SDL_SRCCOLORKEY, SDL_MapRGB(bbishop_img->format, 255, 255, 0));

SDL_Surface* bknight_img = SDL_LoadBMP("bknight.bmp");
if (!bknight_img)
{
    fprintf(stderr, "Unable to load bknight: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(bknight_img, SDL_SRCCOLORKEY, SDL_MapRGB(bknight_img->format, 255, 255, 0));

SDL_Surface* bpawn_img = SDL_LoadBMP("bpawn.bmp");
if (!bpawn_img)
{
    fprintf(stderr, "Unable to load bpawn: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}
SDL_SetColorKey(bpawn_img, SDL_SRCCOLORKEY, SDL_MapRGB(bpawn_img->format, 255, 255, 0));





int PieceInHand = BBISHOP; //debug
SDL_Rect MousePosition = {0,0,0,0};


// 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 = max (MIN_WIDTH,event.resize.w);
           height = max (MIN_HEIGHT,event.resize.h);

           screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
           break;
        }

		case SDL_MOUSEMOTION:
		{
			MousePosition.x = event.motion.x;
			MousePosition.y = event.motion.y;
		}

		// 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, 0xC0, 0xC0, 0xC0));


	int i,j;
	SDL_Rect rect;
	int square_size = min ( (width - 2 * FRAME_TICKNESS - VLINE_TICKNESS) / 10, (height - 2 * FRAME_TICKNESS) / 8);

	//8*8 squares
	for (i = 0 ; i < 8 ; i++)
	{
		for (j = 0 ; j < 8 ; j++)
		{
			make_rect(&rect, FRAME_TICKNESS+i*square_size, FRAME_TICKNESS+j*square_size, square_size, square_size);
			if ( (i + j) % 2 == 0)
				SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xCC, 0xBD, 0xA4));  // light square
			else
				SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xA8, 0x89, 0x64));  // dark square
		}
	}

	//vertical line separation between board and menu
	make_rect(&rect,FRAME_TICKNESS+8*square_size,FRAME_TICKNESS,VLINE_TICKNESS,8*square_size);
	SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

	//menu
	make_rect(&rect,8*square_size+VLINE_TICKNESS+FRAME_TICKNESS,FRAME_TICKNESS,2*square_size,8*square_size);
	SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xCC, 0xBD, 0xA4));

	//right border
	make_rect(&rect,10*square_size+VLINE_TICKNESS+FRAME_TICKNESS,0,FRAME_TICKNESS,8*square_size+2*FRAME_TICKNESS);
	SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

	//bottom border
	make_rect(&rect,0,8*square_size+FRAME_TICKNESS,10*square_size+VLINE_TICKNESS+2*FRAME_TICKNESS,FRAME_TICKNESS);
	SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

	//upper border
	make_rect(&rect,0,0,10*square_size+VLINE_TICKNESS+2*FRAME_TICKNESS,FRAME_TICKNESS);
	SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

	//left border
	make_rect(&rect,0,0,FRAME_TICKNESS,8*square_size+2*FRAME_TICKNESS);
	SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));


	if (PieceInHand != EMPTY)
	{
		SDL_Rect ImagePosition;
		SDL_Surface* Piece = image(PieceInHand);
		ImagePosition.x = (MousePosition.x - Piece->w) /2;
		ImagePosition.y = (MousePosition.y - Piece->h) /2;
		SDL_BlitSurface(Piece, NULL, screen, &MousePosition);
	}


	// 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;

}

Coord SquareClicked(Coord p, int width, int height)
{
Coord square;
int x = p.x, y = p.y;
int square_size = min ( (width - 2 * FRAME_TICKNESS - VLINE_TICKNESS) / 10, (height - 2 * FRAME_TICKNESS) / 8);
if (x < 0 + FRAME_TICKNESS ||
x >= 10 * square_size + 2 * FRAME_TICKNESS + VLINE_TICKNESS ||
y < 0 + FRAME_TICKNESS ||
y >= 8square_size + 2 * FRAME_TICKNESS ||
(x >= 8
square_size + FRAME_TICKNESS && x < 8square_size + FRAME_TICKNESS + VLINE_TICKNESS))
{
square.x = -1;
square.y = -1;
return square;
}
if(x < 8
square_size + FRAME_TICKNESS)
square.x = (x - FRAME_TICKNESS) /square_size;
else
square.x = (x- FRAME_TICKNESS - VLINE_TICKNESS) / square_size;
square.y = (y - FRAME_TICKNESS) / square_size;
return square;
}

void make_rect(SDL_Rect* rect, int x, int y, int w, int h)
{
rect->x = x ; rect->y = y ; rect->w = w ; rect->h = h;
}

SDL_Surface* image(int PieceInHand)
{
switch(PieceInHand)
{
case EMPTY:
return NULL;
break;
case WKING:
return wking_img;
break;
case WQUEEN:
return wqueen_img;
break;
case WROOK:
return wrook_img;
break;
case WBISHOP:
return wbishop_img;
break;
case WKNIGHT:
return wknight_img;
break;
case WPAWN:
return wpawn_img;
break;
case BKING:
return bking_img;
break;
case BQUEEN:
return bqueen_img;
break;
case BROOK:
return brook_img;
break;
case BBISHOP:
return bbishop_img;
break;
case BKNIGHT:
return bknight_img;
break;
case BPAWN:
return bpawn_img;
break;
default:
return NULL;
break;
}
}

One reason may be that you’re creating local variables to hold your image
data, e.g.:
SDL_Surface* wking_img = SDL_LoadBMP(“wking.bmp”);

Use the global variable instead:
wking_img = SDL_LoadBMP(“wking.bmp”);

Jonny DOn Sun, Mar 7, 2010 at 9:37 AM, Lilly wrote:

Hello,

I come to you for yet another question [image: Smile].

I have a problem at runtime and I think I found out where the problem is,
but I’m not sure, so if everything I’m going to describe to you seems
normal, maybe the problem comes from somewhere else. If so, please tell me.
So I’m trying to deal with images now, and in order to make them easily
available within the code, I’d like to associate an int to each of them (a
constant defined with #define). In order to make this association, I created
a function SDL_Surface* image(int) with a big ‘switch’ so that image(KING)
returns the image of the king.
The problem is that these images are not defined inside the image function,
so I declared them as global images (prior to ‘main’). Thus they are
declared as global, but they are only initialized in the ‘main’ function.
The program compiles but fails at runtime and tells me that the function
’image’ actually returns a NULL pointer.

I hope the explanation of what I tried to do was clear enough. Here is the
code (the problem at runtime appears line 277 according to the debugger)

(if you want to try it tell me so and I’ll find a way to send you the
images so that you can compile it).

Code:

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

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

#define INIT_WIDTH 400
#define INIT_HEIGHT 400

#define MIN_WIDTH 150
#define MIN_HEIGHT 150

#define FRAME_TICKNESS 1
#define VLINE_TICKNESS 2

#define EMPTY 0
#define WKING 1
#define WQUEEN 2
#define WROOK 3
#define WBISHOP 4
#define WKNIGHT 5
#define WPAWN 6
#define BKING 7
#define BQUEEN 8
#define BROOK 9
#define BBISHOP 10
#define BKNIGHT 11
#define BPAWN 12

typedef struct {int x; int y;} Coord;

Coord SquareClicked(Coord p, int width, int height);
void make_rect(SDL_Rect* rect, int x, int y, int w, int h);
SDL_Surface* image(int PieceInHand);

SDL_Surface *wking_img, *wqueen_img, *wrook_img, *wbishop_img,
*wknight_img, *wpawn_img;
SDL_Surface *bking_img, *bqueen_img, *brook_img, *bbishop_img,
*bknight_img, *bpawn_img;

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 = INIT_WIDTH;
int height = INIT_HEIGHT;
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(“Chess Position Editor”, NULL);

// load all images
SDL_Surface* wking_img = SDL_LoadBMP(“wking.bmp”);
if (!wking_img)
{
fprintf(stderr, “Unable to load wking: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}
SDL_SetColorKey(wking_img, SDL_SRCCOLORKEY,
SDL_MapRGB(wking_img->format, 255, 255, 0));

SDL_Surface* wqueen_img = SDL_LoadBMP("wqueen.bmp");
if (!wqueen_img)
{
    fprintf(stderr, "Unable to load bqueen: %s\n", SDL_GetError());
    return EXIT_FAILURE;
}

SDL_SetColorKey(wqueen_img, SDL_SRCCOLORKEY,
SDL_MapRGB(wqueen_img->format, 255, 255, 0));

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

SDL_SetColorKey(wrook_img, SDL_SRCCOLORKEY,
SDL_MapRGB(wrook_img->format, 255, 255, 0));

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

SDL_SetColorKey(wbishop_img, SDL_SRCCOLORKEY,
SDL_MapRGB(wbishop_img->format, 255, 255, 0));

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

SDL_SetColorKey(wknight_img, SDL_SRCCOLORKEY,
SDL_MapRGB(wknight_img->format, 255, 255, 0));

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

SDL_SetColorKey(wpawn_img, SDL_SRCCOLORKEY,
SDL_MapRGB(wpawn_img->format, 255, 255, 0));

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

SDL_SetColorKey(bking_img, SDL_SRCCOLORKEY,
SDL_MapRGB(bking_img->format, 255, 255, 0));

SDL_Surface* bqueen_img = SDL_LoadBMP(“bqueen.bmp”);
if (!bqueen_img)
{
fprintf(stderr, “Unable to load bqueen: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}
SDL_SetColorKey(bqueen_img, SDL_SRCCOLORKEY,
SDL_MapRGB(bqueen_img->format, 255, 255, 0));

SDL_Surface* brook_img = SDL_LoadBMP(“brook.bmp”);
if (!brook_img)
{
fprintf(stderr, “Unable to load brook: %s\n”, SDL_GetError());
return EXIT_FAILURE;
}
SDL_SetColorKey(brook_img, SDL_SRCCOLORKEY,
SDL_MapRGB(brook_img->format, 255, 255, 0));

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

SDL_SetColorKey(bbishop_img, SDL_SRCCOLORKEY,
SDL_MapRGB(bbishop_img->format, 255, 255, 0));

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

SDL_SetColorKey(bknight_img, SDL_SRCCOLORKEY,
SDL_MapRGB(bknight_img->format, 255, 255, 0));

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

SDL_SetColorKey(bpawn_img, SDL_SRCCOLORKEY,
SDL_MapRGB(bpawn_img->format, 255, 255, 0));

int PieceInHand = BBISHOP; //debug
SDL_Rect MousePosition = {0,0,0,0};

// 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 = max (MIN_WIDTH,event.resize.w);
           height = max (MIN_HEIGHT,event.resize.h);

           screen = SDL_SetVideoMode(width, height, 32,

SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE);
break;
}

     case SDL_MOUSEMOTION:
     {
        MousePosition.x = event.motion.x;
        MousePosition.y = event.motion.y;

     }

     // 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, 0xC0, 0xC0,

0xC0));

  int i,j;
  SDL_Rect rect;

  int square_size = min ( (width - 2 * FRAME_TICKNESS - VLINE_TICKNESS)

/ 10, (height - 2 * FRAME_TICKNESS) / 8);

  //8*8 squares
  for (i = 0 ; i < 8 ; i++)
  {
     for (j = 0 ; j < 8 ; j++)
     {
        make_rect(&rect, FRAME_TICKNESS+i*square_size,

FRAME_TICKNESS+j*square_size, square_size, square_size);

        if ( (i + j) % 2 == 0)
           SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xCC,

0xBD, 0xA4)); // light square
else
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xA8,
0x89, 0x64)); // dark square

     }
  }

  //vertical line separation between board and menu

make_rect(&rect,FRAME_TICKNESS+8square_size,FRAME_TICKNESS,VLINE_TICKNESS,8square_size);
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

  //menu

make_rect(&rect,8square_size+VLINE_TICKNESS+FRAME_TICKNESS,FRAME_TICKNESS,2square_size,8*square_size);
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xCC, 0xBD,
0xA4));

  //right border

make_rect(&rect,10square_size+VLINE_TICKNESS+FRAME_TICKNESS,0,FRAME_TICKNESS,8square_size+2*FRAME_TICKNESS);
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

  //bottom border

make_rect(&rect,0,8square_size+FRAME_TICKNESS,10square_size+VLINE_TICKNESS+2*FRAME_TICKNESS,FRAME_TICKNESS);
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

  //upper border

make_rect(&rect,0,0,10square_size+VLINE_TICKNESS+2FRAME_TICKNESS,FRAME_TICKNESS);
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));

  //left border
  make_rect(&rect,0,0,FRAME_TICKNESS,8*square_size+2*FRAME_TICKNESS);
  SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));


  if (PieceInHand != EMPTY)
  {
     SDL_Rect ImagePosition;
     SDL_Surface* Piece = image(PieceInHand);
     ImagePosition.x = (MousePosition.x - Piece->w) /2;
     ImagePosition.y = (MousePosition.y - Piece->h) /2;
     SDL_BlitSurface(Piece, NULL, screen, &MousePosition);

  }


  // 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;
}

Coord SquareClicked(Coord p, int width, int height)
{
Coord square;
int x = p.x, y = p.y;
int square_size = min ( (width - 2 * FRAME_TICKNESS - VLINE_TICKNESS) /
10, (height - 2 * FRAME_TICKNESS) / 8);
if (x < 0 + FRAME_TICKNESS ||
x >= 10 * square_size + 2 * FRAME_TICKNESS + VLINE_TICKNESS ||
y < 0 + FRAME_TICKNESS ||
y >= 8square_size + 2 * FRAME_TICKNESS ||
(x >= 8
square_size + FRAME_TICKNESS && x < 8*square_size +
FRAME_TICKNESS + VLINE_TICKNESS))

{
square.x = -1;
square.y = -1;
return square;
}
if(x < 8*square_size + FRAME_TICKNESS)
square.x = (x - FRAME_TICKNESS) /square_size;
else
square.x = (x- FRAME_TICKNESS - VLINE_TICKNESS) / square_size;
square.y = (y - FRAME_TICKNESS) / square_size;
return square;
}

void make_rect(SDL_Rect* rect, int x, int y, int w, int h)
{
rect->x = x ; rect->y = y ; rect->w = w ; rect->h = h;
}

SDL_Surface* image(int PieceInHand)
{
switch(PieceInHand)
{
case EMPTY:
return NULL;
break;
case WKING:
return wking_img;
break;
case WQUEEN:
return wqueen_img;
break;
case WROOK:
return wrook_img;
break;
case WBISHOP:
return wbishop_img;
break;
case WKNIGHT:
return wknight_img;
break;
case WPAWN:
return wpawn_img;
break;
case BKING:
return bking_img;
break;
case BQUEEN:
return bqueen_img;
break;
case BROOK:
return brook_img;
break;
case BBISHOP:
return bbishop_img;
break;
case BKNIGHT:
return bknight_img;
break;
case BPAWN:
return bpawn_img;
break;
default:
return NULL;
break;
}
}


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

Of course !!
Thanks a lot !

It just amazes me how quickly you see these stupid mistakes, whereas I have trouble reading my own code and spend hours trying to figure out what’s wrong.

Is there any chance you could be kind enough to take the time to answer my question regarding the installation of gfx and the ability to return a string that can be pasted elsewhere ?

I have 2 other small questions :

  • How can I check if maj is pressed ?
    This is what I have :

Code:
case SDLK_k:
PieceInHand = WKING;
break;

I’d like to return PieceInHand = BKING instead if both k and maj is pressed.
I saw that I’m supposed to use KMOD_SHIFT, but how exactly ?

  • My keyboard is not a qwerty but azerty. Yet SDLK_q only occurs when I press A. Is there a way I can change that ?

C++ is not that different from a human language. Noticing grammar mistakes
quickly just takes experience.

You can check out this article I wrote about installing SDL:
http://code.bluedinosaurs.com/tutorials/installSDL.html

It covers how to install C/C++ libraries in general, so the same advice
should work for SDL_gfx.

Copy & paste is specific to your operating system. You could look up the
appropriate calls on MSDN
(http://msdn.microsoft.comhttp://msdn.microsoft.com/en-us/default.aspx)
or Google around for a better way. SDL_scrap might be something worth
looking at, but I haven’t.

Check the SDL documentation (http://www.libsdl.org/docs.php) for
SDL_EnableUNICODE. This may be what you’re after. You can check the
unicode field of a keypress event, which will have the appropriate character
regardless of the key mapping. I’m not sure how to figure it out with just
keysyms. There is another field in the keypress event, called ‘mod’. This
lets you check what modifiers (shift, alt, etc.) are applied. Look in the
SDL docs for the SDL_Event structure and poke around to learn more about the
various events and fields.

If you need answers to further questions, you should start new forum
threads. It’ll help other people recognize that you still need help.

See ya,
Jonny DOn Sun, Mar 7, 2010 at 12:09 PM, Lilly wrote:

I have 2 other small questions :

  • How can I check if maj is pressed ?
    This is what I have :

Code:

case SDLK_k:
PieceInHand = WKING;
break;

I’d like to return PieceInHand = BKING instead if both k and maj is
pressed.
I saw that I’m supposed to use KMOD_SHIFT, but how exactly ?

  • My keyboard is not a qwerty but azerty. Yet SDLK_q only occurs when I
    press A. Is there a way I can change that ?

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

Thanks for the answers. I’m sorry I got carried away, I should have created other threads indeed.

My main issue (according to the name of my thread : resizing windows) isn’t solved though. I tried to apply what Jeff told me but I didn’t manage to make it work. I want to resize dynamically, as opposed to only when the mouse bouton is released.
Before closing this thread for good, is there any chance someone could give me a fully working ‘main’ with this feature ?

Thanks.

As far as I know your main thread appears to pause when the windows is
being resized, at least on Win32.

A long time ago I found a really awful hack around this, I described
it here: http://www.gamedev.net/community/forums/topic.asp?topic_id=428022

Disclaimer: it isn’t pretty, and I don’t know if it will play nicely
with recent versions of SDL, in particular SDL 1.3.On 7 March 2010 21:46, Lilly wrote:

Thanks for the answers. I’m sorry I got carried away, I should have created
other threads indeed.

My main issue (according to the name of my thread : resizing windows) isn’t
solved though. I tried to apply what Jeff told me but I didn’t manage to
make it work. I want to resize dynamically, as opposed to only when the
mouse bouton is released.
Before closing this thread for good, is there any chance someone could give
me a fully working ‘main’ with this feature ?

Hmm…
On Linux, using that code, I place the mouse pointer over a corner or edge of
the window, hold down the left mouse button, drag the mouse, and the window
resizes as the mouse moves. It stops resizing when the mouse button is
released.

I never tried it on Windows, mainly because I don’t have Windows, but I don’t
see why it wouldn’t work. Maybe Windows only sends the resize event when the
button is released? Are you updating the screen when you get a resize event?
? ? ? ? ? ?SDL_UpdateRect(screen, 0, 0, 0, 0);

JeffOn Sunday 07 March 2010 13:46, Lilly wrote:

I tried to apply what Jeff told me but I didn’t manage to
make it work. I want to resize dynamically, as opposed to only when the
mouse bouton is released.

Can someone help me please ?

There’s no need to read and understand my code, I just want you to help me to understand the following code :

Code:

include “SDL.h”

int x,y;
int vx,vy;

void draw()
{
SDL_Surface * screen = SDL_GetVideoSurface();
SDL_FillRect(screen,NULL,0);
SDL_Rect rect = {x,y,20,20};
x += vx;
y += vy;
if( x > screen->w - 20 || x < 0 )
{
x = x<0? 0 : screen->w - 20;
vx = -vx;
}
if( y > screen->h - 20 || y < 0 )
{
y = y<0 ? 0 : screen->h - 20;
vy = -vy;
}
SDL_FillRect(screen,&rect,0xfffffff);
SDL_Flip(screen);
SDL_Delay(10);
}

int eventFilter( const SDL_Event *e )
{
if( e->type == SDL_VIDEORESIZE )
{
SDL_SetVideoMode(e->resize.w,e->resize.h,0,SDL_ANYFORMAT | SDL_RESIZABLE);
draw();
}
return 1; // return 1 so all events are added to queue
}

int main( int , char ** )
{
SDL_Init( SDL_INIT_VIDEO );
#ifdef WIN32
SDL_SetEventFilter( &eventFilter );
#endif
SDL_SetVideoMode(800,600,0,SDL_ANYFORMAT | SDL_RESIZABLE);

x = y = 0;
vx = vy = 1;
bool running = true;
while(running)
{
    SDL_Event e;
    while(SDL_PollEvent(&e) )
    {
        if( e.type == SDL_QUIT )
        {
            running = false;
        }
    }
    draw();
}
SDL_Quit();
return 0;

}

and to tell me what exactly I need to extract from it (and where to put it) to make my window dynamically resizable.
I’m a beginner and I don’t really understand this code.

Thanks.