/* main.c: Entrypoint
 * Created: 2026-03-09
 * Author: Aryadev Chavali
 * License: See end of file
 */

#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#include <time.h>

#include "base.h"
#include "bf.h"
#include "simulation.h"
#include "sv.h"
#include "vec.h"

void simulation_soup(simulation_t *sim)
{
  for (size_t i = 0; i < sizeof(sim->memory); ++i)
  {
    sim->memory[i] = rand() % 255;
  }
}

struct ThreadState
{
  simulation_t *simulation;
  bool paused, done;
};

int thread_simulation_iterate(void *ptr)
{
  struct ThreadState *state = ptr;
  while (!state->done)
  {
    while (state->paused && !state->done)
    {
    }
    simulation_iterate(state->simulation);
  }
  return 0;
}

int thread_simulation_mutate(void *ptr)
{
  struct ThreadState *state = ptr;
  while (!state->done)
  {
    while (state->paused && !state->done)
    {
    }
    simulation_mutate(state->simulation);
  }
  return 0;
}

int main(void)
{
  simulation_t sim = {0};
  simulation_soup(&sim);
  srand(time(NULL));

  thrd_t thread_iterator, thread_mutator;
  struct ThreadState state_iterator = {
      .simulation = &sim,
      .paused     = true,
      .done       = false,
  };
  struct ThreadState state_mutator = {
      .simulation = &sim,
      .paused     = true,
      .done       = false,
  };
  thrd_create(&thread_iterator, thread_simulation_iterate, &state_iterator);
  thrd_create(&thread_mutator, thread_simulation_mutate, &state_mutator);

  InitWindow(WIDTH, HEIGHT, "CompLife");
  SetTargetFPS(60);
  for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
  {
    if (IsKeyPressed(KEY_SPACE))
    {
      state_iterator.paused = !state_iterator.paused;
      state_mutator.paused  = !state_mutator.paused;
    }

    BeginDrawing();
    ClearBackground(BLACK);
    simulation_draw(&sim);
    EndDrawing();
  }
  state_iterator.done = true;
  state_mutator.done  = true;
  thrd_join(thread_iterator, NULL);
  thrd_join(thread_mutator, NULL);
  CloseWindow();
  return 0;
}

/* Copyright (C) 2026 Aryadev Chavali

 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License Version 2 for
 * details.

 * You may distribute and modify this code under the terms of the GNU General
 * Public License Version 2, which you should have received a copy of along with
 * this program.  If not, please go to <https://www.gnu.org/licenses/>.

 */
