File: gamerend.cc

package info (click to toggle)
exult 0.98rc1-2
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,924 kB
  • ctags: 8,928
  • sloc: cpp: 83,768; sh: 7,643; ansic: 4,328; makefile: 890; yacc: 618; lex: 255; xml: 19
file content (457 lines) | stat: -rw-r--r-- 12,520 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 *	gamerend.cc - Rendering methods.
 *
 *  Copyright (C) 1998-1999  Jeffrey S. Freedman
 *  Copyright (C) 2000-2001  The Exult Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <cstdio>

using std::snprintf;

#include "gamewin.h"
#include "gamemap.h"
#include "actors.h"
#include "chunks.h"
#include "objiter.h"
#include "Gump_manager.h"
#include "effects.h"
#include "cheat.h"

/*
 *	Paint just the map with given top-left-corner tile.
 */

void Game_window::paint_map_at_tile
	(
	int x, int y, int w, int h,	// Clip to this area.
	int toptx, int topty,
	int skip_above			// Don't display above this lift.
	)
	{
	int savescrolltx = scrolltx, savescrollty = scrollty;
	int saveskip = skip_lift;
	scrolltx = toptx;
	scrollty = topty;
	skip_lift = skip_above;
	map->read_map_data();		// Gather in all objs., etc.
	win->set_clip(x, y, w, h);
	paint_map(0, 0, get_width(), get_height());
	win->clear_clip();
	scrolltx = savescrolltx;
	scrollty = savescrollty;
	skip_lift = saveskip;
	}

/*
 *	Figure offsets on screen.
 */

inline int Figure_screen_offset
	(
	int ch,				// Chunk #
	int scroll			// Top/left tile of screen.
	)
	{
					// Watch for wrapping.
	int t = ch*c_tiles_per_chunk - scroll;
	if (t < -c_num_tiles/2)
		t += c_num_tiles;
	t %= c_num_tiles;
	return t*c_tilesize;
	}

/*
 *	Show the outline around a chunk.
 */

inline void Paint_chunk_outline
	(
	Game_window *gwin,
	int pixel,			// Pixel value to use.
	int cx, int cy,			// Chunk coords.
	int tnum,			// Terrain #.
	int xoff, int yoff		// Where chunk was painted.
	)
	{
	gwin->get_win()->fill8(pixel, c_chunksize, 1, xoff, yoff);
	gwin->get_win()->fill8(pixel, 1, c_chunksize, xoff, yoff);
	char text[40];			// Show chunk #.
	snprintf(text, sizeof(text), "(%d,%d)T%d", cx, cy, tnum);
	gwin->paint_text(2, text, xoff + 2, yoff + 2);
	}

/*
 *	Paint tile grid.
 */

static void Paint_grid
	(
	Game_window *gwin,
	Xform_palette& xform		// For transparency.
	)
	{
	Image_window8 *win = gwin->get_win();
					// Paint grid at edit height.
	int xtiles = gwin->get_width()/c_tilesize,
	    ytiles = gwin->get_height()/c_tilesize;
	int lift = cheat.get_edit_lift();
	int liftpixels = lift*(c_tilesize/2) + 1;
	for (int y = 0; y < ytiles; y++)
		win->fill_translucent8(0, xtiles*c_tilesize, 1, 
				-liftpixels, y*c_tilesize - liftpixels, xform);
	for (int x = 0; x < xtiles; x++)
		win->fill_translucent8(0, 1, ytiles*c_tilesize,
				x*c_tilesize - liftpixels, -liftpixels, xform);
	}


/*
 *	Just paint terrain.  This is for terrain_editing mode.
 */

void Game_window::paint_terrain_only
	(
	int start_chunkx, int start_chunky,
	int stop_chunkx, int stop_chunky
	)
	{
	int cx, cy;			// Chunk #'s.
					// Paint all the flat scenery.
	for (cy = start_chunky; cy != stop_chunky; cy = INCR_CHUNK(cy))
		{
		int yoff = Figure_screen_offset(cy, scrollty);
		for (cx = start_chunkx; cx != stop_chunkx; cx = INCR_CHUNK(cx))
			{
			int xoff = Figure_screen_offset(cx, scrolltx);
			Map_chunk *chunk = get_chunk(cx, cy);
			chunk->get_terrain()->render_all(cx, cy);
			if (cheat.in_map_editor())
				Paint_chunk_outline(this, hit_pixel, cx, cy,
				    map->get_terrain_num(cx, cy), xoff, yoff);
			}
		}
					// Paint tile grid if desired.
	if (cheat.show_tile_grid())
		Paint_grid(this, xforms[10]);
	}

/*
 *	Paint just the map and its objects (no gumps, effects).
 *	(The caller should set/clear clip area.)
 *
 *	Output:	# light-sources found.
 */

