aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-12 16:41:37 -0400
committeriamcheeseman <[email protected]>2026-05-12 16:41:37 -0400
commit897a153d290ed4d3647ad9e0100d1b717f580b5f (patch)
tree844dee0b3b2b7d869cd8e1ba27ffb403e8d51d99
parentc1dd3ce9850fb2906aa5937b4374d1c0fc74ccf7 (diff)
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.
-rw-r--r--dc/dc.c24
-rw-r--r--platform/gl/gl.c22
-rw-r--r--platform/x11/x11.c12
-rw-r--r--teensy/teensy.h109
-rw-r--r--teensy/teensy_context.c15
-rw-r--r--teensy/teensy_context.h8
-rw-r--r--teensy/teensy_list.c2
-rw-r--r--teensy/teensy_list.h34
-rw-r--r--teensy/teensy_mem.c16
-rw-r--r--teensy/teensy_platform.h8
-rw-r--r--teensy/teensy_renderer.c120
-rw-r--r--teensy/teensy_renderer.h10
12 files changed, 194 insertions, 186 deletions
diff --git a/dc/dc.c b/dc/dc.c
index 99f9cf1..b90cca7 100644
--- a/dc/dc.c
+++ b/dc/dc.c
@@ -12,8 +12,8 @@
#define TILE_SIZE 16
-struct ty_image img;
-struct ty_font font = {};
+ty_Image img;
+ty_Font font = {};
double last_frame = 0;
@@ -57,11 +57,11 @@ void tick(void)
int y = 5;
ty_draw_rect(
- (struct ty_recti){
+ (ty_Recti){
5, 5,
ms_width, 8,
},
- TY_COLOR_BLACK
+ TY_COLOR_GRAY
);
ty_draw_text_fmt(
&font,
@@ -75,11 +75,11 @@ void tick(void)
int fps_width = ty_font_width(&font, fps_fmt, (int)(1.0 / frame_time));
ty_draw_rect(
- (struct ty_recti){
+ (ty_Recti){
5, 5 + 8,
fps_width, 8,
},
- TY_COLOR_BLACK
+ TY_COLOR_GRAY
);
ty_draw_text_fmt(
&font,
@@ -124,13 +124,13 @@ void tick(void)
}
-struct ty_image load_qoi_image(const char *path)
+ty_Image load_qoi_image(const char *path)
{
qoi_desc desc;
void *data = qoi_read(path, &desc, 3);
if (!data)
ty_log_fatal(TY_ERR_IO, "failed to read image");
- struct ty_image img = ty_create_image(desc.width, desc.height, data);
+ ty_Image img = ty_create_image(desc.width, desc.height, data);
free(data);
return img;
@@ -138,7 +138,7 @@ struct ty_image load_qoi_image(const char *path)
int main(void)
{
- struct ty_hints hints = {
+ ty_Hints hints = {
.scr_width = SCREEN_WIDTH,
.scr_height = SCREEN_HEIGHT,
.game_title = "Demonchime",
@@ -152,12 +152,12 @@ int main(void)
"abcdefghijklmnopqrstuvwxyz0123456789"
"!\"'#$%&()*+,-./@[]\\^_`{}|~:;<>=? "
;
- struct ty_image font_img = load_qoi_image("font.qoi");
+ ty_Image font_img = load_qoi_image("font.qoi");
int glyph_height = 8;
for (int i = 0; font_chars[i] != '\0'; i++) {
- struct ty_image glyph = ty_create_image(
+ ty_Image glyph = ty_create_image(
font_img.width,
glyph_height,
font_img.data + font_img.width * glyph_height * i
@@ -185,7 +185,7 @@ int main(void)
for (int i = 0; i < CHAR_MAX; i++) {
if (i >= 'a' && i <= 'z')
continue;
- struct ty_image glyph = font.glyphs[i];
+ ty_Image glyph = font.glyphs[i];
if (glyph.width * glyph.height == 0)
continue;
ty_free_image(glyph);
diff --git a/platform/gl/gl.c b/platform/gl/gl.c
index acfa0c5..30ea49d 100644
--- a/platform/gl/gl.c
+++ b/platform/gl/gl.c
@@ -36,16 +36,16 @@ static char frag[] =
"}\n"
;
-struct gl_platform {
+typedef struct {
GLFWwindow *win;
GLuint vao;
GLuint vbo;
GLuint program;
GLint uniform_tex_loc;
GLuint screen_handle;
-};
+} Gl_Platform;
-struct gl_platform p;
+Gl_Platform p;
static
GLuint compile_shader(GLenum type, const char *src)
@@ -65,7 +65,7 @@ GLuint compile_shader(GLenum type, const char *src)
return shader;
}
-void ty_platform_init(struct ty_ctx *ctx)
+void ty_platform_init(ty_Ctx *ctx)
{
ty_log_info("initializing GLFW...");
if (glfwInit() < 0)
@@ -92,7 +92,7 @@ void ty_platform_init(struct ty_ctx *ctx)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
ty_log_fatal(TY_PLATFORM_ERR, "could not init GLAD");
- struct ty_vec2 vertices[] = {
+ ty_Vec2 vertices[] = {
ty_vec2(-1, -1), ty_vec2(0, 1),
ty_vec2( 1, -1), ty_vec2(1, 1),
ty_vec2( 1, 1), ty_vec2(1, 0),
@@ -119,14 +119,14 @@ void ty_platform_init(struct ty_ctx *ctx)
0,
2, GL_FLOAT,
GL_FALSE,
- sizeof(struct ty_vec2) * 2, (void*)0
+ sizeof(ty_Vec2) * 2, (void*)0
);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
1,
2, GL_FLOAT,
GL_FALSE,
- sizeof(struct ty_vec2) * 2, (void*)sizeof(struct ty_vec2)
+ sizeof(ty_Vec2) * 2, (void*)sizeof(ty_Vec2)
);
glEnableVertexAttribArray(1);
@@ -167,7 +167,7 @@ void ty_platform_deinit(void)
glfwTerminate();
}
-void ty_platform_frame(struct ty_image img)
+void ty_platform_frame(ty_Image img)
{
glfwPollEvents();
@@ -216,7 +216,7 @@ double ty_platform_get_time(void)
return glfwGetTime();
}
-struct ty_vec2i ty_platform_get_mouse(void)
+ty_Vec2i ty_platform_get_mouse(void)
{
double x, y;
glfwGetCursorPos(p.win, &x, &y);
@@ -224,7 +224,7 @@ struct ty_vec2i ty_platform_get_mouse(void)
}
static
-int button_to_glfw_key(enum ty_button btn)
+int button_to_glfw_key(ty_Button btn)
{
switch (btn) {
case TY_BTN_LEFT_UP: return GLFW_KEY_W;
@@ -245,7 +245,7 @@ int button_to_glfw_key(enum ty_button btn)
return 0;
}
-bool ty_platform_is_button_down(enum ty_button btn)
+bool ty_platform_is_button_down(ty_Button btn)
{
return glfwGetKey(p.win, button_to_glfw_key(btn)) == GLFW_PRESS;
}
diff --git a/platform/x11/x11.c b/platform/x11/x11.c
index 2dde168..ad85677 100644
--- a/platform/x11/x11.c
+++ b/platform/x11/x11.c
@@ -17,7 +17,7 @@
#define SEC2NANO ((uint64_t)1000000000)
-struct x11_platform {
+typedef struct {
Display *dis;
Window root;
Window win;
@@ -28,9 +28,9 @@ struct x11_platform {
bool should_close;
uint64_t initial_time;
-};
+} X11_Platform;
-struct x11_platform p;
+X11_Platform p;
static
int err_handler(Display *dis, XErrorEvent *ev)
@@ -46,7 +46,7 @@ int err_handler(Display *dis, XErrorEvent *ev)
return 0;
}
-void ty_platform_init(struct ty_ctx *ctx)
+void ty_platform_init(ty_Ctx *ctx)
{
XSetErrorHandler(err_handler);
@@ -101,7 +101,7 @@ void ty_platform_deinit(void)
}
static
-void display_image(struct ty_image img)
+void display_image(ty_Image img)
{
size_t size = img.width * img.height;
// Don't use ty_alloc cause it will be owned and freed by Xlib
@@ -157,7 +157,7 @@ void handle_client_msg(XEvent ev)
p.should_close = true;
}
-void ty_platform_frame(struct ty_image img)
+void ty_platform_frame(ty_Image img)
{
XPending(p.dis);
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 <math.h>
#include <time.h>
#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);
+ 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 = y1; dy < y2; dy++) {
- for (int dx = x1; dx < x2; dx++) {
- struct ty_color px = ty_img_get_pixel(
+ 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);
+ ty_Vec2i p1 = clamp_point(ty_recti_start(dst), screenspace());
+ ty_Vec2i p2 = clamp_point(ty_recti_end(dst), screenspace());
- 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));
+ 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);