From 823a82611d37e618a4734ea480fab4c60b73f5b7 Mon Sep 17 00:00:00 2001 From: piernov Date: Thu, 18 Aug 2016 14:13:40 +0200 Subject: Initial Import --- display.cpp | 55 +++++++++ display.h | 34 ++++++ game.cpp | 199 +++++++++++++++++++++++++++++++ loadSongs.cpp | 367 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 126 ++++++++++++++++++++ main_old.cpp | 200 ++++++++++++++++++++++++++++++++ menu.cpp | 65 +++++++++++ 7 files changed, 1046 insertions(+) create mode 100755 display.cpp create mode 100755 display.h create mode 100755 game.cpp create mode 100755 loadSongs.cpp create mode 100755 main.cpp create mode 100755 main_old.cpp create mode 100755 menu.cpp diff --git a/display.cpp b/display.cpp new file mode 100755 index 0000000..6422030 --- /dev/null +++ b/display.cpp @@ -0,0 +1,55 @@ +#include "logging.h" +#include "display.h" + +display screen; + +/** +* Loads a BMP image into a texture on the rendering device +* @param file The BMP image file to load +* @param ren The renderer to load the texture onto +* @return the loaded texture, or nullptr if something went wrong. +*/ +SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){ + SDL_Log("loadTexture %s", file.c_str()); + //Initialize to nullptr to avoid dangling pointer issues + SDL_Texture *texture = nullptr; + //Load the image + SDL_Surface *loadedImage = IMG_Load(file.c_str()); + //If the loading went ok, convert to texture and return the texture + if (loadedImage != nullptr){ + texture = SDL_CreateTextureFromSurface(ren, loadedImage); + SDL_FreeSurface(loadedImage); + //Make sure converting went ok too + if (texture == nullptr) + logSDLError("CreateTextureFromSurface"); + } + else + logSDLError("IMG_Load"); + + return texture; +} + +/** +* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving +* the texture's width and height +* @param tex The source texture we want to draw +* @param ren The renderer we want to draw too +* @param x The x coordinate to draw too +* @param y The y coordinate to draw too +* @param angle The angle of rotation +*/ +void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, float x, float y, double angle){ + //Setup the destination rectangle to be at the position we want + SDL_Rect dst; + dst.x = x*screen.width; + dst.y = y*screen.height; + //Query the texture to get its width and height to use + SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h); + SDL_RenderCopyEx(ren, tex, NULL, &dst, angle, NULL, SDL_FLIP_NONE); +} + +void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, float x, float y){ + renderTexture(tex, ren, x, y, 0); +} + + diff --git a/display.h b/display.h new file mode 100755 index 0000000..9609140 --- /dev/null +++ b/display.h @@ -0,0 +1,34 @@ +#ifndef DISPLAY_H_INCLUDED +#define DISPLAY_H_INCLUDED + +#include +#include +#include +#include + +#define STATE_INIT 0 +#define STATE_MENU 1 +#define STATE_PLAYING 2 + + +SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren); +void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, float x, float y); + +struct display +{ + int height; + int width; + const char * name; + SDL_Renderer *renderer; + SDL_Window *window; + TTF_Font *font; + + int state; + + display(): width(600), height(600), name("MaiMai"), state(STATE_INIT) {} + +}; + +extern display screen; + +#endif // DISPLAY_H_INCLUDED 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 load_notes() +{ + int difficulty = 0; + int current_note = 0; + int current_line = 0; + int current_mes = 0; + + std::vector displayednotes; + int ms=0; + while(current_mes<=songData->songNotes[difficulty].noteData.size()) + { + std::vector 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::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 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::iterator show_note = displayednotes.begin(); show_note != displayednotes.end(); ++show_note) { + bool deleted = false; + if(show_note->orientation != "center") + for(std::vector::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"); +} diff --git a/loadSongs.cpp b/loadSongs.cpp new file mode 100755 index 0000000..0948212 --- /dev/null +++ b/loadSongs.cpp @@ -0,0 +1,367 @@ +#include +#include +#include +#include +#include +#include + +#include "logging.h" +#include "files.h" + +const char *songsPath = "/sdcard/Songs/"; + +void split(std::vector &tokens, const std::string &text, const char *sep) { + int start = 0, end = 0; + while ((end = text.find(sep, start)) != std::string::npos) { + tokens.push_back(text.substr(start, end - start)); + start = end + 1; + } + tokens.push_back(text.substr(start)); +} + +// trim from start +static inline std::string <rim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(isspace)))); + return s; +} + +// trim from end +static inline std::string &rtrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(isspace))).base(), s.end()); + return s; +} + +// trim from both ends +static inline std::string &trim(std::string &s) { + return ltrim(rtrim(s)); +} + +/* Returns a list of directories (except the ones that begin with a dot) */ + +void GetDirectories(std::vector &out, const char *directory) +{ +#ifdef WINDOWS + HANDLE dir; + WIN32_FIND_DATA file_data; + + if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE) + return; /* No files found */ + + do { + const string file_name = file_data.cFileName; + const string full_file_name = directory + "/" + file_name; + const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + + if (file_name[0] == '.') + continue; + + if (!is_directory) + continue; + + out.push_back(full_file_name); + } while (FindNextFile(dir, &file_data)); + + FindClose(dir); +#else + DIR *dir; + class dirent *ent; + class stat st; + + dir = opendir(directory); + if(dir == NULL) + { + std::string errmsg = "Cannot open directory "; + logstdError("GetDirectories", errmsg + directory); + } + else + { + while ((ent = readdir(dir)) != NULL) { + const std::string file_name = ent->d_name; + std::string full_file_name = directory; + full_file_name.append("/" + file_name + "/"); + + if (file_name[0] == '.') + continue; + + if (stat(full_file_name.c_str(), &st) == -1) + continue; + + if (!S_ISDIR(st.st_mode)) + continue; + + out.push_back(full_file_name); + } + closedir(dir); + if(out.empty()) + { + std::string errmsg = "Directory empty: "; + logstdError("GetDirectories", errmsg + directory); + } + } +#endif +} // GetFilesInDirectory + +/* Returns a list of sm files */ + +void GetSmFiles(std::vector &out, const std::string directory) +{ +#ifdef WINDOWS + HANDLE dir; + WIN32_FIND_DATA file_data; + + if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE) + return; /* No files found */ + + do { + const string file_name = file_data.cFileName; + const string full_file_name = directory + "/" + file_name; + const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + + std::vector tokens; + split(tokens, file_name, "."); + + if (strcmp(tokens.back(), "sm")) + continue; + + if (is_directory) + continue; + + out.push_back(full_file_name); + } while (FindNextFile(dir, &file_data)); + + FindClose(dir); +#else + DIR *dir; + class dirent *ent; + class stat st; + + dir = opendir(directory.c_str()); + if(dir == NULL) + { + std::string errmsg = "Cannot open directory "; + logstdError("GetDirectories", errmsg + directory); + } + else + { + while ((ent = readdir(dir)) != NULL) { + const std::string file_name = ent->d_name; + std::string full_file_name = directory; + full_file_name.append("/" + file_name); + + std::vector tokens; + split(tokens, file_name, "."); + + if (tokens.back() != "sm") + continue; + + if (stat(full_file_name.c_str(), &st) == -1) + continue; + + const bool is_readable = (st.st_mode & S_IRUSR) != 0; + + if (!is_readable) + continue; + + out.push_back(file_name); + } + closedir(dir); + if(out.empty()) + { + std::string errmsg = "Directory empty: "; + logstdError("GetDirectories", errmsg + directory); + } + } +#endif +} // GetSmFiles + + +int readSongFile(std::string &path, musicfile &songFile) +{ + bool reading_note = false; + int current_note_line = 0; + std::vector currentNoteData; + std::vector> currentLineNoteData; + notes currentNotes; + + std::string line; + std::ifstream file; + file.open(path.c_str(), std::ifstream::in); + if (!file) + { + std::string msg = "Could not open "; + msg.append(path); + logstdError("loadFile", msg); + return 1; + } + while(file.good()) + { + std::getline(file, line); + line = trim(line); + if(line.length() == 0) + continue; + else if(line[0] == '/') + continue; + else if(line[0] == '#') + { + std::vector tokens; + split(tokens, line, ":"); + + if(tokens[0].compare("#NOTES") == 0) + { + reading_note = true; + continue; + } + else + reading_note = false; + + if(*tokens[1].rbegin() == ';') + tokens[1].pop_back(); + + if(tokens[0].compare("#TITLE") == 0) + { + songFile.title = tokens[1]; + } + else if(tokens[0].compare("#ARTIST") == 0) + { + songFile.artist = tokens[1]; + } + else if(tokens[0].compare("#BANNER") == 0) + { + songFile.banner = tokens[1]; + } + else if(tokens[0].compare("#BACKGROUND") == 0) + { + songFile.background = tokens[1]; + } + else if(tokens[0].compare("#MUSIC") == 0) + { + songFile.file = tokens[1]; + } + else if(tokens[0].compare("#OFFSET") == 0) + { + std::istringstream(tokens[1]) >> songFile.offset; + } + else if(tokens[0].compare("#BPMS") == 0) + { + std::vector listBpms; + split(listBpms, tokens[1], ","); + for(std::vector::iterator bpmData = listBpms.begin(); bpmData != listBpms.end(); ++bpmData) + { + std::vector bpm; + split(bpm, *bpmData, "="); + songBpm bpms; + std::istringstream(bpm[0]) >> bpms.beat; + std::istringstream(bpm[1]) >> bpms.bpm; + songFile.bpms.push_back(bpms); + } + } + } + else if(reading_note) + { + std::vector tokens; + split(tokens, line, ":"); + + if((*line.rbegin() == ':') || (tokens.size() > 1)) + { + switch(current_note_line) + { + case 0: + currentNotes.type = tokens[0]; + break; + case 1: + currentNotes.description = tokens[0]; + break; + case 2: + currentNotes.difficultyClass = tokens[0]; + break; + case 3: + std::istringstream ( tokens[1] ) >> currentNotes.difficultyMeter; + break; + case 4: + currentNotes.radarValues = tokens[0]; + break; + default: + std::string msg = "Too much ':' for #NOTES in "; + msg.append(path); + logstdError("readSongFile", msg); + break; + } + current_note_line += 1; + } + else if(line[0] == ',') + { + currentNotes.noteData.push_back(currentLineNoteData); + currentLineNoteData.clear(); + } + else if(line[0] == ';') + { + current_note_line = 0; + songFile.songNotes.push_back(currentNotes); + static const notes tempStruct; + currentNotes = tempStruct; + } + else + { + for(std::string::iterator c = line.begin(); c != line.end(); ++c) { + switch(*c) + { + case '0': + currentNoteData.push_back(None); + break; + case '1': + currentNoteData.push_back(Tap); + break; + case '2': + currentNoteData.push_back(Hold_begin); + break; + case '3': + currentNoteData.push_back(Hold_end); + break; + case '4': + currentNoteData.push_back(Roll); + break; + case 'M': + currentNoteData.push_back(Mine); + break; + case 'L': + currentNoteData.push_back(Lift); + break; + case 'F': + currentNoteData.push_back(Fake); + break; + } + } + currentLineNoteData.push_back(currentNoteData); + currentNoteData.clear(); + } + } + } + if(!file.eof()) + { + logstdError("readSongFile", "Line cannot be read"); + } + file.close(); + return 0; +} + +std::vector loadSongs() +{ + std::vector directories; + std::vector smfiles; + std::vector songs; + + GetDirectories(directories, songsPath); + + for(std::vector::iterator it = directories.begin(); it != directories.end(); ++it) { + GetSmFiles(smfiles, *it); + for(std::vector::iterator ite = smfiles.begin(); ite != smfiles.end(); ++ite) { + musicfile songData; + std::string file = *it + *ite; + if(!readSongFile(file, songData)) + songData.path = *it; + songs.push_back(songData); + } + } + return songs; +} + diff --git a/main.cpp b/main.cpp new file mode 100755 index 0000000..bc00afd --- /dev/null +++ b/main.cpp @@ -0,0 +1,126 @@ +#include + +#include "logging.h" +#include "files.h" +#include "display.h" +#include "music.h" +#include "game.h" +#include "menu.h" + +#ifdef __cplusplus +extern "C" +#endif + +int main(int argc, char *argv[]) { + + if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ + logSDLError("SDL_Init"); + return 1; + } + + if (TTF_Init()== -1){ + logTTFError("TTF_Init"); + return 1; + } + + screen.font=TTF_OpenFont("/sdcard/LiberationMono-Regular.ttf", 14); + if(!screen.font) { + logTTFError("TTF_OpenFont: "); + } + + screen.window = SDL_CreateWindow(screen.name, 100, 100, screen.width, screen.height, SDL_WINDOW_SHOWN); + if (screen.window == nullptr){ + logSDLError("CreateWindow"); + return 2; + } + + screen.renderer = SDL_CreateRenderer(screen.window, -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if(screen.renderer == nullptr) + screen.renderer = SDL_CreateRenderer(screen.window, -1, 0); + + if (screen.renderer == nullptr){ + logSDLError("CreateRenderer"); + return 3; + } + + if(Mix_OpenAudio(MIX_SAMPLERATE, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) //Initialisation de l'API Mixer + logMixError("OpenAudio"); + + SDL_Event e; + bool quit = false; + bool playing; + + std::vector touchEvents; + + while (!quit) + { + while (SDL_PollEvent(&e)){ + switch (e.type ) { + case SDL_QUIT: + SDL_Log("SDL_QUIT"); + quit = true; + break; + case SDL_KEYDOWN: + SDL_Log("SDL_KEYDOWN"); + quit = true; + break; + case SDL_FINGERDOWN: + SDL_Log("SDL_FINGERDOWN"); + touchEvents.push_back(e.tfinger); + break; + case SDL_WINDOWEVENT: + if(e.window.event == SDL_WINDOWEVENT_RESIZED) + { + screen.width = e.window.data1; + screen.height = e.window.data2; + SDL_DestroyWindow(screen.window); + screen.window = SDL_CreateWindow(screen.name, 100, 100, screen.width, screen.height, SDL_WINDOW_SHOWN); + if (screen.window == nullptr){ + logSDLError("CreateWindow"); + return 2; + } + } + break; + } + } + + if(screen.state == STATE_INIT) + { + menu::init_menu(); + } else if(screen.state == STATE_MENU) + { + menu::update_menu(); + } else if(screen.state == STATE_PLAYING) + { +// game::update(); + } +// if(!playing) +// { +// if(!init_game()) +// playing=true; +// } +// else if(quit) +// quit_game(); +// else +// update_game(); + } + + SDL_Log("Exited main loop"); + + Mix_CloseAudio(); + SDL_Log("SDL_Mixer closed"); + SDL_DestroyRenderer(screen.renderer); + SDL_Log("Renderer destroyed"); + SDL_DestroyWindow(screen.window); + SDL_Log("Window destroyed"); + TTF_CloseFont(screen.font); + screen.font=NULL; + SDL_Log("Font closed"); + TTF_Quit(); + SDL_Log("SDL_TTF closed"); + SDL_Quit(); + SDL_Log("SDL closed"); + + return 0; +} diff --git a/main_old.cpp b/main_old.cpp new file mode 100755 index 0000000..5ee6a25 --- /dev/null +++ b/main_old.cpp @@ -0,0 +1,200 @@ +#include + +#include "logging.h" +#include "files.h" +#include "display.h" +#include "music.h" + +int main(int argc, char** argv){ + if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ + logSDLError(std::cout, "SDL_Init"); + return 1; + } + + SDL_Window *window = SDL_CreateWindow(WINDOW_NAME, 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_SHOWN); + if (window == nullptr){ + logSDLError(std::cout, "CreateWindow"); + return 2; + } + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if(renderer == nullptr) + renderer = SDL_CreateRenderer(window, -1, 0); + + if (renderer == nullptr){ + logSDLError(std::cout, "CreateRenderer"); + return 3; + } + + if(Mix_OpenAudio(MIX_SAMPLERATE, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) //Initialisation de l'API Mixer + logMixError(std::cout, "OpenAudio"); + + + Mix_Music *musique; + + SDL_Texture *background; + SDL_Texture *image; + + std::vector songs = loadSongs(); + for(std::vector::iterator songData = songs.begin(); songData != songs.end(); ++songData) { + std::string path = songData->path + songData->background; + background = loadTexture(path, renderer); + path = songData->path + songData->banner; + image = loadTexture(path, renderer); + path = songData->path + songData->music; + + if (background == nullptr || image == nullptr) + return 4; + + SDL_RenderClear(renderer); + + int bW, bH; + SDL_QueryTexture(background, NULL, NULL, &bW, &bH); + renderTexture(background, renderer, 0, 0); + renderTexture(background, renderer, bW, 0); + renderTexture(background, renderer, 0, bH); + renderTexture(background, renderer, bW, bH); + + int iW, iH; + SDL_QueryTexture(image, NULL, NULL, &iW, &iH); + int x = SCREEN_WIDTH / 2 - iW / 2; + int y = SCREEN_HEIGHT / 2 - iH / 2; + renderTexture(image, renderer, x, y); + + SDL_RenderPresent(renderer); + + SDL_Event e; + bool quit = false; + bool playing = false; + unsigned int last_tick = SDL_GetTicks(); + unsigned int last_update = 0; + int difficulty = 0; + int current_note = 0; + int current_line = 0; + int current_mes = 0; + std::vector displayednotes; + displaynote currentNote; + + SDL_Texture *left; + SDL_Texture *right; + SDL_Texture *up; + SDL_Texture *down; + + musique = Mix_LoadMUS(path.c_str()); + if(musique == nullptr) + logMixError(std::cout, "LoadMUS"); + + left = loadTexture("left_arrow.png", renderer); + right = loadTexture("right_arrow.png", renderer); + up = loadTexture("up_arrow.png", renderer); + down = loadTexture("down_arrow.png", renderer); + unsigned int first_tick = SDL_GetTicks(); + while (!quit) + { + while (SDL_PollEvent(&e)){ + if (e.type == SDL_QUIT) + quit = true; + if (e.type == SDL_KEYDOWN) + quit = true; + if (e.type == SDL_MOUSEBUTTONDOWN) + quit = true; + } + + if((SDL_GetTicks() - first_tick >= (SCREEN_HEIGHT-128*2)/5+songData->offset) && !playing) + { + + if((musique != nullptr) && (Mix_PlayMusic(musique, -1) == -1)) + logMixError(std::cout, "PlayMusic"); + playing = true; + } + + + std::vector note = songData->songNotes[difficulty].noteData[current_mes][current_line]; + + int time = (1000/(songData->bpms[0].bpm/60))-60; + + if(SDL_GetTicks() - last_tick >= time) + { + last_tick = SDL_GetTicks(); + + if(note[0] == Tap) + { + currentNote.texture = left; + currentNote.x = 10; + currentNote.y = SCREEN_HEIGHT; + displayednotes.push_back(currentNote); + } + if(note[1] == Tap) + { + currentNote.texture = up; + currentNote.x = 90; + currentNote.y = SCREEN_HEIGHT; + displayednotes.push_back(currentNote); + } + if(note[2] == Tap) + { + currentNote.texture = down; + currentNote.x = 170; + currentNote.y = SCREEN_HEIGHT; + displayednotes.push_back(currentNote); + } + if(note[3] == Tap) + { + currentNote.texture = right; + currentNote.x = 250; + currentNote.y = SCREEN_HEIGHT; + displayednotes.push_back(currentNote); + } + current_line += 1; + } + +/* renderTexture(background, renderer, 0, 0); + renderTexture(background, renderer, bW, 0); + renderTexture(background, renderer, 0, bH); + renderTexture(background, renderer, bW, bH); + + renderTexture(image, renderer, x, y); +*/ + if(SDL_GetTicks() - last_tick >= 20) +{ + SDL_RenderClear(renderer); + + renderTexture(background, renderer, 0, 0); + renderTexture(background, renderer, bW, 0); + renderTexture(background, renderer, 0, bH); + renderTexture(background, renderer, bW, bH); + + renderTexture(image, renderer, x, y); + + for(std::vector::iterator show_note = displayednotes.begin(); show_note != displayednotes.end(); ++show_note) { + renderTexture(show_note->texture, renderer, show_note->x, show_note->y); + if(SDL_GetTicks() - last_tick >= 1) + { + last_update = SDL_GetTicks(); + show_note->y -= 5; + } + } + SDL_RenderPresent(renderer); +} + + if(songData->songNotes[difficulty].noteData[current_mes].size() <= current_line) + { + current_line = 0; + current_mes +=1; + } + + } + + SDL_DestroyTexture(background); + SDL_DestroyTexture(image); + if(musique) Mix_FreeMusic(musique); +} + + Mix_CloseAudio(); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +} diff --git a/menu.cpp b/menu.cpp new file mode 100755 index 0000000..d87bbdd --- /dev/null +++ b/menu.cpp @@ -0,0 +1,65 @@ +#include "menu.h" + +namespace menu +{ + std::vector songs; + SDL_Texture *background; + +int init_menu() +{ + SDL_Texture *image; + + songs = loadSongs(); + + + screen.state = STATE_MENU; +} + +int update_menu() +{ + int height = 0; + for(std::vector::iterator songData = songs.begin(); songData != songs.end(); ++songData) { + + std::string path = songData->path + songData->background; + background = loadTexture(path, screen.renderer); + path = songData->path + songData->banner; + image = loadTexture(path, screen.renderer); + path = songData->path + songData->file; + + if (background == nullptr || image == nullptr) + continue; + + SDL_RenderClear(screen.renderer); + + SDL_Color color={255,0,0}; + + SDL_Surface *text = TTF_RenderText_Blended(screen.font, path.c_str(), color); + SDL_Texture* texture = SDL_CreateTextureFromSurface(screen.renderer, text); + renderTexture(texture, screen.renderer, 0.5f, 0.5f); + + + int w, h; + + float bW, bH; + SDL_QueryTexture(background, NULL, NULL, &w, &h); + bW = (float)w / (float)screen.width; + bH = (float)h / (float)screen.height; + + float iW, iH; + SDL_QueryTexture(image, NULL, NULL, &w, &h); + //iW = 0.5f - ( / ( ) /2; + //iH = 0.5f - ( (float)h / (float)screen.height ) /2; + iW = (float)screen.width - (float)w; + iH = height; + + renderTexture(background, renderer, bW, bH); + renderTexture(image, renderer, iW, iH); + + SDL_RenderPresent(screen.renderer); + height += height + 0.02f; + } + +} + + +}; -- cgit v1.2.3-54-g00ecf