summaryrefslogtreecommitdiffstats
path: root/src/game/switchCallbacks.c
blob: 235e40ad0759b4496257c547112345806a9a1415 (plain)
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
#include "base/switchCallbacks.h"
#include <string.h>
#include <stdlib.h>

Callbacks *last_callback = NULL;
Callbacks *current_callback = NULL;

void exitCallback(Callbacks *cb) {
  if(cb != NULL)
    if(cb->exit != NULL)
    (cb->exit)(); /* give them the chance to quit */
}

void initCallback(Callbacks *cb) {
  if(cb->init != NULL)
    (cb->init)();
  if(cb->initGL != NULL)
    (cb->initGL)();
}


void switchCallbacks(Callbacks *new) {
	// if(current_callback)
	// fprintf(stderr, "callbacks: exiting %s\n", current_callback->name);
  exitCallback(current_callback);
  SystemRegisterCallbacks(new);
	// fprintf(stderr, "callbacks: initializing %s\n", new->name);
  initCallback(new);

  last_callback = current_callback;
  current_callback = new;
}
  
void updateCallbacks(void) {
  /* called when the window is recreated */
  exitCallback(current_callback);
  SystemRegisterCallbacks(current_callback);
  initCallback(current_callback);
}

void restoreCallbacks(void) {
  if(last_callback == NULL) {
    fprintf(stderr, "fatal: no last callback present, exiting\n");
    exit(1); // OK: programmer error, critical
  }

  exitCallback(last_callback);
  current_callback = last_callback;
  SystemRegisterCallbacks(current_callback);
  initCallback(current_callback);
}

#define N_CALLBACKS 7
Callbacks *callbackList[N_CALLBACKS] = {
	&gameCallbacks, &guiCallbacks, &pauseCallbacks, &configureCallbacks,
	&promptCallbacks, &creditsCallbacks, &timedemoCallbacks
};

void setCallback(const char *name) {
	int i;

	for(i = 0; i < N_CALLBACKS; i++) {
		if(strcmp( callbackList[i]->name, name ) == 0)
			break;
	}
	if(i == N_CALLBACKS) {
		fprintf(stderr, "fatal: no callback named '%s' found\n", name);
		exit(1); // OK: programmer error, critical
	}
	switchCallbacks(callbackList[i]);
}