Added mouse wheel support for windows.
[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_TEventWheelData* wheelData = NULL;
301         GHOST_DisplaySetting setting;
302         GHOST_WindowHandle window = GHOST_GetEventWindow(hEvent);
303         
304         switch (GHOST_GetEventType(hEvent))
305         {
306         /*
307         case GHOST_kEventUnknown:
308                 break;
309         case GHOST_kEventCursorButton:
310                 break;
311         case GHOST_kEventCursorMove:
312                 break;
313         */
314         case GHOST_kEventWheel:
315                 {
316                 wheelData = (GHOST_TEventWheelData*)GHOST_GetEventData(hEvent);
317                 if (wheelData->z > 0)
318                 {
319                         view_rotz += 5.f;
320                 }
321                 else
322                 {
323                         view_rotz -= 5.f;
324                 }
325                 }
326                 break;
327
328         case GHOST_kEventKeyUp:
329                 break;
330                 
331         case GHOST_kEventKeyDown:
332                 {
333                         keyData = (GHOST_TEventKeyData*)GHOST_GetEventData(hEvent);
334                         switch (keyData->key)
335                         {
336                         case GHOST_kKeyC:
337                                 {
338                                         cursor = sCursor;
339                                         cursor++;
340                                         if (cursor >= GHOST_kStandardCursorNumCursors)
341                                         {
342                                                 cursor = GHOST_kStandardCursorFirstCursor;
343                                         }
344                                         sCursor = (GHOST_TStandardCursor)cursor;
345                                         GHOST_SetCursorShape(window, sCursor);
346                                 }
347                                 break;
348                         case GHOST_kKeyF:
349                                 if (!GHOST_GetFullScreen(shSystem))
350                                 {
351                                         /* Begin fullscreen mode */
352                                         setting.bpp = 24;
353                                         setting.frequency = 85;
354                                         setting.xPixels = 640;
355                                         setting.yPixels = 480;
356                                         
357                                         /*
358                                         setting.bpp = 16;
359                                         setting.frequency = 75;
360                                         setting.xPixels = 640;
361                                         setting.yPixels = 480;
362                                         */
363
364                                         sFullScreenWindow = GHOST_BeginFullScreen(shSystem, &setting,
365
366                                                 FALSE /* stereo flag */);
367                                 }
368                                 else
369                                 {
370                                         GHOST_EndFullScreen(shSystem);
371                                         sFullScreenWindow = 0;
372                                 }
373                                 break;
374                         case GHOST_kKeyH:
375                                 {
376                                         visibility = GHOST_GetCursorVisibility(window);
377                                         GHOST_SetCursorVisibility(window, !visibility);
378                                 }
379                                 break;
380                         case GHOST_kKeyQ:
381                                 if (GHOST_GetFullScreen(shSystem))
382                                 {
383                                         GHOST_EndFullScreen(shSystem);
384                                         sFullScreenWindow = 0;
385                                 }
386                                 sExitRequested = 1;
387                         case GHOST_kKeyT:
388                                 if (!sTestTimer)
389                                 {
390                                         sTestTimer = GHOST_InstallTimer(shSystem, 0, 1000, testTimerProc, NULL);
391                                 }
392                                 else
393                                 {
394                                         GHOST_RemoveTimer(shSystem, sTestTimer);
395                                         sTestTimer = 0;
396                                 }
397                                 break;
398                         case GHOST_kKeyW:
399                                 {
400                                         if (sMainWindow)
401                                         {
402                                                 char *title = GHOST_GetTitle(sMainWindow);
403                                                 char *ntitle = malloc(strlen(title)+2);
404
405                                                 sprintf(ntitle, "%s-", title);
406                                                 GHOST_SetTitle(sMainWindow, ntitle);
407                                                 
408                                                 free(ntitle);
409                                                 free(title);
410                                         }
411                                 }
412                                 break;
413                         default:
414                                 break;
415                         }
416                 }
417                 break;
418                 
419         case GHOST_kEventWindowClose:
420                 {
421                         GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
422                         if (window2 == sMainWindow)
423                         {
424                                 sExitRequested = 1;
425                         }
426                         else
427                         {
428                                 if (sGearsTimer)
429                                 {
430                                         GHOST_RemoveTimer(shSystem, sGearsTimer);
431                                         sGearsTimer = 0;
432                                 }
433                                 GHOST_DisposeWindow(shSystem, window2);
434                         }
435                 }
436                 break;
437                 
438         case GHOST_kEventWindowActivate:
439                 handled = 0;
440                 break;
441         case GHOST_kEventWindowDeactivate:
442                 handled = 0;
443                 break;
444         case GHOST_kEventWindowUpdate:
445                 {
446                         GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
447                         if (!GHOST_ValidWindow(shSystem, window2))
448                                 break;
449                         setViewPortGL(window2);
450                         drawGL();
451                         GHOST_SwapWindowBuffers(window2);
452                 }
453                 break;
454                 
455         default:
456                 handled = 0;
457                 break;
458         }
459         return handled;
460 }
461
462
463 int main(int argc, char** argv)
464 {
465         char* title1 = "gears - main window";
466         char* title2 = "gears - secondary window";
467         GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(processEvent, NULL);
468
469         /* Create the system */
470         shSystem = GHOST_CreateSystem();
471         GHOST_AddEventConsumer(shSystem, consumer);
472         
473         if (shSystem)
474         {
475                 /* Create the main window */
476                 sMainWindow = GHOST_CreateWindow(shSystem,
477                         title1,
478                         10,
479                         64,
480                         320,
481                         200,
482                         GHOST_kWindowStateNormal,
483                         GHOST_kDrawingContextTypeOpenGL,
484                         FALSE);
485                 if (!sMainWindow)
486                 {
487                         printf("could not create main window\n");
488                         exit(-1);
489                 }
490                 
491                 /* Create a secondary window */
492                 sSecondaryWindow = GHOST_CreateWindow(shSystem,
493                         title2,
494                         340,
495                         64,
496                         320,
497                         200,
498                         GHOST_kWindowStateNormal,
499                         GHOST_kDrawingContextTypeOpenGL,
500                         FALSE);
501                 if (!sSecondaryWindow)
502                 {
503                         printf("could not create secondary window\n");
504                         exit(-1);
505                 }
506                 
507                 /* Install a timer to have the gears running */
508                  sGearsTimer = GHOST_InstallTimer(shSystem,
509                         0,
510                         10,
511                         gearsTimerProc,
512                         sMainWindow);
513
514                 /* Enter main loop */
515                 while (!sExitRequested)
516                 {
517                         if (!GHOST_ProcessEvents(shSystem, 0)) 
518                         {
519 #ifdef WIN32
520                                 /* If there were no events, be nice to other applications */
521                                 Sleep(10);
522 #endif
523                         }
524                         GHOST_DispatchEvents(shSystem);
525                 }
526         }
527
528         /* Dispose windows */
529         if (GHOST_ValidWindow(shSystem, sMainWindow))
530         {
531                 GHOST_DisposeWindow(shSystem, sMainWindow);
532         }
533         if (GHOST_ValidWindow(shSystem, sSecondaryWindow))
534         {
535                 GHOST_DisposeWindow(shSystem, sSecondaryWindow);
536         }
537
538         /* Dispose the system */
539         GHOST_DisposeSystem(shSystem);
540         GHOST_DisposeEventConsumer(consumer);
541         
542         return 0;
543 }
544
545
546 static void gearsTimerProc(GHOST_TimerTaskHandle hTask, GHOST_TUns64 time)
547 {
548         GHOST_WindowHandle hWindow = NULL;
549     fAngle += 2.0;
550     view_roty += 1.0;
551         hWindow = (GHOST_WindowHandle)GHOST_GetTimerTaskUserData(hTask);
552         if (GHOST_GetFullScreen(shSystem))
553         {
554                 /* Running full screen */
555                 GHOST_InvalidateWindow(sFullScreenWindow);
556         }
557         else
558         {
559                 if (GHOST_ValidWindow(shSystem, hWindow))
560                 {
561                         GHOST_InvalidateWindow(hWindow);
562                 }
563         }
564 }