Added new template (upscaled pixel art)

This commit is contained in:
Casey 2024-07-22 16:37:25 +03:00
parent 9b0a4513a7
commit bcfd9909a7
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
6 changed files with 87 additions and 1 deletions

1
_templates/raylib/pixelart/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__PROGNAME

View File

@ -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

View File

@ -0,0 +1,45 @@
// x-run: make run
#include <math.h>
#include <raylib.h>
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();
}
}

View File

@ -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"

View File

@ -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);
}

2
new.py
View File

@ -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