Last active
December 21, 2024 23:38
-
-
Save jyaif/a816b14d2873865f7c0a3ee2d0d94537 to your computer and use it in GitHub Desktop.
Simple SDL2 program that uses OpenGL
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
// Compile with: | |
// clang++ main.cpp -lSDL2 -lGL | |
#include <cstdlib> | |
#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 WinWidth 640 | |
#define WinHeight 480 | |
void fatal(const char* msg) { | |
printf("%s\n", msg); | |
exit(EXIT_FAILURE); | |
} | |
int main (int ArgCount, char **Args) | |
{ | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
fatal("SDL_Init"); | |
}; | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); | |
u32 WindowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN; | |
SDL_Window *Window = SDL_CreateWindow("OpenGL Test", 0, 0, WinWidth, WinHeight, WindowFlags); | |
assert(Window); | |
if (!Window) { | |
fatal("Window"); | |
} | |
SDL_GLContext Context = SDL_GL_CreateContext(Window); | |
if (!Context) { | |
fatal("Context"); | |
} | |
b32 Running = 1; | |
b32 FullScreen = 0; | |
while (Running) | |
{ | |
SDL_Event Event; | |
while (SDL_PollEvent(&Event)) | |
{ | |
printf("PollEvent\n"); | |
if (Event.type == SDL_KEYDOWN) | |
{ | |
switch (Event.key.keysym.sym) | |
{ | |
case SDLK_ESCAPE: | |
Running = 0; | |
break; | |
default: | |
break; | |
} | |
} | |
else if (Event.type == SDL_QUIT) | |
{ | |
Running = 0; | |
} | |
} | |
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