diff options
| -rw-r--r-- | World.cpp | 26 | ||||
| -rw-r--r-- | World.h | 1 | ||||
| -rw-r--r-- | coin.bmp | bin | 120122 -> 10138 bytes | |||
| -rw-r--r-- | coin.wav | bin | 0 -> 39824 bytes | |||
| -rw-r--r-- | died.wav | bin | 0 -> 661544 bytes | |||
| -rw-r--r-- | main.cpp | 45 | ||||
| -rw-r--r-- | music.wav | bin | 0 -> 4834942 bytes | |||
| -rw-r--r-- | win.wav | bin | 0 -> 882070 bytes |
8 files changed, 63 insertions, 9 deletions
@@ -1,8 +1,9 @@ +#include <random> #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<bricks.size();i++) { - coins_pos.push_back(4); + coins_pos.push_back(dis(gen)); } @@ -135,7 +141,21 @@ void World::sim(double time) } if(last_brick==4){player.y--;player.y2=0;} - if(last_brick==3){player.y++;player.y2=0;} + if(last_brick==3) + { + player.y++;player.y2=0; + if(coins_pos[player.x-1]==player.y){ + coins_pos[player.x-1]=99; + coins++; + player.collected_coin=true; + } + } + + if(coins_pos[player.x]==player.y){ + coins_pos[player.x]=99; + coins++; + player.collected_coin=true; + } bool ok=false; for(Brick &brick:bricks[player.x]) @@ -17,6 +17,7 @@ struct Player int anim; bool dead; bool win; + bool collected_coin; }; struct World Binary files differdiff --git a/coin.wav b/coin.wav Binary files differnew file mode 100644 index 0000000..c0dc31c --- /dev/null +++ b/coin.wav diff --git a/died.wav b/died.wav Binary files differnew file mode 100644 index 0000000..2049014 --- /dev/null +++ b/died.wav @@ -6,10 +6,39 @@ using SDLInitType=std::pair<int,std::pair<SDL_Window*,SDL_Renderer*>>; +// 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:"<<win_width << "*" << win_height ; sdl_put_str(ren, textures[3],10, 10, 4,strbuffer.str(),190,190,222); if(world.player.dead) { @@ -289,6 +317,11 @@ int main(int, char**){ // finish rendering SDL_RenderPresent(ren); + if(world.player.collected_coin) + { + sdl_play_sound(); + world.player.collected_coin=false; + } // progress world simu world.sim(deltaTime); diff --git a/music.wav b/music.wav Binary files differBinary files differnew file mode 100644 index 0000000..5a48762 --- /dev/null +++ b/music.wav |
