Last active
October 10, 2024 10:24
-
-
Save 8Observer8/2ad1e5d3be8ca628ee5dc95334678007 to your computer and use it in GitHub Desktop.
Set a background color using OpenGL, SDL3, and C++
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
# mkdir dist\win | |
# cmake -G "MinGW Makefiles" -S . -B dist/win -DSDL3_DIR=E:/libs/sdl3-desktop-prefix/lib/cmake/SDL3 | |
# cmake -G "MinGW Makefiles" -S . -B dist/win -DSDL3_DIR=E:/libs/sdl3-libs/SDL3-devel-3.1.3-mingw/SDL3-3.1.3/x86_64-w64-mingw32/lib/cmake/SDL3 | |
# cd dist/win | |
# mingw32-make | |
# app | |
cmake_minimum_required(VERSION 3.20) | |
project(background_color_opengl_sdl3_cpp) | |
add_executable(app) # WIN32 - hide the console like this add_executable(app WIN32) | |
target_include_directories(app PRIVATE E:/libs/glad-2.0.6/include) | |
target_include_directories(app PRIVATE E:/libs/glm-1.0.1-light) | |
target_sources(app | |
PRIVATE | |
E:/libs/glad-2.0.6/src/glad.c | |
main.cpp | |
) | |
# This code copies the "SDL3.dll" file to the build directory | |
add_custom_command(TARGET app POST_BUILD | |
COMMAND ${CMAKE_COMMAND} -E copy_if_different | |
$<TARGET_RUNTIME_DLLS:app> $<TARGET_FILE_DIR:app> | |
) | |
find_package(SDL3) | |
target_link_libraries(app PRIVATE SDL3::SDL3) | |
target_compile_definitions(app PRIVATE SDL_MAIN_USE_CALLBACKS) | |
target_link_options(app PRIVATE -static-libgcc -static-libstdc++) |
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
#include <SDL3/SDL.h> | |
#include <SDL3/SDL_main.h> | |
#include <glad/glad.h> | |
#include <iostream> | |
struct AppContext | |
{ | |
SDL_Window *window; | |
SDL_GLContext glContext; | |
SDL_AppResult appQuit = SDL_APP_CONTINUE; | |
}; | |
SDL_AppResult SDL_Fail() | |
{ | |
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError()); | |
return SDL_APP_FAILURE; | |
} | |
SDL_AppResult 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)) | |
{ | |
std::cout << "hello" << std::endl; | |
return SDL_Fail(); | |
} | |
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); // Enable MULTISAMPLE | |
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2); // can be 2, 4, 8 or 16 | |
// Create a window | |
SDL_Window *window = SDL_CreateWindow("SDL3, OpenGL 2.1, C++", 350, 350, | |
SDL_WINDOW_OPENGL); // | SDL_WINDOW_RESIZABLE | |
if (!window) | |
{ | |
return SDL_Fail(); | |
} | |
SDL_GLContext glContext = SDL_GL_CreateContext(window); | |
if (!glContext) | |
{ | |
return SDL_Fail(); | |
} | |
if (!gladLoadGL()) | |
{ | |
std::cout << "Failed to load OpenGL functions" << std::endl; | |
return SDL_APP_FAILURE; | |
} | |
glClearColor(0.2f, 0.3f, 0.5f, 1.f); | |
SDL_ShowWindow(window); | |
// Set up the application data | |
*appstate = new AppContext { | |
window, | |
glContext, | |
}; | |
return SDL_APP_CONTINUE; | |
} | |
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | |
// SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event) // for the old build of SDL3 | |
{ | |
auto *app = (AppContext *)appstate; | |
switch (event->type) | |
{ | |
case SDL_EVENT_QUIT: | |
{ | |
app->appQuit = SDL_APP_SUCCESS; | |
break; | |
} | |
default: | |
{ | |
break; | |
} | |
} | |
return SDL_APP_CONTINUE; | |
} | |
SDL_AppResult SDL_AppIterate(void *appstate) | |
{ | |
auto *app = (AppContext *)appstate; | |
glClear(GL_COLOR_BUFFER_BIT); | |
SDL_GL_SwapWindow(app->window); | |
return app->appQuit; | |
} | |
void SDL_AppQuit(void *appstate, SDL_AppResult result) | |
// void SDL_AppQuit(void *appstate) // for the old build of SDL3 | |
{ | |
auto *app = (AppContext *)appstate; | |
if (app) | |
{ | |
SDL_GL_DestroyContext(app->glContext); | |
SDL_DestroyWindow(app->window); | |
delete app; | |
} | |
SDL_Quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example doesn't work with
SDL 3.1.3 Preview
but it works with SDL3 from 9/24/2024 build. Details: https://discord.com/channels/405784877305298944/1062483238695739522/1293869985357107221