From 5e5689544a420917a0f14ff5d77859fe5f850bfa Mon Sep 17 00:00:00 2001 From: Cardeal Russo Date: Thu, 31 Oct 2024 21:21:35 -0300 Subject: [PATCH] Create cookieclicker --- examples/cookieclicker | 121 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 examples/cookieclicker diff --git a/examples/cookieclicker b/examples/cookieclicker new file mode 100644 index 0000000..3c64f8c --- /dev/null +++ b/examples/cookieclicker @@ -0,0 +1,121 @@ +#include "fenster.h" +#include + +#define COOKIE_BASE_SIZE 60 +#define PULSE_SPEED 2 +#define PULSE_AMOUNT 10 + +struct AutoClicker { + const char* name; + unsigned long long int cost; + unsigned long long int cps; + unsigned long long int owned; +}; + +static struct AutoClicker autoclickers[] = { + {"Cursor", 15, 1, 0}, + {"Grandma", 100, 5, 0}, + {"Farm", 1100, 25, 0}, + {"Mine", 12000, 100, 0}, + {"Factory", 130000, 500, 0} +}; + +static int run() { + struct fenster f = {.title = "Cookie Clicker", .width = 800, .height = 600}; + fenster_open(&f); + + const uint32_t BG_COLOR = 0x2A2A2A; + const uint32_t COOKIE_COLOR = 0x8B4513; + const uint32_t TEXT_COLOR = 0xFFFFFF; + const uint32_t BUTTON_COLOR = 0x404040; + const uint32_t HIGHLIGHT_COLOR = 0x606060; + + FensterFontList fonts = fenster_loadfontlist(); + FensterFont* font = fenster_loadfont(fonts.paths[0]); + + int pulse = 0; + unsigned long long int cookies = 0; + unsigned long long int last_time = 0; + unsigned long long int cookies_per_sec = 0; + + while (fenster_loop(&f) == 0 && f.keys[27] == 0) { + fenster_fill(&f, BG_COLOR); + int hovering_cursor = 1; + + pulse += PULSE_SPEED; + int current_size = COOKIE_BASE_SIZE + (int)(sin(pulse / 30.0f) * PULSE_AMOUNT); + + int cookie_x = f.width / 2; + int cookie_y = f.height / 2; + fenster_drawcirc(&f, cookie_x, cookie_y, current_size, COOKIE_COLOR); + + int dx = f.mpos[0] - cookie_x; + int dy = f.mpos[1] - cookie_y; + + if (dx * dx + dy * dy < current_size * current_size) { + hovering_cursor = 2; + if (f.mclick[0]) { + cookies++; + } + } + + unsigned long long int current_time = f.lastsync; + unsigned long long int dt = current_time - last_time; + if (dt >= 1000) { + cookies_per_sec = 0; + for (int i = 0; i < 5; i++) { + cookies_per_sec += autoclickers[i].owned * autoclickers[i].cps; + } + cookies += cookies_per_sec; + last_time = current_time; + } + + fenster_drawtext(&f, font, vsformat("Cookies: %llu", cookies), 10, 30); + fenster_drawtext(&f, font, vsformat("Per Second: %llu", cookies_per_sec), 10, 60); + + const int button_width = 200; + const int button_height = 80; + const int start_y = 100; + + for (int i = 0; i < 5; i++) { + int x = 10; + int y = start_y + i * (button_height + 10); + + int hover = f.mpos[0] >= x && f.mpos[0] <= x + button_width && + f.mpos[1] >= y && f.mpos[1] <= y + button_height; + + if (hover) { + hovering_cursor = 2; + } + + fenster_drawrec(&f, x, y, button_width, button_height, hover ? HIGHLIGHT_COLOR : BUTTON_COLOR); + + fenster_drawtext(&f, font, vsformat("%s\n Cost: %llu\n Owned: %llu", + autoclickers[i].name, autoclickers[i].cost, autoclickers[i].owned), + x + 10, y + 10); + + if (f.mclick[0] && hover && cookies >= autoclickers[i].cost) { + cookies -= autoclickers[i].cost; + autoclickers[i].owned++; + autoclickers[i].cost = autoclickers[i].cost * 115 / 100; + } + } + + fenster_cursor(&f, hovering_cursor); + fenster_sync(&f, 60); + } + + fenster_freefont(font); + fenster_freefontlist(&fonts); + fenster_close(&f); + return 0; +} + +#if defined(_WIN32) +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) { + (void)hInstance, (void)hPrevInstance, (void)pCmdLine, (void)nCmdShow; + return run(); +} +#else +int main() { return run(); } +#endif