/
sdl-base.c
96 lines (80 loc) · 2.32 KB
/
sdl-base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* $Id: sdl-base.c 19 2006-07-30 13:05:23Z aholkner $ */
#include "sdl-base.h"
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 600
#define DEFAULT_DEPTH 32
#define DEFAULT_FLAGS (SDL_OPENGL | SDL_RESIZABLE)
static SDL_Surface *screen;
static int videoFlags;
/* Frame counting */
static int frame_count;
static Uint32 frame_time;
static int quit_flag;
int fps;
void quit()
{
quit_flag = 1;
}
int main(int argc, char **argv)
{
SDL_Event ev;
Uint32 now, last_frame_time;
quit_flag = 0;
videoFlags = DEFAULT_FLAGS;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
screen = SDL_SetVideoMode(DEFAULT_WIDTH, DEFAULT_HEIGHT,
DEFAULT_DEPTH, videoFlags);
init();
reshape(screen->w, screen->h);
fps = 0;
frame_count = 0;
last_frame_time = frame_time = SDL_GetTicks();
while (!quit_flag)
{
/* Process all pending events */
while (SDL_PollEvent(&ev))
{
switch (ev.type)
{
case SDL_QUIT:
quit();
break;
case SDL_MOUSEBUTTONDOWN:
printf("Mouse button %d pressed at (%d,%d)\n",
ev.button.button, ev.button.x, ev.button.y);
break;
case SDL_VIDEORESIZE:
screen = SDL_SetVideoMode(ev.resize.w,
ev.resize.h,
DEFAULT_DEPTH, videoFlags);
reshape(screen->w, screen->h);
break;
default:
event(&ev);
break;
}
}
/* Calculate time passed */
now = SDL_GetTicks();
update(now - last_frame_time);
last_frame_time = now;
/* Refresh display and flip buffers */
display(screen);
// Draw the screen
SDL_GL_SwapBuffers();
/* Update FPS */
frame_count++;
if (now - frame_time > 1000)
{
fps = (frame_count * 1000) / (now - frame_time);
frame_count = 0;
frame_time = now;
}
}
cleanup();
SDL_Quit();
return EXIT_SUCCESS;
}