From c451018c17ce4953a7011fce6cafa55e362e0005 Mon Sep 17 00:00:00 2001 From: kkard2 Date: Sat, 6 Sep 2025 15:59:51 +0200 Subject: rainbow --- examples/rainbow.c | 25 +++++++++++++++++++++++++ platform_win32.c | 23 ++++++++++++++++++++++- run.bat | 1 + sponge.h | 1 - 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 examples/rainbow.c diff --git a/examples/rainbow.c b/examples/rainbow.c new file mode 100644 index 0000000..33cbb2d --- /dev/null +++ b/examples/rainbow.c @@ -0,0 +1,25 @@ +#include +#include + + + +#include "../sponge.h" + +#define PI ((float)3.14159265358979323846) +#define SPEED (2.0f / 360.0f * 2 * PI) +static float angle = 0.0f; + +void draw_frame(sponge_Texture c) { + angle += SPEED; + + float rf = (sinf(angle) + 1.0f) * 255.0f / 2.0f; + float gf = (sinf(angle + (PI * 2.0f / 3.0f)) + 1.0f) * 255.0f / 2.0f; + float bf = (sinf(angle + (PI * 4.0f / 3.0f)) + 1.0f) * 255.0f / 2.0f; + + int r = (((int)rf) & 0xFF) << 16; + int g = (((int)gf) & 0xFF) << 8; + int b = (((int)bf) & 0xFF) << 0; + + uint32_t color = 0xFF000000 | r | g | b; + sponge_clear(c, color); +} diff --git a/platform_win32.c b/platform_win32.c index 1d7ff57..620b8a2 100644 --- a/platform_win32.c +++ b/platform_win32.c @@ -1,13 +1,16 @@ #define UNICODE #include #include +#include +#include + +#include #define SPONGE_IMPLEMENTATION #include "sponge.h" #include "example.h" -// TODO(kard): ofc some resizing stuff #define DEFAULT_WIDTH 256 #define DEFAULT_HEIGHT 256 @@ -36,6 +39,22 @@ int update_canvas(sponge_Texture *canvas, uint32_t new_width, uint32_t new_heigh bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; + return 1; +} + +// TODO(kard): MSVC complains about freopen (should be freopen_s apparently) +void redirect_io_to_console() { + AllocConsole(); + FILE *fp; + + fp = freopen("CONOUT$", "w", stdout); + setvbuf(stdout, NULL, _IONBF, 0); + + fp = freopen("CONOUT$", "w", stderr); + setvbuf(stderr, NULL, _IONBF, 0); + + fp = freopen("CONIN$", "r", stdin); + setvbuf(stdin, NULL, _IONBF, 0); } LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { @@ -61,6 +80,8 @@ int WinMain( LPSTR lpCmdLine, int nShowCmd ) { + redirect_io_to_console(); + const wchar_t CLASS_NAME[] = L"sponge"; WNDCLASS wc = { 0 }; diff --git a/run.bat b/run.bat index 913a542..0eb177d 100644 --- a/run.bat +++ b/run.bat @@ -1,3 +1,4 @@ del out.exe +del *.obj cl platform_win32.c "examples\%1" user32.lib gdi32.lib /W3 /Fe:out.exe out.exe diff --git a/sponge.h b/sponge.h index 6255236..2978e0e 100644 --- a/sponge.h +++ b/sponge.h @@ -10,7 +10,6 @@ typedef struct { void sponge_clear(sponge_Texture c, uint32_t color); void sponge_draw_rect(sponge_Texture c, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t color); -void sponge_draw_texture(sponge_Texture c, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, sponge_Texture texture); // TODO(kard): prefix stripping -- cgit v1.3.1