diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-08-31 20:38:42 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-08-31 20:38:42 +0300 |
commit | b310c51796d1632aeefc834dc0e931c52f909a41 (patch) | |
tree | 1332b627664b8450cc3fdbbd2995ce0ffc67b724 /src/render | |
parent | 07c2392f2a7a7fa90d1c0dfe97dbd3bf29fdfad1 (diff) | |
download | manaverse-b310c51796d1632aeefc834dc0e931c52f909a41.tar.gz manaverse-b310c51796d1632aeefc834dc0e931c52f909a41.tar.bz2 manaverse-b310c51796d1632aeefc834dc0e931c52f909a41.tar.xz manaverse-b310c51796d1632aeefc834dc0e931c52f909a41.zip |
move render classes into render dir.
Diffstat (limited to 'src/render')
-rw-r--r-- | src/render/graphics.cpp | 586 | ||||
-rw-r--r-- | src/render/graphics.h | 427 | ||||
-rw-r--r-- | src/render/mobileopenglgraphics.cpp | 1288 | ||||
-rw-r--r-- | src/render/mobileopenglgraphics.h | 232 | ||||
-rw-r--r-- | src/render/normalopenglgraphics.cpp | 1515 | ||||
-rw-r--r-- | src/render/normalopenglgraphics.h | 244 | ||||
-rw-r--r-- | src/render/nullopenglgraphics.cpp | 1208 | ||||
-rw-r--r-- | src/render/nullopenglgraphics.h | 239 | ||||
-rw-r--r-- | src/render/safeopenglgraphics.cpp | 654 | ||||
-rw-r--r-- | src/render/safeopenglgraphics.h | 183 | ||||
-rw-r--r-- | src/render/sdl2graphics.cpp | 641 | ||||
-rw-r--r-- | src/render/sdl2graphics.h | 143 | ||||
-rw-r--r-- | src/render/sdlgraphics.cpp | 1272 | ||||
-rw-r--r-- | src/render/sdlgraphics.h | 155 | ||||
-rw-r--r-- | src/render/surfacegraphics.cpp | 87 | ||||
-rw-r--r-- | src/render/surfacegraphics.h | 185 |
16 files changed, 9059 insertions, 0 deletions
diff --git a/src/render/graphics.cpp b/src/render/graphics.cpp new file mode 100644 index 000000000..42de3429d --- /dev/null +++ b/src/render/graphics.cpp @@ -0,0 +1,586 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "render/graphics.h" + +#include "main.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/imagehelper.h" +#include "resources/openglimagehelper.h" + +#include <guichan/sdl/sdlpixel.hpp> + +#ifdef USE_OPENGL +#ifdef __APPLE__ +#include <OpenGL/OpenGL.h> +#endif +#endif + +#include "debug.h" + +#ifdef USE_OPENGL +#ifndef GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX +#define GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX 0x9049 +#endif +#endif + +Graphics::Graphics() : + gcn::Graphics(), + mWidth(0), + mHeight(0), + mWindow(nullptr), +#ifdef USE_SDL2 + mRenderer(nullptr), +#ifdef USE_OPENGL + mGLContext(nullptr), +#endif +#endif + mBpp(0), + mAlpha(false), + mFullscreen(false), + mHWAccel(false), + mRedraw(false), + mDoubleBuffer(false), + mRect(), + mSecure(false), + mOpenGL(0), + mEnableResize(false), + mNoFrame(false), + mName("Software"), + mStartFreeMem(0), + mSync(false), + mColor(), + mColor2() +{ + mRect.x = 0; + mRect.y = 0; + mRect.w = 0; + mRect.h = 0; +} + +Graphics::~Graphics() +{ + _endDraw(); +#ifdef USE_SDL2 + if (mRenderer) + { + SDL_DestroyRenderer(mRenderer); + mRenderer = nullptr; + } +#ifdef USE_OPENGL + if (mGLContext) + { + SDL_GL_DeleteContext(mGLContext); + mGLContext = 0; + } +#endif +#endif +} + +void Graphics::setSync(const bool sync) +{ + mSync = sync; +} + +void Graphics::setMainFlags(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) +{ + logger->log("graphics backend: %s", getName().c_str()); + logger->log("Setting video mode %dx%d %s", + w, h, fs ? "fullscreen" : "windowed"); + + mWidth = w; + mHeight = h; + mBpp = bpp; + mFullscreen = fs; + mHWAccel = hwaccel; + mEnableResize = resize; + mNoFrame = noFrame; +} + +int Graphics::getOpenGLFlags() const +{ +#ifdef USE_OPENGL + +#ifdef USE_SDL2 + int displayFlags = SDL_WINDOW_OPENGL; + if (mFullscreen) + displayFlags |= SDL_WINDOW_FULLSCREEN; +#else + int displayFlags = SDL_ANYFORMAT | SDL_OPENGL; +#endif // USE_SDL2 + + if (mFullscreen) + { + displayFlags |= SDL_FULLSCREEN; + } + else + { + // Resizing currently not supported on Windows, where it would require + // reuploading all textures. +#if !defined(_WIN32) + if (mEnableResize) + displayFlags |= SDL_RESIZABLE; +#endif + } + + if (mNoFrame) + displayFlags |= SDL_NOFRAME; + + return displayFlags; +#else + return 0; +#endif // USE_OPENGL +} + +bool Graphics::setOpenGLMode() +{ +#ifdef USE_OPENGL + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + if (!(mWindow = graphicsManager.createWindow(mWidth, mHeight, + mBpp, getOpenGLFlags()))) + { + mRect.w = 0; + mRect.h = 0; + return false; + } + +#ifdef USE_SDL2 + int w1 = 0; + int h1 = 0; + SDL_GetWindowSize(mWindow, &w1, &h1); + mRect.w = w1; + mRect.h = h1; + + mGLContext = SDL_GL_CreateContext(mWindow); + +#else // USE_SDL2 + + mRect.w = static_cast<uint16_t>(mWindow->w); + mRect.h = static_cast<uint16_t>(mWindow->h); +#endif // USE_SDL2 + +#ifdef __APPLE__ + if (mSync) + { + const GLint VBL = 1; + CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &VBL); + } +#endif + + graphicsManager.setGLVersion(); + graphicsManager.logVersion(); + + // Setup OpenGL + glViewport(0, 0, mWidth, mHeight); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); + int gotDoubleBuffer = 0; + SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &gotDoubleBuffer); + logger->log("Using OpenGL %s double buffering.", + (gotDoubleBuffer ? "with" : "without")); + + graphicsManager.initOpenGL(); + initArrays(); + graphicsManager.updateTextureFormat(); + updateMemoryInfo(); + + GLint texSize; + bool rectTex = graphicsManager.supportExtension( + "GL_ARB_texture_rectangle"); + if (rectTex && OpenGLImageHelper::getInternalTextureType() == 4 + && getOpenGL() != 3 && config.getBoolValue("rectangulartextures") + && !graphicsManager.isUseTextureSampler()) + { + logger->log1("using GL_ARB_texture_rectangle"); + OpenGLImageHelper::mTextureType = GL_TEXTURE_RECTANGLE_ARB; + glEnable(GL_TEXTURE_RECTANGLE_ARB); + glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &texSize); + OpenGLImageHelper::mTextureSize = texSize; + logger->log("OpenGL texture size: %d pixels (rectangle textures)", + OpenGLImageHelper::mTextureSize); + } + else + { + OpenGLImageHelper::mTextureType = GL_TEXTURE_2D; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize); + OpenGLImageHelper::mTextureSize = texSize; + logger->log("OpenGL texture size: %d pixels", + OpenGLImageHelper::mTextureSize); + } + return videoInfo(); +#else // USE_OPENGL + + return false; +#endif // USE_OPENGL +} + +int Graphics::getSoftwareFlags() const +{ +#ifdef USE_SDL2 + int displayFlags = SDL_WINDOW_SHOWN; +#else + int displayFlags = SDL_ANYFORMAT; + + if (mHWAccel) + displayFlags |= SDL_HWSURFACE | SDL_DOUBLEBUF; + else + displayFlags |= SDL_SWSURFACE; +#endif + + if (mFullscreen) + displayFlags |= SDL_FULLSCREEN; + else if (mEnableResize) + displayFlags |= SDL_RESIZABLE; + + if (mNoFrame) + displayFlags |= SDL_NOFRAME; + return displayFlags; +} + + +void Graphics::updateMemoryInfo() +{ +#ifdef USE_OPENGL + if (mStartFreeMem) + return; + + if (graphicsManager.supportExtension("GL_NVX_gpu_memory_info")) + { + glGetIntegerv(GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, + &mStartFreeMem); + logger->log("free video memory: %d", mStartFreeMem); + } +#endif +} + +int Graphics::getMemoryUsage() const +{ +#ifdef USE_OPENGL + if (!mStartFreeMem) + return 0; + + if (graphicsManager.supportExtension("GL_NVX_gpu_memory_info")) + { + GLint val; + glGetIntegerv(GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, + &val); + return mStartFreeMem - val; + } +#endif + return 0; +} + +#ifdef USE_SDL2 +void Graphics::dumpRendererInfo(const char *const str, + const SDL_RendererInfo &info) +{ + logger->log(str, info.name); + logger->log("flags:"); + if (info.flags & SDL_RENDERER_SOFTWARE) + logger->log(" software"); + if (info.flags & SDL_RENDERER_ACCELERATED) + logger->log(" accelerated"); + if (info.flags & SDL_RENDERER_PRESENTVSYNC) + logger->log(" vsync"); + if (info.flags & SDL_RENDERER_TARGETTEXTURE) + logger->log(" texture target"); +} +#endif + +bool Graphics::videoInfo() +{ + logger->log("SDL video info"); +#ifdef USE_SDL2 + logger->log("Using video driver: %s", SDL_GetCurrentVideoDriver()); + + if (mRenderer) + { + SDL_RendererInfo info; + SDL_GetRendererInfo(mRenderer, &info); + dumpRendererInfo("Current SDL renderer name: %s", info); + + const int num = SDL_GetNumRenderDrivers(); + logger->log("Known renderers"); + for (int f = 0; f < num; f ++) + { + if (!SDL_GetRenderDriverInfo(f, &info)) + dumpRendererInfo("renderer name: %s", info); + } + } +#else + char videoDriverName[65]; + if (SDL_VideoDriverName(videoDriverName, 64)) + logger->log("Using video driver: %s", videoDriverName); + else + logger->log1("Using video driver: unknown"); + mDoubleBuffer = ((mWindow->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF); + logger->log("Double buffer mode: %s", mDoubleBuffer ? "yes" : "no"); + + imageHelper->dumpSurfaceFormat(mWindow); + + const SDL_VideoInfo *const vi = SDL_GetVideoInfo(); + if (!vi) + return false; + + logger->log("Possible to create hardware surfaces: %s", + ((vi->hw_available) ? "yes" : "no")); + logger->log("Window manager available: %s", + ((vi->wm_available) ? "yes" : "no")); + logger->log("Accelerated hardware to hardware blits: %s", + ((vi->blit_hw) ? "yes" : "no")); + logger->log("Accelerated hardware to hardware colorkey blits: %s", + ((vi->blit_hw_CC) ? "yes" : "no")); + logger->log("Accelerated hardware to hardware alpha blits: %s", + ((vi->blit_hw_A) ? "yes" : "no")); + logger->log("Accelerated software to hardware blits: %s", + ((vi->blit_sw) ? "yes" : "no")); + logger->log("Accelerated software to hardware colorkey blits: %s", + ((vi->blit_sw_CC) ? "yes" : "no")); + logger->log("Accelerated software to hardware alpha blits: %s", + ((vi->blit_sw_A) ? "yes" : "no")); + logger->log("Accelerated color fills: %s", + ((vi->blit_fill) ? "yes" : "no")); +#endif + + return true; +} + +bool Graphics::setFullscreen(const bool fs) +{ + if (mFullscreen == fs) + return true; + + return setVideoMode(mWidth, mHeight, mBpp, fs, mHWAccel, + mEnableResize, mNoFrame); +} + +bool Graphics::resizeScreen(const int width, const int height) +{ + if (mWidth == width && mHeight == height) + return true; + +#ifdef USE_SDL2 + _endDraw(); + + mRect.w = width; + mRect.h = height; + mWidth = width; + mHeight = height; + +#ifdef USE_OPENGL + // +++ probably this way will not work in windows/mac + // Setup OpenGL + glViewport(0, 0, mWidth, mHeight); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); +#else + // +++ need impliment resize in soft mode +#endif // USE_OPENGL + + _beginDraw(); + return true; + +#else + const int prevWidth = mWidth; + const int prevHeight = mHeight; + + _endDraw(); + + const bool success = setVideoMode(width, height, mBpp, + mFullscreen, mHWAccel, mEnableResize, mNoFrame); + + // If it didn't work, try to restore the previous size. If that didn't + // work either, bail out (but then we're in deep trouble). + if (!success) + { + if (!setVideoMode(prevWidth, prevHeight, mBpp, + mFullscreen, mHWAccel, mEnableResize, mNoFrame)) + { + return false; + } + } + + _beginDraw(); + + return success; +#endif // USE_SDL2 +} + +int Graphics::getWidth() const +{ + return mWidth; +} + +int Graphics::getHeight() const +{ + return mHeight; +} + +bool Graphics::drawImage(const Image *image, int x, int y) +{ + if (image) + { + return drawImage2(image, 0, 0, x, y, + image->mBounds.w, image->mBounds.h, false); + } + else + { + return false; + } +} + +void Graphics::drawImageRect(const int x, const int y, + const int w, const int h, + const Image *const topLeft, + const Image *const topRight, + const Image *const bottomLeft, + const Image *const bottomRight, + const Image *const top, + const Image *const right, + const Image *const bottom, + const Image *const left, + const Image *const center) +{ + BLOCK_START("Graphics::drawImageRect") + const bool drawMain = center && topLeft && topRight + && bottomLeft && bottomRight; + + // Draw the center area + if (center && drawMain) + { + const int tlw = topLeft->getWidth(); + const int tlh = topLeft->getHeight(); + drawImagePattern(center, tlw + x, tlh + y, + w - tlw - topRight->getWidth(), + h - tlh - bottomLeft->getHeight()); + } + + // Draw the sides + if (top && left && bottom && right) + { + const int lw = left->getWidth(); + const int rw = right->getWidth(); + const int th = top->getHeight(); + const int bh = bottom->getHeight(); + drawImagePattern(top, x + lw, y, w - lw - rw, th); + drawImagePattern(bottom, x + lw, h - bh + y, w - lw - rw, bh); + drawImagePattern(left, x, y + th, lw, h - th - bh); + drawImagePattern(right, x + w - rw, th + y, rw, h - th - bh); + } + // Draw the corners + if (drawMain) + { + drawImage(topLeft, x, y); + drawImage(topRight, x + w - topRight->getWidth(), y); + drawImage(bottomLeft, x, h - bottomLeft->getHeight() + y); + drawImage(bottomRight, + x + w - bottomRight->getWidth(), + y + h - bottomRight->getHeight()); + } + BLOCK_END("Graphics::drawImageRect") +} + +void Graphics::drawImageRect(int x, int y, int w, int h, + const ImageRect &imgRect) +{ + drawImageRect(x, y, w, h, + imgRect.grid[0], imgRect.grid[2], imgRect.grid[6], imgRect.grid[8], + imgRect.grid[1], imgRect.grid[5], imgRect.grid[7], imgRect.grid[3], + imgRect.grid[4]); +} + +bool Graphics::drawNet(const int x1, const int y1, const int x2, const int y2, + const int width, const int height) +{ + for (int y = y1; y < y2; y += height) + drawLine(x1, y, x2, y); + + for (int x = x1; x < x2; x += width) + drawLine(x, y1, x, y2); + + return true; +} + +bool Graphics::calcImageRect(ImageVertexes *const vert, + const int x, const int y, + const int w, const int h, + const Image *const topLeft, + const Image *const topRight, + const Image *const bottomLeft, + const Image *const bottomRight, + const Image *const top, + const Image *const right, + const Image *const bottom, + const Image *const left, + const Image *const center) +{ + if (!vert) + return false; + + BLOCK_START("Graphics::calcImageRect") + const bool drawMain = center && topLeft && topRight + && bottomLeft && bottomRight; + +// pushClipArea(gcn::Rectangle(x, y, w, h)); + + // Draw the center area + if (center && drawMain) + { + const int tlw = topLeft->getWidth(); + const int tlh = topLeft->getHeight(); + calcImagePattern(vert, center, tlw + x, tlh + y, + w - tlw - topRight->getWidth(), + h - tlh - bottomLeft->getHeight()); + } + // Draw the sides + if (top && left && bottom && right) + { + const int lw = left->getWidth(); + const int rw = right->getWidth(); + const int th = top->getHeight(); + const int bh = bottom->getHeight(); + calcImagePattern(vert, top, x + lw, y, w - lw - rw, th); + calcImagePattern(vert, bottom, x + lw, y + h - bh, w - lw - rw, bh); + calcImagePattern(vert, left, x, y + th, lw, h - th - bh); + calcImagePattern(vert, right, x + w - rw, y + th, rw, h - th - bh); + } + + calcTile(vert, topLeft, x, y); + if (topRight) + calcTile(vert, topRight, x + w - topRight->getWidth(), y); + if (bottomLeft) + calcTile(vert, bottomLeft, x, y + h - bottomLeft->getHeight()); + if (bottomRight) + { + calcTile(vert, bottomRight, x + w - bottomRight->getWidth(), + y + h - bottomRight->getHeight()); + } + +// popClipArea(); + BLOCK_END("Graphics::calcImageRect") + return 0; +} diff --git a/src/render/graphics.h b/src/render/graphics.h new file mode 100644 index 000000000..7dafcbc45 --- /dev/null +++ b/src/render/graphics.h @@ -0,0 +1,427 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#include "SDL.h" + +#include "sdlshared.h" + +#include <guichan/color.hpp> +#include <guichan/graphics.hpp> + +#include "localconsts.h" + +class Image; +class ImageCollection; +class ImageVertexes; +class MapLayer; + +struct SDL_Surface; +struct SDL_Window; + +static const int defaultScreenWidth = 800; +static const int defaultScreenHeight = 600; + +/** + * 9 images defining a rectangle. 4 corners, 4 sides and a middle area. The + * topology is as follows: + * + * <pre> + * !-----!-----------------!-----! + * ! 0 ! 1 ! 2 ! + * !-----!-----------------!-----! + * ! 3 ! 4 ! 5 ! + * !-----!-----------------!-----! + * ! 6 ! 7 ! 8 ! + * !-----!-----------------!-----! + * </pre> + * + * Sections 0, 2, 6 and 8 will remain as is. 1, 3, 4, 5 and 7 will be + * repeated to fit the size of the widget. + */ +class ImageRect final +{ + public: + ImageRect() + { + for (int f = 0; f < 9; f ++) + grid[f] = nullptr; + } + + A_DELETE_COPY(ImageRect) + + enum ImagePosition + { + UPPER_LEFT = 0, + UPPER_CENTER = 1, + UPPER_RIGHT = 2, + LEFT = 3, + CENTER = 4, + RIGHT = 5, + LOWER_LEFT = 6, + LOWER_CENTER = 7, + LOWER_RIGHT = 8 + }; + + Image *grid[9]; +}; + +/** + * A central point of control for graphics. + */ +class Graphics : public gcn::Graphics +{ + public: + /** + * Constructor. + */ + Graphics(); + + A_DELETE_COPY(Graphics) + + /** + * Destructor. + */ + virtual ~Graphics(); + + void setWindow(SDL_Window *const window, + const int width, const int height) + { + mWindow = window; + mRect.w = width; + mRect.h = height; + } + + SDL_Window *getWindow() const + { return mWindow; } + + /** + * Sets whether vertical refresh syncing is enabled. Takes effect after + * the next call to setVideoMode(). Only implemented on MacOS for now. + */ + void setSync(const bool sync); + + bool getSync() const A_WARN_UNUSED + { return mSync; } + + /** + * Try to create a window with the given settings. + */ + virtual bool setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) = 0; + + /** + * Set fullscreen mode. + */ + bool setFullscreen(const bool fs); + + /** + * Resize the window to the specified size. + */ + bool resizeScreen(const int width, const int height); + + /** + * Blits an image onto the screen. + * + * @return <code>true</code> if the image was blitted properly + * <code>false</code> otherwise. + */ + bool drawImage(const Image *image, int x, int y); + + // override unused abstract function + void drawImage(const gcn::Image* image A_UNUSED, + int srcX A_UNUSED, int srcY A_UNUSED, + int dstX A_UNUSED, int dstY A_UNUSED, + int width A_UNUSED, int height A_UNUSED) override + { + } + + /** + * Draws a resclaled version of the image + */ + virtual bool drawRescaledImage(const Image *const image, int srcX, + int srcY, int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor = false) = 0; + + virtual void drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) = 0; + + /** + * Draw a pattern based on a rescaled version of the given image... + */ + virtual void drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) = 0; + + /** + * Draws a rectangle using images. 4 corner images, 4 side images and 1 + * image for the inside. + */ + void drawImageRect(const int x, const int y, const int w, const int h, + const Image *const topLeft, + const Image *const topRight, + const Image *const bottomLeft, + const Image *const bottomRight, + const Image *const top, + const Image *const right, + const Image *const bottom, + const Image *const left, + const Image *const center); + + /** + * Draws a rectangle using images. 4 corner images, 4 side images and 1 + * image for the inside. + */ + void drawImageRect(int x, int y, int w, int h, + const ImageRect &imgRect); + + bool calcImageRect(ImageVertexes *const vert, + const int x, const int y, + const int w, const int h, + const Image *const topLeft, + const Image *const topRight, + const Image *const bottomLeft, + const Image *const bottomRight, + const Image *const top, + const Image *const right, + const Image *const bottom, + const Image *const left, + const Image *const center); + + virtual void calcImagePattern(ImageVertexes *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const = 0; + + virtual void calcImagePattern(ImageCollection *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const = 0; + + virtual void calcTile(ImageVertexes *const vert, + const Image *const image, + int x, int y) const = 0; + + virtual void calcTileSDL(ImageVertexes *const vert A_UNUSED, + int x A_UNUSED, int y A_UNUSED) const + { + } + + virtual void drawTile(const ImageVertexes *const vert) = 0; + + virtual void drawTile(const ImageCollection *const vertCol) = 0; + + virtual void calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y) = 0; + + virtual bool calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) = 0; + + /** + * Draws a rectangle using images. 4 corner images, 4 side images and 1 + * image for the inside. + */ + inline void drawImageRect(const gcn::Rectangle &area, + const ImageRect &imgRect) + { drawImageRect(area.x, area.y, area.width, area.height, imgRect); } + + virtual void fillRectangle(const gcn::Rectangle& rectangle) + override = 0; + + /** + * Updates the screen. This is done by either copying the buffer to the + * screen or swapping pages. + */ + virtual void updateScreen() = 0; + + /** + * Returns the width of the screen. + */ + int getWidth() const A_WARN_UNUSED; + + /** + * Returns the height of the screen. + */ + int getHeight() const A_WARN_UNUSED; + + /** + * Takes a screenshot and returns it as SDL surface. + */ + virtual SDL_Surface *getScreenshot() A_WARN_UNUSED = 0; + + virtual void prepareScreenshot() + { } + + int getMemoryUsage() const A_WARN_UNUSED; + + virtual bool drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height); + + const gcn::Font *getFont() const A_WARN_UNUSED + { return mFont; } + + gcn::ClipRectangle &getTopClip() A_WARN_UNUSED + { return mClipStack.top(); } + + void setRedraw(const bool n) + { mRedraw = n; } + + bool getRedraw() const A_WARN_UNUSED + { return mRedraw; } + + void setSecure(const bool n) + { mSecure = n; } + + bool getSecure() const A_WARN_UNUSED + { return mSecure; } + + int getBpp() const A_WARN_UNUSED + { return mBpp; } + + bool getFullScreen() const A_WARN_UNUSED + { return mFullscreen; } + + bool getHWAccel() const A_WARN_UNUSED + { return mHWAccel; } + + bool getDoubleBuffer() const A_WARN_UNUSED + { return mDoubleBuffer; } + + int getOpenGL() const A_WARN_UNUSED + { return mOpenGL; } + + void setNoFrame(const bool n) + { mNoFrame = n; } + + const std::string &getName() const A_WARN_UNUSED + { return mName; } + + virtual void initArrays() + { } + + void setColor(const gcn::Color &color) + { + mColor = color; + mColor2 = color; + mAlpha = (color.a != 255); + } + + void setColor2(const gcn::Color &color) + { mColor2 = color; } + + void setColorAll(const gcn::Color &color, const gcn::Color &color2) + { + mColor = color; + mColor2 = color2; + mAlpha = (color.a != 255); + } + + const gcn::Color &getColor() const override + { return mColor; } + + const gcn::Color &getColor2() const + { return mColor2; } + +#ifdef DEBUG_DRAW_CALLS + virtual unsigned int getDrawCalls() const + { return 0; } +#endif +#ifdef DEBUG_BIND_TEXTURE + virtual unsigned int getBinds() const + { return 0; } +#endif +#ifdef USE_SDL2 + void dumpRendererInfo(const char *const str, + const SDL_RendererInfo &info); +#endif + int mWidth; + int mHeight; + + protected: + /** + * Blits an image onto the screen. + * + * @return <code>true</code> if the image was blitted properly + * <code>false</code> otherwise. + */ + virtual bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) = 0; + + + void setMainFlags(int w, int h, int bpp, bool fs, + bool hwaccel, bool resize, bool noFrame); + + int getOpenGLFlags() const A_WARN_UNUSED; + + int getSoftwareFlags() const A_WARN_UNUSED; + + bool setOpenGLMode(); + + void updateMemoryInfo(); + + bool videoInfo(); + + SDL_Window *mWindow; + +#ifdef USE_SDL2 + SDL_Renderer *mRenderer; +#ifdef USE_OPENGL + SDL_GLContext mGLContext; +#endif +#endif + int mBpp; + bool mAlpha; + bool mFullscreen; + bool mHWAccel; + bool mRedraw; + bool mDoubleBuffer; + SDL_Rect mRect; + bool mSecure; + int mOpenGL; + bool mEnableResize; + bool mNoFrame; + std::string mName; + int mStartFreeMem; + bool mSync; + gcn::Color mColor; + gcn::Color mColor2; +}; + +extern Graphics *mainGraphics; + +#endif // GRAPHICS_H diff --git a/src/render/mobileopenglgraphics.cpp b/src/render/mobileopenglgraphics.cpp new file mode 100644 index 000000000..054588645 --- /dev/null +++ b/src/render/mobileopenglgraphics.cpp @@ -0,0 +1,1288 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "main.h" + +#ifdef USE_OPENGL + +#include "render/mobileopenglgraphics.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/image.h" +#include "resources/openglimagehelper.h" + +#include "utils/sdlcheckutils.h" +#include "utils/stringutils.h" + +#include <SDL.h> + +#include "debug.h" + +GLuint MobileOpenGLGraphics::mLastImage = 0; +#ifdef DEBUG_DRAW_CALLS +unsigned int MobileOpenGLGraphics::mDrawCalls = 0; +unsigned int MobileOpenGLGraphics::mLastDrawCalls = 0; +#endif + +MobileOpenGLGraphics::MobileOpenGLGraphics(): + mFloatTexArray(nullptr), + mIntTexArray(nullptr), + mIntVertArray(nullptr), + mShortVertArray(nullptr), + mTexture(false), + mIsByteColor(false), + mByteColor(), + mFloatColor(1.0f), + mMaxVertices(500), + mColorAlpha(false), +#ifdef DEBUG_BIND_TEXTURE + mOldTexture(), + mOldTextureId(0), +#endif + mFbo() +{ + mOpenGL = 3; + mName = "mobile OpenGL"; +} + +MobileOpenGLGraphics::~MobileOpenGLGraphics() +{ + delete [] mFloatTexArray; + delete [] mIntTexArray; + delete [] mIntVertArray; + delete [] mShortVertArray; +} + +void MobileOpenGLGraphics::initArrays() +{ + mMaxVertices = graphicsManager.getMaxVertices(); + if (mMaxVertices < 500) + mMaxVertices = 500; + else if (mMaxVertices > 1024) + mMaxVertices = 1024; + + // need alocate small size, after if limit reached reallocate to double size + const int sz = mMaxVertices * 4 + 30; + vertexBufSize = mMaxVertices; + mFloatTexArray = new GLfloat[sz]; + mIntTexArray = new GLint[sz]; + mIntVertArray = new GLint[sz]; + mShortVertArray = new GLshort[sz]; +} + +bool MobileOpenGLGraphics::setVideoMode(const int w, const int h, + const int bpp, const bool fs, + const bool hwaccel, const bool resize, + const bool noFrame) +{ + setMainFlags(w, h, bpp, fs, hwaccel, resize, noFrame); + + return setOpenGLMode(); +} + +static inline void drawQuad(const Image *const image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height) +{ +// if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + // Find OpenGL normalized texture coordinates. + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + const float texX2 = static_cast<float>(srcX + width) / tw; + const float texY2 = static_cast<float>(srcY + height) / th; + + GLfloat tex[] = + { + texX1, texY1, + texX2, texY1, + texX1, texY2, + texX2, texY2 + }; + + GLshort vert[] = + { + static_cast<GLshort>(dstX), static_cast<GLshort>(dstY), + static_cast<GLshort>(dstX + width), static_cast<GLshort>(dstY), + static_cast<GLshort>(dstX), static_cast<GLshort>(dstY + height), + static_cast<GLshort>(dstX + width), + static_cast<GLshort>(dstY + height) + }; + + glVertexPointer(2, GL_SHORT, 0, &vert); + glTexCoordPointer(2, GL_FLOAT, 0, &tex); + +#ifdef DEBUG_DRAW_CALLS + MobileOpenGLGraphics::mDrawCalls ++; +#endif + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + } +} + +static inline void drawRescaledQuad(const Image *const image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight) +{ +// if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + // Find OpenGL normalized texture coordinates. + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + const float texX2 = static_cast<float>(srcX + width) / tw; + const float texY2 = static_cast<float>(srcY + height) / th; + + GLfloat tex[] = + { + texX1, texY1, + texX2, texY1, + texX1, texY2, + texX2, texY2 + }; + + GLshort vert[] = + { + static_cast<GLshort>(dstX), static_cast<GLshort>(dstY), + static_cast<GLshort>(dstX + desiredWidth), + static_cast<GLshort>(dstY), + static_cast<GLshort>(dstX), static_cast<GLshort>( + dstY + desiredHeight), + static_cast<GLshort>(dstX + desiredWidth), + static_cast<GLshort>(dstY + desiredHeight) + }; + glVertexPointer(2, GL_SHORT, 0, &vert); + glTexCoordPointer(2, GL_FLOAT, 0, &tex); + +#ifdef DEBUG_DRAW_CALLS + MobileOpenGLGraphics::mDrawCalls ++; +#endif + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + } +} + + +bool MobileOpenGLGraphics::drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) +{ + FUNC_BLOCK("Graphics::drawImage2", 1) + if (!image) + return false; + + const SDL_Rect &imageRect = image->mBounds; + + if (!useColor) + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + drawQuad(image, srcX + imageRect.x, srcY + imageRect.y, + dstX, dstY, width, height); + + return true; +} + +bool MobileOpenGLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor) +{ + return drawRescaledImage(image, srcX, srcY, + dstX, dstY, + width, height, + desiredWidth, desiredHeight, + useColor, true); +} + +bool MobileOpenGLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor, + bool smooth) +{ + FUNC_BLOCK("Graphics::drawRescaledImage", 1) + if (!image) + return false; + + // Just draw the image normally when no resizing is necessary, + if (width == desiredWidth && height == desiredHeight) + { + return drawImage2(image, srcX, srcY, dstX, dstY, + width, height, useColor); + } + + // When the desired image is smaller than the current one, + // disable smooth effect. + if (width > desiredWidth && height > desiredHeight) + smooth = false; + + const SDL_Rect &imageRect = image->mBounds; + srcX += imageRect.x; + srcY += imageRect.y; + + if (!useColor) + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + // Draw a textured quad. + drawRescaledQuad(image, srcX, srcY, dstX, dstY, width, height, + desiredWidth, desiredHeight); + + if (smooth) // A basic smooth effect... + { + setColorAlpha(0.2f); + drawRescaledQuad(image, srcX, srcY, dstX - 1, dstY - 1, width, height, + desiredWidth + 1, desiredHeight + 1); + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY + 1, width, height, + desiredWidth - 1, desiredHeight - 1); + + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY, width, height, + desiredWidth - 1, desiredHeight); + drawRescaledQuad(image, srcX, srcY, dstX, dstY + 1, width, height, + desiredWidth, desiredHeight - 1); + } + + return true; +} + +void MobileOpenGLGraphics::drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) +{ + FUNC_BLOCK("Graphics::drawImagePattern", 1) + if (!image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const int iw = imageRect.w; + const int ih = imageRect.h; + + if (iw == 0 || ih == 0) + return; + + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + // Draw a set of textured rectangles +// if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) +// { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const float texY2 = static_cast<float>(srcY + height) / th; + const int dstY = y + py; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + + const float texX2 = static_cast<float>(srcX + width) / tw; + + mFloatTexArray[vp + 0] = texX1; // 1 + mFloatTexArray[vp + 1] = texY1; + + mFloatTexArray[vp + 2] = texX2; // 2 + mFloatTexArray[vp + 3] = texY1; + + mFloatTexArray[vp + 4] = texX2; // 3 + mFloatTexArray[vp + 5] = texY2; + + mFloatTexArray[vp + 6] = texX1; // 1 + mFloatTexArray[vp + 7] = texY1; + + mFloatTexArray[vp + 8] = texX1; // 4 + mFloatTexArray[vp + 9] = texY2; + + mFloatTexArray[vp + 10] = texX2; // 3 + mFloatTexArray[vp + 11] = texY2; + + mShortVertArray[vp + 0] = static_cast<GLshort>(dstX); + mShortVertArray[vp + 1] = static_cast<GLshort>(dstY); + + mShortVertArray[vp + 2] = static_cast<GLshort>(dstX + width); + mShortVertArray[vp + 3] = static_cast<GLshort>(dstY); + + mShortVertArray[vp + 4] = static_cast<GLshort>(dstX + width); + mShortVertArray[vp + 5] = static_cast<GLshort>(dstY + height); + + mShortVertArray[vp + 6] = static_cast<GLshort>(dstX); + mShortVertArray[vp + 7] = static_cast<GLshort>(dstY); + + mShortVertArray[vp + 8] = static_cast<GLshort>(dstX); + mShortVertArray[vp + 9] = static_cast<GLshort>(dstY + height); + + mShortVertArray[vp + 10] = static_cast<GLshort>(dstX + width); + mShortVertArray[vp + 11] = static_cast<GLshort>(dstY + height); + + vp += 12; + if (vp >= vLimit) + { + drawTriangleArrayfs(vp); + vp = 0; + } + } + } + if (vp > 0) + drawTriangleArrayfs(vp); +// } +} + +void MobileOpenGLGraphics::drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) +{ + if (!image) + return; + + if (scaledWidth == 0 || scaledHeight == 0) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const int iw = imageRect.w; + const int ih = imageRect.h; + if (iw == 0 || ih == 0) + return; + + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + + // Draw a set of textured rectangles +// if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) +// { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + const float tFractionW = iw / tw; + const float tFractionH = ih / th; + + for (int py = 0; py < h; py += scaledHeight) + { + const int height = (py + scaledHeight >= h) + ? h - py : scaledHeight; + const int dstY = y + py; + const float visibleFractionH = static_cast<float>(height) + / scaledHeight; + const float texY2 = texY1 + tFractionH * visibleFractionH; + for (int px = 0; px < w; px += scaledWidth) + { + const int width = (px + scaledWidth >= w) + ? w - px : scaledWidth; + const int dstX = x + px; + const float visibleFractionW = static_cast<float>(width) + / scaledWidth; + const float texX2 = texX1 + tFractionW * visibleFractionW; + + mFloatTexArray[vp + 0] = texX1; + mFloatTexArray[vp + 1] = texY1; + + mFloatTexArray[vp + 2] = texX2; + mFloatTexArray[vp + 3] = texY1; + + mFloatTexArray[vp + 4] = texX2; + mFloatTexArray[vp + 5] = texY2; + + mFloatTexArray[vp + 6] = texX1; + mFloatTexArray[vp + 7] = texY1; + + mFloatTexArray[vp + 8] = texX1; + mFloatTexArray[vp + 9] = texY2; + + mFloatTexArray[vp + 10] = texX2; + mFloatTexArray[vp + 11] = texY2; + + mShortVertArray[vp + 0] = static_cast<GLshort>(dstX); + mShortVertArray[vp + 1] = static_cast<GLshort>(dstY); + + mShortVertArray[vp + 2] = static_cast<GLshort>(dstX + width); + mShortVertArray[vp + 3] = static_cast<GLshort>(dstY); + + mShortVertArray[vp + 4] = static_cast<GLshort>(dstX + width); + mShortVertArray[vp + 5] = static_cast<GLshort>(dstY + height); + + mShortVertArray[vp + 6] = static_cast<GLshort>(dstX); + mShortVertArray[vp + 7] = static_cast<GLshort>(dstY); + + mShortVertArray[vp + 8] = static_cast<GLshort>(dstX); + mShortVertArray[vp + 9] = static_cast<GLshort>(dstY + height); + + mShortVertArray[vp + 10] = static_cast<GLshort>(dstX + width); + mShortVertArray[vp + 11] = static_cast<GLshort>(dstY + height); + + vp += 12; + if (vp >= vLimit) + { + drawTriangleArrayfs(vp); + vp = 0; + } + } + } + if (vp > 0) + drawTriangleArrayfs(vp); +// } +} + +inline void MobileOpenGLGraphics::drawVertexes(const + NormalOpenGLGraphicsVertexes + &ogl) +{ + const std::vector<GLshort*> &shortVertPool = ogl.mShortVertPool; + std::vector<GLshort*>::const_iterator iv; + const std::vector<GLshort*>::const_iterator iv_end = shortVertPool.end(); + const std::vector<int> &vp = ogl.mVp; + std::vector<int>::const_iterator ivp; + const std::vector<int>::const_iterator ivp_end = vp.end(); + + // Draw a set of textured rectangles +// if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const std::vector<GLfloat*> &floatTexPool = ogl.mFloatTexPool; + std::vector<GLfloat*>::const_iterator ft; + const std::vector<GLfloat*>::const_iterator + ft_end = floatTexPool.end(); + + for (iv = shortVertPool.begin(), ft = floatTexPool.begin(), + ivp = vp.begin(); + iv != iv_end && ft != ft_end && ivp != ivp_end; + ++ iv, ++ ft, ++ ivp) + { + drawTriangleArrayfs(*iv, *ft, *ivp); + } + } +} + +void MobileOpenGLGraphics::calcImagePattern(ImageVertexes *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + if (!image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const int iw = imageRect.w; + const int ih = imageRect.h; + + if (iw == 0 || ih == 0) + return; + + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const unsigned int vLimit = mMaxVertices * 4; + + NormalOpenGLGraphicsVertexes &ogl = vert->ogl; + unsigned int vp = ogl.continueVp(); + + // Draw a set of textured rectangles +// if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + GLfloat *floatTexArray = ogl.continueFloatTexArray(); + GLshort *shortVertArray = ogl.continueShortVertArray(); + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + const float texY2 = static_cast<float>(srcY + height) / th; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + const float texX2 = static_cast<float>(srcX + width) / tw; + + floatTexArray[vp + 0] = texX1; + floatTexArray[vp + 1] = texY1; + + floatTexArray[vp + 2] = texX2; + floatTexArray[vp + 3] = texY1; + + floatTexArray[vp + 4] = texX2; + floatTexArray[vp + 5] = texY2; + + floatTexArray[vp + 6] = texX1; + floatTexArray[vp + 7] = texY1; + + floatTexArray[vp + 8] = texX1; + floatTexArray[vp + 9] = texY2; + + floatTexArray[vp + 10] = texX2; + floatTexArray[vp + 11] = texY2; + + shortVertArray[vp + 0] = dstX; + shortVertArray[vp + 1] = dstY; + + shortVertArray[vp + 2] = dstX + width; + shortVertArray[vp + 3] = dstY; + + shortVertArray[vp + 4] = dstX + width; + shortVertArray[vp + 5] = dstY + height; + + shortVertArray[vp + 6] = dstX; + shortVertArray[vp + 7] = dstY; + + shortVertArray[vp + 8] = dstX; + shortVertArray[vp + 9] = dstY + height; + + shortVertArray[vp + 10] = dstX + width; + shortVertArray[vp + 11] = dstY + height; + + vp += 12; + if (vp >= vLimit) + { + floatTexArray = ogl.switchFloatTexArray(); + shortVertArray = ogl.switchShortVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + } + } + ogl.switchVp(vp); +} + +void MobileOpenGLGraphics::calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y) +{ + if (vertCol->currentGLImage != image->mGLImage) + { + ImageVertexes *const vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + calcTile(vert, image, x, y); + } + else + { + calcTile(vertCol->currentVert, image, x, y); + } +} + +void MobileOpenGLGraphics::drawTile(const ImageCollection *const vertCol) +{ + const ImageVertexesVector &draws = vertCol->draws; + const ImageCollectionCIter it_end = draws.end(); + for (ImageCollectionCIter it = draws.begin(); it != it_end; ++ it) + { + const ImageVertexes *const vert = *it; + const Image *const image = vert->image; + + setColorAlpha(image->mAlpha); +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + drawVertexes(vert->ogl); + } +} + +void MobileOpenGLGraphics::calcImagePattern(ImageCollection* const vertCol, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + ImageVertexes *vert = nullptr; + if (vertCol->currentGLImage != image->mGLImage) + { + vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + calcImagePattern(vert, image, x, y, w, h); +} + +void MobileOpenGLGraphics::calcTile(ImageVertexes *const vert, + const Image *const image, + int dstX, int dstY) const +{ + if (!vert || !image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const int w = imageRect.w; + const int h = imageRect.h; + + if (w == 0 || h == 0) + return; + + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const unsigned int vLimit = mMaxVertices * 4; + + NormalOpenGLGraphicsVertexes &ogl = vert->ogl; + +// std::vector<int> *vps = ogl.getVp(); + unsigned int vp = ogl.continueVp(); + + // Draw a set of textured rectangles +// if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + float texX1 = static_cast<float>(srcX) / tw; + float texY1 = static_cast<float>(srcY) / th; + float texX2 = static_cast<float>(srcX + w) / tw; + float texY2 = static_cast<float>(srcY + h) / th; + + GLfloat *const floatTexArray = ogl.continueFloatTexArray(); + GLshort *const shortVertArray = ogl.continueShortVertArray(); + + floatTexArray[vp + 0] = texX1; + floatTexArray[vp + 1] = texY1; + + floatTexArray[vp + 2] = texX2; + floatTexArray[vp + 3] = texY1; + + floatTexArray[vp + 4] = texX2; + floatTexArray[vp + 5] = texY2; + + floatTexArray[vp + 6] = texX1; + floatTexArray[vp + 7] = texY1; + + floatTexArray[vp + 8] = texX1; + floatTexArray[vp + 9] = texY2; + + floatTexArray[vp + 10] = texX2; + floatTexArray[vp + 11] = texY2; + + shortVertArray[vp + 0] = dstX; + shortVertArray[vp + 1] = dstY; + + shortVertArray[vp + 2] = dstX + w; + shortVertArray[vp + 3] = dstY; + + shortVertArray[vp + 4] = dstX + w; + shortVertArray[vp + 5] = dstY + h; + + shortVertArray[vp + 6] = dstX; + shortVertArray[vp + 7] = dstY; + + shortVertArray[vp + 8] = dstX; + shortVertArray[vp + 9] = dstY + h; + + shortVertArray[vp + 10] = dstX + w; + shortVertArray[vp + 11] = dstY + h; + + vp += 12; + if (vp >= vLimit) + { + ogl.switchFloatTexArray(); + ogl.switchShortVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + ogl.switchVp(vp); +} + +void MobileOpenGLGraphics::drawTile(const ImageVertexes *const vert) +{ + if (!vert) + return; + const Image *const image = vert->image; + + setColorAlpha(image->mAlpha); +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + drawVertexes(vert->ogl); +} + +bool MobileOpenGLGraphics::calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) +{ + ImageVertexes *vert = nullptr; + const Image *const image = imgRect.grid[4]; + if (vertCol->currentGLImage != image->mGLImage) + { + vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + return calcImageRect(vert, x, y, w, h, + imgRect.grid[0], imgRect.grid[2], imgRect.grid[6], imgRect.grid[8], + imgRect.grid[1], imgRect.grid[5], imgRect.grid[7], imgRect.grid[3], + imgRect.grid[4]); +} + +void MobileOpenGLGraphics::updateScreen() +{ + BLOCK_START("Graphics::updateScreen") +// glFlush(); +// glFinish(); +#ifdef DEBUG_DRAW_CALLS + mLastDrawCalls = mDrawCalls; + mDrawCalls = 0; +#endif +#ifdef USE_SDL2 + SDL_GL_SwapWindow(mWindow); +#else + SDL_GL_SwapBuffers(); +#endif +// may be need clear? +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + BLOCK_END("Graphics::updateScreen") +} + +void MobileOpenGLGraphics::_beginDraw() +{ + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + +#ifdef ANDROID + glOrthof(0.0, static_cast<float>(mRect.w), + static_cast<float>(mRect.h), 0.0, -1.0, 1.0); +#else + glOrtho(0.0, static_cast<double>(mRect.w), + static_cast<double>(mRect.h), 0.0, -1.0, 1.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_SCISSOR_TEST); + glDisable(GL_DITHER); + glDisable(GL_LIGHTING); + glDisable(GL_DEPTH_TEST); + glDisable(GL_FOG); + glDisable(GL_COLOR_LOGIC_OP); + glDisable(GL_COLOR_MATERIAL); + glDisable(GL_STENCIL_TEST); + + glShadeModel(GL_FLAT); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glEnableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + +#ifndef ANDROID + glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); + glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST); + glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); +#ifndef __MINGW32__ + glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST); +#endif +#endif + +// glScalef(0.5f, 0.5f, 0.5f); + + pushClipArea(gcn::Rectangle(0, 0, mRect.w, mRect.h)); +} + +void MobileOpenGLGraphics::_endDraw() +{ + popClipArea(); +} + +void MobileOpenGLGraphics::prepareScreenshot() +{ + if (config.getBoolValue("usefbo")) + graphicsManager.createFBO(mRect.w, mRect.h, &mFbo); +} + +SDL_Surface* MobileOpenGLGraphics::getScreenshot() +{ + const int h = mRect.h; + const int w = mRect.w - (mRect.w % 4); + GLint pack = 1; + + SDL_Surface *const screenshot = MSDL_CreateRGBSurface( + SDL_SWSURFACE, w, h, 24, + 0xff0000, 0x00ff00, 0x0000ff, 0x000000); + + if (!screenshot) + return nullptr; + + if (SDL_MUSTLOCK(screenshot)) + SDL_LockSurface(screenshot); + + // Grap the pixel buffer and write it to the SDL surface + glGetIntegerv(GL_PACK_ALIGNMENT, &pack); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, screenshot->pixels); + + // Flip the screenshot, as OpenGL has 0,0 in bottom left + const unsigned int lineSize = 3 * w; + GLubyte *const buf = static_cast<GLubyte*>(malloc(lineSize)); + + const int h2 = h / 2; + for (int i = 0; i < h2; i++) + { + GLubyte *const top = static_cast<GLubyte*>( + screenshot->pixels) + lineSize * i; + GLubyte *const bot = static_cast<GLubyte*>( + screenshot->pixels) + lineSize * (h - 1 - i); + + memcpy(buf, top, lineSize); + memcpy(top, bot, lineSize); + memcpy(bot, buf, lineSize); + } + + free(buf); + + if (config.getBoolValue("usefbo")) + graphicsManager.deleteFBO(&mFbo); + + glPixelStorei(GL_PACK_ALIGNMENT, pack); + + if (SDL_MUSTLOCK(screenshot)) + SDL_UnlockSurface(screenshot); + + return screenshot; +} + +bool MobileOpenGLGraphics::pushClipArea(gcn::Rectangle area) +{ + int transX = 0; + int transY = 0; + + if (!mClipStack.empty()) + { + const gcn::ClipRectangle &clipArea = mClipStack.top(); + transX = -clipArea.xOffset; + transY = -clipArea.yOffset; + } + + const bool result = gcn::Graphics::pushClipArea(area); + + const gcn::ClipRectangle &clipArea = mClipStack.top(); + transX += clipArea.xOffset; + transY += clipArea.yOffset; + + glPushMatrix(); + if (transX || transY) + { + glTranslatef(static_cast<GLfloat>(transX), + static_cast<GLfloat>(transY), 0); + } + glScissor(clipArea.x, mRect.h - clipArea.y - clipArea.height, + clipArea.width, clipArea.height); + + return result; +} + +void MobileOpenGLGraphics::popClipArea() +{ + gcn::Graphics::popClipArea(); + + if (mClipStack.empty()) + return; + + glPopMatrix(); + const gcn::ClipRectangle &clipArea = mClipStack.top(); + glScissor(clipArea.x, mRect.h - clipArea.y - clipArea.height, + clipArea.width, clipArea.height); +} + +#ifdef ANDROID +void MobileOpenGLGraphics::drawPoint(int x A_UNUSED, int y A_UNUSED) +#else +void MobileOpenGLGraphics::drawPoint(int x, int y) +#endif +{ + setTexturingAndBlending(false); + restoreColor(); + +#ifdef ANDROID + // TODO need fix +#else + glBegin(GL_POINTS); + glVertex2i(x, y); + glEnd(); +#endif +} + +void MobileOpenGLGraphics::drawLine(int x1, int y1, int x2, int y2) +{ + setTexturingAndBlending(false); + restoreColor(); + + mShortVertArray[0] = static_cast<GLshort>(x1); + mShortVertArray[1] = static_cast<GLshort>(y1); + mShortVertArray[2] = static_cast<GLshort>(x2); + mShortVertArray[3] = static_cast<GLshort>(y2); + + drawLineArrays(4); +} + +void MobileOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, false); +} + +void MobileOpenGLGraphics::fillRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, true); +} + +void MobileOpenGLGraphics::setTargetPlane(int width A_UNUSED, + int height A_UNUSED) +{ +} + +void MobileOpenGLGraphics::setTexturingAndBlending(const bool enable) +{ + if (enable) + { + if (!mTexture) + { + glEnable(OpenGLImageHelper::mTextureType); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + mTexture = true; + } + + if (!mAlpha) + { + glEnable(GL_BLEND); + mAlpha = true; + } + } + else + { + mLastImage = 0; + if (mAlpha && !mColorAlpha) + { + glDisable(GL_BLEND); + mAlpha = false; + } + else if (!mAlpha && mColorAlpha) + { + glEnable(GL_BLEND); + mAlpha = true; + } + + if (mTexture) + { + glDisable(OpenGLImageHelper::mTextureType); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + mTexture = false; + } + } +} + +void MobileOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect, + const bool filled) +{ + BLOCK_START("Graphics::drawRectangle") + setTexturingAndBlending(false); + restoreColor(); + + const GLshort x = static_cast<GLshort>(rect.x); + const GLshort y = static_cast<GLshort>(rect.y); + const GLshort width = static_cast<GLshort>(rect.width); + const GLshort height = static_cast<GLshort>(rect.height); + const GLshort xw = static_cast<GLshort>(rect.x + width); + const GLshort yh = static_cast<GLshort>(rect.y + height); + + if (filled) + { + GLshort vert[] = + { + x, y, + xw, y, + x, yh, + xw, yh + }; + + glVertexPointer(2, GL_SHORT, 0, &vert); +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + } + else + { + GLshort vert[] = + { + x, y, + xw, y, + xw, yh, + x, yh + }; + + glVertexPointer(2, GL_SHORT, 0, &vert); +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_LINE_LOOP, 0, 4); + } + BLOCK_END("Graphics::drawRectangle") +} + +bool MobileOpenGLGraphics::drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height) +{ + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + + setTexturingAndBlending(false); + restoreColor(); + + const GLshort xs1 = static_cast<GLshort>(x1); + const GLshort xs2 = static_cast<GLshort>(x2); + const GLshort ys1 = static_cast<GLshort>(y1); + const GLshort ys2 = static_cast<GLshort>(y2); + + for (int16_t y = y1; y < y2; y += height) + { + mShortVertArray[vp + 0] = xs1; + mShortVertArray[vp + 1] = y; + + mShortVertArray[vp + 2] = xs2; + mShortVertArray[vp + 3] = y; + + vp += 4; + if (vp >= vLimit) + { + drawLineArrays(vp); + vp = 0; + } + } + + for (int16_t x = x1; x < x2; x += width) + { + mShortVertArray[vp + 0] = x; + mShortVertArray[vp + 1] = ys1; + + mShortVertArray[vp + 2] = x; + mShortVertArray[vp + 3] = ys2; + + vp += 4; + if (vp >= vLimit) + { + drawLineArrays(vp); + vp = 0; + } + } + + if (vp > 0) + drawLineArrays(vp); + + return true; +} + +void MobileOpenGLGraphics::bindTexture(const GLenum target, + const GLuint texture) +{ + if (mLastImage != texture) + { + mLastImage = texture; + glBindTexture(target, texture); + } +} + +inline void MobileOpenGLGraphics::drawTriangleArrayfs(const int size) +{ + glVertexPointer(2, GL_SHORT, 0, mShortVertArray); + glTexCoordPointer(2, GL_FLOAT, 0, mFloatTexArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_TRIANGLES, 0, size / 2); +} + +inline void MobileOpenGLGraphics::drawTriangleArrayfs(const GLshort *const + shortVertArray, + const GLfloat *const + floatTexArray, + const int size) +{ + glVertexPointer(2, GL_SHORT, 0, shortVertArray); + glTexCoordPointer(2, GL_FLOAT, 0, floatTexArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_TRIANGLES, 0, size / 2); +} + +inline void MobileOpenGLGraphics::drawLineArrays(const int size) +{ + glVertexPointer(2, GL_SHORT, 0, mShortVertArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_LINES, 0, size / 2); +} + +void MobileOpenGLGraphics::dumpSettings() +{ + GLint test[1000]; + logger->log("\n\n"); + logger->log("start opengl dump"); + for (int f = 0; f < 65535; f ++) + { + test[0] = 0; + test[1] = 0; + test[2] = 0; + test[3] = 0; + glGetIntegerv(f, &test[0]); + if (test[0] || test[1] || test[2] || test[3]) + { + logger->log("\n%d = %d, %d, %d, %d", f, + test[0], test[1], test[2], test[3]); + } + } +} + +void MobileOpenGLGraphics::setColorAlpha(const float alpha) +{ + if (!mIsByteColor && mFloatColor == alpha) + return; + + glColor4f(1.0f, 1.0f, 1.0f, alpha); + mIsByteColor = false; + mFloatColor = alpha; +} + +void MobileOpenGLGraphics::restoreColor() +{ + if (mIsByteColor && mByteColor == mColor) + return; + + glColor4ub(static_cast<GLubyte>(mColor.r), + static_cast<GLubyte>(mColor.g), + static_cast<GLubyte>(mColor.b), + static_cast<GLubyte>(mColor.a)); + mIsByteColor = true; + mByteColor = mColor; +} + +#ifdef DEBUG_BIND_TEXTURE +void MobileOpenGLGraphics::debugBindTexture(const Image *const image) +{ + const std::string texture = image->getIdPath(); + if (mOldTexture != texture) + { + if ((!mOldTexture.empty() || !texture.empty()) + && mOldTextureId != image->mGLImage) + { + logger->log("bind: %s (%d) to %s (%d)", mOldTexture.c_str(), + mOldTextureId, texture.c_str(), image->mGLImage); + } + mOldTextureId = image->mGLImage; + mOldTexture = texture; + } +} +#else +void MobileOpenGLGraphics::debugBindTexture(const Image *const image A_UNUSED) +{ +} +#endif + +#endif // USE_OPENGL diff --git a/src/render/mobileopenglgraphics.h b/src/render/mobileopenglgraphics.h new file mode 100644 index 000000000..1597d3bd9 --- /dev/null +++ b/src/render/mobileopenglgraphics.h @@ -0,0 +1,232 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MOBILEOPENGLGRAPHICS_H +#define MOBILEOPENGLGRAPHICS_H + +#include "main.h" +#ifdef USE_OPENGL + +#include "localconsts.h" +#include "render/graphics.h" + +#include "resources/fboinfo.h" + +#ifdef ANDROID +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <GLES2/gl2.h> +#else +#ifndef USE_SDL2 +#define GL_GLEXT_PROTOTYPES 1 +#endif +#include <SDL_opengl.h> +#include <GL/glext.h> +#endif + +#include <set> + +class NormalOpenGLGraphicsVertexes; + +class MobileOpenGLGraphics final : public Graphics +{ + public: + MobileOpenGLGraphics(); + + A_DELETE_COPY(MobileOpenGLGraphics) + + ~MobileOpenGLGraphics(); + + bool setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) override; + + + /** + * Draws a resclaled version of the image + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor) override; + + /** + * Used to get the smooth rescale option over the standard function. + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor, bool smooth); + + void drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) override; + + /** + * Draw a pattern based on a rescaled version of the given image... + */ + void drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) override; + + void calcImagePattern(ImageVertexes *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const override; + + void calcImagePattern(ImageCollection *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const override; + + void calcTile(ImageVertexes *const vert, const Image *const image, + int x, int y) const override; + + void drawTile(const ImageCollection *const vertCol); + + void calcTile(ImageCollection *const vertCol, + const Image *const image, int x, int y) override; + + void drawTile(const ImageVertexes *const vert) override; + + bool calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect); + + void updateScreen() override; + + void _beginDraw(); + + void _endDraw(); + + bool pushClipArea(gcn::Rectangle area); + + void popClipArea(); + + void setColor(const gcn::Color &color) + { + mColor = color; + mColor2 = color; + mColorAlpha = (color.a != 255); + } + + void setColorAll(const gcn::Color &color, const gcn::Color &color2) + { + mColor = color; + mColor2 = color2; + mColorAlpha = (color.a != 255); + } + + void drawPoint(int x, int y); + + void drawLine(int x1, int y1, int x2, int y2); + + void drawRectangle(const gcn::Rectangle &rect, const bool filled); + + void drawRectangle(const gcn::Rectangle &rect); + + void fillRectangle(const gcn::Rectangle &rect); + + void setTargetPlane(int width, int height); + + inline void drawTriangleArrayfs(const GLshort *const shortVertArray, + const GLfloat *const floatTexArray, + const int size); + + inline void drawTriangleArrayfs(const int size); + + inline void drawLineArrays(const int size); + + inline void drawVertexes(const NormalOpenGLGraphicsVertexes &ogl); + + void initArrays() override; + + static void dumpSettings(); + + /** + * Takes a screenshot and returns it as SDL surface. + */ + SDL_Surface *getScreenshot() override A_WARN_UNUSED; + + void prepareScreenshot() override; + + bool drawNet(const int x1, const int y1, const int x2, const int y2, + const int width, const int height) override; + + int getMemoryUsage(); + + void updateTextureFormat(); + +#ifdef DEBUG_DRAW_CALLS + virtual unsigned int getDrawCalls() const + { return mLastDrawCalls; } + + static unsigned int mDrawCalls; + + static unsigned int mLastDrawCalls; +#endif + + static void bindTexture(const GLenum target, const GLuint texture); + + static GLuint mLastImage; + + protected: + bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) override; + + void setTexturingAndBlending(const bool enable); + + void debugBindTexture(const Image *image); + + private: + void inline setColorAlpha(float alpha); + + void inline restoreColor(); + + GLfloat *mFloatTexArray; + GLint *mIntTexArray; + GLint *mIntVertArray; + GLshort *mShortVertArray; + bool mTexture; + + bool mIsByteColor; + gcn::Color mByteColor; + float mFloatColor; + int mMaxVertices; + bool mColorAlpha; +#ifdef DEBUG_BIND_TEXTURE + std::string mOldTexture; + unsigned mOldTextureId; +#endif + FBOInfo mFbo; +}; +#endif + +#endif // MOBILEOPENGLGRAPHICS_H diff --git a/src/render/normalopenglgraphics.cpp b/src/render/normalopenglgraphics.cpp new file mode 100644 index 000000000..91cd429fb --- /dev/null +++ b/src/render/normalopenglgraphics.cpp @@ -0,0 +1,1515 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "main.h" +#if defined USE_OPENGL && !defined ANDROID + +#include "render/normalopenglgraphics.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/image.h" +#include "resources/openglimagehelper.h" + +#include "utils/sdlcheckutils.h" +#include "utils/stringutils.h" + +#include <SDL.h> + +#include "debug.h" + +GLuint NormalOpenGLGraphics::mLastImage = 0; +#ifdef DEBUG_DRAW_CALLS +unsigned int NormalOpenGLGraphics::mDrawCalls = 0; +unsigned int NormalOpenGLGraphics::mLastDrawCalls = 0; +#endif +#ifdef DEBUG_BIND_TEXTURE +unsigned int NormalOpenGLGraphics::mBinds = 0; +unsigned int NormalOpenGLGraphics::mLastBinds = 0; +#endif + +NormalOpenGLGraphics::NormalOpenGLGraphics(): + mFloatTexArray(nullptr), + mIntTexArray(nullptr), + mIntVertArray(nullptr), + mTexture(false), + mIsByteColor(false), + mByteColor(), + mFloatColor(1.0f), + mMaxVertices(500), + mColorAlpha(false), +#ifdef DEBUG_BIND_TEXTURE + mOldTexture(), + mOldTextureId(0), +#endif + mFbo() +{ + mOpenGL = 1; + mName = "fast OpenGL"; +} + +NormalOpenGLGraphics::~NormalOpenGLGraphics() +{ + delete [] mFloatTexArray; + delete [] mIntTexArray; + delete [] mIntVertArray; +} + +void NormalOpenGLGraphics::initArrays() +{ + mMaxVertices = graphicsManager.getMaxVertices(); + if (mMaxVertices < 500) + mMaxVertices = 500; + else if (mMaxVertices > 1024) + mMaxVertices = 1024; + + // need alocate small size, after if limit reached reallocate to double size + vertexBufSize = mMaxVertices; + const int sz = mMaxVertices * 4 + 30; + mFloatTexArray = new GLfloat[sz]; + mIntTexArray = new GLint[sz]; + mIntVertArray = new GLint[sz]; +} + +bool NormalOpenGLGraphics::setVideoMode(const int w, const int h, + const int bpp, const bool fs, + const bool hwaccel, const bool resize, + const bool noFrame) +{ + setMainFlags(w, h, bpp, fs, hwaccel, resize, noFrame); + + return setOpenGLMode(); +} + +static inline void drawQuad(const Image *const image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height) +{ + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + // Find OpenGL normalized texture coordinates. + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + const float texX2 = static_cast<float>(srcX + width) / tw; + const float texY2 = static_cast<float>(srcY + height) / th; + + GLfloat tex[] = + { + texX1, texY1, + texX2, texY1, + texX2, texY2, + texX1, texY2 + }; + + GLint vert[] = + { + dstX, dstY, + dstX + width, dstY, + dstX + width, dstY + height, + dstX, dstY + height + }; + + glVertexPointer(2, GL_INT, 0, &vert); + glTexCoordPointer(2, GL_FLOAT, 0, &tex); + +#ifdef DEBUG_DRAW_CALLS + NormalOpenGLGraphics::mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, 4); + } + else + { + GLint tex[] = + { + srcX, srcY, + srcX + width, srcY, + srcX + width, srcY + height, + srcX, srcY + height + }; + GLint vert[] = + { + dstX, dstY, + dstX + width, dstY, + dstX + width, dstY + height, + dstX, dstY + height + }; + + glVertexPointer(2, GL_INT, 0, &vert); + glTexCoordPointer(2, GL_INT, 0, &tex); + +#ifdef DEBUG_DRAW_CALLS + NormalOpenGLGraphics::mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, 4); + } +} + +static inline void drawRescaledQuad(const Image *const image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight) +{ + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + // Find OpenGL normalized texture coordinates. + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + const float texX2 = static_cast<float>(srcX + width) / tw; + const float texY2 = static_cast<float>(srcY + height) / th; + + GLfloat tex[] = + { + texX1, texY1, + texX2, texY1, + texX2, texY2, + texX1, texY2 + }; + + GLint vert[] = + { + dstX, dstY, + dstX + desiredWidth, dstY, + dstX + desiredWidth, dstY + desiredHeight, + dstX, dstY + desiredHeight + }; + + glVertexPointer(2, GL_INT, 0, &vert); + glTexCoordPointer(2, GL_FLOAT, 0, &tex); + +#ifdef DEBUG_DRAW_CALLS + NormalOpenGLGraphics::mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, 4); + } + else + { + GLint tex[] = + { + srcX, srcY, + srcX + width, srcY, + srcX + width, srcY + height, + srcX, srcY + height + }; + GLint vert[] = + { + dstX, dstY, + dstX + desiredWidth, dstY, + dstX + desiredWidth, dstY + desiredHeight, + dstX, dstY + desiredHeight + }; + + glVertexPointer(2, GL_INT, 0, &vert); + glTexCoordPointer(2, GL_INT, 0, &tex); + +#ifdef DEBUG_DRAW_CALLS + NormalOpenGLGraphics::mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, 4); + } +} + + +bool NormalOpenGLGraphics::drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) +{ + FUNC_BLOCK("Graphics::drawImage2", 1) + if (!image) + return false; + + const SDL_Rect &imageRect = image->mBounds; + + if (!useColor) + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + drawQuad(image, srcX + imageRect.x, srcY + imageRect.y, + dstX, dstY, width, height); + + return true; +} + +bool NormalOpenGLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor) +{ + return drawRescaledImage(image, srcX, srcY, + dstX, dstY, + width, height, + desiredWidth, desiredHeight, + useColor, true); +} + +bool NormalOpenGLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor, + bool smooth) +{ + FUNC_BLOCK("Graphics::drawRescaledImage", 1) + if (!image) + return false; + + // Just draw the image normally when no resizing is necessary, + if (width == desiredWidth && height == desiredHeight) + { + return drawImage2(image, srcX, srcY, dstX, dstY, + width, height, useColor); + } + + // When the desired image is smaller than the current one, + // disable smooth effect. + if (width > desiredWidth && height > desiredHeight) + smooth = false; + + const SDL_Rect &imageRect = image->mBounds; + srcX += imageRect.x; + srcY += imageRect.y; + + if (!useColor) + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + // Draw a textured quad. + drawRescaledQuad(image, srcX, srcY, dstX, dstY, width, height, + desiredWidth, desiredHeight); + + if (smooth) // A basic smooth effect... + { + setColorAlpha(0.2f); + drawRescaledQuad(image, srcX, srcY, dstX - 1, dstY - 1, width, height, + desiredWidth + 1, desiredHeight + 1); + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY + 1, width, height, + desiredWidth - 1, desiredHeight - 1); + + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY, width, height, + desiredWidth - 1, desiredHeight); + drawRescaledQuad(image, srcX, srcY, dstX, dstY + 1, width, height, + desiredWidth, desiredHeight - 1); + } + + return true; +} + +void NormalOpenGLGraphics::drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) +{ + FUNC_BLOCK("Graphics::drawImagePattern", 1) + if (!image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const int iw = imageRect.w; + const int ih = imageRect.h; + + if (iw == 0 || ih == 0) + return; + + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + const float texY2 = static_cast<float>(srcY + height) / th; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + + const float texX2 = static_cast<float>(srcX + width) / tw; + + mFloatTexArray[vp + 0] = texX1; + mFloatTexArray[vp + 1] = texY1; + + mFloatTexArray[vp + 2] = texX2; + mFloatTexArray[vp + 3] = texY1; + + mFloatTexArray[vp + 4] = texX2; + mFloatTexArray[vp + 5] = texY2; + + mFloatTexArray[vp + 6] = texX1; + mFloatTexArray[vp + 7] = texY2; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayfi(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayfi(vp); + } + else + { + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + + mIntTexArray[vp + 0] = srcX; + mIntTexArray[vp + 1] = srcY; + + mIntTexArray[vp + 2] = srcX + width; + mIntTexArray[vp + 3] = srcY; + + mIntTexArray[vp + 4] = srcX + width; + mIntTexArray[vp + 5] = srcY + height; + + mIntTexArray[vp + 6] = srcX; + mIntTexArray[vp + 7] = srcY + height; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayii(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayii(vp); + } +} + +void NormalOpenGLGraphics::drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) +{ + if (!image) + return; + + if (scaledWidth == 0 || scaledHeight == 0) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int iw = imageRect.w; + const int ih = imageRect.h; + if (iw == 0 || ih == 0) + return; + + const int srcX = imageRect.x; + const int srcY = imageRect.y; + + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + const float tFractionW = iw / tw; + const float tFractionH = ih / th; + + for (int py = 0; py < h; py += scaledHeight) + { + const int height = (py + scaledHeight >= h) + ? h - py : scaledHeight; + const int dstY = y + py; + const float visibleFractionH = static_cast<float>(height) + / scaledHeight; + const float texY2 = texY1 + tFractionH * visibleFractionH; + for (int px = 0; px < w; px += scaledWidth) + { + const int width = (px + scaledWidth >= w) + ? w - px : scaledWidth; + const int dstX = x + px; + const float visibleFractionW = static_cast<float>(width) + / scaledWidth; + const float texX2 = texX1 + tFractionW * visibleFractionW; + + mFloatTexArray[vp + 0] = texX1; + mFloatTexArray[vp + 1] = texY1; + + mFloatTexArray[vp + 2] = texX2; + mFloatTexArray[vp + 3] = texY1; + + mFloatTexArray[vp + 4] = texX2; + mFloatTexArray[vp + 5] = texY2; + + mFloatTexArray[vp + 6] = texX1; + mFloatTexArray[vp + 7] = texY2; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayfi(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayfi(vp); + } + else + { + const float scaleFactorW = static_cast<float>(scaledWidth) / iw; + const float scaleFactorH = static_cast<float>(scaledHeight) / ih; + + for (int py = 0; py < h; py += scaledHeight) + { + const int height = (py + scaledHeight >= h) + ? h - py : scaledHeight; + const int dstY = y + py; + const int scaledY = srcY + height / scaleFactorH; + for (int px = 0; px < w; px += scaledWidth) + { + const int width = (px + scaledWidth >= w) + ? w - px : scaledWidth; + const int dstX = x + px; + const int scaledX = srcX + width / scaleFactorW; + + mIntTexArray[vp + 0] = srcX; + mIntTexArray[vp + 1] = srcY; + + mIntTexArray[vp + 2] = scaledX; + mIntTexArray[vp + 3] = srcY; + + mIntTexArray[vp + 4] = scaledX; + mIntTexArray[vp + 5] = scaledY; + + mIntTexArray[vp + 6] = srcX; + mIntTexArray[vp + 7] = scaledY; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayii(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayii(vp); + } +} + +inline void NormalOpenGLGraphics::drawVertexes(const + NormalOpenGLGraphicsVertexes + &ogl) +{ + const std::vector<GLint*> &intVertPool = ogl.mIntVertPool; + std::vector<GLint*>::const_iterator iv; + const std::vector<GLint*>::const_iterator iv_end = intVertPool.end(); + const std::vector<int> &vp = ogl.mVp; + std::vector<int>::const_iterator ivp; + const std::vector<int>::const_iterator ivp_end = vp.end(); + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const std::vector<GLfloat*> &floatTexPool = ogl.mFloatTexPool; + std::vector<GLfloat*>::const_iterator ft; + const std::vector<GLfloat*>::const_iterator + ft_end = floatTexPool.end(); + + for (iv = intVertPool.begin(), ft = floatTexPool.begin(), + ivp = vp.begin(); + iv != iv_end && ft != ft_end && ivp != ivp_end; + ++ iv, ++ ft, ++ ivp) + { + drawQuadArrayfi(*iv, *ft, *ivp); + } + } + else + { + const std::vector<GLint*> &intTexPool = ogl.mIntTexPool; + std::vector<GLint*>::const_iterator it; + const std::vector<GLint*>::const_iterator it_end = intTexPool.end(); + + for (iv = intVertPool.begin(), it = intTexPool.begin(), + ivp = vp.begin(); + iv != iv_end && it != it_end && ivp != ivp_end; + ++ iv, ++ it, ++ ivp) + { + drawQuadArrayii(*iv, *it, *ivp); + } + } +} + +void NormalOpenGLGraphics::calcImagePattern(ImageVertexes* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + if (!image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int iw = imageRect.w; + const int ih = imageRect.h; + + if (iw == 0 || ih == 0) + return; + + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const unsigned int vLimit = mMaxVertices * 4; + + NormalOpenGLGraphicsVertexes &ogl = vert->ogl; + unsigned int vp = ogl.continueVp(); + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + GLfloat *floatTexArray = ogl.continueFloatTexArray(); + GLint *intVertArray = ogl.continueIntVertArray(); + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + const float texY2 = static_cast<float>(srcY + height) / th; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + const float texX2 = static_cast<float>(srcX + width) / tw; + + floatTexArray[vp + 0] = texX1; + floatTexArray[vp + 1] = texY1; + + floatTexArray[vp + 2] = texX2; + floatTexArray[vp + 3] = texY1; + + floatTexArray[vp + 4] = texX2; + floatTexArray[vp + 5] = texY2; + + floatTexArray[vp + 6] = texX1; + floatTexArray[vp + 7] = texY2; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + width; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + width; + intVertArray[vp + 5] = dstY + height; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + floatTexArray = ogl.switchFloatTexArray(); + intVertArray = ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + } + } + else + { + GLint *intTexArray = ogl.continueIntTexArray(); + GLint *intVertArray = ogl.continueIntVertArray(); + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + + intTexArray[vp + 0] = srcX; + intTexArray[vp + 1] = srcY; + + intTexArray[vp + 2] = srcX + width; + intTexArray[vp + 3] = srcY; + + intTexArray[vp + 4] = srcX + width; + intTexArray[vp + 5] = srcY + height; + + intTexArray[vp + 6] = srcX; + intTexArray[vp + 7] = srcY + height; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + width; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + width; + intVertArray[vp + 5] = dstY + height; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + intTexArray = ogl.switchIntTexArray(); + intVertArray = ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + } + } + ogl.switchVp(vp); +} + +void NormalOpenGLGraphics::calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y) +{ + if (vertCol->currentGLImage != image->mGLImage) + { + ImageVertexes *const vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + calcTile(vert, image, x, y); + } + else + { + calcTile(vertCol->currentVert, image, x, y); + } +} + +void NormalOpenGLGraphics::drawTile(const ImageCollection *const vertCol) +{ + const ImageVertexesVector &draws = vertCol->draws; + const ImageCollectionCIter it_end = draws.end(); + for (ImageCollectionCIter it = draws.begin(); it != it_end; ++ it) + { + const ImageVertexes *const vert = *it; + const Image *const image = vert->image; + + setColorAlpha(image->mAlpha); +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + drawVertexes(vert->ogl); + } +} + +void NormalOpenGLGraphics::calcImagePattern(ImageCollection* const vertCol, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + ImageVertexes *vert = nullptr; + if (vertCol->currentGLImage != image->mGLImage) + { + vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + calcImagePattern(vert, image, x, y, w, h); +} + +void NormalOpenGLGraphics::calcTile(ImageVertexes *const vert, + const Image *const image, + int dstX, int dstY) const +{ + if (!vert || !image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int w = imageRect.w; + const int h = imageRect.h; + + if (w == 0 || h == 0) + return; + + const int srcX = imageRect.x; + const int srcY = imageRect.y; + + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const unsigned int vLimit = mMaxVertices * 4; + + NormalOpenGLGraphicsVertexes &ogl = vert->ogl; + +// std::vector<int> *vps = ogl.getVp(); + unsigned int vp = ogl.continueVp(); + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + const float texX2 = static_cast<float>(srcX + w) / tw; + const float texY2 = static_cast<float>(srcY + h) / th; + + GLfloat *const floatTexArray = ogl.continueFloatTexArray(); + GLint *const intVertArray = ogl.continueIntVertArray(); + + floatTexArray[vp + 0] = texX1; + floatTexArray[vp + 1] = texY1; + + floatTexArray[vp + 2] = texX2; + floatTexArray[vp + 3] = texY1; + + floatTexArray[vp + 4] = texX2; + floatTexArray[vp + 5] = texY2; + + floatTexArray[vp + 6] = texX1; + floatTexArray[vp + 7] = texY2; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + w; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + w; + intVertArray[vp + 5] = dstY + h; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + h; + + vp += 8; + if (vp >= vLimit) + { + ogl.switchFloatTexArray(); + ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + else + { + GLint *const intTexArray = ogl.continueIntTexArray(); + GLint *const intVertArray = ogl.continueIntVertArray(); + + intTexArray[vp + 0] = srcX; + intTexArray[vp + 1] = srcY; + + intTexArray[vp + 2] = srcX + w; + intTexArray[vp + 3] = srcY; + + intTexArray[vp + 4] = srcX + w; + intTexArray[vp + 5] = srcY + h; + + intTexArray[vp + 6] = srcX; + intTexArray[vp + 7] = srcY + h; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + w; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + w; + intVertArray[vp + 5] = dstY + h; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + h; + + vp += 8; + if (vp >= vLimit) + { + ogl.switchIntTexArray(); + ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + ogl.switchVp(vp); +} + +void NormalOpenGLGraphics::drawTile(const ImageVertexes *const vert) +{ + if (!vert) + return; + const Image *const image = vert->image; + + setColorAlpha(image->mAlpha); +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + drawVertexes(vert->ogl); +} + +bool NormalOpenGLGraphics::calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) +{ + ImageVertexes *vert = nullptr; + Image *const image = imgRect.grid[4]; + if (!image) + return false; + if (vertCol->currentGLImage != image->mGLImage) + { + vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + const Image *const *const grid = &imgRect.grid[0]; + return calcImageRect(vert, x, y, w, h, + grid[0], grid[2], grid[6], grid[8], + grid[1], grid[5], grid[7], grid[3], + grid[4]); +} + +void NormalOpenGLGraphics::updateScreen() +{ + BLOCK_START("Graphics::updateScreen") +// glFlush(); +// glFinish(); +#ifdef DEBUG_DRAW_CALLS + mLastDrawCalls = mDrawCalls; + mDrawCalls = 0; +#endif +#ifdef DEBUG_BIND_TEXTURE + mLastBinds = mBinds; + mBinds = 0; +#endif +#ifdef USE_SDL2 + SDL_GL_SwapWindow(mWindow); +#else + SDL_GL_SwapBuffers(); +#endif +// may be need clear? +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + BLOCK_END("Graphics::updateScreen") +} + +void NormalOpenGLGraphics::_beginDraw() +{ + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + const int w = mRect.w; + const int h = mRect.h; + +#ifdef ANDROID + glOrthof(0.0, static_cast<float>(w), static_cast<float>(h), + 0.0, -1.0, 1.0); +#else + glOrtho(0.0, static_cast<double>(w), static_cast<double>(h), + 0.0, -1.0, 1.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_SCISSOR_TEST); + glDisable(GL_DITHER); + glDisable(GL_LIGHTING); + glDisable(GL_DEPTH_TEST); + glDisable(GL_FOG); + glDisable(GL_COLOR_LOGIC_OP); + glDisable(GL_COLOR_MATERIAL); + glDisable(GL_STENCIL_TEST); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glEnableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + glShadeModel(GL_FLAT); + glDepthMask(GL_FALSE); + +#ifndef ANDROID + glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); + glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST); + glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); +#ifndef __MINGW32__ + glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST); +#endif +#endif + + pushClipArea(gcn::Rectangle(0, 0, w, h)); +} + +void NormalOpenGLGraphics::_endDraw() +{ + popClipArea(); +} + +void NormalOpenGLGraphics::prepareScreenshot() +{ + if (config.getBoolValue("usefbo")) + graphicsManager.createFBO(mRect.w, mRect.h, &mFbo); +} + +SDL_Surface* NormalOpenGLGraphics::getScreenshot() +{ + const int h = mRect.h; + const int w = mRect.w - (mRect.w % 4); + GLint pack = 1; + + SDL_Surface *const screenshot = MSDL_CreateRGBSurface( + SDL_SWSURFACE, + w, h, 24, + 0xff0000, 0x00ff00, 0x0000ff, 0x000000); + + if (!screenshot) + return nullptr; + + if (SDL_MUSTLOCK(screenshot)) + SDL_LockSurface(screenshot); + + // Grap the pixel buffer and write it to the SDL surface + glGetIntegerv(GL_PACK_ALIGNMENT, &pack); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, screenshot->pixels); + + // Flip the screenshot, as OpenGL has 0,0 in bottom left + const unsigned int lineSize = 3 * w; + GLubyte *const buf = static_cast<GLubyte *const>(malloc(lineSize)); + + const int h2 = h / 2; + for (int i = 0; i < h2; i++) + { + GLubyte *const top = static_cast<GLubyte *const>( + screenshot->pixels) + lineSize * i; + GLubyte *const bot = static_cast<GLubyte *const>( + screenshot->pixels) + lineSize * (h - 1 - i); + + memcpy(buf, top, lineSize); + memcpy(top, bot, lineSize); + memcpy(bot, buf, lineSize); + } + + free(buf); + + if (config.getBoolValue("usefbo")) + graphicsManager.deleteFBO(&mFbo); + + glPixelStorei(GL_PACK_ALIGNMENT, pack); + + if (SDL_MUSTLOCK(screenshot)) + SDL_UnlockSurface(screenshot); + + return screenshot; +} + +bool NormalOpenGLGraphics::pushClipArea(gcn::Rectangle area) +{ + int transX = 0; + int transY = 0; + + if (!mClipStack.empty()) + { + const gcn::ClipRectangle &clipArea = mClipStack.top(); + transX = -clipArea.xOffset; + transY = -clipArea.yOffset; + } + + const bool result = gcn::Graphics::pushClipArea(area); + + const gcn::ClipRectangle &clipArea = mClipStack.top(); + transX += clipArea.xOffset; + transY += clipArea.yOffset; + + glPushMatrix(); + if (transX || transY) + { + glTranslatef(static_cast<GLfloat>(transX), + static_cast<GLfloat>(transY), 0); + } + glScissor(clipArea.x, mRect.h - clipArea.y - clipArea.height, + clipArea.width, clipArea.height); + + return result; +} + +void NormalOpenGLGraphics::popClipArea() +{ + gcn::Graphics::popClipArea(); + + if (mClipStack.empty()) + return; + + glPopMatrix(); + const gcn::ClipRectangle &clipArea = mClipStack.top(); + glScissor(clipArea.x, mRect.h - clipArea.y - clipArea.height, + clipArea.width, clipArea.height); +} + +void NormalOpenGLGraphics::drawPoint(int x, int y) +{ + setTexturingAndBlending(false); + restoreColor(); + +#ifdef ANDROID + // TODO need fix +#else + glBegin(GL_POINTS); + glVertex2i(x, y); + glEnd(); +#endif +} + +void NormalOpenGLGraphics::drawLine(int x1, int y1, int x2, int y2) +{ + setTexturingAndBlending(false); + restoreColor(); + + mFloatTexArray[0] = static_cast<float>(x1) + 0.5f; + mFloatTexArray[1] = static_cast<float>(y1) + 0.5f; + mFloatTexArray[2] = static_cast<float>(x2) + 0.5f; + mFloatTexArray[3] = static_cast<float>(y2) + 0.5f; + + drawLineArrayf(4); +} + +void NormalOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, false); +} + +void NormalOpenGLGraphics::fillRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, true); +} + +void NormalOpenGLGraphics::setTargetPlane(int width A_UNUSED, + int height A_UNUSED) +{ +} + +void NormalOpenGLGraphics::setTexturingAndBlending(const bool enable) +{ + if (enable) + { + if (!mTexture) + { + glEnable(OpenGLImageHelper::mTextureType); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + mTexture = true; + } + + if (!mAlpha) + { + glEnable(GL_BLEND); + mAlpha = true; + } + } + else + { + mLastImage = 0; + if (mAlpha && !mColorAlpha) + { + glDisable(GL_BLEND); + mAlpha = false; + } + else if (!mAlpha && mColorAlpha) + { + glEnable(GL_BLEND); + mAlpha = true; + } + + if (mTexture) + { + glDisable(OpenGLImageHelper::mTextureType); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + mTexture = false; + } + } +} + +void NormalOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect, + const bool filled) +{ + BLOCK_START("Graphics::drawRectangle") + const float offset = filled ? 0 : 0.5f; + const float x = static_cast<float>(rect.x); + const float y = static_cast<float>(rect.y); + const float width = static_cast<float>(rect.width); + const float height = static_cast<float>(rect.height); + + setTexturingAndBlending(false); + restoreColor(); + + GLfloat vert[] = + { + x + offset, y + offset, + x + width - offset, y + offset, + x + width - offset, y + height - offset, + x + offset, y + height - offset + }; + + glVertexPointer(2, GL_FLOAT, 0, &vert); +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(filled ? GL_QUADS : GL_LINE_LOOP, 0, 4); + BLOCK_END("Graphics::drawRectangle") +} + +bool NormalOpenGLGraphics::drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height) +{ + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + + setTexturingAndBlending(false); + restoreColor(); + + const float xf1 = static_cast<float>(x1); + const float xf2 = static_cast<float>(x2); + const float yf1 = static_cast<float>(y1); + const float yf2 = static_cast<float>(y2); + + for (int y = y1; y < y2; y += height) + { + mFloatTexArray[vp + 0] = xf1; + mFloatTexArray[vp + 1] = static_cast<float>(y); + + mFloatTexArray[vp + 2] = xf2; + mFloatTexArray[vp + 3] = static_cast<float>(y); + + vp += 4; + if (vp >= vLimit) + { + drawLineArrayf(vp); + vp = 0; + } + } + + for (int x = x1; x < x2; x += width) + { + mFloatTexArray[vp + 0] = static_cast<float>(x); + mFloatTexArray[vp + 1] = yf1; + + mFloatTexArray[vp + 2] = static_cast<float>(x); + mFloatTexArray[vp + 3] = yf2; + + vp += 4; + if (vp >= vLimit) + { + drawLineArrayf(vp); + vp = 0; + } + } + + if (vp > 0) + drawLineArrayf(vp); + + return true; +} + +void NormalOpenGLGraphics::bindTexture(const GLenum target, + const GLuint texture) +{ + if (mLastImage != texture) + { + mLastImage = texture; + glBindTexture(target, texture); +#ifdef DEBUG_BIND_TEXTURE + mBinds ++; +#endif + } +} + +inline void NormalOpenGLGraphics::drawQuadArrayfi(const int size) +{ + glVertexPointer(2, GL_INT, 0, mIntVertArray); + glTexCoordPointer(2, GL_FLOAT, 0, mFloatTexArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, size / 2); +} + +inline void NormalOpenGLGraphics::drawQuadArrayfi(const GLint *const + intVertArray, + const GLfloat *const + floatTexArray, + const int size) +{ + glVertexPointer(2, GL_INT, 0, intVertArray); + glTexCoordPointer(2, GL_FLOAT, 0, floatTexArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, size / 2); +} + +inline void NormalOpenGLGraphics::drawQuadArrayii(const int size) +{ + glVertexPointer(2, GL_INT, 0, mIntVertArray); + glTexCoordPointer(2, GL_INT, 0, mIntTexArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, size / 2); +} + +inline void NormalOpenGLGraphics::drawQuadArrayii(const GLint *const + intVertArray, + const GLint *const + intTexArray, + const int size) +{ + glVertexPointer(2, GL_INT, 0, intVertArray); + glTexCoordPointer(2, GL_INT, 0, intTexArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_QUADS, 0, size / 2); +} + +inline void NormalOpenGLGraphics::drawLineArrayi(const int size) +{ + glVertexPointer(2, GL_INT, 0, mIntVertArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_LINES, 0, size / 2); +} + +inline void NormalOpenGLGraphics::drawLineArrayf(const int size) +{ + glVertexPointer(2, GL_FLOAT, 0, mFloatTexArray); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_LINES, 0, size / 2); +} + +void NormalOpenGLGraphics::dumpSettings() +{ + GLint test[1000]; + logger->log("\n\n"); + logger->log("start opengl dump"); + for (int f = 0; f < 65535; f ++) + { + test[0] = 0; + test[1] = 0; + test[2] = 0; + test[3] = 0; + glGetIntegerv(f, &test[0]); + if (test[0] || test[1] || test[2] || test[3]) + { + logger->log("\n%d = %d, %d, %d, %d", f, + test[0], test[1], test[2], test[3]); + } + } +} + +void NormalOpenGLGraphics::setColorAlpha(const float alpha) +{ + if (!mIsByteColor && mFloatColor == alpha) + return; + + glColor4f(1.0f, 1.0f, 1.0f, alpha); + mIsByteColor = false; + mFloatColor = alpha; +} + +void NormalOpenGLGraphics::restoreColor() +{ + if (mIsByteColor && mByteColor == mColor) + return; + + glColor4ub(static_cast<GLubyte>(mColor.r), + static_cast<GLubyte>(mColor.g), + static_cast<GLubyte>(mColor.b), + static_cast<GLubyte>(mColor.a)); + mIsByteColor = true; + mByteColor = mColor; +} + +#ifdef DEBUG_BIND_TEXTURE +void NormalOpenGLGraphics::debugBindTexture(const Image *const image) +{ + const std::string texture = image->getIdPath(); + if (mOldTexture != texture) + { + if ((!mOldTexture.empty() || !texture.empty()) + && mOldTextureId != image->mGLImage) + { + logger->log("bind: %s (%d) to %s (%d)", mOldTexture.c_str(), + mOldTextureId, texture.c_str(), image->mGLImage); + } + mOldTextureId = image->mGLImage; + mOldTexture = texture; + } +} +#else +void NormalOpenGLGraphics::debugBindTexture(const Image *const image A_UNUSED) +{ +} +#endif + +#endif // USE_OPENGL diff --git a/src/render/normalopenglgraphics.h b/src/render/normalopenglgraphics.h new file mode 100644 index 000000000..2b489c985 --- /dev/null +++ b/src/render/normalopenglgraphics.h @@ -0,0 +1,244 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef NORMALOPENGLGRAPHICS_H +#define NORMALOPENGLGRAPHICS_H + +#include "main.h" +#if defined USE_OPENGL && !defined ANDROID + +#include "localconsts.h" +#include "render/graphics.h" + +#include "resources/fboinfo.h" + +#ifdef ANDROID +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <GLES2/gl2.h> +#else +#ifndef USE_SDL2 +#define GL_GLEXT_PROTOTYPES 1 +#endif +#include <SDL_opengl.h> +#include <GL/glext.h> +#endif + +#include <set> + +class NormalOpenGLGraphicsVertexes; + +class NormalOpenGLGraphics final : public Graphics +{ + public: + NormalOpenGLGraphics(); + + A_DELETE_COPY(NormalOpenGLGraphics) + + ~NormalOpenGLGraphics(); + + bool setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) override; + + + /** + * Draws a resclaled version of the image + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor) override; + + /** + * Used to get the smooth rescale option over the standard function. + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor, bool smooth); + + void drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) override; + + /** + * Draw a pattern based on a rescaled version of the given image... + */ + void drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) override; + + void calcImagePattern(ImageVertexes* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const override; + + void calcImagePattern(ImageCollection* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const override; + + void calcTile(ImageVertexes *const vert, const Image *const image, + int x, int y) const override; + + void calcTile(ImageCollection *const vertCol, + const Image *const image, int x, int y) override; + + void drawTile(const ImageCollection *const vertCol) override; + + void drawTile(const ImageVertexes *const vert) override; + + bool calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) override; + + void updateScreen() override; + + void _beginDraw(); + + void _endDraw(); + + bool pushClipArea(gcn::Rectangle area); + + void popClipArea(); + + void setColor(const gcn::Color &color) + { + mColor = color; + mColor2 = color; + mColorAlpha = (color.a != 255); + } + + void setColorAll(const gcn::Color &color, const gcn::Color &color2) + { + mColor = color; + mColor2 = color2; + mColorAlpha = (color.a != 255); + } + + void drawPoint(int x, int y); + + void drawLine(int x1, int y1, int x2, int y2); + + void drawRectangle(const gcn::Rectangle &rect, const bool filled); + + void drawRectangle(const gcn::Rectangle &rect); + + void fillRectangle(const gcn::Rectangle &rect); + + void setTargetPlane(int width, int height); + + inline void drawQuadArrayfi(const int size); + + inline void drawQuadArrayfi(const GLint *const intVertArray, + const GLfloat *const floatTexArray, + const int size); + + inline void drawQuadArrayii(const int size); + + inline void drawQuadArrayii(const GLint *const intVertArray, + const GLint *const intTexArray, + const int size); + + inline void drawLineArrayi(const int size); + + inline void drawLineArrayf(const int size); + + inline void drawVertexes(const NormalOpenGLGraphicsVertexes &ogl); + + void initArrays() override; + + static void dumpSettings(); + + /** + * Takes a screenshot and returns it as SDL surface. + */ + SDL_Surface *getScreenshot() override A_WARN_UNUSED; + + void prepareScreenshot() override; + + bool drawNet(const int x1, const int y1, const int x2, const int y2, + const int width, const int height) override; + + int getMemoryUsage() A_WARN_UNUSED; + + void updateTextureFormat(); + +#ifdef DEBUG_DRAW_CALLS + virtual unsigned int getDrawCalls() const + { return mLastDrawCalls; } + + static unsigned int mDrawCalls; + + static unsigned int mLastDrawCalls; +#endif +#ifdef DEBUG_BIND_TEXTURE + virtual unsigned int getBinds() const + { return mLastBinds; } +#endif + static void bindTexture(const GLenum target, const GLuint texture); + + static GLuint mLastImage; + + protected: + bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) override; + + void setTexturingAndBlending(const bool enable); + + void debugBindTexture(const Image *const image); + + private: + void inline setColorAlpha(float alpha); + + void inline restoreColor(); + + GLfloat *mFloatTexArray; + GLint *mIntTexArray; + GLint *mIntVertArray; + bool mTexture; + + bool mIsByteColor; + gcn::Color mByteColor; + float mFloatColor; + int mMaxVertices; + bool mColorAlpha; +#ifdef DEBUG_BIND_TEXTURE + std::string mOldTexture; + unsigned int mOldTextureId; + static unsigned int mBinds; + static unsigned int mLastBinds; +#endif + FBOInfo mFbo; +}; +#endif + +#endif // NORMALOPENGLGRAPHICS_H diff --git a/src/render/nullopenglgraphics.cpp b/src/render/nullopenglgraphics.cpp new file mode 100644 index 000000000..8030353eb --- /dev/null +++ b/src/render/nullopenglgraphics.cpp @@ -0,0 +1,1208 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "main.h" +#if defined USE_OPENGL + +#include "render/nullopenglgraphics.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/image.h" +#include "resources/openglimagehelper.h" + +#include "utils/stringutils.h" + +#include <SDL.h> + +#include "debug.h" + +GLuint NullOpenGLGraphics::mLastImage = 0; +#ifdef DEBUG_DRAW_CALLS +unsigned int NullOpenGLGraphics::mDrawCalls = 0; +unsigned int NullOpenGLGraphics::mLastDrawCalls = 0; +#endif + +NullOpenGLGraphics::NullOpenGLGraphics(): + mFloatTexArray(nullptr), + mIntTexArray(nullptr), + mIntVertArray(nullptr), + mTexture(false), + mIsByteColor(false), + mByteColor(), + mFloatColor(1.0f), + mMaxVertices(500), + mColorAlpha(false), +#ifdef DEBUG_BIND_TEXTURE + mOldTexture(), + mOldTextureId(0), +#endif + mFbo() +{ + mOpenGL = 100; + mName = "null OpenGL"; +} + +NullOpenGLGraphics::~NullOpenGLGraphics() +{ + delete [] mFloatTexArray; + delete [] mIntTexArray; + delete [] mIntVertArray; +} + +void NullOpenGLGraphics::initArrays() +{ + mMaxVertices = graphicsManager.getMaxVertices(); + if (mMaxVertices < 500) + mMaxVertices = 500; + else if (mMaxVertices > 1024) + mMaxVertices = 1024; + + // need alocate small size, after if limit reached reallocate to double size + vertexBufSize = mMaxVertices; + const int sz = mMaxVertices * 4 + 30; + mFloatTexArray = new GLfloat[sz]; + mIntTexArray = new GLint[sz]; + mIntVertArray = new GLint[sz]; +} + +bool NullOpenGLGraphics::setVideoMode(const int w, const int h, + const int bpp, const bool fs, + const bool hwaccel, const bool resize, + const bool noFrame) +{ + setMainFlags(w, h, bpp, fs, hwaccel, resize, noFrame); + + return setOpenGLMode(); +} + +static inline void drawQuad(const Image *const image A_UNUSED, + const int srcX A_UNUSED, const int srcY A_UNUSED, + const int dstX A_UNUSED, const int dstY A_UNUSED, + const int width A_UNUSED, + const int height A_UNUSED) +{ + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { +#ifdef DEBUG_DRAW_CALLS + NullOpenGLGraphics::mDrawCalls ++; +#endif + } + else + { +#ifdef DEBUG_DRAW_CALLS + NullOpenGLGraphics::mDrawCalls ++; +#endif + } +} + +static inline void drawRescaledQuad(const Image *const image A_UNUSED, + const int srcX A_UNUSED, + const int srcY A_UNUSED, + const int dstX A_UNUSED, + const int dstY A_UNUSED, + const int width A_UNUSED, + const int height A_UNUSED, + const int desiredWidth A_UNUSED, + const int desiredHeight A_UNUSED) +{ + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { +#ifdef DEBUG_DRAW_CALLS + NullOpenGLGraphics::mDrawCalls ++; +#endif + } + else + { +#ifdef DEBUG_DRAW_CALLS + NullOpenGLGraphics::mDrawCalls ++; +#endif + } +} + + +bool NullOpenGLGraphics::drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) +{ + FUNC_BLOCK("Graphics::drawImage2", 1) + if (!image) + return false; + + const SDL_Rect &imageRect = image->mBounds; + srcX += imageRect.x; + srcY += imageRect.y; + + if (!useColor) + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + drawQuad(image, srcX, srcY, dstX, dstY, width, height); + + return true; +} + +bool NullOpenGLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor) +{ + return drawRescaledImage(image, srcX, srcY, + dstX, dstY, + width, height, + desiredWidth, desiredHeight, + useColor, true); +} + +bool NullOpenGLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor, + bool smooth) +{ + FUNC_BLOCK("Graphics::drawRescaledImage", 1) + if (!image) + return false; + + // Just draw the image normally when no resizing is necessary, + if (width == desiredWidth && height == desiredHeight) + { + return drawImage2(image, srcX, srcY, dstX, dstY, + width, height, useColor); + } + + // When the desired image is smaller than the current one, + // disable smooth effect. + if (width > desiredWidth && height > desiredHeight) + smooth = false; + + const SDL_Rect &imageRect = image->mBounds; + srcX += imageRect.x; + srcY += imageRect.y; + + if (!useColor) + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + // Draw a textured quad. + drawRescaledQuad(image, srcX, srcY, dstX, dstY, width, height, + desiredWidth, desiredHeight); + + if (smooth) // A basic smooth effect... + { + setColorAlpha(0.2f); + drawRescaledQuad(image, srcX, srcY, dstX - 1, dstY - 1, width, height, + desiredWidth + 1, desiredHeight + 1); + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY + 1, width, height, + desiredWidth - 1, desiredHeight - 1); + + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY, width, height, + desiredWidth - 1, desiredHeight); + drawRescaledQuad(image, srcX, srcY, dstX, dstY + 1, width, height, + desiredWidth, desiredHeight - 1); + } + + return true; +} + +void NullOpenGLGraphics::drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) +{ + FUNC_BLOCK("Graphics::drawImagePattern", 1) + if (!image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const int iw = imageRect.w; + const int ih = imageRect.h; + + if (iw == 0 || ih == 0) + return; + + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + const float texY2 = static_cast<float>(srcY + height) / th; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + + const float texX2 = static_cast<float>(srcX + width) / tw; + + mFloatTexArray[vp + 0] = texX1; + mFloatTexArray[vp + 1] = texY1; + + mFloatTexArray[vp + 2] = texX2; + mFloatTexArray[vp + 3] = texY1; + + mFloatTexArray[vp + 4] = texX2; + mFloatTexArray[vp + 5] = texY2; + + mFloatTexArray[vp + 6] = texX1; + mFloatTexArray[vp + 7] = texY2; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayfi(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayfi(vp); + } + else + { + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + + mIntTexArray[vp + 0] = srcX; + mIntTexArray[vp + 1] = srcY; + + mIntTexArray[vp + 2] = srcX + width; + mIntTexArray[vp + 3] = srcY; + + mIntTexArray[vp + 4] = srcX + width; + mIntTexArray[vp + 5] = srcY + height; + + mIntTexArray[vp + 6] = srcX; + mIntTexArray[vp + 7] = srcY + height; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayii(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayii(vp); + } +} + +void NullOpenGLGraphics::drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) +{ + if (!image) + return; + + if (scaledWidth == 0 || scaledHeight == 0) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int iw = imageRect.w; + const int ih = imageRect.h; + if (iw == 0 || ih == 0) + return; + + const int srcX = imageRect.x; + const int srcY = imageRect.y; + + setColorAlpha(image->mAlpha); + +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + const float tFractionW = iw / tw; + const float tFractionH = ih / th; + + for (int py = 0; py < h; py += scaledHeight) + { + const int height = (py + scaledHeight >= h) + ? h - py : scaledHeight; + const int dstY = y + py; + const float visibleFractionH = static_cast<float>(height) + / scaledHeight; + const float texY2 = texY1 + tFractionH * visibleFractionH; + for (int px = 0; px < w; px += scaledWidth) + { + const int width = (px + scaledWidth >= w) + ? w - px : scaledWidth; + const int dstX = x + px; + const float visibleFractionW = static_cast<float>(width) + / scaledWidth; + const float texX2 = texX1 + tFractionW * visibleFractionW; + + mFloatTexArray[vp + 0] = texX1; + mFloatTexArray[vp + 1] = texY1; + + mFloatTexArray[vp + 2] = texX2; + mFloatTexArray[vp + 3] = texY1; + + mFloatTexArray[vp + 4] = texX2; + mFloatTexArray[vp + 5] = texY2; + + mFloatTexArray[vp + 6] = texX1; + mFloatTexArray[vp + 7] = texY2; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayfi(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayfi(vp); + } + else + { + const float scaleFactorW = static_cast<float>(scaledWidth) / iw; + const float scaleFactorH = static_cast<float>(scaledHeight) / ih; + + for (int py = 0; py < h; py += scaledHeight) + { + const int height = (py + scaledHeight >= h) + ? h - py : scaledHeight; + const int dstY = y + py; + const int scaledY = srcY + height / scaleFactorH; + for (int px = 0; px < w; px += scaledWidth) + { + const int width = (px + scaledWidth >= w) + ? w - px : scaledWidth; + const int dstX = x + px; + const int scaledX = srcX + width / scaleFactorW; + + mIntTexArray[vp + 0] = srcX; + mIntTexArray[vp + 1] = srcY; + + mIntTexArray[vp + 2] = scaledX; + mIntTexArray[vp + 3] = srcY; + + mIntTexArray[vp + 4] = scaledX; + mIntTexArray[vp + 5] = scaledY; + + mIntTexArray[vp + 6] = srcX; + mIntTexArray[vp + 7] = scaledY; + + mIntVertArray[vp + 0] = dstX; + mIntVertArray[vp + 1] = dstY; + + mIntVertArray[vp + 2] = dstX + width; + mIntVertArray[vp + 3] = dstY; + + mIntVertArray[vp + 4] = dstX + width; + mIntVertArray[vp + 5] = dstY + height; + + mIntVertArray[vp + 6] = dstX; + mIntVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + drawQuadArrayii(vp); + vp = 0; + } + } + } + if (vp > 0) + drawQuadArrayii(vp); + } +} + +inline void NullOpenGLGraphics::drawVertexes(const + NormalOpenGLGraphicsVertexes + &ogl) +{ + const std::vector<GLint*> &intVertPool = ogl.mIntVertPool; + std::vector<GLint*>::const_iterator iv; + const std::vector<GLint*>::const_iterator iv_end = intVertPool.end(); + const std::vector<int> &vp = ogl.mVp; + std::vector<int>::const_iterator ivp; + const std::vector<int>::const_iterator ivp_end = vp.end(); + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const std::vector<GLfloat*> &floatTexPool = ogl.mFloatTexPool; + std::vector<GLfloat*>::const_iterator ft; + const std::vector<GLfloat*>::const_iterator + ft_end = floatTexPool.end(); + + for (iv = intVertPool.begin(), ft = floatTexPool.begin(), + ivp = vp.begin(); + iv != iv_end && ft != ft_end && ivp != ivp_end; + ++ iv, ++ ft, ++ ivp) + { + drawQuadArrayfi(*iv, *ft, *ivp); + } + } + else + { + const std::vector<GLint*> &intTexPool = ogl.mIntTexPool; + std::vector<GLint*>::const_iterator it; + const std::vector<GLint*>::const_iterator it_end = intTexPool.end(); + + for (iv = intVertPool.begin(), it = intTexPool.begin(), + ivp = vp.begin(); + iv != iv_end && it != it_end && ivp != ivp_end; + ++ iv, ++ it, ++ ivp) + { + drawQuadArrayii(*iv, *it, *ivp); + } + } +} + +void NullOpenGLGraphics::calcImagePattern(ImageVertexes* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + if (!image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int iw = imageRect.w; + const int ih = imageRect.h; + + if (iw == 0 || ih == 0) + return; + + const int srcX = imageRect.x; + const int srcY = imageRect.y; + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const unsigned int vLimit = mMaxVertices * 4; + + NormalOpenGLGraphicsVertexes &ogl = vert->ogl; + unsigned int vp = ogl.continueVp(); + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + GLfloat *floatTexArray = ogl.continueFloatTexArray(); + GLint *intVertArray = ogl.continueIntVertArray(); + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + const float texY2 = static_cast<float>(srcY + height) / th; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + const float texX2 = static_cast<float>(srcX + width) / tw; + + floatTexArray[vp + 0] = texX1; + floatTexArray[vp + 1] = texY1; + + floatTexArray[vp + 2] = texX2; + floatTexArray[vp + 3] = texY1; + + floatTexArray[vp + 4] = texX2; + floatTexArray[vp + 5] = texY2; + + floatTexArray[vp + 6] = texX1; + floatTexArray[vp + 7] = texY2; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + width; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + width; + intVertArray[vp + 5] = dstY + height; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + floatTexArray = ogl.switchFloatTexArray(); + intVertArray = ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + } + } + else + { + GLint *intTexArray = ogl.continueIntTexArray(); + GLint *intVertArray = ogl.continueIntVertArray(); + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + for (int px = 0; px < w; px += iw) + { + const int width = (px + iw >= w) ? w - px : iw; + const int dstX = x + px; + + intTexArray[vp + 0] = srcX; + intTexArray[vp + 1] = srcY; + + intTexArray[vp + 2] = srcX + width; + intTexArray[vp + 3] = srcY; + + intTexArray[vp + 4] = srcX + width; + intTexArray[vp + 5] = srcY + height; + + intTexArray[vp + 6] = srcX; + intTexArray[vp + 7] = srcY + height; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + width; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + width; + intVertArray[vp + 5] = dstY + height; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + height; + + vp += 8; + if (vp >= vLimit) + { + intTexArray = ogl.switchIntTexArray(); + intVertArray = ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + } + } + ogl.switchVp(vp); +} + +void NullOpenGLGraphics::calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y) +{ + if (vertCol->currentGLImage != image->mGLImage) + { + ImageVertexes *const vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + calcTile(vert, image, x, y); + } + else + { + calcTile(vertCol->currentVert, image, x, y); + } +} + +void NullOpenGLGraphics::drawTile(const ImageCollection *const vertCol) +{ + const ImageVertexesVector &draws = vertCol->draws; + const ImageCollectionCIter it_end = draws.end(); + for (ImageCollectionCIter it = draws.begin(); it != it_end; ++ it) + { + const ImageVertexes *const vert = *it; + const Image *const image = vert->image; + + setColorAlpha(image->mAlpha); +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + drawVertexes(vert->ogl); + } +} + +void NullOpenGLGraphics::calcImagePattern(ImageCollection* const vertCol, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + ImageVertexes *vert = nullptr; + if (vertCol->currentGLImage != image->mGLImage) + { + vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + calcImagePattern(vert, image, x, y, w, h); +} + +void NullOpenGLGraphics::calcTile(ImageVertexes *const vert, + const Image *const image, + int dstX, int dstY) const +{ + if (!vert || !image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int w = imageRect.w; + const int h = imageRect.h; + + if (w == 0 || h == 0) + return; + + const int srcX = imageRect.x; + const int srcY = imageRect.y; + + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + + const unsigned int vLimit = mMaxVertices * 4; + + NormalOpenGLGraphicsVertexes &ogl = vert->ogl; + +// std::vector<int> *vps = ogl.getVp(); + unsigned int vp = ogl.continueVp(); + + // Draw a set of textured rectangles + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + + const float texX2 = static_cast<float>(srcX + w) / tw; + const float texY2 = static_cast<float>(srcY + h) / th; + + GLfloat *const floatTexArray = ogl.continueFloatTexArray(); + GLint *const intVertArray = ogl.continueIntVertArray(); + + floatTexArray[vp + 0] = texX1; + floatTexArray[vp + 1] = texY1; + + floatTexArray[vp + 2] = texX2; + floatTexArray[vp + 3] = texY1; + + floatTexArray[vp + 4] = texX2; + floatTexArray[vp + 5] = texY2; + + floatTexArray[vp + 6] = texX1; + floatTexArray[vp + 7] = texY2; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + w; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + w; + intVertArray[vp + 5] = dstY + h; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + h; + + vp += 8; + if (vp >= vLimit) + { + ogl.switchFloatTexArray(); + ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + else + { + GLint *const intTexArray = ogl.continueIntTexArray(); + GLint *const intVertArray = ogl.continueIntVertArray(); + + intTexArray[vp + 0] = srcX; + intTexArray[vp + 1] = srcY; + + intTexArray[vp + 2] = srcX + w; + intTexArray[vp + 3] = srcY; + + intTexArray[vp + 4] = srcX + w; + intTexArray[vp + 5] = srcY + h; + + intTexArray[vp + 6] = srcX; + intTexArray[vp + 7] = srcY + h; + + intVertArray[vp + 0] = dstX; + intVertArray[vp + 1] = dstY; + + intVertArray[vp + 2] = dstX + w; + intVertArray[vp + 3] = dstY; + + intVertArray[vp + 4] = dstX + w; + intVertArray[vp + 5] = dstY + h; + + intVertArray[vp + 6] = dstX; + intVertArray[vp + 7] = dstY + h; + + vp += 8; + if (vp >= vLimit) + { + ogl.switchIntTexArray(); + ogl.switchIntVertArray(); + ogl.switchVp(vp); + vp = 0; + } + } + ogl.switchVp(vp); +} + +void NullOpenGLGraphics::drawTile(const ImageVertexes *const vert) +{ + if (!vert) + return; + const Image *const image = vert->image; + + setColorAlpha(image->mAlpha); +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + drawVertexes(vert->ogl); +} + +bool NullOpenGLGraphics::calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) +{ + ImageVertexes *vert = nullptr; + Image *const image = imgRect.grid[4]; + if (!image) + return false; + if (vertCol->currentGLImage != image->mGLImage) + { + vert = new ImageVertexes(); + vertCol->currentGLImage = image->mGLImage; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + return calcImageRect(vert, x, y, w, h, + imgRect.grid[0], imgRect.grid[2], imgRect.grid[6], imgRect.grid[8], + imgRect.grid[1], imgRect.grid[5], imgRect.grid[7], imgRect.grid[3], + imgRect.grid[4]); +} + +void NullOpenGLGraphics::updateScreen() +{ + BLOCK_START("Graphics::updateScreen") +#ifdef DEBUG_DRAW_CALLS + mLastDrawCalls = mDrawCalls; + mDrawCalls = 0; +#endif + BLOCK_END("Graphics::updateScreen") +} + +void NullOpenGLGraphics::_beginDraw() +{ + pushClipArea(gcn::Rectangle(0, 0, 640, 480)); +} + +void NullOpenGLGraphics::_endDraw() +{ + popClipArea(); +} + +void NullOpenGLGraphics::prepareScreenshot() +{ +} + +SDL_Surface* NullOpenGLGraphics::getScreenshot() +{ + return nullptr; +} + +bool NullOpenGLGraphics::pushClipArea(gcn::Rectangle area) +{ + int transX = 0; + int transY = 0; + + if (!mClipStack.empty()) + { + const gcn::ClipRectangle &clipArea = mClipStack.top(); + transX = -clipArea.xOffset; + transY = -clipArea.yOffset; + } + + const bool result = gcn::Graphics::pushClipArea(area); + + const gcn::ClipRectangle &clipArea = mClipStack.top(); + transX += clipArea.xOffset; + transY += clipArea.yOffset; + + return result; +} + +void NullOpenGLGraphics::popClipArea() +{ + gcn::Graphics::popClipArea(); + + if (mClipStack.empty()) + return; +} + +void NullOpenGLGraphics::drawPoint(int x A_UNUSED, int y A_UNUSED) +{ + setTexturingAndBlending(false); + restoreColor(); +} + +void NullOpenGLGraphics::drawLine(int x1, int y1, + int x2, int y2) +{ + setTexturingAndBlending(false); + restoreColor(); + + mFloatTexArray[0] = static_cast<float>(x1) + 0.5f; + mFloatTexArray[1] = static_cast<float>(y1) + 0.5f; + mFloatTexArray[2] = static_cast<float>(x2) + 0.5f; + mFloatTexArray[3] = static_cast<float>(y2) + 0.5f; + + drawLineArrayf(4); +} + +void NullOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, false); +} + +void NullOpenGLGraphics::fillRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, true); +} + +void NullOpenGLGraphics::setTargetPlane(int width A_UNUSED, + int height A_UNUSED) +{ +} + +void NullOpenGLGraphics::setTexturingAndBlending(const bool enable) +{ + if (enable) + { + if (!mTexture) + mTexture = true; + + if (!mAlpha) + mAlpha = true; + } + else + { + mLastImage = 0; + if (mAlpha && !mColorAlpha) + mAlpha = false; + else if (!mAlpha && mColorAlpha) + mAlpha = true; + + if (mTexture) + mTexture = false; + } +} + +void NullOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect A_UNUSED, + const bool filled A_UNUSED) +{ + BLOCK_START("Graphics::drawRectangle") + setTexturingAndBlending(false); + restoreColor(); + +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + BLOCK_END("Graphics::drawRectangle") +} + +bool NullOpenGLGraphics::drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height) +{ + unsigned int vp = 0; + const unsigned int vLimit = mMaxVertices * 4; + + setTexturingAndBlending(false); + restoreColor(); + + const float xf1 = static_cast<float>(x1); + const float xf2 = static_cast<float>(x2); + const float yf1 = static_cast<float>(y1); + const float yf2 = static_cast<float>(y2); + + for (int y = y1; y < y2; y += height) + { + mFloatTexArray[vp + 0] = xf1; + mFloatTexArray[vp + 1] = static_cast<float>(y); + + mFloatTexArray[vp + 2] = xf2; + mFloatTexArray[vp + 3] = static_cast<float>(y); + + vp += 4; + if (vp >= vLimit) + { + drawLineArrayf(vp); + vp = 0; + } + } + + for (int x = x1; x < x2; x += width) + { + mFloatTexArray[vp + 0] = static_cast<float>(x); + mFloatTexArray[vp + 1] = yf1; + + mFloatTexArray[vp + 2] = static_cast<float>(x); + mFloatTexArray[vp + 3] = yf2; + + vp += 4; + if (vp >= vLimit) + { + drawLineArrayf(vp); + vp = 0; + } + } + + if (vp > 0) + drawLineArrayf(vp); + + return true; +} + +void NullOpenGLGraphics::bindTexture(const GLenum target A_UNUSED, + const GLuint texture) +{ + if (mLastImage != texture) + mLastImage = texture; +} + +inline void NullOpenGLGraphics::drawQuadArrayfi(const int size A_UNUSED) +{ +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif +} + +inline void NullOpenGLGraphics::drawQuadArrayfi(const GLint *const + intVertArray A_UNUSED, + const GLfloat *const + floatTexArray A_UNUSED, + const int size A_UNUSED) +{ +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif +} + +inline void NullOpenGLGraphics::drawQuadArrayii(const int size A_UNUSED) +{ +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif +} + +inline void NullOpenGLGraphics::drawQuadArrayii(const GLint *const + intVertArray A_UNUSED, + const GLint *const + intTexArray A_UNUSED, + const int size A_UNUSED) +{ +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif +} + +inline void NullOpenGLGraphics::drawLineArrayi(const int size A_UNUSED) +{ +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif +} + +inline void NullOpenGLGraphics::drawLineArrayf(const int size A_UNUSED) +{ +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif +} + +void NullOpenGLGraphics::dumpSettings() +{ +} + +void NullOpenGLGraphics::setColorAlpha(const float alpha) +{ + if (!mIsByteColor && mFloatColor == alpha) + return; + + mIsByteColor = false; + mFloatColor = alpha; +} + +void NullOpenGLGraphics::restoreColor() +{ + if (mIsByteColor && mByteColor == mColor) + return; + + mIsByteColor = true; + mByteColor = mColor; +} + +#ifdef DEBUG_BIND_TEXTURE +void NullOpenGLGraphics::debugBindTexture(const Image *const image) +{ + const std::string texture = image->getIdPath(); + if (mOldTexture != texture) + { + if ((!mOldTexture.empty() || !texture.empty()) + && mOldTextureId != image->mGLImage) + { + logger->log("bind: %s (%d) to %s (%d)", mOldTexture.c_str(), + mOldTextureId, texture.c_str(), image->mGLImage); + } + mOldTextureId = image->mGLImage; + mOldTexture = texture; + } +} +#else +void NullOpenGLGraphics::debugBindTexture(const Image *const image A_UNUSED) +{ +} +#endif + +#endif // USE_OPENGL diff --git a/src/render/nullopenglgraphics.h b/src/render/nullopenglgraphics.h new file mode 100644 index 000000000..856b646bc --- /dev/null +++ b/src/render/nullopenglgraphics.h @@ -0,0 +1,239 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef NULLOPENGLGRAPHICS_H +#define NULLOPENGLGRAPHICS_H + +#include "main.h" +#if defined USE_OPENGL + +#include "localconsts.h" +#include "render/graphics.h" + +#include "resources/fboinfo.h" + +#ifdef ANDROID +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <GLES2/gl2.h> +#else +#ifndef USE_SDL2 +#define GL_GLEXT_PROTOTYPES 1 +#endif +#include <SDL_opengl.h> +#include <GL/glext.h> +#endif + +#include <set> + +class NormalOpenGLGraphicsVertexes; + +class NullOpenGLGraphics final : public Graphics +{ + public: + NullOpenGLGraphics(); + + A_DELETE_COPY(NullOpenGLGraphics) + + ~NullOpenGLGraphics(); + + bool setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) override; + + + /** + * Draws a resclaled version of the image + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor) override; + + /** + * Used to get the smooth rescale option over the standard function. + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor, bool smooth); + + void drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) override; + + /** + * Draw a pattern based on a rescaled version of the given image... + */ + void drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) override; + + void calcImagePattern(ImageVertexes* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const override; + + void calcImagePattern(ImageCollection* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const override; + + void calcTile(ImageVertexes *const vert, const Image *const image, + int x, int y) const override; + + void calcTile(ImageCollection *const vertCol, + const Image *const image, int x, int y) override; + + void drawTile(const ImageCollection *const vertCol) override; + + void drawTile(const ImageVertexes *const vert) override; + + bool calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) override; + + void updateScreen() override; + + void _beginDraw(); + + void _endDraw(); + + bool pushClipArea(gcn::Rectangle area); + + void popClipArea(); + + void setColor(const gcn::Color &color) + { + mColor = color; + mColor2 = color; + mColorAlpha = (color.a != 255); + } + + void setColorAll(const gcn::Color &color, const gcn::Color &color2) + { + mColor = color; + mColor2 = color2; + mColorAlpha = (color.a != 255); + } + + void drawPoint(int x, int y); + + void drawLine(int x1, int y1, int x2, int y2); + + void drawRectangle(const gcn::Rectangle &rect, const bool filled); + + void drawRectangle(const gcn::Rectangle &rect); + + void fillRectangle(const gcn::Rectangle &rect); + + void setTargetPlane(int width, int height); + + inline void drawQuadArrayfi(const int size); + + inline void drawQuadArrayfi(const GLint *const intVertArray, + const GLfloat *const floatTexArray, + const int size); + + inline void drawQuadArrayii(const int size); + + inline void drawQuadArrayii(const GLint *const intVertArray, + const GLint *const intTexArray, + const int size); + + inline void drawLineArrayi(const int size); + + inline void drawLineArrayf(const int size); + + inline void drawVertexes(const NormalOpenGLGraphicsVertexes &ogl); + + void initArrays() override; + + static void dumpSettings(); + + /** + * Takes a screenshot and returns it as SDL surface. + */ + SDL_Surface *getScreenshot() override A_WARN_UNUSED; + + void prepareScreenshot() override; + + bool drawNet(const int x1, const int y1, const int x2, const int y2, + const int width, const int height) override; + + int getMemoryUsage() A_WARN_UNUSED; + + void updateTextureFormat(); + +#ifdef DEBUG_DRAW_CALLS + virtual unsigned int getDrawCalls() const + { return mLastDrawCalls; } + + static unsigned int mDrawCalls; + + static unsigned int mLastDrawCalls; +#endif + + static void bindTexture(const GLenum target, const GLuint texture); + + static GLuint mLastImage; + + protected: + bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) override; + + void setTexturingAndBlending(const bool enable); + + void debugBindTexture(const Image *const image); + + private: + void inline setColorAlpha(float alpha); + + void inline restoreColor(); + + GLfloat *mFloatTexArray; + GLint *mIntTexArray; + GLint *mIntVertArray; + bool mTexture; + + bool mIsByteColor; + gcn::Color mByteColor; + float mFloatColor; + int mMaxVertices; + bool mColorAlpha; +#ifdef DEBUG_BIND_TEXTURE + std::string mOldTexture; + unsigned mOldTextureId; +#endif + FBOInfo mFbo; +}; +#endif + +#endif // NULLOPENGLGRAPHICS_H diff --git a/src/render/safeopenglgraphics.cpp b/src/render/safeopenglgraphics.cpp new file mode 100644 index 000000000..1c7130475 --- /dev/null +++ b/src/render/safeopenglgraphics.cpp @@ -0,0 +1,654 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "main.h" + +#if defined USE_OPENGL && !defined ANDROID +#include "render/safeopenglgraphics.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/image.h" +#include "resources/openglimagehelper.h" + +#include "utils/sdlcheckutils.h" + +#include <SDL.h> + +#include "debug.h" + +GLuint SafeOpenGLGraphics::mLastImage = 0; + +SafeOpenGLGraphics::SafeOpenGLGraphics(): + mTexture(false), + mIsByteColor(false), + mByteColor(), + mFloatColor(1.0f), + mColorAlpha(false), + mFbo() +{ + mOpenGL = 2; + mName = "safe OpenGL"; +} + +SafeOpenGLGraphics::~SafeOpenGLGraphics() +{ +} + +bool SafeOpenGLGraphics::setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) +{ + setMainFlags(w, h, bpp, fs, hwaccel, resize, noFrame); + + return setOpenGLMode(); +} + +static inline void drawQuad(const Image *image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height) +{ + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + // Find OpenGL normalized texture coordinates. + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + const float texX2 = static_cast<float>(srcX + width) / tw; + const float texY2 = static_cast<float>(srcY + height) / th; + + glTexCoord2f(texX1, texY1); + glVertex2i(dstX, dstY); + glTexCoord2f(texX2, texY1); + glVertex2i(dstX + width, dstY); + glTexCoord2f(texX2, texY2); + glVertex2i(dstX + width, dstY + height); + glTexCoord2f(texX1, texY2); + glVertex2i(dstX, dstY + height); + } + else + { + glTexCoord2i(srcX, srcY); + glVertex2i(dstX, dstY); + glTexCoord2i(srcX + width, srcY); + glVertex2i(dstX + width, dstY); + glTexCoord2i(srcX + width, srcY + height); + glVertex2i(dstX + width, dstY + height); + glTexCoord2i(srcX, srcY + height); + glVertex2i(dstX, dstY + height); + } +} + +static inline void drawRescaledQuad(const Image *const image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight) +{ + if (OpenGLImageHelper::mTextureType == GL_TEXTURE_2D) + { + const float tw = static_cast<float>(image->mTexWidth); + const float th = static_cast<float>(image->mTexHeight); + // Find OpenGL normalized texture coordinates. + const float texX1 = static_cast<float>(srcX) / tw; + const float texY1 = static_cast<float>(srcY) / th; + const float texX2 = static_cast<float>(srcX + width) / tw; + const float texY2 = static_cast<float>(srcY + height) / th; + + glTexCoord2f(texX1, texY1); + glVertex2i(dstX, dstY); + glTexCoord2f(texX2, texY1); + glVertex2i(dstX + desiredWidth, dstY); + glTexCoord2f(texX2, texY2); + glVertex2i(dstX + desiredWidth, dstY + desiredHeight); + glTexCoord2f(texX1, texY2); + glVertex2i(dstX, dstY + desiredHeight); + } + else + { + glTexCoord2i(srcX, srcY); + glVertex2i(dstX, dstY); + glTexCoord2i(srcX + width, srcY); + glVertex2i(dstX + desiredWidth, dstY); + glTexCoord2i(srcX + width, srcY + height); + glVertex2i(dstX + desiredWidth, dstY + desiredHeight); + glTexCoord2i(srcX, srcY + height); + glVertex2i(dstX, dstY + desiredHeight); + } +} + + +bool SafeOpenGLGraphics::drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor) +{ + FUNC_BLOCK("Graphics::drawImage2", 1) + if (!image) + return false; + + if (!useColor) + setColorAlpha(image->mAlpha); + + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + // Draw a textured quad. + glBegin(GL_QUADS); + drawQuad(image, srcX + image->mBounds.x, srcY + image->mBounds.y, + dstX, dstY, width, height); + glEnd(); + + return true; +} + +bool SafeOpenGLGraphics::drawRescaledImage(const Image *const image, int srcX, + int srcY, int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor) +{ + return drawRescaledImage(image, srcX, srcY, + dstX, dstY, + width, height, + desiredWidth, desiredHeight, + useColor, true); +} + +bool SafeOpenGLGraphics::drawRescaledImage(const Image *const image, int srcX, + int srcY, int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor, + bool smooth) +{ + FUNC_BLOCK("Graphics::drawRescaledImage", 1) + if (!image) + return false; + + // Just draw the image normally when no resizing is necessary, + if (width == desiredWidth && height == desiredHeight) + { + return drawImage2(image, srcX, srcY, dstX, dstY, + width, height, useColor); + } + + // When the desired image is smaller than the current one, + // disable smooth effect. + if (width > desiredWidth && height > desiredHeight) + smooth = false; + + srcX += image->mBounds.x; + srcY += image->mBounds.y; + + if (!useColor) + setColorAlpha(image->mAlpha); + + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + // Draw a textured quad. + glBegin(GL_QUADS); + drawRescaledQuad(image, srcX, srcY, dstX, dstY, width, height, + desiredWidth, desiredHeight); + + if (smooth) // A basic smooth effect... + { + setColorAlpha(0.2f); + drawRescaledQuad(image, srcX, srcY, dstX - 1, dstY - 1, width, height, + desiredWidth + 1, desiredHeight + 1); + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY + 1, width, height, + desiredWidth - 1, desiredHeight - 1); + + drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY, width, height, + desiredWidth - 1, desiredHeight); + drawRescaledQuad(image, srcX, srcY, dstX, dstY + 1, width, height, + desiredWidth, desiredHeight - 1); + } + + glEnd(); + + return true; +} + +/* Optimising the functions that Graphics::drawImagePattern would call, + * so that glBegin...glEnd are outside the main loop. */ +void SafeOpenGLGraphics::drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) +{ + FUNC_BLOCK("Graphics::drawImagePattern", 1) + if (!image) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int iw = imageRect.w; + const int ih = imageRect.h; + if (iw == 0 || ih == 0) + return; + + const int srcX = imageRect.x; + const int srcY = imageRect.y; + + setColorAlpha(image->mAlpha); + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + + // Draw a set of textured rectangles + glBegin(GL_QUADS); + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + for (int px = 0; px < w; px += iw) + { + int width = (px + iw >= w) ? w - px : iw; + int dstX = x + px; + drawQuad(image, srcX, srcY, dstX, dstY, width, height); + } + } + + glEnd(); +} + +void SafeOpenGLGraphics::drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) +{ + if (!image) + return; + + const int iw = scaledWidth; + const int ih = scaledHeight; + if (iw == 0 || ih == 0) + return; + + const SDL_Rect &imageRect = image->mBounds; + const int srcX = imageRect.x; + const int srcY = imageRect.y; + + setColorAlpha(image->mAlpha); + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + + // Draw a set of textured rectangles + glBegin(GL_QUADS); + + const float scaleFactorW = static_cast<float>(scaledWidth) + / image->getWidth(); + const float scaleFactorH = static_cast<float>(scaledHeight) + / image->getHeight(); + + for (int py = 0; py < h; py += ih) + { + const int height = (py + ih >= h) ? h - py : ih; + const int dstY = y + py; + for (int px = 0; px < w; px += iw) + { + int width = (px + iw >= w) ? w - px : iw; + int dstX = x + px; + + drawRescaledQuad(image, srcX, srcY, dstX, dstY, + width / scaleFactorW, height / scaleFactorH, + scaledWidth, scaledHeight); + } + } + + glEnd(); +} + +void SafeOpenGLGraphics::calcTile(ImageCollection *const vertCol A_UNUSED, + const Image *const image A_UNUSED, + int x A_UNUSED, int y A_UNUSED) +{ +} + +void SafeOpenGLGraphics::calcTile(ImageVertexes *const vert A_UNUSED, + const Image *const image A_UNUSED, + int x A_UNUSED, int y A_UNUSED) const +{ +} + +void SafeOpenGLGraphics::calcImagePattern(ImageVertexes *const vert A_UNUSED, + const Image *const image A_UNUSED, + const int x A_UNUSED, + const int y A_UNUSED, + const int w A_UNUSED, + const int h A_UNUSED) const +{ +} + +void SafeOpenGLGraphics::calcImagePattern(ImageCollection *const vert A_UNUSED, + const Image *const image A_UNUSED, + const int x A_UNUSED, + const int y A_UNUSED, + const int w A_UNUSED, + const int h A_UNUSED) const +{ +} + +void SafeOpenGLGraphics::drawTile(const ImageVertexes *const vert A_UNUSED) +{ +} + +void SafeOpenGLGraphics::drawTile(const ImageCollection *const + vertCol A_UNUSED) +{ +} + +void SafeOpenGLGraphics::updateScreen() +{ + BLOCK_START("Graphics::updateScreen") + glFlush(); + glFinish(); +#ifdef USE_SDL2 + SDL_GL_SwapWindow(mWindow); +#else + SDL_GL_SwapBuffers(); +#endif + BLOCK_END("Graphics::updateScreen") +} + +bool SafeOpenGLGraphics::calcWindow(ImageCollection *const vertCol A_UNUSED, + const int x A_UNUSED, const int y A_UNUSED, + const int w A_UNUSED, const int h A_UNUSED, + const ImageRect &imgRect A_UNUSED) +{ + return false; +} + +void SafeOpenGLGraphics::_beginDraw() +{ + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(0.0, static_cast<double>(mRect.w), + static_cast<double>(mRect.h), 0.0, -1.0, 1.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_SCISSOR_TEST); + glDisable(GL_DITHER); + glDisable(GL_LIGHTING); + glDisable(GL_DEPTH_TEST); + glDisable(GL_FOG); + glDisable(GL_COLOR_LOGIC_OP); + glDisable(GL_COLOR_MATERIAL); + glDisable(GL_STENCIL_TEST); + + glShadeModel(GL_FLAT); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + pushClipArea(gcn::Rectangle(0, 0, mRect.w, mRect.h)); +} + +void SafeOpenGLGraphics::_endDraw() +{ + popClipArea(); +} + +void SafeOpenGLGraphics::prepareScreenshot() +{ + if (config.getBoolValue("usefbo")) + graphicsManager.createFBO(mRect.w, mRect.h, &mFbo); +} + +SDL_Surface* SafeOpenGLGraphics::getScreenshot() +{ + const int h = mRect.h; + const int w = mRect.w - (mRect.w % 4); + GLint pack = 1; + + SDL_Surface *const screenshot = MSDL_CreateRGBSurface( + SDL_SWSURFACE, w, h, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000); + + if (!screenshot) + return nullptr; + + if (SDL_MUSTLOCK(screenshot)) + SDL_LockSurface(screenshot); + + // Grap the pixel buffer and write it to the SDL surface + glGetIntegerv(GL_PACK_ALIGNMENT, &pack); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, screenshot->pixels); + + // Flip the screenshot, as OpenGL has 0,0 in bottom left + unsigned int lineSize = 3 * w; + GLubyte* buf = static_cast<GLubyte*>(malloc(lineSize)); + + const int h2 = h / 2; + for (int i = 0; i < h2; i++) + { + GLubyte *const top = static_cast<GLubyte*>( + screenshot->pixels) + lineSize * i; + GLubyte *const bot = static_cast<GLubyte*>( + screenshot->pixels) + lineSize * (h - 1 - i); + + memcpy(buf, top, lineSize); + memcpy(top, bot, lineSize); + memcpy(bot, buf, lineSize); + } + + free(buf); + + if (config.getBoolValue("usefbo")) + graphicsManager.deleteFBO(&mFbo); + + glPixelStorei(GL_PACK_ALIGNMENT, pack); + + if (SDL_MUSTLOCK(screenshot)) + SDL_UnlockSurface(screenshot); + + return screenshot; +} + +bool SafeOpenGLGraphics::pushClipArea(gcn::Rectangle area) +{ + int transX = 0; + int transY = 0; + + if (!mClipStack.empty()) + { + const gcn::ClipRectangle &clipArea = mClipStack.top(); + transX = -clipArea.xOffset; + transY = -clipArea.yOffset; + } + + const bool result = gcn::Graphics::pushClipArea(area); + + const gcn::ClipRectangle &clipArea = mClipStack.top(); + + glPushMatrix(); + glTranslatef(static_cast<GLfloat>(transX + clipArea.xOffset), + static_cast<GLfloat>(transY + clipArea.yOffset), 0); + glScissor(clipArea.x, mRect.h - clipArea.y - clipArea.height, + clipArea.width, clipArea.height); + + return result; +} + +void SafeOpenGLGraphics::popClipArea() +{ + gcn::Graphics::popClipArea(); + + if (mClipStack.empty()) + return; + + glPopMatrix(); + const gcn::ClipRectangle &clipArea = mClipStack.top(); + glScissor(clipArea.x, mRect.h - clipArea.y - clipArea.height, + clipArea.width, clipArea.height); +} + +void SafeOpenGLGraphics::drawPoint(int x, int y) +{ + setTexturingAndBlending(false); + restoreColor(); + + glBegin(GL_POINTS); + glVertex2i(x, y); + glEnd(); +} + +void SafeOpenGLGraphics::drawLine(int x1, int y1, int x2, int y2) +{ + setTexturingAndBlending(false); + restoreColor(); + + glBegin(GL_LINES); + glVertex2f(static_cast<float>(x1) + 0.5f, static_cast<float>(y1) + 0.5f); + glVertex2f(static_cast<float>(x2) + 0.5f, static_cast<float>(y2) + 0.5f); + glEnd(); + + glBegin(GL_POINTS); + glVertex2f(static_cast<float>(x2) + 0.5f, static_cast<float>(y2) + 0.5f); + glEnd(); +} + +void SafeOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, false); +} + +void SafeOpenGLGraphics::fillRectangle(const gcn::Rectangle& rect) +{ + drawRectangle(rect, true); +} + +void SafeOpenGLGraphics::setTargetPlane(int width A_UNUSED, + int height A_UNUSED) +{ +} + +void SafeOpenGLGraphics::setTexturingAndBlending(const bool enable) +{ + if (enable) + { + if (!mTexture) + { + glEnable(OpenGLImageHelper::mTextureType); + mTexture = true; + } + + if (!mAlpha) + { + glEnable(GL_BLEND); + mAlpha = true; + } + } + else + { + mLastImage = 0; + if (mAlpha && !mColorAlpha) + { + glDisable(GL_BLEND); + mAlpha = false; + } + else if (!mAlpha && mColorAlpha) + { + glEnable(GL_BLEND); + mAlpha = true; + } + + if (mTexture) + { + glDisable(OpenGLImageHelper::mTextureType); + mTexture = false; + } + } +} + +void SafeOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect, + const bool filled) +{ + BLOCK_START("Graphics::drawRectangle") + const float offset = filled ? 0 : 0.5f; + + setTexturingAndBlending(false); + restoreColor(); + + glBegin(filled ? GL_QUADS : GL_LINE_LOOP); + glVertex2f(static_cast<float>(rect.x) + offset, + static_cast<float>(rect.y) + offset); + glVertex2f(static_cast<float>(rect.x + rect.width) - offset, + static_cast<float>(rect.y) + offset); + glVertex2f(static_cast<float>(rect.x + rect.width) - offset, + static_cast<float>(rect.y + rect.height) - offset); + glVertex2f(static_cast<float>(rect.x) + offset, + static_cast<float>(rect.y + rect.height) - offset); + glEnd(); + BLOCK_END("Graphics::drawRectangle") +} + +void SafeOpenGLGraphics::bindTexture(const GLenum target, const GLuint texture) +{ + if (mLastImage != texture) + { + mLastImage = texture; + glBindTexture(target, texture); + } +} + +void SafeOpenGLGraphics::setColorAlpha(const float alpha) +{ + if (!mIsByteColor && mFloatColor == alpha) + return; + + glColor4f(1.0f, 1.0f, 1.0f, alpha); + mIsByteColor = false; + mFloatColor = alpha; +} + +void SafeOpenGLGraphics::restoreColor() +{ + if (mIsByteColor && mByteColor == mColor) + return; + + glColor4ub(static_cast<GLubyte>(mColor.r), + static_cast<GLubyte>(mColor.g), + static_cast<GLubyte>(mColor.b), + static_cast<GLubyte>(mColor.a)); + mIsByteColor = true; + mByteColor = mColor; +} + +#endif // USE_OPENGL diff --git a/src/render/safeopenglgraphics.h b/src/render/safeopenglgraphics.h new file mode 100644 index 000000000..2c31bb696 --- /dev/null +++ b/src/render/safeopenglgraphics.h @@ -0,0 +1,183 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SAFEOPENGLGRAPHICS_H +#define SAFEOPENGLGRAPHICS_H + +#include "main.h" +#if defined USE_OPENGL && !defined ANDROID + +#include "render/graphics.h" + +#include "resources/fboinfo.h" + +#ifdef ANDROID +#include <GLES/gl.h> +#include <GLES/glext.h> +#else +#ifndef USE_SDL2 +#define GL_GLEXT_PROTOTYPES 1 +#endif +#include <SDL_opengl.h> +#include <GL/glext.h> +#endif + +class SafeOpenGLGraphics final : public Graphics +{ + public: + SafeOpenGLGraphics(); + + A_DELETE_COPY(SafeOpenGLGraphics) + + ~SafeOpenGLGraphics(); + + bool setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) override; + + /** + * Draws a resclaled version of the image + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor) override; + + /** + * Used to get the smooth rescale option over the standard function. + */ + bool drawRescaledImage(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, const int desiredHeight, + const bool useColor, bool smooth); + + void drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) override; + + /** + * Draw a pattern based on a rescaled version of the given image... + */ + void drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) override; + + void calcTile(ImageVertexes *const vert, const Image *const image, + int x, int y) const override; + + void calcTile(ImageCollection *const vertCol, + const Image *const image, int x, int y) override; + + void calcImagePattern(ImageVertexes *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const; + + void calcImagePattern(ImageCollection *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const; + + void drawTile(const ImageVertexes *const vert) override; + + void drawTile(const ImageCollection *const vertCol) override; + + void updateScreen() override; + + bool calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect); + + void _beginDraw(); + + void _endDraw(); + + bool pushClipArea(gcn::Rectangle area); + + void popClipArea(); + + void setColor(const gcn::Color &color) + { + mColor = color; + mColor2 = color; + mColorAlpha = (color.a != 255); + } + + void setColorAll(const gcn::Color &color, const gcn::Color &color2) + { + mColor = color; + mColor2 = color2; + mColorAlpha = (color.a != 255); + } + + void drawPoint(int x, int y); + + void drawLine(int x1, int y1, int x2, int y2); + + void drawRectangle(const gcn::Rectangle &rect, const bool filled); + + void drawRectangle(const gcn::Rectangle &rect); + + void fillRectangle(const gcn::Rectangle &rect) override; + + void setTargetPlane(int width, int height); + + /** + * Takes a screenshot and returns it as SDL surface. + */ + SDL_Surface *getScreenshot() override A_WARN_UNUSED; + + void prepareScreenshot() override; + + static void bindTexture(const GLenum target, const GLuint texture); + + static GLuint mLastImage; + + protected: + bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor); + + void setTexturingAndBlending(const bool enable); + + private: + void inline setColorAlpha(float alpha); + + void inline restoreColor(); + + bool mTexture; + bool mIsByteColor; + gcn::Color mByteColor; + float mFloatColor; + bool mColorAlpha; + FBOInfo mFbo; +}; +#endif + +#endif // SAFEOPENGLGRAPHICS_H diff --git a/src/render/sdl2graphics.cpp b/src/render/sdl2graphics.cpp new file mode 100644 index 000000000..218f2cad7 --- /dev/null +++ b/src/render/sdl2graphics.cpp @@ -0,0 +1,641 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifdef USE_SDL2 + +#include "render/sdl2graphics.h" + +#include "main.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/imagehelper.h" +#include "resources/sdl2imagehelper.h" + +#include <guichan/sdl/sdlpixel.hpp> + +#include "utils/sdlcheckutils.h" + +#include <SDL.h> + +#include "debug.h" + +#ifdef DEBUG_SDL_SURFACES + +#define MSDL_RenderCopy(render, texture, src, dst) \ + FakeSDL_RenderCopy(render, texture, src, dst) + +static int FakeSDL_RenderCopy(SDL_Renderer *const renderer, + SDL_Texture *const texture, + const SDL_Rect *const srcrect, + const SDL_Rect *const dstrect) +{ + int ret = SDL_RenderCopy(renderer, texture, srcrect, dstrect); + if (ret) + { + logger->log("rendering error in texture %p: %s", + static_cast<void*>(texture), SDL_GetError()); + } + return ret; +} + +#else + +#define MSDL_RenderCopy(render, texture, src, dst) \ + SDL_RenderCopy(render, texture, src, dst) + +#endif + +SDLGraphics::SDLGraphics() : + Graphics(), + mOldPixel(0), + mOldAlpha(0) +{ +} + +SDLGraphics::~SDLGraphics() +{ +} + +bool SDLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor A_UNUSED) +{ + FUNC_BLOCK("Graphics::drawRescaledImage", 1) + // Check that preconditions for blitting are met. + if (!mWindow || !image) + return false; + if (!image->mTexture) + return false; + + const gcn::ClipRectangle &top = mClipStack.top(); + const SDL_Rect &bounds = image->mBounds; + const SDL_Rect srcRect = + { + static_cast<int32_t>(srcX + bounds.x), + static_cast<int32_t>(srcY + bounds.y), + static_cast<int32_t>(width), + static_cast<int32_t>(height) + }; + const SDL_Rect dstRect = + { + static_cast<int32_t>(dstX + top.xOffset), + static_cast<int32_t>(dstY + top.yOffset), + static_cast<int32_t>(desiredWidth), + static_cast<int32_t>(desiredHeight) + }; + + return (MSDL_RenderCopy(mRenderer, image->mTexture, + &srcRect, &dstRect) < 0); +} + +bool SDLGraphics::drawImage2(const Image *const image, int srcX, int srcY, + int dstX, int dstY, const int width, + const int height, const bool useColor A_UNUSED) +{ + FUNC_BLOCK("Graphics::drawImage2", 1) + // Check that preconditions for blitting are met. + if (!mWindow || !image || !image->mTexture) + return false; + + const gcn::ClipRectangle &top = mClipStack.top(); + if (!top.height) + return false; + + const SDL_Rect &bounds = image->mBounds; + const SDL_Rect srcRect = + { + static_cast<int32_t>(srcX + bounds.x), + static_cast<int32_t>(srcY + bounds.y), + static_cast<int32_t>(width), + static_cast<int32_t>(height) + }; + + const SDL_Rect dstRect = + { + static_cast<int32_t>(dstX + top.xOffset), + static_cast<int32_t>(dstY + top.yOffset), + static_cast<int32_t>(width), + static_cast<int32_t>(height) + }; + + return !MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); +} + +void SDLGraphics::drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) +{ + FUNC_BLOCK("Graphics::drawImagePattern", 1) + // Check that preconditions for blitting are met. + if (!mWindow || !image) + return; + if (!image->mTexture) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + if (!top.height) + return; + + const SDL_Rect &bounds = image->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; + if (iw == 0 || ih == 0) + return; + + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + + SDL_Rect dstRect; + SDL_Rect srcRect; + srcRect.x = static_cast<int32_t>(bounds.x); + srcRect.y = static_cast<int32_t>(bounds.y); + for (int py = 0; py < h; py += ih) + { + const int dh = (py + ih >= h) ? h - py : ih; + dstRect.y = static_cast<int32_t>(py + yOffset); + srcRect.h = static_cast<int32_t>(dh); + dstRect.h = static_cast<int32_t>(dh); + + for (int px = 0; px < w; px += iw) + { + const int dw = (px + iw >= w) ? w - px : iw; + dstRect.x = static_cast<int32_t>(px + xOffset); + srcRect.w = static_cast<int32_t>(dw); + dstRect.w = static_cast<int32_t>(dw); + + MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); + } + } +} + +void SDLGraphics::drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) +{ + // Check that preconditions for blitting are met. + if (!mWindow || !image) + return; + if (!image->mTexture) + return; + + if (scaledHeight == 0 || scaledWidth == 0) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + if (!top.height) + return; + + Image *const tmpImage = image->SDLgetScaledImage( + scaledWidth, scaledHeight); + if (!tmpImage) + return; + + const SDL_Rect &bounds = tmpImage->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; + if (iw == 0 || ih == 0) + return; + + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + + SDL_Rect dstRect; + SDL_Rect srcRect; + srcRect.x = static_cast<int32_t>(bounds.x); + srcRect.y = static_cast<int32_t>(bounds.y); + for (int py = 0; py < h; py += ih) + { + const int dh = (py + ih >= h) ? h - py : ih; + dstRect.y = static_cast<int32_t>(py + yOffset); + srcRect.h = static_cast<int32_t>(dh); + dstRect.h = static_cast<int32_t>(dh); + + for (int px = 0; px < w; px += iw) + { + const int dw = (px + iw >= w) ? w - px : iw; + dstRect.x = static_cast<int32_t>(px + xOffset); + srcRect.w = static_cast<int32_t>(dw); + dstRect.w = static_cast<int32_t>(dw); + + MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); + } + } + + delete tmpImage; +} + +void SDLGraphics::calcImagePattern(ImageVertexes* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + // Check that preconditions for blitting are met. + if (!vert || !mWindow || !image || !image->mTexture) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + if (!top.height) + return; + + const SDL_Rect &bounds = image->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; + if (iw == 0 || ih == 0) + return; + + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + const int srcX = bounds.x; + const int srcY = bounds.y; + for (int py = 0; py < h; py += ih) + { + const int dh = (py + ih >= h) ? h - py : ih; + const int dstY = py + yOffset; + + for (int px = 0; px < w; px += iw) + { + const int dw = (px + iw >= w) ? w - px : iw; + const int dstX = px + xOffset; + + DoubleRect *const r = new DoubleRect(); + SDL_Rect &dstRect = r->dst; + SDL_Rect &srcRect = r->src; + srcRect.x = static_cast<int32_t>(srcX); + srcRect.y = static_cast<int32_t>(srcY); + srcRect.w = static_cast<int32_t>(dw); + srcRect.h = static_cast<int32_t>(dh); + dstRect.x = static_cast<int32_t>(dstX); + dstRect.y = static_cast<int32_t>(dstY); + dstRect.w = static_cast<int32_t>(dw); + dstRect.h = static_cast<int32_t>(dh); + + vert->sdl.push_back(r); + } + } +} + +void SDLGraphics::calcImagePattern(ImageCollection* const vertCol, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + ImageVertexes *vert = nullptr; + if (vertCol->currentImage != image) + { + vert = new ImageVertexes(); + vertCol->currentImage = image; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + calcImagePattern(vert, image, x, y, w, h); +} + +void SDLGraphics::calcTile(ImageVertexes *const vert, + const Image *const image, + int x, int y) const +{ + vert->image = image; + calcTileSDL(vert, x, y); +} + +void SDLGraphics::calcTileSDL(ImageVertexes *const vert, int x, int y) const +{ + // Check that preconditions for blitting are met. + if (!vert || !vert->image || !vert->image->mTexture) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + if (!top.height) + return; + + const Image *const image = vert->image; + const SDL_Rect &bounds = image->mBounds; + + x += top.xOffset; + y += top.yOffset; + + DoubleRect *rect = new DoubleRect(); + SDL_Rect &dstRect = rect->dst; + SDL_Rect &srcRect = rect->src; + + srcRect.x = static_cast<int32_t>(bounds.x); + srcRect.y = static_cast<int32_t>(bounds.y); + srcRect.w = static_cast<int32_t>(bounds.w); + srcRect.h = static_cast<int32_t>(bounds.h); + dstRect.x = static_cast<int32_t>(x); + dstRect.y = static_cast<int32_t>(y); + dstRect.w = static_cast<int32_t>(bounds.w); + dstRect.h = static_cast<int32_t>(bounds.h); + + vert->sdl.push_back(rect); +} + +void SDLGraphics::calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y) +{ + if (vertCol->currentImage != image) + { + ImageVertexes *const vert = new ImageVertexes(); + vertCol->currentImage = image; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + calcTileSDL(vert, x, y); + } + else + { + calcTileSDL(vertCol->currentVert, x, y); + } +} + +void SDLGraphics::drawTile(const ImageCollection *const vertCol) +{ + const ImageVertexesVector &draws = vertCol->draws; + const ImageCollectionCIter it_end = draws.end(); + for (ImageCollectionCIter it = draws.begin(); it != it_end; ++ it) + { + const ImageVertexes *const vert = *it; + const Image *const img = vert->image; + const DoubleRects *const rects = &vert->sdl; + DoubleRects::const_iterator it2 = rects->begin(); + const DoubleRects::const_iterator it2_end = rects->end(); + while (it2 != it2_end) + { + MSDL_RenderCopy(mRenderer, img->mTexture, + &(*it2)->src, &(*it2)->dst); + ++ it2; + } + } +} + +void SDLGraphics::drawTile(const ImageVertexes *const vert) +{ + // vert and img must be != 0 + const Image *const img = vert->image; + const DoubleRects *const rects = &vert->sdl; + DoubleRects::const_iterator it = rects->begin(); + const DoubleRects::const_iterator it_end = rects->end(); + while (it != it_end) + { + MSDL_RenderCopy(mRenderer, img->mTexture, &(*it)->src, &(*it)->dst); + ++ it; + } +} + +void SDLGraphics::updateScreen() +{ + BLOCK_START("Graphics::updateScreen") + SDL_RenderPresent(mRenderer); +// SDL_RenderClear(mRenderer); + BLOCK_END("Graphics::updateScreen") +} + +SDL_Surface *SDLGraphics::getScreenshot() +{ +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + const int rmask = 0xff000000; + const int gmask = 0x00ff0000; + const int bmask = 0x0000ff00; +#else + const int rmask = 0x000000ff; + const int gmask = 0x0000ff00; + const int bmask = 0x00ff0000; +#endif + const int amask = 0x00000000; + + SDL_Surface *const screenshot = MSDL_CreateRGBSurface(SDL_SWSURFACE, + mRect.w, mRect.h, 24, rmask, gmask, bmask, amask); + +// if (screenshot) +// SDL_BlitSurface(mWindow, nullptr, screenshot, nullptr); + + return screenshot; +} + +bool SDLGraphics::drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height) +{ + // +++ need use SDL_RenderDrawLines + for (int y = y1; y < y2; y += height) + drawLine(x1, y, x2, y); + + for (int x = x1; x < x2; x += width) + drawLine(x, y1, x, y2); + + return true; +} + +bool SDLGraphics::calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) +{ + ImageVertexes *vert = nullptr; + Image *const image = imgRect.grid[4]; + if (vertCol->currentImage != image) + { + vert = new ImageVertexes(); + vertCol->currentImage = image; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + const Image *const *const grid = &imgRect.grid[0]; + return calcImageRect(vert, x, y, w, h, + grid[0], grid[2], grid[6], grid[8], + grid[1], grid[5], grid[7], grid[3], + grid[4]); +} + +void SDLGraphics::fillRectangle(const gcn::Rectangle &rectangle) +{ + const gcn::ClipRectangle &top = mClipStack.top(); + const SDL_Rect rect = + { + static_cast<int32_t>(rectangle.x + top.xOffset), + static_cast<int32_t>(rectangle.y + top.yOffset), + static_cast<int32_t>(rectangle.width), + static_cast<int32_t>(rectangle.height) + }; + + SDL_SetRenderDrawColor(mRenderer, mColor.r, mColor.g, mColor.b, mColor.a); + SDL_RenderFillRects(mRenderer, &rect, 1); +} + +void SDLGraphics::_beginDraw() +{ + pushClipArea(gcn::Rectangle(0, 0, mRect.w, mRect.h)); +} + +void SDLGraphics::_endDraw() +{ + popClipArea(); +} + +bool SDLGraphics::pushClipArea(gcn::Rectangle area) +{ + const bool result = gcn::Graphics::pushClipArea(area); + + const gcn::ClipRectangle &carea = mClipStack.top(); + const SDL_Rect rect = + { + static_cast<int32_t>(carea.x), + static_cast<int32_t>(carea.y), + static_cast<int32_t>(carea.width), + static_cast<int32_t>(carea.height) + }; + SDL_RenderSetClipRect(mRenderer, &rect); + return result; +} + +void SDLGraphics::popClipArea() +{ + gcn::Graphics::popClipArea(); + + if (mClipStack.empty()) + return; + + const gcn::ClipRectangle &carea = mClipStack.top(); + const SDL_Rect rect = + { + static_cast<int32_t>(carea.x), + static_cast<int32_t>(carea.y), + static_cast<int32_t>(carea.width), + static_cast<int32_t>(carea.height) + }; + + SDL_RenderSetClipRect(mRenderer, &rect); +} + +void SDLGraphics::drawPoint(int x, int y) +{ + if (mClipStack.empty()) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + + x += top.xOffset; + y += top.yOffset; + + if (!top.isPointInRect(x, y)) + return; + + SDL_SetRenderDrawColor(mRenderer, mColor.r, mColor.g, mColor.b, mColor.a); + const SDL_Point point = + { + x, + y + }; + + SDL_RenderDrawPoints(mRenderer, &point, 1); +} + + +void SDLGraphics::drawRectangle(const gcn::Rectangle &rectangle) +{ + const gcn::ClipRectangle &top = mClipStack.top(); + + SDL_SetRenderDrawColor(mRenderer, mColor.r, mColor.g, mColor.b, mColor.a); + + const int x1 = rectangle.x + top.xOffset; + const int y1 = rectangle.y + top.yOffset; + const int x2 = x1 + rectangle.width - 1; + const int y2 = y1 + rectangle.height - 1; + SDL_Point points[] = + { + {x1, y1}, + {x2, y1}, + {x2, y2}, + {x1, y2}, + {x1, y1} + }; + + SDL_RenderDrawLines(mRenderer, points, 5); +} + +void SDLGraphics::drawLine(int x1, int y1, int x2, int y2) +{ + const gcn::ClipRectangle &top = mClipStack.top(); + + SDL_SetRenderDrawColor(mRenderer, mColor.r, mColor.g, mColor.b, mColor.a); + + const int x0 = top.xOffset; + const int y0 = top.yOffset; + + SDL_Point points[] = + { + {x1 + x0, y1 + y0}, + {x2 + x0, y2 + y0} + }; + + SDL_RenderDrawLines(mRenderer, points, 2); +} + +bool SDLGraphics::setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) +{ + setMainFlags(w, h, bpp, fs, hwaccel, resize, noFrame); + + if (!(mWindow = graphicsManager.createWindow(w, h, bpp, + getSoftwareFlags()))) + { + mRect.w = 0; + mRect.h = 0; + return false; + } + + int w1 = 0; + int h1 = 0; + SDL_GetWindowSize(mWindow, &w1, &h1); + mRect.w = w1; + mRect.h = h1; + + mRenderer = graphicsManager.createRenderer(mWindow, SDL_RENDERER_SOFTWARE); + SDLImageHelper::setRenderer(mRenderer); + return videoInfo(); +} + +#endif // USE_SDL2 diff --git a/src/render/sdl2graphics.h b/src/render/sdl2graphics.h new file mode 100644 index 000000000..f3835e904 --- /dev/null +++ b/src/render/sdl2graphics.h @@ -0,0 +1,143 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SDL2GRAPHICS_H +#define SDL2GRAPHICS_H + +#ifdef USE_SDL2 + +#include "render/graphics.h" + +#include "localconsts.h" + +class Image; +class ImageCollection; +class ImageVertexes; +class MapLayer; + +struct SDL_Surface; + +/** + * A central point of control for graphics. + */ +class SDLGraphics : public Graphics +{ + public: + /** + * Constructor. + */ + SDLGraphics(); + + A_DELETE_COPY(SDLGraphics) + + /** + * Destructor. + */ + virtual ~SDLGraphics(); + + void _beginDraw(); + + void _endDraw(); + + bool pushClipArea(gcn::Rectangle rect); + + void popClipArea(); + + virtual bool drawRescaledImage(const Image *const image, int srcX, + int srcY, int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor = false); + + virtual void drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h); + + virtual void drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight); + + virtual void calcImagePattern(ImageVertexes *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const; + + virtual void calcImagePattern(ImageCollection *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const; + + virtual void calcTile(ImageVertexes *const vert, + const Image *const image, int x, int y) const; + + virtual void calcTileSDL(ImageVertexes *const vert, + int x, int y) const; + + virtual void calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y); + + virtual void drawTile(const ImageVertexes *const vert); + + virtual void drawTile(const ImageCollection *const vertCol); + + virtual void updateScreen(); + + virtual SDL_Surface *getScreenshot() A_WARN_UNUSED; + + virtual bool drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height); + + virtual bool calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect); + + void fillRectangle(const gcn::Rectangle &rect) override; + + void drawRectangle(const gcn::Rectangle &rect) override; + + void drawPoint(int x, int y) override; + + void drawLine(int x1, int y1, int x2, int y2) override; + + bool setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame); + + protected: + virtual bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor); + + uint32_t mOldPixel; + int mOldAlpha; +}; + +#endif // USE_SDL2 +#endif // SDL2GRAPHICS_H diff --git a/src/render/sdlgraphics.cpp b/src/render/sdlgraphics.cpp new file mode 100644 index 000000000..9c410db8d --- /dev/null +++ b/src/render/sdlgraphics.cpp @@ -0,0 +1,1272 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef USE_SDL2 + +#include "render/sdlgraphics.h" + +#include "main.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/imagehelper.h" + +#include "utils/sdlcheckutils.h" + +#include <guichan/sdl/sdlpixel.hpp> + +#include "debug.h" + +#if SDL_BYTEORDER == SDL_LIL_ENDIAN +static unsigned int *cR = nullptr; +static unsigned int *cG = nullptr; +static unsigned int *cB = nullptr; +#endif + +SDLGraphics::SDLGraphics() : + Graphics(), + mOldPixel(0), + mOldAlpha(0) +{ +} + +SDLGraphics::~SDLGraphics() +{ +} + +bool SDLGraphics::drawRescaledImage(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor A_UNUSED) +{ + FUNC_BLOCK("Graphics::drawRescaledImage", 1) + // Check that preconditions for blitting are met. + if (!mWindow || !image) + return false; + if (!image->mSDLSurface) + return false; + + Image *const tmpImage = image->SDLgetScaledImage( + desiredWidth, desiredHeight); + + if (!tmpImage) + return false; + if (!tmpImage->mSDLSurface) + return false; + + const gcn::ClipRectangle &top = mClipStack.top(); + const SDL_Rect &bounds = image->mBounds; + + SDL_Rect srcRect = + { + static_cast<int16_t>(srcX + bounds.x), + static_cast<int16_t>(srcY + bounds.y), + static_cast<uint16_t>(width), + static_cast<uint16_t>(height) + }; + + SDL_Rect dstRect = + { + static_cast<int16_t>(dstX + top.xOffset), + static_cast<int16_t>(dstY + top.yOffset), + 0, + 0 + }; + + const bool returnValue = !(SDL_BlitSurface(tmpImage->mSDLSurface, + &srcRect, mWindow, &dstRect) < 0); + + delete tmpImage; + + return returnValue; +} + +bool SDLGraphics::drawImage2(const Image *const image, int srcX, int srcY, + int dstX, int dstY, const int width, + const int height, const bool useColor A_UNUSED) +{ + FUNC_BLOCK("Graphics::drawImage2", 1) + // Check that preconditions for blitting are met. + if (!mWindow || !image || !image->mSDLSurface) + return false; + + const gcn::ClipRectangle &top = mClipStack.top(); + const SDL_Rect &bounds = image->mBounds; + + + SDL_Surface *const src = image->mSDLSurface; + + srcX += bounds.x; + srcY += bounds.y; + dstX += top.xOffset; + dstY += top.yOffset; + + int w = width; + int h = height; + if (srcX < 0) + { + w += srcX; + dstX -= static_cast<int16_t>(srcX); + srcX = 0; + } + const int maxw = src->w - srcX; + if (maxw < w) + w = maxw; + + if (srcY < 0) + { + h += srcY; + dstY -= static_cast<int16_t>(srcY); + srcY = 0; + } + const int maxh = src->h - srcY; + if (maxh < h) + h = maxh; + + const SDL_Rect *const clip = &mWindow->clip_rect; + const int clipX = clip->x; + const int clipY = clip->y; + int dx = clipX - dstX; + if (dx > 0) + { + w -= dx; + dstX += static_cast<int16_t>(dx); + srcX += dx; + } + dx = dstX + w - clipX - clip->w; + if (dx > 0) + w -= dx; + + int dy = clipY - dstY; + if (dy > 0) + { + h -= dy; + dstY += static_cast<int16_t>(dy); + srcY += dy; + } + dy = dstY + h - clipY - clip->h; + if (dy > 0) + h -= dy; + + if (w > 0 && h > 0) + { + SDL_Rect srcRect = + { + static_cast<int16_t>(srcX), + static_cast<int16_t>(srcY), + static_cast<uint16_t>(w), + static_cast<uint16_t>(h) + }; + + SDL_Rect dstRect = + { + static_cast<int16_t>(dstX), + static_cast<int16_t>(dstY), + static_cast<uint16_t>(w), + static_cast<uint16_t>(h) + }; + + return SDL_LowerBlit(src, &srcRect, mWindow, &dstRect); + } + return 0; +} + +void SDLGraphics::drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h) +{ + FUNC_BLOCK("Graphics::drawImagePattern", 1) + // Check that preconditions for blitting are met. + if (!mWindow || !image) + return; + if (!image->mSDLSurface) + return; + + const SDL_Rect &bounds = image->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; + if (iw == 0 || ih == 0) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + const int srcX = bounds.x; + const int srcY = bounds.y; + SDL_Surface *const src = image->mSDLSurface; + const SDL_Rect *const clip = &mWindow->clip_rect; + const int clipX = clip->x; + const int clipY = clip->y; + + for (int py = 0; py < h; py += ih) + { + const int dh = (py + ih >= h) ? h - py : ih; + int dstY = py + yOffset; + int y2 = srcY; + int h2 = dh; + if (y2 < 0) + { + h2 += y2; + dstY -= static_cast<int16_t>(y2); + y2 = 0; + } + const int maxh = src->h - y2; + if (maxh < h2) + h2 = maxh; + + int dy = clipY - dstY; + if (dy > 0) + { + h2 -= dy; + dstY += static_cast<int16_t>(dy); + y2 += dy; + } + dy = dstY + h2 - clipY - clip->h; + if (dy > 0) + h2 -= dy; + + if (h2 > 0) + { + for (int px = 0; px < w; px += iw) + { + const int dw = (px + iw >= w) ? w - px : iw; + int dstX = px + xOffset; + int x2 = srcX; + int w2 = dw; + if (x2 < 0) + { + w2 += x2; + dstX -= static_cast<int16_t>(x2); + x2 = 0; + } + const int maxw = src->w - x2; + if (maxw < w2) + w2 = maxw; + + int dx = clipX - dstX; + if (dx > 0) + { + w2 -= dx; + dstX += static_cast<int16_t>(dx); + x2 += dx; + } + dx = dstX + w2 - clipX - clip->w; + if (dx > 0) + w2 -= dx; + + if (w2 > 0) + { + SDL_Rect srcRect = + { + static_cast<int16_t>(x2), + static_cast<int16_t>(y2), + static_cast<uint16_t>(w2), + static_cast<uint16_t>(h2) + }; + + SDL_Rect dstRect = + { + static_cast<int16_t>(dstX), + static_cast<int16_t>(dstY), + static_cast<uint16_t>(w2), + static_cast<uint16_t>(h2) + }; + + SDL_LowerBlit(src, &srcRect, mWindow, &dstRect); + } + +// SDL_BlitSurface(image->mSDLSurface, &srcRect, mWindow, &dstRect); + } + } + } +} + +void SDLGraphics::drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight) +{ + // Check that preconditions for blitting are met. + if (!mWindow || !image) + return; + if (!image->mSDLSurface) + return; + + if (scaledHeight == 0 || scaledWidth == 0) + return; + + Image *const tmpImage = image->SDLgetScaledImage( + scaledWidth, scaledHeight); + if (!tmpImage) + return; + + const SDL_Rect &bounds = tmpImage->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; + if (iw == 0 || ih == 0) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + const int srcX = bounds.x; + const int srcY = bounds.y; + + for (int py = 0; py < h; py += ih) // Y position on pattern plane + { + const int dh = (py + ih >= h) ? h - py : ih; + const int dstY = py + yOffset; + + for (int px = 0; px < w; px += iw) // X position on pattern plane + { + const int dw = (px + iw >= w) ? w - px : iw; + const int dstX = px + xOffset; + + SDL_Rect srcRect = + { + static_cast<int16_t>(srcX), + static_cast<int16_t>(srcY), + static_cast<uint16_t>(dw), + static_cast<uint16_t>(dh) + }; + + SDL_Rect dstRect = + { + static_cast<int16_t>(dstX), + static_cast<int16_t>(dstY), + 0, + 0 + }; + + SDL_BlitSurface(tmpImage->mSDLSurface, &srcRect, + mWindow, &dstRect); + } + } + + delete tmpImage; +} + +void SDLGraphics::calcImagePattern(ImageVertexes* const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + // Check that preconditions for blitting are met. + if (!vert || !mWindow || !image || !image->mSDLSurface) + return; + + const SDL_Rect &bounds = image->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; + if (iw == 0 || ih == 0) + return; + + const gcn::ClipRectangle &top = mClipStack.top(); + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + const int srcX = bounds.x; + const int srcY = bounds.y; + + for (int py = 0; py < h; py += ih) // Y position on pattern plane + { + const int dh = (py + ih >= h) ? h - py : ih; + const int dstY = py + yOffset; + + for (int px = 0; px < w; px += iw) // X position on pattern plane + { + const int dw = (px + iw >= w) ? w - px : iw; + const int dstX = px + xOffset; + + DoubleRect *const r = new DoubleRect(); + SDL_Rect &srcRect = r->src; + srcRect.x = static_cast<int16_t>(srcX); + srcRect.y = static_cast<int16_t>(srcY); + srcRect.w = static_cast<uint16_t>(dw); + srcRect.h = static_cast<uint16_t>(dh); + SDL_Rect &dstRect = r->dst; + dstRect.x = static_cast<int16_t>(dstX); + dstRect.y = static_cast<int16_t>(dstY); + + if (SDL_FakeUpperBlit(image->mSDLSurface, &srcRect, + mWindow, &dstRect) == 1) + { + vert->sdl.push_back(r); + } + else + { + delete r; + } + } + } +} + +void SDLGraphics::calcImagePattern(ImageCollection* const vertCol, + const Image *const image, + const int x, const int y, + const int w, const int h) const +{ + ImageVertexes *vert = nullptr; + if (vertCol->currentImage != image) + { + vert = new ImageVertexes(); + vertCol->currentImage = image; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + calcImagePattern(vert, image, x, y, w, h); +} + +void SDLGraphics::calcTile(ImageVertexes *const vert, + const Image *const image, + int x, int y) const +{ + vert->image = image; + calcTileSDL(vert, x, y); +} + +void SDLGraphics::calcTileSDL(ImageVertexes *const vert, int x, int y) const +{ + // Check that preconditions for blitting are met. + if (!vert || !vert->image || !vert->image->mSDLSurface) + return; + + const Image *const image = vert->image; + const gcn::ClipRectangle &top = mClipStack.top(); + const SDL_Rect &bounds = image->mBounds; + + DoubleRect *rect = new DoubleRect(); + rect->src.x = static_cast<int16_t>(bounds.x); + rect->src.y = static_cast<int16_t>(bounds.y); + rect->src.w = static_cast<uint16_t>(bounds.w); + rect->src.h = static_cast<uint16_t>(bounds.h); + rect->dst.x = static_cast<int16_t>(x + top.xOffset); + rect->dst.y = static_cast<int16_t>(y + top.yOffset); + if (SDL_FakeUpperBlit(image->mSDLSurface, &rect->src, + mWindow, &rect->dst) == 1) + { + vert->sdl.push_back(rect); + } + else + { + delete rect; + } +} + +void SDLGraphics::calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y) +{ + if (vertCol->currentImage != image) + { + ImageVertexes *const vert = new ImageVertexes(); + vertCol->currentImage = image; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + calcTileSDL(vert, x, y); + } + else + { + calcTileSDL(vertCol->currentVert, x, y); + } +} + +void SDLGraphics::drawTile(const ImageCollection *const vertCol) +{ + const ImageVertexesVector &draws = vertCol->draws; + const ImageCollectionCIter it_end = draws.end(); + for (ImageCollectionCIter it = draws.begin(); it != it_end; ++ it) + { + const ImageVertexes *const vert = *it; + const Image *const img = vert->image; + const DoubleRects *const rects = &vert->sdl; + DoubleRects::const_iterator it2 = rects->begin(); + const DoubleRects::const_iterator it2_end = rects->end(); + while (it2 != it2_end) + { + SDL_LowerBlit(img->mSDLSurface, &(*it2)->src, + mWindow, &(*it2)->dst); + ++ it2; + } + } +} + +void SDLGraphics::drawTile(const ImageVertexes *const vert) +{ + // vert and img must be != 0 + const Image *const img = vert->image; + const DoubleRects *const rects = &vert->sdl; + DoubleRects::const_iterator it = rects->begin(); + const DoubleRects::const_iterator it_end = rects->end(); + while (it != it_end) + { + SDL_LowerBlit(img->mSDLSurface, &(*it)->src, mWindow, &(*it)->dst); + ++ it; + } +} + +void SDLGraphics::updateScreen() +{ + BLOCK_START("Graphics::updateScreen") + if (mDoubleBuffer) + { + SDL_Flip(mWindow); + } + else + { + SDL_UpdateRects(mWindow, 1, &mRect); +// SDL_UpdateRect(mWindow, 0, 0, 0, 0); + } + BLOCK_END("Graphics::updateScreen") +} + +SDL_Surface *SDLGraphics::getScreenshot() +{ +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + const int rmask = 0xff000000; + const int gmask = 0x00ff0000; + const int bmask = 0x0000ff00; +#else + const int rmask = 0x000000ff; + const int gmask = 0x0000ff00; + const int bmask = 0x00ff0000; +#endif + const int amask = 0x00000000; + + SDL_Surface *const screenshot = MSDL_CreateRGBSurface(SDL_SWSURFACE, + mRect.w, mRect.h, 24, rmask, gmask, bmask, amask); + + if (screenshot) + SDL_BlitSurface(mWindow, nullptr, screenshot, nullptr); + + return screenshot; +} + +bool SDLGraphics::drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height) +{ + for (int y = y1; y < y2; y += height) + drawLine(x1, y, x2, y); + + for (int x = x1; x < x2; x += width) + drawLine(x, y1, x, y2); + + return true; +} + +bool SDLGraphics::calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect) +{ + ImageVertexes *vert = nullptr; + Image *const image = imgRect.grid[4]; + if (vertCol->currentImage != image) + { + vert = new ImageVertexes(); + vertCol->currentImage = image; + vertCol->currentVert = vert; + vert->image = image; + vertCol->draws.push_back(vert); + } + else + { + vert = vertCol->currentVert; + } + + const Image *const *const grid = &imgRect.grid[0]; + return calcImageRect(vert, x, y, w, h, + grid[0], grid[2], grid[6], grid[8], + grid[1], grid[5], grid[7], grid[3], + grid[4]); +} + +int SDLGraphics::SDL_FakeUpperBlit(const SDL_Surface *const src, + SDL_Rect *const srcrect, + const SDL_Surface *const dst, + SDL_Rect *dstrect) const +{ + int srcx, srcy, w, h; + + // Make sure the surfaces aren't locked + if (!src || !dst) + return -1; + + if (!srcrect || !dstrect) + return -1; + + srcx = srcrect->x; + w = srcrect->w; + if (srcx < 0) + { + w += srcx; + dstrect->x -= static_cast<int16_t>(srcx); + srcx = 0; + } + int maxw = src->w - srcx; + if (maxw < w) + w = maxw; + + srcy = srcrect->y; + h = srcrect->h; + if (srcy < 0) + { + h += srcy; + dstrect->y -= static_cast<int16_t>(srcy); + srcy = 0; + } + int maxh = src->h - srcy; + if (maxh < h) + h = maxh; + + const SDL_Rect *const clip = &dst->clip_rect; + const int clipX = clip->x; + const int clipY = clip->y; + int dx = clipX - dstrect->x; + if (dx > 0) + { + w -= dx; + dstrect->x += static_cast<int16_t>(dx); + srcx += dx; + } + dx = dstrect->x + w - clipX - clip->w; + if (dx > 0) + w -= dx; + + int dy = clipY - dstrect->y; + if (dy > 0) + { + h -= dy; + dstrect->y += static_cast<int16_t>(dy); + srcy += dy; + } + dy = dstrect->y + h - clipY - clip->h; + if (dy > 0) + h -= dy; + + if (w > 0 && h > 0) + { + if (srcrect) + { + srcrect->x = static_cast<int16_t>(srcx); + srcrect->y = static_cast<int16_t>(srcy); + srcrect->w = static_cast<int16_t>(w); + srcrect->h = static_cast<int16_t>(h); + } + dstrect->w = static_cast<int16_t>(w); + dstrect->h = static_cast<int16_t>(h); + + return 1; +// return SDL_LowerBlit(src, &sr, dst, dstrect); + } + dstrect->w = dstrect->h = 0; + return 0; +} + +void SDLGraphics::fillRectangle(const gcn::Rectangle& rectangle) +{ + FUNC_BLOCK("Graphics::fillRectangle", 1) + if (mClipStack.empty()) + return; + + const gcn::ClipRectangle& top = mClipStack.top(); + + gcn::Rectangle area = rectangle; + area.x += top.xOffset; + area.y += top.yOffset; + + if (!area.isIntersecting(top)) + return; + + if (mAlpha) + { + const int x1 = area.x > top.x ? area.x : top.x; + const int y1 = area.y > top.y ? area.y : top.y; + const int x2 = area.x + area.width < top.x + top.width ? + area.x + area.width : top.x + top.width; + const int y2 = area.y + area.height < top.y + top.height ? + area.y + area.height : top.y + top.height; + int x, y; + + SDL_LockSurface(mWindow); + + const int bpp = mWindow->format->BytesPerPixel; + const uint32_t pixel = SDL_MapRGB(mWindow->format, + static_cast<uint8_t>(mColor.r), static_cast<uint8_t>(mColor.g), + static_cast<uint8_t>(mColor.b)); + + switch (bpp) + { + case 1: + for (y = y1; y < y2; y++) + { + uint8_t *const p = static_cast<uint8_t *>(mWindow->pixels) + + y * mWindow->pitch; + for (x = x1; x < x2; x++) + *(p + x) = static_cast<uint8_t>(pixel); + } + break; + case 2: + for (y = y1; y < y2; y++) + { + uint8_t *const p0 = static_cast<uint8_t *>(mWindow->pixels) + + y * mWindow->pitch; + for (x = x1; x < x2; x++) + { + uint8_t *const p = p0 + x * 2; + *reinterpret_cast<uint16_t *>(p) = gcn::SDLAlpha16( + static_cast<uint16_t>(pixel), + *reinterpret_cast<uint16_t *>(p), + static_cast<uint8_t>(mColor.a), mWindow->format); + } + } + break; + case 3: + { + const int ca = 255 - mColor.a; + const int cr = mColor.r * mColor.a; + const int cg = mColor.g * mColor.a; + const int cb = mColor.b * mColor.a; + + for (y = y1; y < y2; y++) + { + uint8_t *const p0 = static_cast<uint8_t *>(mWindow->pixels) + + y * mWindow->pitch; + for (x = x1; x < x2; x++) + { + uint8_t *const p = p0 + x * 3; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + p[2] = static_cast<uint8_t>((p[2] * ca + cb) >> 8); + p[1] = static_cast<uint8_t>((p[1] * ca + cg) >> 8); + p[0] = static_cast<uint8_t>((p[0] * ca + cr) >> 8); +#else + p[0] = static_cast<uint8_t>((p[0] * ca + cb) >> 8); + p[1] = static_cast<uint8_t>((p[1] * ca + cg) >> 8); + p[2] = static_cast<uint8_t>((p[2] * ca + cr) >> 8); +#endif + } + } + break; + } + case 4: + { +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + const unsigned pb = (pixel & 0xff) * mColor.a; + const unsigned pg = (pixel & 0xff00) * mColor.a; + const unsigned pr = (pixel & 0xff0000) * mColor.a; + const unsigned a1 = (255 - mColor.a); + + for (y = y1; y < y2; y++) + { + uint8_t *const p0 = static_cast<uint8_t *>(mWindow->pixels) + + y * mWindow->pitch; + for (x = x1; x < x2; x++) + { + uint8_t *p = p0 + x * 4; + uint32_t dst = *reinterpret_cast<uint32_t *>(p); + const unsigned int b = (pb + (dst & 0xff) * a1) >> 8; + const unsigned int g = (pg + (dst & 0xff00) * a1) >> 8; + const unsigned int r = (pr + + (dst & 0xff0000) * a1) >> 8; + + *reinterpret_cast<uint32_t *>(p) = ((b & 0xff) + | (g & 0xff00) | (r & 0xff0000)); + } + } +#else + if (!cR) + { + cR = new unsigned int[0x100]; + cG = new unsigned int[0x100]; + cB = new unsigned int[0x100]; + mOldPixel = 0; + mOldAlpha = mColor.a; + } + + const SDL_PixelFormat * const format = mWindow->format; + const unsigned rMask = format->Rmask; + const unsigned gMask = format->Gmask; + const unsigned bMask = format->Bmask; +// const unsigned aMask = format->Amask; + unsigned rShift = rMask / 0xff; + unsigned gShift = gMask / 0xff; + unsigned bShift = bMask / 0xff; + if (!rShift) + rShift = 1; + if (!gShift) + gShift = 1; + if (!bShift) + bShift = 1; + if (pixel != mOldPixel || mColor.a != mOldAlpha) + { + const unsigned pb = (pixel & bMask) * mColor.a; + const unsigned pg = (pixel & gMask) * mColor.a; + const unsigned pr = (pixel & rMask) * mColor.a; + const unsigned a0 = (255 - mColor.a); + + const unsigned int a1 = a0 * bShift; + const unsigned int a2 = a0 * gShift; + const unsigned int a3 = a0 * rShift; + + for (int f = 0; f <= 0xff; f ++) + { + cB[f] = ((pb + f * a1) >> 8) & bMask; + cG[f] = ((pg + f * a2) >> 8) & gMask; + cR[f] = ((pr + f * a3) >> 8) & rMask; + } + + mOldPixel = pixel; + mOldAlpha = mColor.a; + } + + for (y = y1; y < y2; y++) + { + uint32_t *const p0 = reinterpret_cast<uint32_t*>( + static_cast<uint8_t*>(mWindow->pixels) + + y * mWindow->pitch); + for (x = x1; x < x2; x++) + { + uint32_t *const p = p0 + x; + const uint32_t dst = *p; + *p = cB[dst & bMask / bShift] + | cG[(dst & gMask) / gShift] + | cR[(dst & rMask) / rShift]; + } + } +#endif + break; + } + default: + break; + } + + SDL_UnlockSurface(mWindow); + } + else + { + SDL_Rect rect = + { + static_cast<int16_t>(area.x), + static_cast<int16_t>(area.y), + static_cast<uint16_t>(area.width), + static_cast<uint16_t>(area.height) + }; + + const uint32_t color = SDL_MapRGBA(mWindow->format, + static_cast<int8_t>(mColor.r), + static_cast<int8_t>(mColor.g), + static_cast<int8_t>(mColor.b), + static_cast<int8_t>(mColor.a)); + SDL_FillRect(mWindow, &rect, color); + } +} + +void SDLGraphics::_beginDraw() +{ + pushClipArea(gcn::Rectangle(0, 0, mRect.w, mRect.h)); +} + +void SDLGraphics::_endDraw() +{ + popClipArea(); +} + +bool SDLGraphics::pushClipArea(gcn::Rectangle area) +{ + const bool result = gcn::Graphics::pushClipArea(area); + const gcn::ClipRectangle &carea = mClipStack.top(); + const SDL_Rect rect = + { + static_cast<int16_t>(carea.x), + static_cast<int16_t>(carea.y), + static_cast<uint16_t>(carea.width), + static_cast<uint16_t>(carea.height) + }; + SDL_SetClipRect(mWindow, &rect); + + return result; +} + +void SDLGraphics::popClipArea() +{ + gcn::Graphics::popClipArea(); + + if (mClipStack.empty()) + return; + + const gcn::ClipRectangle &carea = mClipStack.top(); + const SDL_Rect rect = + { + static_cast<int16_t>(carea.x), + static_cast<int16_t>(carea.y), + static_cast<uint16_t>(carea.width), + static_cast<uint16_t>(carea.height) + }; + + SDL_SetClipRect(mWindow, &rect); +} + +void SDLGraphics::drawPoint(int x, int y) +{ + if (mClipStack.empty()) + return; + + const gcn::ClipRectangle& top = mClipStack.top(); + + x += top.xOffset; + y += top.yOffset; + + if (!top.isPointInRect(x, y)) + return; + + if (mAlpha) + SDLputPixelAlpha(mWindow, x, y, mColor); + else + SDLputPixel(mWindow, x, y, mColor); +} + +void SDLGraphics::drawHLine(int x1, int y, int x2) +{ + if (mClipStack.empty()) + return; + + const gcn::ClipRectangle& top = mClipStack.top(); + + const int xOffset = top.xOffset; + x1 += xOffset; + y += top.yOffset; + x2 += xOffset; + + const int topY = top.y; + if (y < topY || y >= topY + top.height) + return; + + if (x1 > x2) + { + x1 ^= x2; + x2 ^= x1; + x1 ^= x2; + } + + const int topX = top.x; + if (topX > x1) + { + if (topX > x2) + return; + + x1 = topX; + } + + const int sumX = topX + top.width; + if (sumX <= x2) + { + if (sumX <= x1) + return; + + x2 = sumX -1; + } + + const int bpp = mWindow->format->BytesPerPixel; + + SDL_LockSurface(mWindow); + + uint8_t *p = static_cast<uint8_t*>(mWindow->pixels) + + y * mWindow->pitch + x1 * bpp; + + const uint32_t pixel = SDL_MapRGB(mWindow->format, + static_cast<uint8_t>(mColor.r), + static_cast<uint8_t>(mColor.g), + static_cast<uint8_t>(mColor.b)); + switch (bpp) + { + case 1: + for (; x1 <= x2; ++x1) + *(p++) = static_cast<uint8_t>(pixel); + break; + + case 2: + { + uint16_t* q = reinterpret_cast<uint16_t*>(p); + for (; x1 <= x2; ++x1) + *(q++) = pixel; + break; + } + + case 3: + { + const uint8_t b0 = static_cast<uint8_t>((pixel >> 16) & 0xff); + const uint8_t b1 = static_cast<uint8_t>((pixel >> 8) & 0xff); + const uint8_t b2 = static_cast<uint8_t>(pixel & 0xff); +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + for (; x1 <= x2; ++x1) + { + p[0] = b0; + p[1] = b1; + p[2] = b2; + p += 3; + } +#else + for (; x1 <= x2; ++x1) + { + p[0] = b2; + p[1] = b1; + p[2] = b0; + p += 3; + } +#endif + break; + } + + case 4: + { + uint32_t *q = reinterpret_cast<uint32_t*>(p); + if (mAlpha) + { + unsigned char a = static_cast<unsigned char>(mColor.a); + unsigned char a1 = 255 - a; + const int b0 = (pixel & 0xff) * a; + const int g0 = (pixel & 0xff00) * a; + const int r0 = (pixel & 0xff0000) * a; + for (; x1 <= x2; ++x1) + { + const unsigned int b = (b0 + (*q & 0xff) * a1) >> 8; + const unsigned int g = (g0 + (*q & 0xff00) * a1) >> 8; + const unsigned int r = (r0 + (*q & 0xff0000) * a1) >> 8; + *q = (b & 0xff) | (g & 0xff00) | (r & 0xff0000); + + q++; + } + } + else + { + for (; x1 <= x2; ++x1) + *(q++) = pixel; + } + break; + } + default: + break; + } // end switch + + SDL_UnlockSurface(mWindow); +} + +void SDLGraphics::drawVLine(int x, int y1, int y2) +{ + if (mClipStack.empty()) + return; + + const gcn::ClipRectangle& top = mClipStack.top(); + + const int yOffset = top.yOffset; + x += top.xOffset; + y1 += yOffset; + y2 += yOffset; + + if (x < top.x || x >= top.x + top.width) + return; + + if (y1 > y2) + { + y1 ^= y2; + y2 ^= y1; + y1 ^= y2; + } + + if (top.y > y1) + { + if (top.y > y2) + return; + + y1 = top.y; + } + + const int sumY = top.y + top.height; + if (sumY <= y2) + { + if (sumY <= y1) + return; + + y2 = sumY - 1; + } + + const int bpp = mWindow->format->BytesPerPixel; + + SDL_LockSurface(mWindow); + + uint8_t *p = static_cast<uint8_t*>(mWindow->pixels) + + y1 * mWindow->pitch + x * bpp; + + const uint32_t pixel = SDL_MapRGB(mWindow->format, + static_cast<uint8_t>(mColor.r), + static_cast<uint8_t>(mColor.g), + static_cast<uint8_t>(mColor.b)); + + const int pitch = mWindow->pitch; + switch (bpp) + { + case 1: + for (; y1 <= y2; ++y1) + { + *p = static_cast<uint8_t>(pixel); + p += pitch; + } + break; + + case 2: + for (; y1 <= y2; ++ y1) + { + *reinterpret_cast<uint16_t*>(p) + = static_cast<uint16_t>(pixel); + p += pitch; + } + break; + + case 3: + { + const uint8_t b0 = static_cast<uint8_t>((pixel >> 16) & 0xff); + const uint8_t b1 = static_cast<uint8_t>((pixel >> 8) & 0xff); + const uint8_t b2 = static_cast<uint8_t>(pixel & 0xff); +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + for (; y1 <= y2; ++y1) + { + p[0] = b0; + p[1] = b1; + p[2] = b2; + p += pitch; + } +#else + for (; y1 <= y2; ++y1) + { + p[0] = b2; + p[1] = b1; + p[2] = b0; + p += pitch; + } +#endif + break; + } + + case 4: + { + if (mAlpha) + { + unsigned char a = static_cast<unsigned char>(mColor.a); + unsigned char a1 = 255 - a; + const int b0 = (pixel & 0xff) * a; + const int g0 = (pixel & 0xff00) * a; + const int r0 = (pixel & 0xff0000) * a; + for (; y1 <= y2; ++y1) + { + const unsigned int dst = *reinterpret_cast<uint32_t*>(p); + const unsigned int b = (b0 + (dst & 0xff) * a1) >> 8; + const unsigned int g = (g0 + (dst & 0xff00) * a1) >> 8; + const unsigned int r = (r0 + (dst & 0xff0000) * a1) >> 8; + *reinterpret_cast<uint32_t*>(p) = + (b & 0xff) | (g & 0xff00) | (r & 0xff0000); + + p += pitch; + } + } + else + { + for (; y1 <= y2; ++y1) + { + *reinterpret_cast<uint32_t*>(p) = pixel; + p += pitch; + } + } + break; + } + + default: + break; + } // end switch + + SDL_UnlockSurface(mWindow); +} + +void SDLGraphics::drawRectangle(const gcn::Rectangle &rectangle) +{ + const int x1 = rectangle.x; + const int x2 = x1 + rectangle.width - 1; + const int y1 = rectangle.y; + const int y2 = y1 + rectangle.height - 1; + + drawHLine(x1, y1, x2); + drawHLine(x1, y2, x2); + + drawVLine(x1, y1, y2); + drawVLine(x2, y1, y2); +} + +void SDLGraphics::drawLine(int x1, int y1, int x2, int y2) +{ + if (x1 == x2) + { + drawVLine(x1, y1, y2); + return; + } + if (y1 == y2) + { + drawHLine(x1, y1, x2); + return; + } + + // other cases not implimented +} + +bool SDLGraphics::setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame) +{ + setMainFlags(w, h, bpp, fs, hwaccel, resize, noFrame); + + if (!(mWindow = graphicsManager.createWindow(w, h, bpp, + getSoftwareFlags()))) + { + mRect.w = 0; + mRect.h = 0; + return false; + } + + mRect.w = static_cast<uint16_t>(mWindow->w); + mRect.h = static_cast<uint16_t>(mWindow->h); + + return videoInfo(); +} + +#endif // USE_SDL2 diff --git a/src/render/sdlgraphics.h b/src/render/sdlgraphics.h new file mode 100644 index 000000000..a7e4be85b --- /dev/null +++ b/src/render/sdlgraphics.h @@ -0,0 +1,155 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SDLGRAPHICS_H +#define SDLGRAPHICS_H + +#ifdef USE_SDL2 +#include "render/sdl2graphics.h" + +#else + +#include "render/graphics.h" + +#include "localconsts.h" + +class Image; +class ImageCollection; +class ImageVertexes; +class MapLayer; + +struct SDL_Surface; + +/** + * A central point of control for graphics. + */ +class SDLGraphics : public Graphics +{ + public: + /** + * Constructor. + */ + SDLGraphics(); + + A_DELETE_COPY(SDLGraphics) + + /** + * Destructor. + */ + virtual ~SDLGraphics(); + + void _beginDraw(); + + void _endDraw(); + + bool pushClipArea(gcn::Rectangle rect); + + void popClipArea(); + + virtual bool drawRescaledImage(const Image *const image, int srcX, + int srcY, int dstX, int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight, + const bool useColor = false); + + virtual void drawImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h); + + virtual void drawRescaledImagePattern(const Image *const image, + const int x, const int y, + const int w, const int h, + const int scaledWidth, + const int scaledHeight); + + virtual void calcImagePattern(ImageVertexes *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const; + + virtual void calcImagePattern(ImageCollection *const vert, + const Image *const image, + const int x, const int y, + const int w, const int h) const; + + virtual void calcTile(ImageVertexes *const vert, + const Image *const image, int x, int y) const; + + virtual void calcTileSDL(ImageVertexes *const vert, + int x, int y) const; + + virtual void calcTile(ImageCollection *const vertCol, + const Image *const image, + int x, int y); + + virtual void drawTile(const ImageVertexes *const vert); + + virtual void drawTile(const ImageCollection *const vertCol); + + virtual void updateScreen(); + + virtual SDL_Surface *getScreenshot() A_WARN_UNUSED; + + virtual bool drawNet(const int x1, const int y1, + const int x2, const int y2, + const int width, const int height); + + virtual bool calcWindow(ImageCollection *const vertCol, + const int x, const int y, + const int w, const int h, + const ImageRect &imgRect); + + void fillRectangle(const gcn::Rectangle &rect) override; + + void drawRectangle(const gcn::Rectangle &rect) override; + + void drawPoint(int x, int y) override; + + void drawLine(int x1, int y1, int x2, int y2) override; + + bool setVideoMode(const int w, const int h, const int bpp, + const bool fs, const bool hwaccel, + const bool resize, const bool noFrame); + + protected: + virtual bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor); + + int SDL_FakeUpperBlit(const SDL_Surface *const src, + SDL_Rect *const srcrect, + const SDL_Surface *const dst, + SDL_Rect *dstrect) const; + + void drawHLine(int x1, int y, int x2); + + void drawVLine(int x, int y1, int y2); + + uint32_t mOldPixel; + int mOldAlpha; +}; + +#endif // USE_SDL2 +#endif // SDLGRAPHICS_H diff --git a/src/render/surfacegraphics.cpp b/src/render/surfacegraphics.cpp new file mode 100644 index 000000000..9a8e51891 --- /dev/null +++ b/src/render/surfacegraphics.cpp @@ -0,0 +1,87 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "render/surfacegraphics.h" + +#include "main.h" + +#include "configuration.h" +#include "graphicsmanager.h" +#include "graphicsvertexes.h" +#include "logger.h" + +#include "resources/imagehelper.h" +#include "resources/surfaceimagehelper.h" + +#include <guichan/sdl/sdlpixel.hpp> + +#include "debug.h" + +SurfaceGraphics::SurfaceGraphics() : + Graphics(), + mBlitMode(BLIT_NORMAL), + mTarget(nullptr) +{ +} + +SurfaceGraphics::~SurfaceGraphics() +{ +} + +bool SurfaceGraphics::drawImage2(const Image *const image, int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor A_UNUSED) +{ + FUNC_BLOCK("Graphics::drawImage2", 1) + // Check that preconditions for blitting are met. + if (!mTarget || !image || !image->mSDLSurface) + return false; + + srcX += image->mBounds.x; + srcY += image->mBounds.y; + + SDL_Rect dstRect; + SDL_Rect srcRect; + dstRect.x = static_cast<int16_t>(dstX); + dstRect.y = static_cast<int16_t>(dstY); + srcRect.x = static_cast<int16_t>(srcX); + srcRect.y = static_cast<int16_t>(srcY); + srcRect.w = static_cast<uint16_t>(width); + srcRect.h = static_cast<uint16_t>(height); + +#ifdef USE_SDL2 + return !(SDL_BlitSurface(image->mSDLSurface, &srcRect, + mTarget, &dstRect) < 0); +#else + if (mBlitMode == BLIT_NORMAL) + { + return !(SDL_BlitSurface(image->mSDLSurface, &srcRect, + mTarget, &dstRect) < 0); + } + else + { + return !(SurfaceImageHelper::combineSurface( + image->mSDLSurface, &srcRect, mTarget, &dstRect) < 0); + } +#endif +} diff --git a/src/render/surfacegraphics.h b/src/render/surfacegraphics.h new file mode 100644 index 000000000..f5194946a --- /dev/null +++ b/src/render/surfacegraphics.h @@ -0,0 +1,185 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SURFACEGRAPHICS_H +#define SURFACEGRAPHICS_H + +#include "render/graphics.h" + +#include "localconsts.h" + +class Image; +class ImageCollection; +class ImageVertexes; +class MapLayer; + +struct SDL_Surface; + +/** + * A central point of control for graphics. + */ +class SurfaceGraphics : public Graphics +{ + public: + enum BlitMode + { + BLIT_NORMAL = 0, + BLIT_GFX + }; + + SurfaceGraphics(); + + A_DELETE_COPY(SurfaceGraphics) + + virtual ~SurfaceGraphics(); + + void setTarget(SDL_Surface *const target) + { mTarget = target; } + + SDL_Surface *getTarget() const + { return mTarget; } + + void _beginDraw() + { } + + void _endDraw() + { } + + bool pushClipArea(gcn::Rectangle rect A_UNUSED) + { return true; } + + void popClipArea() + { } + + bool drawRescaledImage(const Image *const image A_UNUSED, + int srcX A_UNUSED, int srcY A_UNUSED, + int dstX A_UNUSED, int dstY A_UNUSED, + const int width A_UNUSED, + const int height A_UNUSED, + const int desiredWidth A_UNUSED, + const int desiredHeight A_UNUSED, + const bool useColor A_UNUSED = false) + { return false; } + + void drawImagePattern(const Image *const image A_UNUSED, + const int x A_UNUSED, const int y A_UNUSED, + const int w A_UNUSED, const int h A_UNUSED) + { } + + void drawRescaledImagePattern(const Image *const image A_UNUSED, + const int x A_UNUSED, + const int y A_UNUSED, + const int w A_UNUSED, + const int h A_UNUSED, + const int scaledWidth A_UNUSED, + const int scaledHeight A_UNUSED) + { } + + void calcImagePattern(ImageVertexes *const vert A_UNUSED, + const Image *const image A_UNUSED, + const int x A_UNUSED, + const int y A_UNUSED, + const int w A_UNUSED, + const int h A_UNUSED) const + { } + + void calcImagePattern(ImageCollection *const vert A_UNUSED, + const Image *const image A_UNUSED, + const int x A_UNUSED, const int y A_UNUSED, + const int w A_UNUSED, const int h A_UNUSED) const + { } + + void calcTile(ImageVertexes *const vert A_UNUSED, + const Image *const image A_UNUSED, + int x A_UNUSED, int y A_UNUSED) const + { } + + void calcTileSDL(ImageVertexes *const vert A_UNUSED, + int x A_UNUSED, int y A_UNUSED) const + { } + + void calcTile(ImageCollection *const vertCol A_UNUSED, + const Image *const image A_UNUSED, + int x A_UNUSED, int y A_UNUSED) + { } + + void drawTile(const ImageVertexes *const vert A_UNUSED) + { } + + void drawTile(const ImageCollection *const vertCol A_UNUSED) + { } + + void updateScreen() + { } + + SDL_Surface *getScreenshot() A_WARN_UNUSED + { return nullptr; } + + bool drawNet(const int x1 A_UNUSED, const int y1 A_UNUSED, + const int x2 A_UNUSED, const int y2 A_UNUSED, + const int width A_UNUSED, const int height A_UNUSED) + { return false; } + + bool calcWindow(ImageCollection *const vertCol A_UNUSED, + const int x A_UNUSED, const int y A_UNUSED, + const int w A_UNUSED, const int h A_UNUSED, + const ImageRect &imgRect A_UNUSED) + { return false; } + + void setBlitMode(const BlitMode mode) + { mBlitMode = mode; } + + BlitMode getBlitMode() const A_WARN_UNUSED + { return mBlitMode; } + + void fillRectangle(const gcn::Rectangle &rect A_UNUSED) override + { } + + void drawRectangle(const gcn::Rectangle &rect A_UNUSED) override + { } + + void drawPoint(int x A_UNUSED, int y A_UNUSED) override + { } + + void drawLine(int x1 A_UNUSED, int y1 A_UNUSED, + int x2 A_UNUSED, int y2 A_UNUSED) override + { } + + bool setVideoMode(const int w A_UNUSED, const int h A_UNUSED, + const int bpp A_UNUSED, + const bool fs A_UNUSED, const bool hwaccel A_UNUSED, + const bool resize A_UNUSED, + const bool noFrame A_UNUSED) + { return false; } + + protected: + bool drawImage2(const Image *const image, + int srcX, int srcY, + int dstX, int dstY, + const int width, const int height, + const bool useColor); + + BlitMode mBlitMode; + SDL_Surface *mTarget; +}; + +#endif // SURFACEGRAPHICS_H |