-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiled_image.h
63 lines (50 loc) · 1.89 KB
/
tiled_image.h
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
#ifndef TILED_IMAGE_H
#define TILED_IMAGE_H
#include <osbind.h>
#include <cstdint>
#include <utility>
#include <cstdio>
#include "degas_picture.h"
namespace AtariGraphics {
struct SpriteDefinition {
const uint16_t SPRITE_WIDTH;
const uint16_t SPRITE_HEIGHT;
SpriteDefinition(uint16_t w, uint16_t h) : SPRITE_WIDTH(w),
SPRITE_HEIGHT(h) {
}
};
struct TiledImage {
static constexpr int IMAGE_SIZE = sizeof(DegasPicture::picture_data);
static constexpr int BITS_PER_PIXEL = 4;
static constexpr int IMAGE_WDWIDTH = 320 * BITS_PER_PIXEL / sizeof(uint16_t) / 8;
DegasPicture picture;
SpriteDefinition defs;
DegasPicture load(const char * const filename) {
short fh;
int32_t length = 0;
DegasPicture picture;
fh = Fopen(filename, 2);
if (fh > 0) {
length = Fread(fh, sizeof(DegasPicture), &picture);
Fclose(fh);
}
if (fh < 0 || length != sizeof(DegasPicture)) {
(void) Cconws("Error: image ");
(void) Cconws(filename);
(void) Cconws(" not found\r\n");
exit(1);
}
return picture;
}
TiledImage(const char * const filename, const SpriteDefinition& defs) : picture(load(filename)),
defs(defs) {
}
uint16_t* tile(const uint16_t tile_index) {
// return start address of tile # tile_index
// tiles are TILE_WIDTH pixels wide, image is IMAGE_WDWIDTH
// words wide
return picture.picture_data + tile_index * defs.SPRITE_WIDTH * BITS_PER_PIXEL / (sizeof(uint16_t) * 8);
}
};
}
#endif // TILED_IMAGE_H