#include #include #include #include #include #define QOI_IMPLEMENTATION #include "third/qoi.h" #define SCREEN_WIDTH 256 #define SCREEN_HEIGHT 256 #define TILE_SIZE 16 ty_Image img; ty_Font font = {}; tyui_Id winid = 0; double last_frame = 0; bool toggled = false; void tick(void) { ty_draw_clear(TY_COLOR_BLACK); ty_draw_image( img, ty_vec2i( SCREEN_WIDTH*0.5 + sin(ty_get_time()) * 50 - (img.width*0.5), SCREEN_HEIGHT*0.5 - (img.height*0.5) ) ); ty_draw_image_ex( img, ty_recti( 0, 0, img.width, img.height ), ty_recti( sin(-ty_get_time()) * 50, SCREEN_HEIGHT / 2, 32, 64 ) ); ty_draw_image_rot( img, ty_vec2i(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), ty_vec2i(-img.width/2, -img.height/2), ty_get_time() * TY_PI / 2 ); ty_draw_text(&font, ty_vec2i(5, SCREEN_HEIGHT - 5 - 8), "hello, world!"); if (ty_button_pressed(TY_BTN_LEFT_UP)) { toggled = !toggled; } if (toggled) { ty_draw_image(img, ty_vec2i(200, 200)); } double frame_time = ty_get_time() - last_frame; last_frame = ty_get_time(); tyui_layout((float[]){0.25, 0}); tyui_text("%.2g ms", frame_time * 1000); tyui_text("%.2d fps", (int)(1.0 / frame_time)); tyui_layout(NULL); tyui_text("%d", (int)ty_get_time()); tyui_text("abcdefghijklmnopqrstuvwxyz"); tyui_text("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); tyui_text("0123456789"); tyui_text("!\"'#$%&()*+,-./@[]\\"); tyui_text("^_`{}|~:;<>=? "); if (tyui_begin_window("Test Window", ty_recti(5, 5, 100, 100), &winid)) { for (int i = 0; i < 3; i++) tyui_text("example text"); if (tyui_button("press me")) toggled = !toggled; tyui_layout((float[]){0.1, 0.2, 0}); static float xvalue = 10; tyui_text("x"); tyui_text("%g", xvalue); tyui_slider(0, 100, 5, &xvalue); static float yvalue = 50; tyui_text("y"); tyui_text("%g", yvalue); tyui_slider(0, 100, 5, &yvalue); tyui_layout(NULL); tyui_end_window(); } tyui_draw(); ty_Vec2i mouse_pos = ty_mouse_pos(); ty_draw_rect(ty_recti(mouse_pos.x, mouse_pos.y, 1, 1), TY_COLOR_RED); ty_draw_end(); } 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"); ty_Image img = ty_create_image(desc.width, desc.height, data); free(data); return img; } int main(void) { ty_Hints hints = { .scr_width = SCREEN_WIDTH, .scr_height = SCREEN_HEIGHT, .game_title = "Demonchime", .ticrate = 60, }; ty_init(hints); tyui_init(&font); img = load_qoi_image("test_img.qoi"); char font_chars[] = "abcdefghijklmnopqrstuvwxyz0123456789" "!\"'#$%&()*+,-./@[]\\^_`{}|~:;<>=? " ; ty_Image font_img = load_qoi_image("font.qoi"); int glyph_height = 8; for (int i = 0; font_chars[i] != '\0'; i++) { ty_Image glyph = ty_create_image( font_img.width, glyph_height, font_img.data + font_img.width * glyph_height * i ); uint8_t c = *(uint8_t*)&font_chars[i]; ty_font_add_glyph(&font, c, glyph); if (c >= 'a' && c <= 'z') ty_font_add_glyph(&font, c - ('a' - 'A'), glyph); } while (ty_is_game_running()) { int ticks = ty_tick(); for (int i = 0; i < ticks; i++) tick(); ty_free_temp_allocs(); // ty_sleep(1000 / hints.ticrate); } ty_free_image(img); ty_free_image(font_img); for (int i = 0; i < CHAR_MAX; i++) { if (i >= 'a' && i <= 'z') continue; ty_Image glyph = font.glyphs[i]; if (glyph.width * glyph.height == 0) continue; ty_free_image(glyph); } tyui_deinit(); ty_deinit(); return 0; }