summaryrefslogtreecommitdiffstats
path: root/src/area.c
blob: 4f0bc11693755950788f243ca1aa78c58f4a8a0c (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <SDL.h>
#include "area.h"
#include "viewport.h"
#include "resources/map.h"
#include "logging.h"

static void blit_map_layers(struct area *);
static void blit_map_layer(struct area *, enum map_layer, enum area_layer);

struct area *
area_new(struct map *map)
{
	struct area *a;

	if (map == NULL) {
		return NULL;
	}

	a = malloc(sizeof(*a));
	if (a == NULL) {
		return NULL;
	}

	a->map = map;
	blit_map_layers(a);

	return a;
}

static void
blit_map_layers(struct area *area)
{
	SDL_Color colors[256];
	int i;
	SDL_Rect dstrect;
	enum area_layer l;

	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;
	}

	dstrect.x = 0;
	dstrect.y = 0;
	dstrect.w = area->map->width * area->map->tilewidth;
	dstrect.h = area->map->height * area->map->tileheight;

	for (l = 0; l < AREA_LAYERS_MAX; ++l) {
		area->map_layers[l] = SDL_CreateRGBSurface(
				SDL_HWSURFACE | SDL_SRCCOLORKEY,
				area->map->width * area->map->tilewidth,
				area->map->height * area->map->tileheight,
				8, 0x00, 0x00, 0x00, 0x00);
		SDL_FillRect(area->map_layers[l], &dstrect,
				SDL_MapRGB(area->map_layers[l]->format,
					0xFC, 0x00, 0xFF));
		SDL_SetColorKey(area->map_layers[l], SDL_SRCCOLORKEY |
				SDL_RLEACCEL,
				SDL_MapRGB(area->map_layers[l]->format,
					0xFC, 0x00, 0xFF));
		SDL_SetColors(area->map_layers[l], colors, 0, 256);
	}

	blit_map_layer(area, MAP_LAYER_GROUND, AREA_LAYER_BOT);
	blit_map_layer(area, MAP_LAYER_OBJ_LOW, AREA_LAYER_BOT);
	blit_map_layer(area, MAP_LAYER_OBJ_MID, AREA_LAYER_MID);
	blit_map_layer(area, MAP_LAYER_OBJ_HIGH, AREA_LAYER_TOP);
}

static void
blit_map_layer(struct area *area, enum map_layer map_layer,
		enum area_layer area_layer)
{
	int i;
	Uint32 gid;
	int tile_found;
	struct map_tileset *mts;
	Uint32 mts_lastgid;
	SDL_Rect tilerect, layerrect;

	for (i = 0; i < area->map->width * area->map->height; ++i) {
		gid = area->map->layers[map_layer].tiles[i];
		if (gid == 0) {
			continue;
		}
		tile_found = 0;
		for (mts = area->map->tilesets_head;
				mts != NULL; mts = mts->next) {
			mts_lastgid = mts->firstgid +
				mts->tileset->width * mts->tileset->height;
			if (gid >= mts->firstgid && gid < mts_lastgid) {
				gid -= mts->firstgid;
				tilerect.x = gid % mts->tileset->width;
				tilerect.y = gid / mts->tileset->width;
				tilerect.w = mts->tileset->tilewidth;
				tilerect.h = mts->tileset->tileheight;
				tilerect.x *= tilerect.w;
				tilerect.y *= tilerect.h;
				layerrect.x = i % area->map->width;
				layerrect.y = i / area->map->width;
				layerrect.w = mts->tileset->tilewidth;
				layerrect.h = mts->tileset->tileheight;
				layerrect.x *= layerrect.w;
				layerrect.y *= layerrect.h;
#ifdef DEBUG_RENDER
				debug("Blitting %dx%d tile at (%d,%d) "
						"onto layer at (%d,%d)...",
						tilerect.w, tilerect.h,
						tilerect.x, tilerect.y,
						layerrect.x, layerrect.y);
#endif
				SDL_BlitSurface(mts->tileset->image->image,
						&tilerect,
						area->map_layers[area_layer],
						&layerrect);
				tile_found = 1;
				break;
			}
		}
		if (!tile_found) {
			warn("Tile with gid 0x%8.8x not found", gid);
		}
	}
}

void
render_area_to_viewport(struct area *area, struct viewport *vp)
{
	SDL_Rect srcrect;
	SDL_Rect dstrect;

	if (area == NULL || vp == NULL) {
		return;
	}

	srcrect.x = vp->x;
	srcrect.y = vp->y;
	srcrect.w = vp->w;
	srcrect.h = vp->h;
	dstrect.x = 0;
	dstrect.y = 0;
	dstrect.w = vp->w;
	dstrect.h = vp->h;

	render_area_to_surface(area, &srcrect, vp->screen, &dstrect);
}

void
render_area_to_surface(struct area *area, SDL_Rect *arearect,
		SDL_Surface *surface, SDL_Rect *surfacerect)
{
	SDL_Rect srcrect, dstrect;
	/* For demo */
	SDL_Rect tmprect;

	if (area == NULL || arearect == NULL) {
		return;
	}
	if (surface == NULL || surfacerect == NULL) {
		return;
	}

	memcpy(&srcrect, arearect, sizeof(srcrect));
	memcpy(&dstrect, surfacerect, sizeof(dstrect));

	/* Fast fill with black to avoid artifacts in off-map pixels. */
	/* NB: This is gray, at least temporarily. */
	SDL_FillRect(surface, &dstrect,
			SDL_MapRGB(surface->format, 0, 0, 0));

	/* For demo */
	tmprect.x = dstrect.x + 120 -  8;
	tmprect.y = dstrect.y +  80 - 12;
	tmprect.w = 16;
	tmprect.h = 20;

	/* Avoid fun glitches due to negative coordinates in source
	 * rectangles. */
	if (srcrect.x < 0) {
		srcrect.x = 0;
		dstrect.x = surfacerect->x - arearect->x;
	}
	if (srcrect.y < 0) {
		srcrect.y = 0;
		dstrect.y = surfacerect->y - arearect->y;
	}

#ifdef DEBUG_RENDER
	debug("Rendering %dx%d area rect at (%d,%d) "
			"onto %dx%d surface rect at (%d,%d)...",
			srcrect.w, srcrect.h, srcrect.x, srcrect.y,
			dstrect.w, dstrect.h, dstrect.x, dstrect.y);
#endif

	/* Blit bottom layer. */
	SDL_BlitSurface(area->map_layers[AREA_LAYER_BOT], &srcrect,
			surface, &dstrect);
/*	return;*/

	/* TODO: Blit low sprites. */

	/* For demo */
	SDL_FillRect(surface, &tmprect,
			SDL_MapRGB(surface->format, 31, 31, 127));

	/* Blit middle layer. */
	SDL_BlitSurface(area->map_layers[AREA_LAYER_MID], &srcrect,
			surface, &dstrect);

	/* TODO: Blit high sprites. */

	/* Blit top layer. */
	SDL_BlitSurface(area->map_layers[AREA_LAYER_TOP], &srcrect,
			surface, &dstrect);
}