1 /*
2 Copyright (c) 2009-2010 Tero Lindeman (kometbomb)
3 
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 
27 #include "background.h"
28 #include "gfx.h"
29 #include <stdlib.h>
30 #include <string.h>
31 
bg_check_collision(const Background * bg,const ObjHdr * object,ObjHdr * collided_object)32 int bg_check_collision(const Background *bg, const ObjHdr *object, ObjHdr *collided_object)
33 {
34 	int right = (object->x + object->w) / CELLSIZE, bottom = (object->y + object->h) / CELLSIZE;
35 	int top = (object->y) / CELLSIZE-1, left = (object->x) / CELLSIZE-1;
36 
37 	ObjHdr _hdr;
38 	ObjHdr *hdr = (collided_object != NULL) ? collided_object : &_hdr;
39 
40 	hdr->w = CELLSIZE;
41 	hdr->h = CELLSIZE;
42 	hdr->y = top * CELLSIZE;
43 
44 	for (int y = top ; y <= bottom ; ++y)
45 	{
46 		hdr->x = left * CELLSIZE;
47 
48 		int _y = y;
49 
50 		if (bg->flags & BG_REPEAT_Y) _y = ((y % bg->h)+bg->h) % bg->h;
51 		else if (y >= bg->h) break;
52 
53 		//printf("%d %d\n", hdr.x, object->x);
54 
55 
56 		if (_y >= 0)
57 		{
58 			for (int x = left ; x <= right ; ++x)
59 			{
60 				int _x = x;
61 
62 				if (bg->flags & BG_REPEAT_X) _x = ((x % bg->w)+bg->w) % bg->w;
63 				else if (x >= bg->w) break;
64 
65 				if (_x >= 0)
66 				{
67 					if ( bg->data[_x+_y*bg->w].tile && !(bg->tiles[bg->data[_x+_y*bg->w].tile-1].flags & TILE_COL_DISABLE))
68 					{
69 						hdr->objflags = bg->tiles[bg->data[_x+_y*bg->w].tile-1].flags;
70 						hdr->surface = bg->tiles[bg->data[_x+_y*bg->w].tile-1].surface;
71 						hdr->current_frame = bg->tiles[bg->data[_x+_y*bg->w].tile-1].rect.x / CELLSIZE;
72 						hdr->_yofs = bg->tiles[bg->data[_x+_y*bg->w].tile-1].rect.y;
73 
74 						if (objhdr_check_collision(object, hdr))
75 						{
76 							return 1;
77 						}
78 					}
79 				}
80 
81 				hdr->x += CELLSIZE;
82 			}
83 		}
84 
85 		hdr->y += CELLSIZE;
86 	}
87 
88 	return 0;
89 }
90 
bg_draw(GfxDomain * surface,const SDL_Rect * dest,const Background * bg,int xofs,int yofs)91 void bg_draw(GfxDomain *surface, const SDL_Rect * dest, const Background *bg, int xofs, int yofs)
92 {
93 	int sx = 0;
94 	int sy = 0;
95 
96 	const SDL_Rect def = {0, 0, surface->screen_w, surface->screen_h};
97 
98 	if (dest == NULL)
99 		dest = &def;
100 
101 	for (int y = (yofs) / CELLSIZE - 1; sy <= dest->h + 2 * CELLSIZE ; ++y, sy += CELLSIZE)
102 	{
103 		sx = 0;
104 
105 		int _y = y;
106 
107 		if (bg->flags & BG_REPEAT_Y) _y = ((y % bg->h)+bg->h) % bg->h;
108 			else if (y < 0) continue;
109 			else if (y >= bg->h) break;
110 
111 		for (int x = (xofs) / CELLSIZE - 1 ; sx <= dest->w + 2 * CELLSIZE ; ++x, sx += CELLSIZE)
112 		{
113 			int _x = x;
114 
115 			if (bg->flags & BG_REPEAT_X) _x = ((x % bg->w)+bg->w) % bg->w;
116 				else if (x < 0) continue;
117 				else if (x >= bg->w) break;
118 
119 			BgCell * cell = &bg->data[_y*bg->w + _x];
120 
121 			if ( cell->tile )
122 			{
123 				SDL_Rect rect = { sx - (xofs % (CELLSIZE)) + dest->x - CELLSIZE, sy - (yofs % (CELLSIZE)) + dest->y - CELLSIZE, CELLSIZE, CELLSIZE };
124 				my_BlitSurface(bg->tiles[cell->tile-1].surface, &bg->tiles[cell->tile-1].rect, surface, &rect);
125 				// SDL_FillRect(surface, &rect, 0x808080);
126 			}
127 
128 		}
129 	}
130 
131 }
132 
133 
bg_check_collision_chained(const Background * bg,const ObjHdr * head,ObjHdr * collided_tile)134 const ObjHdr * bg_check_collision_chained(const Background *bg, const ObjHdr *head, ObjHdr *collided_tile)
135 {
136 	const ObjHdr *ptr;
137 	objhdr_walk(ptr, head, if (bg_check_collision(bg, ptr, collided_tile)) return ptr);
138 	return NULL;
139 }
140 
141 
bg_create_tile_objhdr(ObjHdr * object_array,const Background * bg,int x,int y,int w,int h,int zero_src,TileDescriptor * tiles)142 int bg_create_tile_objhdr(ObjHdr* object_array, const Background *bg, int x, int y, int w, int h, int zero_src, TileDescriptor *tiles)
143 {
144 	int items = 0;
145 	ObjHdr *obj = object_array;
146 
147 	if (!tiles)
148 		tiles = bg->tiles;
149 
150 	for (int _y = y ; _y < h + y ; ++_y)
151 	{
152 		for (int _x = x ; _x < w + x ; ++_x)
153 		{
154 			if (bg->data[_x + _y * bg->w].tile)
155 			{
156 				if (obj != NULL)
157 				{
158 					obj->objflags = tiles[bg->data[_x + _y * bg->w].tile-1].flags & OBJ_COL_FLAGS;
159 					obj->x = (_x - x) * CELLSIZE;
160 					obj->y = (_y - y) * CELLSIZE;
161 					obj->w = obj->h = CELLSIZE;
162 					obj->anim = NULL;
163 					obj->current_frame = tiles[bg->data[_x + _y * bg->w].tile-1].rect.x / CELLSIZE;
164 					obj->_yofs = tiles[bg->data[_x + _y * bg->w].tile-1].rect.y;
165 					obj->surface = tiles[bg->data[_x + _y * bg->w].tile-1].surface;
166 					obj = obj->next;
167 					if (zero_src) bg->data[_x + _y * bg->w].tile = 0;
168 				}
169 				++items;
170 			}
171 		}
172 	}
173 
174 	if (items > 0) object_array[items-1].next = NULL;
175 
176 	return items;
177 }
178 
179 
bg_create(Background * bg,int w,int h)180 void bg_create(Background *bg, int w, int h)
181 {
182 	memset(bg, 0, sizeof(*bg));
183 	bg->data = calloc(w * h, sizeof(bg->data[0]));
184 	bg->w = w;
185 	bg->h = h;
186 }
187 
188 
bg_destroy(Background * bg)189 void bg_destroy(Background *bg)
190 {
191 	free(bg->data);
192 	memset(bg, 0, sizeof(*bg));
193 }
194