comparison SDL 1.2 Mac OpenGL Application.xctemplate/atlantis/atlantis.c @ 0:b0b91cadc484

Initial Xcode 4 templates for SDL/Mac 1.2 and 1.3. The major difference between the two is SDLMain. Copy all templates to ~/Library/Developer/Xcode/Templates/SDL or /Library/Developer/Xcode/Templates/SDL Templates based off information documented here: http://blog.boreal-kiss.net/2011/03/11/a-minimal-project-template-for-xcode-4/
author Eric Wing <ewing . public |-at-| gmail . com>
date Sun, 02 Oct 2011 21:14:53 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b0b91cadc484
1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /**
5 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <math.h>
44 #include <sys/time.h>
45 #ifdef USE_GLUT
46 #include <GLUT/glut.h>
47 #endif
48 #include "SDL_opengl.h"
49 #include "atlantis.h"
50
51 fishRec sharks[NUM_SHARKS];
52 fishRec momWhale;
53 fishRec babyWhale;
54 fishRec dolph;
55
56 GLboolean Timing = GL_TRUE;
57
58 int w_win = 640;
59 int h_win = 480;
60 GLint count = 0;
61 GLenum StrMode = GL_VENDOR;
62
63 GLboolean moving;
64
65 static double mtime(void)
66 {
67 struct timeval tk_time;
68 struct timezone tz;
69
70 gettimeofday(&tk_time, &tz);
71
72 return 4294.967296 * tk_time.tv_sec + 0.000001 * tk_time.tv_usec;
73 }
74
75 static double filter(double in, double *save)
76 {
77 static double k1 = 0.9;
78 static double k2 = 0.05;
79
80 save[3] = in;
81 save[1] = save[0]*k1 + k2*(save[3] + save[2]);
82
83 save[0]=save[1];
84 save[2]=save[3];
85
86 return(save[1]);
87 }
88
89 void DrawStr(const char *str)
90 {
91 #ifdef USE_GLUT
92 GLint i = 0;
93
94 if(!str) return;
95
96 while(str[i])
97 {
98 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, str[i]);
99 i++;
100 }
101 #endif
102 }
103
104 void
105 InitFishs(void)
106 {
107 int i;
108
109 for (i = 0; i < NUM_SHARKS; i++) {
110 sharks[i].x = 70000.0 + rand() % 6000;
111 sharks[i].y = rand() % 6000;
112 sharks[i].z = rand() % 6000;
113 sharks[i].psi = rand() % 360 - 180.0;
114 sharks[i].v = 1.0;
115 }
116
117 dolph.x = 30000.0;
118 dolph.y = 0.0;
119 dolph.z = 6000.0;
120 dolph.psi = 90.0;
121 dolph.theta = 0.0;
122 dolph.v = 3.0;
123
124 momWhale.x = 70000.0;
125 momWhale.y = 0.0;
126 momWhale.z = 0.0;
127 momWhale.psi = 90.0;
128 momWhale.theta = 0.0;
129 momWhale.v = 3.0;
130
131 babyWhale.x = 60000.0;
132 babyWhale.y = -2000.0;
133 babyWhale.z = -2000.0;
134 babyWhale.psi = 90.0;
135 babyWhale.theta = 0.0;
136 babyWhale.v = 3.0;
137 }
138
139 void
140 Atlantis_Init(void)
141 {
142 static float ambient[] = {0.2, 0.2, 0.2, 1.0};
143 static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
144 static float position[] = {0.0, 1.0, 0.0, 0.0};
145 static float mat_shininess[] = {90.0};
146 static float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
147 static float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
148 static float mat_ambient[] = {0.3, 0.4, 0.5, 1.0};
149 static float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
150 static float lmodel_localviewer[] = {0.0};
151 //GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
152 //GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
153 static float fog_color[] = {0.0, 0.5, 0.9, 1.0};
154
155 glFrontFace(GL_CCW);
156
157 glDepthFunc(GL_LESS);
158 glEnable(GL_DEPTH_TEST);
159
160 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
161 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
162 glLightfv(GL_LIGHT0, GL_POSITION, position);
163 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
164 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
165 glEnable(GL_LIGHTING);
166 glEnable(GL_LIGHT0);
167
168 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
169 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
170 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
171 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
172
173 InitFishs();
174
175 glEnable(GL_FOG);
176 glFogi(GL_FOG_MODE, GL_EXP);
177 glFogf(GL_FOG_DENSITY, 0.0000025);
178 glFogfv(GL_FOG_COLOR, fog_color);
179
180 glClearColor(0.0, 0.5, 0.9, 1.0);
181 }
182
183 void
184 Atlantis_Reshape(int width, int height)
185 {
186 w_win = width;
187 h_win = height;
188
189 glViewport(0, 0, width, height);
190
191 glMatrixMode(GL_PROJECTION);
192 glLoadIdentity();
193 gluPerspective(60.0, (GLfloat) width / (GLfloat) height, 20000.0, 300000.0);
194 glMatrixMode(GL_MODELVIEW);
195 }
196
197 void
198 Atlantis_Animate(void)
199 {
200 int i;
201
202 for (i = 0; i < NUM_SHARKS; i++) {
203 SharkPilot(&sharks[i]);
204 SharkMiss(i);
205 }
206 WhalePilot(&dolph);
207 dolph.phi++;
208 //glutPostRedisplay();
209 WhalePilot(&momWhale);
210 momWhale.phi++;
211 WhalePilot(&babyWhale);
212 babyWhale.phi++;
213 }
214
215 void
216 Atlantis_Key(unsigned char key, int x, int y)
217 {
218 switch (key) {
219 case 't':
220 Timing = !Timing;
221 break;
222 case ' ':
223 switch(StrMode)
224 {
225 case GL_EXTENSIONS:
226 StrMode = GL_VENDOR;
227 break;
228 case GL_VENDOR:
229 StrMode = GL_RENDERER;
230 break;
231 case GL_RENDERER:
232 StrMode = GL_VERSION;
233 break;
234 case GL_VERSION:
235 StrMode = GL_EXTENSIONS;
236 break;
237 }
238 break;
239 case 27: /* Esc will quit */
240 exit(1);
241 break;
242 case 's': /* "s" start animation */
243 moving = GL_TRUE;
244 //glutIdleFunc(Animate);
245 break;
246 case 'a': /* "a" stop animation */
247 moving = GL_FALSE;
248 //glutIdleFunc(NULL);
249 break;
250 case '.': /* "." will advance frame */
251 if (!moving) {
252 Atlantis_Animate();
253 }
254 }
255 }
256 /*
257 void Display(void)
258 {
259 static float P123[3] = {-448.94, -203.14, 9499.60};
260 static float P124[3] = {-442.64, -185.20, 9528.07};
261 static float P125[3] = {-441.07, -148.05, 9528.07};
262 static float P126[3] = {-443.43, -128.84, 9499.60};
263 static float P127[3] = {-456.87, -146.78, 9466.67};
264 static float P128[3] = {-453.68, -183.93, 9466.67};
265
266 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
267
268 glPushMatrix();
269 FishTransform(&dolph);
270 DrawDolphin(&dolph);
271 glPopMatrix();
272
273 glutSwapBuffers();
274 }
275 */
276
277 void
278 Atlantis_Display(void)
279 {
280 int i;
281 static double th[4] = {0.0, 0.0, 0.0, 0.0};
282 static double t1 = 0.0, t2 = 0.0, t;
283 char num_str[128];
284
285 t1 = t2;
286
287 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
288
289 for (i = 0; i < NUM_SHARKS; i++) {
290 glPushMatrix();
291 FishTransform(&sharks[i]);
292 DrawShark(&sharks[i]);
293 glPopMatrix();
294 }
295
296 glPushMatrix();
297 FishTransform(&dolph);
298 DrawDolphin(&dolph);
299 glPopMatrix();
300
301 glPushMatrix();
302 FishTransform(&momWhale);
303 DrawWhale(&momWhale);
304 glPopMatrix();
305
306 glPushMatrix();
307 FishTransform(&babyWhale);
308 glScalef(0.45, 0.45, 0.3);
309 DrawWhale(&babyWhale);
310 glPopMatrix();
311
312 if(Timing)
313 {
314 t2 = mtime();
315 t = t2 - t1;
316 if(t > 0.0001) t = 1.0 / t;
317
318 glDisable(GL_LIGHTING);
319 //glDisable(GL_DEPTH_TEST);
320
321 glColor3f(1.0, 0.0, 0.0);
322
323 glMatrixMode (GL_PROJECTION);
324 glPushMatrix();
325 glLoadIdentity();
326 glOrtho(0, w_win, 0, h_win, -10.0, 10.0);
327
328 glRasterPos2f(5.0, 5.0);
329
330 switch(StrMode)
331 {
332 case GL_VENDOR:
333 sprintf(num_str, "%0.2f Hz, %dx%d, VENDOR: ", filter(t, th), w_win, h_win);
334 DrawStr(num_str);
335 DrawStr(glGetString(GL_VENDOR));
336 break;
337 case GL_RENDERER:
338 sprintf(num_str, "%0.2f Hz, %dx%d, RENDERER: ", filter(t, th), w_win, h_win);
339 DrawStr(num_str);
340 DrawStr(glGetString(GL_RENDERER));
341 break;
342 case GL_VERSION:
343 sprintf(num_str, "%0.2f Hz, %dx%d, VERSION: ", filter(t, th), w_win, h_win);
344 DrawStr(num_str);
345 DrawStr(glGetString(GL_VERSION));
346 break;
347 case GL_EXTENSIONS:
348 sprintf(num_str, "%0.2f Hz, %dx%d, EXTENSIONS: ", filter(t, th), w_win, h_win);
349 DrawStr(num_str);
350 DrawStr(glGetString(GL_EXTENSIONS));
351 break;
352 }
353
354 glPopMatrix();
355 glMatrixMode(GL_MODELVIEW);
356
357 glEnable(GL_LIGHTING);
358 //glEnable(GL_DEPTH_TEST);
359 }
360
361 count++;
362
363 // glutSwapBuffers();
364 }
365
366 /*
367 void
368 Visible(int state)
369 {
370 if (state == GLUT_VISIBLE) {
371 if (moving)
372 glutIdleFunc(Animate);
373 } else {
374 if (moving)
375 glutIdleFunc(NULL);
376 }
377 }
378
379
380 void
381 timingSelect(int value)
382 {
383 switch(value)
384 {
385 case 1:
386 StrMode = GL_VENDOR;
387 break;
388 case 2:
389 StrMode = GL_RENDERER;
390 break;
391 case 3:
392 StrMode = GL_VERSION;
393 break;
394 case 4:
395 StrMode = GL_EXTENSIONS;
396 break;
397 }
398 }
399
400 void
401 menuSelect(int value)
402 {
403 switch (value) {
404 case 1:
405 moving = GL_TRUE;
406 glutIdleFunc(Animate);
407 break;
408 case 2:
409 moving = GL_FALSE;
410 glutIdleFunc(NULL);
411 break;
412 case 4:
413 exit(0);
414 break;
415 }
416 }
417
418 int
419 main(int argc, char **argv)
420 {
421 GLboolean fullscreen = GL_FALSE;
422 GLint time_menu;
423
424 srand(0);
425
426 glutInit(&argc, argv);
427 if (argc > 1 && !strcmp(argv[1], "-w"))
428 fullscreen = GL_FALSE;
429
430 //glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
431 glutInitDisplayString("rgba double depth=24");
432 if (fullscreen) {
433 glutGameModeString("1024x768:32");
434 glutEnterGameMode();
435 } else {
436 glutInitWindowSize(320, 240);
437 glutCreateWindow("Atlantis Timing");
438 }
439 Init();
440 glutDisplayFunc(Display);
441 glutReshapeFunc(Reshape);
442 glutKeyboardFunc(Key);
443 moving = GL_TRUE;
444 glutIdleFunc(Animate);
445 glutVisibilityFunc(Visible);
446
447 time_menu = glutCreateMenu(timingSelect);
448 glutAddMenuEntry("GL_VENDOR", 1);
449 glutAddMenuEntry("GL_RENDERER", 2);
450 glutAddMenuEntry("GL_VERSION", 3);
451 glutAddMenuEntry("GL_EXTENSIONS", 4);
452
453 glutCreateMenu(menuSelect);
454 glutAddMenuEntry("Start motion", 1);
455 glutAddMenuEntry("Stop motion", 2);
456 glutAddSubMenu("Timing Mode", time_menu);
457 glutAddMenuEntry("Quit", 4);
458
459 //glutAttachMenu(GLUT_RIGHT_BUTTON);
460 glutAttachMenu(GLUT_RIGHT_BUTTON);
461 glutMainLoop();
462 return 0; // ANSI C requires main to return int.
463 }
464 */