Skip to content

Commit

Permalink
Rename Graphic::Surface into Graphic::mSurface (and add accessors…
Browse files Browse the repository at this point in the history
…) to prepare guichan upgrade.
  • Loading branch information
Jarod42 committed Dec 6, 2023
1 parent e10832d commit 12e1bb5
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 180 deletions.
9 changes: 6 additions & 3 deletions src/include/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,23 @@ class CGraphic : public gcn::Image
// minor programmatic editing features
void OverlayGraphic(CGraphic *other, bool mask = false);

bool IsLoaded(bool flipped = false) const { return Surface != nullptr && (!flipped || SurfaceFlip != nullptr); }
bool IsLoaded(bool flipped = false) const { return mSurface != nullptr && (!flipped || SurfaceFlip != nullptr); }

//guichan
void *_getData() const override { return Surface; }
void *_getData() const override { return mSurface; }
int getWidth() const override { return Width; }
int getHeight() const override { return Height; }

SDL_Surface *getSurface() const { return mSurface; }
void setSurface(SDL_Surface *surface) { mSurface = surface; }

private:
void ExpandFor(const uint16_t numOfFramesToAdd);

SDL_Surface *mSurface = nullptr; /// Surface
public:
fs::path File; /// Filename
std::string HashFile; /// Filename used in hash
SDL_Surface *Surface = nullptr; /// Surface
SDL_Surface *SurfaceFlip = nullptr; /// Flipped surface
frame_pos_t *frame_map = nullptr;
frame_pos_t *frameFlip_map = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/map/fow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ void CFogOfWar::InitTiled()
SDL_FillRect(TileOfFogOnly, nullptr, Settings.FogColorSDL | uint32_t(Settings.ExploredOpacity) << ASHIFT);
}

SDL_Surface * const newFogSurface = SDL_ConvertSurfaceFormat(CFogOfWar::TiledFogSrc->Surface,
SDL_Surface * const newFogSurface = SDL_ConvertSurfaceFormat(CFogOfWar::TiledFogSrc->getSurface(),
SDL_MasksToPixelFormatEnum(32, RMASK, GMASK, BMASK, AMASK), 0);
TiledAlphaFog = CGraphic::New("");
TiledAlphaFog->Surface = newFogSurface;
TiledAlphaFog->setSurface(newFogSurface);
TiledAlphaFog->Width = PixelTileSize.x;
TiledAlphaFog->Height = PixelTileSize.y;
TiledAlphaFog->GraphicWidth = newFogSurface->w;
TiledAlphaFog->GraphicHeight = newFogSurface->h;
TiledAlphaFog->NumFrames = 16;
TiledAlphaFog->GenFramesMap();
SDL_SetSurfaceBlendMode(TiledAlphaFog->Surface, SDL_BLENDMODE_BLEND);
SDL_SetSurfaceBlendMode(TiledAlphaFog->getSurface(), SDL_BLENDMODE_BLEND);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/map/minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void CMinimap::Create()
}

// Palette updated from UpdateMinimapTerrain()
SDL_PixelFormat *f = Map.TileGraphic->Surface->format;
SDL_PixelFormat *f = Map.TileGraphic->getSurface()->format;
MinimapTerrainSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, W, H, f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask);
MinimapSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, W, H, 32, RMASK, GMASK, BMASK, 0);
MinimapFogSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, W, H, 32, RMASK, GMASK, BMASK, AMASK);
Expand All @@ -153,10 +153,10 @@ void CMinimap::Create()
*/
static inline Uint8 *GetTileGraphicPixel(int xofs, int yofs, int mx, int my, int scalex, int scaley, int bpp)
{
Uint8 *pixels = (Uint8 *)Map.TileGraphic->Surface->pixels;
Uint8 *pixels = (Uint8 *) Map.TileGraphic->getSurface()->pixels;
int x = (xofs + 7 + ((mx * SCALE_PRECISION) % scalex) / SCALE_PRECISION * 8);
int y = (yofs + 6 + ((my * SCALE_PRECISION) % scaley) / SCALE_PRECISION * 8);
return &pixels[x * bpp + y * Map.TileGraphic->Surface->pitch];
return &pixels[x * bpp + y * Map.TileGraphic->getSurface()->pitch];
}

