summaryrefslogtreecommitdiff
path: root/src/tiledmap.c
blob: bb5a1fd0c809d2a3f71430fab9b78ef6b357dc5a (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <raylib.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "tiledmap.h"
#include "tiledio.h"

void buildChunkTree(TiledMap *tiledMap) {
    // initialise chunk tree
    tiledMap->chunkTree = NULL;

    size_t chunkSizeBytes = tiledMap->chunkWidth * tiledMap->chunkHeight;
    // calculate how much space there is until the end of the file
    // TODO save the number of chunks total to avoid having to jump around to find referenceso
    // or just keep reading chunks until eof or other descriptor
    long chunksStart = ftell(tiledMap->file);
    fseek(tiledMap->file, 0, SEEK_END);
    long totalChunksSize = ftell(tiledMap->file) - chunksStart;

    fseek(tiledMap->file, chunksStart, SEEK_SET);

    for (int n = 0; n < totalChunksSize; n += chunkSizeBytes + 8) {
        int x, y;
        readb((char *)&x, 4, tiledMap->file);
        readb((char *)&y, 4, tiledMap->file);
        long pointer = ftell(tiledMap->file);

        CachedChunk * cached = malloc(sizeof(CachedChunk));
        cached->filePos = pointer;
        cached->chunk = NULL;

        kdtree_insert(&tiledMap->chunkTree, x, y, (char *) cached);
        fseek(tiledMap->file, chunkSizeBytes, SEEK_CUR);
    }
}

TiledMap openTiledMap(char * filename) {
    TiledMap tiledMap;

    if (!(tiledMap.file = fopen(filename, "r+b"))) {
        fprintf(stderr, "Failed to load %s\n", filename);
    }

    FILE * file = tiledMap.file;
    
    // skip header 
    fseek(file, 10, SEEK_CUR);
    // 4 bytes for int width
    readb((char *)&tiledMap.chunkWidth, 4, file);
    // 4 bytes for int height
    readb((char *)&tiledMap.chunkHeight, 4, file);

    // read the pixel size of each tile
    readb((char *)&tiledMap.tileSize, 4, file);

    // read the atlas size
    readb((char *)&tiledMap.atlasSize[0], 4, file);
    readb((char *)&tiledMap.atlasSize[1], 4, file);

    // read the atlas itself
    size_t atlasSizeBytes = tiledMap.atlasSize[0]*tiledMap.tileSize*tiledMap.atlasSize[1]*tiledMap.tileSize*4;
    tiledMap.atlasData = malloc(atlasSizeBytes);
    fread(tiledMap.atlasData, atlasSizeBytes, (size_t) 1, file);
    
    tiledMap.tileCount = tiledMap.atlasSize[0]*tiledMap.atlasSize[1] + 1;

    buildChunkTree(&tiledMap);
    return tiledMap;
}

//! save chunk to file
void commitChunk(TiledMap * tiledMap, CachedChunk * cached) {
    size_t chunkSizeBytes = tiledMap->chunkWidth * tiledMap->chunkHeight;

    fseek(tiledMap->file, cached->filePos, SEEK_SET);
    fwrite(cached->chunk, 1, chunkSizeBytes, tiledMap->file);
}

void unloadChunk(TiledMap * tiledMap, CachedChunk * cached) {

    // do not cache an unloaded chunk
    if (cached->chunk == NULL)
        return;

    // commit a chunk before unloading
    commitChunk(tiledMap, cached);

    // free memory
    free(cached->chunk);
    cached->chunk = NULL;
    
}

//! load a chunk into the cache and return it 
CachedChunk * loadChunk(TiledMap * tiledMap, int x, int y) {
    size_t chunkSizeBytes = tiledMap->chunkWidth * tiledMap->chunkHeight;


    CachedChunk *cached = (CachedChunk * ) kdtree_search(tiledMap->chunkTree, x, y);
    // if this chunk is not indexed, return NULL
    if (cached == NULL)
        return NULL;

    // if this chunk is not loaded into memory, load it
    if (cached->chunk == NULL) {
        cached->chunk = malloc(chunkSizeBytes);
        fseek(tiledMap->file, cached->filePos, SEEK_SET);
        fread(cached->chunk, 1, chunkSizeBytes, tiledMap->file);
    }

    return cached;
}

CachedChunk * createChunk(TiledMap *tiledMap, int x, int y, Chunk chunk) {
    size_t chunkSizeBytes = tiledMap->chunkWidth * tiledMap->chunkHeight;

    fseek(tiledMap->file, 0, SEEK_END);

    // calculate position before writing
    long pos = ftell(tiledMap->file) + 8; 
    CachedChunk *cached = malloc(sizeof(CachedChunk));
    cached->filePos = pos;
    cached->chunk = chunk;

    kdtree_insert(&tiledMap->chunkTree, x, y, (char *) cached);

    int chunkx, chunky;
    writeb((char *) &x, 4, tiledMap->file);
    writeb((char *) &y, 4, tiledMap->file);
    fwrite(chunk, 1, chunkSizeBytes, tiledMap->file);

    return cached;
}

CachedChunk * createEmptyChunkAt(TiledMap * tiledMap, int x, int y) {
    Chunk chunk = calloc(tiledMap->chunkWidth*tiledMap->chunkHeight, 1);
    return createChunk(tiledMap, x / tiledMap->chunkWidth, y / tiledMap->chunkHeight, chunk);
}

CachedChunk * loadChunkPosition(TiledMap *tiledMap, int x, int y, int * index) {
    int inChunkX = x % tiledMap->chunkWidth;
    int inChunkY = y % tiledMap->chunkHeight;
    int chunkX = (x - inChunkX) / tiledMap->chunkWidth;
    int chunkY = (y - inChunkY) / tiledMap->chunkHeight;
    *index = inChunkY * tiledMap->chunkWidth + inChunkX;

    return loadChunk(tiledMap, chunkX, chunkY);
}

Tile getChunkedTile(TiledMap *tiledMap, int x, int y) {
    if (x < 0 || y < 0)
        return 0;

    int index;
    CachedChunk * cached = loadChunkPosition(tiledMap, x, y, &index);

    if (cached == NULL)
        return 0;

    if (cached->chunk == NULL)
        return 0;

    return cached->chunk[index];
}

Tile setChunkedTile(TiledMap * tiledMap, int x, int y, Tile value) {
    if (x < 0 || y < 0)
        return 0;

    int index;
    CachedChunk * cached = loadChunkPosition(tiledMap, x, y, &index);

    if (cached == NULL)
        cached = createEmptyChunkAt(tiledMap, x, y);

    cached->chunk[index] = value;

    return value;
}


void writeTiledMapHeader(TiledMap tiledMap) {
    FILE * file = tiledMap.file;
    size_t atlasSizeBytes = tiledMap.atlasSize[0]*tiledMap.tileSize*tiledMap.atlasSize[1]*tiledMap.tileSize*4;

    // write header information from the start of the file
    fseek(file, 0, SEEK_SET);
    fwrite("TILEFILEv3", 10, 1, file);

    writeb((char *) &tiledMap.chunkWidth, 4, file);
    writeb((char *) &tiledMap.chunkHeight, 4, file);

    writeb((char *) &tiledMap.tileSize, 4, file);
    writeb((char *) &tiledMap.atlasSize[0], 4, file);
    writeb((char *) &tiledMap.atlasSize[1], 4, file);

    fwrite(tiledMap.atlasData, 1, atlasSizeBytes, file);
}

TiledMap openNewTiledMap(char * filename, Image atlas, int tileSize, int chunkWidth, int chunkHeight, int width, int height) {
    TiledMap tiledMap;
    tiledMap.chunkWidth = chunkWidth;
    tiledMap.chunkHeight = chunkHeight;

    tiledMap.tileSize = tileSize;

    tiledMap.atlasSize[0] = atlas.width / tileSize;
    tiledMap.atlasSize[1] = atlas.height / tileSize;

    tiledMap.atlasData = LoadImageColors(atlas);
    tiledMap.tileCount = tiledMap.atlasSize[0]*tiledMap.atlasSize[1] + 1;

    tiledMap.chunkTree = NULL;

    if (!(tiledMap.file = fopen(filename, "wb"))) {
        fprintf(stderr, "Failed to load %s\n", filename);
    }

    writeTiledMapHeader(tiledMap);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            Chunk chunk = calloc(chunkWidth*chunkHeight, 1);
            createChunk(&tiledMap, x, y,  chunk);
        }
    }
    
    // reopen the file in read+write mode
    fclose(tiledMap.file);

    if (!(tiledMap.file = fopen(filename, "r+b"))) {
        fprintf(stderr, "Failed to load %s\n", filename);
    }

    return tiledMap;
}

void unloadChunks(TiledMap *tiledMap, kdtree_t * root) {
    if (root == NULL)
        return;

    unloadChunks(tiledMap, root->left);
    unloadChunks(tiledMap, root->right);
    unloadChunk(tiledMap, (CachedChunk *)root->value);
}

void closeTiledMap(TiledMap *tiledMap) {
    unloadChunks(tiledMap, tiledMap->chunkTree);
    kdtree_free(&tiledMap->chunkTree);
    UnloadImageColors(tiledMap->atlasData);
    fclose(tiledMap->file);
}