From 1555b96b32a919c3e3b9e44ac1d80dab005d9ab8 Mon Sep 17 00:00:00 2001 From: miguel Date: Wed, 13 Sep 2017 11:55:36 +0200 Subject: added coin collecting sounds --- World.cpp | 26 +++++++++++++++++++++++--- World.h | 1 + coin.bmp | Bin 120122 -> 10138 bytes coin.wav | Bin 0 -> 39824 bytes died.wav | Bin 0 -> 661544 bytes main.cpp | 45 +++++++++++++++++++++++++++++++++++++++------ music.wav | Bin 0 -> 4834942 bytes win.wav | Bin 0 -> 882070 bytes 8 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 coin.wav create mode 100644 died.wav create mode 100644 music.wav create mode 100644 win.wav diff --git a/World.cpp b/World.cpp index 0e5bff2..04801f5 100644 --- a/World.cpp +++ b/World.cpp @@ -1,8 +1,9 @@ +#include #include "World.h" - World::World(int l):level(l) { + coins=0; level--; next_level(); reset(); @@ -10,6 +11,10 @@ World::World(int l):level(l) void World::reset() { + std::random_device rd; //Will be used to obtain a seed for the random number engine + std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() + std::uniform_int_distribution<> dis(-5, 10); + player.x2=player.x=0; player.speed=level; player.anim=0; @@ -17,12 +22,13 @@ void World::reset() player.y2=0; player.dead=false; player.win=false; + player.collected_coin=false; coins_pos.clear(); for(int i=0;i>; +// audio from http://www.brainybetty.com/soundsforpowerpoint2.htm +// https://freesound.org/people/ProjectsU012/sounds/341695/ +void sdl_play_sound() +{ + + static bool first=true; + static SDL_AudioSpec wavSpec; + static Uint32 wavLength; + static Uint8 *wavBuffer; + static SDL_AudioDeviceID deviceId; + + if (first) + { + SDL_LoadWAV("coin.wav", &wavSpec, &wavBuffer, &wavLength); + deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0); + SDL_PauseAudioDevice(deviceId, 0); + first=false; + } + +// SDL_LoadWAV("died.wav", &wavSpec, &wavBuffer, &wavLength); +// SDL_LoadWAV("music.wav", &wavSpec, &wavBuffer, &wavLength); +// SDL_LoadWAV("win.wav", &wavSpec, &wavBuffer, &wavLength); + + int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength); + + //SDL_CloseAudioDevice(deviceId); + //SDL_FreeWAV(wavBuffer); +} + SDLInitType sdl_start(int win_width, int win_height) { //First we need to start up SDL, and make sure it went ok - if (SDL_Init(SDL_INIT_VIDEO) != 0){ + if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0){ std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return {1,{nullptr,nullptr}}; } @@ -151,7 +180,6 @@ int main(int, char**){ textures.push_back(sdl_load_texture("gridder01.bmp",255,255,255,ren)); textures.push_back(sdl_load_texture("gridder02.bmp",255,255,255,ren)); - // init world LEVEL 1 World world(1); @@ -177,9 +205,9 @@ int main(int, char**){ { if (event.type == SDL_WINDOWEVENT && (event.window.event==SDL_WINDOWEVENT_RESIZED || event.window.event==SDL_WINDOWEVENT_SIZE_CHANGED)) { - SDL_Log("Window %d size changed to %dx%d", - event.window.windowID, event.window.data1, - event.window.data2); +// SDL_Log("Window %d size changed to %dx%d", + // event.window.windowID, event.window.data1, + //event.window.data2); win_width=event.window.data1; win_height=event.window.data2; @@ -267,7 +295,7 @@ int main(int, char**){ // HUD std::stringstream strbuffer; - strbuffer << "COINS: " << world.coins << " LEVEL: " << world.level << " FPS: " << fps ; + strbuffer << "SCORE:" << world.coins << " | LVL:" << world.level << " | FPS:" << fps << " | RES:"<