File: gameGLUT.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,232 kB
  • ctags: 3,287
  • sloc: cpp: 27,302; php: 765; ansic: 636; objc: 272; makefile: 139; sh: 136; perl: 67
file content (218 lines) | stat: -rw-r--r-- 4,178 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "game.h"

#include <stdio.h>
#include <stdlib.h>

#include <GL/gl.h>
#include <GL/glut.h>



// callbacks for GLUT
void callbackMotion( int inX, int inY );
// ignore passive motion
// void callbackPassiveMotion( int inX, int inY );
void callbackMouse( int inButton, int inState, int inX, int inY );
void callbackDisplay();
void callbackTimer( int inValue );

// keyboard to catch quit keys (don't pass to game)
void callbackKeyboard( unsigned char inKey, int inX, int inY );


void callbackReshape( int inW, int inH );



void cleanUpAtExit() {
    printf( "Exiting...\n" );
    
    freeFrameDrawer();    
    }


int w = 320;
int h = 480;


// 25 fps
//int frameMS = 40;
int frameMS = 500;

// s and f keys to slow down and speed up for testing
//char enableSlowdownKeys = false;
char enableSlowdownKeys = true;



int main( int inNumArgs, char **inArgs ) {
    
    glutInit( &inNumArgs, inArgs );
    
    
    
    atexit( cleanUpAtExit );


    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );

    glutInitWindowSize( w, h );
    glutCreateWindow( "game1" );


    glutMotionFunc( callbackMotion );
	glutMouseFunc( callbackMouse );
	//glutPassiveMotionFunc( callbackPassiveMotion );
	glutDisplayFunc( callbackDisplay );
	//glutIdleFunc( callbackIdle );

    glutReshapeFunc( callbackReshape );
    

    // 25 fps
    glutTimerFunc( frameMS, callbackTimer, 0 );
    


    glutKeyboardFunc( callbackKeyboard );




    #ifdef __mac__
        // make sure working directory is the same as the directory
        // that the app resides in
        // this is especially important on the mac platform, which
        // doesn't set a proper working directory for double-clicked
        // app bundles

        // arg 0 is the path to the app executable
        char *appDirectoryPath = stringDuplicate( inArgs[0] );
    
        char *appNamePointer = strstr( appDirectoryPath,
                                       "tileGame1.app" );

        if( appNamePointer != NULL ) {
            // terminate full app path to get parent directory
            appNamePointer[0] = '\0';
            
            chdir( appDirectoryPath );
            }
        
        delete [] appDirectoryPath;
    #endif





    initFrameDrawer( w, h );


    glutMainLoop();


    return 0;    
    }




void callbackDisplay() {
		
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( 0, 320, 480, 0, -1.0f, 1.0f);
    
    
    glMatrixMode(GL_MODELVIEW);

    glDisable( GL_TEXTURE_2D );
	glDisable( GL_CULL_FACE );
    glDisable( GL_DEPTH_TEST );
    
    drawFrame();
    
	
	glutSwapBuffers();
	}


/*
void callbackIdle() {
	glutPostRedisplay();
	}
*/

void callbackTimer( int inValue ) {
    glutTimerFunc( frameMS, callbackTimer, 0 );
    
    
    if( glutGet( GLUT_WINDOW_WIDTH ) != w ||
        glutGet( GLUT_WINDOW_HEIGHT ) != h ) {
        
        glutReshapeWindow( w, h );
        }
    
    glutPostRedisplay();
	}



void callbackMotion( int inX, int inY ) {
    pointerMove( inX, inY );
	}
		
	
	
void callbackMouse( int inButton, int inState, int inX, int inY ) {
	if( inState == GLUT_DOWN ) {
        pointerDown( inX, inY );
        }
    else if( inState == GLUT_UP ) {
        pointerUp( inX, inY );
        }
    else {
        printf( "Error:  Unknown mouse state received from OpenGL\n" );
        }	
    }


void callbackKeyboard( unsigned char inKey, int inX, int inY ) {
    
    // q or escape
    if( inKey == 'q' || inKey == 'Q' || inKey == 27 ) {
        exit( 0 );
        }
    

    if( enableSlowdownKeys ) {
        
        if( inKey == 's' || inKey == 'S' ) {
            // slow
            frameMS = 500;
            }
        if( inKey == 'f' || inKey == 'F' ) {
            // normal
            frameMS = 40;
            }
        }
    
    
	}


void callbackReshape( int inW, int inH ) {
    // force original size
    glutReshapeWindow( w, h );

    // note that we also need to check this in the main loop above
    // because sometimes callbackReshape isn't getting called (maybe
    // after releasing the mouse near the end of a resize)
    }