par_rast
diff src/main_sdl.c @ 0:11f024648101
initial
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 22 Mar 2016 00:55:07 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main_sdl.c Tue Mar 22 00:55:07 2016 +0200 1.3 @@ -0,0 +1,92 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <SDL/SDL.h> 1.7 +#include "app.h" 1.8 + 1.9 +static int handle_event(SDL_Event *ev); 1.10 + 1.11 +static SDL_Surface *fbsurf; 1.12 +static int quit; 1.13 + 1.14 +int main(int argc, char **argv) 1.15 +{ 1.16 + unsigned long msec, prev_fps_upd; 1.17 + unsigned int num_frames, framerate; 1.18 + 1.19 + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { 1.20 + fprintf(stderr, "failed to initialize SDL\n"); 1.21 + return 1; 1.22 + } 1.23 + if(!(fbsurf = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE | SDL_RESIZABLE))) { 1.24 + fprintf(stderr, "failed to create surface\n"); 1.25 + SDL_Quit(); 1.26 + return 1; 1.27 + } 1.28 + SDL_WM_SetCaption("parallel rasterization test", 0); 1.29 + 1.30 + if(app_init() == -1) { 1.31 + return 1; 1.32 + } 1.33 + 1.34 + prev_fps_upd = SDL_GetTicks(); 1.35 + num_frames = 0; 1.36 + 1.37 + while(!quit) { 1.38 + unsigned long dt; 1.39 + SDL_Event ev; 1.40 + while(SDL_PollEvent(&ev)) { 1.41 + if(handle_event(&ev) == -1 || quit) { 1.42 + goto break_main_loop; 1.43 + } 1.44 + } 1.45 + 1.46 + if(SDL_MUSTLOCK(fbsurf)) { 1.47 + SDL_LockSurface(fbsurf); 1.48 + } 1.49 + fb_pixels = fbsurf->pixels; 1.50 + fb_width = fbsurf->w; 1.51 + fb_height = fbsurf->h; 1.52 + 1.53 + app_draw(); 1.54 + 1.55 + if(SDL_MUSTLOCK(fbsurf)) { 1.56 + SDL_UnlockSurface(fbsurf); 1.57 + } 1.58 + SDL_Flip(fbsurf); 1.59 + 1.60 + ++num_frames; 1.61 + msec = SDL_GetTicks(); 1.62 + dt = msec - prev_fps_upd; 1.63 + if(dt > 1500) { 1.64 + char tmp[32]; 1.65 + framerate = 10000 * num_frames / dt; 1.66 + num_frames = 0; 1.67 + prev_fps_upd = msec; 1.68 + 1.69 + sprintf(tmp, "fps: %u.%u\n", framerate / 10, framerate % 10); 1.70 + SDL_WM_SetCaption(tmp, 0); 1.71 + } 1.72 + } 1.73 +break_main_loop: 1.74 + app_cleanup(); 1.75 + SDL_Quit(); 1.76 + return 0; 1.77 +} 1.78 + 1.79 +void app_quit(void) 1.80 +{ 1.81 + quit = 1; 1.82 +} 1.83 + 1.84 +static int handle_event(SDL_Event *ev) 1.85 +{ 1.86 + switch(ev->type) { 1.87 + case SDL_KEYDOWN: 1.88 + app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); 1.89 + break; 1.90 + 1.91 + default: 1.92 + break; 1.93 + } 1.94 + return 0; 1.95 +}