diff --git a/.vscode/settings.json b/.vscode/settings.json index 1c58fb6..78ee1fa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -73,5 +73,6 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp" - } + }, + "C_Cpp.errorSquiggles": "Disabled" } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index fbed5a7..388b933 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -9,7 +9,9 @@ "-std=c++17", "-I${workspaceFolder}/dependencies/include", "-L${workspaceFolder}/dependencies/library", - "-Wall", "${workspaceFolder}/Engine/engine/*.cpp", + "-Wall", + "${workspaceFolder}/Engine/engine/*.hpp", + "${workspaceFolder}/Engine/engine/*.cpp", "${workspaceFolder}/Engine/engine/glad.c", "-o", "${workspaceFolder}/Engine/engine/match", "-lglut", "-lglfw", "-lGLU", "-lGL", "-lm", diff --git a/Engine/engine/constants.hpp b/Engine/engine/constants.hpp new file mode 100644 index 0000000..1371b4b --- /dev/null +++ b/Engine/engine/constants.hpp @@ -0,0 +1,14 @@ +#ifndef CONSTANTS_HPP +#include +#include + +namespace constants +{ + // best practice is to use inline constexpr std::string_view but glfwCreateWindow takes only char* as input + inline const char* MAIN_WINDOW_NAME { "Match" }; + inline constexpr int MAIN_WINDOW_WIDTH { 800 }; + inline constexpr int MAIN_WINDOW_HEIGHT { 600 }; + +} + +#endif \ No newline at end of file diff --git a/Engine/engine/match b/Engine/engine/match index c8985fa..0141014 100755 Binary files a/Engine/engine/match and b/Engine/engine/match differ diff --git a/Engine/engine/match.cpp b/Engine/engine/match.cpp index 85578c0..9556f78 100644 --- a/Engine/engine/match.cpp +++ b/Engine/engine/match.cpp @@ -1,3 +1,4 @@ +#ifndef MAIN_CPP #include #include @@ -5,29 +6,98 @@ #include #include #include -int main() -{ - glfwInit(); +#include "constants.hpp" + +void configureGLFW() { + // first argument tells us what option to configure + // second is to what we set the value of this option + // see: https://www.glfw.org/docs/latest/window.html#window_hints glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - + // we set GLFW to 3.3 CORE + // core profile gives us access to smaller subset of OGL without backwards compatible features - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); -if (window == NULL) -{ - std::cout << "Failed to create GLFW window" << std::endl; - glfwTerminate(); - return -1; + // if we are on Mac OS X we need this for our code to work + #ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + #endif } -glfwMakeContextCurrent(window); +void instantiateGLFWwindow() { + // Initialize GLFW + glfwInit(); + configureGLFW(); +} -if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) +GLFWwindow* createWindowObject() { + // First two arguments are width and height + // Third is the name of the window + // We ignore last two + GLFWwindow* window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL); + return window; +} + +int initializeGLAD() { - std::cout << "Failed to initialize GLAD" << std::endl; - return -1; -} + // we load address of OGL OS-specific function pointers + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) + { + std::cout << "Failed to initialize GLAD" << std::endl; + return -1; + } return 0; -} \ No newline at end of file +} + +// resizes viewport when user resizes window +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + glViewport(0, 0, width, height); +} + +void viewPort(GLFWwindow* window) +{ + // We tell OGL size of rendering window + // First two define left corner of window + // 3th and 4th width and height of rendering window + // we could set them to be smaller than window dimension, ogl rendering will be then displayed in smaller window + glViewport(0, 0, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT); + // processed coordinates are between -1 and 1 so here we map: + // (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH) and (0, constants::MAIN_WINDOW_HEIGHT) + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + // we call framebuffer_size_callback on every window resize +} + +void renderLoop(GLFWwindow* window) +{ + while(!glfwWindowShouldClose(window)) + { + glfwSwapBuffers(window); + glfwPollEvents(); + } +} + +int main() +{ + instantiateGLFWwindow(); + + GLFWwindow* window = createWindowObject(); + // function returns GLFWWindow object + + if (window == NULL) + { + std::cout << "Failed to create GLFW window" << std::endl; + glfwTerminate(); + return -1; + } + // we make context of this window main context of current thread + glfwMakeContextCurrent(window); + + if(initializeGLAD() == -1) return -1; + viewPort(window); + renderLoop(window); + + return 0; +} + +#endif \ No newline at end of file