From 0ce6e9e8400c326e6848688b3b865c84d96e3073 Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Sun, 14 Aug 2005 21:11:54 +0000 Subject: Moved image drawing code into the graphics class. --- ChangeLog | 4 ++ src/graphics.cpp | 63 ++++++++++++++++++++++-- src/graphics.h | 17 ++++++- src/resources/image.cpp | 128 ++++++++---------------------------------------- src/resources/image.h | 47 ++---------------- 5 files changed, 103 insertions(+), 156 deletions(-) diff --git a/ChangeLog b/ChangeLog index dd98dd60..7b10b39b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2005-08-14 Björn Steinbrink + * src/graphics.cpp, src/graphics.h, src/resources/image.cpp, + src/resources/image.h: Moved the image drawing code into the graphics + class. + * src/being.cpp, src/engine.cpp, src/graphics.cpp, src/graphics.h: Made the wrapper functions in the Graphics class conditional on whether we compile with OpenGL support. diff --git a/src/graphics.cpp b/src/graphics.cpp index cd80fd3e..a0e5d921 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -149,9 +149,66 @@ int Graphics::getHeight() return mScreen->h; } -void Graphics::drawImage(Image *image, int x, int y) +bool Graphics::drawImage(Image *image, int x, int y) { - image->draw_deprecated(mScreen, x, y); + return drawImage(image, 0, 0, x, y, image->bounds.w, image->bounds.h); +} + +bool Graphics::drawImage(Image *image, int srcX, int srcY, int dstX, int dstY, + int width, int height) +{ + srcX += image->bounds.x; + srcY += image->bounds.y; + + if (!useOpenGL) { + // Check that preconditions for blitting are met. + if (!mScreen || !image->image) 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->image, &srcRect, mScreen, &dstRect) < 0) { + return false; + } + } +#ifdef USE_OPENGL + else { + // Find OpenGL texture coordinates + float texX1 = srcX / (float)image->texWidth; + float texY1 = srcY / (float)image->texHeight; + float texX2 = (srcX + width) / (float)image->texWidth; + float texY2 = (srcY + height) / (float)image->texHeight; + + glColor4f(1.0f, 1.0f, 1.0f, image->alpha); + glBindTexture(GL_TEXTURE_2D, image->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); + } +#endif + return true; } void Graphics::drawImagePattern(Image *image, int x, int y, int w, int h) @@ -167,7 +224,7 @@ void Graphics::drawImagePattern(Image *image, int x, int y, int w, int h) while (px < w) { int dw = (px + iw >= w) ? w - px : iw; int dh = (py + ih >= h) ? h - py : ih; - image->draw_deprecated(mScreen, 0, 0, x + px, y + py, dw, dh); + drawImage(image, 0, 0, x + px, y + py, dw, dh); px += iw; } py += ih; diff --git a/src/graphics.h b/src/graphics.h index ce4a281f..4b6bdd67 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -65,7 +65,22 @@ public gcn::SDLGraphics { */ bool setFullscreen(bool fs); - void drawImage(Image *image, int x, int y); + /** + * Blits an image onto the screen. + * + * @return true if the image was blitted properly + * false otherwise. + */ + bool drawImage(Image *image, int x, int y); + + /** + * Blits an image onto the screen. + * + * @return true if the image was blitted properly + * false otherwise. + */ + bool drawImage(Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height); + void drawImagePattern(Image *image, int x, int y, int w, int h); /** diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 9dafc64c..854c65e8 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -34,18 +34,26 @@ Image::Image(SDL_Surface *image): { // Default to opaque alpha = 1.0f; + + bounds.x = 0; + bounds.y = 0; + bounds.w = image->w; + bounds.h = image->h; } #ifdef USE_OPENGL Image::Image(GLuint glimage, int width, int height, int texWidth, int texHeight): glimage(glimage), - width(width), - height(height), texWidth(texWidth), texHeight(texHeight) { // Default to opaque alpha = 1.0f; + + bounds.x = 0; + bounds.y = 0; + bounds.w = width; + bounds.h = height; } #endif @@ -261,32 +269,12 @@ void Image::unload() int Image::getWidth() const { - if (!useOpenGL) { - if (image != NULL) { - return image->w; - } - } -#ifdef USE_OPENGL - else { - return width; - } -#endif - return 0; + return bounds.w; } int Image::getHeight() const { - if (!useOpenGL) { - if (image != NULL) { - return image->h; - } - } -#ifdef USE_OPENGL - else { - return height; - } -#endif - return 0; + return bounds.h; } Image *Image::getSubImage(int x, int y, int width, int height) @@ -304,65 +292,6 @@ Image *Image::getSubImage(int x, int y, int width, int height) #endif } -bool Image::draw_deprecated(SDL_Surface *screen, int srcX, int srcY, int dstX, int dstY, - int width, int height) -{ - 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); - } -#endif - return true; -} - -bool Image::draw_deprecated(SDL_Surface *screen, int x, int y) -{ - return draw_deprecated(screen, 0, 0, x, y, getWidth(), getHeight()); -} - void Image::setAlpha(float a) { alpha = a; @@ -389,10 +318,10 @@ SubImage::SubImage(Image *parent, SDL_Surface *image, parent->incRef(); // Set up the rectangle. - rect.x = x; - rect.y = y; - rect.w = width; - rect.h = height; + bounds.x = x; + bounds.y = y; + bounds.w = width; + bounds.h = height; } #ifdef USE_OPENGL @@ -403,10 +332,10 @@ SubImage::SubImage(Image *parent, GLuint image, parent->incRef(); // Set up the rectangle. - rect.x = x; - rect.y = y; - rect.w = width; - rect.h = height; + bounds.x = x; + bounds.y = y; + bounds.w = width; + bounds.h = height; } #endif @@ -418,24 +347,7 @@ SubImage::~SubImage() parent->decRef(); } -int SubImage::getWidth() const -{ - return rect.w; -} - -int SubImage::getHeight() const -{ - return rect.h; -} - Image *SubImage::getSubImage(int x, int y, int w, int h) { return NULL; } - -bool SubImage::draw_deprecated(SDL_Surface *screen, int srcX, int srcY, - int dstX, int dstY, int width, int height) -{ - return Image::draw_deprecated(screen, rect.x + srcX, rect.y + srcY, - dstX, dstY, width, height); -} diff --git a/src/resources/image.h b/src/resources/image.h index 7d1fe3c4..85f790ba 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -36,6 +36,8 @@ */ class Image : public Resource { + friend class Graphics; + public: /** * Destructor. @@ -82,27 +84,6 @@ class Image : public Resource virtual Image* getSubImage(int x, int y, int width, int height); - /** - * Blits the image onto the screen. - * - * @return true if the image was blitted properly - * false otherwise. - */ - virtual bool - draw_deprecated(SDL_Surface *screen, - int srcX, int srcY, - int dstX, int dstY, - int width, int height); - - /** - * Blits the image onto the screen. - * - * @return true if the image was blitted properly - * false otherwise. - */ - virtual bool - draw_deprecated(SDL_Surface *screen, int x, int y); - /** * Sets the alpha value of this image. */ @@ -125,11 +106,11 @@ class Image : public Resource #endif Image(SDL_Surface *image); + SDL_Rect bounds; bool loaded; #ifdef USE_OPENGL GLuint glimage; - int width, height; int texWidth, texHeight; #endif SDL_Surface *image; @@ -157,18 +138,6 @@ class SubImage : public Image */ ~SubImage(); - /** - * Returns the width of the image. - */ - int - getWidth() const; - - /** - * Returns the height of the image. - */ - int - getHeight() const; - /** * Creates a new image with the desired clipping rectangle. * @@ -178,18 +147,8 @@ class SubImage : public Image Image* getSubImage(int x, int y, int width, int height); - /** - * Draws this image. - */ - bool - draw_deprecated(SDL_Surface *screen, - int srcX, int srcY, - int dstX, int dstY, - int width, int height); - private: Image *parent; - SDL_Rect rect; }; #endif -- cgit v1.2.3-60-g2f50