spaces -> tabs (only whitespace changes)
[blender.git] / intern / ghost / test / gears / GHOST_C-Test.c
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /**
30
31  * $Id$
32  * Copyright (C) 2001 NaN Technologies B.V.
33  *
34  * Simple test file for the GHOST library.
35  * The OpenGL gear code is taken from the Qt sample code which,
36  * in turn, is probably taken from somewhere as well.
37  * @author      Maarten Gribnau
38  * @date        May 31, 2001
39  */
40
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <math.h>
45
46 #define FALSE 0
47
48 #include "GHOST_C-api.h"
49
50 #if defined(WIN32) || defined(__APPLE__)
51         #ifdef WIN32
52                 #include <windows.h>
53                 #include <GL/gl.h>
54         #else /* WIN32 */
55                 /* __APPLE__ is defined */
56                 #include <AGL/gl.h>
57         #endif /* WIN32 */
58 #else /* defined(WIN32) || defined(__APPLE__) */
59         #include <GL/gl.h>
60 #endif /* defined(WIN32) || defined(__APPLE__) */
61
62
63 static void gearsTimerProc(GHOST_TimerTaskHandle task, GHOST_TUns64 time);
64 int processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData);
65
66 static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0;
67 static GLfloat fAngle = 0.0;
68 static int sExitRequested = 0;
69 static GHOST_SystemHandle shSystem = NULL;
70 static GHOST_WindowHandle sMainWindow = NULL;
71 static GHOST_WindowHandle sSecondaryWindow = NULL;
72 static GHOST_TStandardCursor sCursor = GHOST_kStandardCursorFirstCursor;
73 static GHOST_WindowHandle sFullScreenWindow = NULL;
74 static GHOST_TimerTaskHandle sTestTimer;
75 static GHOST_TimerTaskHandle sGearsTimer;
76
77 static void testTimerProc(GHOST_TimerTaskHandle task, GHOST_TUns64 time)
78 {
79         printf("timer1, time=%d\n", (int)time);
80 }
81
82
83 static void gearGL(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth)
84 {
85         GLint i;
86         GLfloat r0, r1, r2;
87         GLfloat angle, da;
88         GLfloat u, v, len;
89         const double pi = 3.14159264;
90         
91         r0 = inner_radius;
92         r1 = (float)(outer_radius - tooth_depth/2.0);
93         r2 = (float)(outer_radius + tooth_depth/2.0);
94         
95         da = (float)(2.0*pi / teeth / 4.0);
96         
97         glShadeModel(GL_FLAT);
98         glNormal3f(0.0, 0.0, 1.0);
99         
100         /* draw front face */
101         glBegin(GL_QUAD_STRIP);
102         for (i=0;i<=teeth;i++) {
103                 angle = (float)(i * 2.0*pi / teeth);
104                 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(width*0.5));
105                 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(width*0.5));
106                 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(width*0.5));
107                 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(width*0.5));
108         }
109         glEnd();
110         
111         /* draw front sides of teeth */
112         glBegin(GL_QUADS);
113         da = (float)(2.0*pi / teeth / 4.0);
114         for (i=0;i<teeth;i++) {
115                 angle = (float)(i * 2.0*pi / teeth);
116                 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(width*0.5));
117                 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(width*0.5));
118                 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(width*0.5));
119                 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(width*0.5));
120         }
121         glEnd();
122         
123         glNormal3f(0.0, 0.0, -1.0);
124         
125         /* draw back face */
126         glBegin(GL_QUAD_STRIP);
127         for (i=0;i<=teeth;i++) {
128                 angle = (float)(i * 2.0*pi / teeth);
129                 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(-width*0.5));
130                 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(-width*0.5));
131                 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(-width*0.5));
132                 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(-width*0.5));
133         }
134         glEnd();
135         
136         /* draw back sides of teeth */
137         glBegin(GL_QUADS);
138         da = (float)(2.0*pi / teeth / 4.0);
139         for (i=0;i<teeth;i++) {
140                 angle = (float)(i * 2.0*pi / teeth);
141                 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(-width*0.5));
142                 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(-width*0.5));
143                 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(-width*0.5));
144                 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(-width*0.5));
145         }
146         glEnd();
147         
148         /* draw outward faces of teeth */
149         glBegin(GL_QUAD_STRIP);
150         for (i=0;i<teeth;i++) {
151                 angle = (float)(i * 2.0*pi / teeth);
152                 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(width*0.5));
153                 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(-width*0.5));
154                 u = (float)(r2*cos(angle+da) - r1*cos(angle));
155                 v = (float)(r2*sin(angle+da) - r1*sin(angle));
156                 len = (float)(sqrt(u*u + v*v));
157                 u /= len;
158                 v /= len;
159                 glNormal3f(v, -u, 0.0);
160                 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(width*0.5));
161                 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(-width*0.5));
162                 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
163                 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(width*0.5));
164                 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(-width*0.5));
165                 u = (float)(r1*cos(angle+3*da) - r2*cos(angle+2*da));
166                 v = (float)(r1*sin(angle+3*da) - r2*sin(angle+2*da));
167                 glNormal3f(v, -u, 0.0);
168                 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(width*0.5));
169                 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(-width*0.5));
170                 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
171         }
172         glVertex3f((float)(r1*cos(0.0)), (float)(r1*sin(0.0)), (float)(width*0.5));
173         glVertex3f((float)(r1*cos(0.0)), (float)(r1*sin(0.0)), (float)(-width*0.5));
174         glEnd();
175         
176         glShadeModel(GL_SMOOTH);
177         
178         /* draw inside radius cylinder */
179         glBegin(GL_QUAD_STRIP);
180         for (i=0;i<=teeth;i++) {
181                 angle = (float)(i * 2.0*pi / teeth);
182                 glNormal3f((float)(-cos(angle)), (float)(-sin(angle)), 0.0);
183                 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(-width*0.5));
184                 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(width*0.5));
185         }
186         glEnd();
187 }
188
189
190
191 static void drawGearGL(int id)
192 {
193         static GLfloat pos[4] = { 5.0f, 5.0f, 10.0f, 1.0f };
194         static GLfloat ared[4] = { 0.8f, 0.1f, 0.0f, 1.0f };
195         static GLfloat agreen[4] = { 0.0f, 0.8f, 0.2f, 1.0f };
196         static GLfloat ablue[4] = { 0.2f, 0.2f, 1.0f, 1.0f };
197         
198         glLightfv(GL_LIGHT0, GL_POSITION, pos);
199         glEnable(GL_CULL_FACE);
200         glEnable(GL_LIGHTING);
201         glEnable(GL_LIGHT0);
202         glEnable(GL_DEPTH_TEST);
203         
204         switch (id)
205         {
206         case 1:
207                 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ared);
208                 gearGL(1.0f, 4.0f, 1.0f, 20, 0.7f);
209                 break;
210         case 2:
211                 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, agreen);
212                 gearGL(0.5f, 2.0f, 2.0f, 10, 0.7f);
213                 break;
214         case 3:
215                 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ablue);
216                 gearGL(1.3f, 2.0f, 0.5f, 10, 0.7f);
217                 break;
218         default:
219                 break;
220         }
221         glEnable(GL_NORMALIZE);
222 }
223
224
225 static void drawGL(void)
226 {
227         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228         
229         glPushMatrix();
230         
231         glRotatef(view_rotx, 1.0, 0.0, 0.0);
232         glRotatef(view_roty, 0.0, 1.0, 0.0);
233         glRotatef(view_rotz, 0.0, 0.0, 1.0);
234         
235         glPushMatrix();
236         glTranslatef(-3.0, -2.0, 0.0);
237         glRotatef(fAngle, 0.0, 0.0, 1.0);
238         drawGearGL(1);
239         glPopMatrix();
240         
241         glPushMatrix();
242         glTranslatef(3.1f, -2.0f, 0.0f);
243         glRotatef((float)(-2.0*fAngle-9.0), 0.0, 0.0, 1.0);
244         drawGearGL(2);
245         glPopMatrix();
246         
247         glPushMatrix();
248         glTranslatef(-3.1f, 2.2f, -1.8f);
249         glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
250         glRotatef((float)(2.0*fAngle-2.0), 0.0, 0.0, 1.0);
251         drawGearGL(3);
252         glPopMatrix();
253         
254         glPopMatrix();
255 }
256
257
258 static void setViewPortGL(GHOST_WindowHandle hWindow)
259 {
260         GHOST_RectangleHandle hRect = NULL;
261         GLfloat w, h;
262         
263         GHOST_ActivateWindowDrawingContext(hWindow);
264         hRect = GHOST_GetClientBounds(hWindow);
265         
266         w = (float)GHOST_GetWidthRectangle(hRect) / (float)GHOST_GetHeightRectangle(hRect);
267         h = 1.0;
268         
269         glViewport(0, 0, GHOST_GetWidthRectangle(hRect), GHOST_GetHeightRectangle(hRect));
270
271         glMatrixMode(GL_PROJECTION);
272         glLoadIdentity();
273         glFrustum(-w, w, -h, h, 5.0, 60.0);
274         /* glOrtho(0, bnds.getWidth(), 0, bnds.getHeight(), -10, 10); */
275         glMatrixMode(GL_MODELVIEW);
276         glLoadIdentity();
277         glTranslatef(0.0, 0.0, -40.0);
278         
279         glClearColor(.2f,0.0f,0.0f,0.0f);
280         glClear(GL_COLOR_BUFFER_BIT);
281
282         GHOST_DisposeRectangle(hRect);
283 }
284
285
286
287 int processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData)
288 {
289         int handled = 1;
290         int cursor;
291         int visibility;
292         GHOST_TEventKeyData* keyData = NULL;
293         GHOST_TEventWheelData* wheelData = NULL;
294         GHOST_DisplaySetting setting;
295         GHOST_WindowHandle window = GHOST_GetEventWindow(hEvent);
296         
297         switch (GHOST_GetEventType(hEvent))
298         {
299         /*
300         case GHOST_kEventUnknown:
301                 break;
302         case GHOST_kEventCursorButton:
303                 break;
304         case GHOST_kEventCursorMove:
305                 break;
306         */
307         case GHOST_kEventWheel:
308                 {
309                 wheelData = (GHOST_TEventWheelData*)GHOST_GetEventData(hEvent);
310                 if (wheelData->z > 0)
311                 {
312                         view_rotz += 5.f;
313                 }
314                 else
315                 {
316                         view_rotz -= 5.f;
317                 }
318                 }
319                 break;
320
321         case GHOST_kEventKeyUp:
322                 break;
323                 
324         case GHOST_kEventKeyDown:
325                 {
326                         keyData = (GHOST_TEventKeyData*)GHOST_GetEventData(hEvent);
327                         switch (keyData->key)
328                         {
329                         case GHOST_kKeyC:
330                                 {
331                                         cursor = sCursor;
332                                         cursor++;
333                                         if (cursor >= GHOST_kStandardCursorNumCursors)
334                                         {
335                                                 cursor = GHOST_kStandardCursorFirstCursor;
336                                         }
337                                         sCursor = (GHOST_TStandardCursor)cursor;
338                                         GHOST_SetCursorShape(window, sCursor);
339                                 }
340                                 break;
341                         case GHOST_kKeyF:
342                                 if (!GHOST_GetFullScreen(shSystem))
343                                 {
344                                         /* Begin fullscreen mode */
345                                         setting.bpp = 24;
346                                         setting.frequency = 85;
347                                         setting.xPixels = 640;
348                                         setting.yPixels = 480;
349                                         
350                                         /*
351                                         setting.bpp = 16;
352                                         setting.frequency = 75;
353                                         setting.xPixels = 640;
354                                         setting.yPixels = 480;
355                                         */
356
357                                         sFullScreenWindow = GHOST_BeginFullScreen(shSystem, &setting,
358
359                                                 FALSE /* stereo flag */);
360                                 }
361                                 else
362                                 {
363                                         GHOST_EndFullScreen(shSystem);
364                                         sFullScreenWindow = 0;
365                                 }
366                                 break;
367                         case GHOST_kKeyH:
368                                 {
369                                         visibility = GHOST_GetCursorVisibility(window);
370                                         GHOST_SetCursorVisibility(window, !visibility);
371                                 }
372                                 break;
373                         case GHOST_kKeyQ:
374                                 if (GHOST_GetFullScreen(shSystem))
375                                 {
376                                         GHOST_EndFullScreen(shSystem);
377                                         sFullScreenWindow = 0;
378                                 }
379                                 sExitRequested = 1;
380                         case GHOST_kKeyT:
381                                 if (!sTestTimer)
382                                 {
383                                         sTestTimer = GHOST_InstallTimer(shSystem, 0, 1000, testTimerProc, NULL);
384                                 }
385                                 else
386                                 {
387                                         GHOST_RemoveTimer(shSystem, sTestTimer);
388                                         sTestTimer = 0;
389                                 }
390                                 break;
391                         case GHOST_kKeyW:
392                                 {
393                                         if (sMainWindow)
394                                         {
395                                                 char *title = GHOST_GetTitle(sMainWindow);
396                                                 char *ntitle = malloc(strlen(title)+2);
397
398                                                 sprintf(ntitle, "%s-", title);
399                                                 GHOST_SetTitle(sMainWindow, ntitle);
400                                                 
401                                                 free(ntitle);
402                                                 free(title);
403                                         }
404                                 }
405                                 break;
406                         default:
407                                 break;
408                         }
409                 }
410                 break;
411                 
412         case GHOST_kEventWindowClose:
413                 {
414                         GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
415                         if (window2 == sMainWindow)
416                         {
417                                 sExitRequested = 1;
418                         }
419                         else
420                         {
421                                 if (sGearsTimer)
422                                 {
423                                         GHOST_RemoveTimer(shSystem, sGearsTimer);
424                                         sGearsTimer = 0;
425                                 }
426                                 GHOST_DisposeWindow(shSystem, window2);
427                         }
428                 }
429                 break;
430                 
431         case GHOST_kEventWindowActivate:
432                 handled = 0;
433                 break;
434         case GHOST_kEventWindowDeactivate:
435                 handled = 0;
436                 break;
437         case GHOST_kEventWindowUpdate:
438                 {
439                         GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
440                         if (!GHOST_ValidWindow(shSystem, window2))
441                                 break;
442                         setViewPortGL(window2);
443                         drawGL();
444                         GHOST_SwapWindowBuffers(window2);
445                 }
446                 break;
447                 
448         default:
449                 handled = 0;
450                 break;
451         }
452         return handled;
453 }
454
455
456 int main(int argc, char** argv)
457 {
458         char* title1 = "gears - main window";
459         char* title2 = "gears - secondary window";
460         GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(processEvent, NULL);
461
462         /* Create the system */
463         shSystem = GHOST_CreateSystem();
464         GHOST_AddEventConsumer(shSystem, consumer);
465         
466         if (shSystem)
467         {
468                 /* Create the main window */
469                 sMainWindow = GHOST_CreateWindow(shSystem,
470                         title1,
471                         10,
472                         64,
473                         320,
474                         200,
475                         GHOST_kWindowStateNormal,
476                         GHOST_kDrawingContextTypeOpenGL,
477                         FALSE,
478                         FALSE);
479                 if (!sMainWindow)
480                 {
481                         printf("could not create main window\n");
482                         exit(-1);
483                 }
484                 
485                 /* Create a secondary window */
486                 sSecondaryWindow = GHOST_CreateWindow(shSystem,
487                         title2,
488                         340,
489                         64,
490                         320,
491                         200,
492                         GHOST_kWindowStateNormal,
493                         GHOST_kDrawingContextTypeOpenGL,
494                         FALSE,
495                         FALSE);
496                 if (!sSecondaryWindow)
497                 {
498                         printf("could not create secondary window\n");
499                         exit(-1);
500                 }
501                 
502                 /* Install a timer to have the gears running */
503                  sGearsTimer = GHOST_InstallTimer(shSystem,
504                         0,
505                         10,
506                         gearsTimerProc,
507                         sMainWindow);
508
509                 /* Enter main loop */
510                 while (!sExitRequested)
511                 {
512                         if (!GHOST_ProcessEvents(shSystem, 0)) 
513                         {
514 #ifdef WIN32
515                                 /* If there were no events, be nice to other applications */
516                                 Sleep(10);
517 #endif
518                         }
519                         GHOST_DispatchEvents(shSystem);
520                 }
521         }
522
523         /* Dispose windows */
524         if (GHOST_ValidWindow(shSystem, sMainWindow))
525         {
526                 GHOST_DisposeWindow(shSystem, sMainWindow);
527         }
528         if (GHOST_ValidWindow(shSystem, sSecondaryWindow))
529         {
530                 GHOST_DisposeWindow(shSystem, sSecondaryWindow);
531         }
532
533         /* Dispose the system */
534         GHOST_DisposeSystem(shSystem);
535         GHOST_DisposeEventConsumer(consumer);
536         
537         return 0;
538 }
539
540
541 static void gearsTimerProc(GHOST_TimerTaskHandle hTask, GHOST_TUns64 time)
542 {
543         GHOST_WindowHandle hWindow = NULL;
544         fAngle += 2.0;
545         view_roty += 1.0;
546         hWindow = (GHOST_WindowHandle)GHOST_GetTimerTaskUserData(hTask);
547         if (GHOST_GetFullScreen(shSystem))
548         {
549                 /* Running full screen */
550                 GHOST_InvalidateWindow(sFullScreenWindow);
551         }
552         else
553         {
554                 if (GHOST_ValidWindow(shSystem, hWindow))
555                 {
556                         GHOST_InvalidateWindow(hWindow);
557                 }
558         }
559 }