summaryrefslogtreecommitdiff
path: root/tiled.c
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-07-10 00:22:35 +0100
committerdavidovski <david@davidovski.xyz>2023-07-10 00:22:35 +0100
commitab6501461a12894795661914096704451c343344 (patch)
tree0c4b785ef38d621b64cbc83132ebe5a707682c66 /tiled.c
parent80489b02d737848395506b218b27b86dc116d8b4 (diff)
load tilemap from file
Diffstat (limited to 'tiled.c')
-rw-r--r--tiled.c52
1 files changed, 11 insertions, 41 deletions
diff --git a/tiled.c b/tiled.c
index 220043d..715a91d 100644
--- a/tiled.c
+++ b/tiled.c
@@ -1,66 +1,35 @@
#include <raylib.h>
-#include <stdlib.h>
#include <stdio.h>
-#include <wctype.h>
+
+#include "tiledfile.h"
#define SCREEN_W 1280
#define SCREEN_H 720
-#define MAP_W 16
-#define MAP_H 4
-
-const int tilemap[MAP_H * MAP_W] = {
- 0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0,
- 0, 3, 3, 0, 0, 3, 1, 1, 1, 3, 0, 0, 0, 0, 0, 0,
- 3, 1, 1, 3, 3, 1, 2, 2, 2, 1, 3, 3, 3, 0, 0, 3,
- 1, 2, 2, 1, 1, 2, 4, 4, 4, 2, 1, 1, 1, 3, 3, 1
-};
-
const int atlasSize[2] = {2, 2};
-Texture2D processTilemapTexture(const int * tilemap, 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 = tilemap[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;
-}
-
int main() {
InitWindow(SCREEN_W, SCREEN_H, "tiled");
Shader shader = LoadShader(0, "tiled.glsl");
- Texture2D processedTilemap = processTilemapTexture(tilemap, MAP_W, MAP_H);
+ 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");
@@ -77,7 +46,9 @@ int main() {
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();
@@ -89,12 +60,11 @@ int main() {
BeginShaderMode(shader);
SetShaderValueTexture(shader, textureLoc, atlas);
- SetShaderValueTexture(shader, tilemapLoc, processedTilemap);
+ SetShaderValueTexture(shader, tilemapLoc, tilemap);
// draw the base image to texture0
DrawTexture(target.texture, 0, 0, WHITE);
EndShaderMode();
- DrawTexture(processedTilemap, 0, 0, WHITE);
DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY);