Last active
January 18, 2016 17:53
-
-
Save rioki/631074886d7f49c0dd18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <SDL.h> | |
#include <SDL_image.h> | |
#include <GL/glew.h> | |
#include <glt.h> | |
#define TITLE "OpenGL 4.0 Scaffold" | |
#define WIDTH 1024 | |
#define HEIGHT 768 | |
int running = 1; | |
SDL_Window* window = NULL; | |
SDL_GLContext glcontext = NULL; | |
int open_window() | |
{ | |
int r = SDL_Init(SDL_INIT_VIDEO); | |
if (r != 0) | |
{ | |
printf("Failed to init SDL."); | |
return -1; | |
} | |
window = SDL_CreateWindow(TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
WIDTH, HEIGHT, SDL_WINDOW_OPENGL); | |
if (window == NULL) | |
{ | |
printf("Failed to open window."); | |
return -2; | |
} | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
glcontext = SDL_GL_CreateContext(window); | |
if (glcontext == NULL) | |
{ | |
SDL_DestroyWindow(window); | |
printf("Failed to create OpenGL context."); | |
return -3; | |
} | |
glewExperimental = GL_TRUE; | |
GLenum err = glewInit(); | |
if (GLEW_OK != err) | |
{ | |
SDL_GL_DeleteContext(window); | |
SDL_DestroyWindow(window); | |
printf("Failed to initialize GLEW."); | |
return -4; | |
} | |
return 0; | |
} | |
int build_scene() | |
{ | |
} | |
void pump_events() | |
{ | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) | |
{ | |
switch (event.type) | |
{ | |
case SDL_QUIT: | |
running = 0; | |
break; | |
default: | |
// stfu | |
break; | |
} | |
} | |
} | |
void draw_scene() | |
{ | |
int w, h; | |
SDL_GetWindowSize(window, &w, &h); | |
glViewport(0, 0, w, h); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
// draw stuff here | |
SDL_GL_SwapWindow(window); | |
} | |
void close_window() | |
{ | |
if (glcontext != NULL) | |
{ | |
SDL_GL_DeleteContext(glcontext); | |
glcontext = NULL; | |
} | |
if (window != NULL) | |
{ | |
SDL_DestroyWindow(window); | |
window = NULL; | |
} | |
free(window); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
int r = open_window(); | |
if (r < 0) | |
{ | |
return r; | |
} | |
r = build_scene(); | |
if (r < 0) | |
{ | |
close_window(); | |
return r; | |
} | |
while (running) | |
{ | |
pump_events(); | |
draw_scene(); | |
} | |
close_window(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment