2016-06-05 07:54:41 +00:00
|
|
|
#if 1
|
2016-06-03 05:07:40 +00:00
|
|
|
#include <Dengine/OpenGL.h>
|
|
|
|
#include <Dengine/Common.h>
|
|
|
|
#include <Dengine/Shader.h>
|
2016-06-04 12:42:22 +00:00
|
|
|
#include <Dengine/AssetManager.h>
|
2016-06-03 05:07:40 +00:00
|
|
|
|
2016-06-05 07:54:41 +00:00
|
|
|
#include <Breakout/Game.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 <iostream>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
2016-06-04 06:36:37 +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-05 07:54:41 +00:00
|
|
|
Breakout::Game *game =
|
|
|
|
static_cast<Breakout::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-04 06:36:37 +00:00
|
|
|
|
2016-06-05 07:54:41 +00:00
|
|
|
if (key >= 0 && key < Breakout::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouse_callback(GLFWwindow *window, double xPos, double yPos)
|
|
|
|
{
|
2016-06-03 05:07:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 07:14:08 +00:00
|
|
|
void scroll_callback(GLFWwindow *window, double xOffset, double yOffset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-04 12:42:22 +00:00
|
|
|
GLenum glCheckError_(const char *file, int line)
|
|
|
|
{
|
|
|
|
GLenum errorCode;
|
|
|
|
while ((errorCode = glGetError()) != GL_NO_ERROR)
|
|
|
|
{
|
|
|
|
std::string error;
|
|
|
|
switch (errorCode)
|
|
|
|
{
|
|
|
|
case GL_INVALID_ENUM:
|
|
|
|
error = "INVALID_ENUM";
|
|
|
|
break;
|
|
|
|
case GL_INVALID_VALUE:
|
|
|
|
error = "INVALID_VALUE";
|
|
|
|
break;
|
|
|
|
case GL_INVALID_OPERATION:
|
|
|
|
error = "INVALID_OPERATION";
|
|
|
|
break;
|
|
|
|
case GL_STACK_OVERFLOW:
|
|
|
|
error = "STACK_OVERFLOW";
|
|
|
|
break;
|
|
|
|
case GL_STACK_UNDERFLOW:
|
|
|
|
error = "STACK_UNDERFLOW";
|
|
|
|
break;
|
|
|
|
case GL_OUT_OF_MEMORY:
|
|
|
|
error = "OUT_OF_MEMORY";
|
|
|
|
break;
|
|
|
|
case GL_INVALID_FRAMEBUFFER_OPERATION:
|
|
|
|
error = "INVALID_FRAMEBUFFER_OPERATION";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::cout << error << " | " << file << " (" << line << ")" << std::endl;
|
|
|
|
}
|
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
#define glCheckError() glCheckError_(__FILE__, __LINE__)
|
|
|
|
|
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-05 07:54:41 +00:00
|
|
|
glm::ivec2 windowSize = glm::ivec2(800, 600);
|
|
|
|
|
|
|
|
GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y,
|
|
|
|
"Breakout", nullptr, nullptr);
|
2016-06-03 05:07:40 +00:00
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2016-06-05 07:54:41 +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)
|
|
|
|
{
|
|
|
|
std::cout << "glewInit() failed: Failed to initialise GLEW"
|
|
|
|
<< std::endl;
|
|
|
|
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-06-05 07:54:41 +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);
|
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-05 07:54:41 +00:00
|
|
|
glEnable(GL_CULL_FACE | GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glCullFace(GL_BACK);
|
2016-06-03 05:07:40 +00:00
|
|
|
|
2016-06-05 07:54:41 +00:00
|
|
|
Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y);
|
|
|
|
glfwSetWindowUserPointer(window, static_cast<void *>(&game));
|
|
|
|
game.init();
|
2016-06-03 07:05:28 +00:00
|
|
|
|
2016-06-05 07:54:41 +00:00
|
|
|
/* Initialise shaders */
|
2016-06-04 12:42:22 +00:00
|
|
|
Dengine::AssetManager assetManager;
|
|
|
|
i32 result = 0;
|
2016-06-03 05:07:40 +00:00
|
|
|
|
2016-06-04 12:42:22 +00:00
|
|
|
result = assetManager.loadShaderFiles("data/shaders/default.vert.glsl",
|
|
|
|
"data/shaders/default.frag.glsl",
|
|
|
|
"default");
|
|
|
|
if (result) return result;
|
2016-06-03 05:07:40 +00:00
|
|
|
|
|
|
|
/* Load a texture */
|
2016-06-04 12:42:22 +00:00
|
|
|
result = assetManager.loadTextureImage("data/textures/container.jpg",
|
|
|
|
"container");
|
|
|
|
if (result) return result;
|
2016-06-03 05:07:40 +00:00
|
|
|
|
2016-06-05 07:54:41 +00:00
|
|
|
result = assetManager.loadTextureImage("data/textures/wall.jpg",
|
|
|
|
"wall");
|
|
|
|
if (result) return result;
|
|
|
|
|
2016-06-04 12:42:22 +00:00
|
|
|
Dengine::Texture *containerTex = assetManager.getTexture("container");
|
2016-06-05 07:54:41 +00:00
|
|
|
Dengine::Texture *wallTex = assetManager.getTexture("wall");
|
2016-06-04 12:42:22 +00:00
|
|
|
Dengine::Shader *shader = assetManager.getShader("default");
|
2016-06-03 05:07:40 +00:00
|
|
|
|
2016-06-04 06:36:37 +00:00
|
|
|
f32 deltaTime = 0.0f; // Time between current frame and last frame
|
|
|
|
f32 lastFrame = 0.0f; // Time of last frame
|
2016-06-03 07:05:28 +00:00
|
|
|
|
2016-06-03 05:07:40 +00:00
|
|
|
/* Main game loop */
|
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
{
|
2016-06-04 06:36:37 +00:00
|
|
|
f32 currentFrame = (f32)glfwGetTime();
|
|
|
|
deltaTime = currentFrame - lastFrame;
|
|
|
|
lastFrame = currentFrame;
|
|
|
|
|
2016-06-03 05:07:40 +00:00
|
|
|
/* Check and call events */
|
|
|
|
glfwPollEvents();
|
2016-06-05 07:54:41 +00:00
|
|
|
|
|
|
|
game.processInput(deltaTime);
|
|
|
|
game.update(deltaTime);
|
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-04 12:42:22 +00:00
|
|
|
shader->use();
|
2016-06-05 07:54:41 +00:00
|
|
|
game.render();
|
2016-06-03 05:07:40 +00:00
|
|
|
|
|
|
|
/* Swap the buffers */
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-05 07:54:41 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <Tutorial.cpp>
|
|
|
|
#endif
|