synchronzie laptop and pc

This commit is contained in:
Krzysztof Rudnicki 2022-09-07 18:34:50 +02:00
parent 1860ab5848
commit 0f293adb96
4 changed files with 33 additions and 28 deletions

View File

@ -8,54 +8,57 @@
#include "constants.hpp" #include "constants.hpp"
#include "misc.hpp" #include "misc.hpp"
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion) { void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion)
{
// first argument tells us what option to configure // first argument tells us what option to configure
// second is to what we set the value of this option // second is to what we set the value of this option
// see: https://www.glfw.org/docs/latest/window.html#window_hints // see: https://www.glfw.org/docs/latest/window.html#window_hints
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFWMajorVersion); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFWMajorVersion);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFWMinorVersion); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFWMinorVersion);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// we set GLFW to 3.3 CORE // we set GLFW to 3.3 CORE
// core profile gives us access to smaller subset of OGL without backwards compatible features // 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 // if we are on Mac OS X we need this for our code to work
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif #endif
} }
void instantiateGLFWwindow() { void instantiateGLFWwindow()
{
// Initialize GLFW // Initialize GLFW
glfwInit(); glfwInit();
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION); configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
} }
const GLFWwindow* createWindowObject() { GLFWwindow *createWindowObject()
// First two arguments are width and height {
// First two arguments are width and height
// Third is the name of the window // Third is the name of the window
// We ignore last two // We ignore last two
const GLFWwindow* window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL); GLFWwindow *window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL);
return window; return window;
} }
int initializeGLAD() int initializeGLAD()
{ {
// we load address of OGL OS-specific function pointers // we load address of OGL OS-specific function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{ {
print("Failed to initialize GLAD"); print("Failed to initialize GLAD");
return -1; return -1;
} }
return 0; return 0;
} }
// resizes viewport when user resizes window // resizes viewport when user resizes window
void framebuffer_size_callback(GLFWwindow* window, int width, int height) void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{ {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
void viewPort(GLFWwindow* window) void viewPort(GLFWwindow *window)
{ {
// We tell OGL size of rendering window // We tell OGL size of rendering window
// First two define left corner of window // First two define left corner of window
@ -64,17 +67,17 @@ void viewPort(GLFWwindow* window)
glViewport(0, 0, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT); glViewport(0, 0, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT);
// processed coordinates are between -1 and 1 so here we map: // 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) // (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH) and (0, constants::MAIN_WINDOW_HEIGHT)
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// we call framebuffer_size_callback on every window resize // we call framebuffer_size_callback on every window resize
} }
GLFWwindow* prepareForRender() GLFWwindow *prepareForRender()
{ {
instantiateGLFWwindow(); instantiateGLFWwindow();
GLFWwindow* window = createWindowObject(); GLFWwindow *window = createWindowObject();
// function returns GLFWWindow object // function returns GLFWWindow object
if (window == NULL) if (window == NULL)
{ {
print("Failed to create GLFW window"); print("Failed to create GLFW window");
@ -83,8 +86,9 @@ GLFWwindow* prepareForRender()
} }
// we make context of this window main context of current thread // we make context of this window main context of current thread
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
if(initializeGLAD() == -1) return NULL; if (initializeGLAD() == -1)
return NULL;
viewPort(window); viewPort(window);
return window; return window;
} }

View File

@ -4,10 +4,10 @@
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion); void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
void instantiateGLFWwindow(); void instantiateGLFWwindow();
const GLFWwindow* createWindowObject(); GLFWwindow *createWindowObject();
int initializeGLAD(); int initializeGLAD();
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void viewPort(GLFWwindow* window); void viewPort(GLFWwindow *window);
GLFWwindow* prepareForRender(); GLFWwindow *prepareForRender();
#endif #endif

Binary file not shown.

View File

@ -16,8 +16,9 @@
int main() int main()
{ {
// changed in glfwWindowShouldClose // changed in glfwWindowShouldClose
GLFWwindow* window = prepareForRender(); GLFWwindow *window = prepareForRender();
if(window == NULL) return -1; if (window == NULL)
return -1;
renderLoop(window); renderLoop(window);
// clean GLFW resources // clean GLFW resources