00001 #include <iostream>
00002 #include <cstdlib>
00003 #include <cstring>
00004
00005
00006 #include "utOpenGL.h"
00007 #if defined(_MSC_VER) || defined(_WINDOWS_) || defined(_WIN32)
00008 #include "glfw.h"
00009 #else
00010 #include <GL/glfw.h>
00011 #endif
00012
00013 #include "Start.h"
00014 #include "Game.h"
00015
00016 const char *caption = "The Game";
00017 const int windowWidth = 800;
00018 const int windowHeight = 600;
00019 static bool fullscreen = false;
00020 static bool running;
00021 static int mx0, my0;
00022
00023 Game *game;
00024
00025 int main(int argc, char* argv[]) {
00026
00027 glfwInit();
00028 if(! setupWindow(windowWidth, windowHeight, fullscreen)) return -1;
00029
00030
00031
00032 game = new Game();
00033
00034 if (! game->init()) {
00035
00036 glfwCloseWindow();
00037 glfwOpenWindow(800, 16, 8, 8, 8, 8, 24, 8, GLFW_WINDOW);
00038 glfwSetWindowTitle("Unable to initalize engine - Make sure you have an OpenGL 2.0 compatible graphics card");
00039 glfwSleep(5);
00040
00041 std::cout << "Unable to initalize engine" << std::endl;
00042 std::cout << "Make sure you have an OpenGL 2.0 compatible graphics card";
00043 glfwTerminate();
00044 return -1;
00045 }
00046
00047 game->resize(windowWidth, windowHeight);
00048
00049
00050 glfwDisable(GLFW_MOUSE_CURSOR);
00051
00052 float fps = 0.0f;
00053 double lastTime = glfwGetTime();
00054 int frames = 0;
00055 running = true;
00056
00057
00058 while(running) {
00059
00060 double currentTime = glfwGetTime();
00061 float usedTime = currentTime - lastTime;
00062 frames++;
00063 if(frames >= 3){
00064 fps = frames / usedTime;
00065 frames = 0;
00066 }
00067
00068 lastTime = currentTime;
00069
00070
00071 game->update(usedTime, fps);
00072 glfwSwapBuffers();
00073 }
00074
00075
00076 glfwEnable(GLFW_MOUSE_CURSOR);
00077
00078
00079 game->release();
00080 delete game;
00081
00082 try {
00083 glfwTerminate();
00084 }
00085 catch(...) {
00086
00087 std::cout << "GLFW returns an error while cleaning up" << std::endl;
00088 }
00089
00090 char name[2];
00091 std::cout << "press enter";
00092 std::cin.getline (name,2);
00093
00094 return 0;
00095 }
00096
00097 bool setupWindow(int width, int height, bool fullscreen) {
00098
00099 if(! glfwOpenWindow( width, height, 8, 8, 8, 8, 24, 8, fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW)) {
00100 glfwTerminate();
00101 return false;
00102 }
00103
00104 if(! fullscreen) glfwSetWindowTitle( caption);
00105
00106
00107 glfwSwapInterval(0);
00108
00109
00110 glfwSetWindowCloseCallback(windowCloseListener);
00111 glfwSetKeyCallback( keyPressListener);
00112 glfwSetMousePosCallback( mouseMoveListener);
00113 glfwSetMouseButtonCallback( mouseButtonListener);
00114
00115 return true;
00116 }
00117
00118 int GLFWCALL windowCloseListener() {
00119 running = false;
00120 return 0;
00121 }
00122
00123
00124 void GLFWCALL keyPressListener(int key, int action) {
00125 if(! running) return;
00126
00127 if( action == GLFW_PRESS) {
00128 int width = windowWidth;
00129 int height = windowHeight;
00130
00131 switch (key) {
00132 case GLFW_KEY_ESC:
00133
00134 running = false;
00135 break;
00136 case GLFW_KEY_SPACE:
00137
00138 break;
00139 case GLFW_KEY_F1:
00140
00141
00142
00143
00144
00145 fullscreen = !fullscreen;
00146
00147 if(fullscreen) {
00148 GLFWvidmode mode;
00149 glfwGetDesktopMode(&mode);
00150
00151 float aspect = mode.Width / (float)mode.Height;
00152 if( (int)(aspect * 100) == 133 || (int)(aspect * 100) == 125)
00153 {
00154 width = 1280;
00155 height = 1024;
00156 }
00157 else if( (int)(aspect * 100) == 160)
00158 {
00159 width = 1280;
00160 height = 800;
00161 }
00162 else
00163 {
00164
00165 width = mode.Width;
00166 height = mode.Height;
00167 }
00168 }
00169
00170 if(! setupWindow(width, height, fullscreen)) {
00171 glfwTerminate();
00172 exit(-1);
00173 }
00174
00175 game->resize(width, height);
00176
00177 break;
00178 default:
00179
00180 break;
00181 }
00182 }
00183
00184 if( key >= 0) game->keyStateChange(key, action == GLFW_PRESS);
00185 }
00186
00187
00188 void GLFWCALL mouseMoveListener(int x, int y) {
00189 if(! running) {
00190 mx0 = x;
00191 my0 = y;
00192 return;
00193 }
00194
00195 game->mouseMoveEvent((float)(x - mx0), (float)(my0 - y));
00196 mx0 = x;
00197 my0 = y;
00198 }
00199
00200 void GLFWCALL mouseButtonListener(int button, int action) {
00201 game->mouseButtonEvent(button, action == GLFW_PRESS);
00202 }