1 /*
2     C-Dogs SDL
3     A port of the legendary (and fun) action/arcade cdogs.
4     Copyright (C) 1995 Ronny Wester
5     Copyright (C) 2003 Jeremy Chin
6     Copyright (C) 2003-2007 Lucas Martin-King
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22     This file incorporates work covered by the following copyright and
23     permission notice:
24 
25     Copyright (c) 2013-2014, 2018-2019 Cong Xu
26     All rights reserved.
27 
28     Redistribution and use in source and binary forms, with or without
29     modification, are permitted provided that the following conditions are met:
30 
31     Redistributions of source code must retain the above copyright notice, this
32     list of conditions and the following disclaimer.
33     Redistributions in binary form must reproduce the above copyright notice,
34     this list of conditions and the following disclaimer in the documentation
35     and/or other materials provided with the distribution.
36 
37     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47     POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "draw/draw_buffer.h"
50 
51 #include <assert.h>
52 
53 #include "algorithms.h"
54 #include "log.h"
55 #include "los.h"
56 
57 
DrawBufferInit(DrawBuffer * b,struct vec2i size,GraphicsDevice * g)58 void DrawBufferInit(DrawBuffer *b, struct vec2i size, GraphicsDevice *g)
59 {
60 	b->OrigSize = size;
61 	CArrayInit(&b->tiles, sizeof(Tile *));
62 	for (int i = 0; i < size.x * size.y; i++)
63 	{
64 		const Tile *t = NULL;
65 		CArrayPushBack(&b->tiles, &t);
66 	}
67 	b->g = g;
68 	CArrayInit(&b->displaylist, sizeof(const Thing *));
69 	CArrayReserve(&b->displaylist, 32);
70 }
DrawBufferTerminate(DrawBuffer * b)71 void DrawBufferTerminate(DrawBuffer *b)
72 {
73 	CArrayTerminate(&b->tiles);
74 	CArrayTerminate(&b->displaylist);
75 }
76 
DrawBufferSetFromMap(DrawBuffer * buffer,const Map * map,const struct vec2 origin,const int width)77 void DrawBufferSetFromMap(
78 	DrawBuffer *buffer, const Map *map, const struct vec2 origin,
79 	const int width)
80 {
81 	buffer->Size = svec2i(width, buffer->OrigSize.y);
82 
83 	buffer->xTop = (int)origin.x - TILE_WIDTH * width / 2;
84 	buffer->yTop = (int)origin.y - TILE_HEIGHT * buffer->OrigSize.y / 2;
85 
86 	buffer->xStart = buffer->xTop / TILE_WIDTH;
87 	buffer->yStart = buffer->yTop / TILE_HEIGHT;
88 	if (buffer->xTop < 0)
89 	{
90 		buffer->xStart--;
91 	}
92 	if (buffer->yTop < 0)
93 	{
94 		buffer->yStart--;
95 	}
96 
97 	buffer->dx = buffer->xStart * TILE_WIDTH - buffer->xTop;
98 	buffer->dy = buffer->yStart * TILE_HEIGHT - buffer->yTop;
99 
100 	Tile **bufTile = CArrayGet(&buffer->tiles, 0);
101 	struct vec2i pos;
102 	for (pos.y = buffer->yStart;
103 		pos.y < buffer->yStart + buffer->Size.y;
104 		pos.y++)
105 	{
106 		for (pos.x = buffer->xStart;
107 			pos.x < buffer->xStart + buffer->Size.x;
108 			pos.x++, bufTile++)
109 		{
110 			if (MapIsTileIn(map, pos))
111 			{
112 				*bufTile = MapGetTile(map, pos);
113 			}
114 			else
115 			{
116 				*bufTile = NULL;
117 			}
118 		}
119 		bufTile += buffer->OrigSize.x - buffer->Size.x;
120 	}
121 }
122 
123 // Set visibility and draw order for wall/door columns
DrawBufferFix(DrawBuffer * buffer)124 void DrawBufferFix(DrawBuffer *buffer)
125 {
126 	int tileIdx = 0;
127 	for (int y = 0; y < Y_TILES; y++)
128 	{
129 		for (int x = 0; x < buffer->Size.x; x++, tileIdx++)
130 		{
131 			Tile **tile = CArrayGet(&buffer->tiles, tileIdx);
132 			if (*tile == NULL) continue;
133 			const struct vec2i mapTile =
134 				svec2i(x + buffer->xStart, y + buffer->yStart);
135 			(*tile)->outOfSight = !LOSTileIsVisible(&gMap, mapTile);
136 		}
137 		tileIdx += X_TILES - buffer->Size.x;
138 	}
139 }
140 
141 static int CompareY(const void *v1, const void *v2);
DrawBufferSortDisplayList(DrawBuffer * buffer)142 void DrawBufferSortDisplayList(DrawBuffer *buffer)
143 {
144 	qsort(
145 		buffer->displaylist.data,
146 		buffer->displaylist.size,
147 		buffer->displaylist.elemSize,
148 		CompareY);
149 }
CompareY(const void * v1,const void * v2)150 static int CompareY(const void *v1, const void *v2)
151 {
152 	const Thing * const *t1 = v1;
153 	const Thing * const *t2 = v2;
154 	if ((*t1)->Pos.y < (*t2)->Pos.y)
155 	{
156 		return -1;
157 	}
158 	else if ((*t1)->Pos.y >(*t2)->Pos.y)
159 	{
160 		return 1;
161 	}
162 	return 0;
163 }
164 
DrawBufferGetFirstTile(const DrawBuffer * b)165 const Tile **DrawBufferGetFirstTile(const DrawBuffer *b)
166 {
167 	return CArrayGet(&b->tiles, 0);
168 }
169