forked from zserge/fenster
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CookieClicker example fenster_cursor redundancy check
- Loading branch information
1 parent
e5e9fba
commit 80fcd75
Showing
8 changed files
with
175 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include "fenster.h" | ||
#include <math.h> | ||
|
||
struct AutoClicker { | ||
const char* name; | ||
__uint128_t cost, cps, owned; | ||
int unlocked; | ||
}; | ||
|
||
static struct AutoClicker autoclickers[] = { | ||
{"Cursor", 15, 1, 0, 0}, | ||
{"Grandma", 100, 5, 0, 0}, | ||
{"Farm", 1100, 25, 0, 0}, | ||
{"Mine", 12000, 100, 0, 0}, | ||
{"Factory", 130000, 500, 0, 0} | ||
}; | ||
|
||
int run() { | ||
struct fenster f = {.title = "Cookie Clicker", .width = 800, .height = 600}; | ||
fenster_open(&f); | ||
|
||
FensterFontList fonts = fenster_loadfontlist(); | ||
FensterFont* font = fenster_loadfont(fonts.paths[0]); | ||
|
||
__uint128_t cookies = 0, last_time = 0, cookies_per_sec = 0; | ||
float cookie_size = 60.f, cookie_grow_rate = 1.1f, cookie_max_size = 100.f; | ||
int cookie_growing = 0; | ||
|
||
while (fenster_loop(&f) == 0 && f.keys[27] == 0) { | ||
fenster_fill(&f, 0x2A2A2A); | ||
|
||
int hovering_cursor = 1; | ||
int cookie_x = f.width / 2, cookie_y = f.height / 2; | ||
|
||
if (cookie_growing) { | ||
cookie_size = fminf(cookie_size * cookie_grow_rate, cookie_max_size); | ||
} else { | ||
cookie_size = 60.f; | ||
} | ||
fenster_drawcirc(&f, cookie_x, cookie_y, (int)cookie_size, 0x8B4513); | ||
|
||
if (fenster_point_in_circle(f.mpos[0], f.mpos[1], cookie_x, cookie_y, (int)cookie_size)) { | ||
hovering_cursor = 2; | ||
if (f.mclick[0]) { | ||
cookies += 1 + (1*autoclickers[0].owned); | ||
cookie_growing = 0; | ||
} else { | ||
cookie_growing = 1; | ||
} | ||
} else { | ||
cookie_growing = 0; | ||
} | ||
|
||
int64_t current_time = f.lastsync; | ||
if (current_time - last_time >= 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 \\n Per Second: %llu", (unsigned long long)cookies, (unsigned long long)cookies_per_sec), 10, 30); | ||
|
||
int button_w = 200, button_h = 80, start_y = 100; | ||
for (int i = 0; i < 5; i++) { | ||
int x = 10, y = start_y + i * (button_h + 10); | ||
int button_color = 0x404040; | ||
|
||
if (cookies >= autoclickers[i].cost && !autoclickers[i].unlocked) { | ||
autoclickers[i].unlocked = 1; | ||
} | ||
|
||
if (autoclickers[i].unlocked) { | ||
if (cookies >= autoclickers[i].cost) { | ||
if (fenster_point_in_rect(f.mpos[0], f.mpos[1], x, y, button_w, button_h)) { | ||
button_color = 0x517554; | ||
hovering_cursor = 2; | ||
if (f.mclick[0]) { | ||
cookies -= autoclickers[i].cost; | ||
autoclickers[i].owned++; | ||
autoclickers[i].cost = autoclickers[i].cost * 115 / 100; | ||
} | ||
} else { | ||
button_color = 0x475c49; | ||
} | ||
} | ||
fenster_drawrec(&f, x, y, button_w, button_h, button_color); | ||
fenster_drawtext(&f, font, vsformat("\\c0xA9A9A9 %s\\n \\c0xFFFFFF Cost: %llu\\n Owned: %llu", autoclickers[i].name, | ||
(unsigned long long)autoclickers[i].cost, (unsigned long long)autoclickers[i].owned), x + 10, y + 10); | ||
} | ||
} | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CFLAGS ?= -mavx2 -lm -DUSE_FONTS -O3 -flto -Wall -Wextra -std=c99 | ||
DESTDIR ?= . | ||
|
||
ifeq ($(OS),Windows_NT) | ||
CC = gcc | ||
LDFLAGS = -mwindows -lgdi32 | ||
else | ||
UNAME_S := $(shell uname -s) | ||
ifeq ($(UNAME_S),Darwin) | ||
LDFLAGS = -framework Cocoa | ||
else | ||
LDFLAGS = -lX11 | ||
endif | ||
endif | ||
|
||
all: main | ||
|
||
main: main.c ../../src/fenster/fenster.h | ||
$(CC) main.c -I../../src/fenster/ -o $(DESTDIR)/$@ $(CFLAGS) $(LDFLAGS) | ||
|
||
clean: | ||
rm -f main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters