-
-
Save mattearly/11c05912c1a8bedcc0197f046e5b0029 to your computer and use it in GitHub Desktop.
Simple SDL2/OpenGL example
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
// To compile with gcc: (tested on Ubuntu 14.04 64bit): | |
// g++ sdl2_opengl.cpp -lSDL2main -lSDL2 -lGL | |
// To compile with msvc: (tested on Windows 7 64bit) | |
// cl sdl2_opengl.cpp /I C:\sdl2path\include /link C:\path\SDL2.lib C:\path\SDL2main.lib /SUBSYSTEM:CONSOLE /NODEFAULTLIB:libcmtd.lib opengl32.lib | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <assert.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#include <GL/gl.h> | |
typedef int32_t i32; | |
typedef uint32_t u32; | |
// typedef int32_t b32; | |
#define DefaultWinWidth 1000 | |
#define DefaultWinHeight 1000 | |
int main (int ArgCount, char **Args) | |
{ | |
u32 WindowFlags = SDL_WINDOW_OPENGL; | |
i32 WinWidth = DefaultWinWidth; | |
i32 WinHeight = DefaultWinHeight; | |
SDL_Window *Window = SDL_CreateWindow("OpenGL Test", 0, 0, WinWidth, WinHeight, WindowFlags); | |
assert(Window); | |
SDL_GLContext Context = SDL_GL_CreateContext(Window); | |
bool Running = true; | |
bool FullScreen = false; | |
while (Running) | |
{ | |
SDL_Event Event; | |
while (SDL_PollEvent(&Event)) | |
{ | |
if (Event.type == SDL_KEYDOWN) | |
{ | |
switch (Event.key.keysym.sym) | |
{ | |
case SDLK_ESCAPE: | |
Running = false; | |
break; | |
case SDLK_f: | |
FullScreen = !FullScreen; | |
if (FullScreen) | |
{ | |
SDL_SetWindowFullscreen(Window, WindowFlags | SDL_WINDOW_FULLSCREEN_DESKTOP); | |
SDL_GetWindowSize(Window, &WinWidth, &WinHeight); | |
} | |
else | |
{ | |
SDL_SetWindowFullscreen(Window, WindowFlags); | |
SDL_GetWindowSize(Window, &WinWidth, &WinHeight); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
else if (Event.type == SDL_QUIT) | |
{ | |
Running = false; | |
} | |
} | |
glViewport(0, 0, WinWidth, WinHeight); | |
glClearColor(1.f, 0.f, 1.f, 0.f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
SDL_GL_SwapWindow(Window); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment