-
Notifications
You must be signed in to change notification settings - Fork 86
/
tilemap.c
89 lines (76 loc) · 2.63 KB
/
tilemap.c
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
/* libmypaint - The MyPaint Brush Library
* Copyright (C) 2012 Jon Nordby <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include "tilemap.h"
TileMap *
tile_map_new(int size, size_t item_size, TileMapItemFreeFunc item_free_func)
{
TileMap *self = (TileMap *)malloc(sizeof(TileMap));
self->size = size;
self->item_size = item_size;
self->item_free_func = item_free_func;
const int map_size = 2*self->size*2*self->size;
self->map = malloc(map_size*self->item_size);
for(int i = 0; i < map_size; i++) {
self->map[i] = NULL;
}
return self;
}
void
tile_map_free(TileMap *self, gboolean free_items)
{
const int map_size = 2*self->size*2*self->size;
if (free_items) {
for(int i = 0; i < map_size; i++) {
self->item_free_func(self->map[i]);
}
}
free(self->map);
free(self);
}
/* Get the data in the tile map for a given tile @index.
* Must be reentrant and lock-free on different @index */
void **
tile_map_get(TileMap *self, TileIndex index)
{
const int rowstride = self->size*2;
const int offset = ((self->size + index.y) * rowstride) + self->size + index.x;
assert(offset < 2*self->size*2*self->size);
assert(offset >= 0);
return self->map + offset;
}
/* Copy
* The size of @other must be equal or larger to that of @self */
void
tile_map_copy_to(TileMap *self, TileMap *other)
{
assert(other->size >= self->size);
for(int y = -self->size; y < self->size; y++) {
for(int x = -self->size; x < self->size; x++) {
TileIndex index = {x, y};
*tile_map_get(other, index) = *tile_map_get(self, index);
}
}
}
/* Must be reentrant and lock-free on different @index */
gboolean
tile_map_contains(TileMap *self, TileIndex index)
{
return (index.x >= -self->size && index.x < self->size
&& index.y >= -self->size && index.y < self->size);
}