From 7bbe721a23b9caba7a67ac806be10aa536f7f46d Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Wed, 10 Aug 2005 07:46:41 +0000 Subject: Moved knowledge about whether we use OpenGL into the Graphics class. Added helper function for being creation to reduce code duplication. Some code simplifications. --- ChangeLog | 10 +++++++ src/engine.cpp | 9 +----- src/game.cpp | 80 ++++++++++++++++++------------------------------- src/graphics.cpp | 36 ++++++++++++++++++++++ src/graphics.h | 5 ++++ src/gui/browserbox.cpp | 35 +++++----------------- src/gui/gui.cpp | 21 ++----------- src/gui/listbox.cpp | 12 ++------ src/gui/minimap.cpp | 11 +------ src/gui/progressbar.cpp | 10 +------ src/gui/scrollarea.cpp | 78 ++++++----------------------------------------- 11 files changed, 104 insertions(+), 203 deletions(-) diff --git a/ChangeLog b/ChangeLog index 995169ad..bbda0a0c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2005-08-10 Björn Steinbrink + + * src/engine.cpp, src/graphics.cpp, src/graphics.h, + src/gui/browserbox.cpp, src/gui/gui.cpp, src/gui/listbox.cpp, + src/gui/minimap.cpp, src/gui/progressbar.cpp, src/gui/scrollarea.cpp: + Moved knowledge about whether we use OpenGL into the Graphics class (as + much as possible) + * src/game.cpp: Added helper function for being creation to reduce code + duplication. Some code simplifications. + 2005-08-09 Björn Steinbrink * src/engine.cpp: Adjust drawing offset to have the player centered in diff --git a/src/engine.cpp b/src/engine.cpp index 7fe4d4df..dae8c8be 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -531,14 +531,7 @@ void Engine::draw() int squareX = node.x * 32 - map_x + 12; int squareY = node.y * 32 - map_y + 12; guiGraphics->setColor(gcn::Color(255, 0, 0)); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); - } + graphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); MetaTile *tile = mCurrentMap->getMetaTile(node.x, node.y); diff --git a/src/game.cpp b/src/game.cpp index 7e3e1bd2..210442f5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -141,6 +141,17 @@ int get_elapsed_time(int start_time) } } +Being* createBeing(unsigned int id, unsigned short job, Map *map) +{ + Being *being = new Being; + + being->setId(id); + being->job = job; + being->setMap(map); + + return being; +} + void do_init() { std::string path(map_path); @@ -165,14 +176,12 @@ void do_init() SDL_AddTimer(1000, nextSecond, NULL); // Seconds counter // Initialize beings - player_node = new Being(); - player_node->setId(account_ID); + player_node = createBeing(account_ID, 0, tiledMap); player_node->x = startX; player_node->y = startY; player_node->speed = 150; player_node->setHairColor(char_info->hair_color); player_node->setHairStyle(char_info->hair_style); - player_node->setMap(tiledMap); if (char_info->weapon == 11) { char_info->weapon = 2; @@ -384,14 +393,8 @@ void do_input() unsigned short y = player_node->y; int id = find_floor_item_by_cor(x, y); - if (id) - { - WFIFOW(0) = net_w_value(0x009f); - WFIFOL(2) = net_l_value(id); - WFIFOSET(6); - } - else - { + // If none below the player, try the tile in front of the player + if (!id) { switch (player_node->direction) { case NORTH: y--; break; @@ -404,6 +407,10 @@ void do_input() case SE: x++; y++; break; } id = find_floor_item_by_cor(x, y); + } + + if (id) + { WFIFOW(0) = net_w_value(0x009f); WFIFOL(2) = net_l_value(id); WFIFOSET(6); @@ -929,19 +936,14 @@ void do_parse() if (being == NULL) { - being = new Being(); - being->setId(beingId); + being = createBeing(beingId, beingJob, tiledMap); being->speed = RFIFOW(6); if (being->speed == 0) { // Else division by 0 when calculating frame being->speed = 150; } - being->job = beingJob; being->setHairStyle(RFIFOW(16)); being->setHairColor(RFIFOW(28)); - being->x = get_x(RFIFOP(46)); - being->y = get_y(RFIFOP(46)); - being->direction = get_direction(RFIFOP(46)); being->setWeapon(RFIFOW(18)); being->setMap(tiledMap); add_node(being); @@ -949,14 +951,14 @@ void do_parse() else { being->clearPath(); - being->x = get_x(RFIFOP(46)); - being->y = get_y(RFIFOP(46)); - being->direction = get_direction(RFIFOP(46)); //being->setWeapon(RFIFOW(18)); being->frame = 0; being->walk_time = tick_time; being->action = Being::STAND; } + being->x = get_x(RFIFOP(46)); + being->y = get_y(RFIFOP(46)); + being->direction = get_direction(RFIFOP(46)); break; case SMSG_REMOVE_BEING: @@ -997,10 +999,7 @@ void do_parse() if (being == NULL) { - being = new Being(); - being->setId(RFIFOL(2)); - being->job = RFIFOW(14); - being->setMap(tiledMap); + being = createBeing(RFIFOL(2), RFIFOW(14), tiledMap); add_node(being); } @@ -1028,10 +1027,7 @@ void do_parse() if (being == NULL) { - being = new Being(); - being->setId(RFIFOL(2)); - being->job = RFIFOW(14); - being->setMap(tiledMap); + being = createBeing(RFIFOL(2), RFIFOW(14), tiledMap); add_node(being); } @@ -1051,10 +1047,7 @@ void do_parse() if (being == NULL) { - being = new Being(); - being->setId(RFIFOL(2)); - being->job = RFIFOW(14); - being->setMap(tiledMap); + being = createBeing(RFIFOL(2), RFIFOW(14), tiledMap); add_node(being); } @@ -1178,17 +1171,8 @@ void do_parse() break; // Trade received Ok message case 0x00ec: - switch (RFIFOB(2)) - { - // Received ok from myself - case 0: - tradeWindow->receivedOk(true); - break; - // Received ok from the other - case 1: - tradeWindow->receivedOk(false); - break; - } + // 0 means ok from myself, 1 means ok from other; + tradeWindow->receivedOk((RFIFOB(2) == 0)); break; // Trade cancelled @@ -1589,14 +1573,8 @@ void do_parse() break; // Item disappearing case 0x00a1: - floorItem = find_floor_item_by_id(net_l_value(RFIFOL(2))); - if (floorItem != NULL) { - remove_floor_item(net_l_value(RFIFOL(2))); - } - else { - remove_floor_item(net_l_value(RFIFOL(2))); - } - break; + remove_floor_item(net_l_value(RFIFOL(2))); + break; // Next/Close button in NPC dialog case 0x00b5: case 0x00b6: diff --git a/src/graphics.cpp b/src/graphics.cpp index 0ee2371d..21797f55 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -192,6 +192,42 @@ void Graphics::setColor(gcn::Color color) #endif } +void Graphics::popClipArea() +{ + if (!useOpenGL) { + gcn::SDLGraphics::popClipArea(); + } +#ifdef USE_OPENGL + else { + gcn::OpenGLGraphics::popClipArea(); + } +#endif +} + +bool Graphics::pushClipArea(gcn::Rectangle area) +{ + if (!useOpenGL) { + return gcn::SDLGraphics::pushClipArea(area); + } +#ifdef USE_OPENGL + else { + return gcn::OpenGLGraphics::pushClipArea(area); + } +#endif +} + +void Graphics::fillRectangle(const gcn::Rectangle &rectangle) +{ + if (!useOpenGL) { + gcn::SDLGraphics::fillRectangle(rectangle); + } +#ifdef USE_OPENGL + else { + gcn::OpenGLGraphics::fillRectangle(rectangle); + } +#endif +} + int Graphics::getWidth() { return mScreen->w; diff --git a/src/graphics.h b/src/graphics.h index 1d616611..4e2b6c7e 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -135,6 +135,11 @@ public gcn::SDLGraphics { void setColor(gcn::Color color); + void popClipArea(); + bool pushClipArea(gcn::Rectangle area); + + void fillRectangle(const gcn::Rectangle &rectangle); + private: SDL_Surface *mScreen; bool mFullscreen, mHWAccel; diff --git a/src/gui/browserbox.cpp b/src/gui/browserbox.cpp index 52a13763..504fb987 100644 --- a/src/gui/browserbox.cpp +++ b/src/gui/browserbox.cpp @@ -211,14 +211,7 @@ void BrowserBox::draw(gcn::Graphics* graphics) if (mOpaque) { graphics->setColor(gcn::Color(BGCOLOR)); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); - } + graphics->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); } if (mSelectedLink >= 0) @@ -226,26 +219,12 @@ void BrowserBox::draw(gcn::Graphics* graphics) if ((mHighMode == BACKGROUND) || (mHighMode == BOTH)) { graphics->setColor(gcn::Color(HIGHLIGHT)); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle( - gcn::Rectangle( - mLinks[mSelectedLink].x1, - mLinks[mSelectedLink].y1, - mLinks[mSelectedLink].x2 - mLinks[mSelectedLink].x1, - mLinks[mSelectedLink].y2 - mLinks[mSelectedLink].y1 - )); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle( - gcn::Rectangle( - mLinks[mSelectedLink].x1, - mLinks[mSelectedLink].y1, - mLinks[mSelectedLink].x2 - mLinks[mSelectedLink].x1, - mLinks[mSelectedLink].y2 - mLinks[mSelectedLink].y1 - )); - } + graphics->fillRectangle(gcn::Rectangle( + mLinks[mSelectedLink].x1, + mLinks[mSelectedLink].y1, + mLinks[mSelectedLink].x2 - mLinks[mSelectedLink].x1, + mLinks[mSelectedLink].y2 - mLinks[mSelectedLink].y1 + )); } if ((mHighMode == UNDERLINE) || (mHighMode == BOTH)) diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 033ed3df..bd6a9ba4 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -181,16 +181,8 @@ void Gui::logic() void Gui::draw() { - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(guiGraphics)->pushClipArea(guiTop->getDimension()); - guiTop->draw((gcn::OpenGLGraphics*)guiGraphics); -#endif - } - else { - dynamic_cast(guiGraphics)->pushClipArea(guiTop->getDimension()); - guiTop->draw((gcn::SDLGraphics*)guiGraphics); - } + guiGraphics->pushClipArea(guiTop->getDimension()); + guiTop->draw(mGraphics); int mouseX, mouseY; Uint8 button = SDL_GetMouseState(&mouseX, &mouseY); @@ -201,14 +193,7 @@ void Gui::draw() guiGraphics->drawImage(mMouseCursor, mouseX - 5, mouseY - 2); } - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(guiGraphics)->popClipArea(); -#endif - } - else { - dynamic_cast(guiGraphics)->popClipArea(); - } + guiGraphics->popClipArea(); } void Gui::mousePress(int mx, int my, int button) diff --git a/src/gui/listbox.cpp b/src/gui/listbox.cpp index fee67cbd..0dd9203c 100644 --- a/src/gui/listbox.cpp +++ b/src/gui/listbox.cpp @@ -48,16 +48,8 @@ void ListBox::draw(gcn::Graphics *graphics) // Draw rectangle below the selected list element if (mSelected >= 0) { - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle( - gcn::Rectangle(0, fontHeight * mSelected, getWidth(), fontHeight)); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle( - gcn::Rectangle(0, fontHeight * mSelected, getWidth(), fontHeight)); - } + graphics->fillRectangle( + gcn::Rectangle(0, fontHeight * mSelected, getWidth(), fontHeight)); } // Draw the list elements diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index c6069821..b4ef3491 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -88,18 +88,9 @@ void Minimap::draw(gcn::Graphics *graphics) { // Player dot graphics->setColor(gcn::Color(209, 52, 61)); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle( - being->x / 2 + getPadding() - 1, - being->y / 2 + getTitleBarHeight() - 1, 3, 3)); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle( + graphics->fillRectangle(gcn::Rectangle( being->x / 2 + getPadding() - 1, being->y / 2 + getTitleBarHeight() - 1, 3, 3)); - } } else { diff --git a/src/gui/progressbar.cpp b/src/gui/progressbar.cpp index fb03d90b..505262b6 100644 --- a/src/gui/progressbar.cpp +++ b/src/gui/progressbar.cpp @@ -104,16 +104,8 @@ void ProgressBar::draw(gcn::Graphics *graphics) if (progress > 0) { graphics->setColor(gcn::Color(red, green, blue, 200)); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(4, 4, + graphics->fillRectangle(gcn::Rectangle(4, 4, (int)(progress * (getWidth() - 8)), getHeight() - 8)); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(4, 4, - (int)(progress * (getWidth() - 8)), getHeight() - 8)); - } } } diff --git a/src/gui/scrollarea.cpp b/src/gui/scrollarea.cpp index 3350a252..05fb49ce 100644 --- a/src/gui/scrollarea.cpp +++ b/src/gui/scrollarea.cpp @@ -179,33 +179,16 @@ void ScrollArea::draw(gcn::Graphics *graphics) if (mHBarVisible && mVBarVisible) { graphics->setColor(getBaseColor()); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth, + graphics->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth, getHeight() - mScrollbarWidth, mScrollbarWidth, mScrollbarWidth)); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth, - getHeight() - mScrollbarWidth, - mScrollbarWidth, - mScrollbarWidth)); - } } if (mContent) { gcn::Rectangle contdim = mContent->getDimension(); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->pushClipArea(getContentDimension()); -#endif - } - else { - dynamic_cast(graphics)->pushClipArea(getContentDimension()); - } + graphics->pushClipArea(getContentDimension()); if (mContent->getBorderSize() > 0) { @@ -214,44 +197,15 @@ void ScrollArea::draw(gcn::Graphics *graphics) rec.y -= mContent->getBorderSize(); rec.width += 2 * mContent->getBorderSize(); rec.height += 2 * mContent->getBorderSize(); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->pushClipArea(rec); -#endif - } - else { - dynamic_cast(graphics)->pushClipArea(rec); - } + graphics->pushClipArea(rec); mContent->drawBorder(graphics); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->popClipArea(); -#endif - } - else { - dynamic_cast(graphics)->popClipArea(); - } + graphics->popClipArea(); } - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->pushClipArea(contdim); -#endif - } - else { - dynamic_cast(graphics)->pushClipArea(contdim); - } + graphics->pushClipArea(contdim); mContent->draw(graphics); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->popClipArea(); - dynamic_cast(graphics)->popClipArea(); -#endif - } - else { - dynamic_cast(graphics)->popClipArea(); - dynamic_cast(graphics)->popClipArea(); - } + graphics->popClipArea(); + graphics->popClipArea(); } } @@ -341,14 +295,7 @@ void ScrollArea::drawVBar(gcn::Graphics *graphics) { gcn::Rectangle dim = getVerticalBarDimension(); graphics->setColor(gcn::Color(0, 0, 0, 32)); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle(dim); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle(dim); - } + graphics->fillRectangle(dim); graphics->setColor(gcn::Color(255, 255, 255)); } @@ -356,14 +303,7 @@ void ScrollArea::drawHBar(gcn::Graphics *graphics) { gcn::Rectangle dim = getHorizontalBarDimension(); graphics->setColor(gcn::Color(0, 0, 0, 32)); - if (useOpenGL) { -#ifdef USE_OPENGL - dynamic_cast(graphics)->fillRectangle(dim); -#endif - } - else { - dynamic_cast(graphics)->fillRectangle(dim); - } + graphics->fillRectangle(dim); graphics->setColor(gcn::Color(255, 255, 255)); } -- cgit v1.2.3-70-g09d2