#if 1 #include #include #include #include #include #include #include #include #include #include #include #include void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode) { Breakout::Game *game = static_cast(glfwGetWindowUserPointer(window)); if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { glfwSetWindowShouldClose(window, GL_TRUE); } if (key >= 0 && key < Breakout::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) { } 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__) 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); glm::ivec2 windowSize = glm::ivec2(800, 600); GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, "Breakout", nullptr, nullptr); if (!window) { std::cout << "glfwCreateWindow() failed: Failed to create window" << std::endl; 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; } // NOTE(doyle): glewInit() bug that sets the gl error flag after init // regardless of success. Catch it once by calling glGetError glGetError(); glm::ivec2 frameBufferSize; glfwGetFramebufferSize(window, &frameBufferSize.x, &frameBufferSize.y); glViewport(0, 0, frameBufferSize.x, frameBufferSize.y); glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glEnable(GL_CULL_FACE | GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glCullFace(GL_BACK); Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y); glfwSetWindowUserPointer(window, static_cast(&game)); game.init(); /* Initialise shaders */ Dengine::AssetManager assetManager; i32 result = 0; result = assetManager.loadShaderFiles("data/shaders/default.vert.glsl", "data/shaders/default.frag.glsl", "default"); if (result) return result; /* Load a texture */ result = assetManager.loadTextureImage("data/textures/container.jpg", "container"); if (result) return result; result = assetManager.loadTextureImage("data/textures/wall.jpg", "wall"); if (result) return result; Dengine::Texture *containerTex = assetManager.getTexture("container"); Dengine::Texture *wallTex = assetManager.getTexture("wall"); Dengine::Shader *shader = assetManager.getShader("default"); f32 deltaTime = 0.0f; // Time between current frame and last frame f32 lastFrame = 0.0f; // Time of last frame /* Main game loop */ while (!glfwWindowShouldClose(window)) { f32 currentFrame = (f32)glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; /* Check and call events */ glfwPollEvents(); game.processInput(deltaTime); game.update(deltaTime); /* Rendering commands here*/ glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader->use(); game.render(); /* Swap the buffers */ glfwSwapBuffers(window); } glfwTerminate(); return 0; } #else #include #endif