comparison Xcode/TemplatesForProjectBuilder/SDL OpenGL Application/main.c @ 2220:4d2d0548f5b2

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