From 56fbc89983e770b98309fe2fba40abe5aaf0ee58 Mon Sep 17 00:00:00 2001 From: hkc Date: Fri, 14 Oct 2022 08:49:58 +0300 Subject: [PATCH] Hot reloading and somewhat working but SLOW shader --- src/asciify.frag | 36 ++++++++++++++++++++++++++++++--- src/main.c | 52 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/asciify.frag b/src/asciify.frag index d0fea0b..cb288bf 100644 --- a/src/asciify.frag +++ b/src/asciify.frag @@ -16,12 +16,42 @@ void main() { vec2 screenSize = fontSize * charactersXY; vec2 pixelCoord = floor(fragTexCoord * screenSize); - vec2 symbol = floor(pixelCoord / fontSize) / screenSize; - vec2 inSymbol = mod(pixelCoord, fontSize) / fontSize; + vec2 symbol = floor(pixelCoord / fontSize); + vec2 inSymbol = mod(pixelCoord, fontSize) / fontSize / vec2(16., 8.); vec4 texelColor = vec4(0.0, 0.0, 0.0, 1.0); - texelColor.rgb = texture(texture0, symbol * fontSize).rgb; + //texelColor.rgb = texture(texture0, symbol * fontSize).rgb; + //texelColor.rg = texture(fontAtlas, symbol + inSymbol / vec2(16., 8.)).rg; + //texelColor.rg = symbol + inSymbol / vec2(64., 32.); + vec3 smallest_diff = vec3(99999999999.), diff = vec3(0.); + ivec2 probeSym = ivec2(0), closestSymbol = ivec2(5, -3); + for (probeSym.y = 0; probeSym.y < 8; probeSym.y++) { + for (probeSym.x = 0; probeSym.x < 16; probeSym.x++) { + vec2 charPosition = probeSym / vec2(16., 8.), inCharPosition = vec2(0.); + diff = vec3(0.); + for (inCharPosition.y = 0.; inCharPosition.y < 1.0; inCharPosition.y += 1. / fontSize.y) { + for (inCharPosition.x = 0.; inCharPosition.x < 1.0; inCharPosition.x += 1. / fontSize.x) { + vec3 charRGB = texture(fontAtlas, (inCharPosition + probeSym) / vec2(16., 8.)).rgb, + srcRGB = texture(texture0, pixelCoord / screenSize).rgb; + diff = diff + abs(srcRGB - charRGB); + } + } + if (length(diff) < length(smallest_diff)) { + smallest_diff = diff; + closestSymbol = probeSym; + } + } + } + texelColor.rg = texture(fontAtlas, inSymbol + closestSymbol / vec2(16., 8.)).rg; + +#if 0 + if (fragTexCoord.x >= 0.5) + finalColor = texture(texture0, fragTexCoord); + else + finalColor = texelColor; +#else finalColor = texelColor; +#endif } diff --git a/src/main.c b/src/main.c index 3778f79..fafcd38 100644 --- a/src/main.c +++ b/src/main.c @@ -32,9 +32,13 @@ int main(int argc, char **argv) { InitWindow(320, 240, "Loading..."); SetTargetFPS(60); + char *font_path = "unscii-16-full.ttf", + *shader_path = "./src/asciify.frag"; + if (argc >= 2) font_path = argv[1]; + int font_size = 16, chars_x = 160, chars_y = 50; - Font font = LoadFontEx("unscii-16-full.ttf", font_size, NULL, 256); + Font font = LoadFontEx(font_path, font_size, NULL, 256); Vector2 glyphSize = MeasureTextEx(font, "A", font_size, 0); Image img_atlas = GenImageColor(glyphSize.x * 16, glyphSize.y * 8, BLACK); @@ -51,6 +55,8 @@ int main(int argc, char **argv) { }, WHITE); } + ImageFlipVertical(&img_atlas); + Texture2D tex_atlas = LoadTextureFromImage(img_atlas); int wsize_x = chars_x * glyphSize.x, wsize_y = chars_y * glyphSize.y; SetWindowMinSize(glyphSize.x * 40, glyphSize.y * 12); @@ -71,18 +77,8 @@ int main(int argc, char **argv) { .position.z = 50.0, }; - Shader shd_asciify = LoadShader(NULL, "./src/asciify.frag"); - int locAtlas = GetShaderLocation(shd_asciify, "fontAtlas"); - - { - float tmp[2]; - - tmp[0] = glyphSize.x; tmp[1] = glyphSize.y; - printf("glyph size: %fx%f\n", tmp[0], tmp[1]); - SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "fontSize"), tmp, SHADER_UNIFORM_VEC2); - tmp[0] = chars_x; tmp[1] = chars_y; - SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "charactersXY"), tmp, SHADER_UNIFORM_VEC2); - } + Shader shd_asciify; + long shaderLastModified = 0, shaderModified = -1; SetCameraMode(cam, CAMERA_FREE); @@ -102,6 +98,27 @@ int main(int argc, char **argv) { SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "charactersXY"), tmp, SHADER_UNIFORM_VEC2); } + if ((frame % 10) == 0) { + shaderModified = GetFileModTime(shader_path); + } + + if (shaderModified != shaderLastModified) + { + printf("Recompiling...\n"); + shd_asciify = LoadShader(NULL, shader_path); + + + float tmp[2]; + + tmp[0] = glyphSize.x; tmp[1] = glyphSize.y; + SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "fontSize"), tmp, SHADER_UNIFORM_VEC2); + + tmp[0] = chars_x; tmp[1] = chars_y; + SetShaderValue(shd_asciify, GetShaderLocation(shd_asciify, "charactersXY"), tmp, SHADER_UNIFORM_VEC2); + shaderLastModified = shaderModified; + } + + if (IsKeyDown(KEY_LEFT_SHIFT) && IsKeyPressed(KEY_F)) { SetWindowSize(wsize_x, wsize_y); } @@ -137,8 +154,13 @@ int main(int argc, char **argv) { ClearBackground(RED); DrawRectangle(0, 0, wsize_x, wsize_y, BLACK); - if (shading) BeginShaderMode(shd_asciify); - SetShaderValueTexture(shd_asciify, locAtlas, tex_atlas); + + if (shading) { + BeginShaderMode(shd_asciify); + int locAtlas = GetShaderLocation(shd_asciify, "fontAtlas"); + SetShaderValueTexture(shd_asciify, locAtlas, tex_atlas); + } + DrawTexturePro(framebuffer.texture, (Rectangle){ 0, 0, wsize_x, -wsize_y }, (Rectangle){