Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/test/sdl_opengl_test/sdl_opengl_test.cc @ 9930

Last change on this file since 9930 was 9930, checked in by stefalie, 17 years ago

test: added simple tut

File size: 7.2 KB
Line 
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 */
18SDL_Surface *surface;
19
20/* Prototype declaration */
21int resizeWindow(int width, int height);
22int drawGLScene();
23
24
25int 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 */
146int 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  glLoadIdentity();
163  /* Move the object */
164  glTranslatef(0.0f, 0.0f, -6.0f);
165
166  /* Rotate The Triangle On The Y axis */
167  glRotatef(rtri, 0.0f, 1.0f, 0.0f);
168
169  /* Drawing the Triangles4 */
170  glBegin(GL_TRIANGLES);           /* Drawing Using Triangles        */
171  glColor3f(  2.0f,  0.0f,  0.0f); /* Red                            */
172  glVertex3f( 0.0f,  2.0f,  0.0f); /* Top Of Triangle (Front)        */
173  glColor3f(  0.0f,  2.0f,  0.0f); /* Green                          */
174  glVertex3f(-2.0f, -2.0f,  2.0f); /* Left Of Triangle (Front)       */
175  glColor3f(  0.0f,  0.0f,  2.0f); /* Blue                           */
176  glVertex3f( 2.0f, -2.0f,  2.0f); /* Right Of Triangle (Front)      */
177
178  glColor3f(  2.0f,  0.0f,  0.0f); /* Red                            */
179  glVertex3f( 0.0f,  2.0f,  0.0f); /* Top Of Triangle (Right)        */
180  glColor3f(  0.0f,  0.0f,  2.0f); /* Blue                           */
181  glVertex3f( 2.0f, -2.0f,  2.0f); /* Left Of Triangle (Right)       */
182  glColor3f(  0.0f,  2.0f,  0.0f); /* Green                          */
183  glVertex3f( 2.0f, -2.0f, -2.0f); /* Right Of Triangle (Right)      */
184
185  glColor3f(  2.0f,  0.0f,  0.0f); /* Red                            */
186  glVertex3f( 0.0f,  2.0f,  0.0f); /* Top Of Triangle (Back)         */
187  glColor3f(  0.0f,  2.0f,  0.0f); /* Green                          */
188  glVertex3f( 2.0f, -2.0f, -2.0f); /* Left Of Triangle (Back)        */
189  glColor3f(  0.0f,  0.0f,  2.0f); /* Blue                           */
190  glVertex3f(-2.0f, -2.0f, -2.0f); /* Right Of Triangle (Back)       */
191
192  glColor3f(  2.0f,  0.0f,  0.0f); /* Red                            */
193  glVertex3f( 0.0f,  2.0f,  0.0f); /* Top Of Triangle (Left)         */
194  glColor3f(  0.0f,  0.0f,  2.0f); /* Blue                           */
195  glVertex3f(-2.0f, -2.0f, -2.0f); /* Left Of Triangle (Left)        */
196  glColor3f(  0.0f,  2.0f,  0.0f); /* Green                          */
197  glVertex3f(-2.0f, -2.0f,  2.0f); /* Right Of Triangle (Left)       */
198  glEnd();                         /* Finished Drawing The Triangles */
199  /* ================================== WorldEntity zeichnen fertig ================================ */
200
201
202  /* Draw it to the screen */
203   SDL_GL_SwapBuffers();
204
205  /* Gather our frames per second */
206  Frames++;
207  {
208    GLint t = SDL_GetTicks();
209    if (t - T0 >= 5000) {
210      GLfloat seconds = (t - T0) / 1000.0;
211      GLfloat fps = Frames / seconds;
212      printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
213      T0 = t;
214      Frames = 0;
215    }
216  }
217
218  /* Increase The Rotation Variable For The Triangle ( NEW ) */
219  rtri  += 0.2f;
220
221  return(true);
222}
223
224
225
226/* function to reset our viewport after a window resize */
227int resizeWindow(int width, int height)
228{
229  /* Height / width ration */
230  GLfloat ratio;
231
232  /* Protect against a divide by zero */
233  if(height == 0)
234    height = 1;
235
236  ratio = (GLfloat)width / (GLfloat)height;
237
238  /* Setup our viewport. */
239  glViewport(0, 0, (GLsizei)width, (GLsizei)height);
240
241  /* change to the projection matrix and set our viewing volume. */
242  glMatrixMode(GL_PROJECTION);
243  glLoadIdentity();
244
245  /* Set our perspective */
246  gluPerspective(90.0f, ratio, 0.1f, 100.0f);
247
248  /* Make sure we're chaning the model view and not the projection */
249  glMatrixMode(GL_MODELVIEW);
250
251  /* Reset The View */
252  glLoadIdentity();
253
254  return(true);
255}
256
257
Note: See TracBrowser for help on using the repository browser.