embedded files loading
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [others] example - embedded files loading
//!
//! Zig-API translation of examples/others/embedded_files_loading.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2020-2025 Kristian Holmgren (@defutura) and Ramon Santamaria (@raysan5)
//!
//! The C example embeds the data as arrays in headers generated by
//! ExportWaveAsCode()/ExportImageAsCode(); Zig embeds the same bytes with
//! @embedFile (resources/*.raw hold exactly the bytes of those header arrays).
const rl = @import("raylib");
// Wave data information (audio_data.h)
const audio_frame_count = 24367;
const audio_sample_rate = 44100;
const audio_sample_size = 32;
const audio_channels = 1;
var audio_data align(@alignOf(f32)) = @embedFile("resources/audio_data.raw").*;
// Image data information (image_data.h)
const image_width = 256;
const image_height = 256;
const image_format: rl.PixelFormat = .uncompressed_r8g8b8;
var image_data = @embedFile("resources/image_data.raw").*;
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [others] example - embedded files loading");
defer rl.closeWindow(); // Close window and OpenGL context
rl.initAudioDevice(); // Initialize audio device
defer rl.closeAudioDevice(); // Close audio device
// Loaded in CPU memory (RAM) from the embedded data
// Same as: const wave = try rl.loadWave("sound.wav");
const wave: rl.Wave = .{
.data = &audio_data,
.frameCount = audio_frame_count,
.sampleRate = audio_sample_rate,
.sampleSize = audio_sample_size,
.channels = audio_channels,
};
// Wave converted to Sound to be played
const sound = try rl.loadSoundFromWave(wave);
defer rl.unloadSound(sound); // Unload sound from VRAM
// With a Wave loaded from file, after Sound is loaded, we can unload Wave
// but in our case, Wave is embedded in executable, in program .data segment
// we can not (and should not) try to free that private memory region
// Loaded in CPU memory (RAM) from the embedded data
// Same as: const image = try rl.loadImage("raylib_logo.png");
const image: rl.Image = .{
.data = &image_data,
.width = image_width,
.height = image_height,
.format = image_format,
.mipmaps = 1,
};
// Image converted to Texture (VRAM) to be drawn
const texture = try rl.loadTextureFromImage(image);
defer rl.unloadTexture(texture); // Unload texture from VRAM
// With an Image loaded from file, after Texture is loaded, we can unload Image
// but in our case, Image is embedded in executable, in program .data segment
// we can not (and should not) try to free that private memory region
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
if (rl.isKeyPressed(.space)) rl.playSound(sound); // Play sound
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
rl.drawTexture(texture, screen_width / 2 - @divTrunc(texture.width, 2), 40, .white);
rl.drawText("raylib logo and sound loaded from header files", 150, 320, 20, .light_gray);
rl.drawText("Press SPACE to PLAY the sound!", 220, 370, 20, .light_gray);
}
}/*******************************************************************************************
*
* raylib [others] example - embedded files loading
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 3.0, last time updated with raylib 3.5
*
* Example contributed by Kristian Holmgren (@defutura) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2020-2025 Kristian Holmgren (@defutura) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "resources/audio_data.h" // Wave file exported with ExportWaveAsCode()
#include "resources/image_data.h" // Image file exported with ExportImageAsCode()
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [others] example - embedded files loading");
InitAudioDevice(); // Initialize audio device
// Loaded in CPU memory (RAM) from header file (audio_data.h)
// Same as: Wave wave = LoadWave("sound.wav");
Wave wave = {
.data = AUDIO_DATA,
.frameCount = AUDIO_FRAME_COUNT,
.sampleRate = AUDIO_SAMPLE_RATE,
.sampleSize = AUDIO_SAMPLE_SIZE,
.channels = AUDIO_CHANNELS
};
// Wave converted to Sound to be played
Sound sound = LoadSoundFromWave(wave);
// With a Wave loaded from file, after Sound is loaded, we can unload Wave
// but in our case, Wave is embedded in executable, in program .data segment
// we can not (and should not) try to free that private memory region
//UnloadWave(wave); // Do not unload wave data!
// Loaded in CPU memory (RAM) from header file (image_data.h)
// Same as: Image image = LoadImage("raylib_logo.png");
Image image = {
.data = IMAGE_DATA,
.width = IMAGE_WIDTH,
.height = IMAGE_HEIGHT,
.format = IMAGE_FORMAT,
.mipmaps = 1
};
// Image converted to Texture (VRAM) to be drawn
Texture2D texture = LoadTextureFromImage(image);
// With an Image loaded from file, after Texture is loaded, we can unload Image
// but in our case, Image is embedded in executable, in program .data segment
// we can not (and should not) try to free that private memory region
//UnloadImage(image); // Do not unload image data!
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE)) PlaySound(sound); // Play sound
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(texture, screenWidth/2 - texture.width/2, 40, WHITE);
DrawText("raylib logo and sound loaded from header files", 150, 320, 20, LIGHTGRAY);
DrawText("Press SPACE to PLAY the sound!", 220, 370, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadSound(sound); // Unload sound from VRAM
UnloadTexture(texture); // Unload texture from VRAM
CloseAudioDevice(); // Close audio device
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.