Skip to content

Instantly share code, notes, and snippets.

@tmhedberg
Created February 12, 2012 21:45
Show Gist options
  • Select an option

  • Save tmhedberg/1811004 to your computer and use it in GitHub Desktop.

Select an option

Save tmhedberg/1811004 to your computer and use it in GitHub Desktop.
Simple SDL graphics test
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(void)
{
SDL_Surface *main_surf;
SDL_Event *const ev = malloc(sizeof (SDL_Event));
FILE *rnd = fopen("/dev/urandom", "r");
unsigned rseed;
SDL_Init(SDL_INIT_VIDEO);
main_surf = SDL_SetVideoMode(640, 480, 32, 0);
fread(&rseed, sizeof (unsigned), 1, rnd);
fclose(rnd);
srandom(rseed);
for (;;) {
SDL_WaitEvent(ev);
switch (ev->type) {
case SDL_MOUSEBUTTONDOWN: {
const SDL_MouseButtonEvent mb_ev = ev->button;
SDL_Rect rect = {.x = mb_ev.x, .y = mb_ev.y, .w = 2, .h = 2};
fputs("Mouse button down\n", stderr);
SDL_FillRect(
main_surf,
&rect,
SDL_MapRGB(main_surf->format,
random() % 0xFF,
random() % 0xFF,
random() % 0xFF)
);
SDL_Flip(main_surf);
break;
}
case SDL_KEYDOWN:
fputs("Key down\n", stderr);
if (ev->key.keysym.sym == SDLK_q) {
free(ev);
SDL_Quit();
return EXIT_SUCCESS;
}
break;
default: fputs("Other event\n", stderr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment