Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/guichan/include/guichan/sdl/sdlgraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
};
Expand Down
130 changes: 79 additions & 51 deletions src/guichan/sdl/sdlgraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -77,6 +78,7 @@ namespace gcn
{
mAlpha = false;
mTarget = NULL;
mSrcDataTexture = NULL;
}

void SDLGraphics::_beginDraw()
Expand Down Expand Up @@ -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;
}
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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<int>(area.x, top.x);
int y1 = std::max<int>(area.y, top.y);
int x2 = std::min<int>(area.x + area.width, top.x + top.width);
int y2 = std::min<int>(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<int>(area.x, top.x);
int y1 = std::max<int>(area.y, top.y);
int x2 = std::min<int>(area.x + area.width, top.x + top.width);
int y2 = std::min<int>(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);
}
}
}

Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
4 changes: 3 additions & 1 deletion src/include/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/video/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
-- Includes
----------------------------------------------------------------------------*/

#include "SDL_render.h"
#include "stratagus.h"

#include "font.h"
Expand Down Expand Up @@ -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<SDL_Color> 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);
}

/**
Expand Down
Loading