Compare commits

..

2 Commits

Author SHA1 Message Date
3fb220ff54 tim_draw_lot -> tim_fill 2026-01-12 22:57:31 +05:00
7a3bde6321 removed malloc from TimEditState_construct 2026-01-12 22:51:33 +05:00
6 changed files with 20 additions and 22 deletions

View File

@@ -119,7 +119,7 @@ i32 main(void) {
// draw every 10 ms
while (tim_run(60)) {
TimCell bg = tim_cell(" ", 0, BG);
tim_draw_lot(bg, 0, 0, tim->w, tim->h);
tim_fill(bg, 0, 0, tim->w, tim->h);
if (snek.state == RUN) {
game();

View File

@@ -301,14 +301,10 @@ bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, TimStyle
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style);
/// @param e uninitialized state
/// @param capacity in bytes
/// @param buffer an array
/// @param capacity buffer size in bytes
/// @param initial_content may be NULL
void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content);
static inline void TimEditState_destroy(TimEditState* e) {
if(!e) return;
free(e->s);
}
void TimEditState_construct(TimEditState* e, char* buffer, i32 capacity, cstr initial_content);
void TimEditState_insert(TimEditState* e, cstr s);
void TimEditState_delete(TimEditState* e);
@@ -364,8 +360,8 @@ void tim_draw_row(TimCell cell, i32 x, i32 y, i32 w);
// draw column of cells
void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h);
// fill lot (area) of cells
void tim_draw_lot(TimCell cell, i32 x, i32 y, i32 w, i32 h);
// fill area with cells
void tim_fill(TimCell cell, i32 x, i32 y, i32 w, i32 h);
// draw string to line, tags potential wide characters
void tim_draw_str(cstr s, i32 x, i32 y, i32 w, u8 fg, u8 bg);

View File

@@ -36,7 +36,7 @@ void tim_draw_col(TimCell cell, i32 x, i32 y, i32 h) {
}
}
void tim_draw_lot(TimCell cell, i32 x, i32 y, i32 w, i32 h) {
void tim_fill(TimCell cell, i32 x, i32 y, i32 w, i32 h) {
if (w > 0 && h > 0) {
for (i32 iy = MAX(y, 0); iy < MIN(y + h, tim->h); iy++) {
for (i32 ix = MAX(x, 0); ix < MIN(x + w, tim->w); ix++) {
@@ -71,7 +71,7 @@ void tim_draw_box(i32 x, i32 y, i32 w, i32 h, u8 fg, u8 bg) {
tim_draw_row(tim_cell("", fg, bg), x + 1 , y + h - 1, w - 2);
tim_draw_col(tim_cell("", fg, bg), x , y + 1 , h - 2);
tim_draw_col(tim_cell("", fg, bg), x + w - 1, y + 1 , h - 2);
tim_draw_lot(tim_cell(" ", fg, bg), x + 1 , y + 1 , w - 2, h - 2);
tim_fill(tim_cell(" ", fg, bg), x + 1 , y + 1 , w - 2, h - 2);
}
void tim_draw_invert(i32 x, i32 y, i32 w) {

View File

@@ -1,13 +1,16 @@
#include "tim.h"
void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content){
void TimEditState_construct(TimEditState* e, char* buf, i32 capacity, cstr initial_content){
e->masked = false;
e->length = initial_content ? tim_utf8_len(initial_content) : 0;
e->cursor = e->length;
e->capacity = capacity;
e->s = (char*)malloc(capacity + 1);
i32 byte_len = strlen(initial_content);
memcpy(e->s, initial_content, byte_len);
e->s = buf;
i32 byte_len = 0;
if(e->length > 0){
byte_len = strlen(initial_content);
memcpy(e->s, initial_content, byte_len);
}
e->s[byte_len] = 0;
}

View File

@@ -14,7 +14,7 @@ void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, TimStyle style) {
h = (h == A) ? t.lines : h;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
TimCell c = tim_cell(" ", style.fg, style.bg);
tim_draw_lot(c, r.x, r.y, r.w, r.h);
tim_fill(c, r.x, r.y, r.w, r.h);
TimLine l = {.s = s, .line = ""};
for (i32 i = 0; tim_next_line(&l); i++) {
tim_draw_str(l.line, r.x, r.y + i, l.width, c.fg, c.bg);

View File

@@ -126,8 +126,10 @@ static inline void test_screen(TimEvent* e) {
}
i32 main(void) {
TimEditState_construct(&ed1, 32, "Edit 1");
TimEditState_construct(&ed2, 32, "");
char ed1_buf[32];
char ed2_buf[32];
TimEditState_construct(&ed1, ed1_buf, ARRAY_SIZE(ed1_buf), "Edit 1");
TimEditState_construct(&ed2, ed2_buf, ARRAY_SIZE(ed2_buf), NULL);
while (tim_run(1.5)) {
test_screen(&tim->event);
@@ -135,9 +137,6 @@ i32 main(void) {
break;
}
}
TimEditState_destroy(&ed1);
TimEditState_destroy(&ed2);
return 0;
}