Dengine/src/dengine.cpp

163 lines
4.2 KiB
C++
Raw Normal View History

#if 1
#include <Dengine/AssetManager.h>
2016-06-16 14:14:58 +00:00
#include <Dengine/Common.h>
#include <Dengine/OpenGL.h>
2016-06-09 05:49:03 +00:00
#include <Dengine/Renderer.h>
2016-06-16 14:14:58 +00:00
#include <Dengine/Shader.h>
2016-06-03 05:07:40 +00:00
2016-06-16 14:14:58 +00:00
#include <WorldTraveller/WorldTraveller.h>
2016-06-03 05:07:40 +00:00
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <cstdint>
#include <fstream>
2016-06-16 14:14:58 +00:00
#include <iostream>
2016-06-16 17:00:11 +00:00
#include <sstream>
2016-06-03 05:07:40 +00:00
#include <string>
2016-06-16 14:14:58 +00:00
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode)
2016-06-03 05:07:40 +00:00
{
2016-06-16 14:14:58 +00:00
WorldTraveller::Game *game =
static_cast<WorldTraveller::Game *>(glfwGetWindowUserPointer(window));
2016-06-03 05:07:40 +00:00
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
2016-06-16 14:14:58 +00:00
if (key >= 0 && key < WorldTraveller::NUM_KEYS)
{
if (action == GLFW_PRESS)
game->keys[key] = TRUE;
else if (action == GLFW_RELEASE)
game->keys[key] = FALSE;
}
}
2016-06-16 14:14:58 +00:00
void mouse_callback(GLFWwindow *window, double xPos, double yPos) {}
2016-06-03 05:07:40 +00:00
2016-06-16 14:14:58 +00:00
void scroll_callback(GLFWwindow *window, double xOffset, double yOffset) {}
2016-06-03 05:07:40 +00:00
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
2016-06-09 05:49:03 +00:00
glm::ivec2 windowSize = glm::ivec2(1280, 720);
2016-06-16 17:00:11 +00:00
GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, "Dengine", nullptr, nullptr);
2016-06-03 05:07:40 +00:00
if (!window)
{
2016-06-16 14:14:58 +00:00
std::cout << "glfwCreateWindow() failed: Failed to create window" << std::endl;
2016-06-03 05:07:40 +00:00
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
/* Make GLEW use more modern technies for OGL on core profile*/
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
2016-06-16 14:14:58 +00:00
std::cout << "glewInit() failed: Failed to initialise GLEW" << std::endl;
2016-06-03 05:07:40 +00:00
return -1;
}
// NOTE(doyle): glewInit() bug that sets the gl error flag after init
// regardless of success. Catch it once by calling glGetError
glGetError();
2016-06-03 05:07:40 +00:00
glm::ivec2 frameBufferSize;
glfwGetFramebufferSize(window, &frameBufferSize.x, &frameBufferSize.y);
glViewport(0, 0, frameBufferSize.x, frameBufferSize.y);
2016-06-03 05:07:40 +00:00
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
2016-06-09 05:49:03 +00:00
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK);
2016-06-03 05:07:40 +00:00
2016-06-16 14:14:58 +00:00
WorldTraveller::Game game = WorldTraveller::Game(frameBufferSize.x, frameBufferSize.y);
2016-06-09 05:49:03 +00:00
game.init();
glfwSetWindowUserPointer(window, static_cast<void *>(&game));
2016-06-03 05:07:40 +00:00
f32 startTime = static_cast<f32>(glfwGetTime());
2016-06-16 17:00:11 +00:00
f32 secondsElapsed = 0.0f; // Time between current frame and last frame
#if 0
2016-06-16 17:00:11 +00:00
// TODO(doyle): Get actual monitor refresh rate
i32 monitorRefreshHz = 60;
f32 targetSecondsPerFrame = 1.0f / static_cast<f32>(monitorRefreshHz);
#else
// TODO(doyle): http://gafferongames.com/game-physics/fix-your-timestep/
// NOTE(doyle): Prevent glfwSwapBuffer until a vertical retrace has
// occurred, i.e. limit framerate to monitor refresh rate
glfwSwapInterval(1);
#endif
2016-06-03 07:05:28 +00:00
2016-06-03 05:07:40 +00:00
/* Main game loop */
while (!glfwWindowShouldClose(window))
{
2016-06-03 05:07:40 +00:00
/* Check and call events */
glfwPollEvents();
2016-06-16 17:00:11 +00:00
game.update(secondsElapsed);
2016-06-03 05:07:40 +00:00
/* Rendering commands here*/
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
2016-06-03 07:05:28 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2016-06-03 05:07:40 +00:00
game.render();
2016-06-09 05:49:03 +00:00
glCheckError();
2016-06-03 05:07:40 +00:00
/* Swap the buffers */
glfwSwapBuffers(window);
2016-06-16 17:00:11 +00:00
f32 endTime = static_cast<f32>(glfwGetTime());
2016-06-16 17:00:11 +00:00
secondsElapsed = endTime - startTime;
#if 0
2016-06-16 17:00:11 +00:00
// TODO(doyle): Busy waiting, should sleep
while (secondsElapsed < targetSecondsPerFrame)
{
endTime = static_cast<f32>(glfwGetTime());
secondsElapsed = endTime - startTime;
}
#endif
2016-06-16 17:00:11 +00:00
LOCAL_PERSIST f32 titleUpdateFrequencyInSeconds = 0.5f;
titleUpdateFrequencyInSeconds -= secondsElapsed;
if (titleUpdateFrequencyInSeconds <= 0)
{
f32 msPerFrame = secondsElapsed * 1000.0f;
f32 framesPerSecond = 1.0f / secondsElapsed;
std::stringstream ss;
ss << "Dengine | " << msPerFrame << " ms/f | " << framesPerSecond << " fps";
glfwSetWindowTitle(window, ss.str().c_str());
titleUpdateFrequencyInSeconds = 0.5f;
}
startTime = endTime;
2016-06-03 05:07:40 +00:00
}
glfwTerminate();
return 0;
}
#else
#include <Tutorial.cpp>
#endif