1 | |
---|
2 | #ifdef WIN32 |
---|
3 | # define WIN32_LEAN_AND_MEAN |
---|
4 | # include <windows.h> |
---|
5 | #endif |
---|
6 | #if defined __APPLE__ && defined __MACH__ |
---|
7 | # include <OpenGL/gl.h> |
---|
8 | #else |
---|
9 | # define GL_GLEXT_PROTOTYPES |
---|
10 | # include <GL/gl.h> |
---|
11 | # include <GL/glext.h> |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <SDL.h> |
---|
15 | |
---|
16 | #include "video.h" |
---|
17 | |
---|
18 | /* |
---|
19 | * Video implementation class |
---|
20 | */ |
---|
21 | |
---|
22 | class VideoData |
---|
23 | { |
---|
24 | friend class Video; |
---|
25 | |
---|
26 | private: |
---|
27 | SDL_Surface *video; |
---|
28 | Uint32 start, ticks; |
---|
29 | int frames; |
---|
30 | }; |
---|
31 | |
---|
32 | /* |
---|
33 | * Public Video class |
---|
34 | */ |
---|
35 | |
---|
36 | Video::Video(char const *title, int width, int height) |
---|
37 | { |
---|
38 | data = new VideoData(); |
---|
39 | |
---|
40 | /* Initialise SDL */ |
---|
41 | if (SDL_Init(SDL_INIT_VIDEO) < 0) |
---|
42 | { |
---|
43 | fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError()); |
---|
44 | exit(EXIT_FAILURE); |
---|
45 | } |
---|
46 | |
---|
47 | data->video = SDL_SetVideoMode(width, height, 0, SDL_OPENGL); |
---|
48 | if (!data->video) |
---|
49 | { |
---|
50 | fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError()); |
---|
51 | SDL_Quit(); |
---|
52 | exit(EXIT_FAILURE); |
---|
53 | } |
---|
54 | |
---|
55 | SDL_WM_SetCaption(title, NULL); |
---|
56 | SDL_ShowCursor(0); |
---|
57 | SDL_WM_GrabInput(SDL_GRAB_ON); |
---|
58 | |
---|
59 | /* Initialise OpenGL */ |
---|
60 | glViewport(0, 0, data->video->w, data->video->h); |
---|
61 | glMatrixMode(GL_PROJECTION); |
---|
62 | glLoadIdentity(); |
---|
63 | glOrtho(0, data->video->w, data->video->h, 0, -1, 10); |
---|
64 | glMatrixMode(GL_MODELVIEW); |
---|
65 | glLoadIdentity(); |
---|
66 | |
---|
67 | glEnable(GL_TEXTURE_2D); |
---|
68 | glShadeModel(GL_SMOOTH); |
---|
69 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
---|
70 | glClearDepth(1.0); |
---|
71 | glEnable(GL_DEPTH_TEST); |
---|
72 | glDepthFunc(GL_LEQUAL); |
---|
73 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
---|
74 | |
---|
75 | glEnable(GL_BLEND); |
---|
76 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
77 | |
---|
78 | /* Initialise timer */ |
---|
79 | data->start = data->ticks = SDL_GetTicks(); |
---|
80 | data->frames = 0; |
---|
81 | } |
---|
82 | |
---|
83 | int Video::GetWidth() const |
---|
84 | { |
---|
85 | return data->video->w; |
---|
86 | } |
---|
87 | |
---|
88 | int Video::GetHeight() const |
---|
89 | { |
---|
90 | return data->video->h; |
---|
91 | } |
---|
92 | |
---|
93 | void Video::Clear() |
---|
94 | { |
---|
95 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
96 | glLoadIdentity(); |
---|
97 | } |
---|
98 | |
---|
99 | void Video::Refresh(float milliseconds) |
---|
100 | { |
---|
101 | if (milliseconds > 0.0f) |
---|
102 | while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f)) |
---|
103 | SDL_Delay(1); |
---|
104 | data->ticks = SDL_GetTicks(); |
---|
105 | data->frames++; |
---|
106 | |
---|
107 | SDL_GL_SwapBuffers(); |
---|
108 | } |
---|
109 | |
---|
110 | void Video::FullScreen() |
---|
111 | { |
---|
112 | SDL_WM_ToggleFullScreen(data->video); |
---|
113 | } |
---|
114 | |
---|
115 | Video::~Video() |
---|
116 | { |
---|
117 | Uint32 total = SDL_GetTicks() - data->start; |
---|
118 | printf("%f fps\n", 1000.0f * data->frames / total); |
---|
119 | SDL_Quit(); |
---|
120 | delete data; |
---|
121 | } |
---|
122 | |
---|