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