Moved all GLFW related stuff to the main files.

This commit is contained in:
Faerbit 2015-01-25 23:29:37 +01:00
parent b6a2b0d7b3
commit 81f94ba221
4 changed files with 77 additions and 83 deletions

View File

@ -70,10 +70,6 @@ void Graphics::init(Level* level) {
depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE); depthTexture_cube->setCompareMode(GL_COMPARE_REF_TO_TEXTURE);
} }
GLFWwindow* Graphics::getWindow() {
return window;
}
glm::uvec2 Graphics::getWindowSize() { glm::uvec2 Graphics::getWindowSize() {
return windowSize; return windowSize;
} }
@ -211,57 +207,3 @@ glm::mat4 Graphics::buildViewMatrix(Level* level) {
float Graphics::getFarPlane() { float Graphics::getFarPlane() {
return farPlane; return farPlane;
} }
void Graphics::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 Graphics::createWindow()
{
// Initialise GLFW
if ( !glfwInit() )
{
ACGL::Utils::error() << "Failed to initialize GLFW" << std::endl;
exit( -1 );
}
// Configure OpenGL context
setGLFWHintsForOpenGLVersion( ACGL_OPENGL_VERSION );
// activate multisampling (second parameter is the number of samples):
//glfwWindowHint( GLFW_SAMPLES, 8 );
// request an OpenGL debug context:
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, true );
// define whether the window can get resized:
glfwWindowHint(GLFW_RESIZABLE, true);
// try to create an OpenGL context in a window and check the supported OpenGL version:
// R,G,B,A, Depth,Stencil
window = glfwCreateWindow(windowSize.x, windowSize.y, "SWP MarbleGame Group C", NULL, NULL);
if (!getWindow()) {
ACGL::Utils::error() << "Failed to open a GLFW window - requested OpenGL: " << ACGL_OPENGL_VERSION << std::endl;
return false;
}
glfwMakeContextCurrent(window);
ACGL::init();
return true;
}

View File

