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 >= 8square_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