rekurzivni-piskvorky/graphics.cpp
2025-08-02 23:42:11 +02:00

61 lines
1.5 KiB
C++

#include "global.hpp"
// Rendering
void Renderer::prepareTextures() {
const char* paths[] = {"img/circle.png", "img/cross.png"};
for(int i = 0; i < IMG_COUNT; ++i) {
Image img;
img = LoadImage(paths[i]);
textures[i] = LoadTextureFromImage(img);
}
}
void Renderer::render_image(tile t, Rectangle dst, Color color) {
if(t == empty || t == tie)
return;
Texture tex = textures[t-1];
Rectangle src = {0, 0, (float)tex.width, (float)tex.height};
DrawTexturePro(tex, src, dst, {0,0}, 0, color);
}
void Renderer::render_block(Block *b) {
if(b->depth > 0)
DrawRectangleLinesEx(b->place, b->depth, GRID);
render_image(b->t, b->place);
if(b->depth > 0 && (b->t == empty || b->t == tie))
for(int i = 0; i < 9; i++)
render_block(b->sons[i]);
}
void Renderer::render_all(Block* root, int time, Rectangle play_area, deque<int> highlight) {
BeginDrawing();
ClearBackground(BACKGROUND);
render_block(root);
if(valid(root, play_area, highlight))
render_image(act(time), root->get_rect(highlight), GHOST);
DrawRectangleLinesEx(play_area, 3*LINE_W, col(time+1));
EndDrawing();
}
int Renderer::update_board_size(int depth) {
int max_size = std::min(GetScreenWidth(), GetScreenHeight());
int size = 1;
for(int i = 0; i < depth; i++)
size *= 3;
max_size -= max_size % size;
return max_size;
}
Renderer::Renderer(Block* root, int time, Rectangle dst) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(SCREEN, SCREEN, "Rekurze");
SetTargetFPS(FPS);
prepareTextures();
render_all(root, time, dst);
}