summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-12-27 02:39:15 +0000
committerdavidovski <david@davidovski.xyz>2023-12-27 02:39:15 +0000
commit00bade5c186bc0c1d703bb8d52afdabb1ff0ab92 (patch)
tree22fd992cf5f61d97af0677e97d10c164a59e0437 /src
parent5651b14b3084d18061516c65fa58b47134b926fe (diff)
remove whitespace
Diffstat (limited to 'src')
-rw-r--r--src/editor.c4
-rw-r--r--src/kdtree.c8
-rw-r--r--src/tiled.c1
-rw-r--r--src/tiled.h2
-rw-r--r--src/tiledio.c51
-rw-r--r--src/tiledio.h8
-rw-r--r--src/tiledmap.c14
7 files changed, 74 insertions, 14 deletions
diff --git a/src/editor.c b/src/editor.c
index 73c1e5a..aa541e9 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -12,7 +12,7 @@ int mode = -1;
void drawOverlay(Tiled tiled) {
Vector2 screenPos = translateTiledScreenPosition(tiled, (Vector2){selectedTile[0], selectedTile[1]});
-
+
DrawRectangleLinesEx((Rectangle) {
screenPos.x,
screenPos.y,
@@ -117,7 +117,7 @@ int main(int argc, char *argv[]) {
atlasFilePath = optarg;
}
}
-
+
if (optind >= argc)
printUsage(argv[0]);
diff --git a/src/kdtree.c b/src/kdtree.c
index cfa5866..543bf4e 100644
--- a/src/kdtree.c
+++ b/src/kdtree.c
@@ -1,7 +1,7 @@
#include "kdtree.h"
void print_node(kdtree_t *tree) {
- printf("[%d,%d] %ld\n", tree->x, tree->y, (long)tree->value);
+ printf("[%d,%d] %ld\n", tree->x, tree->y, (long)tree->value);
}
kdtree_t * kdtree_create(int x, int y, char * value) {
@@ -36,7 +36,7 @@ char * kdtree_search_rec(kdtree_t *root, int x, int y, int depth) {
if (root->x == x && root->y == y)
return root->value;
-
+
unsigned int r, p;
if (depth % 2 == 0) {
r = root->x;
@@ -66,7 +66,7 @@ char * kdtree_search(kdtree_t *root, int x, int y) {
void kdtree_free(kdtree_t **root) {
if ((*root)->left != NULL)
kdtree_free(&(*root)->left);
-
+
if ((*root)->right != NULL)
kdtree_free(&(*root)->right);
@@ -85,7 +85,7 @@ void kdtree_walk(kdtree_t *root, void (* consume)(kdtree_t*)) {
int kdtree_size(kdtree_t *root) {
if (root == NULL)
return 0;
-
+
return 1 + kdtree_size(root->left) + kdtree_size(root->right);
}
diff --git a/src/tiled.c b/src/tiled.c
index 436029b..3ee8a0b 100644
--- a/src/tiled.c
+++ b/src/tiled.c
@@ -74,6 +74,7 @@ void redrawTile(Tiled tiled, int x, int y) {
void redrawTiledMap(Tiled tiled) {
// TODO since we have to do this every time a chunk is loaded, maybe there is a better way
+ // TODO send chunks that are visible to shader and let shader figure out how to arrange / draw them
int realX, realY;
Tile v;
diff --git a/src/tiled.h b/src/tiled.h
index ae8fcc5..7bae861 100644
--- a/src/tiled.h
+++ b/src/tiled.h
@@ -26,7 +26,7 @@ typedef struct {
RenderTexture2D tilemapTexture;
RenderTexture2D targetTexture;
- Shader shader;
+ Shader shader;
int zoomLoc;
int offsetLoc;
diff --git a/src/tiledio.c b/src/tiledio.c
new file mode 100644
index 0000000..2d06ba7
--- /dev/null
+++ b/src/tiledio.c
@@ -0,0 +1,51 @@
+#include "tiledio.h"
+
+const int endian = 1;
+#define is_bigendian() ( (*(char*)&endian) == 0 )
+
+
+void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height) {
+ Image checkedIm = {
+ .data = pixels,
+ .width = width,
+ .height = height,
+ .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
+ .mipmaps = 1
+ };
+
+ *texOut = LoadTextureFromImage(checkedIm);
+}
+
+
+int readb(char * out, size_t noBytes, FILE * file) {
+ if (!fread(out, (size_t)1, (size_t) noBytes, file))
+ return 1;
+
+ if (is_bigendian())
+ return 0;
+
+ int tmp;
+ // reverse byte order
+ for(int i = 0; i < noBytes/2; i++) {
+ tmp = out[i];
+ out[i] = out[noBytes-i-1];
+ out[noBytes-i-1] = tmp;
+ }
+
+ return 0;
+}
+
+int writeb(char * in, size_t noBytes, FILE * file) {
+ if (!is_bigendian()) {
+ int tmp;
+ // reverse byte order
+ for(int i = 0; i < noBytes/2; i++) {
+ tmp = in[i];
+ in[i] = in[noBytes-i-1];
+ in[noBytes-i-1] = tmp;
+ }
+ }
+
+ return fwrite(in, (size_t)1, (size_t) noBytes, file);
+}
+
diff --git a/src/tiledio.h b/src/tiledio.h
new file mode 100644
index 0000000..9083ede
--- /dev/null
+++ b/src/tiledio.h
@@ -0,0 +1,8 @@
+#include <raylib.h>
+#include <stdio.h>
+
+void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height);
+
+int readb(char *out, size_t noBytes, FILE *file);
+
+int writeb(char *in, size_t noBytes, FILE *file);
diff --git a/src/tiledmap.c b/src/tiledmap.c
index a567ce3..aed86de 100644
--- a/src/tiledmap.c
+++ b/src/tiledmap.c
@@ -42,8 +42,8 @@ TiledMap openTiledMap(char * filename) {
}
FILE * file = tiledMap.file;
-
- // skip header
+
+ // skip header
fseek(file, 10, SEEK_CUR);
// 4 bytes for int width
readb((char *)&tiledMap.chunkWidth, 4, file);
@@ -61,7 +61,7 @@ TiledMap openTiledMap(char * filename) {
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);
@@ -88,10 +88,10 @@ void unloadChunk(TiledMap * tiledMap, CachedChunk * cached) {
// free memory
free(cached->chunk);
cached->chunk = NULL;
-
+
}
-//! load a chunk into the cache and return it
+//! load a chunk into the cache and return it
CachedChunk * loadChunk(TiledMap * tiledMap, int x, int y) {
size_t chunkSizeBytes = tiledMap->chunkWidth * tiledMap->chunkHeight;
@@ -117,7 +117,7 @@ CachedChunk * createChunk(TiledMap *tiledMap, int x, int y, Chunk chunk) {
fseek(tiledMap->file, 0, SEEK_END);
// calculate position before writing
- long pos = ftell(tiledMap->file) + 8;
+ long pos = ftell(tiledMap->file) + 8;
CachedChunk *cached = malloc(sizeof(CachedChunk));
cached->filePos = pos;
cached->chunk = chunk;
@@ -223,7 +223,7 @@ TiledMap openNewTiledMap(char * filename, Image atlas, int tileSize, int chunkWi
createChunk(&tiledMap, x, y, chunk);
}
}
-
+
// reopen the file in read+write mode
fclose(tiledMap.file);