Advertisement
SrinjoySS01

Window.cpp

Dec 5th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.89 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include "Game.h"
  5. using namespace std;
  6.  
  7. GLFWwindow* glfwWindow;
  8. Window window;
  9.  
  10. float positions[6] = {
  11.     -0.5F, -0.5F,
  12.      0.0F,  0.5F,
  13.      0.5F, -0.5F
  14. };
  15.  
  16. static unsigned int CompileShader(unsigned int type, const string& source){
  17.     unsigned int id = glCreateShader(type);
  18.     const char* src = source.c_str();
  19.     glShaderSource(id, 1, &src, nullptr);
  20.     glCompileShader(id);
  21.    
  22.     int result;
  23.     glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  24.     if (result == GL_FALSE) {
  25.         int length;
  26.         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  27.         char message[length];
  28.         glGetShaderInfoLog(id, length, &length, message);
  29.         cout << "Failed to compile" << (type == GL_VERTEX_SHADER ? " vertex" : " fragment") << " shader!" << endl;
  30.         cout << message << endl;
  31.         glDeleteShader(id);
  32.         return 0;
  33.     }
  34.    
  35.     return id;
  36. }
  37.  
  38. static unsigned int CreateShader(const string& vertexShader, const string& fragmentShader) {
  39.     unsigned int program = glCreateProgram();
  40.     unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
  41.     unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
  42.    
  43.     glAttachShader(program, vs);
  44.     glAttachShader(program, fs);
  45.     glLinkProgram(program);
  46.     glValidateProgram(program);
  47.    
  48.     glDeleteShader(vs);
  49.     glDeleteShader(fs);
  50.    
  51.     return program;
  52. }
  53.  
  54. void createWindow(Window newWindow){
  55.     window = newWindow;
  56.     run();
  57. }
  58.  
  59. void run() {
  60.     init();
  61.     cout << "Hello " << glGetString(GL_VERSION) << " !" << endl;
  62.     loop();
  63.     glfwDestroyWindow(glfwWindow);
  64.     glfwTerminate();
  65. }
  66.  
  67. int init() {
  68.     /* Initialize the library */
  69.     if (!glfwInit()) {
  70.         cout << "Unable to initialize GLFW" << endl;
  71.         return -1;
  72.     }
  73.     glfwDefaultWindowHints();
  74.     glfwWindowHint(GLFW_RESIZABLE, window.resizable ? GLFW_TRUE : GLFW_FALSE);
  75.     glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
  76.     glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_FALSE);
  77.     glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
  78.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  79.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  80.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  81.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  82.     /* Create a windowed mode window and its OpenGL context */
  83.     glfwWindow = glfwCreateWindow(
  84.             window.fullScreen ? 1366 : window.width/*640*/,
  85.             window.fullScreen ? 768 : window.height/*480*/,
  86.             window.title,
  87.             window.fullScreen ? glfwGetPrimaryMonitor() : NULL,
  88.             NULL
  89.         );
  90.     if (!glfwWindow){
  91.         glfwTerminate();
  92.         cout << "Failed to create the GLFW window" << endl;
  93.         return -1;
  94.     }
  95.     // Make the OpenGL context current
  96.     glfwMakeContextCurrent(glfwWindow);
  97.     if(glewInit() != GLEW_OK)
  98.         cout << "Error" << endl;
  99.     // Enable v-sync
  100.     glfwSwapInterval(1);
  101.     // Make the window visible
  102.     glfwShowWindow(glfwWindow);
  103.     return 0;
  104. }
  105.  
  106. void loop() {
  107.     unsigned int buffer;
  108.     glGenBuffers(1, &buffer);
  109.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  110.     glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
  111.    
  112.     glEnableVertexAttribArray(0);
  113.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  114.    
  115.     string vertexShader =
  116.         "#version 330\n"
  117.         "layout(location=0) in vec4 position;\n"
  118.         "void main() {\n"
  119.         "   gl_Position = position;\n"
  120.         "}";
  121.     string fragmentShader =
  122.         "#version 330\n"
  123.         "layout(location=0) out vec4 color;\n"
  124.         "void main() {\n"
  125.         "   color = vec4(1.0, 0.0, 0.0, 1.0);\n"
  126.         "}";
  127.    
  128.     unsigned int shader = CreateShader(vertexShader, fragmentShader);
  129.     glUseProgram(shader);
  130.    
  131.     double previousTime = glfwGetTime();
  132.     int frameCount = 0;
  133.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  134.     while (!glfwWindowShouldClose(glfwWindow)) {
  135.         double currentTime = glfwGetTime();
  136.         frameCount++;
  137.         if (currentTime - previousTime >= 1.0) {
  138.             string title = window.title;
  139.             title = title.append(" | ");
  140.             glfwSetWindowTitle(glfwWindow, title.append(to_string(frameCount)).c_str());
  141.             frameCount = 0;
  142.             previousTime = currentTime;
  143.         }
  144.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
  145.        
  146.         glDrawArrays(GL_TRIANGLES, 0, 3);
  147. //        glBegin(GL_TRIANGLES);
  148. //        glVertex2f(-0.5F, -0.5F);
  149. //        glVertex2f(0.0F, 0.5F);
  150. //        glVertex2f(0.5F, -0.5F);
  151. //        glEnd();
  152.        
  153.         glfwSwapBuffers(glfwWindow); // swap the color buffers
  154.         // Poll for window events. The key callback above will only be
  155.         // invoked during this call.
  156.         glfwPollEvents();
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement