render klotski board

This commit is contained in:
2026-02-17 13:27:12 +01:00
parent 017d11245c
commit 9d0afffb57
4 changed files with 101 additions and 11 deletions

View File

@ -1,5 +1,3 @@
#include "mass_springs.hpp"
#define VERLET_UPDATE
#include <iostream>
@ -7,6 +5,8 @@
#include <raymath.h>
#include "config.hpp"
#include "klotski.hpp"
#include "mass_springs.hpp"
#include "renderer.hpp"
auto main(int argc, char *argv[]) -> int {
@ -21,8 +21,7 @@ auto main(int argc, char *argv[]) -> int {
SetConfigFlags(FLAG_VSYNC_HINT);
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(WIDTH, HEIGHT, "MassSprings");
InitWindow(WIDTH * 2, HEIGHT, "MassSprings");
MassSpringSystem mass_springs;
mass_springs.AddMass(1.0, Vector3(-0.5, 0.5, 0.0), true);
@ -33,6 +32,18 @@ auto main(int argc, char *argv[]) -> int {
mass_springs.AddSpring(1, 2, DEFAULT_SPRING_CONSTANT,
DEFAULT_DAMPENING_CONSTANT, DEFAULT_REST_LENGTH);
State s = State(4, 5);
Block a = Block(0, 0, 2, 1, false);
Block b = Block(0, 1, 1, 3, true);
Block c = Block(0, 2, "45");
Block d = Block(0, 3, "de");
s.AddBlock(a);
s.AddBlock(b);
for (Block block : s) {
std::cout << "Block (" << block.x << ", " << block.y << ")" << std::endl;
}
Renderer renderer(WIDTH, HEIGHT);
Edge2Set edges;
edges.reserve(mass_springs.springs.size());
@ -55,8 +66,9 @@ auto main(int argc, char *argv[]) -> int {
renderer.Transform(edges, vertices, mass_springs, abstime * ROTATION_SPEED,
CAMERA_DISTANCE);
renderer.DrawMassSprings(edges, vertices);
renderer.DrawKlotski(s); // TODO: Don't need to render each frame
renderer.DrawTextures();
renderer.Draw(viewport);
abstime += frametime;
}

View File

@ -1,7 +1,8 @@
#include "renderer.hpp"
#include <raylib.h>
#include "config.hpp"
#include "mass_springs.hpp"
auto Renderer::Rotate(const Vector3 &a, const float cos_angle,
const float sin_angle) -> Vector3 {
@ -63,11 +64,75 @@ auto Renderer::DrawMassSprings(const Edge2Set &edges,
}
DrawLine(0, 0, 0, height, BLACK);
EndTextureMode();
}
auto Renderer::DrawKlotski(State &state) -> void {
BeginTextureMode(klotski_target);
ClearBackground(RAYWHITE);
// Draw Board
const int board_width = width - 2 * BOARD_PADDING;
const int board_height = height - 2 * BOARD_PADDING;
float block_size;
float x_offset = 0.0;
float y_offset = 0.0;
if (state.width > state.height) {
block_size =
static_cast<float>(board_width) / state.width - 2 * BLOCK_PADDING;
y_offset = (board_height - block_size * state.height -
BLOCK_PADDING * 2 * state.height) /
2.0;
} else {
block_size =
static_cast<float>(board_height) / state.height - 2 * BLOCK_PADDING;
x_offset = (board_width - block_size * state.width -
BLOCK_PADDING * 2 * state.width) /
2.0;
}
DrawRectangle(0, 0, width, height, RAYWHITE);
DrawRectangle(x_offset, y_offset,
board_width - 2 * x_offset + 2 * BOARD_PADDING,
board_height - 2 * y_offset + 2 * BOARD_PADDING, LIGHTGRAY);
for (int x = 0; x < state.width; ++x) {
for (int y = 0; y < state.height; ++y) {
DrawRectangle(x_offset + BOARD_PADDING + x * BLOCK_PADDING * 2 +
BLOCK_PADDING + x * block_size,
y_offset + BOARD_PADDING + y * BLOCK_PADDING * 2 +
BLOCK_PADDING + y * block_size,
block_size, block_size, WHITE);
}
}
// Draw Blocks
for (Block block : state) {
Color c = EDGE_COLOR;
if (block.target) {
c = RED;
}
DrawRectangle(x_offset + BOARD_PADDING + block.x * BLOCK_PADDING * 2 +
BLOCK_PADDING + block.x * block_size,
y_offset + BOARD_PADDING + block.y * BLOCK_PADDING * 2 +
BLOCK_PADDING + block.y * block_size,
block.width * block_size + block.width * 2 * BLOCK_PADDING -
2 * BLOCK_PADDING,
block.height * block_size + block.height * 2 * BLOCK_PADDING -
2 * BLOCK_PADDING,
c);
}
DrawLine(width - 1, 0, width - 1, height, BLACK);
EndTextureMode();
}
auto Renderer::DrawTextures() -> void {
BeginDrawing();
DrawTextureRec(render_target.texture,
DrawTextureRec(klotski_target.texture,
Rectangle(0, 0, (float)width, -(float)height), Vector2(0, 0),
WHITE);
DrawFPS(10, 10);
DrawTextureRec(render_target.texture,
Rectangle(0, 0, (float)width, -(float)height),
Vector2(width, 0), WHITE);
DrawFPS(width + 10, 10);
EndDrawing();
}