restrcuct

This commit is contained in:
2025-11-11 11:09:32 -07:00
parent e148177bbd
commit 20c73b6ab4
13 changed files with 3864 additions and 490 deletions

62
include/String.h Normal file
View File

@@ -0,0 +1,62 @@
#define CUSTOM_STRING_MAX_LEN 64
typedef struct {
char buffer[CUSTOM_STRING_MAX_LEN];
int length; // current string length, excluding null terminator
} String;
// Initialize String to empty
void cs_init(String* cs)
{
cs->buffer[0] = '\0';
cs->length = 0;
}
// Set content from a C-string (truncates if needed)
void cs_set(String* cs, const char* src)
{
int i = 0;
while (src[i] != '\0' && i < (CUSTOM_STRING_MAX_LEN - 1))
{
cs->buffer[i] = src[i];
i++;
}
cs->buffer[i] = '\0';
cs->length = i;
}
// Compare two Strings for equality
int cs_equal(const String* a, const String* b)
{
if (a->length != b->length)
return 0;
for (int i = 0; i < a->length; i++)
{
if (a->buffer[i] != b->buffer[i])
return 0;
}
return 1;
}
// Check if String starts with another String prefix
int cs_starts_with(const String* cs, const String* prefix)
{
if (prefix->length > cs->length)
return 0;
for (int i = 0; i < prefix->length; i++)
{
if (cs->buffer[i] != prefix->buffer[i])
return 0;
}
return 1;
}
// Append a char to String (ignores if full)
void cs_append_char(String* cs, char c)
{
if (cs->length < (CUSTOM_STRING_MAX_LEN - 1))
{
cs->buffer[cs->length++] = c;
cs->buffer[cs->length] = '\0';
}
}

1882
include/bitmapfont.h Normal file

File diff suppressed because it is too large Load Diff

27
include/color.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef COLOR_H
#define COLOR_H
// Color format: 0xAABBGGRR (BGRA32 - little endian)
#define COLOR_BLACK 0xFF000000
#define COLOR_WHITE 0xFFFFFFFF
#define COLOR_RED 0xFF0000FF
#define COLOR_GREEN 0xFF00FF00
#define COLOR_BLUE 0xFFFF0000
#define COLOR_YELLOW 0xFF00FFFF
#define COLOR_CYAN 0xFFFFFF00
#define COLOR_MAGENTA 0xFFFF00FF
#define COLOR_GRAY 0xFF808080
#define COLOR_ORANGE 0xFF00A5FF
#define COLOR_BROWN 0xFF2A2AA5
#define COLOR_PURPLE 0xFF800080
#define COLOR_PINK 0xFFCBC0FF
#define COLOR_LIME 0xFF00FFBF
#define COLOR_NAVY 0xFF800000
#define COLOR_TEAL 0xFF808000
#define COLOR_OLIVE 0xFF008080
// For custom BGR colors
#define BGR(r, g, b) (0xFF000000 | ((b) << 16) | ((g) << 8) | (r))
#endif // COLOR_H

23
include/font_pack.h Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#ifndef FONT_PACK_H
#define FONT_PACK_H
#include <stdint.h>
#define FONT_WIDTH 16
#define FONT_HEIGHT 16
#define FONT_BITMAP_SIZE ((FONT_WIDTH * FONT_HEIGHT) / 8) // 32 bytes per character
static const uint8_t empty_char_16x16[32] = {0};
typedef struct {
const uint8_t *bitmaps[26]; // Pointers to each 16x16 bitmap
} FontPack;
extern const FontPack basic_font_pack;
static inline const uint8_t *font_get_char_bitmap(const FontPack *pack, char c) {
if (c >= 'A' && c <= 'Z')
return pack->bitmaps[c - 'A'];
return empty_char_16x16; // Or point to empty_char_16x16
}
#endif // FONT_PACK_H

64
include/io.h Normal file
View File

