tags:

views:

34

answers:

1

I'm writing a simple program for testing mouse. It compiles fine, but doesn't work. When I launch it, the window freezes. What am I doing wrong?

#include <SDL/SDL.h>
#undef main

int main()
{
    if (SDL_Init (SDL_INIT_EVERYTHING) != 0)
        return 1;
    SDL_Surface* Scr;
    if ((Scr = SDL_SetVideoMode (300, 200, 32, 0)) == 0)
        return 2;

    SDL_Rect Mouse1 = {50, 50, 50, 100};
    SDL_Rect Mouse3 = {150, 50, 50, 100};
    SDL_Rect Mouse2 = {250, 50, 50, 100};
    SDL_Surface Colors;
    SDL_Rect Click = {0, 0, 50, 100};
    SDL_Rect NoClick = {50, 0, 50, 100};

    SDL_FillRect (Scr, 0, SDL_MapRGB (Scr->format, 255, 255, 255));
    SDL_FillRect (&Colors, &Click, SDL_MapRGB (Colors.format, 255, 0, 0));
    SDL_FillRect (&Colors, &NoClick, SDL_MapRGB (Colors.format, 0, 0, 255));

    while (true)
    {
        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(1))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse1);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse1);

        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(2))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse2);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse2);

        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(3))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse3);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse3);

        if (SDL_GetKeyState (0) [SDLK_ESCAPE])
            return 0;

        SDL_Delay (17);
    }
}
+2  A: 

You are not processing any events. In your case, call SDL_PumpEvents to make SDL process them and update all its internal states:

while (true)
{
  SDL_PumpEvents();

  // The rest is the same ...
}
dark_charlie
OK, it helped with freezig, but nothing shows on screen. After I added SDL_Flip() just before SDL_Delay(), still don't work.
Xirdus
I wonder it doesn't crash. You should never declare a static SDL_Surface like you did it in the `SDL_Surface Colors;` case. Instead, always use a pointer (`SDL_Surface *Colors`) and use the SDL_CreateRGBSurface (http://sdl.beuc.net/sdl.wiki/SDL_CreateRGBSurface) and SDL_FreeSurface functions to allocate/deallocate the surface. I recommend reading tutorials linked at the SDL website.
dark_charlie