<commit_before>#include "lifegraphics.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>

/* Ideally there wouldn't be any GL calls or low-level backend type stuff here.
Just high level draw calls. Low level stuff should be compartmentalized in
graphics.c or alike. */

LifeGraphicsContext_t *CreateLifeGraphicsContext(LifeWorldDim_t w, LifeWorldDim_t h)
{
  LifeGraphicsContext_t *context = malloc(sizeof(LifeGraphicsContext_t));

  context->pWorldRenderBuffer = NewLifeWorld(w, h);

  context->pQuadDrawData = NewQuadDataBuffer(w * h);
  if (!SetQuadShader(context->pQuadDrawData, 
    BuildShaderProgram("shaders/vs1.glsl", "shaders/fs1.glsl"))) //default shaders
  {
    printf("Couldn't build quad shader(s)!\n");
    DestroyLifeGraphicsContext(context);
    return NULL;
  }

  return context;
}

void DestroyLifeGraphicsContext(LifeGraphicsContext_t *context)
{
  DestroyLifeWorld(context->pWorldRenderBuffer);
  DestroyQuadDrawData(context->pQuadDrawData);
  free(context);
}

void SyncWorldToScreen(SDL_Window *window, ThreadedLifeContext_t *worldContext, LifeGraphicsContext_t *graphicsContext, int syncRateHz)
{
  //fair amount of duplicated code here, can be cleaned up?
  static unsigned long endTime = 0;
  if (syncRateHz <= 0)
  {
    SDL_LockMutex(worldContext->lock);
    CopyWorld(graphicsContext->pWorldRenderBuffer, worldContext->front);
    SDL_UnlockMutex(worldContext->lock);

    DrawWorld(window, graphicsContext);
  }
  else if (SDL_GetTicks() > endTime)
  {

    SDL_LockMutex(worldContext->lock);
    CopyWorld(graphicsContext->pWorldRenderBuffer, worldContext->front);
    SDL_UnlockMutex(worldContext->lock);

    DrawWorld(window, graphicsContext);

    unsigned long delayMs = 1000 / syncRateHz;
    endTime = SDL_GetTicks() + delayMs;
  }
}
<commit_msg>Rewrote SyncWorldToScreen critical section to not duplicate code.<commit_after>#include "lifegraphics.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>

/* Ideally there wouldn't be any GL calls or low-level backend type stuff here.
Just high level draw calls. Low level stuff should be compartmentalized in
graphics.c or alike. */

LifeGraphicsContext_t *CreateLifeGraphicsContext(LifeWorldDim_t w, LifeWorldDim_t h)
{
  LifeGraphicsContext_t *context = malloc(sizeof(LifeGraphicsContext_t));

  context->pWorldRenderBuffer = NewLifeWorld(w, h);

  context->pQuadDrawData = NewQuadDataBuffer(w * h);
  if (!SetQuadShader(context->pQuadDrawData, 
    BuildShaderProgram("shaders/vs1.glsl", "shaders/fs1.glsl"))) //default shaders
  {
    printf("Couldn't build quad shader(s)!\n");
    DestroyLifeGraphicsContext(context);
    return NULL;
  }

  return context;
}

void DestroyLifeGraphicsContext(LifeGraphicsContext_t *context)
{
  DestroyLifeWorld(context->pWorldRenderBuffer);
  DestroyQuadDrawData(context->pQuadDrawData);
  free(context);
}

void SyncWorldToScreen(SDL_Window *window, ThreadedLifeContext_t *worldContext, LifeGraphicsContext_t *graphicsContext, int syncRateHz)
{
  static unsigned long endTime = 0;
  if (SDL_GetTicks() > endTime)
  {
		//Critical section!
    SDL_LockMutex(worldContext->lock);
    CopyWorld(graphicsContext->pWorldRenderBuffer, worldContext->front);
    SDL_UnlockMutex(worldContext->lock);
    DrawWorld(window, graphicsContext);
		//end crititcal section!

		if (syncRateHz > 0)
		{
    	unsigned long delayMs = 1000 / syncRateHz;
    	endTime = SDL_GetTicks() + delayMs;
		}
  }

}

{'fraction_non_alphanumeric': 0.10697674418604651, 'fraction_numerical': 0.005232558139534884, 'mean_word_length': 5.22242314647378, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6223026', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1086, 'n_words': 239}