From b87245927e08887af7746ce0f7a91d3f73af0572 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 10 Jul 2023 00:29:01 +0100 Subject: cleanup project --- atlas.png | Bin 0 -> 875 bytes createmap.py | 23 --------------- makefile | 4 +-- src/tiled.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tiledfile.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tiledfile.h | 3 ++ tiled.c | 81 ----------------------------------------------------- tiled.png | Bin 875 -> 0 bytes tiledfile.c | 77 -------------------------------------------------- tiledfile.h | 3 -- tools/createmap.py | 23 +++++++++++++++ 11 files changed, 189 insertions(+), 186 deletions(-) create mode 100644 atlas.png delete mode 100755 createmap.py create mode 100644 src/tiled.c create mode 100644 src/tiledfile.c create mode 100644 src/tiledfile.h delete mode 100644 tiled.c delete mode 100644 tiled.png delete mode 100644 tiledfile.c delete mode 100644 tiledfile.h create mode 100755 tools/createmap.py diff --git a/atlas.png b/atlas.png new file mode 100644 index 0000000..455750a Binary files /dev/null and b/atlas.png differ diff --git a/createmap.py b/createmap.py deleted file mode 100755 index 59fb315..0000000 --- a/createmap.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python -import sys - -outfile = "map.tiles" - -WIDTH = 500 -HEIGHT = 100 -# number of bytes each tile needs to represent -TILEBYTES = 25 - -HEADER = "TILEFILEv1" - -with open(outfile, "wb") as file: - file.write(bytes(HEADER, "ascii")) - file.write(WIDTH.to_bytes(4, 'big')); - file.write(HEIGHT.to_bytes(4, 'big')); - file.write(TILEBYTES.to_bytes(4, 'big')); - - for y in range(HEIGHT): - for x in range(WIDTH): - index = (x + y) % 5 - file.write(index.to_bytes(TILEBYTES, 'big')); - diff --git a/makefile b/makefile index 4c0072f..43709b3 100644 --- a/makefile +++ b/makefile @@ -6,8 +6,8 @@ FLAGS=-lm -lraylib install: tiled cp tiled ${PREFIX}/bin/ -build: tiled.c tiledfile.c tiledfile.h - ${CC} tiled.c tiledfile.c -o tiled ${FLAGS} +build: src/*.c src/*.h + ${CC} src/*.c -o tiled ${FLAGS} clean: tiled rm tiled diff --git a/src/tiled.c b/src/tiled.c new file mode 100644 index 0000000..2cb1cb0 --- /dev/null +++ b/src/tiled.c @@ -0,0 +1,81 @@ +#include +#include + +#include "tiledfile.h" + +#define SCREEN_W 1280 +#define SCREEN_H 720 + +const int atlasSize[2] = {2, 2}; + +int main() { + InitWindow(SCREEN_W, SCREEN_H, "tiled"); + + Shader shader = LoadShader(0, "tiled.glsl"); + + Texture2D tilemap = loadTileMap("map.tiles"); + + Texture atlas = LoadTexture("atlas.png"); + RenderTexture2D target = LoadRenderTexture(SCREEN_W, SCREEN_H); + + float resolution[2] = {SCREEN_W, SCREEN_H}; + float offset[2] = {0, 0}; + float zoom = 16.0f; + int mapSize[2] = {tilemap.width, tilemap.height}; + + + int resolutionLoc = GetShaderLocation(shader, "resolution"); + int locationLoc = GetShaderLocation(shader, "offset"); + int zoomLoc = GetShaderLocation(shader, "zoom"); + + int atlasSizeLoc = GetShaderLocation(shader, "atlasSize"); + int mapSizeLoc = GetShaderLocation(shader, "mapSize"); + + int textureLoc = GetShaderLocation(shader, "texture1"); + int tilemapLoc = GetShaderLocation(shader, "texture2"); + + while (!WindowShouldClose()) { + if (IsKeyDown(KEY_UP)) offset[1] += zoom * 0.01f; + if (IsKeyDown(KEY_DOWN)) offset[1] -= zoom * 0.01f; + if (IsKeyDown(KEY_RIGHT)) offset[0] -= zoom * 0.01f; + if (IsKeyDown(KEY_LEFT)) offset[0] += zoom * 0.01f; + + if (IsKeyDown(KEY_W)) zoom -= zoom * 0.01f; + if (IsKeyDown(KEY_S)) zoom += zoom * 0.01f; + + SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2); + SetShaderValue(shader, locationLoc, &offset, SHADER_UNIFORM_VEC2); + SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT); + + SetShaderValue(shader, atlasSizeLoc, &atlasSize, SHADER_UNIFORM_IVEC2); + SetShaderValue(shader, mapSizeLoc, &tilemap.width, SHADER_UNIFORM_IVEC2); + + BeginDrawing(); + + ClearBackground(LIGHTGRAY); + + BeginTextureMode(target); + DrawRectangle(0, 0, SCREEN_W, SCREEN_H, BLACK); + EndTextureMode(); + + BeginShaderMode(shader); + SetShaderValueTexture(shader, textureLoc, atlas); + SetShaderValueTexture(shader, tilemapLoc, tilemap); + + // draw the base image to texture0 + DrawTexture(target.texture, 0, 0, WHITE); + EndShaderMode(); + + DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY); + + EndDrawing(); + } + + UnloadShader(shader); + UnloadRenderTexture(target); + UnloadTexture(atlas); + + CloseWindow(); + + return 0; +} diff --git a/src/tiledfile.c b/src/tiledfile.c new file mode 100644 index 0000000..c1ba42e --- /dev/null +++ b/src/tiledfile.c @@ -0,0 +1,80 @@ +#include +#include +#include + +const int i = 1; +#define is_bigendian() ( (*(char*)&i) == 0 ) + +Texture2D processTilemapTexture(int * tilelayout, int width, int height) { + Color *pixels = (Color*) malloc(width * height * sizeof(Color)); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int tile = tilelayout[y*width + x]; + pixels[y*width + x] = (Color){ + (int) tile, 0, 0, 0 + }; + } + } + + Image checkedIm = { + .data = pixels, + .width = width, + .height = height, + .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, + .mipmaps = 1 + }; + + Texture2D checked = LoadTextureFromImage(checkedIm); + UnloadImage(checkedIm); + + return checked; +} + +//! read a big endian bytes from file +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; +} + + +Texture2D loadTileMap(char *filename) { + int width, height, tilebytes; + int *tilelayout; + + FILE *file; + + file = fopen(filename, "rb"); + // skip header + fseek(file, 10, SEEK_CUR); + // 4 bytes for int width + readb((char *)&width, 4, file); + // 4 bytes for int height + readb((char *)&height, 4, file); + // 1 byte saying how big each tile is + readb((char *)&tilebytes, 4, file); + printf("tilebytes: %d\n", tilebytes); + + tilelayout = malloc(width*height*tilebytes); + int tile; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + readb((char *)&tile, tilebytes, file); + tilelayout[y*width + x] = tile; + } + } + return processTilemapTexture(tilelayout, width, height); +} diff --git a/src/tiledfile.h b/src/tiledfile.h new file mode 100644 index 0000000..cf2980b --- /dev/null +++ b/src/tiledfile.h @@ -0,0 +1,3 @@ +#include + +Texture2D loadTileMap(char *filename); diff --git a/tiled.c b/tiled.c deleted file mode 100644 index 715a91d..0000000 --- a/tiled.c +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include - -#include "tiledfile.h" - -#define SCREEN_W 1280 -#define SCREEN_H 720 - -const int atlasSize[2] = {2, 2}; - -int main() { - InitWindow(SCREEN_W, SCREEN_H, "tiled"); - - Shader shader = LoadShader(0, "tiled.glsl"); - - Texture2D tilemap = loadTileMap("map.tiles"); - - Texture atlas = LoadTexture("tiled.png"); - RenderTexture2D target = LoadRenderTexture(SCREEN_W, SCREEN_H); - - float resolution[2] = {SCREEN_W, SCREEN_H}; - float offset[2] = {0, 0}; - float zoom = 16.0f; - int mapSize[2] = {tilemap.width, tilemap.height}; - - - int resolutionLoc = GetShaderLocation(shader, "resolution"); - int locationLoc = GetShaderLocation(shader, "offset"); - int zoomLoc = GetShaderLocation(shader, "zoom"); - - int atlasSizeLoc = GetShaderLocation(shader, "atlasSize"); - int mapSizeLoc = GetShaderLocation(shader, "mapSize"); - - int textureLoc = GetShaderLocation(shader, "texture1"); - int tilemapLoc = GetShaderLocation(shader, "texture2"); - - while (!WindowShouldClose()) { - if (IsKeyDown(KEY_UP)) offset[1] += zoom * 0.01f; - if (IsKeyDown(KEY_DOWN)) offset[1] -= zoom * 0.01f; - if (IsKeyDown(KEY_RIGHT)) offset[0] -= zoom * 0.01f; - if (IsKeyDown(KEY_LEFT)) offset[0] += zoom * 0.01f; - - if (IsKeyDown(KEY_W)) zoom -= zoom * 0.01f; - if (IsKeyDown(KEY_S)) zoom += zoom * 0.01f; - - SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2); - SetShaderValue(shader, locationLoc, &offset, SHADER_UNIFORM_VEC2); - SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT); - - SetShaderValue(shader, atlasSizeLoc, &atlasSize, SHADER_UNIFORM_IVEC2); - SetShaderValue(shader, mapSizeLoc, &tilemap.width, SHADER_UNIFORM_IVEC2); - - BeginDrawing(); - - ClearBackground(LIGHTGRAY); - - BeginTextureMode(target); - DrawRectangle(0, 0, SCREEN_W, SCREEN_H, BLACK); - EndTextureMode(); - - BeginShaderMode(shader); - SetShaderValueTexture(shader, textureLoc, atlas); - SetShaderValueTexture(shader, tilemapLoc, tilemap); - - // draw the base image to texture0 - DrawTexture(target.texture, 0, 0, WHITE); - EndShaderMode(); - - DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY); - - EndDrawing(); - } - - UnloadShader(shader); - UnloadRenderTexture(target); - UnloadTexture(atlas); - - CloseWindow(); - - return 0; -} diff --git a/tiled.png b/tiled.png deleted file mode 100644 index 455750a..0000000 Binary files a/tiled.png and /dev/null differ diff --git a/tiledfile.c b/tiledfile.c deleted file mode 100644 index 8cb8dfa..0000000 --- a/tiledfile.c +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include -#include - -const int i = 1; -#define is_bigendian() ( (*(char*)&i) == 0 ) - -Texture2D processTilemapTexture(int * tilelayout, int width, int height) { - Color *pixels = (Color*) malloc(width * height * sizeof(Color)); - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int tile = tilelayout[y*width + x]; - pixels[y*width + x] = (Color){ - (int) tile, 0, 0, 0 - }; - } - } - - Image checkedIm = { - .data = pixels, - .width = width, - .height = height, - .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, - .mipmaps = 1 - }; - - Texture2D checked = LoadTextureFromImage(checkedIm); - UnloadImage(checkedIm); - - return checked; -} - -// read a big endian bytes from file -int readb(char *out, size_t noBytes, FILE *file) { - int s = fread(out, (size_t)1, (size_t) noBytes, file); - - if (!is_bigendian()) { - 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 s; -} - - -Texture2D loadTileMap(char *filename) { - int width, height, tilebytes; - int *tilelayout; - - FILE *file; - - file = fopen(filename, "rb"); - // skip header - fseek(file, 10, SEEK_CUR); - // 4 bytes for int width - readb((char *)&width, 4, file); - // 4 bytes for int height - readb((char *)&height, 4, file); - // 1 byte saying how big each tile is - readb((char *)&tilebytes, 4, file); - printf("tilebytes: %d\n", tilebytes); - - tilelayout = malloc(width*height*tilebytes); - int tile; - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - readb((char *)&tile, tilebytes, file); - tilelayout[y*width + x] = tile; - } - } - return processTilemapTexture(tilelayout, width, height); -} diff --git a/tiledfile.h b/tiledfile.h deleted file mode 100644 index cf2980b..0000000 --- a/tiledfile.h +++ /dev/null @@ -1,3 +0,0 @@ -#include - -Texture2D loadTileMap(char *filename); diff --git a/tools/createmap.py b/tools/createmap.py new file mode 100755 index 0000000..59fb315 --- /dev/null +++ b/tools/createmap.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +import sys + +outfile = "map.tiles" + +WIDTH = 500 +HEIGHT = 100 +# number of bytes each tile needs to represent +TILEBYTES = 25 + +HEADER = "TILEFILEv1" + +with open(outfile, "wb") as file: + file.write(bytes(HEADER, "ascii")) + file.write(WIDTH.to_bytes(4, 'big')); + file.write(HEIGHT.to_bytes(4, 'big')); + file.write(TILEBYTES.to_bytes(4, 'big')); + + for y in range(HEIGHT): + for x in range(WIDTH): + index = (x + y) % 5 + file.write(index.to_bytes(TILEBYTES, 'big')); + -- cgit v1.2.1