par_rast

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL/SDL.h>
4 #include "app.h"
6 static int handle_event(SDL_Event *ev);
8 static SDL_Surface *fbsurf;
9 static int quit;
11 int main(int argc, char **argv)
12 {
13 unsigned long msec, prev_fps_upd;
14 unsigned int num_frames, framerate;
16 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
17 fprintf(stderr, "failed to initialize SDL\n");
18 return 1;
19 }
20 if(!(fbsurf = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE | SDL_RESIZABLE))) {
21 fprintf(stderr, "failed to create surface\n");
22 SDL_Quit();
23 return 1;
24 }
25 SDL_WM_SetCaption("parallel rasterization test", 0);
27 if(app_init() == -1) {
28 return 1;
29 }
31 prev_fps_upd = SDL_GetTicks();
32 num_frames = 0;
34 while(!quit) {
35 unsigned long dt;
36 SDL_Event ev;
37 while(SDL_PollEvent(&ev)) {
38 if(handle_event(&ev) == -1 || quit) {
39 goto break_main_loop;
40 }
41 }
43 if(SDL_MUSTLOCK(fbsurf)) {
44 SDL_LockSurface(fbsurf);
45 }
46 fb_pixels = fbsurf->pixels;
47 fb_width = fbsurf->w;
48 fb_height = fbsurf->h;
50 app_draw();
52 if(SDL_MUSTLOCK(fbsurf)) {
53 SDL_UnlockSurface(fbsurf);
54 }
55 SDL_Flip(fbsurf);
57 ++num_frames;
58 msec = SDL_GetTicks();
59 dt = msec - prev_fps_upd;
60 if(dt > 1500) {
61 char tmp[32];
62 framerate = 10000 * num_frames / dt;
63 num_frames = 0;
64 prev_fps_upd = msec;
66 sprintf(tmp, "fps: %u.%u\n", framerate / 10, framerate % 10);
67 SDL_WM_SetCaption(tmp, 0);
68 }
69 }
70 break_main_loop:
71 app_cleanup();
72 SDL_Quit();
73 return 0;
74 }
76 void app_quit(void)
77 {
78 quit = 1;
79 }
81 static int handle_event(SDL_Event *ev)
82 {
83 switch(ev->type) {
84 case SDL_KEYDOWN:
85 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
86 break;
88 default:
89 break;
90 }
91 return 0;
92 }