Skip to content

Instantly share code, notes, and snippets.

@jordandee
Last active October 14, 2024 13:12
Show Gist options
  • Save jordandee/94b187bcc51df9528a2f to your computer and use it in GitHub Desktop.
Save jordandee/94b187bcc51df9528a2f to your computer and use it in GitHub Desktop.
Simple SDL2/OpenGL example
// To compile with gcc: (tested on Ubuntu 14.04 64bit):
// g++ sdl2_opengl.cpp -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 WinWidth 1000
#define WinHeight 1000
int main (int ArgCount, char **Args)
{
u32 WindowFlags = SDL_WINDOW_OPENGL;
SDL_Window *Window = SDL_CreateWindow("OpenGL Test", 0, 0, WinWidth, WinHeight, WindowFlags);
assert(Window);
SDL_GLContext Context = SDL_GL_CreateContext(Window);
b32 Running = 1;
b32 FullScreen = 0;
while (Running)
{
SDL_Event Event;
while (SDL_PollEvent(&Event))
{
if (Event.type == SDL_KEYDOWN)
{
switch (Event.key.keysym.sym)
{
case SDLK_ESCAPE:
Running = 0;
break;
case 'f':
FullScreen = !FullScreen;
if (FullScreen)
{
SDL_SetWindowFullscreen(Window, WindowFlags | SDL_WINDOW_FULLSCREEN_DESKTOP);
}
else
{
SDL_SetWindowFullscreen(Window, WindowFlags);
}
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;
}
@SourceCodeDeleted
Copy link

SourceCodeDeleted commented Apr 4, 2020

To compile on linux use
g++ sdl2_opengl.cpp -o myprogram -l SDL2 -lGL
./myprogram

Damn I didn't see the very top... ))

@RedactedProfile
Copy link

Severity	Code	Description	Project	File	Line	Suppression State
Warning	C26812	The enum type 'SDL_bool' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).	Corrode	C:\Users\djdar\source\repos\Corrode\vendor\sdl2\include\SDL_rect.h	102	

:(

@mattearly
Copy link

mattearly commented Aug 17, 2020

fixed on my fork: fullscreen 'f' key and logic, linked SDL2main, booleans for running and fullscreen vars

@nandakoryaaa
Copy link

Hello, does anyone know how to build with -lGL? It's not found, i use -lopengl32 instead. Where does -lGL come from?

@QuinRider
Copy link

Hello, does anyone know how to build with -lGL? It's not found, i use -lopengl32 instead. Where does -lGL come from?

"-lGL" is for linking with OpenGL under Linux, while "-lopengl32" is used under Windows

@weather-tracker
Copy link

if you get a 2019 error you do not have to change it from cmd to window application.
i got it to run after having to look it up on StackOverflow. just add this without the quots after the included "#undef main"

@weather-tracker
Copy link

or you can do this

@mjbmon
Copy link

mjbmon commented Jan 2, 2023

You don't need "#include <GL/gl.h>" because it is already included by "#include <SDL2/SDL_opengl.h>" and it also takes care of the problem with OSX.

Copy link

ghost commented Apr 1, 2023

you didint init SDL, oooof

@VitoVan
Copy link

VitoVan commented Apr 22, 2023

You don't need "#include <GL/gl.h>" because it is already included by "#include <SDL2/SDL_opengl.h>" and it also takes care of the problem with OSX.

and with:

gcc -lSDL2 -framework OpenGL sdl2_opengl.cpp 

@Kitsune64
Copy link

Kitsune64 commented Mar 4, 2024

Thanks it work!

I have commented some lines of code that's seemed unnecessary to put in. Tested like this on Debian 11(Linux) it work.

//#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 1000
#define WinHeight 1000

int main (int ArgCount, char **Args)
{

  u32 WindowFlags = SDL_WINDOW_OPENGL;
  SDL_Window *Window = SDL_CreateWindow("OpenGL Test", 0, 0, WinWidth, WinHeight, WindowFlags);
  //assert(Window);
  //SDL_GLContext Context =
  SDL_GL_CreateContext(Window);
...

@vastina
Copy link

vastina commented Jul 27, 2024

Thanks it works on win11 !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment