-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilemap-engine.c
More file actions
68 lines (54 loc) · 1.71 KB
/
tilemap-engine.c
File metadata and controls
68 lines (54 loc) · 1.71 KB
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
#include "tilemap-engine.h"
Viewport viewport = {.x=0, .y=0};
void draw_map(const Map __memx *map)
{
int16_t x = viewport.x;
int16_t y = viewport.y;
uint8_t x_offset = x & 7; // x % 8
x >>= 3;
uint8_t y_offset = y & 7; // y % 8
y >>= 3;
uint8_t NUM_ROWS = SCREEN_ROWS;
if (y_offset > 0)
NUM_ROWS += 1;
uint8_t NUM_COLS = SCREEN_COLUMNS;
if (x_offset > 0)
NUM_COLS += 1;
for (uint8_t row=0 ; row<NUM_ROWS ; row++)
{
for (uint8_t col=0 ; col<NUM_COLS ; col++)
{
draw_tile(&map->tileset[map->tiles[map->cols * (row+y) + (col+x)]*8], &BLOCK_MASKS[OPAQUE], col*8-x_offset, row*8-y_offset);
}
}
}
void draw_sprite(Sprite *s)
{
uint16_t x = s->x - (s->width>>1);
uint16_t y = s->y - (s->height>>1);
for(uint8_t rows=0 ; rows<(s->height>>3) ; rows++)
{
for(uint8_t cols=0 ; cols<(s->width>>3) ; cols++)
{
draw_tile(s->tile + cols*8 + rows*s->width, s->mask, (x+(cols*8))-viewport.x, (y+(rows*8))-viewport.y);
}
}
//draw_tile(s->tile, s->mask, s->x-viewport.x, s->y-viewport.y);
}
void center_on_sprite(Sprite *s, const Map __memx *map)
{
viewport.x = s->x - (SCREEN_WIDTH>>1);
viewport.y = s->y - (SCREEN_HEIGHT>>1);
if (viewport.x < 0)
viewport.x = 0;
if (viewport.y < 0)
viewport.y = 0;
if (viewport.x > map->cols*8 - SCREEN_WIDTH)
viewport.x = map->cols*8 - SCREEN_WIDTH;
if (viewport.y > map->rows*8 - SCREEN_HEIGHT)
viewport.y = map->rows*8 - SCREEN_HEIGHT;
}
uint8_t tile_at_xy(const __memx Map* m, uint16_t x, uint16_t y)
{
return m->tiles[ (y>>3) * m->cols + (x>>3) ];
}