summaryrefslogtreecommitdiffstats
path: root/src/area.c
blob: bc331a1edce6cd7bd23778e4235b9a6167320ca0 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2013  Patrick "P. J." McDermott
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <SDL.h>
#include "area.h"
#include "viewport.h"
#include "resources/map.h"
#include "resources/tileset.h"
#include "logging.h"

static void init_map_layers(struct area *);
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, SDL_Palette *pal)
{
	struct area *a;

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

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

	a->map = map;
	init_map_layers(a);
	set_area_palette(a, pal);
	blit_map_layers(a);

	return a;
}

void
set_area_palette(struct area *area, SDL_Palette *pal)
{
	enum area_layer l;

	for (l = 0; l < AREA_LAYERS_MAX; ++l) {
		SDL_SetPalette(area->map_layers[l], SDL_LOGPAL,
				pal->colors, 0, pal->ncolors);
	}
}

static void
init_map_layers(struct area *area)
{
	SDL_Rect dstrect;
	enum area_layer l;

	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_MapRGB(area->map_layers[l]->format,
					0xFC, 0x00, 0xFF));
	}
}

static void
blit_map_layers(struct area *area)
{
	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 + 160 -  8;
	tmprect.y = dstrect.y + 120 - 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, 255, 0, 255));

	/* 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);
}