int main() { std::cout << "Abstact Factory GoF Kata" << std::endl; GUIFactory* guiFactory = NULL; Button* button = NULL; // Mac stuff guiFactory = new MacGUIFactory(); // notice that this in the only diff between Mac and Win code! button = guiFactory->getButton(); button->Draw(); // we all love c++ delete guiFactory; guiFactory = NULL; delete button; button = NULL; // Win stuff guiFactory = new WinGUIFactory(); // notice that this in the only diff between Mac and Win code! button = guiFactory->getButton(); button->Draw(); // we all love c++ delete guiFactory; guiFactory = NULL; delete button; button = NULL; return 0; }
void Game::ShowFail()//显示失败信息 { static FullScrAni ani; static Button butcont(clientw*4/5-200,clienth*4/5); static Button butback(clientw/5,clienth*4/5); ani.SetAni(ANI_FAIL); ani.Draw(); butcont.SetAni(ANI_BUTTON,ANI_BUTTON_CONTINUE); butcont.Deal(); butcont.Draw(); butback.SetAni(ANI_BUTTON,ANI_BUTTON_BACK); butback.Deal(); butback.Draw(); if(butcont.MousePressed()) { butcont.Recover(); state=GAME_RESTART; ani.Ani()->Restart(); } else if(butback.MousePressed()) { butback.Recover(); level=1; Destroy(); state=GAME_SHOWMENU; } }
void Game::Run()//游戏运行 { //界面元素的创建 static FullScrAni outline; static Text texlevel(675,217,20,20); static Text texfood(675,300,20,20); static Text texscore(675,387,20,20); static Button butpause(628,500,100,50); static Button butret(740,40,50,50); //界面元素动画的设置 butpause.SetAni(ANI_BUTTON,pPanel->IsPause()?ANI_BUTTON_RECOVER:ANI_BUTTON_PAUSE); butret.SetAni(ANI_BUTTON,ANI_BUTTON_RETURN); outline.SetAni(ANI_OUTLINE); //响应 butpause.Deal(); butret.Deal(); if(butpause.MousePressed()) {//是否暂停 butpause.Recover(); pPanel->SetPause(!pPanel->IsPause()); } outline.Draw(); pPanel->DoFrame(); //绘制 butpause.Draw(); butret.Draw(); texlevel.SetText(level,RGB(255,255,255)); texscore.SetText(pPanel->Score(),RGB(255,255,255)); texfood.SetText(pPanel->RestFood(),RGB(255,255,255)); texlevel.Draw(); texscore.Draw(); texfood.Draw(); if(pPanel->Overed()) { if(pPanel->Won())state=GAME_WIN; else state=GAME_FAIL; manager.Stop(); } if(butret.MousePressed()) { butret.Recover(); Destroy(); state=GAME_SHOWMENU; } roundtime+=1000/FPS; if(roundtime%1000<1000/FPS)//每秒储存一次 SaveArchive(); }
void Draw() { // init glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glEnable(GL_POINT_SMOOTH); glEnable(GL_LINE_SMOOTH); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // grid glColor3f(0, 1, 0); glLineWidth(.9f); for (int i = 0; showGrid && i < grid.res; i++) { float v = grid.cellSize*i; Line(0.f, v, (float) appSize, v); Line(v, 0.f, v, (float) appSize); } // circles glLineWidth(1.5f); for (size_t i = 0; !showCircles && i < circles.size(); i++) circles[i].Draw(); // statistics if (fps > 0) { char buf[1000]; sprintf(buf, "%s %5.2f fps (%i tests)", mode == Grid? "grid: " : "brute:", fps, displayCollisionCount); glColor3d(1, 1, 0); glRasterPos2i(20, appSize-20); glutBitmapString(GLUT_BITMAP_HELVETICA_18, (unsigned char *) buf); if (mode == Grid) { sprintf(buf, "%4.2f/cell, %i cells", displayCirclesPerCell, (int) displayCellsWithPairs); glRasterPos2i(25, appSize-35); glutBitmapString(GLUT_BITMAP_HELVETICA_12, (unsigned char *) buf); } } // buttons, sliders, finish modeButton.Draw(); blankScreenButton.Draw(); showGridButton.Draw(); gridResSlider.Draw(); nCirclesSlider.Draw(); glFlush(); }
void Game::ShowHelp()//显示帮助信息 { static FullScrAni helppage; static Button helpback(clientw*3/4-30,clienth*7/8,200,50); helppage.SetAni(ANI_HELP); helpback.SetAni(ANI_BUTTON,ANI_BUTTON_BACK); helpback.Deal(); helppage.Draw(); helpback.Draw(); if(helpback.MousePressed()) { helpback.Recover(); state=GAME_SHOWMENU; } }
void Game::ShowWin()//显示胜利信息 { static FullScrAni ani; static Button butcont(clientw/2-100,clienth*4/5); ani.SetAni(ANI_WIN); ani.Draw(); butcont.SetAni(ANI_BUTTON,ANI_BUTTON_CONTINUE); butcont.Deal(); butcont.Draw(); if(butcont.MousePressed()) { butcont.Recover(); state=GAME_RESTART; level++; ani.Ani()->Restart(); } }
int main() { Label label; label.SetText( "This is a label" ); Button button; button.SetDimensions( 10, 5 ); TextButton textButton; textButton.SetText( "Click Me" ); textButton.SetDimensions( 10, 5 ); cout << endl << endl << "LABEL" << endl; label.Draw(); cout << endl << endl << "BUTTON" << endl; button.Draw(); cout << endl << endl << "TEXT BUTTON" << endl; textButton.Draw(); return 0; }
void Game::ShowMenu()//显示菜单 { static bool firstrun=true; //创建界面元素 static FullScrAni menubk; static Button butnew(clientw/2,clienth/3+20); static Button butold(clientw/2,clienth/3+80); static Button butsel(clientw/2,clienth/3+140); static Button buthelp(clientw/2,clienth/3+200); static Button butquit(clientw/2,clienth/3+260); static SelBox sel(clientw/8+110,clienth*2/5+45,150,50); static Rect selbkground(clientw/8,clienth*2/5,clientw/3,clienth/3); static Button butok(clientw/8+20,clienth*2/5+120,100,50); static Button butcl(clientw/8+145,clienth*2/5+120,100,50); manager.Play(MID_MENUBKM); if(firstrun) { //设置动画 menubk.SetAni(ANI_MENUBK); butnew.SetAni(ANI_BUTTON,ANI_BUTTON_NEW); butold.SetAni(ANI_BUTTON,ANI_BUTTON_OLD); butsel.SetAni(ANI_BUTTON,ANI_BUTTON_SEL); buthelp.SetAni(ANI_BUTTON,ANI_BUTTON_HELP); butquit.SetAni(ANI_BUTTON,ANI_BUTTON_QUIT); butok.SetAni(ANI_BUTTON,ANI_BUTTON_OK); butcl.SetAni(ANI_BUTTON,ANI_BUTTON_CANCEL); sel.Add(1).Add(2).Add(3).Add(4).Add(5).Add(6).Add(7).Add(8).Add(9); sel.SetAni(-1,ANI_BUTTON,ANI_BUTTON,-1,ANI_BUTTON_LEFT,ANI_BUTTON_RIGHT); selbkground.SetAni(ANI_SELBKGROUND); } //界面元素的响应 butnew.Deal(); butold.Deal(); butsel.Deal(); buthelp.Deal(); butquit.Deal(); //界面元素的绘制 menubk.Draw(); butnew.Draw(); butold.Draw(); butsel.Draw(); buthelp.Draw(); butquit.Draw(); //检测界面元素的状态 if(butnew.MousePressed())//新的开始 { level=1; butnew.Recover(); state=GAME_CREATE; } if(butold.MousePressed())//旧的回忆 { ReadArchive(); butold.Recover(); state=GAME_RUN; } if(butsel.MousePressed())//选择关卡 { selbkground.Draw(); sel.Deal(); sel.Draw(); butok.Deal(); butcl.Deal(); butok.Draw(); butcl.Draw(); if(butok.MousePressed()) { butsel.Recover(); butok.Recover(); level=atoi(sel.str().c_str()); state=GAME_CREATE; } if(butcl.MousePressed()) { butcl.Recover(); butsel.Recover(); } } if(buthelp.MousePressed())//帮助 { buthelp.Recover(); state=GAME_SHOWHELP; } if(butquit.MousePressed()) { ::PostQuitMessage(0); } firstrun=false; }
int Game::ScenarioInfo(void) { Settings & conf = Settings::Get(); AGG::PlayMusic(MUS::MAINMENU); MapsFileInfoList lists; if(!PrepareMapsFileInfoList(lists, (conf.GameType(Game::TYPE_MULTI)))) { Dialog::Message(_("Warning"), _("No maps available!"), Font::BIG, Dialog::OK); return MAINMENU; } int result = QUITGAME; LocalEvent & le = LocalEvent::Get(); // cursor Cursor & cursor = Cursor::Get(); cursor.Hide(); cursor.SetThemes(cursor.POINTER); Display & display = Display::Get(); Point top, pointDifficultyInfo, pointOpponentInfo, pointClassInfo; Rect rectPanel; Button* buttonSelectMaps = NULL; Button* buttonOk = NULL; Button* buttonCancel = NULL; // vector coord difficulty Rects coordDifficulty; coordDifficulty.reserve(5); const Sprite & ngextra = AGG::GetICN(ICN::NGEXTRA, 62); Dialog::FrameBorder* frameborder = NULL; // image background if(conf.QVGA()) { frameborder = new Dialog::FrameBorder(Size(380, 224)); rectPanel = frameborder->GetArea(); pointDifficultyInfo = Point(rectPanel.x + 4, rectPanel.y + 24); pointOpponentInfo = Point(rectPanel.x + 4, rectPanel.y + 94); pointClassInfo = Point(rectPanel.x + 4, rectPanel.y + 148); coordDifficulty.push_back(Rect(rectPanel.x + 1, rectPanel.y + 21, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 78, rectPanel.y + 21, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 154, rectPanel.y + 21, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 231, rectPanel.y + 21, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 308, rectPanel.y + 21, ngextra.w(), ngextra.h())); buttonOk = new Button(rectPanel.x + rectPanel.w / 2 - 160, rectPanel.y + rectPanel.h - 30, ICN::NGEXTRA, 66, 67); buttonCancel = new Button(rectPanel.x + rectPanel.w / 2 + 60, rectPanel.y + rectPanel.h - 30, ICN::NGEXTRA, 68, 69); Text text; text.Set(conf.CurrentFileInfo().name, Font::BIG); text.Blit(rectPanel.x + (rectPanel.w - text.w()) / 2, rectPanel.y + 5); } else { const Sprite &panel = AGG::GetICN(ICN::NGHSBKG, 0); const Sprite &back = AGG::GetICN(ICN::HEROES, 0); const Point top((display.w() - back.w()) / 2, (display.h() - back.h()) / 2); rectPanel = Rect(top.x + 204, top.y + 32, panel.w(), panel.h()); pointDifficultyInfo = Point(rectPanel.x + 24, rectPanel.y + 93); pointOpponentInfo = Point(rectPanel.x + 24, rectPanel.y + 202); pointClassInfo = Point(rectPanel.x + 24, rectPanel.y + 282); coordDifficulty.push_back(Rect(rectPanel.x + 21, rectPanel.y + 91, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 98, rectPanel.y + 91, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 174, rectPanel.y + 91, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 251, rectPanel.y + 91, ngextra.w(), ngextra.h())); coordDifficulty.push_back(Rect(rectPanel.x + 328, rectPanel.y + 91, ngextra.w(), ngextra.h())); buttonSelectMaps = new Button(rectPanel.x + 309, rectPanel.y + 45, ICN::NGEXTRA, 64, 65); buttonOk = new Button(rectPanel.x + 31, rectPanel.y + 380, ICN::NGEXTRA, 66, 67); buttonCancel = new Button(rectPanel.x + 287, rectPanel.y + 380, ICN::NGEXTRA, 68, 69); back.Blit(top); } const bool reset_starting_settings = conf.MapsFile().empty() || ! System::IsFile(conf.MapsFile()); Players & players = conf.GetPlayers(); Interface::PlayersInfo playersInfo(true, !conf.QVGA(), !conf.QVGA()); // set first maps settings if(reset_starting_settings) conf.SetCurrentFileInfo(lists.front()); playersInfo.UpdateInfo(players, pointOpponentInfo, pointClassInfo); RedrawScenarioStaticInfo(rectPanel); RedrawDifficultyInfo(pointDifficultyInfo, !conf.QVGA()); playersInfo.RedrawInfo(); TextSprite* rating = conf.QVGA() ? NULL : new TextSprite(); if(rating) { rating->SetFont(Font::BIG); rating->SetPos(rectPanel.x + 166, rectPanel.y + 383); RedrawRatingInfo(*rating); } SpriteMove levelCursor(ngextra); switch(conf.GameDifficulty()) { case Difficulty::EASY: levelCursor.Move(coordDifficulty[0]); break; case Difficulty::NORMAL: levelCursor.Move(coordDifficulty[1]); break; case Difficulty::HARD: levelCursor.Move(coordDifficulty[2]); break; case Difficulty::EXPERT: levelCursor.Move(coordDifficulty[3]); break; case Difficulty::IMPOSSIBLE: levelCursor.Move(coordDifficulty[4]); break; } if(buttonSelectMaps) buttonSelectMaps->Draw(); buttonOk->Draw(); buttonCancel->Draw(); cursor.Show(); display.Flip(); while(le.HandleEvents()) { // press button if(buttonSelectMaps) le.MousePressLeft(*buttonSelectMaps) ? buttonSelectMaps->PressDraw() : buttonSelectMaps->ReleaseDraw(); le.MousePressLeft(*buttonOk) ? buttonOk->PressDraw() : buttonOk->ReleaseDraw(); le.MousePressLeft(*buttonCancel) ? buttonCancel->PressDraw() : buttonCancel->ReleaseDraw(); // click select if(buttonSelectMaps && (HotKeyPressEvent(Game::EVENT_BUTTON_SELECT) || le.MouseClickLeft(*buttonSelectMaps))) { levelCursor.Hide(); const Maps::FileInfo* fi = Dialog::SelectScenario(lists); if(fi) { conf.SetCurrentFileInfo(*fi); playersInfo.UpdateInfo(players, pointOpponentInfo, pointClassInfo); cursor.Hide(); RedrawScenarioStaticInfo(rectPanel); RedrawDifficultyInfo(pointDifficultyInfo, !conf.QVGA()); playersInfo.RedrawInfo(); if(rating) RedrawRatingInfo(*rating); // default difficulty normal levelCursor.Move(coordDifficulty[1]); conf.SetGameDifficulty(Difficulty::NORMAL); buttonOk->Draw(); buttonCancel->Draw(); } cursor.Show(); display.Flip(); } else // click cancel if(HotKeyPressEvent(EVENT_DEFAULT_EXIT) || le.MouseClickLeft(*buttonCancel)) { result = MAINMENU; break; } else // click ok if(HotKeyPressEvent(EVENT_DEFAULT_READY) || le.MouseClickLeft(*buttonOk)) { DEBUG(DBG_GAME, DBG_INFO, "select maps: " << conf.MapsFile() << \ ", difficulty: " << Difficulty::String(conf.GameDifficulty())); result = STARTGAME; break; } else if(le.MouseClickLeft(rectPanel)) { const s32 index = coordDifficulty.GetIndex(le.GetMouseCursor()); // select difficulty if(0 <= index) { cursor.Hide(); levelCursor.Move(coordDifficulty[index]); conf.SetGameDifficulty(index); if(rating) RedrawRatingInfo(*rating); cursor.Show(); display.Flip(); } else // playersInfo if(playersInfo.QueueEventProcessing()) { cursor.Hide(); RedrawScenarioStaticInfo(rectPanel); levelCursor.Redraw(); RedrawDifficultyInfo(pointDifficultyInfo, !conf.QVGA()); playersInfo.RedrawInfo(); if(rating) RedrawRatingInfo(*rating); buttonOk->Draw(); buttonCancel->Draw(); cursor.Show(); display.Flip(); } } if(le.MousePressRight(rectPanel)) { if(buttonSelectMaps && le.MousePressRight(*buttonSelectMaps)) Dialog::Message(_("Scenario"), _("Click here to select which scenario to play."), Font::BIG); else if(0 <= coordDifficulty.GetIndex(le.GetMouseCursor())) Dialog::Message(_("Game Difficulty"), _("This lets you change the starting difficulty at which you will play. Higher difficulty levels start you of with fewer resources, and at the higher settings, give extra resources to the computer."), Font::BIG); else if(rating && le.MousePressRight(rating->GetRect())) Dialog::Message(_("Difficulty Rating"), _("The difficulty rating reflects a combination of various settings for your game. This number will be applied to your final score."), Font::BIG); else if(le.MousePressRight(*buttonOk)) Dialog::Message(_("OK"), _("Click to accept these settings and start a new game."), Font::BIG); else if(le.MousePressRight(*buttonCancel)) Dialog::Message(_("Cancel"), _("Click to return to the main menu."), Font::BIG); else playersInfo.QueueEventProcessing(); } } cursor.Hide(); if(result == STARTGAME) { players.SetStartGame(); if(conf.ExtGameUseFade()) display.Fade(); Game::ShowLoadMapsText(); // Load maps std::string lower = StringLower(conf.MapsFile()); if(lower.size() > 3) { std::string ext = lower.substr(lower.size() - 3); if(ext == "mp2" || ext == "mx2") result = world.LoadMapMP2(conf.MapsFile()) ? STARTGAME : MAINMENU; else if(ext == "map") result = world.LoadMapMAP(conf.MapsFile()) ? STARTGAME : MAINMENU; } else { result = MAINMENU; DEBUG(DBG_GAME, DBG_WARN, conf.MapsFile() << ", " << "unknown map format"); } } if(frameborder) delete frameborder; if(rating) delete rating; if(buttonSelectMaps) delete buttonSelectMaps; delete buttonOk; delete buttonCancel; return result; }
void TradeWindowGUI::ShowTradeArea(u8 resourceFrom, u8 resourceTo, u32 max_buy, u32 max_sell, u32 count_buy, u32 count_sell, bool fromTradingPost) { Cursor &cursor = Cursor::Get(); Display &display = Display::Get(); if(resourceFrom == resourceTo || (Resource::GOLD != resourceTo && 0 == max_buy)) { cursor.Hide(); back.Restore(); Rect dst_rt(pos_rt.x, pos_rt.y + 30, pos_rt.w, 100); TextBox(_("You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?"), Font::BIG, dst_rt); buttonTrade.SetDisable(true); buttonLeft.SetDisable(true); buttonRight.SetDisable(true); cursor.Show(); display.Flip(); } else { cursor.Hide(); back.Restore(); Point dst_pt; const Sprite & bar = AGG::GetICN(tradpost, 1); dst_pt.x = pos_rt.x + (pos_rt.w - bar.w()) / 2 - 2; dst_pt.y = pos_rt.y + 128; display.Blit(bar, dst_pt); splitter.SetRange(0, (Resource::GOLD == resourceTo ? max_sell : max_buy)); Resource::resource_t rs_from = static_cast<Resource::resource_t>(resourceFrom); Resource::resource_t rs_to = static_cast<Resource::resource_t>(resourceTo); u16 exchange_rate = GetTradeCosts(resourceFrom, resourceTo, fromTradingPost); std::string message; if(Resource::GOLD == resourceTo) { message = _("I can offer you %{count} for 1 unit of %{resfrom}."); String::Replace(message, "%{count}", exchange_rate); String::Replace(message, "%{resfrom}", Resource::String(rs_from)); } else { message = _("I can offer you 1 unit of %{resto} for %{count} units of %{resfrom}."); String::Replace(message, "%{resto}", Resource::String(rs_to)); String::Replace(message, "%{resfrom}", Resource::String(rs_from)); String::Replace(message, "%{count}", exchange_rate); } TextBox(message, Font::BIG, Rect(pos_rt.x, pos_rt.y + 30, pos_rt.w, 100)); const Sprite & sprite_from = AGG::GetICN(ICN::RESOURCE, Resource::GetIndexSprite2(rs_from)); dst_pt.x = pos_rt.x + pos_rt.w / 2 - 70 - sprite_from.w() / 2; dst_pt.y = pos_rt.y + 115 - sprite_from.h(); display.Blit(sprite_from, dst_pt); message.clear(); String::AddInt(message, count_sell); const Sprite & sprite_to = AGG::GetICN(ICN::RESOURCE, Resource::GetIndexSprite2(rs_to)); dst_pt.x = pos_rt.x + pos_rt.w / 2 + 70 - sprite_to.w() / 2; dst_pt.y = pos_rt.y + 115 - sprite_to.h(); display.Blit(sprite_to, dst_pt); message.clear(); String::AddInt(message, count_buy); const Sprite & sprite_fromto = AGG::GetICN(tradpost, 0); dst_pt.x = pos_rt.x + pos_rt.w / 2 - sprite_fromto.w() / 2; dst_pt.y = pos_rt.y + 90; display.Blit(sprite_fromto, dst_pt); Text text(_("Qty to trade"), Font::SMALL); dst_pt.x = pos_rt.x + (pos_rt.w - text.w()) / 2; dst_pt.y = pos_rt.y + 110; text.Blit(dst_pt); buttonTrade.SetDisable(false); buttonLeft.SetDisable(false); buttonRight.SetDisable(false); buttonTrade.Draw(); buttonLeft.Draw(); buttonRight.Draw(); RedrawInfoBuySell(count_sell, count_buy); cursor.Show(); display.Flip(); } }