comparison Xcode/TemplatesForXcode/SDL OpenGL Application/main.c @ 2213:59a667370c57

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