From 9e4f25acd69489d949104eebec690ce5586849e6 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Tue, 26 Jul 2005 22:33:53 +0000 Subject: OpenGL/SDL combined in the same exe. Not in the cutest way, but we'll find a nicer approach after the release. --- src/engine.cpp | 8 +- src/game.cpp | 16 +-- src/graphics.cpp | 51 ++++++- src/graphics.h | 21 +-- src/gui/browserbox.cpp | 69 +++++---- src/gui/button.cpp | 4 +- src/gui/gui.cpp | 30 +++- src/gui/listbox.cpp | 12 +- src/gui/minimap.cpp | 10 +- src/gui/playerbox.cpp | 2 +- src/gui/progressbar.cpp | 11 +- src/gui/scrollarea.cpp | 69 +++++++-- src/gui/textfield.cpp | 2 +- src/gui/window.cpp | 2 +- src/main.cpp | 4 +- src/resources/image.cpp | 359 ++++++++++++++++++++++++---------------------- src/resources/image.h | 26 ++-- src/resources/mapreader.h | 26 ++-- 18 files changed, 448 insertions(+), 274 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 45a2c3e4..1d511e6a 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -29,6 +29,7 @@ #include "log.h" #include "being.h" #include "floor_item.h" +#include "graphics.h" #include "gui/gui.h" #include "gui/minimap.h" #include "gui/chargedialog.h" @@ -536,7 +537,12 @@ void Engine::draw() int squareX = (node.x - camera_x) * 32 - offset_x + 12; int squareY = (node.y - camera_y) * 32 - offset_y + 12; guiGraphics->setColor(gcn::Color(255, 0, 0)); - guiGraphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); + } + else { + dynamic_cast(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 df5033be..98be0bc6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -264,7 +264,7 @@ void do_input() // Get the state of the keyboard keys Uint8* keys; keys = SDL_GetKeyState(NULL); - + // Get the state of the joypad buttons // TODO: Only 6- buttons joypads are allowed bool joy[10]; @@ -619,7 +619,7 @@ void do_input() } } // End Mouse left button - + // Mouse button middle else if (event.button.button == SDL_BUTTON_MIDDLE) { @@ -628,7 +628,7 @@ void do_input() * right Usiu??? ;-) */ } - + // Mouse button right else if (event.button.button == SDL_BUTTON_RIGHT) { @@ -746,7 +746,7 @@ void do_input() player_node->direction = Direction; } } - + // Attacking monsters if (player_node->action == STAND) { @@ -763,12 +763,12 @@ void do_input() } } } - + if (joy[JOY_BTN1]) { unsigned short x = player_node->x; unsigned short y = player_node->y; int id = find_floor_item_by_cor(x, y); - + if (id != 0) { WFIFOW(0) = net_w_value(0x009f); @@ -1234,7 +1234,7 @@ void do_parse() logger->log("Warping to %s (%d, %d)", map_path, RFIFOW(18), RFIFOW(20)); strcpy(strrchr(map_path, '.') + 1, "tmx.gz"); - + Map *oldMap; oldMap = tiledMap; tiledMap = MapReader::readMap(map_path); @@ -1718,7 +1718,7 @@ void do_parse() break; case 0x0012: char_info->LUK = RFIFOB(5); - break; + break; } } break; diff --git a/src/graphics.cpp b/src/graphics.cpp index 2ebbc146..8fc60abb 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -46,19 +46,62 @@ Graphics::Graphics(SDL_Surface *screen): #endif } else { -#ifndef USE_OPENGL setTarget(mScreen); -#endif } // Initialize for drawing - _beginDraw(); + if (useOpenGL) { + gcn::OpenGLGraphics::_beginDraw() ; + } + else { + gcn::SDLGraphics::_beginDraw(); + } + //_beginDraw(); } Graphics::~Graphics() { // Deinitialize for drawing - _endDraw(); + if (useOpenGL) { + gcn::OpenGLGraphics::_endDraw() ; + } + else { + gcn::SDLGraphics::_endDraw(); + } + //_endDraw(); +} + +void Graphics::setFont(gcn::ImageFont *font) +{ + if (useOpenGL) { + gcn::OpenGLGraphics::setFont(font); + } + else { + gcn::SDLGraphics::setFont(font); + } +} + +void Graphics::drawText(const std::string &text, + int x, + int y, + unsigned int alignment) +{ + if (useOpenGL) { + gcn::OpenGLGraphics::drawText(text, x, y, alignment); + } + else { + gcn::SDLGraphics::drawText(text, x, y, alignment); + } +} + +void Graphics::setColor(gcn::Color color) +{ + if (useOpenGL) { + gcn::OpenGLGraphics::setColor(color); + } + else { + gcn::SDLGraphics::setColor(color); + } } int Graphics::getWidth() diff --git a/src/graphics.h b/src/graphics.h index 26b83692..a66bb2da 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -26,10 +26,10 @@ #include #include -#ifdef USE_OPENGL #include #include -#endif +#include +#include #include "resources/image.h" /** @@ -56,11 +56,7 @@ struct ImageRect { /** * A central point of control for graphics. */ -#ifdef USE_OPENGL -class Graphics : public gcn::OpenGLGraphics { -#else -class Graphics : public gcn::SDLGraphics { -#endif +class Graphics : public gcn::SDLGraphics, public gcn::OpenGLGraphics { public: /** * Constructor. @@ -104,7 +100,7 @@ class Graphics : public gcn::SDLGraphics { /** * Returns the width of the screen. */ - int getWidth(); + virtual int getWidth(); /** * Returns the height of the screen. @@ -118,6 +114,15 @@ class Graphics : public gcn::SDLGraphics { */ void setScreen(SDL_Surface *screen); + void setFont(gcn::ImageFont *font); + + void drawText(const std::string &text, + int x, + int y, + unsigned int alignment); + + void setColor(gcn::Color color); + private: SDL_Surface *mScreen; }; diff --git a/src/gui/browserbox.cpp b/src/gui/browserbox.cpp index c4d063ea..56539455 100644 --- a/src/gui/browserbox.cpp +++ b/src/gui/browserbox.cpp @@ -22,11 +22,10 @@ */ #include +#include "../graphics.h" #include "../main.h" #include "browserbox.h" -#ifndef USE_OPENGL #include "gui.h" -#endif int BrowserBox::instances = 0; gcn::ImageFont* BrowserBox::browserFont; @@ -44,14 +43,16 @@ BrowserBox::BrowserBox(unsigned int mode): if (instances == 0) { -#ifdef USE_OPENGL - browserFont = new gcn::ImageFont( - TMW_DATADIR "data/graphics/gui/browserfont.png", - " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567" - "89:@!\"$%&/=?^+*#[]{}()<>_;'.,\\|-~`"); -#else - browserFont = gui->getFont(); -#endif + if (useOpenGL) { + browserFont = new gcn::ImageFont( + TMW_DATADIR "data/graphics/gui/browserfont.png", + " abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567" + "89:@!\"$%&/=?^+*#[]{}()<>_;'.,\\|-~`"); + } + else { + browserFont = gui->getFont(); + } } instances++; @@ -84,7 +85,7 @@ void BrowserBox::setHighlightMode(unsigned int highMode) { mHighMode = highMode; } - + void BrowserBox::disableLinksAndUserColors() { mUseLinksAndUserColors = false; @@ -96,10 +97,10 @@ void BrowserBox::addRow(const std::string& row) std::string newRow; BROWSER_LINK bLink; int idx1, idx2, idx3; - + // Use links and user defined colors if (mUseLinksAndUserColors) - { + { // Check for links in format "@@link|Caption@@" idx1 = tmp.find("@@"); while (idx1 >= 0) @@ -137,7 +138,7 @@ void BrowserBox::addRow(const std::string& row) newRow += tmp; } - + // Don't use links and user defined colors else { @@ -153,7 +154,7 @@ void BrowserBox::addRow(const std::string& row) std::string plain = newRow; for (idx1 = plain.find("##"); idx1 >= 0; idx1 = plain.find("##")) plain.erase(idx1, 3); - + // Adjust the BrowserBox size int w = browserFont->getWidth(plain); if (w > getWidth()) @@ -207,20 +208,38 @@ void BrowserBox::draw(gcn::Graphics* graphics) { if (mOpaque) { - graphics->setColor(gcn::Color(BGCOLOR)); - graphics->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); + graphics->setColor(gcn::Color(BGCOLOR)); + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); + } + else { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); + } } if (mSelectedLink >= 0) { if ((mHighMode == BACKGROUND) || (mHighMode == BOTH)) { - graphics->setColor(gcn::Color(HIGHLIGHT)); - graphics->fillRectangle(gcn::Rectangle( - mLinks[mSelectedLink].x1, - mLinks[mSelectedLink].y1, - mLinks[mSelectedLink].x2 - mLinks[mSelectedLink].x1, - mLinks[mSelectedLink].y2 - mLinks[mSelectedLink].y1)); + graphics->setColor(gcn::Color(HIGHLIGHT)); + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle( + gcn::Rectangle( + mLinks[mSelectedLink].x1, + mLinks[mSelectedLink].y1, + mLinks[mSelectedLink].x2 - mLinks[mSelectedLink].x1, + mLinks[mSelectedLink].y2 - mLinks[mSelectedLink].y1 + )); + } + 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 + )); + } } if ((mHighMode == UNDERLINE) || (mHighMode == BOTH)) @@ -342,7 +361,7 @@ void BrowserBox::draw(gcn::Graphics* graphics) if (nextSpacePos <= 0) { nextSpacePos = row.size() - 1; - } + } int nextWordWidth = browserFont->getWidth( row.substr(nextChar, (nextSpacePos - nextChar))); @@ -355,7 +374,7 @@ void BrowserBox::draw(gcn::Graphics* graphics) j++; } } - + // Wrapping looong lines (brutal force) else if ((x + 2 * hyphenWidth) > getWidth()) { diff --git a/src/gui/button.cpp b/src/gui/button.cpp index 10fbe625..0fe207c7 100644 --- a/src/gui/button.cpp +++ b/src/gui/button.cpp @@ -105,7 +105,7 @@ void Button::draw(gcn::Graphics* graphics) int x, y; getAbsolutePosition(x, y); - ((Graphics*)graphics)->drawImageRect(x, y, getWidth(), getHeight(), + dynamic_cast(graphics)->drawImageRect(x, y, getWidth(), getHeight(), button[mode]); graphics->setColor(getForegroundColor()); @@ -127,7 +127,7 @@ void Button::draw(gcn::Graphics* graphics) throw GCN_EXCEPTION("Button::draw. Uknown alignment."); } - graphics->setFont(getFont()); + graphics->setFont((gcn::ImageFont*)getFont()); if (isPressed()) { graphics->drawText(getCaption(), textX + 1, textY + 1, getAlignment()); diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 3babd746..ca5c8133 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -56,7 +56,13 @@ Gui::Gui(Graphics *graphics): { // Set graphics guiGraphics = graphics; - setGraphics(graphics); + //setGraphics(graphics); + if (useOpenGL) { + setGraphics((gcn::OpenGLGraphics*)graphics); + } + else { + setGraphics((gcn::SDLGraphics*)graphics); + } // Set input guiInput = new gcn::SDLInput(); @@ -112,7 +118,7 @@ Gui::Gui(Graphics *graphics): logger->error("Unable to load sansserif8.png!"); } } - + // Set speech font try { speechFont = new gcn::ImageFont( @@ -180,7 +186,7 @@ Gui::~Gui() delete hitRedFont; delete hitBlueFont; delete hitYellowFont; - + if (mMouseCursor) { mMouseCursor->decRef(); } @@ -220,9 +226,14 @@ void Gui::logic() void Gui::draw() { - guiGraphics->pushClipArea(guiTop->getDimension()); - - guiTop->draw(guiGraphics); + if (useOpenGL) { + dynamic_cast(guiGraphics)->pushClipArea(guiTop->getDimension()); + guiTop->draw((gcn::OpenGLGraphics*)guiGraphics); + } + else { + dynamic_cast(guiGraphics)->pushClipArea(guiTop->getDimension()); + guiTop->draw((gcn::SDLGraphics*)guiGraphics); + } int mouseX, mouseY; Uint8 button = SDL_GetMouseState(&mouseX, &mouseY); @@ -233,7 +244,12 @@ void Gui::draw() guiGraphics->drawImage(mMouseCursor, mouseX - 5, mouseY - 2); } - guiGraphics->popClipArea(); + if (useOpenGL) { + dynamic_cast(guiGraphics)->popClipArea(); + } + else { + dynamic_cast(guiGraphics)->popClipArea(); + } } void Gui::mousePress(int mx, int my, int button) diff --git a/src/gui/listbox.cpp b/src/gui/listbox.cpp index b45823a6..bf80589a 100644 --- a/src/gui/listbox.cpp +++ b/src/gui/listbox.cpp @@ -22,6 +22,8 @@ */ #include "listbox.h" +#include "../graphics.h" +#include "../main.h" ListBox::ListBox(): gcn::ListBox() @@ -49,10 +51,16 @@ void ListBox::draw(gcn::Graphics *graphics) for (i = 0; i < mListModel->getNumberOfElements(); ++i) { if (i == mSelected) { - graphics->fillRectangle( + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle( gcn::Rectangle(0, y, getWidth(), fontHeight)); + } + else { + dynamic_cast(graphics)->fillRectangle( + gcn::Rectangle(0, y, getWidth(), fontHeight)); + } } - + graphics->drawText(mListModel->getElementAt(i), 1, y); y += fontHeight; diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index c733da42..59447fd7 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -23,6 +23,7 @@ #include "minimap.h" #include "../being.h" +#include "../main.h" #include "../resources/resourcemanager.h" Minimap::Minimap(): @@ -87,9 +88,16 @@ void Minimap::draw(gcn::Graphics *graphics) { // Player dot graphics->setColor(gcn::Color(209, 52, 61)); - graphics->fillRectangle(gcn::Rectangle( + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle( being->x / 2 + getPadding() - 1, being->y / 2 + getTitleBarHeight() - 1, 3, 3)); + } + else { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle( + being->x / 2 + getPadding() - 1, + being->y / 2 + getTitleBarHeight() - 1, 3, 3)); + } } else { diff --git a/src/gui/playerbox.cpp b/src/gui/playerbox.cpp index a94e7c4b..3b23f3f2 100644 --- a/src/gui/playerbox.cpp +++ b/src/gui/playerbox.cpp @@ -107,5 +107,5 @@ void PlayerBox::drawBorder(gcn::Graphics *graphics) x -= bs; y -= bs; - ((Graphics*)graphics)->drawImageRect(x, y, w, h, background); + dynamic_cast(graphics)->drawImageRect(x, y, w, h, background); } diff --git a/src/gui/progressbar.cpp b/src/gui/progressbar.cpp index e9e3f1b9..a1083db3 100644 --- a/src/gui/progressbar.cpp +++ b/src/gui/progressbar.cpp @@ -23,6 +23,7 @@ #include "progressbar.h" #include "../graphics.h" +#include "../main.h" #include "../resources/resourcemanager.h" ImageRect ProgressBar::mBorder; @@ -96,15 +97,21 @@ void ProgressBar::draw(gcn::Graphics *graphics) int x, y; getAbsolutePosition(x, y); - ((Graphics*)graphics)->drawImageRect(x, y, getWidth(), getHeight(), + dynamic_cast(graphics)->drawImageRect(x, y, getWidth(), getHeight(), mBorder); // The bar if (progress > 0) { graphics->setColor(gcn::Color(red, green, blue, 200)); - graphics->fillRectangle(gcn::Rectangle(4, 4, + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(4, 4, (int)(progress * (getWidth() - 8)), getHeight() - 8)); + } + 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 863f8a85..c8524657 100644 --- a/src/gui/scrollarea.cpp +++ b/src/gui/scrollarea.cpp @@ -22,6 +22,7 @@ */ #include "scrollarea.h" +#include "../main.h" #include "../resources/resourcemanager.h" int ScrollArea::instances = 0; @@ -178,16 +179,29 @@ void ScrollArea::draw(gcn::Graphics *graphics) if (mHBarVisible && mVBarVisible) { graphics->setColor(getBaseColor()); - graphics->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth, + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth, getHeight() - mScrollbarWidth, mScrollbarWidth, mScrollbarWidth)); + } + else { + dynamic_cast(graphics)->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth, + getHeight() - mScrollbarWidth, + mScrollbarWidth, + mScrollbarWidth)); + } } if (mContent) { gcn::Rectangle contdim = mContent->getDimension(); - graphics->pushClipArea(getContentDimension()); + if (useOpenGL) { + dynamic_cast(graphics)->pushClipArea(getContentDimension()); + } + else { + dynamic_cast(graphics)->pushClipArea(getContentDimension()); + } if (mContent->getBorderSize() > 0) { @@ -196,15 +210,36 @@ void ScrollArea::draw(gcn::Graphics *graphics) rec.y -= mContent->getBorderSize(); rec.width += 2 * mContent->getBorderSize(); rec.height += 2 * mContent->getBorderSize(); - graphics->pushClipArea(rec); + if (useOpenGL) { + dynamic_cast(graphics)->pushClipArea(rec); + } + else { + dynamic_cast(graphics)->pushClipArea(rec); + } mContent->drawBorder(graphics); - graphics->popClipArea(); + if (useOpenGL) { + dynamic_cast(graphics)->popClipArea(); + } + else { + dynamic_cast(graphics)->popClipArea(); + } } - graphics->pushClipArea(contdim); + if (useOpenGL) { + dynamic_cast(graphics)->pushClipArea(contdim); + } + else { + dynamic_cast(graphics)->pushClipArea(contdim); + } mContent->draw(graphics); - graphics->popClipArea(); - graphics->popClipArea(); + if (useOpenGL) { + dynamic_cast(graphics)->popClipArea(); + dynamic_cast(graphics)->popClipArea(); + } + else { + dynamic_cast(graphics)->popClipArea(); + dynamic_cast(graphics)->popClipArea(); + } } } @@ -219,7 +254,7 @@ void ScrollArea::drawBorder(gcn::Graphics *graphics) y -= bs; if (isOpaque()) { - ((Graphics*)graphics)->drawImageRect(x, y, w, h, background); + dynamic_cast(graphics)->drawImageRect(x, y, w, h, background); } } @@ -294,7 +329,12 @@ void ScrollArea::drawVBar(gcn::Graphics *graphics) { gcn::Rectangle dim = getVerticalBarDimension(); graphics->setColor(gcn::Color(0, 0, 0, 32)); - graphics->fillRectangle(dim); + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle(dim); + } + else { + dynamic_cast(graphics)->fillRectangle(dim); + } graphics->setColor(gcn::Color(255, 255, 255)); } @@ -302,7 +342,12 @@ void ScrollArea::drawHBar(gcn::Graphics *graphics) { gcn::Rectangle dim = getHorizontalBarDimension(); graphics->setColor(gcn::Color(0, 0, 0, 32)); - graphics->fillRectangle(dim); + if (useOpenGL) { + dynamic_cast(graphics)->fillRectangle(dim); + } + else { + dynamic_cast(graphics)->fillRectangle(dim); + } graphics->setColor(gcn::Color(255, 255, 255)); } @@ -312,7 +357,7 @@ void ScrollArea::drawVMarker(gcn::Graphics *graphics) gcn::Rectangle dim = getVerticalMarkerDimension(); getAbsolutePosition(x, y); - ((Graphics*)graphics)->drawImageRect( + dynamic_cast(graphics)->drawImageRect( x + dim.x, y + dim.y, dim.width, dim.height, vMarker); } @@ -322,6 +367,6 @@ void ScrollArea::drawHMarker(gcn::Graphics *graphics) gcn::Rectangle dim = getHorizontalMarkerDimension(); getAbsolutePosition(x, y); - ((Graphics*)graphics)->drawImageRect( + dynamic_cast(graphics)->drawImageRect( x + dim.x, y + dim.y, dim.width, dim.height, vMarker); } diff --git a/src/gui/textfield.cpp b/src/gui/textfield.cpp index c89b7ea4..9470fd54 100644 --- a/src/gui/textfield.cpp +++ b/src/gui/textfield.cpp @@ -100,5 +100,5 @@ void TextField::drawBorder(gcn::Graphics *graphics) x -= bs; y -= bs; - ((Graphics*)graphics)->drawImageRect(x, y, w, h, skin); + dynamic_cast(graphics)->drawImageRect(x, y, w, h, skin); } diff --git a/src/gui/window.cpp b/src/gui/window.cpp index 158e9272..0228e183 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -126,7 +126,7 @@ void Window::draw(gcn::Graphics* graphics) int x, y; getAbsolutePosition(x, y); - ((Graphics*)graphics)->drawImageRect(x, y, getWidth(), getHeight(), + dynamic_cast(graphics)->drawImageRect(x, y, getWidth(), getHeight(), border); // Draw title diff --git a/src/main.cpp b/src/main.cpp index ed6e9427..b1ef5661 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -150,7 +150,7 @@ void init_engine() } // Checking if /home/user/.tmw folder exists. - + homeDir = pass->pw_dir; homeDir += "/.tmw"; //sprintf(homeDir, "%s/.tmw", pass->pw_dir); @@ -219,7 +219,7 @@ void init_engine() SDL_WM_SetCaption("The Mana World", NULL); #ifdef USE_OPENGL - useOpenGL = true; + useOpenGL = (config.getValue("opengl", 0) == 1); #else useOpenGL = false; #endif diff --git a/src/resources/image.cpp b/src/resources/image.cpp index bb8d7c9a..eb550a3a 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -21,22 +21,25 @@ * $Id$ */ +#include "../main.h" #include "../log.h" #include "image.h" #include #include -#ifndef USE_OPENGL Image::Image(SDL_Surface *image): image(image) -#else -Image::Image(GLuint image, int width, int height, int texWidth, int texHeight): - image(image), +{ + // Default to opaque + alpha = 1.0f; +} + +Image::Image(GLuint glimage, int width, int height, int texWidth, int texHeight): + glimage(glimage), width(width), height(height), texWidth(texWidth), texHeight(texHeight) -#endif { // Default to opaque alpha = 1.0f; @@ -134,209 +137,214 @@ Image* Image::load(void* buffer, unsigned int bufferSize) SDL_SetAlpha(tmpImage, SDL_SRCALPHA | SDL_RLEACCEL, SDL_ALPHA_OPAQUE); } -#ifndef USE_OPENGL + if (!useOpenGL) { + // Set color key and alpha blending optins, and convert the surface to the + // current display format + SDL_Surface *prevImage = tmpImage; + if (hasAlpha) { + image = SDL_DisplayFormatAlpha(tmpImage); + } + else { + image = SDL_DisplayFormat(tmpImage); + } + SDL_FreeSurface(prevImage); - // Set color key and alpha blending optins, and convert the surface to the - // current display format - SDL_Surface *prevImage = tmpImage; - if (hasAlpha) { - image = SDL_DisplayFormatAlpha(tmpImage); - } - else { - image = SDL_DisplayFormat(tmpImage); - } - SDL_FreeSurface(prevImage); + if (image == NULL) { + logger->log("Error: Image convert failed."); + return NULL; + } - if (image == NULL) { - logger->log("Error: Image convert failed."); - return NULL; + return new Image(image); } +#ifdef USE_OPENGL + else { + int width = tmpImage->w; + int height = tmpImage->h; + int realWidth = 1, realHeight = 1; - return new Image(image); - -#else - - int width = tmpImage->w; - int height = tmpImage->h; - int realWidth = 1, realHeight = 1; - - while (realWidth < width && realWidth < 1024) { - realWidth *= 2; - } + while (realWidth < width && realWidth < 1024) { + realWidth *= 2; + } - while (realHeight < height && realHeight < 1024) { - realHeight *= 2; - } + while (realHeight < height && realHeight < 1024) { + realHeight *= 2; + } - SDL_SetAlpha(tmpImage, 0, SDL_ALPHA_OPAQUE); - SDL_Surface *oldImage = tmpImage; - tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, 32, - rmask, gmask, bmask, amask); + SDL_SetAlpha(tmpImage, 0, SDL_ALPHA_OPAQUE); + SDL_Surface *oldImage = tmpImage; + tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, 32, + rmask, gmask, bmask, amask); - if (tmpImage == NULL) { - logger->log("Error, image convert failed: out of memory"); - return NULL; - } + if (tmpImage == NULL) { + logger->log("Error, image convert failed: out of memory"); + return NULL; + } - SDL_BlitSurface(oldImage, NULL, tmpImage, NULL); - SDL_FreeSurface(oldImage); + SDL_BlitSurface(oldImage, NULL, tmpImage, NULL); + SDL_FreeSurface(oldImage); - GLuint texture; - glGenTextures(1, &texture); - logger->log("Binding texture %d (%dx%d)", - texture, tmpImage->w, tmpImage->h); - glBindTexture(GL_TEXTURE_2D, texture); + GLuint texture; + glGenTextures(1, &texture); + logger->log("Binding texture %d (%dx%d)", + texture, tmpImage->w, tmpImage->h); + glBindTexture(GL_TEXTURE_2D, texture); - if (SDL_MUSTLOCK(tmpImage)) { - SDL_LockSurface(tmpImage); - } + if (SDL_MUSTLOCK(tmpImage)) { + SDL_LockSurface(tmpImage); + } - glTexImage2D( - GL_TEXTURE_2D, 0, 4, - tmpImage->w, tmpImage->h, - 0, GL_RGBA, GL_UNSIGNED_BYTE, - tmpImage->pixels); + glTexImage2D( + GL_TEXTURE_2D, 0, 4, + tmpImage->w, tmpImage->h, + 0, GL_RGBA, GL_UNSIGNED_BYTE, + tmpImage->pixels); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - if (SDL_MUSTLOCK(tmpImage)) { - SDL_UnlockSurface(tmpImage); - } + if (SDL_MUSTLOCK(tmpImage)) { + SDL_UnlockSurface(tmpImage); + } - SDL_FreeSurface(tmpImage); + SDL_FreeSurface(tmpImage); - GLenum error = glGetError(); - if (error) - { - std::string errmsg = "Unkown error"; - switch (error) + GLenum error = glGetError(); + if (error) { - case GL_INVALID_ENUM: - errmsg = "GL_INVALID_ENUM"; - break; - case GL_INVALID_VALUE: - errmsg = "GL_INVALID_VALUE"; - break; - case GL_INVALID_OPERATION: - errmsg = "GL_INVALID_OPERATION"; - break; - case GL_STACK_OVERFLOW: - errmsg = "GL_STACK_OVERFLOW"; - break; - case GL_STACK_UNDERFLOW: - errmsg = "GL_STACK_UNDERFLOW"; - break; - case GL_OUT_OF_MEMORY: - errmsg = "GL_OUT_OF_MEMORY"; - break; + std::string errmsg = "Unkown error"; + switch (error) + { + case GL_INVALID_ENUM: + errmsg = "GL_INVALID_ENUM"; + break; + case GL_INVALID_VALUE: + errmsg = "GL_INVALID_VALUE"; + break; + case GL_INVALID_OPERATION: + errmsg = "GL_INVALID_OPERATION"; + break; + case GL_STACK_OVERFLOW: + errmsg = "GL_STACK_OVERFLOW"; + break; + case GL_STACK_UNDERFLOW: + errmsg = "GL_STACK_UNDERFLOW"; + break; + case GL_OUT_OF_MEMORY: + errmsg = "GL_OUT_OF_MEMORY"; + break; + } + logger->log("Error: Image GL import failed: %s", errmsg.c_str()); + return NULL; } - logger->log("Error: Image GL import failed: %s", errmsg.c_str()); - return NULL; - } - return new Image(texture, width, height, realWidth, realHeight); + return new Image(texture, width, height, realWidth, realHeight); + } #endif + + return NULL; } void Image::unload() { // Free the image surface. -#ifndef USE_OPENGL - if (image != NULL) { - SDL_FreeSurface(image); - image = NULL; - loaded = false; + if (!useOpenGL) { + if (image != NULL) { + SDL_FreeSurface(image); + image = NULL; + loaded = false; + } } -#endif loaded = false; } int Image::getWidth() const { -#ifndef USE_OPENGL - if (image != NULL) { - return image->w; + if (!useOpenGL) { + if (image != NULL) { + return image->w; + } + } + else { + return width; } -#else - return width; -#endif return 0; } int Image::getHeight() const { -#ifndef USE_OPENGL - if (image != NULL) { - return image->h; + if (!useOpenGL) { + if (image != NULL) { + return image->h; + } + } + else { + return height; } -#else - return height; -#endif return 0; } Image *Image::getSubImage(int x, int y, int width, int height) { // Create a new clipped sub-image -#ifdef USE_OPENGL - return new SubImage(this, image, x, y, width, height, texWidth, texHeight); -#else - return new SubImage(this, image, x, y, width, height); -#endif + if (useOpenGL) { + return new SubImage(this, glimage, x, y, width, height, texWidth, texHeight); + } + else { + return new SubImage(this, image, x, y, width, height); + } } bool Image::draw_deprecated(SDL_Surface *screen, int srcX, int srcY, int dstX, int dstY, int width, int height) { -#ifndef USE_OPENGL - // Check that preconditions for blitting are met. - if (screen == NULL || image == NULL) return false; - - SDL_Rect dstRect; - SDL_Rect srcRect; - dstRect.x = dstX; dstRect.y = dstY; - srcRect.x = srcX; srcRect.y = srcY; - srcRect.w = width; - srcRect.h = height; - - if (SDL_BlitSurface(image, &srcRect, screen, &dstRect) < 0) { - return false; + if (!useOpenGL) { + // Check that preconditions for blitting are met. + if (screen == NULL || image == NULL) return false; + + SDL_Rect dstRect; + SDL_Rect srcRect; + dstRect.x = dstX; dstRect.y = dstY; + srcRect.x = srcX; srcRect.y = srcY; + srcRect.w = width; + srcRect.h = height; + + if (SDL_BlitSurface(image, &srcRect, screen, &dstRect) < 0) { + return false; + } + } +#ifdef USE_OPENGL + else { + // Find OpenGL texture coordinates + float texX1 = srcX / (float)texWidth; + float texY1 = srcY / (float)texHeight; + float texX2 = (srcX + width) / (float)texWidth; + float texY2 = (srcY + height) / (float)texHeight; + + glColor4f(1.0f, 1.0f, 1.0f, alpha); + glBindTexture(GL_TEXTURE_2D, glimage); + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + + // Draw a textured quad -- the image + glBegin(GL_QUADS); + glTexCoord2f(texX1, texY1); + glVertex3i(dstX, dstY, 0); + + glTexCoord2f(texX2, texY1); + glVertex3i(dstX + width, dstY, 0); + + glTexCoord2f(texX2, texY2); + glVertex3i(dstX + width, dstY + height, 0); + + glTexCoord2f(texX1, texY2); + glVertex3i(dstX, dstY + height, 0); + glEnd(); + + glDisable(GL_TEXTURE_2D); + glDisable(GL_BLEND); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } - -#else - - // Find OpenGL texture coordinates - float texX1 = srcX / (float)texWidth; - float texY1 = srcY / (float)texHeight; - float texX2 = (srcX + width) / (float)texWidth; - float texY2 = (srcY + height) / (float)texHeight; - - glColor4f(1.0f, 1.0f, 1.0f, alpha); - glBindTexture(GL_TEXTURE_2D, image); - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - - // Draw a textured quad -- the image - glBegin(GL_QUADS); - glTexCoord2f(texX1, texY1); - glVertex3i(dstX, dstY, 0); - - glTexCoord2f(texX2, texY1); - glVertex3i(dstX + width, dstY, 0); - - glTexCoord2f(texX2, texY2); - glVertex3i(dstX + width, dstY + height, 0); - - glTexCoord2f(texX1, texY2); - glVertex3i(dstX, dstY + height, 0); - glEnd(); - - glDisable(GL_TEXTURE_2D); - glDisable(GL_BLEND); - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - #endif return true; } @@ -350,10 +358,10 @@ void Image::setAlpha(float a) { alpha = a; -#ifndef USE_OPENGL - // Set the alpha value this image is drawn at - SDL_SetAlpha(image, SDL_SRCALPHA | SDL_RLEACCEL, (int)(255 * alpha)); -#endif + if (!useOpenGL) { + // Set the alpha value this image is drawn at + SDL_SetAlpha(image, SDL_SRCALPHA | SDL_RLEACCEL, (int)(255 * alpha)); + } } float Image::getAlpha() @@ -365,16 +373,23 @@ float Image::getAlpha() // SubImage Class //============================================================================ -#ifndef USE_OPENGL SubImage::SubImage(Image *parent, SDL_Surface *image, int x, int y, int width, int height): - Image(image), -#else + Image(image), parent(parent) +{ + parent->incRef(); + + // Set up the rectangle. + rect.x = x; + rect.y = y; + rect.w = width; + rect.h = height; +} + +//SubImage::SubImage((GLuint*)Image *parent, GLuint glimage, SubImage::SubImage(Image *parent, GLuint image, int x, int y, int width, int height, int texWidth, int texHeight): - Image(image, width, height, texWidth, texHeight), -#endif - parent(parent) + Image(image, width, height, texWidth, texHeight), parent(parent) { parent->incRef(); @@ -387,9 +402,9 @@ SubImage::SubImage(Image *parent, GLuint image, SubImage::~SubImage() { -#ifndef USE_OPENGL - image = NULL; -#endif + if (!useOpenGL) { + image = NULL; + } parent->decRef(); } diff --git a/src/resources/image.h b/src/resources/image.h index 9151a869..cfa42330 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -26,9 +26,7 @@ #include "resource.h" #include -#ifdef USE_OPENGL #include -#endif /** @@ -120,23 +118,21 @@ class Image : public Resource /** * Constructor. */ -#ifdef USE_OPENGL - Image(GLuint image, - int width, int height, - int texWidth, int texHeight); -#else +//#ifdef USE_OPENGL + Image(GLuint glimage, int width, int height, int texWidth, int texHeight); +//#else Image(SDL_Surface *image); -#endif +//#endif bool loaded; -#ifdef USE_OPENGL - GLuint image; +//#ifdef USE_OPENGL + GLuint glimage; int width, height; int texWidth, texHeight; -#else +//#else SDL_Surface *image; -#endif +//#endif float alpha; }; @@ -149,13 +145,13 @@ class SubImage : public Image /** * Constructor. */ -#ifndef USE_OPENGL +//#ifndef USE_OPENGL SubImage(Image *parent, SDL_Surface *image, int x, int y, int width, int height); -#else +//#else SubImage(Image *parent, GLuint image, int x, int y, int width, int height, int texWidth, int textHeight); -#endif +//#endif /** * Destructor. diff --git a/src/resources/mapreader.h b/src/resources/mapreader.h index d0faac6d..be38afeb 100644 --- a/src/resources/mapreader.h +++ b/src/resources/mapreader.h @@ -57,48 +57,54 @@ class MapReader /** * Read an XML map from a file. */ - static Map *readMap(const std::string &filename); + static Map* + readMap(const std::string &filename); /** * Read an XML map from a parsed XML tree. The path is used to find the * location of referenced tileset images. */ - static Map *readMap(xmlNodePtr node, const std::string &path); + static Map* + readMap(xmlNodePtr node, const std::string &path); private: /** * Reads a map layer. */ - static void readLayer(xmlNodePtr node, Map *map, int layer); + static void + readLayer(xmlNodePtr node, Map *map, int layer); /** * Reads a tile set. */ - static Tileset *readTileset(xmlNodePtr node, const std::string &path, - Map *map); + static Tileset* + readTileset(xmlNodePtr node, const std::string &path, Map *map); /** * Gets an integer property from an xmlNodePtr. */ - static int getProperty(xmlNodePtr node, const char* name, int def); + static int + getProperty(xmlNodePtr node, const char* name, int def); /** * Converts a global tile id to the Image* pointing to the associated * tile image. */ - static Image *getTileWithGid(int gid); + static Image* + getTileWithGid(int gid); /** * Finds the tile set that a tile with the given global id is part of. */ - static Tileset *MapReader::getTilesetWithGid(int gid); + static Tileset* + getTilesetWithGid(int gid); /** * Sets a tile using a global tile id. Used by the layer loading * routine. */ - static void MapReader::setTileWithGid( - Map *map, int x, int y, int layer, int gid); + static void + setTileWithGid(Map *map, int x, int y, int layer, int gid); static std::vector tilesets; }; -- cgit v1.2.3-70-g09d2