/**
Expand All @@ -172,17 +172,19 @@ void CMinimap::UpdateTerrain()
if (!scaley) {
scaley = 1;
}
const int bpp = Map.TileGraphic->Surface->format->BytesPerPixel;
const int bpp = Map.TileGraphic->getSurface()->format->BytesPerPixel;

if (bpp == 1) {
SDL_SetPaletteColors(MinimapTerrainSurface->format->palette,
Map.TileGraphic->Surface->format->palette->colors, 0, 256);
Map.TileGraphic->getSurface()->format->palette->colors,
0,
256);
}

const int tilepitch = Map.TileGraphic->Surface->w / PixelTileSize.x;
const int tilepitch = Map.TileGraphic->getSurface()->w / PixelTileSize.x;

Assert(SDL_MUSTLOCK(MinimapTerrainSurface) == 0);
Assert(SDL_MUSTLOCK(Map.TileGraphic->Surface) == 0);
Assert(SDL_MUSTLOCK(Map.TileGraphic->getSurface()) == 0);

//
// Pixel 7,6 7,14, 15,6 15,14 are taken for the minimap picture.
Expand Down Expand Up @@ -247,8 +249,8 @@ void CMinimap::UpdateXY(const Vec2i &pos)
scaley = 1;
}

const int tilepitch = Map.TileGraphic->Surface->w / PixelTileSize.x;
const int bpp = Map.TileGraphic->Surface->format->BytesPerPixel;
const int tilepitch = Map.TileGraphic->getSurface()->w / PixelTileSize.x;
const int bpp = Map.TileGraphic->getSurface()->format->BytesPerPixel;

//
// Pixel 7,6 7,14, 15,6 15,14 are taken for the minimap picture.
Expand Down
34 changes: 17 additions & 17 deletions src/map/script_tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1291,23 +1291,23 @@ void CTilesetGraphicGenerator::parseModifier(lua_State *luaStack, const int argP
**/
sdl2::SurfacePtr CTilesetGraphicGenerator::newBlankImage() const
{
const SDL_PixelFormat *format = SrcTilesetGraphic->Surface->format;

sdl2::SurfacePtr blankImg {SDL_CreateRGBSurface(SrcTilesetGraphic->Surface->flags,
SrcTileset->getPixelTileSize().x,
SrcTileset->getPixelTileSize().y,
format->BitsPerPixel,
format->Rmask,
format->Gmask,
format->Bmask,
format->Amask)};
uint32_t colorKey = 0;
if (!SDL_GetColorKey(SrcTilesetGraphic->Surface, &colorKey)) {
SDL_SetColorKey(blankImg.get(), SDL_TRUE, colorKey);
}
if (format->palette) {
SDL_SetSurfacePalette(blankImg.get(), format->palette);
}
const SDL_PixelFormat *format = SrcTilesetGraphic->getSurface()->format;

sdl2::SurfacePtr blankImg{SDL_CreateRGBSurface(SrcTilesetGraphic->getSurface()->flags,
SrcTileset->getPixelTileSize().x,
SrcTileset->getPixelTileSize().y,
format->BitsPerPixel,
format->Rmask,
format->Gmask,
format->Bmask,
format->Amask)};
uint32_t colorKey = 0;
if (!SDL_GetColorKey(SrcTilesetGraphic->getSurface(), &colorKey)) {
SDL_SetColorKey(blankImg.get(), SDL_TRUE, colorKey);
}
if (format->palette) {
SDL_SetSurfacePalette(blankImg.get(), format->palette);
}
return blankImg;
}

