You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [others] example - Embedded files loading (Wave and Image)
  4. *
  5. * This example has been created using raylib 3.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Kristian Holmgren (@defutura) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2020 Kristian Holmgren (@defutura) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include "resources/audio_data.h" // Wave file exported with ExportWaveAsCode()
  15. #include "resources/image_data.h" // Image file exported with ExportImageAsCode()
  16. int main(void)
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. const int screenWidth = 800;
  21. const int screenHeight = 450;
  22. InitWindow(screenWidth, screenHeight, "raylib [others] example - embedded files loading");
  23. InitAudioDevice(); // Initialize audio device
  24. // Loaded in CPU memory (RAM) from header file (audio_data.h)
  25. // Same as: Wave wave = LoadWave("sound.wav");
  26. Wave wave = {
  27. .data = AUDIO_DATA,
  28. .sampleCount = AUDIO_SAMPLE_COUNT,
  29. .sampleRate = AUDIO_SAMPLE_RATE,
  30. .sampleSize = AUDIO_SAMPLE_SIZE,
  31. .channels = AUDIO_CHANNELS
  32. };
  33. // Wave converted to Sound to be played
  34. Sound sound = LoadSoundFromWave(wave);
  35. // With a Wave loaded from file, after Sound is loaded, we can unload Wave
  36. // but in our case, Wave is embedded in executable, in program .data segment
  37. // we can not (and should not) try to free that private memory region
  38. //UnloadWave(wave); // Do not unload wave data!
  39. // Loaded in CPU memory (RAM) from header file (image_data.h)
  40. // Same as: Image image = LoadImage("raylib_logo.png");
  41. Image image = {
  42. .data = IMAGE_DATA,
  43. .width = IMAGE_WIDTH,
  44. .height = IMAGE_HEIGHT,
  45. .format = IMAGE_FORMAT,
  46. .mipmaps = 1
  47. };
  48. // Image converted to Texture (VRAM) to be drawn
  49. Texture2D texture = LoadTextureFromImage(image);
  50. // With an Image loaded from file, after Texture is loaded, we can unload Image
  51. // but in our case, Image is embedded in executable, in program .data segment
  52. // we can not (and should not) try to free that private memory region
  53. //UnloadImage(image); // Do not unload image data!
  54. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  55. //--------------------------------------------------------------------------------------
  56. // Main game loop
  57. while (!WindowShouldClose()) // Detect window close button or ESC key
  58. {
  59. // Update
  60. //----------------------------------------------------------------------------------
  61. if (IsKeyPressed(KEY_SPACE)) PlaySound(sound); // Play sound
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. DrawTexture(texture, screenWidth/2 - texture.width/2, 40, WHITE);
  68. DrawText("raylib logo and sound loaded from header files", 65, 320, 20, LIGHTGRAY);
  69. DrawText("Press SPACE to PLAY the sound!", 200, 360, 20, LIGHTGRAY);
  70. EndDrawing();
  71. //----------------------------------------------------------------------------------
  72. }
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. UnloadSound(sound); // Unload sound from VRAM
  76. UnloadTexture(texture); // Unload texture from VRAM
  77. CloseAudioDevice(); // Close audio device
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }