diff --git a/src/guichan/include/guichan/sdl/sdlgraphics.h b/src/guichan/include/guichan/sdl/sdlgraphics.h index 2620509073..4aba8b685b 100644 --- a/src/guichan/include/guichan/sdl/sdlgraphics.h +++ b/src/guichan/include/guichan/sdl/sdlgraphics.h @@ -91,17 +91,6 @@ namespace gcn */ virtual void setTarget(SDL_Surface** surface); - /** - * Draws an SDL_Surface on the target surface. Normaly you'll - * use drawImage, but if you want to write SDL specific code - * this function might come in handy. - * - * NOTE: The clip areas will be taken into account. - */ - virtual void drawSDLSurface(SDL_Surface* surface, SDL_Rect source, - SDL_Rect destination); - - // Inherited from Graphics virtual void _beginDraw(); @@ -148,6 +137,7 @@ namespace gcn virtual void drawVLine(int x, int y1, int y2); SDL_Surface** mTarget; + SDL_Texture* mSrcDataTexture; // lazily initialized Color mColor; bool mAlpha; }; diff --git a/src/guichan/sdl/sdlgraphics.cpp b/src/guichan/sdl/sdlgraphics.cpp index 087e0587ec..efe925e89b 100644 --- a/src/guichan/sdl/sdlgraphics.cpp +++ b/src/guichan/sdl/sdlgraphics.cpp @@ -56,6 +56,7 @@ * For comments regarding functions please see the header file. */ +#include "SDL_render.h" #include "guichan/exception.h" #include "guichan/font.h" #include "guichan/sdl/sdlgraphics.h" @@ -77,6 +78,7 @@ namespace gcn { mAlpha = false; mTarget = NULL; + mSrcDataTexture = NULL; } void SDLGraphics::_beginDraw() @@ -110,7 +112,11 @@ namespace gcn rect.w = carea.width; rect.h = carea.height; - SDL_SetClipRect(*mTarget, &rect); + if (mTarget == &TheScreen) { + SDL_RenderSetClipRect(TheRenderer, &rect); + } else { + SDL_SetClipRect(*mTarget, &rect); + } return result; } @@ -131,7 +137,11 @@ namespace gcn rect.w = carea.width; rect.h = carea.height; - SDL_SetClipRect(*mTarget, &rect); + if (mTarget == &TheScreen) { + SDL_RenderSetClipRect(TheRenderer, &rect); + } else { + SDL_SetClipRect(*mTarget, &rect); + } } void SDLGraphics::drawImage(const Image* image, int srcX, @@ -147,17 +157,32 @@ namespace gcn src.h = height; dst.x = dstX + top.xOffset; dst.y = dstY + top.yOffset; + dst.w = width; + dst.h = height; - SDL_Surface* srcImage = (SDL_Surface*)image->_getData(); - - Color c = getColor(); - if (!SDL_SetSurfaceColorMod(srcImage, c.r, c.g, c.b)) { - SDL_SetSurfaceColorMod(srcImage, 255, 255, 255); - } - if (!SDL_SetSurfaceAlphaMod(srcImage, c.a)) { - SDL_SetSurfaceAlphaMod(srcImage, 255); + if (mTarget == &TheScreen) { + if (!mSrcDataTexture) { + mSrcDataTexture = SDL_CreateTextureFromSurface(TheRenderer, (SDL_Surface*)image->_getData()); + } + Color c = getColor(); + if (!SDL_SetTextureColorMod(mSrcDataTexture, c.r, c.g, c.b)) { + SDL_SetTextureColorMod(mSrcDataTexture, 255, 255, 255); + } + if (!SDL_SetTextureAlphaMod(mSrcDataTexture, c.a)) { + SDL_SetTextureAlphaMod(mSrcDataTexture, 255); + } + SDL_RenderCopy(TheRenderer, mSrcDataTexture, &src, &dst); + } else { + SDL_Surface* srcImage = (SDL_Surface*)image->_getData(); + Color c = getColor(); + if (!SDL_SetSurfaceColorMod(srcImage, c.r, c.g, c.b)) { + SDL_SetSurfaceColorMod(srcImage, 255, 255, 255); + } + if (!SDL_SetSurfaceAlphaMod(srcImage, c.a)) { + SDL_SetSurfaceAlphaMod(srcImage, 255); + } + SDL_BlitSurface(srcImage, &src, *mTarget, &dst); } - SDL_BlitSurface(srcImage, &src, *mTarget, &dst); } void SDLGraphics::fillRectangle(const Rectangle& rectangle) @@ -168,36 +193,40 @@ namespace gcn area.x += top.xOffset; area.y += top.yOffset; - if(!area.intersect(top) || mColor.a == 0) - { + if (!area.intersect(top) || mColor.a == 0) { return; } - if (mAlpha) - { - int x1 = std::max(area.x, top.x); - int y1 = std::max(area.y, top.y); - int x2 = std::min(area.x + area.width, top.x + top.width); - int y2 = std::min(area.y + area.height, top.y + top.height); - int x, y; - for (y = y1; y < y2; y++) - { - for (x = x1; x < x2; x++) - { - SDLputPixelAlpha(*mTarget, x, y, mColor); - } - } - } - else - { + if (mTarget == &TheScreen) { SDL_Rect rect; rect.x = area.x; rect.y = area.y; rect.w = area.width; rect.h = area.height; - - Uint32 color = SDL_MapRGBA((*mTarget)->format, mColor.r, mColor.g, mColor.b, mColor.a); - SDL_FillRect(*mTarget, &rect, color); + SDL_SetRenderDrawColor(TheRenderer, mColor.r, mColor.g, mColor.b, mAlpha ? mColor.a : 0); + SDL_RenderFillRect(TheRenderer, &rect); + } else { + if (mAlpha) { + int x1 = std::max(area.x, top.x); + int y1 = std::max(area.y, top.y); + int x2 = std::min(area.x + area.width, top.x + top.width); + int y2 = std::min(area.y + area.height, top.y + top.height); + int x, y; + for (y = y1; y < y2; y++) { + for (x = x1; x < x2; x++) { + SDLputPixelAlpha(*mTarget, x, y, mColor); + } + } + } else { + SDL_Rect rect; + rect.x = area.x; + rect.y = area.y; + rect.w = area.width; + rect.h = area.height; + + Uint32 color = SDL_MapRGBA((*mTarget)->format, mColor.r, mColor.g, mColor.b, mColor.a); + SDL_FillRect(*mTarget, &rect, color); + } } } @@ -207,16 +236,19 @@ namespace gcn x += top.xOffset; y += top.yOffset; - if(!top.isPointInRect(x,y)) + if (!top.isPointInRect(x,y)) { return; - - if (mAlpha) - { - SDLputPixelAlpha(*mTarget, x, y, mColor); } - else - { - SDLputPixel(*mTarget, x, y, mColor); + + if (mTarget == &TheScreen) { + SDL_SetRenderDrawColor(TheRenderer, mColor.r, mColor.g, mColor.b, mAlpha ? mColor.a : 0); + SDL_RenderDrawPoint(TheRenderer, x, y); + } else { + if (mAlpha) { + SDLputPixelAlpha(*mTarget, x, y, mColor); + } else { + SDLputPixel(*mTarget, x, y, mColor); + } } } @@ -339,6 +371,12 @@ namespace gcn x2 += top.xOffset; y2 += top.yOffset; + if (mTarget == &TheScreen) { + SDL_SetRenderDrawColor(TheRenderer, mColor.r, mColor.g, mColor.b, mAlpha ? mColor.a : 0); + SDL_RenderDrawLine(TheRenderer, x1, y1, x2, y2); + return; + } + // Draw a line with Bresenham int dx = ABS(x2 - x1); @@ -501,14 +539,4 @@ namespace gcn { return mColor; } - - void SDLGraphics::drawSDLSurface(SDL_Surface* surface, SDL_Rect source, - SDL_Rect destination) - { - ClipRectangle top = mClipStack.top(); - destination.x += top.xOffset; - destination.y += top.yOffset; - - SDL_BlitSurface(surface, &source, *mTarget, &destination); - } } diff --git a/src/include/video.h b/src/include/video.h index ba200f1454..9fadd9af09 100644 --- a/src/include/video.h +++ b/src/include/video.h @@ -97,7 +97,7 @@ class CGraphic : public gcn::Image }; protected: - CGraphic() : Surface(NULL), SurfaceFlip(NULL), frame_map(NULL), + CGraphic() : Surface(NULL), SurfaceFlip(NULL), Texture(NULL), TextureFlip(NULL), frame_map(NULL), Width(0), Height(0), NumFrames(1), GraphicWidth(0), GraphicHeight(0), Refs(1), Resized(false) { @@ -185,6 +185,8 @@ class CGraphic : public gcn::Image std::string HashFile; /// Filename used in hash SDL_Surface *Surface; /// Surface SDL_Surface *SurfaceFlip; /// Flipped surface + SDL_Texture *Texture; /// Surface + SDL_Texture *TextureFlip; /// Flipped surface frame_pos_t *frame_map; frame_pos_t *frameFlip_map; void GenFramesMap(); diff --git a/src/video/font.cpp b/src/video/font.cpp index f13f1b895a..8bffbe85e4 100644 --- a/src/video/font.cpp +++ b/src/video/font.cpp @@ -33,6 +33,7 @@ -- Includes ----------------------------------------------------------------------------*/ +#include "SDL_render.h" #include "stratagus.h" #include "font.h" @@ -138,10 +139,11 @@ static void VideoDrawChar(const CGraphic &g, int gx, int gy, int w, int h, int x, int y, const CFontColor &fc) { SDL_Rect srect = {Sint16(gx), Sint16(gy), Uint16(w), Uint16(h)}; - SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0}; + SDL_Rect drect = {Sint16(x), Sint16(y), Uint16(w), Uint16(h)}; std::vector sdlColors(fc.Colors, fc.Colors + MaxFontColors); SDL_SetPaletteColors(g.Surface->format->palette, &sdlColors[0], 0, MaxFontColors); - SDL_BlitSurface(g.Surface, &srect, TheScreen, &drect); + SDL_RenderCopy(TheRenderer, g.Texture, &srect, &drect); + // SDL_BlitSurface(g.Surface, &srect, TheScreen, &drect); } /** diff --git a/src/video/graphic.cpp b/src/video/graphic.cpp index 4c141df8d7..797c6dac1e 100644 --- a/src/video/graphic.cpp +++ b/src/video/graphic.cpp @@ -34,6 +34,8 @@ -- Includes ----------------------------------------------------------------------------*/ +#include "SDL_blendmode.h" +#include "SDL_render.h" #include "stratagus.h" #include @@ -96,9 +98,13 @@ void CGraphic::DrawSub(int gx, int gy, int w, int h, int x, int y, Assert(surface); SDL_Rect srect = {Sint16(gx), Sint16(gy), Uint16(w), Uint16(h)}; - SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0}; - - SDL_BlitSurface(Surface, &srect, surface, &drect); + SDL_Rect drect = {Sint16(x), Sint16(y), Uint16(w), Uint16(h)}; + + if (surface == TheScreen) { + SDL_RenderCopy(TheRenderer, Texture, &srect, &drect); + } else { + SDL_BlitSurface(Surface, &srect, surface, &drect); + } } /** @@ -122,25 +128,35 @@ void CGraphic::DrawSubCustomMod(int gx, int gy, int w, int h, int x, int y, Assert(surface); Assert(surface->format->BitsPerPixel == 32); + if (surface == TheScreen) { + SDL_Rect srcrect = {gx, gy, w, h}; + SDL_Rect dstrect = {x, y, w, h}; + Uint8 alpha; + SDL_GetTextureAlphaMod(Texture, &alpha); + SDL_SetTextureAlphaMod(Texture, alpha * param); + SDL_SetTextureBlendMode(Texture, SDL_BLENDMODE_ADD); + SDL_RenderCopy(TheRenderer, Texture, &srcrect, &dstrect); + SDL_SetTextureAlphaMod(Texture, alpha); + return; + } else { + size_t srcOffset = Surface->w * gy + gx; + size_t dstOffset = surface->w * y + x; - size_t srcOffset = Surface->w * gy + gx; - size_t dstOffset = surface->w * y + x; - - uint32_t *const src = reinterpret_cast(Surface->pixels); - uint32_t *const dst = reinterpret_cast(surface->pixels); + uint32_t *const src = reinterpret_cast(Surface->pixels); + uint32_t *const dst = reinterpret_cast(surface->pixels); - for (uint16_t posY = 0; posY < h; posY++) { - for (uint16_t posX = 0; posX < w; posX++) { - const uint32_t srcColor = src[srcOffset + posX]; - const uint32_t dstColor = dst[dstOffset + posX]; - const uint32_t resColor = modifier(srcColor, dstColor, param); + for (uint16_t posY = 0; posY < h; posY++) { + for (uint16_t posX = 0; posX < w; posX++) { + const uint32_t srcColor = src[srcOffset + posX]; + const uint32_t dstColor = dst[dstOffset + posX]; + const uint32_t resColor = modifier(srcColor, dstColor, param); - dst[dstOffset + posX] = resColor; + dst[dstOffset + posX] = resColor; + } + dstOffset += surface->w; + srcOffset += Surface->w; } - dstOffset += surface->w; - srcOffset += Surface->w; } - } @@ -167,8 +183,12 @@ void CGraphic::DrawSubClip(int gx, int gy, int w, int h, int x, int y, gy += y - oldy; SDL_Rect srect = {Sint16(gx), Sint16(gy), Uint16(w), Uint16(h)}; - SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0}; - SDL_BlitSurface(Surface, &srect, surface, &drect); + SDL_Rect drect = {Sint16(x), Sint16(y), Uint16(w), Uint16(h)}; + if (surface == TheScreen) { + SDL_RenderCopy(TheRenderer, Texture, &srect, &drect); + } else { + SDL_BlitSurface(Surface, &srect, surface, &drect); + } } /** @@ -190,10 +210,19 @@ void CGraphic::DrawSubTrans(int gx, int gy, int w, int h, int x, int y, Assert(surface); Uint8 oldalpha = 0xff; - SDL_GetSurfaceAlphaMod(Surface, &oldalpha); - SDL_SetSurfaceAlphaMod(Surface, alpha); + if (surface == TheScreen) { + SDL_GetTextureAlphaMod(Texture, &oldalpha); + SDL_SetTextureAlphaMod(Texture, alpha); + } else { + SDL_GetSurfaceAlphaMod(Surface, &oldalpha); + SDL_SetSurfaceAlphaMod(Surface, alpha); + } DrawSub(gx, gy, w, h, x, y, surface); - SDL_SetSurfaceAlphaMod(Surface, oldalpha); + if (surface == TheScreen) { + SDL_SetTextureAlphaMod(Texture, oldalpha); + } else { + SDL_SetSurfaceAlphaMod(Surface, oldalpha); + } } /** @@ -326,9 +355,13 @@ void CGraphic::DrawFrameX(unsigned frame, int x, int y, SDL_Surface *surface /*= TheScreen*/) const { SDL_Rect srect = {frameFlip_map[frame].x, frameFlip_map[frame].y, Uint16(Width), Uint16(Height)}; - SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0}; + SDL_Rect drect = {Sint16(x), Sint16(y), Uint16(Width), Uint16(Height)}; - SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); + if (surface == TheScreen) { + SDL_RenderCopy(TheRenderer, TextureFlip, &srect, &drect); + } else { + SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); + } } /** @@ -350,22 +383,31 @@ void CGraphic::DrawFrameClipX(unsigned frame, int x, int y, srect.x += x - oldx; srect.y += y - oldy; - SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0}; - - SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); + SDL_Rect drect = {Sint16(x), Sint16(y), srect.w, srect.h}; + if (surface == TheScreen) { + SDL_RenderCopy(TheRenderer, TextureFlip, &srect, &drect); + } else { + SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); + } } void CGraphic::DrawFrameTransX(unsigned frame, int x, int y, int alpha, SDL_Surface *surface /*= TheScreen*/) const { SDL_Rect srect = {frameFlip_map[frame].x, frameFlip_map[frame].y, Uint16(Width), Uint16(Height)}; - SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0}; + SDL_Rect drect = {Sint16(x), Sint16(y), Uint16(Width), Uint16(Height)}; Uint8 oldalpha = 0xff; - SDL_GetSurfaceAlphaMod(SurfaceFlip, &oldalpha); - - SDL_SetSurfaceAlphaMod(SurfaceFlip, alpha); - SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); - SDL_SetSurfaceAlphaMod(SurfaceFlip, oldalpha); + if (surface == TheScreen) { + SDL_GetTextureAlphaMod(TextureFlip, &oldalpha); + SDL_SetTextureAlphaMod(TextureFlip, alpha); + SDL_RenderCopy(TheRenderer, TextureFlip, &srect, &drect); + SDL_SetTextureAlphaMod(TextureFlip, oldalpha); + } else { + SDL_GetSurfaceAlphaMod(SurfaceFlip, &oldalpha); + SDL_SetSurfaceAlphaMod(SurfaceFlip, alpha); + SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); + SDL_SetSurfaceAlphaMod(SurfaceFlip, oldalpha); + } } void CGraphic::DrawFrameClipTransX(unsigned frame, int x, int y, int alpha, @@ -379,13 +421,19 @@ void CGraphic::DrawFrameClipTransX(unsigned frame, int x, int y, int alpha, srect.x += x - oldx; srect.y += y - oldy; - SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0}; + SDL_Rect drect = {Sint16(x), Sint16(y), srect.w, srect.h}; Uint8 oldalpha = 0xff; - SDL_GetSurfaceAlphaMod(SurfaceFlip, &oldalpha); - - SDL_SetSurfaceAlphaMod(SurfaceFlip, alpha); - SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); - SDL_SetSurfaceAlphaMod(SurfaceFlip, oldalpha); + if (surface == TheScreen) { + SDL_GetTextureAlphaMod(TextureFlip, &oldalpha); + SDL_SetTextureAlphaMod(TextureFlip, alpha); + SDL_RenderCopy(TheRenderer, TextureFlip, &srect, &drect); + SDL_SetTextureAlphaMod(TextureFlip, oldalpha); + } else { + SDL_GetSurfaceAlphaMod(SurfaceFlip, &oldalpha); + SDL_SetSurfaceAlphaMod(SurfaceFlip, alpha); + SDL_BlitSurface(SurfaceFlip, &srect, surface, &drect); + SDL_SetSurfaceAlphaMod(SurfaceFlip, oldalpha); + } } /** @@ -662,17 +710,21 @@ void CGraphic::Load(bool grayscale) return; } - CFile fp; const std::string name = LibraryFileName(File.c_str()); if (name.empty()) { perror("Cannot find file"); goto error; } - if (fp.open(name.c_str(), CL_OPEN_READ) == -1) { - perror("Can't open file"); - goto error; + Surface = IMG_Load(name.c_str()); + if (Surface == NULL) { + CFile fp; + if (fp.open(name.c_str(), CL_OPEN_READ) == -1) { + perror("Can't open file"); + goto error; + } + Surface = IMG_Load_RW(fp.as_SDL_RWops(), 0); + fp.close(); } - Surface = IMG_Load_RW(fp.as_SDL_RWops(), 0); if (Surface == NULL) { fprintf(stderr, "Couldn't load file %s: %s", name.c_str(), IMG_GetError()); goto error; @@ -680,7 +732,6 @@ void CGraphic::Load(bool grayscale) GraphicWidth = Surface->w; GraphicHeight = Surface->h; - fp.close(); if (Surface->format->BytesPerPixel == 1) { VideoPaletteListAdd(Surface); @@ -710,6 +761,9 @@ void CGraphic::Load(bool grayscale) } GenFramesMap(); + + Texture = SDL_CreateTextureFromSurface(TheRenderer, Surface); + return; error: @@ -756,10 +810,18 @@ void CGraphic::Free(CGraphic *g) --g->Refs; if (!g->Refs) { FreeSurface(&g->Surface); + if (g->Texture) { + SDL_DestroyTexture(g->Texture); + g->Texture = NULL; + } delete[] g->frame_map; g->frame_map = NULL; FreeSurface(&g->SurfaceFlip); + if (g->TextureFlip) { + SDL_DestroyTexture(g->TextureFlip); + g->TextureFlip = NULL; + } delete[] g->frameFlip_map; g->frameFlip_map = NULL; @@ -820,6 +882,8 @@ void CGraphic::Flip() SDL_UnlockSurface(Surface); SDL_UnlockSurface(s); + TextureFlip = SDL_CreateTextureFromSurface(TheRenderer, SurfaceFlip); + delete[] frameFlip_map; frameFlip_map = new frame_pos_t[NumFrames]; @@ -879,6 +943,7 @@ void CGraphic::Resize(int w, int h) memcpy(pal, Surface->format->palette->colors, sizeof(SDL_Color) * 256); SDL_FreeSurface(Surface); + SDL_DestroyTexture(Texture); Surface = SDL_CreateRGBSurfaceFrom(data, w, h, 8, w, 0, 0, 0, 0); if (Surface->format->BytesPerPixel == 1) { @@ -943,6 +1008,7 @@ void CGraphic::Resize(int w, int h) SDL_UnlockSurface(Surface); VideoPaletteListRemove(Surface); SDL_FreeSurface(Surface); + SDL_DestroyTexture(Texture); Surface = SDL_CreateRGBSurfaceFrom(data, w, h, 8 * bpp, w * bpp, Rmask, Gmask, Bmask, Amask); @@ -958,6 +1024,7 @@ void CGraphic::Resize(int w, int h) Assert(GraphicWidth / Width * GraphicHeight / Height == NumFrames); GenFramesMap(); + Texture = SDL_CreateTextureFromSurface(TheRenderer, Surface); } /** @@ -977,17 +1044,24 @@ void CGraphic::SetOriginalSize() FreeSurface(&Surface); Surface = NULL; } + if (Texture) { + SDL_DestroyTexture(Texture); + Texture = NULL; + } delete[] frame_map; frame_map = NULL; if (SurfaceFlip) { FreeSurface(&SurfaceFlip); SurfaceFlip = NULL; } + if (TextureFlip) { + SDL_DestroyTexture(TextureFlip); + TextureFlip = NULL; + } delete[] frameFlip_map; frameFlip_map = NULL; this->Width = this->Height = 0; - this->Surface = NULL; this->Load(); Resized = false; @@ -1042,6 +1116,8 @@ void CGraphic::SetPaletteColor(int idx, int r, int g, int b) { color.g = g; color.b = b; SDL_SetPaletteColors(Surface->format->palette, &color, idx, 1); + SDL_DestroyTexture(Texture); + Texture = SDL_CreateTextureFromSurface(TheRenderer, Surface); } void CGraphic::OverlayGraphic(CGraphic *other, bool mask) @@ -1241,6 +1317,14 @@ void CGraphic::MakeShadow(int xOffset, int yOffset) if (SurfaceFlip) { shearSurface(SurfaceFlip, xOffset, yOffset, NumFrames, frameFlip_map, Width, Height); } + + SDL_DestroyTexture(Texture); + Texture = SDL_CreateTextureFromSurface(TheRenderer, Surface); + + if (SurfaceFlip) { + SDL_DestroyTexture(TextureFlip); + TextureFlip = SDL_CreateTextureFromSurface(TheRenderer, SurfaceFlip); + } } void FreeGraphics() @@ -1353,6 +1437,8 @@ void CFiller::bits_map::Init(CGraphic *g) } SDL_UnlockSurface(s); + SDL_DestroyTexture(g->Texture); + g->Texture = SDL_CreateTextureFromSurface(TheRenderer, g->Surface); } void CFiller::Load() diff --git a/src/video/linedraw.cpp b/src/video/linedraw.cpp index 64e28dfeab..22a357d0a6 100644 --- a/src/video/linedraw.cpp +++ b/src/video/linedraw.cpp @@ -34,6 +34,8 @@ -- Includes ----------------------------------------------------------------------------*/ +#include "SDL_pixels.h" +#include "SDL_render.h" #include "stratagus.h" #include "video.h" @@ -68,7 +70,8 @@ static void (*VideoDoDrawTransPixel)(Uint32 color, int x, int y, unsigned char a */ static void VideoDoDrawPixel16(Uint32 color, int x, int y) { - ((Uint16 *)TheScreen->pixels)[x + y * Video.Width] = color; + throw "no 16 bit"; + // ((Uint16 *)TheScreen->pixels)[x + y * Video.Width] = color; } /** @@ -86,7 +89,10 @@ void VideoDrawPixel16(Uint32 color, int x, int y) */ static void VideoDoDrawPixel32(Uint32 color, int x, int y) { - ((Uint32 *)TheScreen->pixels)[x + y * Video.Width] = color; + Uint8 r, g, b, a; + SDL_GetRGBA(color, TheScreen->format, &r, &g, &b, &a); + SDL_SetRenderDrawColor(TheRenderer, r, g, b, a); + SDL_RenderDrawPoint(TheRenderer, x, y); } /** @@ -104,15 +110,16 @@ void VideoDrawPixel32(Uint32 color, int x, int y) */ static void VideoDoDrawTransPixel16(Uint32 color, int x, int y, unsigned char alpha) { + throw "no 16bit"; // Loses precision for speed - alpha = (255 - alpha) >> 3; + // alpha = (255 - alpha) >> 3; - Uint16 *p = &((Uint16 *)TheScreen->pixels)[x + y * Video.Width]; - color = (((color << 16) | color) & 0x07E0F81F); - unsigned long dp = *p; - dp = ((dp << 16) | dp) & 0x07E0F81F; - dp = ((((dp - color) * alpha) >> 5) + color) & 0x07E0F81F; - *p = (Uint16)((dp >> 16) | dp); + // Uint16 *p = &((Uint16 *)TheScreen->pixels)[x + y * Video.Width]; + // color = (((color << 16) | color) & 0x07E0F81F); + // unsigned long dp = *p; + // dp = ((dp << 16) | dp) & 0x07E0F81F; + // dp = ((((dp - color) * alpha) >> 5) + color) & 0x07E0F81F; + // *p = (Uint16)((dp >> 16) | dp); } /** @@ -130,20 +137,10 @@ void VideoDrawTransPixel16(Uint32 color, int x, int y, unsigned char alpha) */ static void VideoDoDrawTransPixel32(Uint32 color, int x, int y, unsigned char alpha) { - alpha = 255 - alpha; - - Uint32 *p = &((Uint32 *)TheScreen->pixels)[x + y * Video.Width]; - - const unsigned long sp2 = (color & 0xFF00FF00) >> 8; - color &= 0x00FF00FF; - - unsigned long dp1 = *p; - unsigned long dp2 = (dp1 & 0xFF00FF00) >> 8; - dp1 &= 0x00FF00FF; - - dp1 = ((((dp1 - color) * alpha) >> 8) + color) & 0x00FF00FF; - dp2 = ((((dp2 - sp2) * alpha) >> 8) + sp2) & 0x00FF00FF; - *p = (dp1 | (dp2 << 8)); + Uint8 r, g, b, a; + SDL_GetRGBA(color, TheScreen->format, &r, &g, &b, &a); + SDL_SetRenderDrawColor(TheRenderer, r, g, b, alpha); + SDL_RenderDrawPoint(TheRenderer, x, y); } /** @@ -297,96 +294,10 @@ void DrawTransHLineClip(Uint32 color, int x, int y, */ void DrawLine(Uint32 color, int sx, int sy, int dx, int dy) { - if (sx == dx) { - if (sy < dy) { - DrawVLine(color, sx, sy, dy - sy + 1); - } else { - DrawVLine(color, dx, dy, sy - dy + 1); - } - return; - } - - if (sy == dy) { - if (sx < dx) { - DrawHLine(color, sx, sy, dx - sx + 1); - } else { - DrawHLine(color, dx, dy, sx - dx + 1); - } - return; - } - - // exchange coordinates - if (sy > dy) { - std::swap(dx, sx); - std::swap(dy, sy); - } - int xlen; - int incr; - - int ylen = dy - sy; - - if (sx > dx) { - xlen = sx - dx; - incr = -1; - } else { - xlen = dx - sx; - incr = 1; - } - - int y = sy; - int x = sx; - - if (xlen > ylen) { - int p; - - if (sx > dx) { - std::swap(sx, dx); - y = dy; - } - - p = (ylen << 1) - xlen; - - Video.LockScreen(); - for (x = sx; x < dx; ++x) { - VideoDoDrawPixel(color, x, y); - if (p >= 0) { - y += incr; - p += (ylen - xlen) << 1; - } else { - p += (ylen << 1); - } - } - Video.UnlockScreen(); - return; - } - - if (ylen > xlen) { - int p = (xlen << 1) - ylen; - - Video.LockScreen(); - for (y = sy; y < dy; ++y) { - VideoDoDrawPixel(color, x, y); - if (p >= 0) { - x += incr; - p += (xlen - ylen) << 1; - } else { - p += (xlen << 1); - } - } - Video.UnlockScreen(); - return; - } - - // Draw a diagonal line - if (ylen == xlen) { - Video.LockScreen(); - while (y != dy) { - VideoDoDrawPixel(color, x, y); - x += incr; - ++y; - } - Video.UnlockScreen(); - } + Uint8 r, g, b, a; + SDL_GetRGBA(color, TheScreen->format, &r, &g, &b, &a); + SDL_SetRenderDrawColor(TheRenderer, r, g, b, a); + SDL_RenderDrawLine(TheRenderer, sx, sy, dx, dy); } /** @@ -394,93 +305,19 @@ void DrawLine(Uint32 color, int sx, int sy, int dx, int dy) */ void DrawLineClip(Uint32 color, int sx, int sy, int dx, int dy) { - if (sx == dx) { - if (sy < dy) { - DrawVLineClip(color, sx, sy, dy - sy + 1); - } else { - DrawVLineClip(color, dx, dy, sy - dy + 1); - } - return; - } - - if (sy == dy) { - if (sx < dx) { - DrawHLineClip(color, sx, sy, dx - sx + 1); - } else { - DrawHLineClip(color, dx, dy, sx - dx + 1); - } - return; - } - - // exchange coordinates - if (sy > dy) { - std::swap(dx, sx); - std::swap(dy, sy); - } - int ylen = dy - sy; - int xlen; - int incr; - - if (sx > dx) { - xlen = sx - dx; - incr = -1; - } else { - xlen = dx - sx; - incr = 1; - } - - int y = sy; - int x = sx; - - if (xlen > ylen) { - if (sx > dx) { - std::swap(dx, sx); - y = dy; - } - - int p = (ylen << 1) - xlen; - - Video.LockScreen(); - for (x = sx; x < dx; ++x) { - VideoDoDrawPixelClip(color, x, y); - if (p >= 0) { - y += incr; - p += (ylen - xlen) << 1; - } else { - p += (ylen << 1); - } - } - Video.UnlockScreen(); - return; - } - - if (ylen > xlen) { - int p = (xlen << 1) - ylen; - - Video.LockScreen(); - for (y = sy; y < dy; ++y) { - VideoDoDrawPixelClip(color, x, y); - if (p >= 0) { - x += incr; - p += (xlen - ylen) << 1; - } else { - p += (xlen << 1); - } - } - Video.UnlockScreen(); - return; - } - - // Draw a diagonal line - if (ylen == xlen) { - Video.LockScreen(); - while (y != dy) { - VideoDoDrawPixelClip(color, x, y); - x += incr; - ++y; - } - Video.UnlockScreen(); - } + Uint8 r, g, b, a; + SDL_GetRGBA(color, TheScreen->format, &r, &g, &b, &a); + SDL_SetRenderDrawColor(TheRenderer, r, g, b, a); + SDL_Rect clipRect; + clipRect.x = ClipX1; + clipRect.y = ClipY1; + clipRect.w = ClipX2 - ClipX1; + clipRect.h = ClipY2 - ClipY1; + SDL_Rect oldClip; + SDL_RenderGetClipRect(TheRenderer, &oldClip); + SDL_RenderSetClipRect(TheRenderer, &clipRect); + SDL_RenderDrawLine(TheRenderer, sx, sy, dx, dy); + SDL_RenderSetClipRect(TheRenderer, &oldClip); } /** @@ -565,8 +402,11 @@ void DrawTransRectangleClip(Uint32 color, int x, int y, */ void FillRectangle(Uint32 color, int x, int y, int w, int h) { + Uint8 r, g, b, a; + SDL_GetRGBA(color, TheScreen->format, &r, &g, &b, &a); + SDL_SetRenderDrawColor(TheRenderer, r, g, b, a); SDL_Rect drect = {Sint16(x), Sint16(y), Uint16(w), Uint16(h)}; - SDL_FillRect(TheScreen, &drect, color); + SDL_RenderDrawRect(TheRenderer, &drect); } /** @@ -576,17 +416,15 @@ void FillRectangleClip(Uint32 color, int x, int y, int w, int h) { SDL_Rect oldrect; - SDL_Rect newrect; - - SDL_GetClipRect(TheScreen, &oldrect); - newrect.x = ClipX1; - newrect.y = ClipY1; - newrect.w = ClipX2 + 1 - ClipX1; - newrect.h = ClipY2 + 1 - ClipY1; - - SDL_SetClipRect(TheScreen, &newrect); - FillRectangle(color, x, y, w, h); - SDL_SetClipRect(TheScreen, &oldrect); + SDL_Rect clipRect = {ClipX1, ClipY1, ClipX2 - ClipX1, ClipY2 - ClipY1}; + SDL_Rect drawRect = {x, y, w, h}; + Uint8 r, g, b, a; + SDL_GetRGBA(color, TheScreen->format, &r, &g, &b, &a); + SDL_SetRenderDrawColor(TheRenderer, r, g, b, a); + SDL_RenderGetClipRect(TheRenderer, &oldrect); + SDL_RenderSetClipRect(TheRenderer, &clipRect); + SDL_RenderFillRect(TheRenderer, &drawRect); + SDL_RenderSetClipRect(TheRenderer, &oldrect); } /** @@ -595,17 +433,16 @@ void FillRectangleClip(Uint32 color, int x, int y, void FillTransRectangle(Uint32 color, int x, int y, int w, int h, unsigned char alpha) { - int ex = x + w; - int ey = y + h; - int sx = x; - - Video.LockScreen(); - for (; y < ey; ++y) { - for (x = sx; x < ex; ++x) { - VideoDoDrawTransPixel(color, x, y, alpha); - } - } - Video.UnlockScreen(); + SDL_Rect oldrect; + SDL_Rect clipRect = {ClipX1, ClipY1, ClipX2 - ClipX1, ClipY2 - ClipY1}; + SDL_Rect drawRect = {x, y, w, h}; + Uint8 r, g, b, a; + SDL_GetRGBA(color, TheScreen->format, &r, &g, &b, &a); + SDL_SetRenderDrawColor(TheRenderer, r, g, b, alpha); + SDL_RenderGetClipRect(TheRenderer, &oldrect); + SDL_RenderSetClipRect(TheRenderer, &clipRect); + SDL_RenderFillRect(TheRenderer, &drawRect); + SDL_RenderSetClipRect(TheRenderer, &oldrect); } /** diff --git a/src/video/sdl.cpp b/src/video/sdl.cpp index edd80fb92a..5bb7937d91 100644 --- a/src/video/sdl.cpp +++ b/src/video/sdl.cpp @@ -798,27 +798,24 @@ void RealizeVideoMemory() if (dummyRenderer) { return; } - if (NumRects) { - //SDL_UpdateWindowSurfaceRects(TheWindow, Rects, NumRects); - SDL_UpdateTexture(TheTexture, NULL, TheScreen->pixels, TheScreen->pitch); - if (!RenderWithShader(TheRenderer, TheWindow, TheTexture)) { - SDL_RenderClear(TheRenderer); - //for (int i = 0; i < NumRects; i++) - // SDL_UpdateTexture(TheTexture, &Rects[i], TheScreen->pixels, TheScreen->pitch); - SDL_RenderCopy(TheRenderer, TheTexture, NULL, NULL); - if (EnableDebugPrint) { - // show a bar representing fps scaled by 10 - SDL_SetRenderDrawColor(TheRenderer, 255, 0, 0, 255); - Uint32 nextTick = SDL_GetTicks(); - double fps = 10000.0 / (nextTick - LastTick); - SDL_RenderDrawLine(TheRenderer, 0, 0, floorl(fps), 0); - SDL_SetRenderDrawColor(TheRenderer, 0, 0, 0, 255); - LastTick = nextTick; - } - SDL_RenderPresent(TheRenderer); - } - NumRects = 0; - } + if (EnableDebugPrint) { + // show a bar representing fps scaled by 10 + SDL_SetRenderDrawColor(TheRenderer, 255, 0, 0, 255); + Uint32 nextTick = SDL_GetTicks(); + double fps = 10000.0 / (nextTick - LastTick); + SDL_RenderDrawLine(TheRenderer, 0, 0, floorl(fps), 0); + SDL_SetRenderDrawColor(TheRenderer, 0, 0, 0, 255); + LastTick = nextTick; + } + if (!RenderWithShader(TheRenderer, TheWindow, TheTexture)) { + SDL_SetRenderTarget(TheRenderer, NULL); + SDL_RenderCopy(TheRenderer, TheTexture, NULL, NULL); + // SDL_UpdateTexture(TheTexture, NULL, TheScreen->pixels, TheScreen->pitch); + // SDL_RenderCopy(TheRenderer, TheTexture, NULL, NULL); + SDL_RenderPresent(TheRenderer); + } + SDL_SetRenderTarget(TheRenderer, TheTexture); + // SDL_RenderClear(TheRenderer); if (!Preference.HardwareCursor) { HideCursor(); } diff --git a/src/video/shaders.cpp b/src/video/shaders.cpp index 4f7dad8215..16ec3a475f 100644 --- a/src/video/shaders.cpp +++ b/src/video/shaders.cpp @@ -368,8 +368,8 @@ bool RenderWithShader(SDL_Renderer *renderer, SDL_Window* win, SDL_Texture* back const GLfloat minu = 0.0f; const GLfloat maxu = 1.0f; - const GLfloat minv = 0.0f; - const GLfloat maxv = 1.0f; + const GLfloat minv = 1.0f; + const GLfloat maxv = 0.0f; lazyGlMatrixMode(GL_PROJECTION); lazyGlLoadIdentity(); diff --git a/src/video/video.cpp b/src/video/video.cpp index 2a7df52796..a7494b8cb0 100644 --- a/src/video/video.cpp +++ b/src/video/video.cpp @@ -301,8 +301,9 @@ bool CVideo::ResizeScreen(int w, int h) } TheTexture = SDL_CreateTexture(TheRenderer, SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, + SDL_TEXTUREACCESS_TARGET, w, h); + SDL_SetRenderTarget(TheRenderer, TheTexture); SetClipping(0, 0, w - 1, h - 1);