u/mmknightx

How to fix custom font blurriness when drawing inside texture mode?
▲ 5 r/raylib

How to fix custom font blurriness when drawing inside texture mode?

I am using font from https://poppyworks.itch.io/silver . The size should be at size 19. I tried to use https://www.raylib.com/examples/core/loader.html?name=core_window_letterbox letter box to draw my game at 640x480 resolution and then scale it up.

https://preview.redd.it/vgsbi041ug1h1.png?width=708&format=png&auto=webp&s=c7234575061291d383050b3840f848a8d725b424

I also tried using 19 pixel font in the texture drawing but it is way more blurry. I am not sure what to do. Also, I use Raylib vendor library in Odin.

package main


import "core:fmt"
import "core:math"
import "core:strings"
import rl "vendor:raylib"


font: rl.Font
big_font: rl.Font
test_sprite: rl.Texture2D


main :: proc() {
    rl.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT, .WINDOW_HIGHDPI, .MSAA_4X_HINT})
    rl.InitWindow(1280, 720, "Greed Ninja")
    rl.SetTargetFPS(60)


    target := rl.LoadRenderTexture(GAME_WIDTH, GAME_HEIGHT)
    // Load Silver fontwith
    font = rl.LoadFontEx("assets/Silver.ttf", 19, nil, 0)
    big_font = rl.LoadFontEx("assets/Silver.ttf", 19 * 2, nil, 0)
    very_big_font := rl.LoadFontEx("assets/Silver.ttf", 19 * 4, nil, 0)
    test_sprite = rl.LoadTexture("assets/victor.png")



    // Pixel art scale
    rl.SetTextureFilter(target.texture, .POINT)
    rl.SetTextureFilter(font.texture, .POINT)
    rl.SetTextureFilter(big_font.texture, .POINT)
    rl.SetTextureFilter(test_sprite, .POINT)
    rl.SetTextureFilter(very_big_font.texture, .POINT)


    for (!rl.WindowShouldClose()) {


        if (rl.IsKeyPressed(.F11)) {
            rl.ToggleFullscreen()
        }


        scale := f32(min(rl.GetScreenWidth() / GAME_WIDTH, rl.GetScreenHeight() / GAME_HEIGHT))


        rl.BeginTextureMode(target)
        rl.ClearBackground(rl.RAYWHITE)
        rl.DrawRectangle(0, 0, GAME_WIDTH / 2, GAME_HEIGHT / 2, rl.BLUE)
        // rl.DrawText("Somethign is wrong", 16, 16, 24, rl.YELLOW)
        rl.DrawTextureEx(test_sprite, {32, 16}, 0, 1, rl.WHITE)
        rl.DrawTextureEx(test_sprite, {32 * 2, 16}, 0, 2, rl.WHITE)
        rl.DrawTextEx(big_font, "Something is wrong", {20, 20}, 19*2, 1, rl.BLACK)
        rl.EndTextureMode()


        rl.BeginDrawing()
        rl.ClearBackground(rl.BLACK)
        rl.BeginBlendMode(.ALPHA_PREMULTIPLY)
        rl.DrawTexturePro(
            target.texture,
            {0, 0, f32(target.texture.width), -f32(target.texture.height)},
            {
                math.floor((f32(rl.GetScreenWidth()) - GAME_WIDTH * scale) * 0.5),
                math.floor((f32(rl.GetScreenHeight()) - GAME_HEIGHT * scale) * 0.5),
                GAME_WIDTH * scale,
                GAME_HEIGHT * scale,
            },
            {0, 0},
            0,
            rl.WHITE,
        )
        rl.EndBlendMode()
        rl.DrawTextEx(
            big_font,
            strings.clone_to_cstring(
                fmt.tprintf(
                    "Scale at %.2f, Target width %.2f Target Height %.2f",
                    scale,
                    GAME_WIDTH * scale,
                    GAME_HEIGHT * scale,
                ),
                allocator = context.temp_allocator,
            ),
            {16, 16},
            19 * 2,
            1,
            rl.WHITE,
        )
        rl.DrawTextEx(
            big_font,
            strings.clone_to_cstring(
                fmt.tprintf(
                    "Top left corner x = %.2f, y = %.2f ",
                    math.floor((f32(rl.GetScreenWidth()) - GAME_WIDTH * scale) * 0.5),
                    math.floor((f32(rl.GetScreenHeight()) - GAME_HEIGHT * scale) * 0.5),
                ),
                allocator = context.temp_allocator,
            ),
            {16, 48},
            19 * 2,
            1,
            rl.WHITE,
        )
        rl.EndDrawing()


        free_all(context.temp_allocator)
    }


    rl.UnloadRenderTexture(target)
    rl.UnloadFont(font)
    rl.UnloadFont(big_font)
    rl.CloseWindow()
}
reddit.com
u/mmknightx — 6 days ago