1 | /* |
---|
2 | * Original Tutorial by Jeff Molofee |
---|
3 | * (ported to Linux/SDL by Ti Leggett '01) |
---|
4 | * |
---|
5 | * Visit Jeff at http://nehe.gamedev.net/ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <GL/gl.h> |
---|
9 | #include <GL/glu.h> |
---|
10 | #include "SDL.h" |
---|
11 | |
---|
12 | /* screen width, height, and bit depth */ |
---|
13 | #define SCREEN_WIDTH 640 |
---|
14 | #define SCREEN_HEIGHT 480 |
---|
15 | #define SCREEN_BPP 32 |
---|
16 | |
---|
17 | /* This is our SDL surface */ |
---|
18 | SDL_Surface *surface; |
---|
19 | |
---|
20 | /* Prototype declaration */ |
---|
21 | int resizeWindow(int width, int height); |
---|
22 | int drawGLScene(); |
---|
23 | |
---|
24 | |
---|
25 | int main( int argc, char **argv ) |
---|
26 | { |
---|
27 | /*================================ SDL INIT ============================================ */ |
---|
28 | // Wir automatisch in der GraphicsEngine gemacht |
---|
29 | // siehe src/lib/graphics/graphics_engine.cc |
---|
30 | |
---|
31 | /* Flags to pass to SDL_SetVideoMode */ |
---|
32 | int videoFlags; |
---|
33 | /* main loop variable */ |
---|
34 | int done = false; |
---|
35 | /* used to collect events */ |
---|
36 | SDL_Event event; |
---|
37 | /* this holds some info about our display */ |
---|
38 | const SDL_VideoInfo *videoInfo; |
---|
39 | |
---|
40 | |
---|
41 | /* initialize SDL */ |
---|
42 | if (SDL_Init( SDL_INIT_VIDEO ) < 0) |
---|
43 | { |
---|
44 | fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError()); |
---|
45 | exit(1); |
---|
46 | } |
---|
47 | |
---|
48 | /* Fetch the video info */ |
---|
49 | videoInfo = SDL_GetVideoInfo(); |
---|
50 | |
---|
51 | if(!videoInfo) |
---|
52 | { |
---|
53 | fprintf(stderr, "Video query failed: %s\n", SDL_GetError()); |
---|
54 | exit(1); |
---|
55 | } |
---|
56 | |
---|
57 | /* the flags to pass to SDL_SetVideoMode */ |
---|
58 | videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */ |
---|
59 | videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */ |
---|
60 | videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */ |
---|
61 | videoFlags |= SDL_RESIZABLE; /* Enable window resizing */ |
---|
62 | |
---|
63 | /* This checks to see if surfaces can be stored in memory */ |
---|
64 | if(videoInfo->hw_available) |
---|
65 | videoFlags |= SDL_HWSURFACE; |
---|
66 | else |
---|
67 | videoFlags |= SDL_SWSURFACE; |
---|
68 | |
---|
69 | /* This checks if hardware blits can be done */ |
---|
70 | if (videoInfo->blit_hw) |
---|
71 | videoFlags |= SDL_HWACCEL; |
---|
72 | |
---|
73 | /* Sets up OpenGL double buffering */ |
---|
74 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); |
---|
75 | |
---|
76 | /* get a SDL surface */ |
---|
77 | surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, |
---|
78 | videoFlags ); |
---|
79 | |
---|
80 | /* Verify there is a surface */ |
---|
81 | if(!surface) |
---|
82 | { |
---|
83 | fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError()); |
---|
84 | exit(1); |
---|
85 | } |
---|
86 | |
---|
87 | /* initialize OpenGL */ |
---|
88 | /* Set the background black */ |
---|
89 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
---|
90 | |
---|
91 | /* Enables Depth Testing */ |
---|
92 | glEnable( GL_DEPTH_TEST ); |
---|
93 | |
---|
94 | |
---|
95 | /* resize the initial window */ |
---|
96 | resizeWindow(SCREEN_WIDTH, SCREEN_HEIGHT); |
---|
97 | |
---|
98 | |
---|
99 | /*================================ MAIN-LOOP ============================================ */ |
---|
100 | // Entspricht der Orxonox-Main-Loop in |
---|
101 | // src/story_entities/game_world.cc:291 |
---|
102 | // ( void GameWorld::run() ) |
---|
103 | |
---|
104 | |
---|
105 | /* wait for events */ |
---|
106 | while(!done) |
---|
107 | { |
---|
108 | /* handle the events in the queue */ |
---|
109 | |
---|
110 | while(SDL_PollEvent(&event)) |
---|
111 | { |
---|
112 | switch( event.type ) |
---|
113 | { |
---|
114 | case SDL_VIDEORESIZE: |
---|
115 | /* handle resize event */ |
---|
116 | surface = SDL_SetVideoMode(event.resize.w, event.resize.h, 16, videoFlags); |
---|
117 | if(!surface) |
---|
118 | { |
---|
119 | fprintf(stderr, "Could not get a surface after resize: %s\n", SDL_GetError()); |
---|
120 | exit(1); |
---|
121 | } |
---|
122 | resizeWindow(event.resize.w, event.resize.h); |
---|
123 | break; |
---|
124 | case SDL_QUIT: |
---|
125 | /* handle quit requests */ |
---|
126 | done = true; |
---|
127 | break; |
---|
128 | default: |
---|
129 | break; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | // hier wird das ganze gezeichnet! |
---|
134 | drawGLScene(); |
---|
135 | } |
---|
136 | |
---|
137 | /* clean up the window */ |
---|
138 | SDL_Quit( ); |
---|
139 | |
---|
140 | /* Should never get here */ |
---|
141 | return( 0 ); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | /* Here goes our drawing code */ |
---|
146 | int drawGLScene() |
---|
147 | { |
---|
148 | /* rotational vars for the triangle and quad, respectively */ |
---|
149 | static GLfloat rtri; |
---|
150 | /* These are to calculate our fps */ |
---|
151 | static GLint T0 = 0; |
---|
152 | static GLint Frames = 0; |
---|
153 | |
---|
154 | |
---|
155 | /* Clear The Screen And The Depth Buffer */ |
---|
156 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
157 | |
---|
158 | |
---|
159 | /* ================================== WorldEntity zeichnen ======================================== */ |
---|
160 | // das ist der code, der in eine WorldEntity::draw() funktion passen wuerde |
---|
161 | |
---|
162 | |
---|
163 | /* ================================== WorldEntity zeichnen fertig ================================ */ |
---|
164 | |
---|
165 | |
---|
166 | /* Draw it to the screen */ |
---|
167 | SDL_GL_SwapBuffers(); |
---|
168 | |
---|
169 | /* Gather our frames per second */ |
---|
170 | Frames++; |
---|
171 | { |
---|
172 | GLint t = SDL_GetTicks(); |
---|
173 | if (t - T0 >= 5000) { |
---|
174 | GLfloat seconds = (t - T0) / 1000.0; |
---|
175 | GLfloat fps = Frames / seconds; |
---|
176 | printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps); |
---|
177 | T0 = t; |
---|
178 | Frames = 0; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | /* Increase The Rotation Variable For The Triangle ( NEW ) */ |
---|
183 | rtri += 0.2f; |
---|
184 | |
---|
185 | return(true); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | /* function to reset our viewport after a window resize */ |
---|
191 | int resizeWindow(int width, int height) |
---|
192 | { |
---|
193 | /* Height / width ration */ |
---|
194 | GLfloat ratio; |
---|
195 | |
---|
196 | /* Protect against a divide by zero */ |
---|
197 | if(height == 0) |
---|
198 | height = 1; |
---|
199 | |
---|
200 | ratio = (GLfloat)width / (GLfloat)height; |
---|
201 | |
---|
202 | /* Setup our viewport. */ |
---|
203 | glViewport(0, 0, (GLsizei)width, (GLsizei)height); |
---|
204 | |
---|
205 | /* change to the projection matrix and set our viewing volume. */ |
---|
206 | glMatrixMode(GL_PROJECTION); |
---|
207 | glLoadIdentity(); |
---|
208 | |
---|
209 | /* Set our perspective */ |
---|
210 | gluPerspective(90.0f, ratio, 0.1f, 100.0f); |
---|
211 | |
---|
212 | /* Make sure we're chaning the model view and not the projection */ |
---|
213 | glMatrixMode(GL_MODELVIEW); |
---|
214 | |
---|
215 | /* Reset The View */ |
---|
216 | glLoadIdentity(); |
---|
217 | |
---|
218 | return(true); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|