From f5abffe4f7bb1128439e2f287566b31c3cdac3fc Mon Sep 17 00:00:00 2001 From: Miko <54701389+vtech6@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:10:19 +0100 Subject: [PATCH] feat: textures image loading example (#167) Co-authored-by: Mikolaj Witkowski --- build.zig | 6 ++ examples/textures/textures_image_loading.zig | 67 ++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 examples/textures/textures_image_loading.zig diff --git a/build.zig b/build.zig index a7b4b60..9dd54f7 100644 --- a/build.zig +++ b/build.zig @@ -276,6 +276,12 @@ pub fn build(b: *std.Build) !void { .path = "examples/text/text_format_text.zig", .desc = "Renders variables as text", }, + .{ + .name = "textures_image_loading", + .path = "examples/textures/textures_image_loading.zig", + .desc = "Image loading and texture creation", + }, + // .{ // .name = "models_loading", // .path = "examples/models/models_loading.zig", diff --git a/examples/textures/textures_image_loading.zig b/examples/textures/textures_image_loading.zig new file mode 100644 index 0000000..e1a3d73 --- /dev/null +++ b/examples/textures/textures_image_loading.zig @@ -0,0 +1,67 @@ +// raylib-zig (c) Nikolas Wipper 2023 +// +// raylib-zig [textures] example - image loading +// +// Example licensed under an unmodified zlib/libpng license, which is an +// OSI-certified, BSD-like license that allows static linking with closed +// source software + +const rl = @import("raylib"); +const Color = rl.Color; +const screenWidth = 800; +const screenHeight = 450; + +//-------------------------------------------------------------------------------------- +// Program entry point +//-------------------------------------------------------------------------------------- +pub fn main() anyerror!void { + //Initialization + //-------------------------------------------------------------------------------------- + rl.initWindow( + screenWidth, + screenHeight, + "raylib [textures] example - image loading", + ); + + // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) + + const image = rl.loadImage("logo/logo.png"); // Loaded in CPU memory (RAM) + const texture = rl.loadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM) + // Once image has been converted to texture and uploaded to VRAM, + // it can be unloaded from RAM + rl.unloadImage(image); + + // De-Initialization + //-------------------------------------------------------------------------------------- + defer rl.unloadTexture(texture); // Texture unloading + defer rl.closeWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second + while (!rl.windowShouldClose()) { + // Update + //-------------------------------------------------------------------------------------- + // TODO: Update your variables here + //-------------------------------------------------------------------------------------- + + // Draw + //-------------------------------------------------------------------------------------- + rl.beginDrawing(); + rl.clearBackground(Color.white); + rl.drawTexture( + texture, + screenWidth / 2 - @divFloor(texture.width, 2), + screenHeight / 2 - @divFloor(texture.height, 2), + Color.white, + ); + rl.drawText( + "this IS a texture loaded from an image!", + 300, + 370, + 10, + Color.gray, + ); + rl.endDrawing(); + //-------------------------------------------------------------------------------------- + } +}