Complete port over to C, replace glm math lib
This commit is contained in:
+158
@@ -0,0 +1,158 @@
|
||||
#if 1
|
||||
#include <Dengine/AssetManager.h>
|
||||
#include <Dengine/Renderer.h>
|
||||
#include <Dengine/Math.h>
|
||||
|
||||
#include <WorldTraveller/WorldTraveller.h>
|
||||
|
||||
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode)
|
||||
{
|
||||
GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window));
|
||||
|
||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||
{
|
||||
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||
}
|
||||
|
||||
if (key >= 0 && key < NUM_KEYS)
|
||||
{
|
||||
if (action == GLFW_PRESS)
|
||||
game->keys[key] = TRUE;
|
||||
else if (action == GLFW_RELEASE)
|
||||
game->keys[key] = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow *window, double xPos, double yPos) {}
|
||||
|
||||
void scroll_callback(GLFWwindow *window, double xOffset, double yOffset) {}
|
||||
|
||||
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);
|
||||
|
||||
v2 windowSize = V2(1280.0f, 720.0f);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(
|
||||
CAST(i32) windowSize.x, CAST(i32) windowSize.y, "Dengine", NULL, NULL);
|
||||
|
||||
if (!window)
|
||||
{
|
||||
printf("glfwCreateWindow() failed: Failed to create window\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
/* Make GLEW use more modern technies for OGL on core profile*/
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK)
|
||||
{
|
||||
printf("glewInit() failed: Failed to initialise GLEW\n");
|
||||
return -1;
|
||||
}
|
||||
// NOTE(doyle): glewInit() bug that sets the gl error flag after init
|
||||
// regardless of success. Catch it once by calling glGetError
|
||||
glGetError();
|
||||
|
||||
i32 frameBufferSizeX;
|
||||
i32 frameBufferSizeY;
|
||||
glfwGetFramebufferSize(window, &frameBufferSizeX, &frameBufferSizeY);
|
||||
const v2 frameBufferSize =
|
||||
V2(CAST(f32) frameBufferSizeX, CAST(f32) frameBufferSizeY);
|
||||
|
||||
glViewport(0, 0, CAST(i32)frameBufferSize.x, CAST(i32)frameBufferSize.y);
|
||||
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glCullFace(GL_BACK);
|
||||
|
||||
GameState worldTraveller = {0};
|
||||
worldTraveller.state = state_active;
|
||||
worldTraveller.width = CAST(i32)frameBufferSize.x;
|
||||
worldTraveller.height = CAST(i32)frameBufferSize.y;
|
||||
|
||||
worldTraveller_gameInit(&worldTraveller);
|
||||
|
||||
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));
|
||||
|
||||
f32 startTime = CAST(f32)(glfwGetTime());
|
||||
f32 secondsElapsed = 0.0f; // Time between current frame and last frame
|
||||
|
||||
#if 0
|
||||
// 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
|
||||
|
||||
/* Main game loop */
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
/* Check and call events */
|
||||
glfwPollEvents();
|
||||
|
||||
/* Rendering commands here*/
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
worldTraveller_gameUpdateAndRender(&worldTraveller, secondsElapsed);
|
||||
glCheckError();
|
||||
|
||||
/* Swap the buffers */
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
f32 endTime = CAST(f32)glfwGetTime();
|
||||
secondsElapsed = endTime - startTime;
|
||||
|
||||
#if 0
|
||||
// TODO(doyle): Busy waiting, should sleep
|
||||
while (secondsElapsed < targetSecondsPerFrame)
|
||||
{
|
||||
endTime = static_cast<f32>(glfwGetTime());
|
||||
secondsElapsed = endTime - startTime;
|
||||
}
|
||||
#endif
|
||||
|
||||
LOCAL_PERSIST f32 titleUpdateFrequencyInSeconds = 0.5f;
|
||||
|
||||
titleUpdateFrequencyInSeconds -= secondsElapsed;
|
||||
if (titleUpdateFrequencyInSeconds <= 0)
|
||||
{
|
||||
f32 msPerFrame = secondsElapsed * 1000.0f;
|
||||
f32 framesPerSecond = 1.0f / secondsElapsed;
|
||||
|
||||
char textBuffer[256];
|
||||
snprintf(textBuffer, ARRAY_COUNT(textBuffer), "Dengine | %f ms/f | %f fps", msPerFrame,
|
||||
framesPerSecond);
|
||||
|
||||
glfwSetWindowTitle(window, textBuffer);
|
||||
titleUpdateFrequencyInSeconds = 0.5f;
|
||||
}
|
||||
|
||||
startTime = endTime;
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
#include <Tutorial.cpp>
|
||||
#endif
|
||||
Reference in New Issue
Block a user