Compare commits
	
		
			No commits in common. "762d165cd14f0d4423c2de6f7f1659f7b36a4fd3" and "c372a4db3a28dd292793756f0ca36e9460d24b33" have entirely different histories.
		
	
	
		
			762d165cd1
			...
			c372a4db3a
		
	
		
| 
						 | 
				
			
			@ -20,7 +20,7 @@ void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intpt
 | 
			
		|||
		},
 | 
			
		||||
	}) {
 | 
			
		||||
		Clay_OnHover(onHovered, onHoveredData);
 | 
			
		||||
		Body(text, {
 | 
			
		||||
		cera::Body(text, {
 | 
			
		||||
			.textColor = theme->textColor,
 | 
			
		||||
			.textAlignment = CLAY_TEXT_ALIGN_CENTER,
 | 
			
		||||
		});
 | 
			
		||||
| 
						 | 
				
			
			@ -35,8 +35,7 @@ void ToggleHovered(Clay_ElementId element, Clay_PointerData pointer, intptr_t da
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bool Toggle(Clay_String label, Clay_Color selected, bool &state) {
 | 
			
		||||
	bool const before{state};
 | 
			
		||||
void Toggle(Clay_String label, Clay_Color selected, bool &state) {
 | 
			
		||||
	CLAY_AUTO_ID({
 | 
			
		||||
		.layout = {
 | 
			
		||||
			.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
 | 
			
		||||
| 
						 | 
				
			
			@ -69,12 +68,10 @@ bool Toggle(Clay_String label, Clay_Color selected, bool &state) {
 | 
			
		|||
			.textColor = theme->textColor
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
	// return if the value changed this frame
 | 
			
		||||
	return before != state;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Body(Clay_String string, Clay_TextElementConfig baseCfg) {
 | 
			
		||||
	baseCfg.fontId = cera::FONT_REGULAR;
 | 
			
		||||
	baseCfg.fontId = cera::FONT_DEFAULT;
 | 
			
		||||
	baseCfg.fontSize = cera::baseFontSize;
 | 
			
		||||
	CLAY_TEXT(string, CLAY_TEXT_CONFIG(baseCfg));
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,13 @@
 | 
			
		|||
#ifndef ELEMENTS_H
 | 
			
		||||
#define ELEMENTS_H
 | 
			
		||||
 | 
			
		||||
#include "style.h"
 | 
			
		||||
#include <SDL3/SDL_render.h>
 | 
			
		||||
#include <clay/clay.h>
 | 
			
		||||
#include "style.h"
 | 
			
		||||
 | 
			
		||||
namespace cera {
 | 
			
		||||
typedef void(*OnHoveredFn)(Clay_ElementId element, Clay_PointerData pointer, intptr_t data);
 | 
			
		||||
void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intptr_t onHoveredData = 0);
 | 
			
		||||
bool Toggle(Clay_String label, Clay_Color selected, bool &state);
 | 
			
		||||
void Toggle(Clay_String label, Clay_Color selected, bool &state);
 | 
			
		||||
void Body(Clay_String string, Clay_TextElementConfig baseCfg = {.textColor = theme->textColor});
 | 
			
		||||
void Header(Clay_String string, size_t header, Clay_TextElementConfig baseCfg = {.textColor = theme->textColor});
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,34 +4,18 @@
 | 
			
		|||
#include <SDL3/SDL_log.h>
 | 
			
		||||
#include <SDL3/SDL_render.h>
 | 
			
		||||
#include <SDL3_image/SDL_image.h>
 | 
			
		||||
#include <cstddef>
 | 
			
		||||
 | 
			
		||||
#ifndef CERA_RESOURCE_REGISTRY_SIZE
 | 
			
		||||
#define CERA_RESOURCE_REGISTRY_SIZE 64
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace cera {
 | 
			
		||||
struct ResourceRegistry {
 | 
			
		||||
	void *data;
 | 
			
		||||
	DestructorFn destructor;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
TTF_Font *defaultFont[FONT_MAX];
 | 
			
		||||
TTF_TextEngine *textEngine = nullptr;
 | 
			
		||||
 | 
			
		||||
ResourceRegistry resources[CERA_RESOURCE_REGISTRY_SIZE]{};
 | 
			
		||||
size_t usedResources{0};
 | 
			
		||||
 | 
			
		||||
void SetDefaultFont(char const *path) {
 | 
			
		||||
	// LOAD REGULAR FONT
 | 
			
		||||
	defaultFont[FONT_REGULAR] = TTF_OpenFont(path, cera::baseFontSize * 5);
 | 
			
		||||
	if (defaultFont[FONT_REGULAR] == nullptr) {
 | 
			
		||||
	defaultFont[FONT_DEFAULT] = TTF_OpenFont(path, cera::baseFontSize * 5);
 | 
			
		||||
	if (defaultFont[FONT_DEFAULT] == nullptr) {
 | 
			
		||||
		SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load default font '%s': %s", path, SDL_GetError());
 | 
			
		||||
		exit(6);
 | 
			
		||||
	}
 | 
			
		||||
	TTF_SetFontHinting(defaultFont[FONT_REGULAR], TTF_HINTING_LIGHT_SUBPIXEL);
 | 
			
		||||
	StoreResource(defaultFont[FONT_REGULAR], (DestructorFn)&TTF_CloseFont);
 | 
			
		||||
	// LOAD BOLD FONT
 | 
			
		||||
	TTF_SetFontHinting(defaultFont[FONT_DEFAULT], TTF_HINTING_LIGHT_SUBPIXEL);
 | 
			
		||||
	defaultFont[FONT_BOLD] = TTF_OpenFont(path, cera::baseFontSize * 5);
 | 
			
		||||
	if (defaultFont[FONT_BOLD] == nullptr) {
 | 
			
		||||
		SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load default bold font '%s': %s", path, SDL_GetError());
 | 
			
		||||
| 
						 | 
				
			
			@ -39,33 +23,6 @@ void SetDefaultFont(char const *path) {
 | 
			
		|||
	}
 | 
			
		||||
	TTF_SetFontHinting(defaultFont[FONT_BOLD], TTF_HINTING_LIGHT_SUBPIXEL);
 | 
			
		||||
	TTF_SetFontStyle(defaultFont[FONT_BOLD], TTF_STYLE_BOLD);
 | 
			
		||||
	StoreResource(defaultFont[FONT_BOLD], (DestructorFn)&TTF_CloseFont);
 | 
			
		||||
	SDL_Log("SetDefaultFont: Success");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void StoreResource(void *data, DestructorFn destructor) {
 | 
			
		||||
	for (size_t i{0}; i < usedResources; ++i) {
 | 
			
		||||
		if(resources[i].data == nullptr) {
 | 
			
		||||
			resources[i].data = data;
 | 
			
		||||
			resources[i].destructor = destructor;
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if(usedResources < CERA_RESOURCE_REGISTRY_SIZE) {
 | 
			
		||||
		resources[usedResources].data = data;
 | 
			
		||||
		resources[usedResources].destructor = destructor;
 | 
			
		||||
		++usedResources;
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to register resource, expect memory leaks. Consider using less resources or defining a higher CERA_RESOURCE_REGISTRY_SIZE at compile time");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CleanupResources() {
 | 
			
		||||
	for (size_t i{0}; i < usedResources; ++i) {
 | 
			
		||||
		if(resources[i].data) {
 | 
			
		||||
			resources[i].destructor(resources[i].data);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	usedResources = 0;
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										16
									
								
								resources.h
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								resources.h
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -1,13 +1,11 @@
 | 
			
		|||
#ifndef RESOURCES_H
 | 
			
		||||
#define RESOURCES_H
 | 
			
		||||
 | 
			
		||||
#include "SDL3/SDL_render.h"
 | 
			
		||||
#include <SDL3_image/SDL_image.h>
 | 
			
		||||
#include <SDL3_ttf/SDL_ttf.h>
 | 
			
		||||
 | 
			
		||||
namespace cera {
 | 
			
		||||
enum Font {
 | 
			
		||||
	FONT_REGULAR = 0,
 | 
			
		||||
	FONT_DEFAULT = 0,
 | 
			
		||||
	FONT_BOLD = 1,
 | 
			
		||||
	FONT_MAX
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			@ -15,19 +13,7 @@ enum Font {
 | 
			
		|||
extern TTF_TextEngine *textEngine;
 | 
			
		||||
extern TTF_Font *defaultFont[FONT_MAX];
 | 
			
		||||
 | 
			
		||||
typedef void (*DestructorFn)(void* data);
 | 
			
		||||
 | 
			
		||||
void SetDefaultFont(char const *path);
 | 
			
		||||
void StoreResource(void *data, DestructorFn destructor);
 | 
			
		||||
void CleanupResources();
 | 
			
		||||
 | 
			
		||||
static inline SDL_Texture *LoadAndStoreTexture(SDL_Renderer *renderer, char const *path) {
 | 
			
		||||
	if(SDL_Texture *texture{IMG_LoadTexture(renderer, path)}) {
 | 
			
		||||
		StoreResource(texture, (DestructorFn)&SDL_DestroyTexture);
 | 
			
		||||
		return texture;
 | 
			
		||||
	}
 | 
			
		||||
	return nullptr;
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // !RESOURCES_H
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue