Currently we have the following code in the 'main.cpp' from this folder: sdl3-sample/src/
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <cmath>
struct AppContext {
SDL_Window* window;
SDL_Renderer* renderer;
SDL_bool app_quit = SDL_FALSE;
};
int SDL_Fail(){
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError());
return -1;
}
int SDL_AppInit(void** appstate, int argc, char* argv[]) {
// init the library, here we make a window so we only need the Video capabilities.
if (SDL_Init(SDL_INIT_VIDEO)){
return SDL_Fail();
}
// create a window
SDL_Window* window = SDL_CreateWindow("Window", 352, 430, SDL_WINDOW_RESIZABLE);
if (!window){
return SDL_Fail();
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, NULL, 0);
if (!renderer){
return SDL_Fail();
}
// print some information about the window
SDL_ShowWindow(window);
{
int width, height, bbwidth, bbheight;
SDL_GetWindowSize(window, &width, &height);
SDL_GetWindowSizeInPixels(window, &bbwidth, &bbheight);
SDL_Log("Window size: %ix%i", width, height);
SDL_Log("Backbuffer size: %ix%i", bbwidth, bbheight);
if (width != bbwidth){
SDL_Log("This is a highdpi environment.");
}
}
// set up the application data
*appstate = new AppContext{
window,
renderer,
};
SDL_Log("Application started successfully!");
return 0;
}
int SDL_AppEvent(void *appstate, const SDL_Event* event) {
auto* app = (AppContext*)appstate;
if (event->type == SDL_EVENT_QUIT) {
app->app_quit = SDL_TRUE;
}
return 0;
}
int SDL_AppIterate(void *appstate) {
auto* app = (AppContext*)appstate;
// draw a color
auto time = SDL_GetTicks() / 1000.f;
auto red = (std::sin(time) + 1) / 2.0 * 255;
auto green = (std::sin(time / 2) + 1) / 2.0 * 255;
auto blue = (std::sin(time) * 2 + 1) / 2.0 * 255;
SDL_SetRenderDrawColor(app->renderer, red, green, blue, SDL_ALPHA_OPAQUE);
SDL_RenderClear(app->renderer);
SDL_RenderPresent(app->renderer);
return app->app_quit;
}
void SDL_AppQuit(void* appstate) {
auto* app = (AppContext*)appstate;
if (app) {
SDL_DestroyRenderer(app->renderer);
SDL_DestroyWindow(app->window);
delete app;
}
SDL_Quit();
SDL_Log("Application quit successfully!");
}
Let's change it by step by step to render with OpenGL ES 2.0. Include the "SDL_opengles2.h" header file:
Old code:
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <cmath>
New code:
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_opengles2.h>
#include <cmath>
Delete 'SDL_Renderer' and add 'SDL_GLContext' in the 'AppContext' structure:
Old code:
struct AppContext
{
SDL_Window *window;
SDL_Renderer* renderer;
SDL_bool app_quit = SDL_FALSE;
};
New code:
struct AppContext
{
SDL_Window *window;
SDL_GLContext glcontext;
SDL_bool app_quit = SDL_FALSE;
};
Add the 'SDL_WINDOW_OPENGL' flag to the 'SDL_CreateWindow' function inside of the 'SDL_AppInit' function:
Old code:
int SDL_AppInit(void **appstate, int argc, char *argv[])
{
// init the library, here we make a window so we only need the Video capabilities.
if (SDL_Init(SDL_INIT_VIDEO))
{
return SDL_Fail();
}
// create a window
SDL_Window *window = SDL_CreateWindow("Window", 352, 430, SDL_WINDOW_RESIZABLE);
New code:
int SDL_AppInit(void **appstate, int argc, char *argv[])
{
// init the library, here we make a window so we only need the Video capabilities.
if (SDL_Init(SDL_INIT_VIDEO))
{
return SDL_Fail();
}
// create a window
SDL_Window *window = SDL_CreateWindow("Window", 352, 430,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
Remove the "SDL_Renderer" creation and add the "SDL_GLContext" creation:
Old code:
SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL, 0);
if (!renderer)
{
return SDL_Fail();
}
New code:
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
if (!glcontext)
{
return SDL_Fail();
}
Note. 'glcontext' is not a pointer.
Replace 'renderer' with 'glcontext' in 'AppContext':
Old code:
// set up the application data
*appstate = new AppContext {
window,
renderer,
};
New code:
// set up the application data
*appstate = new AppContext {
window,
glcontext,
};
Remove the "* 255" in the "SDL_AppIterate()" function where the colors are calculated:
Old code:
int SDL_AppIterate(void *appstate)
{
auto *app = (AppContext *)appstate;
// draw a color
auto time = SDL_GetTicks() / 1000.f;
auto red = (std::sin(time) + 1) / 2.0 * 255;
auto green = (std::sin(time / 2) + 1) / 2.0 * 255;
auto blue = (std::sin(time) * 2 + 1) / 2.0 * 255;
New code:
int SDL_AppIterate(void *appstate)
{
auto *app = (AppContext *)appstate;
// draw a color
auto time = SDL_GetTicks() / 1000.f;
auto red = (std::sin(time) + 1) / 2.0;
auto green = (std::sin(time / 2) + 1) / 2.0;
auto blue = (std::sin(time) * 2 + 1) / 2.0;
Replace SDL Renderer functions with OpenGL functions:
Old code:
SDL_SetRenderDrawColor(app->renderer, red, green, blue, SDL_ALPHA_OPAQUE);
SDL_RenderClear(app->renderer);
SDL_RenderPresent(app->renderer);
New code:
glClearColor(red, green, blue, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw your stuff here
SDL_GL_SwapWindow(app->window);
Replace 'SDL_DestroyRenderer' with 'SDL_GL_DeleteContext' in 'SDL_AppQuit':
Old code:
void SDL_AppQuit(void *appstate)
{
auto *app = (AppContext *)appstate;
if (app)
{
SDL_DestroyRenderer(app->renderer);
SDL_DestroyWindow(app->window);
delete app;
}
SDL_Quit();
SDL_Log("Application quit successfully!");
}
New code:
void SDL_AppQuit(void *appstate)
{
auto *app = (AppContext *)appstate;
if (app)
{
SDL_GL_DeleteContext(app->glcontext);
SDL_DestroyWindow(app->window);
delete app;
}
SDL_Quit();
SDL_Log("Application quit successfully!");
}
Follow the next tutorials to run the example above on Android and WebAssembly:
Android:
WebAssembly: