Skip to content

Commit ef209e6

Browse files
committed
2 parents e8e383b + cc9fc7f commit ef209e6

5 files changed

Lines changed: 86 additions & 56 deletions

File tree

src/backends/sdl2_renderer.cpp

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace {
7777
void showDriverInUse(const Context &ctx) {
7878
ctx.mLogger(LogSeverity::Message, "Driver in use:");
7979
SDL_RendererInfo info;
80-
SDL_GetRendererInfo(ctx.mSDLContext.mRenderer, &info);
80+
SDL_GetRendererInfo(ctx.mSDLContext->mRenderer, &info);
8181
printDriverInfo(info);
8282
}
8383

@@ -178,10 +178,16 @@ ret_code Renderer::releaseRenderer(Context &ctx) {
178178
delete ctx.mDefaultFont;
179179
ctx.mDefaultFont = nullptr;
180180
}
181-
182181
IMG_Quit();
183-
SDL_DestroyRenderer(ctx.mSDLContext.mRenderer);
184-
ctx.mSDLContext.mRenderer = nullptr;
182+
if (ctx.mSDLContext == nullptr) {
183+
SDL_Quit();
184+
return ResultOk;
185+
}
186+
187+
if (ctx.mSDLContext->mRenderer != nullptr) {
188+
SDL_DestroyRenderer(ctx.mSDLContext->mRenderer);
189+
ctx.mSDLContext->mRenderer = nullptr;
190+
}
185191
SDL_Quit();
186192

187193
return ResultOk;
@@ -214,7 +220,7 @@ ret_code Renderer::drawText(Context &ctx, const char *string, Font *font, const
214220
return ErrorCode;
215221
}
216222

217-
SDL_Texture *messageTexture = SDL_CreateTextureFromSurface(ctx.mSDLContext.mRenderer, surfaceMessage);
223+
SDL_Texture *messageTexture = SDL_CreateTextureFromSurface(ctx.mSDLContext->mRenderer, surfaceMessage);
218224
if (messageTexture == nullptr) {
219225
const std::string msg = "Cannot create texture: " + std::string(SDL_GetError()) + ".";
220226
ctx.mLogger(LogSeverity::Error, msg.c_str());
@@ -244,7 +250,7 @@ ret_code Renderer::drawText(Context &ctx, const char *string, Font *font, const
244250
break;
245251
}
246252

247-
SDL_RenderCopy(ctx.mSDLContext.mRenderer, messageTexture, NULL, &Message_rect);
253+
SDL_RenderCopy(ctx.mSDLContext->mRenderer, messageTexture, nullptr, &Message_rect);
248254
SDL_FreeSurface(surfaceMessage);
249255
SDL_DestroyTexture(messageTexture);
250256

@@ -256,8 +262,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int
256262
ctx.mLogger(LogSeverity::Error, "Not initialzed.");
257263
return ErrorCode;
258264
}
259-
260-
if (ctx.mSDLContext.mWindow != nullptr ) {
265+
ctx.mSDLContext = SDLContext::create();
266+
if (ctx.mSDLContext->mWindow != nullptr) {
261267
ctx.mLogger(LogSeverity::Error, "Already created.");
262268
return ErrorCode;
263269
}
@@ -277,8 +283,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int
277283
title = "TinyUI Window";
278284
}
279285

280-
ctx.mSDLContext.mWindow = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
281-
if (ctx.mSDLContext.mWindow == nullptr) {
286+
ctx.mSDLContext->mWindow = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
287+
if (ctx.mSDLContext->mWindow == nullptr) {
282288
const std::string msg = "Error while SDL_CreateWindow: " + std::string(SDL_GetError()) + ".";
283289
ctx.mLogger(LogSeverity::Error, msg.c_str());
284290
return ErrorCode;
@@ -290,8 +296,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int
290296
return ErrorCode;
291297
}
292298

293-
ctx.mSDLContext.mRenderer = SDL_CreateRenderer(ctx.mSDLContext.mWindow, driverIndex, SDL_RENDERER_ACCELERATED);
294-
if (nullptr == ctx.mSDLContext.mRenderer) {
299+
ctx.mSDLContext->mRenderer = SDL_CreateRenderer(ctx.mSDLContext->mWindow, driverIndex, SDL_RENDERER_ACCELERATED);
300+
if (nullptr == ctx.mSDLContext->mRenderer) {
295301
const std::string msg = "Error while SDL_CreateRenderer: " + std::string(SDL_GetError()) + ".";
296302
ctx.mLogger(LogSeverity::Error, msg.c_str());
297303
return ErrorCode;
@@ -303,8 +309,8 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int
303309

304310
showDriverInUse(ctx);
305311

306-
ctx.mSDLContext.mSurface = SDL_GetWindowSurface(ctx.mSDLContext.mWindow);
307-
if (ctx.mSDLContext.mSurface == nullptr) {
312+
ctx.mSDLContext->mSurface = SDL_GetWindowSurface(ctx.mSDLContext->mWindow);
313+
if (ctx.mSDLContext->mSurface == nullptr) {
308314
ctx.mLogger(LogSeverity::Error, "Surface pointer from window is nullptr.");
309315
return ErrorCode;
310316
}
@@ -325,13 +331,13 @@ ret_code Renderer::initScreen(Context &ctx, SDL_Window *window, SDL_Renderer *re
325331

326332
TTF_Init();
327333

328-
ctx.mSDLContext.mRenderer = renderer;
329-
ctx.mSDLContext.mWindow = window;
334+
ctx.mSDLContext->mRenderer = renderer;
335+
ctx.mSDLContext->mWindow = window;
330336

331337
showDriverInUse(ctx);
332338

333-
ctx.mSDLContext.mSurface = SDL_GetWindowSurface(ctx.mSDLContext.mWindow);
334-
ctx.mSDLContext.mOwner = false;
339+
ctx.mSDLContext->mSurface = SDL_GetWindowSurface(ctx.mSDLContext->mWindow);
340+
ctx.mSDLContext->mOwner = false;
335341
ctx.mCreated = true;
336342

337343
return ResultOk;
@@ -343,8 +349,8 @@ ret_code Renderer::releaseScreen(Context &ctx) {
343349
return ErrorCode;
344350
}
345351

346-
SDL_DestroyWindow(ctx.mSDLContext.mWindow);
347-
ctx.mSDLContext.mWindow = nullptr;
352+
SDL_DestroyWindow(ctx.mSDLContext->mWindow);
353+
ctx.mSDLContext->mWindow = nullptr;
348354

349355
return ResultOk;
350356
}
@@ -356,19 +362,19 @@ ret_code Renderer::beginRender(Context &ctx, Color4 bg, SDL_Texture *renderTarge
356362
}
357363

358364
const SDL_Color sdl_bg = getSDLColor(bg);
359-
SDL_SetRenderDrawColor(ctx.mSDLContext.mRenderer, sdl_bg.r, sdl_bg.g, sdl_bg.b, sdl_bg.a);
360-
SDL_RenderClear(ctx.mSDLContext.mRenderer);
365+
SDL_SetRenderDrawColor(ctx.mSDLContext->mRenderer, sdl_bg.r, sdl_bg.g, sdl_bg.b, sdl_bg.a);
366+
SDL_RenderClear(ctx.mSDLContext->mRenderer);
361367

362368
return ResultOk;
363369
}
364370

365371
ret_code Renderer::drawRect(Context &ctx, int32_t x, int32_t y, int32_t w, int32_t h, bool filled, Color4 fg) {
366372
SDL_Rect r = {x, y, w, h};
367-
SDL_SetRenderDrawColor(ctx.mSDLContext.mRenderer, fg.r, fg.g, fg.b, fg.a);
373+
SDL_SetRenderDrawColor(ctx.mSDLContext->mRenderer, fg.r, fg.g, fg.b, fg.a);
368374
if (filled) {
369-
SDL_RenderFillRect(ctx.mSDLContext.mRenderer, &r);
375+
SDL_RenderFillRect(ctx.mSDLContext->mRenderer, &r);
370376
} else {
371-
SDL_RenderDrawRect(ctx.mSDLContext.mRenderer, &r);
377+
SDL_RenderDrawRect(ctx.mSDLContext->mRenderer, &r);
372378
}
373379

374380
return ResultOk;
@@ -380,28 +386,28 @@ ret_code Renderer::drawImage(Context &ctx, int32_t x, int32_t y, int32_t w, int3
380386
}
381387

382388
SDL_Rect imageRect = {x, y, w, h};
383-
SDL_Texture *tex = SDL_CreateTextureFromSurface(ctx.mSDLContext.mRenderer, image->mSurfaceImpl->mSurface);
384-
SDL_RenderCopy(ctx.mSDLContext.mRenderer, tex, nullptr, &imageRect);
389+
SDL_Texture *tex = SDL_CreateTextureFromSurface(ctx.mSDLContext->mRenderer, image->mSurfaceImpl->mSurface);
390+
SDL_RenderCopy(ctx.mSDLContext->mRenderer, tex, nullptr, &imageRect);
385391
SDL_DestroyTexture(tex);
386392

387393
return ResultOk;
388394
}
389395

390396
ret_code Renderer::closeScreen(Context &ctx) {
391-
if (ctx.mSDLContext.mWindow == nullptr) {
397+
if (ctx.mSDLContext->mWindow == nullptr) {
392398
return ErrorCode;
393399
}
394400

395401
TTF_Quit();
396402

397-
SDL_DestroyWindow(ctx.mSDLContext.mWindow);
398-
ctx.mSDLContext.mWindow = nullptr;
403+
SDL_DestroyWindow(ctx.mSDLContext->mWindow);
404+
ctx.mSDLContext->mWindow = nullptr;
399405

400406
return ResultOk;
401407
}
402408

403409
ret_code Renderer::endRender(Context &ctx) {
404-
SDL_RenderPresent(ctx.mSDLContext.mRenderer);
410+
SDL_RenderPresent(ctx.mSDLContext->mRenderer);
405411

406412
return ResultOk;
407413
}
@@ -410,7 +416,7 @@ ret_code Renderer::createRenderTexture(Context &ctx, int w, int h, SDL_Texture *
410416
if (texture == nullptr) {
411417
return ErrorCode;
412418
}
413-
*texture = SDL_CreateTexture(ctx.mSDLContext.mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
419+
*texture = SDL_CreateTexture(ctx.mSDLContext->mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
414420

415421
return ResultOk;
416422
}

src/backends/sdl2_renderer.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@ struct FontImpl {
7171
}
7272
};
7373

74+
/// @brief The SDL context.
75+
struct SDLContext {
76+
SDL_Window *mWindow{ nullptr }; ///< The window.
77+
SDL_Surface *mSurface{ nullptr }; ///< The surface.
78+
SDL_Renderer *mRenderer{ nullptr }; ///< The renderer.
79+
bool mOwner{ false }; ///< The owner state.
80+
81+
/// @brief Will create a new SDL context.
82+
/// @return The created SDL context.
83+
static SDLContext *create() {
84+
return new SDLContext;
85+
}
86+
87+
/// @brief Will destroy the SDL context and all its resources.
88+
void destroy() {
89+
if (mOwner) {
90+
if (mRenderer != nullptr) {
91+
SDL_DestroyRenderer(mRenderer);
92+
mRenderer = nullptr;
93+
}
94+
if (mWindow != nullptr) {
95+
SDL_DestroyWindow(mWindow);
96+
mWindow = nullptr;
97+
}
98+
}
99+
delete this;
100+
}
101+
};
102+
103+
74104
/// @brief The renderer implementation using the SDL2 library.
75105
struct Renderer {
76106
static ret_code initRenderer(Context &ctx);

src/tinyui.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ ret_code TinyUi::getSurfaceInfo(int32_t &w, int32_t &h) {
114114
return ErrorCode;
115115
}
116116

117-
if (ctx.mSDLContext.mSurface == nullptr) {
117+
if (ctx.mSDLContext->mSurface == nullptr) {
118118
return ErrorCode;
119119
}
120120

121-
w = ctx.mSDLContext.mSurface->w;
122-
h = ctx.mSDLContext.mSurface->h;
121+
w = ctx.mSDLContext->mSurface->w;
122+
h = ctx.mSDLContext->mSurface->h;
123123

124124
return ResultOk;
125125
}
@@ -171,8 +171,8 @@ ret_code TinyUi::release() {
171171
}
172172
Renderer::releaseRenderer(ctx);
173173
Renderer::releaseScreen(ctx);
174-
ctx.mSDLContext.mRenderer = nullptr;
175-
ctx.mSDLContext.mWindow = nullptr;
174+
ctx.mSDLContext->mRenderer = nullptr;
175+
ctx.mSDLContext->mWindow = nullptr;
176176
ctx.mRoot = nullptr;
177177

178178
ctx.mCreated = false;

src/tinyui.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ struct SurfaceImpl;
7272
struct FontImpl;
7373
struct Widget;
7474

75+
struct SDLContext;
76+
7577
// Type declarations ----------------------------------------------------------
7678

7779
/// @brief This enum is used to describe the alignment of a widget.
@@ -380,14 +382,6 @@ using EventDispatchMap = std::map<int32_t, EventCallbackArray>;
380382
/// @brief Function pointer declaration for callbacks.
381383
typedef void (*tui_log_func) (LogSeverity severity, const char *message);
382384

383-
/// @brief The SDL context.
384-
struct SDLContext {
385-
SDL_Window *mWindow{nullptr}; ///< The window.
386-
SDL_Surface *mSurface{ nullptr }; ///< The surface.
387-
SDL_Renderer *mRenderer{ nullptr }; ///< The renderer.
388-
bool mOwner{ false }; ///< The owner state.
389-
};
390-
391385
/// @brief The update callback list.
392386
using UpdateCallbackList = std::list<CallbackI*>;
393387

@@ -397,7 +391,7 @@ struct Context {
397391
bool mRequestShutdown{false}; ///< The request shutdown state.
398392
const char *mAppTitle{nullptr}; ///< The application title.
399393
const char *mWindowsTitle{nullptr}; ///< The window title.
400-
SDLContext mSDLContext; ///< The SDL context.
394+
SDLContext *mSDLContext{ nullptr }; ///< The SDL context.
401395
Style mStyle{}; ///< The active style.
402396
Widget *mRoot{nullptr}; ///< The root widget.
403397
Widget *mFocus{nullptr}; ///< The widget which is in focus.

src/widgets.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void Widgets::findSelectedWidget(int x, int y, Widget *currentChild, Widget **fo
221221

222222
ret_code Widgets::label(Id id, Id parentId, const char *text, const Rect &rect, Alignment alignment) {
223223
auto &ctx = TinyUi::getContext();
224-
if (ctx.mSDLContext.mRenderer == nullptr) {
224+
if (ctx.mSDLContext->mRenderer == nullptr) {
225225
return InvalidRenderHandle;
226226
}
227227

@@ -243,7 +243,7 @@ ret_code Widgets::label(Id id, Id parentId, const char *text, const Rect &rect,
243243

244244
ret_code Widgets::inputText(Id id, Id parentId, const Rect &rect, Alignment alignment) {
245245
auto &ctx = TinyUi::getContext();
246-
if (ctx.mSDLContext.mRenderer == nullptr) {
246+
if (ctx.mSDLContext->mRenderer == nullptr) {
247247
return InvalidRenderHandle;
248248
}
249249

@@ -263,7 +263,7 @@ ret_code Widgets::inputText(Id id, Id parentId, const Rect &rect, Alignment alig
263263

264264
ret_code Widgets::button(Id id, Id parentId, const char *text, const Rect &rect, CallbackI *callback) {
265265
auto &ctx = TinyUi::getContext();
266-
if (ctx.mSDLContext.mRenderer == nullptr) {
266+
if (ctx.mSDLContext->mRenderer == nullptr) {
267267
return InvalidRenderHandle;
268268
}
269269

@@ -289,7 +289,7 @@ ret_code Widgets::button(Id id, Id parentId, const char *text, const Rect &rect,
289289

290290
ret_code Widgets::imageButton(Id id, Id parentId, const char *image, const Rect &rect, CallbackI *callback) {
291291
auto &ctx = TinyUi::getContext();
292-
if (ctx.mSDLContext.mRenderer == nullptr) {
292+
if (ctx.mSDLContext->mRenderer == nullptr) {
293293
return InvalidRenderHandle;
294294
}
295295

@@ -316,7 +316,7 @@ ret_code Widgets::imageButton(Id id, Id parentId, const char *image, const Rect
316316

317317
ret_code Widgets::box(Id id, Id parentId, const Rect &rect, bool filled) {
318318
auto &ctx = TinyUi::getContext();
319-
if (ctx.mSDLContext.mRenderer == nullptr) {
319+
if (ctx.mSDLContext->mRenderer == nullptr) {
320320
return InvalidRenderHandle;
321321
}
322322

@@ -336,7 +336,7 @@ ret_code Widgets::box(Id id, Id parentId, const Rect &rect, bool filled) {
336336

337337
ret_code Widgets::imageBox(Id id, Id parentId, const char* image, const Rect& rect, bool filled) {
338338
auto &ctx = TinyUi::getContext();
339-
if (ctx.mSDLContext.mRenderer == nullptr) {
339+
if (ctx.mSDLContext->mRenderer == nullptr) {
340340
return InvalidRenderHandle;
341341
}
342342

@@ -358,7 +358,7 @@ ret_code Widgets::imageBox(Id id, Id parentId, const char* image, const Rect& re
358358

359359
ret_code Widgets::panel(Id id, Id parentId, const char *title, const Rect &rect, CallbackI *callback) {
360360
auto &ctx = TinyUi::getContext();
361-
if (ctx.mSDLContext.mRenderer == nullptr) {
361+
if (ctx.mSDLContext->mRenderer == nullptr) {
362362
return InvalidRenderHandle;
363363
}
364364

@@ -388,7 +388,7 @@ static int onTreeViewItemClicked(Id id, void *data) {
388388

389389
ret_code Widgets::treeView(Id id, Id parentId, const char *title, const Rect &rect) {
390390
auto &ctx = TinyUi::getContext();
391-
if (ctx.mSDLContext.mRenderer == nullptr) {
391+
if (ctx.mSDLContext->mRenderer == nullptr) {
392392
return InvalidRenderHandle;
393393
}
394394

@@ -417,7 +417,7 @@ ret_code Widgets::treeView(Id id, Id parentId, const char *title, const Rect &re
417417

418418
ret_code Widgets::treeItem(Id id, Id parentItemId, const char *text) {
419419
auto &ctx = TinyUi::getContext();
420-
if (ctx.mSDLContext.mRenderer == nullptr) {
420+
if (ctx.mSDLContext->mRenderer == nullptr) {
421421
return InvalidRenderHandle;
422422
}
423423

@@ -453,7 +453,7 @@ ret_code Widgets::treeItem(Id id, Id parentItemId, const char *text) {
453453

454454
ret_code Widgets::progressBar(Id id, Id parentId, const Rect &rect, int fillRate, CallbackI *callback) {
455455
auto &ctx = TinyUi::getContext();
456-
if (ctx.mSDLContext.mRenderer == nullptr) {
456+
if (ctx.mSDLContext->mRenderer == nullptr) {
457457
return InvalidRenderHandle;
458458
}
459459

0 commit comments

Comments
 (0)