opengl

OpenGL - SDL
Log | Files | Refs

window.c (1560B)


      1 #include "window.h"
      2 #include "objload.h"
      3 #include "triangle.h"
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <GL/glew.h>
      7 #include <GLFW/glfw3.h>
      8 #define SDL_DISABLE_IMMINTRIN_H
      9 #include <SDL2/SDL.h>
     10 
     11 #define BG_RED       0.7
     12 #define BG_GREEN     0.0
     13 #define BG_BLUE      1.0
     14 #define BG_ALPHA     1.0
     15 
     16 SDL_Window *window;
     17 SDL_GLContext ctx;
     18 SDL_Renderer *renderer;
     19 
     20 
     21 void create_window()
     22 {
     23     setup_sdl();
     24     setup_gl();
     25 }
     26 
     27 void setup_sdl()
     28 {
     29     SDL_Init(SDL_INIT_VIDEO);
     30     window = SDL_CreateWindow("sedona", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
     31                               WINDOW_W, WINDOW_H, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
     32 
     33     if(!window) exit(fprintf(stderr, "%s\n", SDL_GetError()));
     34 }
     35 
     36 void setup_gl()
     37 {
     38     SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
     39     SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
     40     SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
     41 
     42     ctx = SDL_GL_CreateContext(window);
     43 
     44     if(!ctx) exit(fprintf(stderr, "Could not create GL context\n"));
     45 
     46     SDL_GL_SetSwapInterval(1);
     47 
     48 #ifndef __APPLE__
     49     glewExperimental = GL_TRUE;
     50     glewInit();
     51 #endif
     52     glViewport(0, 0, WINDOW_W, WINDOW_H);
     53 }
     54 
     55 void color_window()
     56 {
     57     glClearColor(BG_RED, BG_GREEN, BG_BLUE, BG_ALPHA);
     58     glClear(GL_COLOR_BUFFER_BIT);
     59 }
     60 
     61 
     62 void draw()
     63 {
     64     SDL_GL_SwapWindow(window);
     65 }
     66 
     67 void cleanup()
     68 {
     69     SDL_DestroyWindow(window);
     70     SDL_Quit();
     71     glDeleteVertexArrays(1, &VAO);
     72     glDeleteBuffers(1, &VBO);
     73     glDeleteProgram(shaderProgram);
     74 }