Skip to content

Commit

Permalink
Add CreateTexture and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkulling committed Jan 5, 2025
1 parent 4628ff7 commit 2a35cf5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 48 deletions.
9 changes: 9 additions & 0 deletions src/backends/sdl2_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,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
1 change: 1 addition & 0 deletions src/backends/sdl2_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,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 2a35cf5

Please sign in to comment.