Files
GameOfLifeEditor/src/render.cpp

79 lines
1.7 KiB
C++

/*
* File name: render.cpp
* Author: lejulien
* Date created: 10-01-2026 21:49:04
// Date modified: 12-01-2026 21:54:56
* ------
*/
#include "../includes/render.hpp"
#include "raylib.h"
#include <cmath>
// Constructor
Render::Render(int cell_size) : cell_size_(cell_size) {}
// Display function
int HSVtoHex(float h) {
float r, g, b;
int i = int(h / 60) % 6;
float f = (h / 60) - i;
float p = 0.0f;
float q = 1.0f - f;
float t = f;
switch (i) {
case 0:
r = 1.0f, g = t, b = p;
break;
case 1:
r = q, g = 1.0f, b = p;
break;
case 2:
r = p, g = 1.0f, b = t;
break;
case 3:
r = p, g = q, b = 1.0f;
break;
case 4:
r = t, g = p, b = 1.0f;
break;
default:
r = 1.0f, g = p, b = q;
break;
}
int R = static_cast<int>(r * 255);
int G = static_cast<int>(g * 255);
int B = static_cast<int>(b * 255);
return (R << 24) | (G << 16) | (B << 8) | 255; // Return as 0xRRGGBB integer
}
void Render::display_world(std::vector<bool> *data, int width, int height) {
float maxDist =
std::sqrt((width - 1) * (width - 1) + (height - 1) * (height - 1));
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if ((*data)[i + j * width]) {
float dist = std::sqrt(i * i + j * j);
float factor = dist / maxDist;
float hue = factor * 360.0f; // Map to 0-360 degrees
DrawRectangle(i * cell_size_, j * cell_size_, cell_size_, cell_size_,
GetColor(HSVtoHex(hue)));
}
}
}
}
// Render loop
void Render::display(std::shared_ptr<World> world) {
display_world(world->getWorldData(), world->getWidth(), world->getHeight());
}
void Render::updateCellSize(int new_size) { cell_size_ = new_size; }