Ported raylib example "text/text_format_text.c" (#141)

* Added example: text_format_text.zig

* reverted autoformatting changes
This commit is contained in:
Krieg 2024-08-31 02:41:42 -06:00 committed by GitHub
parent 58df62807f
commit 2176d37bcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 9 deletions

View File

@ -44,7 +44,7 @@ fn link(
exe: *std.Build.Step.Compile, exe: *std.Build.Step.Compile,
target: std.Build.ResolvedTarget, target: std.Build.ResolvedTarget,
optimize: std.builtin.Mode, optimize: std.builtin.Mode,
options: Options options: Options,
) void { ) void {
const lib = getRaylib(b, target, optimize, options); const lib = getRaylib(b, target, optimize, options);
@ -104,7 +104,7 @@ fn getRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.buil
.platform_drm = options.platform_drm, .platform_drm = options.platform_drm,
.shared = options.shared, .shared = options.shared,
.linux_display_backend = options.linux_display_backend, .linux_display_backend = options.linux_display_backend,
.opengl_version = options.opengl_version .opengl_version = options.opengl_version,
}); });
const lib = raylib.artifact("raylib"); const lib = raylib.artifact("raylib");
@ -118,12 +118,15 @@ fn getRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.buil
lib.step.dependOn(&gen_step.step); lib.step.dependOn(&gen_step.step);
const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n"); const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
lib.addCSourceFile(.{ .file = raygui_c_path, .flags = &[_][]const u8{ lib.addCSourceFile(.{
"-std=gnu99", .file = raygui_c_path,
"-D_GNU_SOURCE", .flags = &[_][]const u8{
"-DGL_SILENCE_DEPRECATION=199309L", "-std=gnu99",
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674 "-D_GNU_SOURCE",
}}); "-DGL_SILENCE_DEPRECATION=199309L",
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
},
});
lib.addIncludePath(raylib.path("src")); lib.addIncludePath(raylib.path("src"));
lib.addIncludePath(raygui_dep.path("src")); lib.addIncludePath(raygui_dep.path("src"));
@ -220,7 +223,7 @@ pub fn build(b: *std.Build) !void {
.name = "3d_camera_first_person", .name = "3d_camera_first_person",
.path = "examples/core/3d_camera_first_person.zig", .path = "examples/core/3d_camera_first_person.zig",
.desc = "Simple first person demo", .desc = "Simple first person demo",
}, },
.{ .{
.name = "2d_camera_mouse_zoom", .name = "2d_camera_mouse_zoom",
.path = "examples/core/2d_camera_mouse_zoom.zig", .path = "examples/core/2d_camera_mouse_zoom.zig",
@ -252,6 +255,11 @@ pub fn build(b: *std.Build) !void {
.path = "examples/textures/textures_background_scrolling.zig", .path = "examples/textures/textures_background_scrolling.zig",
.desc = "Background scrolling & parallax demo", .desc = "Background scrolling & parallax demo",
}, },
.{
.name = "text_format_text",
.path = "examples/text/text_format_text.zig",
.desc = "Renders variables as text",
},
// .{ // .{
// .name = "models_loading", // .name = "models_loading",
// .path = "examples/models/models_loading.zig", // .path = "examples/models/models_loading.zig",

View File

@ -0,0 +1,49 @@
// A raylib port of https://github.com/raysan5/raylib/blob/master/examples/text/text_format_text.c
const rl = @import("raylib");
const std = @import("std");
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
pub fn main() anyerror!void {
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
rl.initWindow(screenWidth, screenHeight, "raylib [text] example - text formatting");
defer rl.closeWindow(); // Close window and OpenGL context
// Set as 'const' for demonstration purposes, but would need to be 'var' once you actually update them to real values.
const score: i32 = 100020;
const hiscore: i32 = 200450;
const lives: i32 = 5;
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();
defer rl.endDrawing();
rl.clearBackground(rl.Color.white);
rl.drawText(rl.textFormat("Score: %08i", .{score}), 200, 80, 20, rl.Color.red);
rl.drawText(rl.textFormat("HiScore: %08i", .{hiscore}), 200, 120, 20, rl.Color.green);
rl.drawText(rl.textFormat("Lives: %02i", .{lives}), 200, 160, 40, rl.Color.blue);
rl.drawText(rl.textFormat("Elapsed Time: %02.02f ms", .{rl.getFrameTime() * 1000}), 200, 220, 20, rl.Color.black);
//----------------------------------------------------------------------------------
}
}