-
Notifications
You must be signed in to change notification settings - Fork 0
Move sdl concext to backend #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -77,7 +77,7 @@ namespace { | |||||||||||||||||||||
| void showDriverInUse(const Context &ctx) { | ||||||||||||||||||||||
| ctx.mLogger(LogSeverity::Message, "Driver in use:"); | ||||||||||||||||||||||
| SDL_RendererInfo info; | ||||||||||||||||||||||
| SDL_GetRendererInfo(ctx.mSDLContext.mRenderer, &info); | ||||||||||||||||||||||
| SDL_GetRendererInfo(ctx.mSDLContext->mRenderer, &info); | ||||||||||||||||||||||
| printDriverInfo(info); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -178,10 +178,16 @@ ret_code Renderer::releaseRenderer(Context &ctx) { | |||||||||||||||||||||
| delete ctx.mDefaultFont; | ||||||||||||||||||||||
| ctx.mDefaultFont = nullptr; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| IMG_Quit(); | ||||||||||||||||||||||
| SDL_DestroyRenderer(ctx.mSDLContext.mRenderer); | ||||||||||||||||||||||
| ctx.mSDLContext.mRenderer = nullptr; | ||||||||||||||||||||||
| if (ctx.mSDLContext == nullptr) { | ||||||||||||||||||||||
| SDL_Quit(); | ||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (ctx.mSDLContext->mRenderer != nullptr) { | ||||||||||||||||||||||
| SDL_DestroyRenderer(ctx.mSDLContext->mRenderer); | ||||||||||||||||||||||
| ctx.mSDLContext->mRenderer = nullptr; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| SDL_Quit(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
|
|
@@ -214,7 +220,7 @@ ret_code Renderer::drawText(Context &ctx, const char *string, Font *font, const | |||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SDL_Texture *messageTexture = SDL_CreateTextureFromSurface(ctx.mSDLContext.mRenderer, surfaceMessage); | ||||||||||||||||||||||
| SDL_Texture *messageTexture = SDL_CreateTextureFromSurface(ctx.mSDLContext->mRenderer, surfaceMessage); | ||||||||||||||||||||||
| if (messageTexture == nullptr) { | ||||||||||||||||||||||
| const std::string msg = "Cannot create texture: " + std::string(SDL_GetError()) + "."; | ||||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, msg.c_str()); | ||||||||||||||||||||||
|
|
@@ -244,7 +250,7 @@ ret_code Renderer::drawText(Context &ctx, const char *string, Font *font, const | |||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SDL_RenderCopy(ctx.mSDLContext.mRenderer, messageTexture, NULL, &Message_rect); | ||||||||||||||||||||||
| SDL_RenderCopy(ctx.mSDLContext->mRenderer, messageTexture, nullptr, &Message_rect); | ||||||||||||||||||||||
| SDL_FreeSurface(surfaceMessage); | ||||||||||||||||||||||
| SDL_DestroyTexture(messageTexture); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -256,8 +262,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int | |||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Not initialzed."); | ||||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (ctx.mSDLContext.mWindow != nullptr ) { | ||||||||||||||||||||||
| ctx.mSDLContext = SDLContext::create(); | ||||||||||||||||||||||
| if (ctx.mSDLContext->mWindow != nullptr) { | ||||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Already created."); | ||||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -277,8 +283,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int | |||||||||||||||||||||
| title = "TinyUI Window"; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ctx.mSDLContext.mWindow = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); | ||||||||||||||||||||||
| if (ctx.mSDLContext.mWindow == nullptr) { | ||||||||||||||||||||||
| ctx.mSDLContext->mWindow = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); | ||||||||||||||||||||||
| if (ctx.mSDLContext->mWindow == nullptr) { | ||||||||||||||||||||||
| const std::string msg = "Error while SDL_CreateWindow: " + std::string(SDL_GetError()) + "."; | ||||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, msg.c_str()); | ||||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
|
|
@@ -290,8 +296,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int | |||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ctx.mSDLContext.mRenderer = SDL_CreateRenderer(ctx.mSDLContext.mWindow, driverIndex, SDL_RENDERER_ACCELERATED); | ||||||||||||||||||||||
| if (nullptr == ctx.mSDLContext.mRenderer) { | ||||||||||||||||||||||
| ctx.mSDLContext->mRenderer = SDL_CreateRenderer(ctx.mSDLContext->mWindow, driverIndex, SDL_RENDERER_ACCELERATED); | ||||||||||||||||||||||
| if (nullptr == ctx.mSDLContext->mRenderer) { | ||||||||||||||||||||||
| const std::string msg = "Error while SDL_CreateRenderer: " + std::string(SDL_GetError()) + "."; | ||||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, msg.c_str()); | ||||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
|
|
@@ -303,8 +309,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| showDriverInUse(ctx); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ctx.mSDLContext.mSurface = SDL_GetWindowSurface(ctx.mSDLContext.mWindow); | ||||||||||||||||||||||
| if (ctx.mSDLContext.mSurface == nullptr) { | ||||||||||||||||||||||
| ctx.mSDLContext->mSurface = SDL_GetWindowSurface(ctx.mSDLContext->mWindow); | ||||||||||||||||||||||
| if (ctx.mSDLContext->mSurface == nullptr) { | ||||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Surface pointer from window is nullptr."); | ||||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -325,13 +331,13 @@ ret_code Renderer::initScreen(Context &ctx, SDL_Window *window, SDL_Renderer *re | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| TTF_Init(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ctx.mSDLContext.mRenderer = renderer; | ||||||||||||||||||||||
| ctx.mSDLContext.mWindow = window; | ||||||||||||||||||||||
| ctx.mSDLContext->mRenderer = renderer; | ||||||||||||||||||||||
| ctx.mSDLContext->mWindow = window; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Comment on lines
+334
to
336
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cd /tmp && find . -name "sdl2_renderer.cpp" -o -name "sdl2_renderer.h" 2>/dev/null | head -20Repository: kimkulling/tiny_ui Length of output: 44 🏁 Script executed: find . -name "sdl2_renderer.cpp" -o -name "sdl2_renderer.h" 2>/dev/nullRepository: kimkulling/tiny_ui Length of output: 125 🏁 Script executed: find . -type f -name "*sdl2*" 2>/dev/null | head -20Repository: kimkulling/tiny_ui Length of output: 189 🏁 Script executed: git ls-files | grep -i sdl2Repository: kimkulling/tiny_ui Length of output: 181 🏁 Script executed: git ls-files | grep -E "\.(cpp|h|hpp)$" | head -20Repository: kimkulling/tiny_ui Length of output: 306 🏁 Script executed: git ls-files | grep -E "src/backends" | head -20Repository: kimkulling/tiny_ui Length of output: 181 🏁 Script executed: ls -la src/backends/ 2>/dev/null || echo "Directory not found"Repository: kimkulling/tiny_ui Length of output: 443 🏁 Script executed: git ls-files | head -30Repository: kimkulling/tiny_ui Length of output: 566 🏁 Script executed: rg -l "initScreen" --type cpp --type hRepository: kimkulling/tiny_ui Length of output: 166 🏁 Script executed: rg "initScreen" -A 5 -B 2 --type cpp --type h | head -100Repository: kimkulling/tiny_ui Length of output: 3559 🏁 Script executed: test -f "src/backends/sdl2_renderer.cpp" && echo "File exists" || echo "File does not exist"Repository: kimkulling/tiny_ui Length of output: 73 🏁 Script executed: find . -type f \( -name "*.cpp" -o -name "*.h" \) 2>/dev/null | sortRepository: kimkulling/tiny_ui Length of output: 328 🏁 Script executed: ls -laRepository: kimkulling/tiny_ui Length of output: 1118 🏁 Script executed: pwd && ls -la src/ 2>/dev/null | head -20Repository: kimkulling/tiny_ui Length of output: 561 🏁 Script executed: find . -path "*backends*" -name "*.cpp" -o -path "*backends*" -name "*.h" | head -20Repository: kimkulling/tiny_ui Length of output: 189 🏁 Script executed: find . -name "*sdl2_renderer*" -type fRepository: kimkulling/tiny_ui Length of output: 125 🏁 Script executed: rg "initScreen" -C 20 | head -150Repository: kimkulling/tiny_ui Length of output: 7254 🏁 Script executed: rg "mSDLContext->mRenderer" -B 5 -A 2Repository: kimkulling/tiny_ui Length of output: 10343 🏁 Script executed: cat "src/backends/sdl2_renderer.cpp" 2>/dev/null | head -50Repository: kimkulling/tiny_ui Length of output: 1869 🏁 Script executed: ls -la src/ 2>/dev/null || ls -laRepository: kimkulling/tiny_ui Length of output: 542 🏁 Script executed: git ls-files --cached 2>/dev/null | head -30Repository: kimkulling/tiny_ui Length of output: 566 🏁 Script executed: git status 2>&1 | head -10
git ls-files 2>/dev/null | wc -lRepository: kimkulling/tiny_ui Length of output: 265 🏁 Script executed: find . -type f -name "*.cpp" 2>/dev/null | head -20Repository: kimkulling/tiny_ui Length of output: 181 🏁 Script executed: ls -la && find . -maxdepth 3 -type f \( -name "*.cpp" -o -name "*.h" \) 2>/dev/null | head -30Repository: kimkulling/tiny_ui Length of output: 1385 🌐 Web query:
💡 Result: A solid “null-pointer safe”
`#include` <SDL.h>
`#include` <memory>
`#include` <stdexcept>
`#include` <string>
struct SdlError : std::runtime_error {
using std::runtime_error::runtime_error;
};
using Window = std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)>;
using Renderer = std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)>;
struct Screen {
Window window {nullptr, SDL_DestroyWindow};
Renderer renderer {nullptr, SDL_DestroyRenderer};
};
inline void sdl_ok(int rc, const char* what) {
if (rc < 0) throw SdlError(std::string(what) + ": " + SDL_GetError());
}
inline Screen initScreen(const char* title, int w, int h) {
sdl_ok(SDL_Init(SDL_INIT_VIDEO), "SDL_Init");
Screen s;
s.window.reset(SDL_CreateWindow(title,
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
w, h, SDL_WINDOW_SHOWN));
if (!s.window) throw SdlError(std::string("SDL_CreateWindow: ") + SDL_GetError());
s.renderer.reset(SDL_CreateRenderer(s.window.get(), -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
if (!s.renderer) throw SdlError(std::string("SDL_CreateRenderer: ") + SDL_GetError());
return s; // move/NRVO
}
// Usage:
// int main() {
// try {
// auto screen = initScreen("My Game", 1280, 720);
// ...
// } catch (const std::exception& e) {
// SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s\n", e.what());
// }
// SDL_Quit(); // or wrap SDL_Init/SDL_Quit in its own RAII guard
// }Why this is “null-safe”: you never dereference Sources: SDL “returns NULL on failure; call SDL_GetError()” guidance [1]; RAII with [1] SDL Wiki: Citations:
🏁 Script executed: rg "mSDLContext" -B 10 -A 5 --type cppRepository: kimkulling/tiny_ui Length of output: 23230 🏁 Script executed: rg "ctx.mSDLContext->mRenderer" -B 20 -A 5Repository: kimkulling/tiny_ui Length of output: 26191 🏁 Script executed: rg "ret_code initScreen" -A 30 --type cppRepository: kimkulling/tiny_ui Length of output: 3344 🏁 Script executed: rg "initScreen\s*\(" -B 2 -A 15 --type cpp | head -200Repository: kimkulling/tiny_ui Length of output: 7173 🏁 Script executed: ast-grep --pattern 'fn initScreen($$$) { $$$ }' 2>/dev/null || rg "void initScreen|ret_code initScreen" -A 20Repository: kimkulling/tiny_ui Length of output: 2836 🏁 Script executed: rg "SDLContext" --type cpp --type h | head -40Repository: kimkulling/tiny_ui Length of output: 3201 🏁 Script executed: ls -la src/backends/Repository: kimkulling/tiny_ui Length of output: 443 External-window At lines 328-330, the function directly dereferences Proposed fix- ctx.mSDLContext->mRenderer = renderer;
- ctx.mSDLContext->mWindow = window;
+ if (ctx.mSDLContext == nullptr) {
+ ctx.mSDLContext = SDLContext::create();
+ }
+ if (ctx.mSDLContext == nullptr) {
+ return ErrorCode;
+ }
+ ctx.mSDLContext->mRenderer = renderer;
+ ctx.mSDLContext->mWindow = window;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| showDriverInUse(ctx); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ctx.mSDLContext.mSurface = SDL_GetWindowSurface(ctx.mSDLContext.mWindow); | ||||||||||||||||||||||
| ctx.mSDLContext.mOwner = false; | ||||||||||||||||||||||
| ctx.mSDLContext->mSurface = SDL_GetWindowSurface(ctx.mSDLContext->mWindow); | ||||||||||||||||||||||
| ctx.mSDLContext->mOwner = false; | ||||||||||||||||||||||
| ctx.mCreated = true; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
|
|
@@ -343,8 +349,8 @@ ret_code Renderer::releaseScreen(Context &ctx) { | |||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SDL_DestroyWindow(ctx.mSDLContext.mWindow); | ||||||||||||||||||||||
| ctx.mSDLContext.mWindow = nullptr; | ||||||||||||||||||||||
| SDL_DestroyWindow(ctx.mSDLContext->mWindow); | ||||||||||||||||||||||
| ctx.mSDLContext->mWindow = nullptr; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -356,19 +362,19 @@ ret_code Renderer::beginRender(Context &ctx, Color4 bg, SDL_Texture *renderTarge | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const SDL_Color sdl_bg = getSDLColor(bg); | ||||||||||||||||||||||
| SDL_SetRenderDrawColor(ctx.mSDLContext.mRenderer, sdl_bg.r, sdl_bg.g, sdl_bg.b, sdl_bg.a); | ||||||||||||||||||||||
| SDL_RenderClear(ctx.mSDLContext.mRenderer); | ||||||||||||||||||||||
| SDL_SetRenderDrawColor(ctx.mSDLContext->mRenderer, sdl_bg.r, sdl_bg.g, sdl_bg.b, sdl_bg.a); | ||||||||||||||||||||||
| SDL_RenderClear(ctx.mSDLContext->mRenderer); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ret_code Renderer::drawRect(Context &ctx, int32_t x, int32_t y, int32_t w, int32_t h, bool filled, Color4 fg) { | ||||||||||||||||||||||
| SDL_Rect r = {x, y, w, h}; | ||||||||||||||||||||||
| SDL_SetRenderDrawColor(ctx.mSDLContext.mRenderer, fg.r, fg.g, fg.b, fg.a); | ||||||||||||||||||||||
| SDL_SetRenderDrawColor(ctx.mSDLContext->mRenderer, fg.r, fg.g, fg.b, fg.a); | ||||||||||||||||||||||
| if (filled) { | ||||||||||||||||||||||
| SDL_RenderFillRect(ctx.mSDLContext.mRenderer, &r); | ||||||||||||||||||||||
| SDL_RenderFillRect(ctx.mSDLContext->mRenderer, &r); | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| SDL_RenderDrawRect(ctx.mSDLContext.mRenderer, &r); | ||||||||||||||||||||||
| SDL_RenderDrawRect(ctx.mSDLContext->mRenderer, &r); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
|
|
@@ -380,28 +386,28 @@ ret_code Renderer::drawImage(Context &ctx, int32_t x, int32_t y, int32_t w, int3 | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SDL_Rect imageRect = {x, y, w, h}; | ||||||||||||||||||||||
| SDL_Texture *tex = SDL_CreateTextureFromSurface(ctx.mSDLContext.mRenderer, image->mSurfaceImpl->mSurface); | ||||||||||||||||||||||
| SDL_RenderCopy(ctx.mSDLContext.mRenderer, tex, nullptr, &imageRect); | ||||||||||||||||||||||
| SDL_Texture *tex = SDL_CreateTextureFromSurface(ctx.mSDLContext->mRenderer, image->mSurfaceImpl->mSurface); | ||||||||||||||||||||||
| SDL_RenderCopy(ctx.mSDLContext->mRenderer, tex, nullptr, &imageRect); | ||||||||||||||||||||||
| SDL_DestroyTexture(tex); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ret_code Renderer::closeScreen(Context &ctx) { | ||||||||||||||||||||||
| if (ctx.mSDLContext.mWindow == nullptr) { | ||||||||||||||||||||||
| if (ctx.mSDLContext->mWindow == nullptr) { | ||||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| TTF_Quit(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| SDL_DestroyWindow(ctx.mSDLContext.mWindow); | ||||||||||||||||||||||
| ctx.mSDLContext.mWindow = nullptr; | ||||||||||||||||||||||
| SDL_DestroyWindow(ctx.mSDLContext->mWindow); | ||||||||||||||||||||||
| ctx.mSDLContext->mWindow = nullptr; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ret_code Renderer::endRender(Context &ctx) { | ||||||||||||||||||||||
| SDL_RenderPresent(ctx.mSDLContext.mRenderer); | ||||||||||||||||||||||
| SDL_RenderPresent(ctx.mSDLContext->mRenderer); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -410,7 +416,7 @@ ret_code Renderer::createRenderTexture(Context &ctx, int w, int h, SDL_Texture * | |||||||||||||||||||||
| if (texture == nullptr) { | ||||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| *texture = SDL_CreateTexture(ctx.mSDLContext.mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); | ||||||||||||||||||||||
| *texture = SDL_CreateTexture(ctx.mSDLContext->mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.