main: adjustments
This commit is contained in:
134
src/main.c
134
src/main.c
@@ -14,135 +14,49 @@
|
|||||||
#include "sv.h"
|
#include "sv.h"
|
||||||
#include "vec.h"
|
#include "vec.h"
|
||||||
|
|
||||||
#include "program_iter.h"
|
#include "bf.h"
|
||||||
#include "simulation.h"
|
|
||||||
|
|
||||||
static const size_t CELL_WIDTH = WIDTH / GRID_WIDTH;
|
static char *VALID_OPS = "<>{}-+.,[]";
|
||||||
|
|
||||||
static const char *VALID_OPS = "<>{}-+.,[]";
|
u8 get_op(const u8 cell)
|
||||||
|
|
||||||
Color simulation_cell_color(const u8 *program)
|
|
||||||
{
|
{
|
||||||
// How do we compute a "colour" for a program? I say we count all the valid
|
if (strchr(VALID_OPS, cell))
|
||||||
// opcodes in the program. These counts are used as weights for 10 distinct
|
return cell;
|
||||||
// colours.
|
else
|
||||||
const Vector4 bases[] = {
|
return '\0';
|
||||||
['<'] = ColorNormalize(ColorFromHSV(0.121, 0.467, 0.706)),
|
}
|
||||||
['>'] = ColorNormalize(ColorFromHSV(1.000, 0.498, 0.055)),
|
|
||||||
['{'] = ColorNormalize(ColorFromHSV(0.173, 0.627, 0.173)),
|
Color simulation_cell_color(const u8 cell)
|
||||||
['}'] = ColorNormalize(ColorFromHSV(0.839, 0.153, 0.157)),
|
{
|
||||||
['-'] = ColorNormalize(ColorFromHSV(0.580, 0.404, 0.741)),
|
const Color possible_colors[] = {
|
||||||
['+'] = ColorNormalize(ColorFromHSV(0.549, 0.337, 0.294)),
|
['<'] = ColorFromHSV(0.121, 0.467, 0.706),
|
||||||
['.'] = ColorNormalize(ColorFromHSV(0.890, 0.467, 0.761)),
|
['>'] = ColorFromHSV(1.000, 0.498, 0.055),
|
||||||
[','] = ColorNormalize(ColorFromHSV(0.498, 0.498, 0.498)),
|
['{'] = ColorFromHSV(0.173, 0.627, 0.173),
|
||||||
['['] = ColorNormalize(ColorFromHSV(0.737, 0.741, 0.133)),
|
['}'] = ColorFromHSV(0.839, 0.153, 0.157),
|
||||||
[']'] = ColorNormalize(ColorFromHSV(0.090, 0.745, 0.812)),
|
['-'] = ColorFromHSV(0.580, 0.404, 0.741),
|
||||||
|
['+'] = ColorFromHSV(0.549, 0.337, 0.294),
|
||||||
|
['.'] = ColorFromHSV(0.890, 0.467, 0.761),
|
||||||
|
[','] = ColorFromHSV(0.498, 0.498, 0.498),
|
||||||
|
['['] = ColorFromHSV(0.737, 0.741, 0.133),
|
||||||
|
[']'] = ColorFromHSV(0.090, 0.745, 0.812),
|
||||||
|
['\0'] = BLACK,
|
||||||
};
|
};
|
||||||
|
|
||||||
u64 counter[] = {
|
return possible_colors[get_op(cell)];
|
||||||
['<'] = 0, ['>'] = 0, ['{'] = 0, ['}'] = 0, ['-'] = 0,
|
|
||||||
['+'] = 0, ['.'] = 0, [','] = 0, ['['] = 0, [']'] = 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
u64 total_valid = 0;
|
|
||||||
for (u64 i = 0; i < SIZEOF_PROGRAM; ++i)
|
|
||||||
{
|
|
||||||
if (strchr(VALID_OPS, program[i]))
|
|
||||||
{
|
|
||||||
counter[(u64)program[i]]++;
|
|
||||||
++total_valid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (total_valid == 0)
|
|
||||||
return BLACK;
|
|
||||||
|
|
||||||
f64 colour_cells[3];
|
|
||||||
for (const char *ptr = VALID_OPS; *ptr; ++ptr)
|
|
||||||
{
|
|
||||||
colour_cells[0] += bases[(u64)*ptr].x;
|
|
||||||
colour_cells[1] += bases[(u64)*ptr].y;
|
|
||||||
colour_cells[2] += bases[(u64)*ptr].z;
|
|
||||||
}
|
|
||||||
colour_cells[0] /= total_valid;
|
|
||||||
colour_cells[1] /= total_valid;
|
|
||||||
colour_cells[2] /= total_valid;
|
|
||||||
|
|
||||||
return (Color){.r = 255 * colour_cells[0],
|
|
||||||
.g = 255 * colour_cells[1],
|
|
||||||
.b = 255 * colour_cells[2],
|
|
||||||
.a = 255};
|
|
||||||
}
|
|
||||||
|
|
||||||
void simulation_draw(struct Simulation *sim)
|
|
||||||
{
|
|
||||||
sv_t sv = SV(sim->buffer, SIMULATION_SIZE);
|
|
||||||
|
|
||||||
for (u64 i = 0; i < SIMULATION_SIZE / SIZEOF_PROGRAM; ++i)
|
|
||||||
{
|
|
||||||
sv_t program = sv_truncate(sv, SIZEOF_PROGRAM);
|
|
||||||
Color color = simulation_cell_color((const u8 *)program.data);
|
|
||||||
|
|
||||||
u64 x = i / GRID_WIDTH;
|
|
||||||
u64 y = i % GRID_WIDTH;
|
|
||||||
DrawRectangle(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
|
|
||||||
color);
|
|
||||||
|
|
||||||
sv = sv_chop_left(sv, 64);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (u64 i = 0; i < THREAD_POOL; ++i)
|
|
||||||
{
|
|
||||||
u64 p1 = sim->states[i].p1;
|
|
||||||
u64 p2 = sim->states[i].p2;
|
|
||||||
DrawRectangleLines((p1 / GRID_WIDTH) * CELL_WIDTH,
|
|
||||||
(p1 % GRID_WIDTH) * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
|
|
||||||
BLUE);
|
|
||||||
DrawRectangleLines((p2 / GRID_WIDTH) * CELL_WIDTH,
|
|
||||||
(p2 % GRID_WIDTH) * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
|
|
||||||
RED);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
struct Simulation sim = {0};
|
|
||||||
simulation_start(&sim);
|
|
||||||
|
|
||||||
InitWindow(WIDTH, HEIGHT, "CompLife");
|
InitWindow(WIDTH, HEIGHT, "CompLife");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
|
for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
|
||||||
{
|
{
|
||||||
if (IsKeyPressed(KEY_SPACE))
|
|
||||||
{
|
|
||||||
sim.paused = !sim.paused;
|
|
||||||
}
|
|
||||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
|
||||||
{
|
|
||||||
Vector2 pos = GetMousePosition();
|
|
||||||
u64 x = (pos.x / WIDTH) * GRID_WIDTH;
|
|
||||||
u64 y = (pos.y / WIDTH) * GRID_WIDTH;
|
|
||||||
printf("(%lu,%lu)=", x, y);
|
|
||||||
sv_t sv = sv_truncate(
|
|
||||||
sv_chop_left(SV(sim.buffer, SIMULATION_SIZE), (x * GRID_WIDTH) + y),
|
|
||||||
64);
|
|
||||||
for (u64 i = 0; i < sv.size; ++i)
|
|
||||||
{
|
|
||||||
if (strchr(VALID_OPS, sv.data[i]))
|
|
||||||
{
|
|
||||||
printf("%c", sv.data[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
simulation_draw(&sim);
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
simulation_stop(&sim);
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user