diff --git a/build.zig b/build.zig index 17ddb08..5405904 100644 --- a/build.zig +++ b/build.zig @@ -342,6 +342,11 @@ pub fn build(b: *std.Build) !void { .path = "examples/text/text_format_text.zig", .desc = "Renders variables as text", }, + .{ + .name = "text_raylib_fonts", + .path = "examples/text/text_raylib_fonts.zig", + .desc = "Show fonts included with raylib", + }, .{ .name = "textures_image_loading", .path = "examples/textures/textures_image_loading.zig", diff --git a/examples/text/text_raylib_fonts.zig b/examples/text/text_raylib_fonts.zig new file mode 100644 index 0000000..408eb0e --- /dev/null +++ b/examples/text/text_raylib_fonts.zig @@ -0,0 +1,89 @@ +const rl = @import("raylib"); +const Color = rl.Color; + +const MAX_FONTS = 8; + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +pub fn main() !void { + // Initialization + //-------------------------------------------------------------------------------------- + const screenWidth = 800; + const screenHeight = 450; + + rl.initWindow(screenWidth, screenHeight, "raylib [text] example - raylib fonts"); + defer rl.closeWindow(); // Close window and OpenGL context + + // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) + var fonts: [MAX_FONTS]rl.Font = undefined; + + fonts[0] = try rl.loadFont("resources/text/fonts/alagard.png"); + fonts[1] = try rl.loadFont("resources/text/fonts/pixelplay.png"); + fonts[2] = try rl.loadFont("resources/text/fonts/mecha.png"); + fonts[3] = try rl.loadFont("resources/text/fonts/setback.png"); + fonts[4] = try rl.loadFont("resources/text/fonts/romulus.png"); + fonts[5] = try rl.loadFont("resources/text/fonts/pixantiqua.png"); + fonts[6] = try rl.loadFont("resources/text/fonts/alpha_beta.png"); + fonts[7] = try rl.loadFont("resources/text/fonts/jupiter_crash.png"); + + // Fonts unloading + defer for (fonts) |font| { + rl.unloadFont(font); + }; + + const messages = [MAX_FONTS][*:0]const u8{ + "ALAGARD FONT designed by Hewett Tsoi", + "PIXELPLAY FONT designed by Aleksander Shevchuk", + "MECHA FONT designed by Captain Falcon", + "SETBACK FONT designed by Brian Kent (AEnigma)", + "ROMULUS FONT designed by Hewett Tsoi", + "PIXANTIQUA FONT designed by Gerhard Grossmann", + "ALPHA_BETA FONT designed by Brian Kent (AEnigma)", + "JUPITER_CRASH FONT designed by Brian Kent (AEnigma)", + }; + + const spacings = [_]i32{ 2, 4, 8, 4, 3, 4, 4, 1 }; + + var positions: [MAX_FONTS]rl.Vector2 = undefined; + for (0..MAX_FONTS) |i| { + const font_base_size = @as(f32, @floatFromInt(fonts[i].baseSize)); + positions[i].x = screenWidth / 2.0 - rl.measureTextEx(fonts[i], messages[i], font_base_size * 2.0, @floatFromInt(spacings[i])).x / 2.0; + positions[i].y = 60.0 + font_base_size + 45.0 * @as(f32, @floatFromInt(i)); + } + + // Small Y position corrections + positions[3].y += 8; + positions[4].y += 2; + positions[7].y -= 8; + + const colors = [MAX_FONTS]Color{ Color.maroon, Color.orange, Color.dark_green, Color.dark_blue, Color.dark_purple, Color.lime, Color.gold, Color.red }; + + rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!rl.windowShouldClose()) { // Detect window close button or ESC key + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + rl.beginDrawing(); + + rl.clearBackground(Color.white); + + rl.drawText("free fonts included with raylib", 250, 20, 20, Color.dark_gray); + rl.drawLine(220, 50, 590, 50, Color.dark_gray); + + for (0..MAX_FONTS) |i| { + const font_base_size = @as(f32, @floatFromInt(fonts[i].baseSize)); + rl.drawTextEx(fonts[i], messages[i], positions[i], font_base_size * 2.0, @floatFromInt(spacings[i]), colors[i]); + } + + rl.endDrawing(); + //---------------------------------------------------------------------------------- + } +} diff --git a/resources/text/fonts/alagard.png b/resources/text/fonts/alagard.png new file mode 100644 index 0000000..e8d15ee Binary files /dev/null and b/resources/text/fonts/alagard.png differ diff --git a/resources/text/fonts/alpha_beta.png b/resources/text/fonts/alpha_beta.png new file mode 100644 index 0000000..6b880bd Binary files /dev/null and b/resources/text/fonts/alpha_beta.png differ diff --git a/resources/text/fonts/jupiter_crash.png b/resources/text/fonts/jupiter_crash.png new file mode 100644 index 0000000..064df10 Binary files /dev/null and b/resources/text/fonts/jupiter_crash.png differ diff --git a/resources/text/fonts/mecha.png b/resources/text/fonts/mecha.png new file mode 100644 index 0000000..88c2df7 Binary files /dev/null and b/resources/text/fonts/mecha.png differ diff --git a/resources/text/fonts/pixantiqua.png b/resources/text/fonts/pixantiqua.png new file mode 100644 index 0000000..d9d3dfa Binary files /dev/null and b/resources/text/fonts/pixantiqua.png differ diff --git a/resources/text/fonts/pixelplay.png b/resources/text/fonts/pixelplay.png new file mode 100644 index 0000000..5d5d563 Binary files /dev/null and b/resources/text/fonts/pixelplay.png differ diff --git a/resources/text/fonts/romulus.png b/resources/text/fonts/romulus.png new file mode 100644 index 0000000..37b8f63 Binary files /dev/null and b/resources/text/fonts/romulus.png differ diff --git a/resources/text/fonts/setback.png b/resources/text/fonts/setback.png new file mode 100644 index 0000000..7331c95 Binary files /dev/null and b/resources/text/fonts/setback.png differ