mirror of
https://github.com/Not-Nik/raylib-zig.git
synced 2025-09-09 12:07:27 +00:00
feat: textures image loading example (#167)
Co-authored-by: Mikolaj Witkowski <notai@mikolajs-mbp.home>
This commit is contained in:
parent
6c0774619d
commit
f5abffe4f7
@ -276,6 +276,12 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.path = "examples/text/text_format_text.zig",
|
.path = "examples/text/text_format_text.zig",
|
||||||
.desc = "Renders variables as text",
|
.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",
|
// .name = "models_loading",
|
||||||
// .path = "examples/models/models_loading.zig",
|
// .path = "examples/models/models_loading.zig",
|
||||||
|
67
examples/textures/textures_image_loading.zig
Normal file
67
examples/textures/textures_image_loading.zig
Normal 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();
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user