Advertisement
RedstoneHair

window.h

Jun 15th, 2022
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #ifndef WINDOW_H
  2. #define WINDOW_H
  3.  
  4. #include <GLFW/glfw3.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include "colors.h"
  8.  
  9. // DECLARE
  10. static void error_callback(int error, const char* description){ fputs(description, stderr); }
  11. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){ if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { glfwSetWindowShouldClose(window, GL_TRUE); } }
  12.  
  13. // Window info
  14. static int WindowWidth = 1000, WindowHeight = 1000;
  15. GLFWwindow* window; // declare the window
  16.  
  17. // INIT
  18. static int InitGLFW(){
  19.     if (!glfwInit()) { return -1; }
  20.     return 1;
  21. }
  22.  
  23. // Window Settings, such as resizable... and so on...
  24. class WindowSettings{
  25. public:
  26.     int width;
  27.     int height;
  28.     bool resizable;
  29.     bool focused;
  30.     bool floating;
  31.     bool bordered;
  32.     bool hidden;
  33.     char* title;
  34.     rgbaColor backgroundColor;
  35.     WindowSettings(char* engrave, int WindowWidth = 512, int WindowHeight = 512, rgbaColor BGC = COLOR_BLACK){
  36.         title = engrave;
  37.         width = WindowWidth;
  38.         height = WindowHeight;
  39.         resizable = true;
  40.         focused = true;
  41.         floating = false;
  42.         bordered = true;
  43.         hidden = false;
  44.         backgroundColor = BGC;
  45.     }
  46. };
  47.  
  48. static int InitWindow(WindowSettings Ws) {
  49.     // Setup window
  50.     WindowWidth = Ws.width; WindowHeight = Ws.height;
  51.    
  52.     // Set all hints
  53.     if(Ws.resizable){ glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); } else { glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); }
  54.     if(Ws.focused){ glfwWindowHint(  GLFW_FOCUSED,   GL_TRUE); } else { glfwWindowHint(GLFW_FOCUSED,   GL_FALSE); }
  55.     if(Ws.floating){ glfwWindowHint( GLFW_FLOATING,  GL_TRUE); } else { glfwWindowHint(GLFW_FLOATING,  GL_FALSE); }
  56.     if(Ws.bordered){ glfwWindowHint( GLFW_DECORATED, GL_TRUE); } else { glfwWindowHint(GLFW_DECORATED, GL_FALSE); }
  57.     if(!Ws.hidden){ glfwWindowHint(  GLFW_VISIBLE,   GL_TRUE); } else { glfwWindowHint(GLFW_VISIBLE,   GL_FALSE); }
  58.  
  59.    
  60.     window = glfwCreateWindow(WindowWidth, WindowHeight, Ws.title, NULL, NULL);
  61.    
  62.     if (!window){ glfwTerminate(); exit(EXIT_FAILURE); }
  63.  
  64.     // Setup callback
  65.     glfwSetErrorCallback(error_callback);
  66.     glfwSetKeyCallback(window, key_callback);
  67.  
  68.     // Add the window to the context
  69.     glfwMakeContextCurrent(window);
  70.  
  71.     // Add the clear color
  72.     glClearColor(Ws.backgroundColor.GetR(), Ws.backgroundColor.GetG(), Ws.backgroundColor.GetB(), Ws.backgroundColor.GetA());
  73.     return 1;
  74. }
  75.  
  76. static void TerminateWindow() { glfwDestroyWindow(window); }
  77. static void TerminateGLFW() { glfwTerminate(); exit(EXIT_SUCCESS); }
  78.  
  79. static void CoordinateWindow(){
  80.     glfwGetFramebufferSize(window, &WindowWidth, &WindowHeight);
  81.  
  82.     glViewport(0, 0, WindowWidth, WindowHeight);
  83.  
  84.     glMatrixMode(GL_PROJECTION);
  85.     glLoadIdentity();
  86.     glOrtho(0, WindowWidth, WindowHeight, 0, -1, 1);
  87.     glMatrixMode(GL_MODELVIEW);
  88.     glLoadIdentity();
  89. }
  90.  
  91. static void UpdateWindow(){
  92.     glfwSwapBuffers(window);
  93.     glfwPollEvents();
  94. }
  95.  
  96. static void ClearWindow(){
  97.     glClear(GL_COLOR_BUFFER_BIT);
  98. }
  99.  
  100. static void ClearColor(rgbaColor color){
  101.     glClearColor(color.GetR(), color.GetG(), color.GetB(), color.GetA());
  102.     ClearWindow();
  103. }
  104.  
  105.  
  106. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement