~rabbits/hiversaires

a5570c1c634e41ac435d877fd425527dd1af169b — Rezmason 11 months ago db83151
Added delays module
3 files changed, 114 insertions(+), 0 deletions(-)

A src/delays.c
A src/delays.h
M src/hiversaires.c
A src/delays.c => src/delays.c +79 -0
@@ 0,0 1,79 @@
#include "delays.h"

#include <string.h>
#include <stdlib.h>

/*
Copyright (c) 2013-2021 Devine Lu Linvega
2018 web port by rezmason
2021 C89 port by rezmason

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

static Delay (*delays)[] = NULL;
static double _totalTime = 0;
static int _numDelays;

void
Delays_Init(int numDelays)
{
	int i;
	_numDelays = numDelays;
	delays = calloc(_numDelays, sizeof(Delay));
	for(i = 0; i < _numDelays; i++) {
		(*delays)[i].endTime = -1;
	}
}

void
Delays_Quit()
{
	free(delays);
	delays = NULL;
}

int
Delays_Add(double duration, void (*onComplete)(void *data), void *data)
{
	int i;
	for(i = 0; i < _numDelays; i++) {
		if((*delays)[i].onComplete == NULL) {
			(*delays)[i].endTime = _totalTime + duration;
			(*delays)[i].onComplete = onComplete;
			(*delays)[i].data = data;
			return i;
		}
	}
	return -1;
}

void
Delays_Cancel(int uid, int complete)
{
	void (*onComplete)() = (*delays)[uid].onComplete;
	void *data = (*delays)[uid].data;
	(*delays)[uid].onComplete = NULL;
	(*delays)[uid].data = NULL;
	(*delays)[uid].endTime = -1;
	if(complete && onComplete != NULL) {
		onComplete(data);
	}
}

void
Delays_Update(double totalTime)
{
	int i;
	_totalTime = totalTime;
	for(i = 0; i < _numDelays; i++) {
		if((*delays)[i].endTime > 0 && (*delays)[i].endTime < _totalTime) {
			Delays_Cancel(i, 1);
		}
	}
}

A src/delays.h => src/delays.h +31 -0
@@ 0,0 1,31 @@
#pragma once

/*
Copyright (c) 2013-2021 Devine Lu Linvega
2018 web port by rezmason
2021 C89 port by rezmason

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

/*
	This module implements delays— functions to be called
	at some point in the future.
*/

typedef struct {
	double endTime;
	void (*onComplete)(void *data);
	void *data;
} Delay;

void Delays_Init(int numDelays);
void Delays_Quit();
int Delays_Add(double duration, void (*onComplete)(), void *data);
void Delays_Cancel(int uid, int complete);
void Delays_Update(double totalTime);

M src/hiversaires.c => src/hiversaires.c +4 -0
@@ 10,6 10,7 @@
#include "gui.h"
#include "audio.h"
#include "input.h"
#include "delays.h"

/*
Copyright (c) 2013-2021 Devine Lu Linvega


@@ 37,6 38,7 @@ static int reset = 0;
static int
quit()
{
	Delays_Quit();
	Audio_Quit();
	Input_Quit();
	GUI_Quit();


@@ 69,6 71,7 @@ processWindowResize(int w, int h)
static void
update(double totalTime, double deltaTime)
{
	Delays_Update(totalTime);
	Play_Update(totalTime, deltaTime);
	Stage_Update(totalTime, deltaTime);
}


@@ 117,6 120,7 @@ Hiversaires_Run(int argc, char *argv[])
		return quit();
	}

	Delays_Init(20);
	Audio_Init();
	Stage_Init();
	GUI_Init();