~rabbits/hiversaires

ref: 48e00fcdd82d1c3e02fabb9c29dceaf4d461f02c hiversaires/src/delays.c -rw-r--r-- 1.6 KiB
48e00fcdRezmason Remade the splash animation sequence 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);
		}
	}
}