Expand Down
9 changes: 5 additions & 4 deletions src/stratagus/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,13 +1252,14 @@ void GraphicPlayerPixels(int colorIndex, const CGraphic &sprite)
{
Assert(PlayerColorIndexCount);

Assert(SDL_MUSTLOCK(sprite.Surface) == 0);
Assert(SDL_MUSTLOCK(sprite.getSurface()) == 0);
// TODO: This vector allocation is costly in profiles
std::vector<SDL_Color> sdlColors = PlayerColorsSDL[colorIndex];
Assert(!sprite.Surface->format->palette || sprite.Surface->format->palette->ncolors > PlayerColorIndexStart + PlayerColorIndexCount);
SDL_SetPaletteColors(sprite.Surface->format->palette, &sdlColors[0], PlayerColorIndexStart, PlayerColorIndexCount);
const auto palette = sprite.getSurface()->format->palette;
Assert(!palette || palette->ncolors > PlayerColorIndexStart + PlayerColorIndexCount);
SDL_SetPaletteColors(palette, &sdlColors[0], PlayerColorIndexStart, PlayerColorIndexCount);
if (sprite.SurfaceFlip) {
SDL_SetPaletteColors(sprite.SurfaceFlip->format->palette, &sdlColors[0], PlayerColorIndexStart, PlayerColorIndexCount);
SDL_SetPaletteColors(palette, &sdlColors[0], PlayerColorIndexStart, PlayerColorIndexCount);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/ui/icons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ static void ApplyPaletteSwaps(const std::vector<PaletteSwap> &swaps, const CUnit
if (unit.Variable[varIdx].Enable) {
int value = def ? unit.Variable[varIdx].Max : unit.Variable[varIdx].Value;
const SDL_Color *colors = swap.GetColorsForPercentAndAlternative(value, unit.Variable[varIdx].Max, UnitNumber(unit));
SDL_SetPaletteColors(graphic->Surface->format->palette, colors, swap.GetColorIndexStart(), swap.GetColorCount());
SDL_SetPaletteColors(graphic->getSurface()->format->palette,
colors,
swap.GetColorIndexStart(),
swap.GetColorCount());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/video/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ SDL_Cursor *CCursor::GetSDLCursor()
SDL_Rect srect = {G->frame_map[i].x, G->frame_map[i].y, G->getWidth(), G->getHeight()};

SDL_Surface *intermediate = SDL_CreateRGBSurface(0, srect.w, srect.h, 32, RMASK, GMASK, BMASK, AMASK);
SDL_BlitSurface(G->Surface, &srect, intermediate, nullptr);
SDL_BlitSurface(G->getSurface(), &srect, intermediate, nullptr);

SDL_Surface *cursorFrame = SDL_CreateRGBSurface(0, w, h, 32, RMASK, GMASK, BMASK, AMASK);
SDL_BlitScaled(intermediate, nullptr, cursorFrame, nullptr);
Expand Down
27 changes: 14 additions & 13 deletions src/video/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ static void VideoDrawChar(const CGraphic &g,
{
SDL_Rect srect = {Sint16(gx), Sint16(gy), Uint16(w), Uint16(h)};
SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0};
SDL_SetPaletteColors(g.Surface->format->palette, fc.Colors.data(), 0, fc.Colors.size());
SDL_BlitSurface(g.Surface, &srect, TheScreen, &drect);
SDL_SetPaletteColors(g.getSurface()->format->palette, fc.Colors.data(), 0, fc.Colors.size());
SDL_BlitSurface(g.getSurface(), &srect, TheScreen, &drect);
}

/**
Expand Down Expand Up @@ -868,17 +868,18 @@ void CFont::MeasureWidths()
std::fill(std::begin(CharWidth), std::end(CharWidth), 0);
CharWidth[0] = G->Width / 2; // a reasonable value for SPACE
Uint32 ckey = 0;
const int ipr = G->Surface->w / G->Width; // images per row
const unsigned char* lsp = (const unsigned char *) G->Surface->pixels +
G->Surface->pitch * G->GraphicHeight; // last surface pointer + 1
const int ipr = G->getSurface()->w / G->Width; // images per row
const unsigned char *lsp = (const unsigned char *) G->getSurface()->pixels
+ G->getSurface()->pitch * G->GraphicHeight; // last surface pointer + 1

SDL_LockSurface(G->Surface);
SDL_GetColorKey(G->Surface, &ckey);
SDL_LockSurface(G->getSurface());
SDL_GetColorKey(G->getSurface(), &ckey);
for (int y = 1; y < maxy; ++y) {
const unsigned char *sp = (const unsigned char *)G->Surface->pixels +
(y / ipr) * G->Surface->pitch * G->Height +
(y % ipr) * G->Width - 1; // start pointer of glyph
const unsigned char *gp = sp + G->Surface->pitch * (G->Height - 1) + G->Width; // last pointer of glyph + 1
const unsigned char *sp = (const unsigned char *) G->getSurface()->pixels
+ (y / ipr) * G->getSurface()->pitch * G->Height
+ (y % ipr) * G->Width - 1; // start pointer of glyph
const unsigned char *gp =
sp + G->getSurface()->pitch * (G->Height - 1) + G->Width; // last pointer of glyph + 1
// Bail out if no letters left
if (gp >= lsp) {
break;
Expand All @@ -895,10 +896,10 @@ void CFont::MeasureWidths()
CharWidth[y] = std::max<char>(CharWidth[y], lp - sp);
}
}
sp += G->Surface->pitch;
sp += G->getSurface()->pitch;
}
}
SDL_UnlockSurface(G->Surface);
SDL_UnlockSurface(G->getSurface());
}

void CFont::Load()
Expand Down
Loading

0 comments on commit 12e1bb5

Please sign in to comment.