您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

109 行
4.4 KiB

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