comparison Xcode/TemplatesForXcode/SDL OpenGL Application/main.c @ 2207:d63e9f5944ae

Unpacked project archives to get individual file history in subversion
author Sam Lantinga <slouken@libsdl.org>
date Sat, 21 Jul 2007 17:09:01 +0000
parents
children 59a667370c57
comparison
equal deleted inserted replaced
2206:ca7d2227d630 2207:d63e9f5944ae
1
2 /* Simple program: Create a blank window, wait for keypress, quit.
3
4 Please see the SDL documentation for details on using the SDL API:
5 /Developer/Documentation/SDL/docs.html
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12
13 #include "SDL.h"
14
15 extern void Atlantis_Init ();
16 extern void Atlantis_Reshape (int w, int h);
17 extern void Atlantis_Animate ();
18 extern void Atlantis_Display ();
19
20 static SDL_Surface *gScreen;
21
22 static void initAttributes ()
23 {
24 // Setup attributes we want for the OpenGL context
25
26 int value;
27
28 // Don't set color bit sizes (SDL_GL_RED_SIZE, etc)
29 // Mac OS X will always use 8-8-8-8 ARGB for 32-bit screens and
30 // 5-5-5 RGB for 16-bit screens
31
32 // Request a 16-bit depth buffer (without this, there is no depth buffer)
33 value = 16;
34 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, value);
35
36
37 // Request double-buffered OpenGL
38 // The fact that windows are double-buffered on Mac OS X has no effect
39 // on OpenGL double buffering.
40 value = 1;
41 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, value);
42 }
43
44 static void printAttributes ()
45 {
46 // Print out attributes of the context we created
47 int nAttr;
48 int i;
49
50 int attr[] = { SDL_GL_RED_SIZE, SDL_GL_BLUE_SIZE, SDL_GL_GREEN_SIZE,
51 SDL_GL_ALPHA_SIZE, SDL_GL_BUFFER_SIZE, SDL_GL_DEPTH_SIZE };
52
53 char *desc[] = { "Red size: %d bits\n", "Blue size: %d bits\n", "Green size: %d bits\n",
54 "Alpha size: %d bits\n", "Color buffer size: %d bits\n",
55 "Depth bufer size: %d bits\n" };
56
57 nAttr = sizeof(attr) / sizeof(int);
58
59 for (i = 0; i < nAttr; i++) {
60
61 int value;
62 SDL_GL_GetAttribute (attr[i], &value);
63 printf (desc[i], value);
64 }
65 }
66
67 static void createSurface (int fullscreen)
68 {
69 Uint32 flags = 0;
70
71 flags = SDL_OPENGL;
72 if (fullscreen)
73 flags |= SDL_FULLSCREEN;
74
75 // Create window
76 gScreen = SDL_SetVideoMode (640, 480, 0, flags);
77 if (gScreen == NULL) {
78
79 fprintf (stderr, "Couldn't set 640x480 OpenGL video mode: %s\n",
80 SDL_GetError());
81 SDL_Quit();
82 exit(2);
83 }
84 }
85
86 static void initGL ()
87 {
88 Atlantis_Init ();
89 Atlantis_Reshape (gScreen->w, gScreen->h);
90 }
91
92 static void drawGL ()
93 {
94 Atlantis_Animate ();
95 Atlantis_Display ();
96 }
97
98 static void mainLoop ()
99 {
100 SDL_Event event;
101 int done = 0;
102 int fps = 24;
103 int delay = 1000/fps;
104 int thenTicks = -1;
105 int nowTicks;
106
107 while ( !done ) {
108
109 /* Check for events */
110 while ( SDL_PollEvent (&event) ) {
111 switch (event.type) {
112
113 case SDL_MOUSEMOTION:
114 break;
115 case SDL_MOUSEBUTTONDOWN:
116 break;
117 case SDL_KEYDOWN:
118 /* Any keypress quits the app... */
119 case SDL_QUIT:
120 done = 1;
121 break;
122 default:
123 break;
124 }
125 }
126
127 // Draw at 24 hz
128 // This approach is not normally recommended - it is better to
129 // use time-based animation and run as fast as possible
130 drawGL ();
131 SDL_GL_SwapBuffers ();
132
133 // Time how long each draw-swap-delay cycle takes
134 // and adjust delay to get closer to target framerate
135 if (thenTicks > 0) {
136 nowTicks = SDL_GetTicks ();
137 delay += (1000/fps - (nowTicks-thenTicks));
138 thenTicks = nowTicks;
139 if (delay < 0)
140 delay = 1000/fps;
141 }
142 else {
143 thenTicks = SDL_GetTicks ();
144 }
145
146 SDL_Delay (delay);
147 }
148 }
149
150 int main(int argc, char *argv[])
151 {
152 // Init SDL video subsystem
153 if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) {
154
155 fprintf(stderr, "Couldn't initialize SDL: %s\n",
156 SDL_GetError());
157 exit(1);
158 }
159
160 // Set GL context attributes
161 initAttributes ();
162
163 // Create GL context
164 createSurface (0);
165
166 // Get GL context attributes
167 printAttributes ();
168
169 // Init GL state
170 initGL ();
171
172 // Draw, get events...
173 mainLoop ();
174
175 // Cleanup
176 SDL_Quit();
177
178 return 0;
179 }