程式語言 - Simple DirectMedia Layer (SDL) - v1.2 - C/C++ - Mouse Event



參考資訊:
https://www.libsdl.org/release/SDL-1.2.15/docs/html/index.html

main.c

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_gfxPrimitives.h>

int main(int argc, char **args)
{
    char buf[255] = {0};
    SDL_Surface *screen = NULL;

    SDL_Init(SDL_INIT_VIDEO);
    screen = SDL_SetVideoMode(320, 240, 16, SDL_HWSURFACE);
    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
    SDL_Flip(screen);

    SDL_Event event;
    while (1) {
        if (SDL_PollEvent(&event)) {
            if (event.type == SDL_MOUSEMOTION) {
                int x = 0, y = 0;

                SDL_GetMouseState(&x, &y);
                sprintf(buf, "mouse pos: %d %d", x, y);
                SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
                stringRGBA(screen, 100, 100, buf, 0xff, 0xff, 0xff, 0xff);
                SDL_Flip(screen);
            }

            if ((event.type == SDL_MOUSEBUTTONUP) || (event.type == SDL_MOUSEBUTTONDOWN)) {
                break;
            }
        }
        SDL_Delay(100);
    }
    SDL_Quit();
    return 0;
}

編譯、執行

$ gcc main.c -o main -lSDL -lSDL_gfx -I/usr/include/SDL
$ ./main