From bcfd9909a794a8351d67c3bf1a24b9fbeed1950e Mon Sep 17 00:00:00 2001 From: hkc Date: Mon, 22 Jul 2024 16:37:25 +0300 Subject: [PATCH] Added new template (upscaled pixel art) --- _templates/raylib/pixelart/.gitignore | 1 + _templates/raylib/pixelart/Makefile | 11 ++++++ _templates/raylib/pixelart/main.c | 45 ++++++++++++++++++++++++ _templates/raylib/pixelart/template.toml | 14 ++++++++ _templates/raylib/pixelart/upscale.frag | 15 ++++++++ new.py | 2 +- 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 _templates/raylib/pixelart/.gitignore create mode 100644 _templates/raylib/pixelart/Makefile create mode 100644 _templates/raylib/pixelart/main.c create mode 100644 _templates/raylib/pixelart/template.toml create mode 100644 _templates/raylib/pixelart/upscale.frag diff --git a/_templates/raylib/pixelart/.gitignore b/_templates/raylib/pixelart/.gitignore new file mode 100644 index 0000000..4996ad8 --- /dev/null +++ b/_templates/raylib/pixelart/.gitignore @@ -0,0 +1 @@ +__PROGNAME diff --git a/_templates/raylib/pixelart/Makefile b/_templates/raylib/pixelart/Makefile new file mode 100644 index 0000000..4d78ded --- /dev/null +++ b/_templates/raylib/pixelart/Makefile @@ -0,0 +1,11 @@ +CFLAGS += -Wall -Wextra `exec pkg-config --cflags raylib` +LDFLAGS := -lm `pkg-config --libs raylib` + +__PROGNAME: main.c + $(CC) $(CFLAGS) main.c $(LDFLAGS) -o __PROGNAME + +clean: + $(RM) __PROGNAME + +run: __PROGNAME + ./__PROGNAME diff --git a/_templates/raylib/pixelart/main.c b/_templates/raylib/pixelart/main.c new file mode 100644 index 0000000..4d88fdd --- /dev/null +++ b/_templates/raylib/pixelart/main.c @@ -0,0 +1,45 @@ +// x-run: make run +#include +#include + +int main(int argc, char **argv) { + SetTraceLogLevel(LOG_ERROR); + SetConfigFlags(FLAG_WINDOW_RESIZABLE); + InitWindow(__WINDOW_WIDTH * 2, __WINDOW_HEIGHT * 2, "__PROJECT"); + const int _window_size[2] = { __WINDOW_WIDTH, __WINDOW_HEIGHT }; + + Image canvas = GenImageColor(__WINDOW_WIDTH, __WINDOW_HEIGHT, BLANK); + Texture2D t_canvas = LoadTextureFromImage(canvas); + SetTextureFilter(t_canvas, TEXTURE_FILTER_BILINEAR); + Shader shd_upscale = LoadShader(0, "upscale.frag"); + int shdloc_texsize = GetShaderLocation(shd_upscale, "texture0_size"); + int shdloc_scale = GetShaderLocation(shd_upscale, "px_per_tx"); + + for (int frame = 0; !WindowShouldClose(); frame++) { + BeginDrawing(); + int window_width = GetScreenWidth(), window_height = GetScreenHeight(); + float scale = fminf(window_width / (float)__WINDOW_WIDTH, window_height / (float)__WINDOW_HEIGHT); + { // BEGIN frame + ImageClearBackground(&canvas, GetColor(0x1A1A1AFF)); + ImageDrawText(&canvas, TextFormat("scale: %7.2fx", scale), 20, 20, 20, WHITE); + } // END frame + { // BEGIN update + UpdateTexture(t_canvas, canvas.data); + BeginShaderMode(shd_upscale); + SetShaderValue(shd_upscale, shdloc_texsize, _window_size, SHADER_UNIFORM_IVEC2); + SetShaderValue(shd_upscale, shdloc_scale, &scale, SHADER_UNIFORM_FLOAT); + int texture_width = scale * __WINDOW_WIDTH, texture_height = scale * __WINDOW_HEIGHT; + + ClearBackground(BLACK); + DrawTexturePro(t_canvas, (Rectangle) { + 0, 0, __WINDOW_WIDTH, __WINDOW_HEIGHT + }, (Rectangle) { + floorf((window_width - texture_width) / 2.0), + floorf((window_height - texture_height) / 2.0), + texture_width, texture_height + }, (Vector2) { 0, 0 }, 0.0, WHITE); + EndShaderMode(); + } // END update + EndDrawing(); + } +} diff --git a/_templates/raylib/pixelart/template.toml b/_templates/raylib/pixelart/template.toml new file mode 100644 index 0000000..9cf869a --- /dev/null +++ b/_templates/raylib/pixelart/template.toml @@ -0,0 +1,14 @@ +name = "Raylib project with fancy pixel-art scaling and software-rendered canvas" +templates = [ + "Makefile", + "main.c", + ".gitignore" +] + +[params.WINDOW_WIDTH] +prompt = "Initial window width" +default = "480" + +[params.WINDOW_HEIGHT] +prompt = "Initial window height" +default = "320" diff --git a/_templates/raylib/pixelart/upscale.frag b/_templates/raylib/pixelart/upscale.frag new file mode 100644 index 0000000..08365a6 --- /dev/null +++ b/_templates/raylib/pixelart/upscale.frag @@ -0,0 +1,15 @@ +#version 330 +uniform sampler2D texture0; +uniform ivec2 texture0_size; +uniform float px_per_tx; + +in vec2 fragTexCoord; +in vec4 fragColor; +out vec4 finalColor; + +void main() { vec2 uv = vec2(fragTexCoord.x, fragTexCoord.y); + vec2 tx = uv * vec2(texture0_size); + vec2 off = clamp(fract(tx) * px_per_tx, 0, 0.5) - clamp((1 - fract(tx)) * px_per_tx, 0, 0.5); + vec2 pos = (floor(tx) + 0.5 + off) / vec2(texture0_size); + finalColor = texture(texture0, pos); +} diff --git a/new.py b/new.py index 419ce55..cdc66ef 100644 --- a/new.py +++ b/new.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from typing import Any, Literal, Optional +from typing import Any, Optional from tomllib import load from pathlib import Path from dataclasses import dataclass, field