summaryrefslogtreecommitdiffstats
path: root/game.cpp
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-08-18 14:13:40 +0200
committerpiernov <piernov@piernov.org>2016-08-18 14:13:40 +0200
commit823a82611d37e618a4734ea480fab4c60b73f5b7 (patch)
treead26261abe2c2cf976f3ca38c00f0fefb4373fbd /game.cpp
downloadMaiMai-C++-823a82611d37e618a4734ea480fab4c60b73f5b7.tar.gz
MaiMai-C++-823a82611d37e618a4734ea480fab4c60b73f5b7.tar.bz2
MaiMai-C++-823a82611d37e618a4734ea480fab4c60b73f5b7.tar.xz
MaiMai-C++-823a82611d37e618a4734ea480fab4c60b73f5b7.zip
Initial ImportHEADmaster
Diffstat (limited to 'game.cpp')
-rwxr-xr-xgame.cpp199
1 files changed, 199 insertions, 0 deletions
diff --git a/game.cpp b/game.cpp
new file mode 100755
index 0000000..5090746
--- /dev/null
+++ b/game.cpp
@@ -0,0 +1,199 @@
+#include "display.h"
+#include "music.h"
+
+std::vector<displaynote> load_notes()
+{
+ int difficulty = 0;
+ int current_note = 0;
+ int current_line = 0;
+ int current_mes = 0;
+
+ std::vector<displaynote> displayednotes;
+ int ms=0;
+ while(current_mes<=songData->songNotes[difficulty].noteData.size())
+ {
+ std::vector<Note> note = songData->songNotes[difficulty].noteData[current_mes][current_line];
+
+ float time = 1000.0f/(songData->bpms[0].bpm/60.0f);
+
+ if(songData->songNotes[difficulty].noteData[current_mes].size() != 4)
+ time = time/((float)songData->songNotes[difficulty].noteData[current_mes].size()/4.0f);
+
+ for(std::vector<displaynote>::iterator current_note = note.begin(); show_note != note.end(); ++current_note) {
+ if(note[0] == Tap)
+ {
+ currentNote.orientation = "left";
+ displayednotes[ms]=currentNote;
+ }
+ if(note[1] == Tap)
+ {
+ currentNote.orientation = "up";
+ displayednotes[ms]=currentNote;
+ }
+ if(note[2] == Tap)
+ {
+ currentNote.orientation = "down";
+ displayednotes[ms]=currentNote;
+ }
+ if(note[3] == Tap)
+ {
+ currentNote.orientation = "right";
+ currentNote.x = 0.5f - (float)rightW / (float)(SCREEN_WIDTH*2);
+ currentNote.y = 0.5f - (float)rightH / (float)(SCREEN_HEIGHT*2);
+ displayednotes[ms]=currentNote;
+ }
+ }
+ current_line += 1;
+
+ if(songData->songNotes[difficulty].noteData[current_mes].size() <= current_line)
+ {
+ current_line = 0;
+ current_mes +=1;
+ }
+ ms+=time;
+ }
+ return displayednotes;
+}
+
+int init_game()
+{
+ unsigned int last_tick = SDL_GetTicks();
+ unsigned int last_update = 0;
+ std::vector<displaynote> displayednotes = load_notes();
+ displaynote currentNote;
+
+ SDL_Texture *arrow;
+
+ musique = Mix_LoadMUS(path.c_str());
+ if(musique == nullptr)
+ logMixError(std::cout, "LoadMUS");
+
+ arrow = loadTexture("/sdcard/up_arrow.png", screen.renderer);
+
+ int arrowW, arrowH;
+ SDL_QueryTexture(arrow, NULL, NULL, &arrowW, &arrowH);
+
+ unsigned int first_tick = SDL_GetTicks();
+ float song_offset = (((0.4f/speed)*20.0f)+songData->offset*1000.0f);
+ SDL_Log("offset: %f", song_offset);
+}
+
+int update_game()
+{
+
+ if(((SDL_GetTicks() - first_tick) >= (int)song_offset) && !playing)
+ {
+ if((musique != nullptr) && (Mix_PlayMusic(musique, 0) == -1))
+ {
+ logMixError(std::cout, "PlayMusic");
+ quit = true;
+ }
+ else
+ {
+ first_tick = SDL_GetTicks();
+ playing = true;
+ }
+ }
+
+ if(SDL_GetTicks() - last_update >= 20)
+ {
+ last_update = SDL_GetTicks();
+ SDL_RenderPresent(renderer);
+
+ SDL_RenderClear(renderer);
+ renderTexture(background, renderer, 0.0f, 0.0f);
+ renderTexture(background, renderer, bW, 0.0f);
+ renderTexture(background, renderer, 0.0f, bH);
+ renderTexture(background, renderer, bW, bH);
+
+ SDL_SetTextureColorMod(left, 0, 0, 255);
+ SDL_SetTextureColorMod(up, 0, 0, 255);
+ SDL_SetTextureColorMod(down, 0, 0, 255);
+ SDL_SetTextureColorMod(right, 0, 0, 255);
+
+ renderTexture(left, renderer, 0.1f - (float)leftW / (float)(SCREEN_WIDTH*2), 0.5f - (float)leftH / (float)(SCREEN_HEIGHT*2));
+ renderTexture(up, renderer, 0.5f - (float)upW / (float)(SCREEN_WIDTH*2), 0.9f - (float)upH / (float)(SCREEN_HEIGHT*2));
+ renderTexture(down, renderer, 0.5f - (float)downW / (float)(SCREEN_WIDTH*2), 0.1f - (float)downH / (float)(SCREEN_HEIGHT*2));
+ renderTexture(right, renderer, 0.9f - (float)rightW / (float)(SCREEN_WIDTH*2), 0.5f - (float)rightH / (float)(SCREEN_HEIGHT*2));
+
+ SDL_SetTextureColorMod(left, 255, 255, 255);
+ SDL_SetTextureColorMod(up, 255, 255, 255);
+ SDL_SetTextureColorMod(down, 255, 255, 255);
+ SDL_SetTextureColorMod(right, 255, 255, 255);
+
+ for(std::vector<displaynote>::iterator show_note = displayednotes.begin(); show_note != displayednotes.end(); ++show_note) {
+ bool deleted = false;
+ if(show_note->orientation != "center")
+ for(std::vector<SDL_TouchFingerEvent>::iterator touch = touchEvents.begin(); touch != touchEvents.end(); ++touch) {
+
+ if( ((show_note->orientation == "left") &&
+ (show_note->x >= 0.05f && show_note->x <= 0.15f && show_note->y == 0.5f - (float)leftH / (float)(SCREEN_HEIGHT*2)) &&
+ (touch->x >= 0.0f && touch->x <= 0.25f && touch->y >= 0.20f && touch->y <= 0.80f)) ||
+ ((show_note->orientation == "up") &&
+ (show_note->x == 0.5f - (float)upW / (float)(SCREEN_WIDTH*2) && show_note->y >= 0.05f && show_note->y <= 0.15f) &&
+ (touch->x >= 0.20f && touch->x <= 0.80f && touch->y >= 0.0f && touch->y <= 0.25f)) ||
+ ((show_note->orientation == "down") &&
+ (show_note->x == 0.5f - (float)downW / (float)(SCREEN_WIDTH*2) && show_note->y >= 0.85f && show_note->y <= 0.95f) &&
+ (touch->x >= 0.20f && touch->x <= 0.80f && touch->y >= 1.0f && touch->y <= 0.75f)) ||
+ ((show_note->orientation == "right") &&
+ (show_note->x >= 0.85f && show_note->x <= 0.95f && show_note->y == 0.5f - (float)rightH / (float)(SCREEN_HEIGHT*2)) &&
+ (touch->x >= 0.75f && touch->x <= 1.0f && touch->y >= 0.20f && touch->y <= 0.80f)))
+ {
+
+ currentNote.texture = image;
+ currentNote.orientation = "center";
+ currentNote.x = iW;
+ currentNote.y = iH;
+ displayednotes.push_back(currentNote);
+
+ SDL_SetTextureColorMod(show_note->texture, 255, 0, 0);
+ renderTexture(show_note->texture, renderer, show_note->x, show_note->y);
+ SDL_SetTextureColorMod(show_note->texture, 255, 255, 255);
+ touchEvents.erase(touch);
+ displayednotes.erase(show_note);
+ deleted = true;
+ break;
+ }
+ }
+
+ if(!deleted)
+ {
+ if(show_note->x <= 0.0f || show_note->y <= 0.0f)
+ {
+ displayednotes.erase(show_note);
+ continue;
+ }
+ renderTexture(show_note->texture, renderer, show_note->x, show_note->y);
+
+ if(show_note->orientation == "left")
+ show_note->x -= speed;
+ if(show_note->orientation == "up")
+ show_note->y -= speed;
+ if(show_note->orientation == "down")
+ show_note->y += speed;
+ if(show_note->orientation == "right")
+ show_note->x += speed;
+ if(show_note->orientation == "center")
+ {
+ show_note->y += speed;
+ show_note->x += speed;
+ }
+ }
+ }
+
+ touchEvents.clear();
+ }
+}
+
+int quit_game()
+{
+ SDL_DestroyTexture(background);
+ SDL_DestroyTexture(image);
+
+ SDL_Log("Music was playing");
+ Mix_HaltMusic();
+ SDL_Log("Music halted");
+ Mix_FreeMusic(musique);
+ musique = NULL;
+ SDL_Log("Music freed");
+}