2014-04-19 18:54:48 +04:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
2014-09-21 16:26:42 +04:00
|
|
|
* raylib [audio] example - Music playing (streaming)
|
2014-04-19 18:54:48 +04:00
|
|
|
*
|
2019-05-20 17:36:42 +03:00
|
|
|
* This example has been created using raylib 1.3 (www.raylib.com)
|
2014-04-19 18:54:48 +04:00
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
2015-09-01 23:59:16 +03:00
|
|
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
2014-04-19 18:54:48 +04:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2014-04-19 18:54:48 +04:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-09-21 16:26:42 +04:00
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)");
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-09-21 16:26:42 +04:00
|
|
|
InitAudioDevice(); // Initialize audio device
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2017-04-09 00:31:58 +03:00
|
|
|
Music music = LoadMusicStream("resources/guitar_noodling.ogg");
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-08-01 13:49:17 +03:00
|
|
|
PlayMusicStream(music);
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2016-01-16 14:52:55 +03:00
|
|
|
float timePlayed = 0.0f;
|
2016-09-15 12:53:16 +03:00
|
|
|
bool pause = false;
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-09-21 16:26:42 +04:00
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2014-04-19 18:54:48 +04:00
|
|
|
//--------------------------------------------------------------------------------------
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-04-19 18:54:48 +04:00
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
UpdateMusicStream(music); // Update music buffer with new stream data
|
|
|
|
|
2016-09-15 12:53:16 +03:00
|
|
|
// Restart music playing (stop and play)
|
2019-05-20 17:36:42 +03:00
|
|
|
if (IsKeyPressed(KEY_SPACE))
|
2014-04-19 18:54:48 +04:00
|
|
|
{
|
2016-09-15 12:53:16 +03:00
|
|
|
StopMusicStream(music);
|
|
|
|
PlayMusicStream(music);
|
2014-04-19 18:54:48 +04:00
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
// Pause/Resume music playing
|
2016-09-15 12:53:16 +03:00
|
|
|
if (IsKeyPressed(KEY_P))
|
|
|
|
{
|
|
|
|
pause = !pause;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-09-15 12:53:16 +03:00
|
|
|
if (pause) PauseMusicStream(music);
|
|
|
|
else ResumeMusicStream(music);
|
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-09-15 12:53:16 +03:00
|
|
|
// Get timePlayed scaled to bar dimensions (400 pixels)
|
2016-12-25 03:58:56 +03:00
|
|
|
timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*400;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-10-31 19:04:24 +03:00
|
|
|
if (timePlayed > 400) StopMusicStream(music);
|
2014-04-19 18:54:48 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-04-19 18:54:48 +04:00
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-04-19 18:54:48 +04:00
|
|
|
ClearBackground(RAYWHITE);
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2016-09-15 12:53:16 +03:00
|
|
|
DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2016-09-15 12:53:16 +03:00
|
|
|
DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
|
|
|
|
DrawRectangle(200, 200, (int)timePlayed, 12, MAROON);
|
|
|
|
DrawRectangleLines(200, 200, 400, 12, GRAY);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-09-15 12:53:16 +03:00
|
|
|
DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
|
|
|
|
DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-04-19 18:54:48 +04:00
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2016-08-01 13:49:17 +03:00
|
|
|
UnloadMusicStream(music); // Unload music stream buffers from RAM
|
|
|
|
|
|
|
|
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2016-08-01 13:49:17 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
2014-04-19 18:54:48 +04:00
|
|
|
//--------------------------------------------------------------------------------------
|
2014-09-30 01:41:05 +04:00
|
|
|
|
2014-04-19 18:54:48 +04:00
|
|
|
return 0;
|
|
|
|
}
|