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 72a99ba
Showing
7 changed files
with
194 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,142 @@ | ||
#include "fenster.h" | ||
#include <math.h> | ||
|
||
#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} | ||
}; | ||
|
||
void fenster_background_gradient(struct fenster *f, uint64_t time_ms) { | ||
float t = time_ms / 100.0f; | ||
uint8_t r1 = (uint8_t)(128 + 64 * sin(t * 1.0)); | ||
uint8_t g1 = (uint8_t)(128 + 64 * sin(t * 1.2)); | ||
uint8_t b1 = (uint8_t)(128 + 64 * sin(t * 1.4)); | ||
uint8_t r2 = (uint8_t)(128 + 64 * sin(t * 1.5)); | ||
uint8_t g2 = (uint8_t)(128 + 64 * sin(t * 1.7)); | ||
uint8_t b2 = (uint8_t)(128 + 64 * sin(t * 1.9)); | ||
|
||
for (int y = 0; y < f->height; y++) { | ||
float blend = (float)y / f->height; | ||
uint8_t r = r1 + (r2 - r1) * blend; | ||
uint8_t g = g1 + (g2 - g1) * blend; | ||
uint8_t b = b1 + (b2 - b1) * blend; | ||
uint32_t line_color = (r << 16) | (g << 8) | b; | ||
size_t offset = y * f->width; | ||
vector_fill(&f->buf[offset], f->width, line_color); | ||
} | ||
} | ||
|
||
|
||
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_background_gradient(&f, f.lastsync); | ||
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 |
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