@ -16,8 +16,6 @@ class Graphics {
void render(); void render();
glm::mat4 buildViewMatrix(Level* level); glm::mat4 buildViewMatrix(Level* level);
glm::uvec2 getWindowSize(); glm::uvec2 getWindowSize();
bool createWindow();
GLFWwindow* getWindow();
void resize(glm::uvec2 windowSize); void resize(glm::uvec2 windowSize);
float getFarPlane(); float getFarPlane();
private: private:
@ -25,7 +23,6 @@ class Graphics {
glm::uvec2 windowSize; glm::uvec2 windowSize;
float nearPlane; float nearPlane;
float farPlane; float farPlane;
GLFWwindow* window;
ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram lightingShader;
ACGL::OpenGL::SharedShaderProgram depthShader; ACGL::OpenGL::SharedShaderProgram depthShader;
ACGL::OpenGL::SharedTexture2D depthTexture; ACGL::OpenGL::SharedTexture2D depthTexture;

98
main.cc
View File

@ -16,15 +16,15 @@ static void keyCallback(GLFWwindow* _window, int _key, int, int _action, int)
if (app.isFocused() && !app.isLocked()) { if (app.isFocused() && !app.isLocked()) {
glfwSetWindowShouldClose( _window, GL_TRUE ); glfwSetWindowShouldClose( _window, GL_TRUE );
} }
glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
app.setCameraLock(false); app.setCameraLock(false);
} }
} }
static void mouseCallback(GLFWwindow* window, int button, int action, int) { static void mouseCallback(GLFWwindow* window, int button, int action, int) {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2); glfwSetCursorPos(window, app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2);
app.setCameraLock(true); app.setCameraLock(true);
} }
} }
@ -36,38 +36,92 @@ static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
static void focusCallback(GLFWwindow* window, int focused) { static void focusCallback(GLFWwindow* window, int focused) {
if (focused) { if (focused) {
// Hide mouse cursor // Hide mouse cursor
glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
app.setFocused(focused); app.setFocused(focused);
} }
else { else {
// Show mouse cursor // Show mouse cursor
glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
app.setFocused(focused); app.setFocused(focused);
app.setCameraLock(focused); app.setCameraLock(focused);
} }
} }
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 );
}
// Configure OpenGL context
setGLFWHintsForOpenGLVersion( ACGL_OPENGL_VERSION );
// activate multisampling (second parameter is the number of samples):
//glfwWindowHint( GLFW_SAMPLES, 8 );
// request an OpenGL debug context:
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, true );
// define whether the window can get resized:
glfwWindowHint(GLFW_RESIZABLE, true);
// 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, "SWP MarbleGame Group C", NULL, NULL);
if (!window) {
ACGL::Utils::error() << "Failed to open a GLFW window - requested OpenGL: " << ACGL_OPENGL_VERSION << std::endl;
return false;
}
glfwMakeContextCurrent(window);
ACGL::init();
return true;
}
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
// app gets created as global variable, to work properly with GLFW // app gets created as global variable, to work properly with GLFW
// Create OpenGL capable window: // Create OpenGL capable window:
if ( !app.getGraphics()->createWindow() ) { if ( !createWindow() ) {
glfwTerminate(); glfwTerminate();
exit( -1 ); exit( -1 );
} }
// Set window title to binary name (without the path): // Set window title to binary name (without the path):
std::vector<std::string> tmp = ACGL::Utils::StringHelpers::split( std::string( argv[0] ), '/' ); std::vector<std::string> tmp = ACGL::Utils::StringHelpers::split( std::string( argv[0] ), '/' );
glfwSetWindowTitle(app.getGraphics()->getWindow(), tmp[tmp.size()-1].c_str() ); glfwSetWindowTitle(window, tmp[tmp.size()-1].c_str() );
// Ensure we can capture the escape key being pressed below // Ensure we can capture the escape key being pressed below
glfwSetInputMode(app.getGraphics()->getWindow(), GLFW_STICKY_KEYS, 1); glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
// set Callbacks // set Callbacks
glfwSetWindowSizeCallback(app.getGraphics()->getWindow(), resizeCallback); glfwSetWindowSizeCallback(window, resizeCallback);
glfwSetKeyCallback(app.getGraphics()->getWindow(), keyCallback ); glfwSetKeyCallback(window, keyCallback );
glfwSetScrollCallback(app.getGraphics()->getWindow(), scrollCallback ); glfwSetScrollCallback(window, scrollCallback );
glfwSetWindowFocusCallback(app.getGraphics()->getWindow(), focusCallback); glfwSetWindowFocusCallback(window, focusCallback);
glfwSetMouseButtonCallback(app.getGraphics()->getWindow(), mouseCallback); glfwSetMouseButtonCallback(window, mouseCallback);
// Enable vertical sync (on cards that support it) with parameter 1 - 0 means off // Enable vertical sync (on cards that support it) with parameter 1 - 0 means off
glfwSwapInterval( 0 ); glfwSwapInterval( 0 );
@ -97,20 +151,20 @@ int main( int argc, char *argv[] )
std::stringstream sstream (std::stringstream::in | std::stringstream::out); std::stringstream sstream (std::stringstream::in | std::stringstream::out);
sstream << std::setprecision(1) << std::fixed sstream << std::setprecision(1) << std::fixed
<< tmp[tmp.size()-1] << " - FPS: " << frameCount / (now-showNextFPS + FPSdelay) << " " << 1000 * (now-showNextFPS + FPSdelay)/frameCount << " msec"; << tmp[tmp.size()-1] << " - FPS: " << frameCount / (now-showNextFPS + FPSdelay) << " " << 1000 * (now-showNextFPS + FPSdelay)/frameCount << " msec";
glfwSetWindowTitle(app.getGraphics()->getWindow(), sstream.str().c_str() ); glfwSetWindowTitle(window, sstream.str().c_str() );
showNextFPS = now + FPSdelay; showNextFPS = now + FPSdelay;
frameCount = 0; frameCount = 0;
} }
if (app.isLocked() && app.getIgnoredMouseUpdates() == 0) { if (app.isLocked() && app.getIgnoredMouseUpdates() == 0) {
int stateW = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_W); int stateW = glfwGetKey(window, GLFW_KEY_W);
int stateA = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_A); int stateA = glfwGetKey(window, GLFW_KEY_A);
int stateS = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_S); int stateS = glfwGetKey(window, GLFW_KEY_S);
int stateD = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_D); int stateD = glfwGetKey(window, GLFW_KEY_D);
double xpos, ypos; double xpos, ypos;
glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos); glfwGetCursorPos(window, &xpos, &ypos);
glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2); glfwSetCursorPos(window, app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2);
app.getLevel()->update(now - lastUpdate, app.getLevel()->update(now - lastUpdate,
glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2, glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2,
(float)xpos-app.getGraphics()->getWindowSize().x/2), (float)xpos-app.getGraphics()->getWindowSize().x/2),
@ -130,12 +184,12 @@ int main( int argc, char *argv[] )
// MacOS X will not swap correctly is another FBO is bound: // MacOS X will not swap correctly is another FBO is bound:
glBindFramebuffer( GL_FRAMEBUFFER, 0 ); glBindFramebuffer( GL_FRAMEBUFFER, 0 );
glfwSwapBuffers(app.getGraphics()->getWindow()); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
frameCount++; frameCount++;
} // Check if the window was closed } // Check if the window was closed
while( !glfwWindowShouldClose(app.getGraphics()->getWindow()) ); while( !glfwWindowShouldClose(window) );
glfwTerminate(); glfwTerminate();
exit(0); exit(0);

View File

@ -4,4 +4,5 @@
#include "application.hh" #include "application.hh"
Application app; Application app;
GLFWwindow* window;
#endif #endif