-
an odd SDL problem...
I am having a really weird problem with SDL right now, maybe someone can shed some light....
I have a surface which contains a picture of a mouse cursor on it, and I am displaying it to the screen. I have never had any problem displaying surfaces to the screen before, but this one just does not want to display for some reason...
Here is the code...
Code:
int main ( int argc, char * argv[] )
{
...SDL initialization code...
mouseRect = new SDL_Rect;
mousePosRect = new SDL_Rect;
while ( processMessages(event) )
{
drawObjects ( screen );
}
return 0;
}
bool processMessages ( SDL_Event &event )
{
while( SDL_PollEvent ( &event ) )
{
switch(event.type)
{
case SDL_MOUSEBUTTONDOWN:
switch(event.button.button)
{
case SDL_BUTTON_LEFT:
break;
case SDL_BUTTON_MIDDLE:
break;
case SDL_BUTTON_RIGHT:
break;
}
case SDL_MOUSEMOTION:
mouseX = event.motion.x;
mouseY = event.motion.y;
break;
}
}
return true;
}
void drawObjects ( SDL_Surface *screen )
{
SDL_BlitSurface ( mouseGraphic, mouseRect, screen, mousePosRect );
SDL_Flip(screen);
SDL_FillRect(screen, 0, 0);
}
Now....here is the weird thing...it DOES display correctly if I do this:
Code:
void drawObjects ( SDL_Surface *screen )
{
mouseRect = new SDL_Rect();
mousePosRect = new SDL_Rect();
SDL_BlitSurface ( mouseGraphic, mouseRect, screen, mousePosRect );
SDL_Flip(screen);
SDL_FillRect(screen, 0, 0);
}
Of course that is stupid because all the memory allocations would quickly kill the computer, unless I used delete right after the call to SDL_BlitSurface().
But I dont want to have to do that, because the whole point of mousePosRect is to hold the position of the mouse so it knows where to blit the surface...
And, if I do this, it suddenly does not work again:
Code:
void drawObjects ( SDL_Surface *screen )
{
mouseRect = new SDL_Rect();
mousePosRect = new SDL_Rect;
mousePosRect->x = mouseX;
mousePosRect->y = mouseY;
SDL_BlitSurface ( mouseGraphic, mouseRect, screen, mousePosRect );
delete mouseRect;
delete mousePosRect;
SDL_Flip(screen);
SDL_FillRect(screen, 0, 0);
}
It does work if I take out these two lines:
mousePosRect->x = mouseX;
mousePosRect->y = mouseY;
when i say work, i mean at runtime, not compile time. It compiles fine under all scenarios.
-
well i got it to work using a slightly different method of doing things, but still, if you know what was wrong with that old code, please do tell....i want to know
-
I haven't tested this, but I think you are not filling the SDL_Rect completely. Not only to you have to fill in the rect.x and rect.y fields but also rect.w and rect.h