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

View File

@@ -1,27 +0,0 @@
#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

File diff suppressed because it is too large Load Diff

View File

@@ -1,22 +0,0 @@
#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

View File

@@ -1,64 +0,0 @@
#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

View File

@@ -2,9 +2,30 @@
#include "color.h"
#include "font_pack.h"
#include "io.h"
#include "String.h"
static void delay_1s(void)
{
volatile unsigned long count = 80000000; // Example for ~1 sec at ~80 MHz CPU
while (count--)
{
__asm__ volatile("nop");
}
}
void delay_seconds(unsigned int seconds)
{
for (unsigned int i = 0; i < seconds; i++)
{
delay_1s();
}
}
int kernel_main(void)
{
String str;
cs_init(&str);
cs_set(&str, "HELLO MARIO");
uart_io.puts("Initializing renderer...\n");
Renderer renderer;
@@ -23,48 +44,38 @@ int kernel_main(void)
uart_io.putc('\n');
RenderObject background = {0, 0, renderer.width, renderer.height, COLOR_RED};
RenderObject blue_rect = {100, 100, 100, 100, COLOR_BLUE};
RenderText text = {
.base = {
.x = 20,
.y = 30,
.color = 0xFFFFFFFF},
.text = "HELLO MARIO",
.text = str.buffer,
.font = &basic_font_pack,
};
uart_io.puts("Use WASD keys to move the rectangle.\n");
// Initial draw
renderer.clear_screen(&renderer, background.color);
renderer.draw_rect(&renderer, &blue_rect);
renderer.draw_text(&renderer, &text);
while (1)
{
char c = uart_io.getc();
uart_io.putc(c);
// Movement
if ((c == 'w' || c == 'W') && blue_rect.y > 0)
blue_rect.y = (blue_rect.y > 10) ? blue_rect.y - 10 : 0;
if ((c == 's' || c == 'S') && (blue_rect.y + blue_rect.height) < renderer.height)
blue_rect.y += 10;
if ((c == 'a' || c == 'A') && blue_rect.x > 0)
blue_rect.x = (blue_rect.x > 10) ? blue_rect.x - 10 : 0;
if ((c == 'd' || c == 'D') && (blue_rect.x + blue_rect.width) < renderer.width)
blue_rect.x += 10;
// Redraw everything
renderer.clear_screen(&renderer, background.color);
renderer.draw_rect(&renderer, &blue_rect);
renderer.draw_text(&renderer, &text);
// Print updated position
uart_io.puts("\nRect X: ");
uart_io.print_int(blue_rect.x);
uart_io.puts(" Y: ");
uart_io.print_int(blue_rect.y);
uart_io.putc('\n');
char c = uart_getc();
if (c >= 32 && c <= 126) // Printable ASCII
{
cs_append_char(&str, c);
}
else if (c == '\b' || c == 127) // Backspace support
{
if (str.length > 0)
{
str.length--;
str.buffer[str.length] = '\0';
}
}
uart_puts(str.buffer);
renderer.draw_text(&renderer, &text);
delay_seconds(4);
}
}

View File

@@ -1,59 +0,0 @@
#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