void GameState::saveHighScore() { std::string saveFilename; #if defined (DB_WIN32) saveFilename = ResourceHandler::getInstance()->getRealFilename("dr.madness.highscore.txt"); #else saveFilename = std::string(getenv("HOME")) + "/.dr.madness.highscore.txt"; #endif #if defined (DB_WIN32) if (!exists(saveFilename.c_str())) #else if (!file_exists(saveFilename.c_str(), FA_HIDDEN, NULL)) #endif { throw DBSH07_EXCEPTION("Unable to open " + saveFilename); } std::ofstream os(saveFilename.c_str()); if (!os.good()) { throw DBSH07_EXCEPTION("Unable to open " + saveFilename); } for (int row = 0; row < mHighScore.size() && row < 10; row++) { HighScorePair p = mHighScore[row]; os << p.name << "," << p.points << std::endl; } os.close(); }
std::string loadFile(const std::string &fileName, bool fullpath) { std::string realFilename; if (fullpath) { realFilename = fileName; } else { realFilename = ResourceHandler::getInstance()->getRealFilename(fileName); } std::ifstream is(realFilename.c_str()); if (!is.good()) { throw DBSH07_EXCEPTION("Unable to open " + fileName); } std::string result; while (is.good()) { int c = is.get(); if (c >= 0) { result += (unsigned char)c; } else { break; } } return result; }
void GameState::loadLevelsData() { std::vector<std::string> data = tokenize(loadFile("levels.txt"), "\n", true); if (data.size() == 0) { throw DBSH07_EXCEPTION("levels.txt is empty!"); } int numberOfLevels = 0; int numberOfBonusLevels = 0; for (unsigned int i = 0; i < data.size(); i++) { std::vector<std::string> row = tokenize(data[i], " "); if (row.size() < 2) { throw DBSH07_EXCEPTION("Corrupt levels.txt file at row " + toString(i) + "."); } if (row[0] == "N") { numberOfLevels++; mLevelsData.push_back(LevelData(row[1], false, "LEVEL " + toString(numberOfLevels))); } else if (row[0] == "B") { numberOfBonusLevels++; mLevelsData.push_back(LevelData(row[1], true, "BONUS LEVEL " + toString(numberOfBonusLevels))); } else { throw DBSH07_EXCEPTION("Corrupt levels.txt file at row " + toString(i) +". Unknown token " + row[0] + "."); } } }
SAMPLE* ResourceHandler::getSample(const std::string& filename) { if (mSamples.find(filename) == mSamples.end()) { SAMPLE* sample = load_sample(getRealFilename(filename).c_str()); if (sample == NULL) { throw DBSH07_EXCEPTION("Unable to load: " + getRealFilename(filename)); } mSamples[filename] = sample; } return mSamples[filename]; }
BITMAP* ResourceHandler::getBitmap(const std::string &filename) { if (mBitmaps.find(filename) == mBitmaps.end()) { BITMAP* bitmap = load_bitmap(getRealFilename(filename).c_str(), NULL); if (bitmap == NULL) { throw DBSH07_EXCEPTION("Unable to load: " + getRealFilename(filename)); } mBitmaps[filename] = bitmap; } return mBitmaps[filename]; }
Game::Game() :mPauseButtonPressed(false), mLevel(NULL) { allegro_init(); install_keyboard(); install_mouse(); set_window_title("Dr. Madness Or: How Fluffy Love Learned to Stop " "Worrying and Love The Bomb"); int driver = GFX_AUTODETECT_FULLSCREEN; set_color_depth(32); if (set_gfx_mode(driver, 640, 480, 0, 0) < 0) { set_color_depth(24); if (set_gfx_mode(driver, 640, 480, 0, 0) < 0) { set_color_depth(16); if (set_gfx_mode(driver, 640, 480, 0, 0) < 0) { set_color_depth(15); if (set_gfx_mode(driver, 640, 480, 0, 0) < 0) { throw DBSH07_EXCEPTION("Unable to set graphics mode."); } } } } install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL); initTimer(); mScreenBuffer = create_bitmap(640, 480); mBuffer = create_bitmap(320, 240); mSplashScreen = new SplashScreen(); mEnding = new Ending(); initGui(); initMusic(); setState(SPLASHSCREEN); }
void GameState::loadHighScore() { std::string saveFilename; #if defined (DB_WIN32) saveFilename = "data/dr.madness.highscore.txt"; #else saveFilename = std::string(getenv("HOME")) + "/.dr.madness.highscore.txt"; #endif #if defined (DB_WIN32) if (!exists(saveFilename.c_str())) #else if (!file_exists(saveFilename.c_str(), FA_HIDDEN, NULL)) #endif { throw DBSH07_EXCEPTION("Unable to open " + saveFilename); } std::vector<std::string> data = tokenize(loadFile(saveFilename, true), "\n"); mHighScore.clear(); for (unsigned int row = 0; row < data.size(); row++) { std::vector<std::string> stringPair = tokenize(data[row], ","); if (stringPair.size() == 2) { HighScorePair p; p.name = stringPair[0]; p.points = fromString<int>(stringPair[1]); mHighScore.push_back(p); } } while (mHighScore.size() > 10) { mHighScore.pop_back(); } }
void Game::draw() { acquire_bitmap(mBuffer); switch (mState) { case SPLASHSCREEN: mSplashScreen->draw(mBuffer); break; case END: mEnding->draw(mBuffer); break; case BONUS_LEVEL_OR_SHOP: clear_to_color(mBuffer, makecol(0, 0, 0)); mLevel->draw(mBuffer); mAllegroGraphics->setTarget(mBuffer); mGui->draw(); draw_sprite(mBuffer, mouse_sprite, mouse_x / 2, mouse_y / 2); break; case HIGH_SCORE: case SHOP: case MENU: mAllegroGraphics->setTarget(mBuffer); mGui->draw(); draw_sprite(mBuffer, mouse_sprite, mouse_x / 2, mouse_y / 2); break; case LEVEL: clear_to_color(mBuffer, makecol(0, 0, 0)); mLevel->draw(mBuffer); break; case PAUSE: break; case EXIT: break; default: throw DBSH07_EXCEPTION("Unknown game state."); } release_bitmap(mBuffer); }
void playMusic(std::string file, float volume) { if (playingFile == file) { return; } if (playing != NULL) { al_stop_duh(player); unload_duh(playing); } std::string fileName = ResourceHandler::getInstance()->getRealFilename(file); if (!exists(fileName.c_str())) { throw DBSH07_EXCEPTION("Unable to load " + fileName); } playing = dumb_load_xm_quick(fileName.c_str()); player = al_start_duh(playing, 2, 0, volume, 4096, 44100); playingFile = file; pollMusic(); }
void Game::logic() { pollMusic(); switch (mState) { case SPLASHSCREEN: mSplashScreen->logic(); if (mSplashScreen->isDrawingDone()) { clear_keybuf(); setState(MENU); } break; case BONUS_LEVEL_OR_SHOP: mLevel->logic(); if (mOptionalDialog->getState() == OptionalDialog::NONE && !mOptionalDialog->isVisible() && !mDialog->isVisible()) { mOptionalDialog->setVisible(true); } else if (!mDialog->isVisible() && mOptionalDialog->getState() == OptionalDialog::SHOP) { setState(SHOP); } else if (!mDialog->isVisible() && mOptionalDialog->getState() == OptionalDialog::BONUS_LEVEL) { startNextLevel(); } break; case HIGH_SCORE: case SHOP: case MENU: break; case LEVEL: mLevel->logic(); if (mLevel->isGameOver()) { setState(HIGH_SCORE); } else if (mLevel->isLevelComplete()) { std::cout << "LEVEL COMPLETE" << std::endl; prepareNextLevel(); } else if (mLevel->isQuit()) { setState(MENU); } break; case END: mEnding->logic(); if (mEnding->isDone()) { setState(HIGH_SCORE); } break; case PAUSE: break; case EXIT: break; default: throw DBSH07_EXCEPTION("Unknown game state."); } mGui->logic(); if (key[KEY_F10]) { setState(EXIT); } if (key[KEY_P] && !mPauseButtonPressed && mState == PAUSE) { setState(LEVEL); } else if (key[KEY_P] && !mPauseButtonPressed && mState == LEVEL) { setState(PAUSE); } mPauseButtonPressed = key[KEY_P] != 0; }