summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 8caab890f785ddcdf0bc6b0e86c4b149c6f66707 (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
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
132
133
134
135
136
137
138
139
140
141
#include <math.h>
#include <SDL.h>
#include "init.h"
#include "logging.h"
#include "resources/image.h"
#include "resources/map.h"
#include "viewport.h"
#include "area.h"

#define DEMO()                                                                 \
	do {                                                                   \
		vp->x = x + map->tilewidth / 2 - vp->w / 2;                    \
		vp->y = y + map->tileheight / 2 - vp->h / 2;                   \
		render_area_to_viewport(a, vp);                                \
		SDL_Flip(vp->screen);                                          \
		SDL_Delay(10);                                                 \
		++i;                                                           \
	} while (0)

int
main(void)
{
	struct viewport *vp;
	struct map *map;
	struct area *a;
	SDL_Color colors[256];
	int i;
	struct map_exit *e;

	init();
	vp = init_viewport(240, 160, 8);

	map = map_get(MAPSDIR "/forest1-8bit.tmx");
	if (map == NULL) {
		map = map_get("data/maps/forest1-8bit.tmx");
	}
	if (map == NULL) {
		err(1, "Where's the map, George?");
	}
	for (i = 0; i < 256; ++i) {
		colors[i].r = (i % 8) * 36;
		colors[i].g = (i % 64 / 8) * 36;
		colors[i].b = (i / 64) * 85;
	}
	if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, colors, 0, 256)) {
		warn("Failed to set palette");
	}
	a = area_new(map, vp);

	for (e = map->map_exits_head; e != NULL; e = e->next) {
		debug("Map exit (size %dx%d) at (%d,%d) "
				"to map %s (size %dx%d) at (%d,%d)",
				e->width, e->height,
				e->x, e->y,
				e->target_map_name,
				e->target_map->width, e->target_map->height,
				e->target_x_coord, e->target_y_coord);
	}

	render_area_to_viewport(a, vp);
	{
		Uint32 t;
		double x;
		int y_start, y_end;

		for (t = 0; t <= 2880; t += 5) {
			for (i = 0; i < 256; ++i) {
				/*
				 * t => [0 .. INF]
				 * t[0 .. 1440] => x[0, 3.14159]
				 * x = (t % 1440) * 3.14159 / 720
				 * y => [y_start, y_end]
				 * ((cos(x) + 1) / 2) => [0,1]
				 * y = ((cos(x) + 1) / 2) *
				 *     (y_end - y_start) + y_start
				 */

				x = t % 1440;
				x = x * 3.14159 / 720;

				y_start = (i % 8) * 36;
				y_end = y_start * 0.25;
				colors[i].r = ((cos(x) + 1) / 2) *
					(y_end - y_start) + y_start;

				y_start = (i % 64 / 8) * 36;
				y_end = y_start * 0.25;
				colors[i].g = ((cos(x) + 1) / 2) *
					(y_end - y_start) + y_start;

				y_start = (i / 64) * 85;
				y_end = y_start * 0.375;
				colors[i].b = ((cos(x) + 1) / 2) *
					(y_end - y_start) + y_start;
			}
			if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, colors, 0, 256)) {
				warn("Failed to set palette");
			}
			SDL_Flip(vp->screen);
			SDL_Delay(10);
		}
	}

	/* Demo */
	for (i = 0; i < 256; ++i) {
		colors[i].r = (i % 8) * 36;
		colors[i].g = (i % 64 / 8) * 36;
		colors[i].b = (i / 64) * 85;
	}
	if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, colors, 0, 256)) {
		warn("Failed to set palette");
	}

	{
		Uint32 start, end;
		int x, y;

		i = 0;
		start = SDL_GetTicks();
		x = 19 * map->tilewidth ;
		y = 38 * map->tileheight;
		for (; y > 34 * map->tileheight; y -= 2) DEMO(); /* Up */
		for (; x < 23 * map->tilewidth ; x += 2) DEMO(); /* Right */
		for (; y > 16 * map->tileheight; y -= 2) DEMO(); /* Up */
		for (; x > 15 * map->tilewidth ; x -= 2) DEMO(); /* Left */
		for (; y < 25 * map->tileheight; y += 2) DEMO(); /* Down */
		for (; x >  4 * map->tilewidth ; x -= 2) DEMO(); /* Left */
		for (; y > 18 * map->tileheight; y -= 2) DEMO(); /* Up */
		for (; x <  9 * map->tilewidth ; x += 2) DEMO(); /* Right */
		for (; y >  5 * map->tileheight; y -= 2) DEMO(); /* Up */
		for (; x < 15 * map->tilewidth ; x += 2) DEMO(); /* Right */
		for (; y >  1 * map->tileheight; y -= 2) DEMO(); /* Up */
		end = SDL_GetTicks();
		debug("Rendered %d frames in %d milliseconds", i, end - start);
	}

	quit(0);

	/* Control doesn't actually reach here. */
	return 0;
}