2016-06-05 07:54:41 +00:00
|
|
|
#if 1
|
2016-06-28 06:00:03 +00:00
|
|
|
#include "Dengine/AssetManager.h"
|
2016-07-26 15:34:26 +00:00
|
|
|
#include "Dengine/Audio.h"
|
2016-07-16 13:27:52 +00:00
|
|
|
#include "Dengine/Common.h"
|
2016-07-09 10:46:04 +00:00
|
|
|
#include "Dengine/Debug.h"
|
2016-07-16 13:27:52 +00:00
|
|
|
#include "Dengine/Math.h"
|
|
|
|
#include "Dengine/OpenGL.h"
|
2016-07-25 12:10:50 +00:00
|
|
|
#include "Dengine/Platform.h"
|
2016-06-03 05:07:40 +00:00
|
|
|
|
2016-06-28 06:00:03 +00:00
|
|
|
#include "WorldTraveller/WorldTraveller.h"
|
2016-06-05 07:54:41 +00:00
|
|
|
|
2016-07-26 15:34:26 +00:00
|
|
|
// TODO(doyle): Temporary
|
|
|
|
struct AudioRenderer;
|
2016-07-24 12:19:25 +00:00
|
|
|
|
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-17 14:40:40 +00:00
|
|
|
GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window));
|
2016-06-05 07:54:41 +00:00
|
|
|
|
2016-06-03 05:07:40 +00:00
|
|
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
|
|
|
{
|
|
|
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
|
|
}
|
2016-06-04 06:36:37 +00:00
|
|
|
|
2016-06-17 14:40:40 +00:00
|
|
|
if (key >= 0 && key < NUM_KEYS)
|
2016-06-04 06:36:37 +00:00
|
|
|
{
|
|
|
|
if (action == GLFW_PRESS)
|
2016-06-05 07:54:41 +00:00
|
|
|
game->keys[key] = TRUE;
|
2016-06-04 06:36:37 +00:00
|
|
|
else if (action == GLFW_RELEASE)
|
2016-06-05 07:54:41 +00:00
|
|
|
game->keys[key] = FALSE;
|
2016-06-04 06:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04 07:14:08 +00:00
|
|
|
|
2016-06-03 05:07:40 +00:00
|
|
|
int main()
|
|
|
|
{
|
2016-07-24 12:19:25 +00:00
|
|
|
/*
|
|
|
|
**************************
|
|
|
|
* INIT APPLICATION WINDOW
|
|
|
|
**************************
|
|
|
|
*/
|
2016-06-03 05:07:40 +00:00
|
|
|
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-07-17 13:45:59 +00:00
|
|
|
i32 windowWidth = 1600;
|
|
|
|
i32 windowHeight = 900;
|
2016-06-05 07:54:41 +00:00
|
|
|
|
2016-06-29 10:44:35 +00:00
|
|
|
GLFWwindow *window =
|
2016-07-17 13:45:59 +00:00
|
|
|
glfwCreateWindow(windowWidth, windowHeight, "Dengine", NULL, NULL);
|
2016-06-03 05:07:40 +00:00
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2016-06-17 14:40:40 +00:00
|
|
|
printf("glfwCreateWindow() failed: Failed to create window\n");
|
2016-06-03 05:07:40 +00:00
|
|
|
glfwTerminate();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
|
2016-07-24 12:19:25 +00:00
|
|
|
/*
|
|
|
|
**************************
|
|
|
|
* INITIALISE OPENGL STATE
|
|
|
|
**************************
|
|
|
|
*/
|
2016-06-03 05:07:40 +00:00
|
|
|
/* Make GLEW use more modern technies for OGL on core profile*/
|
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
if (glewInit() != GLEW_OK)
|
|
|
|
{
|
2016-06-17 14:40:40 +00:00
|
|
|
printf("glewInit() failed: Failed to initialise GLEW\n");
|
2016-06-03 05:07:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-05 07:54:41 +00:00
|
|
|
// 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
|
|
|
|
2016-07-17 13:45:59 +00:00
|
|
|
i32 frameBufferWidth, frameBufferHeight;
|
|
|
|
glfwGetFramebufferSize(window, &frameBufferWidth, &frameBufferHeight);
|
|
|
|
glViewport(0, 0, frameBufferWidth, frameBufferHeight);
|
2016-06-03 05:07:40 +00:00
|
|
|
|
|
|
|
glfwSetKeyCallback(window, key_callback);
|
2016-06-04 06:36:37 +00:00
|
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
2016-06-04 07:14:08 +00:00
|
|
|
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);
|
2016-06-07 13:54:14 +00:00
|
|
|
|
2016-06-05 07:54:41 +00:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glCullFace(GL_BACK);
|
2016-06-03 05:07:40 +00:00
|
|
|
|
2016-07-25 12:10:50 +00:00
|
|
|
/*
|
|
|
|
*******************
|
|
|
|
* INITIALISE GAME
|
|
|
|
*******************
|
|
|
|
*/
|
|
|
|
GameState worldTraveller = {0};
|
|
|
|
worldTraveller_gameInit(&worldTraveller,
|
|
|
|
V2i(frameBufferWidth, frameBufferHeight));
|
|
|
|
#ifdef DENGINE_DEBUG
|
|
|
|
debug_init(&worldTraveller.arena, V2i(windowWidth, windowHeight),
|
|
|
|
worldTraveller.assetManager.font);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller));
|
|
|
|
|
2016-07-24 12:19:25 +00:00
|
|
|
/*
|
|
|
|
*******************
|
|
|
|
* INITIALISE AUDIO
|
|
|
|
*******************
|
|
|
|
*/
|
2016-07-26 15:34:26 +00:00
|
|
|
AudioRenderer audioRenderer = {0};
|
|
|
|
audio_rendererInit(&audioRenderer);
|
2016-07-24 12:19:25 +00:00
|
|
|
|
2016-07-26 15:34:26 +00:00
|
|
|
/* Load audio assets */
|
|
|
|
char *audioPath = "data/audio/Nobuo Uematsu - Battle 1.ogg";
|
|
|
|
asset_loadVorbis(&worldTraveller.assetManager, &worldTraveller.arena,
|
|
|
|
audioPath, audiolist_battle);
|
|
|
|
audioPath = "data/audio/Yuki Kajiura - Swordland.ogg";
|
|
|
|
asset_loadVorbis(&worldTraveller.assetManager, &worldTraveller.arena,
|
|
|
|
audioPath, audiolist_overworld);
|
|
|
|
AudioVorbis *audio =
|
|
|
|
asset_getVorbis(&worldTraveller.assetManager, audiolist_battle);
|
2016-07-24 12:19:25 +00:00
|
|
|
|
2016-07-26 15:34:26 +00:00
|
|
|
audio_streamVorbis(&audioRenderer, audio);
|
2016-07-25 12:10:50 +00:00
|
|
|
|
2016-07-24 12:19:25 +00:00
|
|
|
/*
|
|
|
|
*******************
|
2016-07-25 12:10:50 +00:00
|
|
|
* GAME LOOP
|
2016-07-24 12:19:25 +00:00
|
|
|
*******************
|
|
|
|
*/
|
2016-06-17 14:40:40 +00:00
|
|
|
f32 startTime = CAST(f32)(glfwGetTime());
|
2016-06-16 17:00:11 +00:00
|
|
|
f32 secondsElapsed = 0.0f; // Time between current frame and last frame
|
|
|
|
|
2016-06-17 06:32:59 +00:00
|
|
|
#if 0
|
2016-06-16 17:00:11 +00:00
|
|
|
// TODO(doyle): Get actual monitor refresh rate
|
2016-06-17 06:32:59 +00:00
|
|
|
i32 monitorRefreshHz = 60;
|
2016-06-25 11:23:15 +00:00
|
|
|
f32 targetSecondsPerFrame = 1.0f / CAST(f32)(monitorRefreshHz);
|
2016-06-17 06:32:59 +00:00
|
|
|
#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
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
{
|
|
|
|
/* Check and call events */
|
|
|
|
glfwPollEvents();
|
2016-06-05 07:54:41 +00:00
|
|
|
|
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
|
|
|
|
2016-06-17 14:40:40 +00:00
|
|
|
worldTraveller_gameUpdateAndRender(&worldTraveller, secondsElapsed);
|
2016-07-24 12:19:25 +00:00
|
|
|
GL_CHECK_ERROR();
|
2016-07-26 15:34:26 +00:00
|
|
|
audio_updateAndPlay(&audioRenderer);
|
2016-06-03 05:07:40 +00:00
|
|
|
|
|
|
|
/* Swap the buffers */
|
|
|
|
glfwSwapBuffers(window);
|
2016-06-16 17:00:11 +00:00
|
|
|
|
2016-06-18 09:12:09 +00:00
|
|
|
f32 endTime = CAST(f32)glfwGetTime();
|
2016-06-16 17:00:11 +00:00
|
|
|
secondsElapsed = endTime - startTime;
|
|
|
|
|
2016-06-17 06:32:59 +00:00
|
|
|
#if 0
|
2016-06-16 17:00:11 +00:00
|
|
|
// TODO(doyle): Busy waiting, should sleep
|
|
|
|
while (secondsElapsed < targetSecondsPerFrame)
|
|
|
|
{
|
2016-06-25 11:23:15 +00:00
|
|
|
endTime = CAST(f32)(glfwGetTime());
|
2016-06-16 17:00:11 +00:00
|
|
|
secondsElapsed = endTime - startTime;
|
|
|
|
}
|
2016-06-17 06:32:59 +00:00
|
|
|
#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;
|
2016-06-17 14:40:40 +00:00
|
|
|
|
|
|
|
char textBuffer[256];
|
2016-07-16 13:27:52 +00:00
|
|
|
snprintf(textBuffer, ARRAY_COUNT(textBuffer),
|
|
|
|
"Dengine | %f ms/f | %f fps", msPerFrame, framesPerSecond);
|
2016-06-17 14:40:40 +00:00
|
|
|
|
|
|
|
glfwSetWindowTitle(window, textBuffer);
|
2016-06-16 17:00:11 +00:00
|
|
|
titleUpdateFrequencyInSeconds = 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime = endTime;
|
2016-06-03 05:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-05 07:54:41 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <Tutorial.cpp>
|
|
|
|
#endif
|