From 897a153d290ed4d3647ad9e0100d1b717f580b5f Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Tue, 12 May 2026 16:41:37 -0400 Subject: typedef all structs and rename to Ada_Case There isn't really a technical reason that I made this change. I just wanted to use Ada_Case. All types were prefixed with ty_, e.g. ty_Image. --- teensy/teensy.h | 109 ++++++++++++++++++++++------------------- teensy/teensy_context.c | 15 +++--- teensy/teensy_context.h | 8 +-- teensy/teensy_list.c | 2 +- teensy/teensy_list.h | 34 ++++++------- teensy/teensy_mem.c | 16 +++--- teensy/teensy_platform.h | 8 +-- teensy/teensy_renderer.c | 124 +++++++++++++++++++++++------------------------ teensy/teensy_renderer.h | 10 ++-- 9 files changed, 167 insertions(+), 159 deletions(-) (limited to 'teensy') diff --git a/teensy/teensy.h b/teensy/teensy.h index 4a60882..c272664 100644 --- a/teensy/teensy.h +++ b/teensy/teensy.h @@ -6,10 +6,17 @@ #include "teensy_common.h" #include "teensy_list.h" -#define ty_vec2(x, y) ((struct ty_vec2){x, y}) -#define ty_vec2i(x, y) ((struct ty_vec2i){x, y}) -#define ty_color(R, G, B) ((struct ty_color){R, G, B}) -#define ty_img_full(img) ((struct ty_recti){0, 0, (img).width, (img).height}) +#define ty_vec2(x, y) ((ty_Vec2){x, y}) +#define ty_vec2i(x, y) ((ty_Vec2i){x, y}) +#define ty_color(R, G, B) ((ty_Color){R, G, B}) +#define ty_img_full(img) ((ty_Recti){0, 0, (img).width, (img).height}) + +#define ty_rect_start(rect) (ty_vec2(rect.x, rect.y)) +#define ty_recti_start(rect) (ty_vec2i(rect.x, rect.y)) +#define ty_rect_end(rect) (ty_vec2(rect.x+rect.w, rect.y+rect.h)) +#define ty_recti_end(rect) (ty_vec2i(rect.x+rect.w, rect.y+rect.h)) +#define ty_rect_size(rect) (ty_vec2(rect.w, rect.h)) +#define ty_recti_size(rect) (ty_vec2i(rect.w, rect.h)) #define TY_COLOR_BLACK ty_color(0, 0, 0 ) #define TY_COLOR_RED ty_color(255, 0, 0 ) @@ -19,9 +26,10 @@ #define TY_COLOR_MAGENTA ty_color(255, 0, 255) #define TY_COLOR_CYAN ty_color(0, 255, 255) #define TY_COLOR_WHITE ty_color(255, 255, 255) +#define TY_COLOR_GRAY ty_color(128, 128, 128) // Virtual controller. Keyboard keys are also mapped to this. -enum ty_button { +enum { TY_BTN_LEFT_UP, // D-pad #1 up TY_BTN_LEFT_DOWN, // D-pad #1 down TY_BTN_LEFT_LEFT, // D-pad #1 left @@ -43,61 +51,62 @@ enum ty_button { TY_BTN_COUNT, }; +typedef uint8_t ty_Button; -struct ty_hints { +typedef struct { int scr_width; int scr_height; const char *game_title; int ticrate; -}; +} ty_Hints; -struct ty_color { +typedef struct { uint8_t r; uint8_t g; uint8_t b; -}; +} ty_Color; -struct ty_vec2 { +typedef struct { float x; float y; -}; +} ty_Vec2; -struct ty_vec2i { +typedef struct { int x; int y; -}; +} ty_Vec2i; -struct ty_rect { +typedef struct { float x; float y; float w; float h; -}; +} ty_Rect; -struct ty_recti { +typedef struct { int x; int y; int w; int h; -}; +} ty_Recti; -struct ty_image { - struct ty_color *data; +typedef struct { + ty_Color *data; int width; int height; -}; +} ty_Image; -struct ty_font { - struct ty_image glyphs[CHAR_MAX]; -}; +typedef struct { + ty_Image glyphs[CHAR_MAX]; +} ty_Font; -void ty_init(struct ty_hints hints); +void ty_init(ty_Hints hints); void ty_deinit(void); // If the button is being held down -bool ty_button_down(enum ty_button btn); +bool ty_button_down(ty_Button btn); // If the button was pressed just now -bool ty_button_pressed(enum ty_button btn); +bool ty_button_pressed(ty_Button btn); // Whether or not the main loop should continue executing. bool ty_is_game_running(void); @@ -107,43 +116,43 @@ double ty_get_time(void); // tic. int ty_tick(void); -struct ty_image ty_create_image(int w, int h, const struct ty_color* data); -void ty_free_image(struct ty_image img); -struct ty_color ty_img_get_pixel(struct ty_image img, struct ty_vec2i pos); +ty_Image ty_create_image(int w, int h, const ty_Color* data); +void ty_free_image(ty_Image img); +ty_Color ty_img_get_pixel(ty_Image img, ty_Vec2i pos); void ty_img_set_pixel( - struct ty_image img, - struct ty_vec2i pos, - struct ty_color color + ty_Image img, + ty_Vec2i pos, + ty_Color color ); -void ty_font_add_glyph(struct ty_font *font, uint8_t c, struct ty_image img); -int ty_font_width(struct ty_font *font, const char *fmt, ...); +void ty_font_add_glyph(ty_Font *font, uint8_t c, ty_Image img); +int ty_font_width(ty_Font *font, const char *fmt, ...); // Pass NULL to restore the screen. -void ty_draw_set_target(const struct ty_image *img); -void ty_draw_clear(struct ty_color col); -void ty_draw_image(struct ty_image img, struct ty_vec2i pos); +void ty_draw_set_target(const ty_Image *img); +void ty_draw_clear(ty_Color col); +void ty_draw_image(ty_Image img, ty_Vec2i pos); void ty_draw_image_ex( - struct ty_image img, - struct ty_recti src, - struct ty_recti dst + ty_Image img, + ty_Recti src, + ty_Recti dst ); void ty_draw_image_rot( - struct ty_image img, - struct ty_vec2i pos, - struct ty_vec2i offset, + ty_Image img, + ty_Vec2i pos, + ty_Vec2i offset, double rot ); -void ty_draw_rect(struct ty_recti rect, struct ty_color color); +void ty_draw_rect(ty_Recti rect, ty_Color color); void ty_draw_line( - struct ty_vec2i start, - struct ty_vec2i end, - struct ty_color color + ty_Vec2i start, + ty_Vec2i end, + ty_Color color ); -void ty_draw_text(struct ty_font *font, struct ty_vec2i pos, const char *text); +void ty_draw_text(ty_Font *font, ty_Vec2i pos, const char *text); void ty_draw_text_fmt( - struct ty_font *font, - struct ty_vec2i pos, + ty_Font *font, + ty_Vec2i pos, const char *fmt, ... ); diff --git a/teensy/teensy_context.c b/teensy/teensy_context.c index 9dff405..547a5dd 100644 --- a/teensy/teensy_context.c +++ b/teensy/teensy_context.c @@ -1,13 +1,12 @@ #include "teensy_context.h" -#include "teensy_renderer.h" #include #include #include "teensy_platform.h" +#include "teensy_renderer.h" -struct ty_ctx ctx; -extern struct ty_renderer r; +ty_Ctx ctx; bool is_init(void) { @@ -15,7 +14,7 @@ bool is_init(void) return scr_size > 0; } -void ty_init(struct ty_hints hints) +void ty_init(ty_Hints hints) { ty_init_mem(); @@ -35,15 +34,15 @@ void ty_deinit(void) ty_deinit_mem(); } -bool ty_button_down(enum ty_button btn) +bool ty_button_down(ty_Button btn) { - assert(btn >= 0 && btn < TY_BTN_COUNT); + assert(btn < TY_BTN_COUNT); return ctx.down[btn]; } -bool ty_button_pressed(enum ty_button btn) +bool ty_button_pressed(ty_Button btn) { - assert(btn >= 0 && btn < TY_BTN_COUNT); + assert(btn < TY_BTN_COUNT); return ctx.pressed[btn]; } diff --git a/teensy/teensy_context.h b/teensy/teensy_context.h index 7223d3d..f9d4bf4 100644 --- a/teensy/teensy_context.h +++ b/teensy/teensy_context.h @@ -5,15 +5,15 @@ #include "teensy_renderer.h" #include "teensy.h" -struct ty_ctx { - struct ty_hints hints; +typedef struct { + ty_Hints hints; double ticrate; double prev_tic_ts; bool down[TY_BTN_COUNT]; bool pressed[TY_BTN_COUNT]; -}; +} ty_Ctx; -extern struct ty_ctx ctx; +extern ty_Ctx ctx; bool is_init(void); diff --git a/teensy/teensy_list.c b/teensy/teensy_list.c index 342af61..76de21a 100644 --- a/teensy/teensy_list.c +++ b/teensy/teensy_list.c @@ -5,7 +5,7 @@ void *ty_list_create(void) { - struct ty_list_header *header = ty_new(struct ty_list_header); + ty_List_Header *header = ty_new(ty_List_Header); header->cap = 0; header->len = 0; return (void*)(header + 1); diff --git a/teensy/teensy_list.h b/teensy/teensy_list.h index fb1a401..6265e6a 100644 --- a/teensy/teensy_list.h +++ b/teensy/teensy_list.h @@ -4,36 +4,36 @@ #include "teensy_common.h" #include "teensy_mem.h" -struct ty_list_header { +typedef struct { size_t cap; size_t len; -}; +} ty_List_Header; #define TY_LIST_MIN_CAP 8 #define TY_LIST_GROW_RATE 2 -#define ty_list_get_header(arr) ((struct ty_list_header*)arr - 1) +#define ty_list_get_header(arr) ((ty_List_Header*)arr - 1) #define ty_list_cap(arr) (ty_list_get_header(arr)->cap) #define ty_list_len(arr) (ty_list_get_header(arr)->len) -#define ty_list_reserve(arr, amt) \ - do { \ - struct ty_list_header *header = ty_list_get_header(arr); \ - if (amt > header->cap) { \ - header->cap = header->cap < TY_LIST_MIN_CAP \ - ? TY_LIST_MIN_CAP \ - : header->cap * TY_LIST_GROW_RATE; \ - header = ty_realloc( \ - header, \ - (sizeof(*(arr)) * amt) + sizeof(struct ty_list_header) \ - ); \ - (arr) = (void*)(header + 1); \ - } \ +#define ty_list_reserve(arr, amt) \ + do { \ + ty_List_Header *header = ty_list_get_header(arr); \ + if (amt > header->cap) { \ + header->cap = header->cap < TY_LIST_MIN_CAP \ + ? TY_LIST_MIN_CAP \ + : header->cap * TY_LIST_GROW_RATE; \ + header = ty_realloc( \ + header, \ + (sizeof(*(arr)) * amt) + sizeof(ty_List_Header) \ + ); \ + (arr) = (void*)(header + 1); \ + } \ } while (0) #define ty_list_append(arr, elem) \ do { \ - struct ty_list_header *header = ty_list_get_header(arr); \ + ty_List_Header *header = ty_list_get_header(arr); \ ty_list_reserve(arr, header->len + 1); \ (arr)[header->len++] = (elem); \ } while (0) diff --git a/teensy/teensy_mem.c b/teensy/teensy_mem.c index 62088b8..7e8dd58 100644 --- a/teensy/teensy_mem.c +++ b/teensy/teensy_mem.c @@ -14,16 +14,16 @@ uint8_t *next_temp_alloc = temp_arena; #define BACKTRACE_SIZE 16 -struct alloc { +typedef struct { void *ptr; size_t size; int backtrace_len; char **backtrace; -}; +} Alloc; int allocations_cap; int allocations_len; -struct alloc *allocations; +Alloc *allocations; #endif // TODO TEENSY_DEBUG @@ -32,7 +32,7 @@ void ty_init_mem(void) #ifdef TEENSY_DEBUG allocations_len = 0; allocations_cap = 8; - allocations = malloc(sizeof(struct alloc) * 8); + allocations = malloc(sizeof(Alloc) * 8); if (!allocations) ty_log_fatal(TY_ERR_MEM, "(%s) (track) ran out of memory", __func__); #endif // TEENSY_DEBUG @@ -43,7 +43,7 @@ void ty_deinit_mem(void) #ifdef TEENSY_DEBUG fprintf(stderr, "%d allocations leaked\n", allocations_len); for (int i = 0; i < allocations_len; i++) { - struct alloc *alloc = &allocations[i]; + Alloc *alloc = &allocations[i]; fprintf( stderr, "leaked %zu bytes at %p\n", @@ -87,7 +87,7 @@ void *ty_alloc(size_t size) #ifdef TEENSY_DEBUG // Log allocation - struct alloc alloc; + Alloc alloc; void *bt[BACKTRACE_SIZE]; int bt_len = backtrace(bt, BACKTRACE_SIZE); char **symbols = backtrace_symbols(bt, bt_len); @@ -130,7 +130,7 @@ void *ty_realloc(void *ptr, size_t new_size) #ifdef TEENSY_DEBUG for (int i = 0; i < allocations_len; i++) { - struct alloc *alloc = &allocations[i]; + Alloc *alloc = &allocations[i]; if (alloc->ptr == ptr) { alloc->ptr = new_ptr; alloc->size = new_size; @@ -148,7 +148,7 @@ void ty_free(void *ptr) bool double_free = true; for (int i = 0; i < allocations_len; i++) { - struct alloc *alloc = &allocations[i]; + Alloc *alloc = &allocations[i]; if (alloc->ptr == ptr) { free(alloc->backtrace); *alloc = allocations[allocations_len - 1]; diff --git a/teensy/teensy_platform.h b/teensy/teensy_platform.h index 2128c5e..5301385 100644 --- a/teensy/teensy_platform.h +++ b/teensy/teensy_platform.h @@ -6,14 +6,14 @@ // This platform functions are to be implemented by a platform library. -void ty_platform_init(struct ty_ctx *ctx); +void ty_platform_init(ty_Ctx *ctx); void ty_platform_deinit(void); -void ty_platform_frame(struct ty_image img); +void ty_platform_frame(ty_Image img); bool ty_platform_os_wants_quit(void); double ty_platform_get_time(void); -bool ty_platform_is_button_down(enum ty_button btn); +bool ty_platform_is_button_down(ty_Button btn); // Optional, no editor if not implemented -struct ty_vec2i ty_platform_get_mouse(void); +ty_Vec2i ty_platform_get_mouse(void); #endif // TEENSY_PLATFORM_H_ diff --git a/teensy/teensy_renderer.c b/teensy/teensy_renderer.c index d457ef6..173fbed 100644 --- a/teensy/teensy_renderer.c +++ b/teensy/teensy_renderer.c @@ -9,8 +9,9 @@ #include "teensy_list.h" #define BLEND_COLOR ty_color(255, 0, 255) +#define screenspace() ((ty_Recti){0, 0, r.target.width, r.target.height}) -struct ty_renderer r; +ty_Renderer r; static bool is_renderer_init(void) @@ -19,7 +20,7 @@ bool is_renderer_init(void) } static -void image_bounds_check(struct ty_image img, struct ty_vec2i pos) +void image_bounds_check(ty_Image img, ty_Vec2i pos) { #ifdef TEENSY_DEBUG if ( @@ -58,14 +59,14 @@ void ty_deinit_renderer(void) ty_free_image(r.screen); } -struct ty_image ty_create_image(int w, int h, const struct ty_color* data) +ty_Image ty_create_image(int w, int h, const ty_Color* data) { - struct ty_image img; + ty_Image img; img.width = w; img.height = h; - size_t size = sizeof(struct ty_color) * w * h; + size_t size = sizeof(ty_Color) * w * h; img.data = ty_alloc(size); if (data) memcpy(img.data, data, size); @@ -75,21 +76,21 @@ struct ty_image ty_create_image(int w, int h, const struct ty_color* data) return img; } -void ty_free_image(struct ty_image img) +void ty_free_image(ty_Image img) { ty_free(img.data); } -struct ty_color ty_img_get_pixel(struct ty_image img, struct ty_vec2i pos) +ty_Color ty_img_get_pixel(ty_Image img, ty_Vec2i pos) { image_bounds_check(img, pos); return img.data[pos.y * img.width + pos.x]; } void ty_img_set_pixel( - struct ty_image img, - struct ty_vec2i pos, - struct ty_color color + ty_Image img, + ty_Vec2i pos, + ty_Color color ) { image_bounds_check(img, pos); img.data[pos.y * img.width + pos.x] = color; @@ -97,12 +98,12 @@ void ty_img_set_pixel( // This function technically is not needed, but I may want unicode support in // the future, when an abstraction like this is needed. -void ty_font_add_glyph(struct ty_font *font, uint8_t c, struct ty_image img) +void ty_font_add_glyph(ty_Font *font, uint8_t c, ty_Image img) { font->glyphs[c] = img; } -int ty_font_width(struct ty_font *font, const char *fmt, ...) +int ty_font_width(ty_Font *font, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -120,7 +121,7 @@ int ty_font_width(struct ty_font *font, const char *fmt, ...) return width; } -void ty_draw_set_target(const struct ty_image *img) +void ty_draw_set_target(const ty_Image *img) { if (!img) { r.target = r.screen; @@ -129,7 +130,7 @@ void ty_draw_set_target(const struct ty_image *img) memcpy(&r.target, img, sizeof(r.target)); } -void ty_draw_clear(struct ty_color col) +void ty_draw_clear(ty_Color col) { assert(is_renderer_init()); for (int i = 0; i < r.target.width * r.target.height; i++) { @@ -137,18 +138,27 @@ void ty_draw_clear(struct ty_color col) } } -void ty_draw_image(struct ty_image img, struct ty_vec2i pos) +static +ty_Vec2i clamp_point(ty_Vec2i p, ty_Recti rect) +{ + p.x = fmin(fmax(p.x, rect.x), rect.w); + p.y = fmin(fmax(p.y, rect.y), rect.h); + return p; +} + +void ty_draw_image(ty_Image img, ty_Vec2i pos) { - int x1 = fmin(fmax(pos.x, 0), r.target.width); - int y1 = fmin(fmax(pos.y, 0), r.target.height); - int x2 = fmin(fmax(pos.x + img.width, 0), r.target.width); - int y2 = fmin(fmax(pos.y + img.height, 0), r.target.height); - - for (int dy = y1; dy < y2; dy++) { - for (int dx = x1; dx < x2; dx++) { - struct ty_color px = ty_img_get_pixel( + ty_Vec2i p1 = clamp_point(ty_vec2i(pos.x, pos.y), screenspace()); + ty_Vec2i p2 = clamp_point( + ty_vec2i(pos.x + img.width, pos.y + img.height), + screenspace() + ); + + for (int dy = p1.y; dy < p2.y; dy++) { + for (int dx = p1.x; dx < p2.x; dx++) { + ty_Color px = ty_img_get_pixel( img, - ty_vec2i(dx - x1, dy - y1) + ty_vec2i(dx - p1.x, dy - p1.y) ); if (memcmp(&px, &BLEND_COLOR, sizeof(px)) == 0) continue; @@ -162,20 +172,18 @@ void ty_draw_image(struct ty_image img, struct ty_vec2i pos) } void ty_draw_image_ex( - struct ty_image img, - struct ty_recti src, - struct ty_recti dst + ty_Image img, + ty_Recti src, + ty_Recti dst ) { - int x1 = fmin(fmax(dst.x, 0), r.target.width); - int y1 = fmin(fmax(dst.y, 0), r.target.height); - int x2 = fmin(fmax(dst.x + dst.w, 0), r.target.width); - int y2 = fmin(fmax(dst.y + dst.h, 0), r.target.height); - - for (int dy = y1; dy < y2; dy++) { - for (int dx = x1; dx < x2; dx++) { - int img_x = ((dx - x1) * src.w) / dst.w + src.x; - int img_y = ((dy - y1) * src.h) / dst.h + src.y; - struct ty_color px = ty_img_get_pixel(img, ty_vec2i(img_x, img_y)); + ty_Vec2i p1 = clamp_point(ty_recti_start(dst), screenspace()); + ty_Vec2i p2 = clamp_point(ty_recti_end(dst), screenspace()); + + for (int dy = p1.y; dy < p2.y; dy++) { + for (int dx = p1.x; dx < p2.x; dx++) { + int img_x = ((dx - p1.x) * src.w) / dst.w + src.x; + int img_y = ((dy - p1.y) * src.h) / dst.h + src.y; + ty_Color px = ty_img_get_pixel(img, ty_vec2i(img_x, img_y)); if (memcmp(&px, &BLEND_COLOR, sizeof(px)) == 0) continue; @@ -188,17 +196,10 @@ void ty_draw_image_ex( } } -struct ty_vec2i clamp_point(struct ty_vec2i p, struct ty_recti rect) -{ - p.x = fmin(fmax(p.x, rect.x), rect.w); - p.y = fmin(fmax(p.y, rect.y), rect.h); - return p; -} - void ty_draw_image_rot( - struct ty_image img, - struct ty_vec2i pos, - struct ty_vec2i offset, + ty_Image img, + ty_Vec2i pos, + ty_Vec2i offset, double rot ) { double s = sin(rot); @@ -219,22 +220,19 @@ void ty_draw_image_rot( if (dx < 0 || dx >= img.width || dy < 0 || dy >= img.height) continue; - struct ty_color px = ty_img_get_pixel(img, ty_vec2i(dx, dy)); + ty_Color px = ty_img_get_pixel(img, ty_vec2i(dx, dy)); ty_img_set_pixel(r.target, ty_vec2i(pos.x + img_x, pos.y + img_y), px); } } } -void ty_draw_rect(struct ty_recti rect, struct ty_color color) +void ty_draw_rect(ty_Recti rect, ty_Color color) { - int x1 = fmin(fmax(rect.x, 0), r.target.width); - int y1 = fmin(fmax(rect.y, 0), r.target.height); - - int x2 = fmin(fmax(rect.x + rect.w, 0), r.target.width); - int y2 = fmin(fmax(rect.y + rect.h, 0), r.target.height); + ty_Vec2i p1 = clamp_point(ty_vec2i(rect.x, rect.y), screenspace()); + ty_Vec2i p2 = clamp_point(ty_recti_end(rect), screenspace()); - for (int dy = y1; dy < y2; dy++) { - for (int dx = x1; dx < x2; dx++) { + for (int dy = p1.y; dy < p2.y; dy++) { + for (int dx = p1.x; dx < p2.x; dx++) { ty_img_set_pixel( r.target, ty_vec2i(dx, dy), @@ -245,9 +243,9 @@ void ty_draw_rect(struct ty_recti rect, struct ty_color color) } void ty_draw_line( - struct ty_vec2i start, - struct ty_vec2i end, - struct ty_color color + ty_Vec2i start, + ty_Vec2i end, + ty_Color color ) { int dx = end.x - start.x; int dy = end.y - start.y; @@ -295,18 +293,18 @@ void ty_draw_line( } } -void ty_draw_text(struct ty_font *font, struct ty_vec2i pos, const char *text) +void ty_draw_text(ty_Font *font, ty_Vec2i pos, const char *text) { for (const uint8_t *c = (const uint8_t*)text; *c != '\0'; c++) { - struct ty_image glyph = font->glyphs[*c]; + ty_Image glyph = font->glyphs[*c]; ty_draw_image(glyph, pos); pos.x += glyph.width; } } void ty_draw_text_fmt( - struct ty_font *font, - struct ty_vec2i pos, + ty_Font *font, + ty_Vec2i pos, const char *fmt, ... ) { diff --git a/teensy/teensy_renderer.h b/teensy/teensy_renderer.h index d436619..ebd5d6e 100644 --- a/teensy/teensy_renderer.h +++ b/teensy/teensy_renderer.h @@ -4,10 +4,12 @@ #include "teensy_common.h" #include "teensy.h" -struct ty_renderer { - struct ty_image screen; - struct ty_image target; -}; +typedef struct { + ty_Image screen; + ty_Image target; +} ty_Renderer; + +extern ty_Renderer r; void ty_init_renderer(void); void ty_deinit_renderer(void); -- cgit v1.3-2-g0d8e