@@ -0,0 +1,64 @@
#ifndef UART_IO_H
#define UART_IO_H
#include <stdint.h>
#define UART0_BASE 0x3F201000
#define UART0_DR (*((volatile uint32_t *)(UART0_BASE + 0x00)))
#define UART0_FR (*((volatile uint32_t *)(UART0_BASE + 0x18)))
typedef struct UART_IO {
void (*putc)(char c);
void (*puts)(const char* str);
char (*getc)(void);
void (*print_int)(unsigned int value);
} UART_IO;
// Function implementations
static void uart_putc(char c)
{
while (UART0_FR & (1 << 5)) { } // Wait while TXFF is set
UART0_DR = c;
}
static void uart_puts(const char* str)
{
while (*str)
uart_putc(*str++);
}
static char uart_getc(void)
{
while (UART0_FR & (1 << 4)) { } // Wait while RXFE is set
return (char)(UART0_DR & 0xFF);
}
static void uart_print_int(unsigned int value)
{
char buffer[10];
int i = 0;
if (value == 0) {
uart_putc('0');
return;
}
while (value > 0 && i < 10) {
buffer[i++] = (value % 10) + '0';
value /= 10;
}
while (i--)
uart_putc(buffer[i]);
}
// UART_IO instance with function pointers assigned
static const UART_IO uart_io = {
.putc = uart_putc,
.puts = uart_puts,
.getc = uart_getc,
.print_int = uart_print_int,
};
#endif // UART_IO_H

59
include/renderer.h Normal file
View File

@@ -0,0 +1,59 @@
#ifndef RENDERER_H
#define RENDERER_H
#include <stddef.h>
#include <stdbool.h>
#include "font_pack.h"
typedef struct RenderObject {
unsigned int x, y;
unsigned int width, height;
unsigned int color;
} RenderObject;
typedef struct RenderText {
RenderObject base; // Position, size, color
const char *text; // String to render
const FontPack *font; // Font used for rendering
} RenderText;
typedef struct Renderer {
volatile unsigned int *fb;
unsigned int width;
unsigned int height;
unsigned int pitch;
void (*clear_screen)(struct Renderer *renderer, unsigned int color);
void (*draw_rect)(struct Renderer *renderer, RenderObject *obj);
void (*draw_text)(struct Renderer *renderer, RenderText *text);
void (*draw_objects)(struct Renderer *renderer, RenderObject *objs[], unsigned int count);
} Renderer;
// ===== Window definition =====
typedef struct Window {
RenderObject frame; // Outer frame of the window
RenderObject content_area; // Area inside window (excluding titlebar)
RenderText title_bar; // Title bar text
unsigned int background_color; // Background fill color for content
bool visible; // Visibility flag
} Window;
// Function to initialize a Window
void window_init(Window *win,
unsigned int x, unsigned int y,
unsigned int width, unsigned int height,
const char *title, const FontPack *font,
unsigned int title_color, unsigned int bg_color,
unsigned int text_color);
// Function to render the Window
void window_draw(Renderer *renderer, Window *win);
// ===== Renderer functions =====
bool renderer_init(Renderer *renderer);
void clear_screen_impl(Renderer *renderer, unsigned int color);
void draw_rect_impl(Renderer *renderer, RenderObject *obj);
void draw_text_impl(Renderer *renderer, RenderText *text);
void draw_objects_impl(Renderer *renderer, RenderObject *objs[], unsigned int count);
#endif // RENDERER_H

46
include/renderer_common.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef RENDERER_COMMON_H
#define RENDERER_COMMON_H
#include <stdbool.h>
#include <stdint.h>
#define FONT_WIDTH 16
#define FONT_HEIGHT 16
typedef struct {
unsigned int x, y;
unsigned int color;
} RenderBase;
typedef struct {
RenderBase base;
const char *text;
void *font; // Opaque font pointer
} RenderText;
typedef struct {
unsigned int x, y;
unsigned int width, height;
unsigned int color;
} RenderObject;
typedef struct Renderer Renderer;
struct Renderer {
volatile unsigned int *fb;
unsigned int width;
unsigned int height;
unsigned int pitch;
bool (*init)(Renderer *renderer);
void (*clear_screen)(Renderer *renderer, unsigned int color);
void (*draw_text)(Renderer *renderer, RenderText *text_obj);
};
/**
* Initializes the framebuffer (common across both renderers)
*/
bool renderer_init_common(Renderer *renderer);
#endif // RENDERER_COMMON_H