Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

107 Zeilen
4.3 KiB

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