Created
November 16, 2014 20:36
-
-
Save anonymous/5316e620c0881f625c59 to your computer and use it in GitHub Desktop.
This file contains 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 <iostream> | |
#include <ctime> | |
#include <SDL.h> | |
#include <OpenGL/gl.h> | |
using namespace std; | |
SDL_Window* win; | |
Args args; | |
typedef struct _Args { | |
bool debug; | |
} Args; | |
typedef struct _Input { | |
bool quit; | |
} Input; | |
int parse_args(int argc, char** argv, Args* outArgs) | |
{ | |
for (int i = 0; i < argc; ++i) | |
{ | |
char* arg = argv[i]; | |
char first = arg[0]; | |
if (first == '-') | |
{ | |
if (arg[1] == 'd') | |
outArgs->debug = true; | |
} | |
} | |
return 0; | |
} | |
Input handle_input() | |
{ | |
SDL_Event event; | |
Input ret; | |
while (SDL_PollEvent(&event) ) | |
{ | |
if (event.type == SDL_QUIT) ret.quit = true; | |
if (event.type == SDL_KEYDOWN) ret.quit = true; | |
} | |
return ret; | |
} | |
void loop() | |
{ | |
// Set up VBO | |
const float vertexPositions[] = { | |
0.75f, 0.75f, 0.0f, 1.0f, | |
0.75f, -0.75f, 0.0f, 1.0f, | |
-0.75f, -0.75f, 0.0f, 1.0f, | |
}; | |
GLuint vbo; | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
// TODO init shaders | |
// Misc setup | |
glViewport(-1, -1, 20, 20); | |
glClearColor(0.0f, 1.0f, 1.0f, 0.0f); | |
while (true) | |
{ | |
// Input | |
Input input = handle_input(); | |
if (input.quit) break; | |
// Clear | |
glLoadIdentity(); | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Render | |
glColor3f(0, 0, 1.0); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glEnableVertexAttribArray(0); | |
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glDisableVertexAttribArray(0); | |
// Commit | |
SDL_GL_SwapWindow(win); | |
SDL_Delay(50); | |
} | |
} | |
void print_info() | |
{ | |
SDL_version version; | |
SDL_VERSION(&version); | |
cout << "SDL version: " << (int)version.major << "." << (int)version.minor << (int)version.patch << endl; | |
SDL_GetVersion(&version); | |
cout << "runtime version: " << (int)version.major << "." << (int)version.minor << (int)version.patch << endl; | |
printf("OpenGL vendor: " "'%s'\n" , glGetString(GL_VENDOR)); | |
printf("OpenGL renderer: " "'%s'\n" , glGetString(GL_RENDERER)); | |
printf("OpenGL version: " "'%s'\n" , glGetString(GL_VERSION)); | |
printf("GLSL version: " "'%s'\n" , glGetString(GL_SHADING_LANGUAGE_VERSION)); | |
} | |
int main ( int argc, char** argv ) | |
{ | |
int ret = parse_args(argc, argv, &args); | |
if (ret) return ret; | |
srand(time(NULL)); | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) | |
{ | |
cerr << "Couldn't init SDL"; | |
return 1; | |
} | |
win = SDL_CreateWindow("SDL2/GL4.3", 0, 0, 200, 200, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); | |
if (win == NULL) | |
{ | |
cerr << "Couldn't set video mode"; | |
return 2; | |
} | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
SDL_GLContext context = SDL_GL_CreateContext(win); | |
if (context == NULL) | |
{ | |
cerr << "Couldn't get a gl context: " << SDL_GetError(); | |
return 3; | |
} | |
print_info(); | |
loop(); | |
SDL_GL_DeleteContext(context); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment