Hey everybody… I’m getting a segmentation fault while doing what would
seem to be a basic arithmetic operation, and I’ve having a bitch of a
time trying to figure it out. Hopefully somebody can shed some light on
the problem.
I’m using SDL-1.2.2 with VisualC++ 6.0 Standard Edition on Windows 98. I
have DirectX 8.0a, and am using it with a SiS6326-based video card.
The offending code is below. The segmentation fault (and the
corresponding SDL parachute) occur between the output of “Entering
function” and “Before blit”. This would suggest that it bombs out while
modifying SDL_Rect position for some reason.
Any help would be appreciated. Thanks in advance!
#include <stdio.h>
#include “SDL.h”
#include “SDL_image.h”
#define WINSIZEX 700
#define WINSIZEY 500
SDL_Surface *screen = NULL;
void drawcard (SDL_Surface *card, int x, int y)
{
SDL_Rect position;
printf(“Entering function\n”);
position.x = x - (card->w / 2);
position.y = y - (card->h / 2);
position.w = card->w;
position.h = card->h;
printf(“Before blit\n”);
if (SDL_BlitSurface(card, NULL, screen, &position) < 0)
printf(“BlitSurface error: %s\n”, SDL_GetError());
printf(“Before update\n”);
SDL_UpdateRects(screen, 1, &position);
}
int main(int argc, char *argv[])
{
Uint8 video_bpp = 32;
Uint32 videoflags;
SDL_Surface *card;
SDL_Event event;
int done, mousex, mousey;
videoflags = SDL_SWSURFACE;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, “Couldn’t initialize SDL: %s\n”,SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
if ((screen = SDL_SetVideoMode(WINSIZEX, WINSIZEY, video_bpp,
videoflags)) == NULL ) {
fprintf(stderr, “Couldn’t set %dx%dx%d video mode: %s\n”, WINSIZEX,
WINSIZEY, video_bpp, SDL_GetError());
exit(2);
}
SDL_WM_SetCaption(“Blah”, NULL);
SDL_FillRect(screen, NULL, 0x338833);
SDL_UpdateRect(screen, 0, 0, 0, 0);
card = IMG_Load(“cardbase.gif”);
done = 0;
while (!done) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
done = 1;
break;
case SDL_MOUSEBUTTONDOWN:
if (SDL_GetMouseState(&mousex, &mousey) & SDL_BUTTON(1)) {
drawcard(card, mousex, mousey);
}
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
}
return(0);
}