summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-07-09 22:47:25 +0100
committerdavidovski <david@davidovski.xyz>2023-07-09 22:47:25 +0100
commit80489b02d737848395506b218b27b86dc116d8b4 (patch)
treee74e9351b6fadef073b5adbda19ab94fe0df1186
parent7b6726504eca15933f7d5d14acf98cb3386acca4 (diff)
use texture to pass tilemap
-rw-r--r--tiled.c58
-rw-r--r--tiled.glsl5
-rw-r--r--tiled.png (renamed from tiles.png)bin875 -> 875 bytes
3 files changed, 42 insertions, 21 deletions
diff --git a/tiled.c b/tiled.c
index 64118bb..220043d 100644
--- a/tiled.c
+++ b/tiled.c
@@ -1,4 +1,6 @@
#include <raylib.h>
+#include <stdlib.h>
+#include <stdio.h>
#include <wctype.h>
#define SCREEN_W 1280
@@ -7,40 +9,61 @@
#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 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);
+ 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;
- Texture atlas = LoadTexture("tiles.png");
+
int resolutionLoc = GetShaderLocation(shader, "resolution");
int locationLoc = GetShaderLocation(shader, "offset");
int zoomLoc = GetShaderLocation(shader, "zoom");
int atlasSizeLoc = GetShaderLocation(shader, "atlasSize");
- int tilemapLoc[MAP_H][MAP_W] = {};
- for (int x = 0; x < MAP_W; x++) {
- for (int y = 0; y < MAP_H; y++) {
- tilemapLoc[y][x] =
- GetShaderLocation(shader, TextFormat("tilemap[%d][%d]", y, x));
- }
- }
-
int textureLoc = GetShaderLocation(shader, "texture1");
+ int tilemapLoc = GetShaderLocation(shader, "texture2");
while (!WindowShouldClose()) {
if (IsKeyDown(KEY_UP)) offset[1] += zoom * 0.01f;
@@ -55,11 +78,6 @@ int main() {
SetShaderValue(shader, locationLoc, &offset, SHADER_UNIFORM_VEC2);
SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, atlasSizeLoc, &atlasSize, SHADER_UNIFORM_IVEC2);
- for (int x = 0; x < MAP_W; x++) {
- for (int y = 0; y < MAP_H; y++) {
- SetShaderValue(shader, tilemapLoc[y][x], &tilemap[y][x], SHADER_UNIFORM_INT);
- }
- }
BeginDrawing();
@@ -71,10 +89,12 @@ int main() {
BeginShaderMode(shader);
SetShaderValueTexture(shader, textureLoc, atlas);
+ SetShaderValueTexture(shader, tilemapLoc, processedTilemap);
// 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);
diff --git a/tiled.glsl b/tiled.glsl
index 9d00690..3ec1c66 100644
--- a/tiled.glsl
+++ b/tiled.glsl
@@ -10,12 +10,12 @@ in vec4 fragColor;
uniform sampler2D texture0;
uniform sampler2D texture1;
+uniform sampler2D texture2;
uniform vec2 resolution;
uniform vec2 offset;
uniform float zoom;
uniform ivec2 atlasSize;
-uniform int tilemap[MAP_H][MAP_W];
out vec4 finalColor;
@@ -65,6 +65,7 @@ void main() {
finalColor = none;
} else {
vec2 position = mod(uv, 1);
- finalColor = tile(position, tilemap[tilemapPos.y][tilemapPos.x]);
+ int tileIndex = int(256*texelFetch(texture2, tilemapPos, 0).r);
+ finalColor = tile(position, tileIndex);
}
}
diff --git a/tiles.png b/tiled.png
index 455750a..455750a 100644
--- a/tiles.png
+++ b/tiled.png
Binary files differ