mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-26 11:07:20 +01:00
feat: create window
This commit is contained in:
parent
5fbbe90d5c
commit
5373d14909
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -73,5 +73,6 @@
|
|||||||
"streambuf": "cpp",
|
"streambuf": "cpp",
|
||||||
"cinttypes": "cpp",
|
"cinttypes": "cpp",
|
||||||
"typeinfo": "cpp"
|
"typeinfo": "cpp"
|
||||||
}
|
},
|
||||||
|
"C_Cpp.errorSquiggles": "Disabled"
|
||||||
}
|
}
|
||||||
4
.vscode/tasks.json
vendored
4
.vscode/tasks.json
vendored
@ -9,7 +9,9 @@
|
|||||||
"-std=c++17",
|
"-std=c++17",
|
||||||
"-I${workspaceFolder}/dependencies/include",
|
"-I${workspaceFolder}/dependencies/include",
|
||||||
"-L${workspaceFolder}/dependencies/library",
|
"-L${workspaceFolder}/dependencies/library",
|
||||||
"-Wall", "${workspaceFolder}/Engine/engine/*.cpp",
|
"-Wall",
|
||||||
|
"${workspaceFolder}/Engine/engine/*.hpp",
|
||||||
|
"${workspaceFolder}/Engine/engine/*.cpp",
|
||||||
"${workspaceFolder}/Engine/engine/glad.c",
|
"${workspaceFolder}/Engine/engine/glad.c",
|
||||||
"-o",
|
"-o",
|
||||||
"${workspaceFolder}/Engine/engine/match", "-lglut", "-lglfw", "-lGLU", "-lGL", "-lm",
|
"${workspaceFolder}/Engine/engine/match", "-lglut", "-lglfw", "-lGLU", "-lGL", "-lm",
|
||||||
|
|||||||
14
Engine/engine/constants.hpp
Normal file
14
Engine/engine/constants.hpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef CONSTANTS_HPP
|
||||||
|
#include <iostream>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
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
|
||||||
Binary file not shown.
@ -1,3 +1,4 @@
|
|||||||
|
#ifndef MAIN_CPP
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
@ -5,29 +6,98 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
int main()
|
#include "constants.hpp"
|
||||||
{
|
|
||||||
glfwInit();
|
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_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
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
|
||||||
|
|
||||||
|
// if we are on Mac OS X we need this for our code to work
|
||||||
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
#ifdef __APPLE__
|
||||||
if (window == NULL)
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
{
|
#endif
|
||||||
std::cout << "Failed to create GLFW window" << std::endl;
|
|
||||||
glfwTerminate();
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
|
|
||||||
|
void instantiateGLFWwindow() {
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
// Initialize GLFW
|
||||||
{
|
glfwInit();
|
||||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
configureGLFW();
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
// 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
Loading…
Reference in New Issue
Block a user