summary refs log tree commit diff stats
path: root/mazeoflife.cpp
blob: c744b284a64f65036f4543a306253363dfd00749 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "includes.h"

SDL_Surface *screen;
State* state;

int main(int argc, char *argv[])
{
	srand(time(NULL));

	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
	{ 
		printf("Could not initialize SDL: %s.\n", SDL_GetError());
		exit(-1);
	}

	if (TTF_Init() == -1)
	{
		printf("Could not initialize SDL_ttf: %s.\n", TTF_GetError());
		exit(-1);
	}

	/* Clean up on exit */
	atexit(SDL_Quit);
	atexit(TTF_Quit);

	SDL_WM_SetCaption("Maze Of Life", NULL);

	SDL_Surface* icon;
	LOADIMAGE(icon,icon)
	SDL_WM_SetIcon(icon, NULL);

	/*
	* Initialize the display in a 640x480 8-bit palettized mode,
	* requesting a software surface
	*/
	screen = SDL_SetVideoMode(WIDTH*16, HEIGHT*16, 8, SDL_DOUBLEBUF);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set %dx%dx8 video mode: %s\n", WIDTH*16, WIDTH*16, SDL_GetError());
		exit(1);
	}

	SDL_EnableKeyRepeat(100, 50);

	state = new TitleState();

	SDL_AddTimer(TICKDELAY, *tick, NULL);

	SDL_Event anEvent;
	for (;;)
	{
		while (SDL_PollEvent(&anEvent))
		{
			switch (anEvent.type)
			{
				case SDL_QUIT:
					exit(0);

					break;
				case SDL_KEYDOWN:
					state->input(anEvent.key.keysym);

					break;
			}
		}

		state->render(screen);

		SDL_Flip(screen);
	}

	exit(0);
}

void wrap(int* x, int* y)
{
	if (*x < 0)
	{
		*x = WIDTH-(0-*x);
	} else if (*y < 0)
	{
		*y = HEIGHT-(0-*y);
	} else if (*x >= WIDTH)
	{
		*x = *x-WIDTH;
	} else if (*y >= HEIGHT)
	{
		*y = *y-HEIGHT;
	}
}

Uint32 getColor(int r, int g, int b)
{
	return SDL_MapRGB(screen->format, r, g, b);
}

void changeState(State* nState)
{
	state = nState;
}

Uint32 tick(Uint32 interval, void *param)
{
	state->tick();

	return interval;
}

TTF_Font* loadFont(int size)
{
	SDL_RWops* mono_rw = SDL_RWFromMem(&RESNAME(mono_ttf,start), (int) &RESNAME(mono_ttf,size));
	TTF_Font* tmpfont = TTF_OpenFontRW(mono_rw, 1, size);

	if (tmpfont == NULL)
	{
		printf("Unable to load font: %s\n", TTF_GetError());
		exit(1);
	}

	return tmpfont;
}

const char* getDataFile()
{
#ifdef WINDOWS
	char* dir = getenv("USERPROFILE");
#else
	char* dir = getenv("HOME");
#endif

	return (std::string(dir) + "/.molhslist").c_str();
}