moved code from header to source files

This commit is contained in:
2026-01-09 05:04:35 +05:00
parent 50940e5190
commit 3f75902aa0
20 changed files with 1304 additions and 1207 deletions

View File

@@ -9,40 +9,40 @@
#define CYES 0xa000f // yes green, black, white
#define CNO 0x9000f // no red, black white
int main(int argc, char** argv) {
i32 main(i32 argc, char** argv) {
if (argc < 2 || strcmp(argv[1], "-h") == 0) {
printf("syntax: %s message\n", argv[0]);
exit(1);
}
// get text properties
TimText_t msg = scan_str(argv[1]);
TimText msg = tim_scan_str(argv[1]);
while (tim_run(0)) {
// calculate size of message box
int w = MAX(msg.width + 4, 24);
int h = MAX(msg.lines + 6, 7);
i32 w = MAX(msg.width + 4, 28);
i32 h = MAX(msg.lines + 6, 7);
scope (A, A, w, h) {
tim_scope(A, A, w, h) {
// draw frame around entire scope
frame(0, 0, ~0, ~0, CFR);
tim_frame(0, 0, ~0, ~0, CFR);
// draw message
label(argv[1], A, 1, msg.width, msg.lines, CTXT);
// draw 'yes' button, return 0 when clicked
if (button("Yes", 2, ~1, A, A, CYES)) {
if (button("[y] Yes", 2, ~1, A, A, CYES) || tim_is_key_press('y')) {
exit(0);
}
// draw 'no' button, return 1 when clicked
if (button("No ", ~2, ~1, A, A, CNO)) {
if (button("[n] No", ~2, ~1, A, A, CNO) || tim_is_key_press('n')) {
exit(1);
}
// return with 1 when q or esc is pressed
if (is_key_press('q') || is_key_press(TimKey_Escape)) {
exit(1);
if (tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) {
exit(2);
}
}
}

View File

@@ -1,14 +1,14 @@
#include "tim.h"
int main(void) {
i32 main(void) {
while (tim_run(0)) { // event loop
scope (A, A, 24, 8) { // centered 24x8 scope
uint64_t c = 0x0a060f; // three colors
frame(0, 0, ~0, ~0, c); // draw frame for scope
tim_scope(A, A, 24, 8) { // centered 24x8 scope
u64 c = 0x0a060f; // three colors
tim_frame(0, 0, ~0, ~0, c); // draw frame for scope
label("Greetings!", A, 2, A, A, c); // label in top center
if (button("OK", A, ~1, 8, A, c)) // button in bottom center
return 0; // exit on button click
if (is_key_press('q')) // ctrl-c is masked
if (tim_is_key_press('q')) // ctrl-c is masked
return 0; // exit on 'q' press
}
} //TODO: remove atexit cleanup

View File

@@ -13,16 +13,16 @@
typedef union {
struct {
int32_t x;
int32_t y;
i32 x;
i32 y;
};
int64_t xy;
i64 xy;
} point;
static struct {
int state; // game state (NEW RUN PAUSE OVER)
int64_t tick; // updates every 10 ms
int len; // snake length
i32 state; // game state (NEW RUN PAUSE OVER)
i64 tick; // updates every 10 ms
i32 len; // snake length
point body[200]; // snake body
point food; // food position
point look; // active direction
@@ -38,7 +38,7 @@ static void start(void) {
static void game(void) {
// update game state about every 10 ms
int64_t tick = time_us() / 100000;
i64 tick = tim_time_usec() / 100000;
if (snek.tick != tick) {
snek.tick = tick;
// move one unit
@@ -47,7 +47,7 @@ static void game(void) {
snek.body[0].y = snek.body[1].y + snek.look.y;
// self crash
bool crash = false;
for (int i = 1; i < snek.len; i++) {
for (i32 i = 1; i < snek.len; i++) {
crash |= snek.body[0].xy == snek.body[i].xy;
}
// border crash
@@ -56,7 +56,7 @@ static void game(void) {
snek.state = crash ? OVER : snek.state;
// food
if (snek.food.xy == snek.body[0].xy) {
snek.len = MIN(snek.len + 2, (int)ARRAY_SIZE(snek.body));
snek.len = MIN(snek.len + 2, (i32)ARRAY_SIZE(snek.body));
snek.food.x = rand() % (tim.w / 2 - 2) + 1;
snek.food.y = rand() % (tim.h - 2) + 1;
}
@@ -65,22 +65,22 @@ static void game(void) {
// draw
if (tim.event.type == TimEvent_Draw) {
// food
draw_chr(cell(" ", 0, 0xc5), snek.food.x * 2 + 0, snek.food.y);
draw_chr(cell(" ", 0, 0xc5), snek.food.x * 2 + 1, snek.food.y);
tim_draw_chr(tim_cell(" ", 0, 0xc5), snek.food.x * 2 + 0, snek.food.y);
tim_draw_chr(tim_cell(" ", 0, 0xc5), snek.food.x * 2 + 1, snek.food.y);
// snek
TimCell_t s = cell(" ", 0, 0);
for (int i = 0; i < snek.len; i++) {
TimCell s = tim_cell(" ", 0, 0);
for (i32 i = 0; i < snek.len; i++) {
s.bg = (i / 2) % 2 ? 0xe3 : 0xea;
int x = snek.body[i].x * 2;
int y = snek.body[i].y;
draw_chr(s, x + 0, y);
draw_chr(s, x + 1, y);
i32 x = snek.body[i].x * 2;
i32 y = snek.body[i].y;
tim_draw_chr(s, x + 0, y);
tim_draw_chr(s, x + 1, y);
}
}
// user input
if (tim.event.type == KEY_EVENT) {
int key = tim.event.key;
i32 key = tim.event.key;
if ((key == TimKey_Right || key == 'd') && snek.look.x != -1) {
snek.look = (point){{1, 0}};
} else if ((key == TimKey_Left || key == 'a') && snek.look.x != 1) {
@@ -90,31 +90,34 @@ static void game(void) {
} else if ((key == TimKey_Up || key == 'w') && snek.look.y != 1) {
snek.look = (point){{0, -1}};
}
else if (key == 'q' || key == TimKey_Escape){
snek.state = PAUSE;
}
}
}
static void menu(void) {
scope(A, A, 20, 13) {
tim_scope(A, A, 20, 13) {
char* lbl = snek.state == OVER ? "GAME OVER" : "SNEK - THE GAME";
char* btn = snek.state == PAUSE ? "Resume" : "Play";
label(lbl, A, 0, A, A, BTN);
if (button(btn, A, 2, 20, 5, BTN) || is_key_press(TimKey_Enter)) {
if (button(btn, A, 2, 20, 5, BTN) || tim_is_key_press(TimKey_Enter)) {
if (snek.state != PAUSE) {
start();
}
snek.state = RUN;
}
if (button("Exit", A, 8, 20, 5, BTN) || is_key_press(TimKey_Escape)) {
if (button("Exit", A, 8, 20, 5, BTN) || tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) {
exit(0);
}
}
}
int main(void) {
i32 main(void) {
// draw every 10 ms
while (tim_run(10)) {
TimCell_t bg = cell(" ", 0, BG);
draw_lot(bg, 0, 0, tim.w, tim.h);
while (tim_run(60)) {
TimCell bg = tim_cell(" ", 0, BG);
tim_draw_lot(bg, 0, 0, tim.w, tim.h);
if (snek.state == RUN) {
game();
@@ -122,8 +125,5 @@ int main(void) {
menu();
}
if (is_key_press(TimKey_Escape)) {
snek.state = PAUSE;
}
}
}