summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-07-11 00:11:06 +0100
committerdavidovski <david@davidovski.xyz>2023-07-11 00:11:06 +0100
commitda40b1083161b1a53e2764ffac3a3a1f8568e24c (patch)
treeb2ccb9e23031d376a5741f80f85eb286ef7376b8
parenta65880a11878a9b8e08174b7c897894b2a81a226 (diff)
use struct to store multiple uniforms
-rw-r--r--src/tiled.c104
-rw-r--r--src/tiled.h23
-rw-r--r--tiled.glsl14
3 files changed, 90 insertions, 51 deletions
diff --git a/src/tiled.c b/src/tiled.c
index 95d3950..1dd1d43 100644
--- a/src/tiled.c
+++ b/src/tiled.c
@@ -2,82 +2,100 @@
#include <stdio.h>
#include "tiledfile.h"
+#include "tiled.h"
#define SCREEN_W 1280
#define SCREEN_H 720
-int main() {
- InitWindow(SCREEN_W, SCREEN_H, "tiled");
+void updateCamera(Vector2 *offset, float *zoom) {
+ if (IsKeyDown(KEY_UP)) offset->y += 16.0f / *zoom;
+ if (IsKeyDown(KEY_DOWN)) offset->y -= 16.0f/ *zoom;
+ if (IsKeyDown(KEY_RIGHT)) offset->x -= 16.0f / *zoom;
+ if (IsKeyDown(KEY_LEFT)) offset->x += 16.0f / *zoom;
+
+ if (IsKeyDown(KEY_W)) *zoom += *zoom * 0.01f;
+ if (IsKeyDown(KEY_S)) *zoom -= *zoom * 0.01f;
+}
+
+void drawOverlay() {
+ DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY);
+}
+Shader initTiledShader(TiledUniforms *uniforms) {
Shader shader = LoadShader(0, "tiled.glsl");
- int atlasSize[2] = {0, 0};
- Texture2D tilemap, atlas;
+ uniforms->offsetLoc = GetShaderLocation(shader, "offset");
+ uniforms->zoomLoc = GetShaderLocation(shader, "zoom");
- if (loadTileMap("map.tiles", &tilemap, &atlas, atlasSize)) {
- return 1;
- }
+ uniforms->atlasSizeLoc = GetShaderLocation(shader, "atlasSize");
+ uniforms->mapSizeLoc = GetShaderLocation(shader, "mapSize");
- RenderTexture2D target = LoadRenderTexture(SCREEN_W, SCREEN_H);
+ uniforms->atlasTextureLoc = GetShaderLocation(shader, "atlasTexture");
+ uniforms->tilemapTextureLoc = GetShaderLocation(shader, "tilemapTexture");
- float resolution[2] = {SCREEN_W, SCREEN_H};
- float offset[2] = {0, 0};
- float zoom = 16.0f;
- int mapSize[2] = {tilemap.width, tilemap.height};
+ return shader;
+}
+void setTiledShaderUniforms(Shader shader, TiledUniforms uniforms) {
+ SetShaderValue(shader, uniforms.offsetLoc, &uniforms.offset, SHADER_UNIFORM_VEC2);
+ SetShaderValue(shader, uniforms.zoomLoc, &uniforms.zoom, SHADER_UNIFORM_FLOAT);
- int resolutionLoc = GetShaderLocation(shader, "resolution");
- int locationLoc = GetShaderLocation(shader, "offset");
- int zoomLoc = GetShaderLocation(shader, "zoom");
+ SetShaderValue(shader, uniforms.atlasSizeLoc, &uniforms.atlasSize, SHADER_UNIFORM_IVEC2);
+ SetShaderValue(shader, uniforms.mapSizeLoc, &uniforms.tilemapTexture.width, SHADER_UNIFORM_IVEC2);
- int atlasSizeLoc = GetShaderLocation(shader, "atlasSize");
- int mapSizeLoc = GetShaderLocation(shader, "mapSize");
- int textureLoc = GetShaderLocation(shader, "texture1");
- int tilemapLoc = GetShaderLocation(shader, "texture2");
+ SetShaderValueTexture(shader, uniforms.atlasTextureLoc, uniforms.atlasTexture);
+ SetShaderValueTexture(shader, uniforms.tilemapTextureLoc, uniforms.tilemapTexture);
+}
- 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;
+void unloadTiledShader(Shader shader, TiledUniforms uniforms) {
+ UnloadShader(shader);
+ UnloadTexture(uniforms.atlasTexture);
+ UnloadTexture(uniforms.tilemapTexture);
- if (IsKeyDown(KEY_W)) zoom -= zoom * 0.01f;
- if (IsKeyDown(KEY_S)) zoom += zoom * 0.01f;
+}
+
+int main() {
+ SetConfigFlags(FLAG_WINDOW_RESIZABLE);
+ InitWindow(SCREEN_W, SCREEN_H, "tiled");
+
+ TiledUniforms uniforms;
+ if (loadTileMap("map.tiles", &uniforms.tilemapTexture, &uniforms.atlasTexture, uniforms.atlasSize))
+ return 1;
- SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
- SetShaderValue(shader, locationLoc, &offset, SHADER_UNIFORM_VEC2);
- SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
+ uniforms.offset = (Vector2) {0, 0};
+ uniforms.zoom = 64;
+ uniforms.mapSize[0] = uniforms.tilemapTexture.width;
+ uniforms.mapSize[1] = uniforms.tilemapTexture.height;
- SetShaderValue(shader, atlasSizeLoc, &atlasSize, SHADER_UNIFORM_IVEC2);
- SetShaderValue(shader, mapSizeLoc, &tilemap.width, SHADER_UNIFORM_IVEC2);
+ RenderTexture2D target = LoadRenderTexture(1, 1);
+ Shader shader = initTiledShader(&uniforms);
+
+
+ while (!WindowShouldClose()) {
+ updateCamera(&uniforms.offset, &uniforms.zoom);
BeginDrawing();
ClearBackground(LIGHTGRAY);
- BeginTextureMode(target);
- DrawRectangle(0, 0, SCREEN_W, SCREEN_H, BLACK);
- EndTextureMode();
-
BeginShaderMode(shader);
- SetShaderValueTexture(shader, textureLoc, atlas);
- SetShaderValueTexture(shader, tilemapLoc, tilemap);
+ setTiledShaderUniforms(shader, uniforms);
- // draw the base image to texture0
- DrawTexture(target.texture, 0, 0, WHITE);
+ DrawTextureRec(target.texture,
+ (Rectangle){0, 0, GetScreenWidth(), GetScreenHeight()},
+ (Vector2){0, 0},
+ WHITE);
EndShaderMode();
- DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY);
+ drawOverlay();
EndDrawing();
}
- UnloadShader(shader);
UnloadRenderTexture(target);
- UnloadTexture(atlas);
+ unloadTiledShader(shader, uniforms);
CloseWindow();
-
return 0;
}
diff --git a/src/tiled.h b/src/tiled.h
new file mode 100644
index 0000000..5791dc0
--- /dev/null
+++ b/src/tiled.h
@@ -0,0 +1,23 @@
+#include <raylib.h>
+
+typedef struct TiledUniforms {
+ float zoom;
+ Vector2 offset;
+
+ int atlasSize[2];
+ int mapSize[2];
+
+ Texture2D atlasTexture;
+ Texture2D tilemapTexture;
+
+
+ int zoomLoc;
+ int offsetLoc;
+
+ int atlasSizeLoc;
+ int mapSizeLoc;
+
+ int atlasTextureLoc;
+ int tilemapTextureLoc;
+
+} TiledUniforms;
diff --git a/tiled.glsl b/tiled.glsl
index 8580f52..c5f762d 100644
--- a/tiled.glsl
+++ b/tiled.glsl
@@ -7,10 +7,9 @@ in vec2 fragTexCoord;
in vec4 fragColor;
uniform sampler2D texture0;
-uniform sampler2D texture1;
-uniform sampler2D texture2;
+uniform sampler2D atlasTexture;
+uniform sampler2D tilemapTexture;
-uniform vec2 resolution;
uniform vec2 offset;
uniform float zoom;
uniform ivec2 atlasSize;
@@ -44,17 +43,16 @@ vec4 tile(vec2 coords, int tileIndex) {
vec2 texCoords = coords + (tileOffset);
texCoords /= atlasSize;
- return texture(texture1, texCoords);
+ return texture(atlasTexture, texCoords);
}
}
void main() {
- ivec2 texSize = textureSize(texture1, 1);
+ ivec2 res = textureSize(texture0, 1);
vec2 uv = fragTexCoord;
- uv.x *= resolution.x / resolution.y;
- uv *= zoom;
+ uv /= zoom;
uv -= offset;
// get position in tiled world wow
@@ -64,7 +62,7 @@ void main() {
finalColor = none;
} else {
vec2 position = mod(uv, 1);
- int tileIndex = int(256*texelFetch(texture2, tilemapPos, 0).r);
+ int tileIndex = int(256*texelFetch(tilemapTexture, tilemapPos, 0).r);
finalColor = tile(position, tileIndex);
}
}