diff options
| author | raysan5 <[email protected]> | 2020-05-14 23:58:36 +0200 |
|---|---|---|
| committer | raysan5 <[email protected]> | 2020-05-14 23:58:36 +0200 |
| commit | b897ae092a31f29f6ac6ebd2b6dac3080ccd5372 (patch) | |
| tree | 6fa407b461b5117a5db62d67ef4f9ab469c23be0 /examples/others/embedded_files_loading.c | |
| parent | d14c51aa2a5d04ff3ab69ea719ed3e26499ae2a0 (diff) | |
| download | raylib-b897ae092a31f29f6ac6ebd2b6dac3080ccd5372.tar.gz raylib-b897ae092a31f29f6ac6ebd2b6dac3080ccd5372.zip | |
ADDED: Ecample: embedded files loading
Diffstat (limited to 'examples/others/embedded_files_loading.c')
| -rw-r--r-- | examples/others/embedded_files_loading.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/examples/others/embedded_files_loading.c b/examples/others/embedded_files_loading.c new file mode 100644 index 00000000..7064ebc6 --- /dev/null +++ b/examples/others/embedded_files_loading.c @@ -0,0 +1,103 @@ +/******************************************************************************************* +* +* raylib [others] example - Embedded files loading (Wave and Image) +* +* This example has been created using raylib 3.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by Kristian Holmgren (@defutura) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2020 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() + +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, + .sampleCount = AUDIO_SAMPLE_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", 65, 320, 20, LIGHTGRAY); + DrawText("Press SPACE to PLAY the sound!", 200, 360, 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; +}
\ No newline at end of file |