int Game_window::paint_map
	(
	int x, int y, int w, int h	// Rectangle to cover.
	)
	{
	render_seq++;			// Increment sequence #.
	painted = 1;
	int light_sources = 0;		// Count light sources found.
					// Get chunks to start with, starting
					//   1 tile left/above.
	int start_chunkx = (scrolltx + x/c_tilesize - 1)/c_tiles_per_chunk;
					// Wrap around.
	start_chunkx = (start_chunkx + c_num_chunks)%c_num_chunks;
	int start_chunky = (scrollty + y/c_tilesize - 1)/c_tiles_per_chunk;
	start_chunky = (start_chunky + c_num_chunks)%c_num_chunks;
					// End 8 tiles to right.
	int stop_chunkx = 1 + (scrolltx + (x + w + c_tilesize - 2)/c_tilesize + 
					c_tiles_per_chunk/2)/c_tiles_per_chunk;
	int stop_chunky = 1 + (scrollty + (y + h + c_tilesize - 2)/c_tilesize + 
					c_tiles_per_chunk/2)/c_tiles_per_chunk;
					// Wrap around the world:
	stop_chunkx = (stop_chunkx + c_num_chunks)%c_num_chunks;
	stop_chunky = (stop_chunky + c_num_chunks)%c_num_chunks;
	if (skip_lift == 0)		// Special mode for editing?
		{
		paint_terrain_only(start_chunkx, start_chunky,
						stop_chunkx, stop_chunky);
		return 10;		// Pretend there's lots of light!
		}
	int cx, cy;			// Chunk #'s.
					// Paint all the flat scenery.
	for (cy = start_chunky; cy != stop_chunky; cy = INCR_CHUNK(cy))
		{
		int yoff = Figure_screen_offset(cy, scrollty);
		for (cx = start_chunkx; cx != stop_chunkx; cx = INCR_CHUNK(cx))
			{
			int xoff = Figure_screen_offset(cx, scrolltx);
			paint_chunk_flats(cx, cy, xoff, yoff);

			if (cheat.in_map_editor())
				Paint_chunk_outline(this, hit_pixel, cx, cy,
				    map->get_terrain_num(cx, cy), xoff, yoff);
			}
		}
					// Draw the chunks' objects
					//   diagonally NE.
	int tmp_stopy = DECR_CHUNK(start_chunky);
	for (cy = start_chunky; cy != stop_chunky; cy = INCR_CHUNK(cy))
		{
		for (int dx = start_chunkx, dy = cy;
			dx != stop_chunkx && dy != tmp_stopy; 
				dx = INCR_CHUNK(dx), dy = DECR_CHUNK(dy))
			light_sources += paint_chunk_objects(dx, dy);
		}
	for (cx = (start_chunkx + 1)%c_num_chunks; cx != stop_chunkx; 
							cx = INCR_CHUNK(cx))
		{
		for (int dx = cx, 
			dy = (stop_chunky - 1 + c_num_chunks)%c_num_chunks; 
			dx != stop_chunkx && dy != tmp_stopy; 
				dx = INCR_CHUNK(dx), dy = DECR_CHUNK(dy))
			light_sources += paint_chunk_objects(dx, dy);
		}

	/// Dungeon Blackness (but disable in map editor mode)
	if (in_dungeon >= skip_above_actor && !cheat.in_map_editor())
		paint_blackness (start_chunkx, start_chunky, stop_chunkx, 
						stop_chunky, ice_dungeon?73:0);
					// Paint tile grid if desired.
	if (cheat.in_map_editor() && cheat.show_tile_grid())
		Paint_grid(this, xforms[10]);
	return light_sources;
	}

/*
 *	Paint a rectangle in the window by pulling in vga chunks.
 */

void Game_window::paint
	(
	int x, int y, int w, int h	// Rectangle to cover.
	)
	{
	if (!win->ready()) return;

	win->set_clip(x, y, w, h);	// Clip to this area.
	int light_sources = paint_map(x, y, w, h);

					// Draw gumps unless in dont_move mode.
    if (!main_actor_dont_move())
        gump_man->paint(this);
					// Draw text, sprites.
	for (Special_effect *txt = effects; txt; txt = txt->next)
		txt->paint(this);
	win->clear_clip();
					// Complete repaint?
	if (!x && !y && w == get_width() && h == get_height() && main_actor)
		{			// Look for lights.
		Actor *party[9];	// Get party, including Avatar.
		int cnt = get_party(party, 1);
		int carried_light = 0;
		for (int i = 0; !carried_light && i < cnt; i++)
			carried_light = party[i]->has_light_source();
					// Also check light spell.
		if (special_light && clock.get_total_minutes() > special_light)
			special_light = 0;
					// Set palette for lights.
		clock.set_light_source(carried_light + (light_sources > 0));
		}
	}

/*
 *	Paint whole window.
 */
void Game_window::paint()
	{
	map->read_map_data();		// Gather in all objs., etc.
	set_all_dirty();
	paint_dirty();
	}

/*
 *	Paint the flat (non-rle) shapes in a chunk.
 */

