Skip to content

Commit

Permalink
Merge pull request #8 from kimkulling/bugfix/fix_sonarcube_findings
Browse files Browse the repository at this point in the history
Bugfix/fix sonarcube findings
  • Loading branch information
kimkulling authored Jan 5, 2025
2 parents 062e55e + 91108b8 commit 3757e76
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 71 deletions.
13 changes: 2 additions & 11 deletions samples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@ SOFTWARE.
#include <iostream>
#include "widgets.h"

#ifdef main
# undef main
#endif

using namespace tinyui;

static constexpr Id RootPanelId = 1;

static constexpr Id NextPanelId = 100;

static void renderDialog(const char *title, Context &ctx) {
Rect r(240, 90, 120, 250);
Widgets::panel(ctx, NextPanelId, 0, title, r, nullptr);
}

int quit(uint32_t id, void *instance) {
int quit(uint32_t, void *instance) {
if (instance == nullptr) {
return ErrorCode;
}
Expand All @@ -50,7 +41,7 @@ int quit(uint32_t id, void *instance) {
return ResultOk;
}

int updateProgressbar(uint32_t id, void *instance) {
int updateProgressbar(uint32_t, void *instance) {
if (instance == nullptr) {
return ErrorCode;
}
Expand Down
39 changes: 27 additions & 12 deletions src/backends/sdl2_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SOFTWARE.
namespace tinyui {
namespace {

static SDL_Color getSDLColor(const Color4 &col) {
SDL_Color getSDLColor(const Color4 &col) {
SDL_Color sdl_col = {};
sdl_col.r = col.r;
sdl_col.g = col.g;
Expand All @@ -41,11 +41,11 @@ static SDL_Color getSDLColor(const Color4 &col) {
return sdl_col;
}

static void printDriverInfo(const SDL_RendererInfo &info) {
void printDriverInfo(const SDL_RendererInfo &info) {
printf("Driver : %s\n", info.name);
}

static void listAllRenderDivers(const Context &ctx) {
void listAllRenderDivers(const Context &ctx) {
const int numRenderDrivers = SDL_GetNumRenderDrivers();
ctx.mLogger(LogSeverity::Message, "Available drivers:");
for (int i = 0; i < numRenderDrivers; ++i) {
Expand All @@ -55,15 +55,16 @@ static void listAllRenderDivers(const Context &ctx) {
}
}

static void showDriverInUse(const Context &ctx) {
void showDriverInUse(const Context &ctx) {
ctx.mLogger(LogSeverity::Message, "Driver in use:");
SDL_RendererInfo info;
SDL_GetRendererInfo(ctx.mSDLContext.mRenderer, &info);
printDriverInfo(info);
}

static int queryDriver(const Context &ctx, const char *type) {
if (type == nullptr) {
int queryDriver(const Context &ctx, const char *driverType, size_t maxLen) {
if (driverType == nullptr) {
ctx.mLogger(LogSeverity::Error, "Driver type is a nullptr.");
return -1;
}

Expand All @@ -72,7 +73,11 @@ static int queryDriver(const Context &ctx, const char *type) {
for (int i = 0; i < numRenderDrivers; ++i) {
SDL_RendererInfo info;
SDL_GetRenderDriverInfo(i, &info);
if (strncmp(type, info.name, strlen(type)) == 0) {
size_t len = strlen(driverType);
if (len > maxLen) {
len = maxLen;
}
if (strncmp(driverType, info.name, len) == 0) {
found = i;
break;
}
Expand All @@ -81,14 +86,14 @@ static int queryDriver(const Context &ctx, const char *type) {
return found;
}

static void loadFont(Context &ctx) {
void loadFont(Context &ctx) {
ctx.mSDLContext.mDefaultFont = new Font;
ctx.mSDLContext.mDefaultFont->mFont = new FontImpl;
ctx.mSDLContext.mDefaultFont->mSize = ctx.mStyle.mFont.mSize;
ctx.mSDLContext.mDefaultFont->mFont->mFontImpl = TTF_OpenFont(ctx.mStyle.mFont.mName, ctx.mStyle.mFont.mSize);
}

static MouseState getButtonState(const SDL_MouseButtonEvent &b) {
MouseState getButtonState(const SDL_MouseButtonEvent &b) {
MouseState state = MouseState::Invalid;
switch (b.button) {
case SDL_BUTTON_LEFT:
Expand All @@ -105,10 +110,11 @@ static MouseState getButtonState(const SDL_MouseButtonEvent &b) {
default:
break;
}

return state;
}

static int32_t getEventType(Uint32 sdlType) {
int32_t getEventType(Uint32 sdlType) {
switch (sdlType) {
case SDL_QUIT:
return Events::QuitEvent;
Expand Down Expand Up @@ -261,7 +267,7 @@ ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int
return ErrorCode;
}

const int driverIndex = queryDriver(ctx, "opengl");
const int driverIndex = queryDriver(ctx, "opengl", 256);
if (driverIndex == -1) {
ctx.mLogger(LogSeverity::Error, "Cannot open opengl driver");
return ErrorCode;
Expand Down Expand Up @@ -322,7 +328,7 @@ ret_code Renderer::beginRender(Context &ctx, Color4 bg, SDL_Texture *renderTarge
SDL_SetRenderDrawColor(ctx.mSDLContext.mRenderer, sdl_bg.r, sdl_bg.g, sdl_bg.b, sdl_bg.a);
SDL_RenderClear(ctx.mSDLContext.mRenderer);

return 0;
return ResultOk;
}

ret_code Renderer::drawRect(Context &ctx, int32_t x, int32_t y, int32_t w, int32_t h, bool filled, Color4 fg) {
Expand Down Expand Up @@ -374,6 +380,15 @@ ret_code Renderer::endRender(Context &ctx) {
return ResultOk;
}

ret_code Renderer::createRenderTexture(Context &ctx, int w, int h, SDL_Texture **texture) {
if (texture == nullptr) {
return ErrorCode;
}
*texture = SDL_CreateTexture(ctx.mSDLContext.mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);

return ResultOk;
}

bool Renderer::update(Context &ctx) {
if (!ctx.mCreated) {
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/backends/sdl2_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ struct SurfaceImpl {
SDL_Surface *mSurface;

~SurfaceImpl() {
clear();
}

void clear() {
if (mSurface != nullptr) {
SDL_FreeSurface(mSurface);
mSurface = nullptr;
}

}
};

Expand Down Expand Up @@ -73,6 +79,7 @@ struct Renderer {
static ret_code drawImage(Context &ctx, int32_t x, int32_t y, int32_t w, int32_t h, Image *image);
static ret_code beginRender(Context &ctx, Color4 bg, SDL_Texture *renderTarget = nullptr);
static ret_code endRender(Context &ctx);
static ret_code createRenderTexture(Context &ctx, int w, int h, SDL_Texture **texture);
static ret_code closeScreen(Context &ctx);
static bool update(Context &ctx);
static SurfaceImpl *createSurfaceImpl(unsigned char *data, int w, int h, int bytesPerPixel, int pitch);
Expand Down
2 changes: 0 additions & 2 deletions src/tinyui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ void Context::destroy(Context &ctx) {
delete ptr;
}

void Context::enableExtensions(Context &ctx, const std::vector<tinyui::Extensions> &extensions) {}

const Style &TinyUi::getDefaultStyle() {
return DefaultStyle;
}
Expand Down
102 changes: 56 additions & 46 deletions src/tinyui.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,39 +208,38 @@ struct Font {
/// @brief The font cache.
using FontCache = std::map<const char*, Font*>;

/// @brief The style struct.
///
/// The style struct is used to describe the style of the tiny ui.
struct Style {
Color4 mClearColor;
Color4 mFg;
Color4 mBg;
Color4 mTextColor;
Color4 mBorder;
int32_t mMargin;
Font mFont;
Color4 mClearColor; ///< The clear color.
Color4 mFg; ///< The foreground color.
Color4 mBg; ///< The background color.
Color4 mTextColor; ///< The text color.
Color4 mBorder; ///< The border color.
int32_t mMargin; ///< The margin.
Font mFont; ///< The font.
};

/// @brief The mouse state
enum class MouseState {
Invalid = -1,
LeftButton = 0,
MiddleButton,
RightButton,
Count
Invalid = -1, ///< The invalid state
LeftButton = 0, ///< The left button
MiddleButton, ///< The middle button
RightButton, ///< The right button
Count ///< The number of mouse states
};

/// @brief The log severity.
enum class LogSeverity {
Invalid = -1,
Message = 0,
Trace,
Debug,
Info,
Warn,
Error,
Count
};

enum class Extensions {
Invalid = -1,
VerboseMode,
Count
Invalid = -1, ///< Marks an invalid entry
Message = 0, ///< A message
Trace, ///< A trace message
Debug, ///< A debug message
Info, ///< An info message
Warn, ///< A warning message
Error, ///< An error message
Count ///< The number of log severities
};

/// @brief The tiny ui events.
Expand Down Expand Up @@ -300,40 +299,51 @@ using EventDispatchMap = std::map<int32_t, EventCallbackArray>;
/// @brief Function pointer declaration for callbacks.
typedef void (*tui_log_func) (LogSeverity severity, const char *message);

/// @brief The SDL context.
struct SDLContext {
SDL_Window *mWindow;
SDL_Surface *mSurface;
SDL_Renderer *mRenderer;
Font *mDefaultFont;
Font *mSelectedFont;
bool mOwner;
SDL_Window *mWindow; ///< The window.
SDL_Surface *mSurface; ///< The surface.
SDL_Renderer *mRenderer; ///< The renderer.
Font *mDefaultFont; ///< The default font.
Font *mSelectedFont; ///< The selected font.
bool mOwner; ///< The owner state.

/// @brief The default class constructor
SDLContext() :
mWindow(nullptr), mSurface(nullptr), mRenderer(nullptr),
mDefaultFont(nullptr), mSelectedFont(nullptr), mOwner(true) {}
};

/// @brief The update callback list.
using UpdateCallbackList = std::list<CallbackI*>;

/// @brief The tiny ui context.
struct Context {
bool mCreated;
bool mRequestShutdown;
const char *mAppTitle;
const char *mWindowsTitle;
SDLContext mSDLContext;
Style mStyle;
Widget *mRoot;
tui_log_func mLogger = nullptr;
EventDispatchMap mEventDispatchMap;
FontCache mFontCache;
ImageCache mImageCache;
UpdateCallbackList mUpdateCallbackList;

bool mCreated; ///< The created state.
bool mRequestShutdown; ///< The request shutdown state.
const char *mAppTitle; ///< The application title.
const char *mWindowsTitle; ///< The window title.
SDLContext mSDLContext; ///< The SDL context.
Style mStyle; ///< The active style.
Widget *mRoot; ///< The root widget.
tui_log_func mLogger = nullptr; ///< The logger function.
EventDispatchMap mEventDispatchMap; ///< The event dispatch map.
FontCache mFontCache; ///< The font cache.
ImageCache mImageCache; ///< The image cache.
UpdateCallbackList mUpdateCallbackList; ///< The update callback list.

/// @brief Will create a new tiny ui context.
/// @param title The title of the context.
/// @param style The style to use.
/// @return The created context.
static Context &create(const char *title, Style &style);

/// @brief Will destroy a valid tinyui context.
/// @param ctx The context to destroy.
static void destroy(Context &ctx);
static void enableExtensions(Context &ctx, const std::vector<tinyui::Extensions> &extensions);

private:
/// @brief The default class constructor
Context() :
mCreated(false),
mRequestShutdown(false),
Expand Down

0 comments on commit 3757e76

Please sign in to comment.