Created
February 18, 2025 01:38
-
-
Save fearofcode/c1fb5a1077eb220153eed1255b8ff23d to your computer and use it in GitHub Desktop.
conditional high DPI window + proper text on retina display with Raylib and zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rl = @cImport(@cInclude("raylib.h")); | |
const std = @import("std"); | |
fn isHighDpi() bool { | |
rl.SetConfigFlags(rl.FLAG_WINDOW_HIDDEN); | |
rl.InitWindow(100, 100, ""); | |
const high_dpi = rl.GetWindowScaleDPI().x > 1.0; | |
rl.CloseWindow(); | |
return high_dpi; | |
} | |
pub fn main() anyerror!void { | |
const screenWidth = 640; | |
const screenHeight = 480; | |
var flags: u32 = rl.FLAG_VSYNC_HINT | rl.FLAG_WINDOW_RESIZABLE | rl.FLAG_MSAA_4X_HINT; | |
const high_dpi = isHighDpi(); | |
if (high_dpi) { | |
flags |= rl.FLAG_WINDOW_HIGHDPI; | |
} | |
rl.ClearWindowState(rl.FLAG_WINDOW_HIDDEN); | |
rl.SetConfigFlags(flags); | |
rl.InitWindow(screenWidth, screenHeight, "Conditional high DPI text test"); | |
defer rl.CloseWindow(); | |
rl.SetTargetFPS(60); | |
const base_font_size: f32 = 72.0; | |
const multiplier: f32 = if (high_dpi) 2.0 else 1.0; | |
const scaled_font_size = base_font_size * multiplier; | |
const custom_font = rl.LoadFontEx("DMSerifText-Regular.ttf", @intFromFloat(scaled_font_size), 0, 0); | |
while (!rl.WindowShouldClose()) { | |
rl.BeginDrawing(); | |
defer rl.EndDrawing(); | |
rl.ClearBackground(rl.RAYWHITE); | |
rl.DrawTextEx(custom_font, "hello, world", rl.Vector2{ .x = 10.0, .y = 40.0 }, base_font_size, // Not scaled | |
0, rl.BLACK); | |
rl.DrawFPS(10, 10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment