tint the board if a winning state is reached

This commit is contained in:
2026-02-20 01:36:48 +01:00
parent d87df74834
commit 6e5a4acdd0
4 changed files with 24 additions and 10 deletions

View File

@ -3,6 +3,7 @@
#include <raylib.h> #include <raylib.h>
// #define PRINT_TIMINGS
#define VERLET_UPDATE #define VERLET_UPDATE
// #define WEB // #define WEB

View File

@ -92,7 +92,8 @@ public:
const State &current) -> void; const State &current) -> void;
auto DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x, auto DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x,
int sel_y, int block_add_x, int block_add_y) -> void; int sel_y, int block_add_x, int block_add_y,
const WinCondition win_condition) -> void;
auto DrawMenu(const MassSpringSystem &masssprings, int current_preset, auto DrawMenu(const MassSpringSystem &masssprings, int current_preset,
const State &current_state) -> void; const State &current_state) -> void;

View File

@ -1,6 +1,4 @@
#include <chrono>
#include <iostream> #include <iostream>
#include <ratio>
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
@ -13,6 +11,10 @@
#ifndef WEB #ifndef WEB
#include <omp.h> #include <omp.h>
#endif #endif
#ifdef PRINT_TIMINGS
#include <chrono>
#include <ratio>
#endif
// TODO: Klotski state file loading // TODO: Klotski state file loading
// - File should contain a single state per line, multiple lines possible // - File should contain a single state per line, multiple lines possible
@ -110,12 +112,14 @@ auto main(int argc, char *argv[]) -> int {
int hov_y = 0; int hov_y = 0;
int sel_x = 0; int sel_x = 0;
int sel_y = 0; int sel_y = 0;
#ifdef PRINT_TIMINGS
double last_print_time = GetTime(); double last_print_time = GetTime();
std::chrono::duration<double, std::milli> physics_time_accumulator = std::chrono::duration<double, std::milli> physics_time_accumulator =
std::chrono::duration<double, std::milli>(0); std::chrono::duration<double, std::milli>(0);
std::chrono::duration<double, std::milli> render_time_accumulator = std::chrono::duration<double, std::milli> render_time_accumulator =
std::chrono::duration<double, std::milli>(0); std::chrono::duration<double, std::milli>(0);
int time_measure_count = 0; int time_measure_count = 0;
#endif
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
frametime = GetFrameTime(); frametime = GetFrameTime();
std::string previous_state = current_state.state; std::string previous_state = current_state.state;
@ -286,8 +290,10 @@ auto main(int argc, char *argv[]) -> int {
} }
// Physics update // Physics update
#ifdef PRINT_TIMINGS
std::chrono::high_resolution_clock::time_point ps = std::chrono::high_resolution_clock::time_point ps =
std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::now();
#endif
for (int i = 0; i < UPDATES_PER_FRAME; ++i) { for (int i = 0; i < UPDATES_PER_FRAME; ++i) {
masssprings.ClearForces(); masssprings.ClearForces();
masssprings.CalculateSpringForces(); masssprings.CalculateSpringForces();
@ -298,20 +304,25 @@ auto main(int argc, char *argv[]) -> int {
mass_springs.EulerUpdate(frametime * SIM_SPEED); mass_springs.EulerUpdate(frametime * SIM_SPEED);
#endif #endif
} }
#ifdef PRINT_TIMINGS
std::chrono::high_resolution_clock::time_point pe = std::chrono::high_resolution_clock::time_point pe =
std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::now();
physics_time_accumulator += pe - ps; physics_time_accumulator += pe - ps;
#endif
// Rendering // Rendering
#ifdef PRINT_TIMINGS
std::chrono::high_resolution_clock::time_point rs = std::chrono::high_resolution_clock::time_point rs =
std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::now();
#endif
renderer.UpdateCamera(masssprings, current_state); renderer.UpdateCamera(masssprings, current_state);
renderer.UpdateTextureSizes(); renderer.UpdateTextureSizes();
renderer.DrawMassSprings(masssprings, current_state); renderer.DrawMassSprings(masssprings, current_state);
renderer.DrawKlotski(current_state, hov_x, hov_y, sel_x, sel_y, block_add_x, renderer.DrawKlotski(current_state, hov_x, hov_y, sel_x, sel_y, block_add_x,
block_add_y); block_add_y, win_conditions[current_preset]);
renderer.DrawMenu(masssprings, current_preset, current_state); renderer.DrawMenu(masssprings, current_preset, current_state);
renderer.DrawTextures(); renderer.DrawTextures();
#ifdef PRINT_TIMINGS
std::chrono::high_resolution_clock::time_point re = std::chrono::high_resolution_clock::time_point re =
std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::now();
render_time_accumulator += re - rs; render_time_accumulator += re - rs;
@ -329,6 +340,7 @@ auto main(int argc, char *argv[]) -> int {
render_time_accumulator = std::chrono::duration<double, std::milli>(0); render_time_accumulator = std::chrono::duration<double, std::milli>(0);
time_measure_count = 0; time_measure_count = 0;
} }
#endif
} }
CloseWindow(); CloseWindow();

View File

@ -176,8 +176,8 @@ auto Renderer::DrawMassSprings(const MassSpringSystem &masssprings,
} }
auto Renderer::DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x, auto Renderer::DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x,
int sel_y, int block_add_x, int block_add_y) int sel_y, int block_add_x, int block_add_y,
-> void { const WinCondition win_condition) -> void {
BeginTextureMode(klotski_target); BeginTextureMode(klotski_target);
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
@ -194,10 +194,10 @@ auto Renderer::DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x,
DrawRectangle(0, 0, GetScreenWidth() / 2, GetScreenHeight() - MENU_HEIGHT, DrawRectangle(0, 0, GetScreenWidth() / 2, GetScreenHeight() - MENU_HEIGHT,
RAYWHITE); RAYWHITE);
DrawRectangle(x_offset, y_offset, DrawRectangle(
board_width - 2 * x_offset + 2 * BOARD_PADDING, x_offset, y_offset, board_width - 2 * x_offset + 2 * BOARD_PADDING,
board_height - 2 * y_offset + 2 * BOARD_PADDING, board_height - 2 * y_offset + 2 * BOARD_PADDING,
state.restricted ? DARKGRAY : LIGHTGRAY); win_condition(state) ? GREEN : (state.restricted ? DARKGRAY : LIGHTGRAY));
for (int x = 0; x < state.width; ++x) { for (int x = 0; x < state.width; ++x) {
for (int y = 0; y < state.height; ++y) { for (int y = 0; y < state.height; ++y) {
DrawRectangle(x_offset + BOARD_PADDING + x * BLOCK_PADDING * 2 + DrawRectangle(x_offset + BOARD_PADDING + x * BLOCK_PADDING * 2 +