void Game_window::paint_chunk_flats
	(
	int cx, int cy,			// Chunk coords (0 - 12*16).
	int xoff, int yoff		// Pixel offset of top-of-screen.
	)
	{
	Map_chunk *olist = get_chunk(cx, cy);
					// Paint flat tiles.
	Image_buffer8 *cflats = olist->get_rendered_flats();
	if (cflats)
		win->copy8(cflats->get_bits(), c_chunksize, c_chunksize, 
								xoff, yoff);

	Flat_object_iterator next(olist);// Now do flat RLE objects.
	Game_object *obj;
	while ((obj = next.get_next()) != 0)
		obj->paint(this);
	}

/*
 *	Paint a chunk's objects, left-to-right, top-to-bottom.
 *
 *	Output:	# light sources found.
 */

int Game_window::paint_chunk_objects
	(
	int cx, int cy			// Chunk coords (0 - 12*16).
	)
	{
	Game_object *obj;
	Map_chunk *olist = get_chunk(cx, cy);
	int light_sources = 0;		// Also check for light sources.
//	if (is_main_actor_inside() && olist->is_roof()) +++++Correct??
		light_sources += olist->get_light_sources();
	int save_skip = skip_lift;
	if (skip_above_actor < skip_lift)
		skip_lift = skip_above_actor;
	Nonflat_object_iterator next(olist);

	while ((obj = next.get_next()) != 0)
		if (obj->render_seq != render_seq)
			paint_object(obj);

	skip_lift = save_skip;
	return light_sources;
	}

/*
 *	Render an object after first rendering any that it depends on.
 */

void Game_window::paint_object
	(
	Game_object *obj
	)
	{
	int lift = obj->get_lift();
	if (lift >= skip_lift)
		return;
	obj->render_seq = render_seq;
	int cnt = obj->get_dependency_count();
	for (int i = 0; i < cnt; i++)
		{
		Game_object *dep = obj->get_dependency(i);
		if (dep && dep->render_seq != render_seq)
			paint_object(dep);
		}
	obj->paint(this);		// Finally, paint this one.
	}

/*
 *	Paint 'dirty' rectangle.
 */

void Game_window::paint_dirty()
{
	// Update the gumps before painting, unless in dont_move mode (may change dirty area)
    if (!main_actor_dont_move())
        gump_man->update_gumps(this);

	Rectangle box = clip_to_win(dirty);
	if (box.w > 0 && box.h > 0)
		paint(box);	// (Could create new dirty rects.)

	clear_dirty();
}

/*
 *	Dungeon Blacking
 *
 *	This is really simple. If there is a dungeon roof over our head	we
 *	black out every tile on screen that doens't have a roof at the height
 *	of the roof that is directly over our head. The tiles are blacked out
 *	at the height of the the roof. 
 *
 *	I've done some simple optimizations. Generally all the blackness will
 *	cover entire chunks. So, instead of drawing each tile individually, I
 *	work out home many tiles in a row that need to be blacked out, and then
 *	black them all out at the same time.
 */

void Game_window::paint_blackness(int start_chunkx, int start_chunky, int stop_chunkx, int stop_chunky, int index)
{
	// Calculate the offset due to the lift (4x the lift).
	//const int off = skip_above_actor << 2;//in_dungeon << 2;
	const int off = in_dungeon << 2;

	// For each chunk that might be renderable
	for (int cy = start_chunky; cy != stop_chunky; cy = INCR_CHUNK(cy))
	{
		for (int cx = start_chunkx; cx != stop_chunkx; cx = INCR_CHUNK(cx))
		{
			// Coord of the left edge
			const int xoff = Figure_screen_offset(cx, scrolltx) - off;
			// Coord of the top edge 
			int y = Figure_screen_offset(cy, scrollty) - off;

			// Need the chunk cache (needs to be setup!)
			Map_chunk *mc = get_chunk(cx, cy);
			mc->setup_cache();
			Chunk_cache *chunk = mc->need_cache();

			// For each line in the chunk
			for (int tiley = 0; tiley < c_tiles_per_chunk; tiley++)
			{
				// Start and width of the area to black out
				int x = xoff;
				int w = 0;

				// For each tile in the line
				for (int tilex = 0; tilex < c_tiles_per_chunk; tilex++)
				{
					// If the tile is blocked by 'roof'
					if (!chunk->is_blocked_fast(tilex, tiley, skip_above_actor))
					{
						// Add to the width of the area
						w += c_tilesize;
					}
					// If not blocked and have area,
					else if (w)
					{
						// Draw blackness
						win->fill8(index, w, c_tilesize, x, y);	

						// Set the start of the area to the next tile
						x += w + c_tilesize;

						// Clear the width
						w = 0;	
					}
					// Not blocked, and no area
					else
					{
						// Increment the start of the area to the next tile
						x += c_tilesize;
					}

				}

				// If we have an area, paint it.
				if (w) win->fill8(index, w, c_tilesize, x, y);

				// Increment the y coord for the next line
				y += c_tilesize;
			}
		}
	}
}