2014-10-20 15:31:26 +00:00
|
|
|
#include "main.hh"
|
|
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2015-03-12 18:14:14 +00:00
|
|
|
#include "keyboardState.hh"
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2015-04-25 20:29:37 +00:00
|
|
|
Application app;
|
|
|
|
GLFWwindow* window;
|
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
static void resizeCallback(GLFWwindow* window, int newWidth, int newHeight)
|
2014-11-15 14:19:48 +00:00
|
|
|
{
|
|
|
|
// store the new window size and adjust the viewport:
|
2014-12-04 00:13:59 +00:00
|
|
|
app.getGraphics()->resize(glm::uvec2( newWidth, newHeight));
|
2014-11-15 14:19:48 +00:00
|
|
|
glViewport( 0, 0, newWidth, newHeight);
|
|
|
|
}
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
static void keyCallback(GLFWwindow* _window, int _key, int, int _action, int)
|
|
|
|
{
|
2015-03-15 14:02:30 +00:00
|
|
|
if (!app.isGameStarted() && _action == GLFW_PRESS && _key != GLFW_KEY_ESCAPE) {
|
2015-03-07 18:59:52 +00:00
|
|
|
app.startGame();
|
|
|
|
}
|
2014-10-20 15:31:26 +00:00
|
|
|
if (_key == GLFW_KEY_ESCAPE && _action == GLFW_PRESS) {
|
2014-11-25 15:01:13 +00:00
|
|
|
if (app.isFocused() && !app.isLocked()) {
|
|
|
|
glfwSetWindowShouldClose( _window, GL_TRUE );
|
|
|
|
}
|
2015-01-25 22:29:37 +00:00
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
2014-11-25 15:01:13 +00:00
|
|
|
app.setCameraLock(false);
|
|
|
|
}
|
2015-03-15 14:18:16 +00:00
|
|
|
#ifdef SAXUM_DEBUG
|
2015-03-12 18:24:10 +00:00
|
|
|
if (_key == GLFW_KEY_F5 && _action == GLFW_PRESS) {
|
|
|
|
app.getGraphics()->setRenderShadows(!app.getGraphics()->getRenderShadows());
|
|
|
|
}
|
|
|
|
if (_key == GLFW_KEY_F6 && _action == GLFW_PRESS) {
|
|
|
|
app.getGraphics()->setRenderFlames(!app.getGraphics()->getRenderFlames());
|
|
|
|
}
|
2015-03-13 14:30:21 +00:00
|
|
|
if (_key == GLFW_KEY_F7 && _action == GLFW_PRESS) {
|
|
|
|
app.getGraphics()->setRenderDebug(!app.getGraphics()->getRenderDebug());
|
|
|
|
}
|
2015-03-13 14:43:10 +00:00
|
|
|
if (_key == GLFW_KEY_F8 && _action == GLFW_PRESS) {
|
|
|
|
app.getGraphics()->setRenderWorld(!app.getGraphics()->getRenderWorld());
|
|
|
|
}
|
2015-03-15 17:06:24 +00:00
|
|
|
if (_key == GLFW_KEY_F9 && _action == GLFW_PRESS) {
|
|
|
|
app.getLevel()->printPosition();
|
|
|
|
}
|
2015-03-15 13:55:12 +00:00
|
|
|
#endif
|
2014-11-25 15:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mouseCallback(GLFWwindow* window, int button, int action, int) {
|
2015-03-07 18:59:52 +00:00
|
|
|
if (!app.isGameStarted() && action == GLFW_PRESS) {
|
|
|
|
app.startGame();
|
|
|
|
}
|
2014-11-25 15:01:13 +00:00
|
|
|
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
2015-03-12 14:44:21 +00:00
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
|
|
glfwSetCursorPos(window, ((double)app.getGraphics()->getWindowSize().x)/2.0, ((double)app.getGraphics()->getWindowSize().y)/2.0);
|
2014-11-25 15:01:13 +00:00
|
|
|
app.setCameraLock(true);
|
2014-10-20 15:31:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 13:57:01 +00:00
|
|
|
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
|
|
|
|
app.getLevel()->getCamera()->updateDistance(-(float)yoffset);
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:01:13 +00:00
|
|
|
static void focusCallback(GLFWwindow* window, int focused) {
|
|
|
|
if (focused) {
|
|
|
|
// Hide mouse cursor
|
2015-03-12 14:44:21 +00:00
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
2014-11-25 15:01:13 +00:00
|
|
|
app.setFocused(focused);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Show mouse cursor
|
2015-01-25 22:29:37 +00:00
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
2014-11-25 15:01:13 +00:00
|
|
|
app.setFocused(focused);
|
|
|
|
app.setCameraLock(focused);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 22:29:37 +00:00
|
|
|
void setGLFWHintsForOpenGLVersion( unsigned int _version )
|
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#if (ACGL_OPENGL_VERSION >= 30)
|
|
|
|
// request OpenGL 3.2, will return a 4.1 context on Mavericks
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// non-apple
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, _version / 10 );
|
|
|
|
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, _version % 10 );
|
|
|
|
#ifdef ACGL_OPENGL_PROFILE_CORE
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool createWindow()
|
|
|
|
{
|
|
|
|
// Initialise GLFW
|
|
|
|
if ( !glfwInit() )
|
|
|
|
{
|
|
|
|
ACGL::Utils::error() << "Failed to initialize GLFW" << std::endl;
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-01-25 22:29:37 +00:00
|
|
|
// Configure OpenGL context
|
|
|
|
setGLFWHintsForOpenGLVersion( ACGL_OPENGL_VERSION );
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-01-25 22:29:37 +00:00
|
|
|
// activate multisampling (second parameter is the number of samples):
|
2015-03-09 07:21:44 +00:00
|
|
|
glfwWindowHint( GLFW_SAMPLES, 4);
|
2015-03-15 13:55:12 +00:00
|
|
|
|
2015-03-15 14:18:16 +00:00
|
|
|
#ifdef SAXUM_DEBUG
|
2015-03-15 13:55:12 +00:00
|
|
|
// request an OpenGL debug context:
|
|
|
|
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, true );
|
|
|
|
#endif
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-01-25 22:29:37 +00:00
|
|
|
// define whether the window can get resized:
|
|
|
|
glfwWindowHint(GLFW_RESIZABLE, true);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-03-15 16:48:46 +00:00
|
|
|
if(app.makeFullscreen()) {
|
|
|
|
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
|
|
|
|
|
|
|
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
|
|
|
|
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
|
|
|
|
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
|
|
|
|
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
|
2015-05-16 11:54:01 +00:00
|
|
|
app.getGraphics()->saveWindowSize(glm::uvec2(mode->width, mode->height));
|
2015-03-15 16:48:46 +00:00
|
|
|
window = glfwCreateWindow(mode->width, mode->height, "Saxum", glfwGetPrimaryMonitor(), NULL);
|
|
|
|
|
|
|
|
if (!window) {
|
|
|
|
ACGL::Utils::error() << "Failed to open a GLFW window - requested OpenGL: " << ACGL_OPENGL_VERSION << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// try to create an OpenGL context in a window and check the supported OpenGL version:
|
|
|
|
// R,G,B,A, Depth,Stencil
|
|
|
|
window = glfwCreateWindow(app.getGraphics()->getWindowSize().x, app.getGraphics()->getWindowSize().y, "Saxum", NULL, NULL);
|
|
|
|
if (!window) {
|
|
|
|
ACGL::Utils::error() << "Failed to open a GLFW window - requested OpenGL: " << ACGL_OPENGL_VERSION << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-25 22:29:37 +00:00
|
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
2015-03-15 14:18:16 +00:00
|
|
|
#ifdef SAXUM_DEBUG
|
2015-03-15 13:55:12 +00:00
|
|
|
ACGL::init(true);
|
|
|
|
#else
|
|
|
|
ACGL::init(false); // do not request debug context
|
|
|
|
#endif
|
2015-01-25 22:29:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
2015-01-25 22:07:49 +00:00
|
|
|
// app gets created as global variable, to work properly with GLFW
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
// Create OpenGL capable window:
|
2015-01-25 22:29:37 +00:00
|
|
|
if ( !createWindow() ) {
|
2014-10-20 15:31:26 +00:00
|
|
|
glfwTerminate();
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
// Ensure we can capture the escape key being pressed below
|
2015-01-25 22:29:37 +00:00
|
|
|
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
|
2014-11-25 15:01:13 +00:00
|
|
|
// set Callbacks
|
2015-01-25 22:29:37 +00:00
|
|
|
glfwSetWindowSizeCallback(window, resizeCallback);
|
|
|
|
glfwSetWindowFocusCallback(window, focusCallback);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
// Enable vertical sync (on cards that support it) with parameter 1 - 0 means off
|
|
|
|
glfwSwapInterval( 0 );
|
2015-05-16 11:54:01 +00:00
|
|
|
|
2014-11-14 15:47:47 +00:00
|
|
|
app.init();
|
2015-05-16 11:54:01 +00:00
|
|
|
app.getGraphics()->resize(app.getGraphics()->getWindowSize());
|
2015-03-07 18:59:52 +00:00
|
|
|
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
app.initLevel();
|
2015-03-15 14:02:30 +00:00
|
|
|
|
|
|
|
glfwSetKeyCallback(window, keyCallback );
|
|
|
|
glfwSetScrollCallback(window, scrollCallback );
|
|
|
|
glfwSetMouseButtonCallback(window, mouseCallback);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
int frameCount = 0;
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-03-15 21:32:13 +00:00
|
|
|
const double FPSdelay = 1.0;
|
2014-10-20 15:31:26 +00:00
|
|
|
double startTimeInSeconds = glfwGetTime();
|
|
|
|
double showNextFPS = startTimeInSeconds + FPSdelay;
|
2014-11-17 11:57:16 +00:00
|
|
|
|
|
|
|
double lastUpdate=0.0f;
|
2015-03-12 18:14:14 +00:00
|
|
|
|
|
|
|
KeyboardState keyboardState;
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
do {
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-11-17 11:57:16 +00:00
|
|
|
double now = glfwGetTime()- startTimeInSeconds;
|
2015-03-15 21:32:13 +00:00
|
|
|
|
2015-03-17 08:41:37 +00:00
|
|
|
if (showNextFPS <= now) {
|
|
|
|
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
|
|
|
sstream << std::setprecision(1) << std::fixed
|
|
|
|
<< "Saxum" << " - FPS: " << frameCount / (now-showNextFPS + FPSdelay) << " " << 1000 * (now-showNextFPS + FPSdelay)/frameCount << " msec";
|
|
|
|
glfwSetWindowTitle(window, sstream.str().c_str() );
|
|
|
|
showNextFPS = now + FPSdelay;
|
|
|
|
frameCount = 0;
|
|
|
|
}
|
2015-03-07 18:59:52 +00:00
|
|
|
if (app.isGameStarted()) {
|
2015-03-07 22:27:41 +00:00
|
|
|
static float gameStart = now;
|
2015-03-07 18:59:52 +00:00
|
|
|
if (app.isLocked() && app.getIgnoredMouseUpdates() == 0) {
|
2015-03-12 18:14:14 +00:00
|
|
|
keyboardState.wPressed = glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS;
|
|
|
|
keyboardState.aPressed = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS;
|
|
|
|
keyboardState.sPressed = glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS;
|
|
|
|
keyboardState.dPressed = glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS;
|
2015-03-15 14:18:16 +00:00
|
|
|
#ifdef SAXUM_DEBUG
|
2015-03-15 13:55:12 +00:00
|
|
|
keyboardState.lPressed = glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS;
|
|
|
|
keyboardState.kPressed = glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS;
|
|
|
|
keyboardState.f1Pressed = glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS;
|
|
|
|
keyboardState.f2Pressed = glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS;
|
|
|
|
keyboardState.f3Pressed = glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS;
|
|
|
|
keyboardState.f4Pressed = glfwGetKey(window, GLFW_KEY_F4) == GLFW_PRESS;
|
|
|
|
#else
|
|
|
|
keyboardState.lPressed = false;
|
|
|
|
keyboardState.kPressed = false;
|
|
|
|
keyboardState.f1Pressed = false;
|
|
|
|
keyboardState.f2Pressed = false;
|
|
|
|
keyboardState.f3Pressed = false;
|
|
|
|
keyboardState.f4Pressed = false;
|
|
|
|
#endif
|
|
|
|
|
2015-03-07 18:59:52 +00:00
|
|
|
|
|
|
|
double xpos, ypos;
|
|
|
|
glfwGetCursorPos(window, &xpos, &ypos);
|
2015-03-12 14:44:21 +00:00
|
|
|
glfwSetCursorPos(window, ((double)app.getGraphics()->getWindowSize().x)/2.0, ((double)app.getGraphics()->getWindowSize().y)/2.0);
|
2015-03-07 22:27:41 +00:00
|
|
|
app.getLevel()->update(now - lastUpdate, now - gameStart,
|
2015-03-12 18:14:14 +00:00
|
|
|
glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2,
|
|
|
|
(float)xpos-app.getGraphics()->getWindowSize().x/2),
|
|
|
|
&keyboardState);
|
2015-03-07 18:59:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-03-12 18:14:14 +00:00
|
|
|
keyboardState.wPressed = false;
|
|
|
|
keyboardState.aPressed = false;
|
|
|
|
keyboardState.sPressed = false;
|
|
|
|
keyboardState.dPressed = false;
|
|
|
|
keyboardState.lPressed = false;
|
|
|
|
keyboardState.kPressed = false;
|
|
|
|
keyboardState.f1Pressed = false;
|
|
|
|
keyboardState.f2Pressed = false;
|
|
|
|
keyboardState.f3Pressed = false;
|
|
|
|
keyboardState.f4Pressed = false;
|
|
|
|
app.getLevel()->update(now - lastUpdate, now-gameStart, glm::vec2(0.0f, 0.0f),
|
|
|
|
&keyboardState);
|
2015-03-07 18:59:52 +00:00
|
|
|
if (app.isLocked()) {
|
|
|
|
app.ignoredOneMouseUpdate();
|
|
|
|
}
|
2014-11-25 15:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-01-25 23:06:31 +00:00
|
|
|
app.getGraphics()->render(now);
|
2014-11-17 11:57:16 +00:00
|
|
|
lastUpdate = now;
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
openGLCriticalError();
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
// MacOS X will not swap correctly is another FBO is bound:
|
|
|
|
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
2015-01-25 22:29:37 +00:00
|
|
|
glfwSwapBuffers(window);
|
2014-10-20 15:31:26 +00:00
|
|
|
glfwPollEvents();
|
|
|
|
frameCount++;
|
2014-11-17 15:07:40 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
} // Check if the window was closed
|
2015-01-25 22:29:37 +00:00
|
|
|
while( !glfwWindowShouldClose(window) );
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
glfwTerminate();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|