feat: textures image loading example (#167)

Co-authored-by: Mikolaj Witkowski <notai@mikolajs-mbp.home>
This commit is contained in:
Miko 2024-11-08 15:10:19 +01:00 committed by GitHub
parent 6c0774619d
commit f5abffe4f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 73 additions and 0 deletions

View File

@ -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",

View File

@ -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();
//--------------------------------------------------------------------------------------
}
}