Unuk 1.0
src/Unuk/main.cpp
Go to the documentation of this file.
00001 #ifdef __unix__
00002 #include <sys/time.h>
00003 #endif
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <GL/gl.h>
00008 #include <GL/glu.h>
00009 #include "SDL/SDL.h"
00010 #include "Game.h"
00011 #include "../libUnuk/Input.h"
00012 #include "../libUnuk/Debug.h"
00013 
00014 // Screen width, height, and bit depth.
00015 const int SCREEN_WIDTH  = 640;
00016 const int SCREEN_HEIGHT = 480;
00017 const int SCREEN_BPP    = 16;
00018 
00019 // Define our SDL surface.
00020 SDL_Surface *surface;
00021 
00022 void Quit(int returnCode) {
00023   Debug::logger->message("-----Cleaning Up------");
00024   // Clean up the window.
00025   SDL_Quit();
00026   Debug::logger->message("Window destroyed!");
00027   Debug::closeLog();
00028   // And exit appropriately.
00029   exit(returnCode);
00030 }
00031 
00032 // Reset our viewport after a window resize.
00033 int ResizeWindow(int width, int height) {
00034   // Height and width ration.
00035   GLfloat ratio;
00036 
00037   // Prevent divide by zero.
00038   if(height == 0)
00039     height = 1;
00040 
00041   ratio = (GLfloat)width / (GLfloat)height;
00042 
00043   // Setup our viewport.
00044   glViewport(0, 0, (GLsizei)width, (GLsizei)height);
00045 
00046   // Change to the projection matrix and set our viewing volume.
00047   glMatrixMode(GL_PROJECTION);
00048   glLoadIdentity();
00049 
00050   // Set our perspective.
00051   gluPerspective(45.0f, ratio, 0.1f, 100.0f);
00052 
00053   // Change to the MODELVIEW.
00054   glMatrixMode(GL_MODELVIEW);
00055 
00056   // Reset The View.
00057   glLoadIdentity();
00058 
00059   return 1;
00060 }
00061 
00062 void ProcessEvents(SDL_keysym *keysym) {
00063   switch(keysym->sym) {
00064   case SDLK_ESCAPE:
00065     // Quit if we detect 'esc' key.
00066     Quit(0);
00067     break;
00068   case SDLK_F1:
00069     // Fullscreen.
00070     SDL_WM_ToggleFullScreen(surface);
00071     break;
00072   default:
00073     break;
00074   }
00075   return;
00076 }
00077 
00078 int InitGL(void) {
00079 
00080   // Enable smooth shading.
00081   glShadeModel(GL_SMOOTH);
00082 
00083   // Set the background black.
00084   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00085 
00086   // Depth buffer setup.
00087   glClearDepth(1.0f);
00088   glEnable(GL_DEPTH_TEST);
00089   glDepthFunc(GL_LEQUAL);
00090 
00091   // Nice Perspective Calculations.
00092   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00093 
00094   return 1;
00095 }
00096 
00097 unsigned int GetTickCount() {
00098   struct timeval t;
00099   gettimeofday(&t, NULL);
00100 
00101   unsigned long secs = t.tv_sec * 1000;
00102   secs += (t.tv_usec / 1000);
00103   return secs;
00104 }
00105 
00106 float GetElapsedSeconds(void) {
00107   unsigned int lastTime = 0;
00108   unsigned int currentTime = GetTickCount();
00109   unsigned int diff = currentTime - lastTime;
00110   lastTime = currentTime;
00111   return float(diff) / 1000.0f;
00112 }
00113 
00114 int main() {
00115   // Initialize our Debug log.
00116   Debug::openLog(true);
00117   Debug::logger->message("-----Debug Initialized-----");
00118 
00119   int videoFlags;
00120   bool done = false;
00121   SDL_Event event;
00122   const SDL_VideoInfo *videoInfo;
00123   Game game;
00124 
00125   // Initialize SDL.
00126   if(SDL_Init(SDL_INIT_VIDEO) < 0) {
00127     fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError());
00128     Quit(1);
00129   }
00130 
00131   // Fetch the video info.
00132   videoInfo = SDL_GetVideoInfo();
00133 
00134   // Set the window caption.
00135   SDL_WM_SetCaption("Unuk", NULL);
00136 
00137   if(!videoInfo) {
00138     fprintf( stderr, "Video query failed: %s\n", SDL_GetError());
00139     Quit(1);
00140   }
00141 
00142   // Pass some flags to SDL_SetVideoMode.
00143   videoFlags  = SDL_OPENGL;
00144   videoFlags |= SDL_GL_DOUBLEBUFFER;
00145   videoFlags |= SDL_HWPALETTE;
00146   videoFlags |= SDL_RESIZABLE;
00147 
00148   // Can the surface be stored in memory?
00149   if(videoInfo->hw_available)
00150     videoFlags |= SDL_HWSURFACE;
00151   else
00152     videoFlags |= SDL_SWSURFACE;
00153 
00154   // Can we perform blitting on the GPU?
00155   if(videoInfo->blit_hw)
00156     videoFlags |= SDL_HWACCEL;
00157 
00158   // Set up the OpenGL double buffer.
00159   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
00160 
00161   // Get an SDL surface.
00162   surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags);
00163 
00164   // Is there an SDL surface?
00165   if(!surface) {
00166     fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError());
00167     Quit(1);
00168   }
00169 
00170   // Initialize OpenGL.
00171   InitGL();
00172 
00173   game.Init();
00174   Debug::logger->message("Game Initialize!");
00175 
00176   Debug::logger->message("\n\n-----Engine Initialization Complete-----");
00177   Debug::logger->message("\n\n-----Logic-----");
00178 
00179   while(!done) {
00180     // Time to poll events.
00181     while(SDL_PollEvent(&event)) {
00182       switch(event.type) {
00183       case SDL_VIDEORESIZE:
00184         // Handle resize events.
00185         surface = SDL_SetVideoMode(event.resize.w, event.resize.h, 16, videoFlags);
00186         if(!surface) {
00187           Debug::logger->message("Could not get a surface after resize\n");
00188           Quit(1);
00189         }
00190         ResizeWindow(event.resize.w, event.resize.h);
00191         break;
00192       case SDL_KEYDOWN:
00193         // handle keydown events.
00194         ProcessEvents(&event.key.keysym);
00195         break;
00196       case SDL_QUIT:
00197         // Handle quit events.
00198         done = true;
00199         break;
00200       default:
00201         break;
00202       }
00203       //CreateInput();
00204       //UpdateInput();
00205     }
00206     // Render the scene.
00207     float elapsedTime = GetElapsedSeconds();
00208     //game.ProcessEvents();
00209     game.Prepare(elapsedTime);
00210     game.Render();
00211     SDL_GL_SwapBuffers();
00212   }
00213   game.Shutdown();
00214   // Clean ourselves up and exit.
00215   Quit(0);
00216 
00217   // We should never get here.
00218   return(0);
00219 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines