Created
August 31, 2012 03:32
-
-
Save mbohun/3548717 to your computer and use it in GitHub Desktop.
sdl_surface_simple-00.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "SDL.h" | |
struct { | |
SDL_Surface* gfx; | |
SDL_Rect rect; | |
SDL_Rect screen_pos; | |
int dir; | |
} guy; | |
static int running = 1; | |
void handle_input() { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
running = 0; | |
return; | |
case SDL_KEYDOWN: | |
if (event.key.keysym.sym == SDLK_ESCAPE) { | |
running = 0; | |
return; | |
} | |
break; | |
case SDL_MOUSEMOTION: | |
break; | |
default: | |
break; /* unknown event type */ | |
} | |
} | |
} | |
void update() { | |
if (0==guy.dir) { | |
++guy.screen_pos.x; | |
if (guy.screen_pos.x == 800) { | |
guy.dir = 1; | |
} | |
return; | |
} | |
if (1==guy.dir) { | |
--guy.screen_pos.x; | |
if (guy.screen_pos.x == 0) { | |
guy.dir = 0; | |
} | |
return; | |
} | |
} | |
void render_frame(SDL_Surface* screen) { | |
Uint32 color_black = SDL_MapRGB(screen->format, 0, 0, 0); | |
SDL_FillRect(screen, NULL, color_black); | |
SDL_BlitSurface(guy.gfx, | |
&guy.rect, | |
screen, | |
&guy.screen_pos); | |
} | |
int main(int argc, char* argv[]) { | |
SDL_Surface* screen = NULL; | |
const Uint32 flags = SDL_ANYFORMAT | SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF; | |
const int w = 800; | |
const int h = 600; | |
SDL_Init(SDL_INIT_VIDEO); | |
guy.dir = 0; | |
guy.screen_pos.x = 0; | |
guy.screen_pos.y = 200; | |
guy.rect.x = 0; | |
guy.rect.y = 0; | |
guy.rect.w = 64; | |
guy.rect.h = 64; | |
guy.gfx = SDL_LoadBMP("guy_64x64.bmp"); /* SDL_ConvertSurface() */ | |
screen = SDL_SetVideoMode(w, h, 0, flags); | |
printf("SDL_ANYFORMAT:%d\n", (Uint32)(screen->flags & SDL_ANYFORMAT)); | |
printf("SDL_ASYNCBLIT:%d\n", (Uint32)(screen->flags & SDL_ASYNCBLIT)); | |
printf("SDL_DOUBLEBUF:%d\n", (Uint32)(screen->flags & SDL_DOUBLEBUF)); | |
printf("SDL_HWACCEL:%d\n", (Uint32)(screen->flags & SDL_HWACCEL)); | |
printf("SDL_HWPALETTE:%d\n", (Uint32)(screen->flags & SDL_HWPALETTE)); | |
printf("SDL_HWSURFACE:%d\n", (Uint32)(screen->flags & SDL_HWSURFACE)); | |
printf("SDL_FULLSCREEN:%d\n", (Uint32)(screen->flags & SDL_FULLSCREEN)); | |
printf("SDL_OPENGL:%d\n", (Uint32)(screen->flags & SDL_OPENGL)); | |
printf("SDL_OPENGLBLIT:%d\n", (Uint32)(screen->flags & SDL_OPENGLBLIT)); | |
printf("SDL_RESIZABLE:%d\n", (Uint32)(screen->flags & SDL_RESIZABLE)); | |
printf("SDL_RLEACCEL:%d\n", (Uint32)(screen->flags & SDL_RLEACCEL)); | |
printf("SDL_SRCALPHA:%d\n", (Uint32)(screen->flags & SDL_SRCALPHA)); | |
printf("SDL_SRCCOLORKEY:%d\n", (Uint32)(screen->flags & SDL_SRCCOLORKEY)); | |
printf("SDL_SWSURFACE:%d\n", (Uint32)(screen->flags & SDL_SWSURFACE)); | |
printf("SDL_PREALLOC:%d\n", (Uint32)(screen->flags & SDL_PREALLOC)); | |
while (running) { | |
handle_input(); | |
update(); | |
render_frame(screen); | |
SDL_Flip(screen); | |
} | |
SDL_FreeSurface(guy.gfx); | |
SDL_FreeSurface(screen); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment