Skip to content

Commit

Permalink
More clearly separated the color key from the general transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Apr 16, 2024
1 parent 1d4dbf3 commit 4ce02ed
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 46 deletions.
20 changes: 10 additions & 10 deletions src/common/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ bool gfx_loadfullskin(gfxSprite ** gSprites, const std::string& filename, const
return true;
}

SDL_Surface * gfx_createteamcoloredsurface(SDL_Surface * sImage, short iColor, const RGBA& rgba)
SDL_Surface * gfx_createteamcoloredsurface(SDL_Surface * sImage, short iColor, const RGB& key, Uint8 alpha)
{
SDL_Surface * sTempImage = SDL_CreateRGBSurface(sImage->flags, iColor == -2 ? sImage->w << 2 : sImage->w, iColor == -1 ? sImage->h << 2 : sImage->h, sImage->format->BitsPerPixel, sImage->format->Rmask, sImage->format->Gmask, sImage->format->Bmask, sImage->format->Amask);

Expand Down Expand Up @@ -356,13 +356,13 @@ SDL_Surface * gfx_createteamcoloredsurface(SDL_Surface * sImage, short iColor, c
SDL_UnlockSurface(sImage);
SDL_UnlockSurface(sTempImage);

if ( SDL_SETCOLORKEY(sTempImage, SDL_TRUE, SDL_MapRGB(sTempImage->format, rgba.r, rgba.g, rgba.b)) < 0 ) {
if ( SDL_SETCOLORKEY(sTempImage, SDL_TRUE, SDL_MapRGB(sTempImage->format, key.r, key.g, key.b)) < 0 ) {
printf("\n ERROR: Couldn't set ColorKey + RLE for new team colored surface: %s\n", SDL_GetError());
return NULL;
}

if (rgba.a < 255) {
if (SDL_SETALPHABYTE(sTempImage, SDL_TRUE, rgba.a) < 0) {
if (alpha < 255) {
if (SDL_SETALPHABYTE(sTempImage, SDL_TRUE, alpha) < 0) {
cout << endl << " ERROR: Couldn't set per-surface alpha: " << SDL_GetError() << endl;
return NULL;
}
Expand Down Expand Up @@ -393,7 +393,7 @@ bool gfx_loadteamcoloredimage(gfxSprite ** gSprites, const std::string& filename
}

for (short k = 0; k < 4; k++) {
SDL_Surface * sTeamColoredSurface = gfx_createteamcoloredsurface(sImage, k, rgb.withAlpha(a));
SDL_Surface * sTeamColoredSurface = gfx_createteamcoloredsurface(sImage, k, rgb, a);

if (sTeamColoredSurface == NULL) {
cout << endl << " ERROR: Couldn't create menu skin from " << filename << ": " << SDL_GetError() << endl;
Expand All @@ -411,7 +411,7 @@ bool gfx_loadteamcoloredimage(gfxSprite ** gSprites, const std::string& filename
return true;
}

bool gfx_loadteamcoloredimage(gfxSprite * gSprites, const std::string& filename, const RGBA& rgba, bool fVertical, bool fWrap)
bool gfx_loadteamcoloredimage(gfxSprite * gSprites, const std::string& filename, const RGB& rgb, Uint8 a, bool fVertical, bool fWrap)
{
//Load the image into a surface
SDL_Surface * sImage = IMG_Load(filename.c_str());
Expand All @@ -421,7 +421,7 @@ bool gfx_loadteamcoloredimage(gfxSprite * gSprites, const std::string& filename,
return false;
}

SDL_Surface * sTeamColoredSurface = gfx_createteamcoloredsurface(sImage, fVertical ? -1 : -2, rgba);
SDL_Surface * sTeamColoredSurface = gfx_createteamcoloredsurface(sImage, fVertical ? -1 : -2, rgb, a);

if (sTeamColoredSurface == NULL) {
cout << endl << " ERROR: Couldn't create menu skin from " << filename << ": " << SDL_GetError() << endl;
Expand Down Expand Up @@ -580,12 +580,12 @@ void gfx_drawpreview(

bool gfx_loadteamcoloredimage(gfxSprite* sprites, const std::string& path, bool fVertical, bool fWrap)
{
return gfx_loadteamcoloredimage(sprites, path, colors::MAGENTA.withAlpha(255), fVertical, fWrap);
return gfx_loadteamcoloredimage(sprites, path, colors::MAGENTA, 255, fVertical, fWrap);
}

bool gfx_loadteamcoloredimage(gfxSprite* sprites, const std::string& path, Uint8 a, bool fVertical, bool fWrap)
{
return gfx_loadteamcoloredimage(sprites, path, colors::MAGENTA.withAlpha(a), fVertical, fWrap);
return gfx_loadteamcoloredimage(sprites, path, colors::MAGENTA, a, fVertical, fWrap);
}

bool gfx_loadimagenocolorkey(gfxSprite* sprite, const std::string& path)
Expand All @@ -600,7 +600,7 @@ bool gfx_loadimage(gfxSprite& gSprite, const std::string& path, bool fWrap)

bool gfx_loadimage(gfxSprite& gSprite, const std::string& path, Uint8 alpha, bool fWrap)
{
bool success = gSprite.init(path, colors::MAGENTA.withAlpha(alpha));
bool success = gSprite.init(path, colors::MAGENTA, alpha);

if (success)
gSprite.SetWrap(fWrap);
Expand Down
2 changes: 1 addition & 1 deletion src/common/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void gfx_drawpreview(SDL_Surface * surface, short dstX, short dstY, short srcX,
bool gfx_loadfullskin(gfxSprite ** gSprites, const std::string& filename, const RGB& colorkey, short colorScheme);
bool gfx_loadmenuskin(gfxSprite ** gSprite, const std::string& filename, const RGB& colorkey, short colorScheme, bool fLoadBothDirections);

bool gfx_loadteamcoloredimage(gfxSprite * gSprites, const std::string& filename, const RGBA& rgba, bool fVertical, bool fWrap);
bool gfx_loadteamcoloredimage(gfxSprite * gSprites, const std::string& filename, const RGB& rgb, Uint8 a, bool fVertical, bool fWrap);
//Load image into an array of 4 gfxSprites, each with it's own team color
bool gfx_loadteamcoloredimage(gfxSprite ** gSprites, const std::string& filename, const RGB& rgb, Uint8 a, bool fWrap);

Expand Down
13 changes: 0 additions & 13 deletions src/common/gfx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,11 @@
#include <cstdint>


/// Represents an RGBA color.
struct RGBA {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};


/// Represents a solid RGB color.
struct RGB {
uint8_t r;
uint8_t g;
uint8_t b;

constexpr RGBA withAlpha(uint8_t a) const {
return RGBA { r, g, b, a };
}
};


Expand Down
10 changes: 5 additions & 5 deletions src/common/gfx/gfxSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void gfxSprite::freeSurface()
//
// Color keyed without alpha
//
bool gfxSprite::init(const std::string& filename, const RGB& rgb)
bool gfxSprite::init(const std::string& filename, const RGB& key)
{
cout << "loading sprite (mode 1) " << filename << "...";

Expand All @@ -66,7 +66,7 @@ bool gfxSprite::init(const std::string& filename, const RGB& rgb)
return false;
}

if ( SDL_SETCOLORKEY(m_picture, SDL_TRUE, SDL_MapRGB(m_picture->format, rgb.r, rgb.g, rgb.b)) < 0) {
if ( SDL_SETCOLORKEY(m_picture, SDL_TRUE, SDL_MapRGB(m_picture->format, key.r, key.g, key.b)) < 0) {
cout << endl << " ERROR: Couldn't set ColorKey + RLE for "
<< filename << ": " << SDL_GetError() << endl;
return false;
Expand Down Expand Up @@ -98,7 +98,7 @@ bool gfxSprite::init(const std::string& filename, const RGB& rgb)
//
// Color keyed + alpha
//
bool gfxSprite::init(const std::string& filename, const RGBA& rgba)
bool gfxSprite::init(const std::string& filename, const RGB& key, Uint8 alpha)
{
cout << "Loading sprite (mode 2) " << filename << " ...";

Expand All @@ -115,13 +115,13 @@ bool gfxSprite::init(const std::string& filename, const RGBA& rgba)
return false;
}

if ( SDL_SETCOLORKEY(m_picture, SDL_TRUE, SDL_MapRGB(m_picture->format, rgba.r, rgba.g, rgba.b)) < 0) {
if ( SDL_SETCOLORKEY(m_picture, SDL_TRUE, SDL_MapRGB(m_picture->format, key.r, key.g, key.b)) < 0) {
cout << endl << " ERROR: Couldn't set ColorKey + RLE for "
<< filename << ": " << SDL_GetError() << endl;
return false;
}

if ( (SDL_SETALPHABYTE(m_picture, SDL_TRUE, rgba.a)) < 0) {
if ( (SDL_SETALPHABYTE(m_picture, SDL_TRUE, alpha)) < 0) {
cout << endl << " ERROR: Couldn't set per-surface alpha on "
<< filename << ": " << SDL_GetError() << endl;
return false;
Expand Down
5 changes: 2 additions & 3 deletions src/common/gfx/gfxSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class gfxSprite {
void freeSurface();
void clearSurface();

bool init(const std::string& filename, const RGB& rgb); //color keyed
bool init(const std::string& filename, const RGBA& rgba); //color keyed + alpha
bool init(const std::string& filename); //non color keyed
bool initskin(const std::string& filename, const RGB& rgb, short colorscheme, bool expand);
bool init(const std::string& filename, const RGB& key); //color keyed
bool init(const std::string& filename, const RGB& key, Uint8 alpha); //color keyed + alpha

bool draw(short x, short y);
bool draw(short x, short y, short srcx, short srcy, short w, short h, short iHiddenDirection = -1, short iHiddenValue = -1);
Expand Down
20 changes: 10 additions & 10 deletions src/leveleditor/leveleditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ int main(int argc, char *argv[])
printf("\n---------------- loading graphics ----------------\n");

rm->spr_tiletypes.init(convertPath("gfx/leveleditor/leveleditor_tile_types.png"));
rm->spr_transparenttiles.init(convertPath("gfx/leveleditor/leveleditor_transparent_tiles.png"), colors::MAGENTA.withAlpha(160));
rm->spr_transparenttiles.init(convertPath("gfx/leveleditor/leveleditor_transparent_tiles.png"), colors::MAGENTA, 160);

rm->spr_backgroundlevel.init(convertPath("gfx/leveleditor/leveleditor_background_levels.png"), colors::MAGENTA);
rm->spr_tilesetlevel.init(convertPath("gfx/leveleditor/leveleditor_tileset_levels.png"), colors::MAGENTA);
Expand All @@ -471,22 +471,22 @@ int main(int argc, char *argv[])
rm->spr_warps[1].init(convertPath("gfx/leveleditor/leveleditor_warp_preview.png"), colors::MAGENTA);
rm->spr_warps[2].init(convertPath("gfx/leveleditor/leveleditor_warp_thumbnail.png"), colors::MAGENTA);

rm->spr_platformpath.init(convertPath("gfx/leveleditor/leveleditor_platform_path.png"), colors::MAGENTA.withAlpha(128));
rm->spr_platformpath.init(convertPath("gfx/leveleditor/leveleditor_platform_path.png"), colors::MAGENTA, 128);

rm->spr_selectedtile.init(convertPath("gfx/leveleditor/leveleditor_selectedtile.png"), colors::BLACK.withAlpha(128));
rm->spr_nospawntile.init(convertPath("gfx/leveleditor/leveleditor_nospawntile.png"), colors::BLACK.withAlpha(128));
rm->spr_noitemspawntile.init(convertPath("gfx/leveleditor/leveleditor_noitemspawntile.png"), colors::BLACK.withAlpha(128));
rm->spr_platformstarttile.init(convertPath("gfx/leveleditor/leveleditor_platformstarttile.png"), colors::BLACK.withAlpha(64));
rm->spr_platformendtile.init(convertPath("gfx/leveleditor/leveleditor_selectedtile.png"), colors::BLACK.withAlpha(64));
rm->spr_selectedtile.init(convertPath("gfx/leveleditor/leveleditor_selectedtile.png"), colors::BLACK, 128);
rm->spr_nospawntile.init(convertPath("gfx/leveleditor/leveleditor_nospawntile.png"), colors::BLACK, 128);
rm->spr_noitemspawntile.init(convertPath("gfx/leveleditor/leveleditor_noitemspawntile.png"), colors::BLACK, 128);
rm->spr_platformstarttile.init(convertPath("gfx/leveleditor/leveleditor_platformstarttile.png"), colors::BLACK, 64);
rm->spr_platformendtile.init(convertPath("gfx/leveleditor/leveleditor_selectedtile.png"), colors::BLACK, 64);
rm->spr_platformstarttile.SetWrap(true);
rm->spr_platformendtile.SetWrap(true);

rm->spr_mapitems[0].init(convertPath("gfx/leveleditor/leveleditor_mapitems.png"), colors::MAGENTA);
rm->spr_mapitems[1].init(convertPath("gfx/leveleditor/leveleditor_mapitems_preview.png"), colors::MAGENTA);
rm->spr_mapitems[2].init(convertPath("gfx/leveleditor/leveleditor_mapitems_thumbnail.png"), colors::MAGENTA);

rm->spr_dialog.init(convertPath("gfx/leveleditor/leveleditor_dialog.png"), colors::MAGENTA.withAlpha(255));
rm->menu_shade.init(convertPath("gfx/leveleditor/leveleditor_shade.png"), colors::MAGENTA.withAlpha(128));
rm->spr_dialog.init(convertPath("gfx/leveleditor/leveleditor_dialog.png"), colors::MAGENTA, 255);
rm->menu_shade.init(convertPath("gfx/leveleditor/leveleditor_shade.png"), colors::MAGENTA, 128);

rm->spr_tileanimation[0].init(convertPath("gfx/packs/Classic/tilesets/tile_animation.png"), colors::MAGENTA);
rm->spr_tileanimation[1].init(convertPath("gfx/packs/Classic/tilesets/tile_animation_preview.png"), colors::MAGENTA);
Expand All @@ -501,7 +501,7 @@ int main(int argc, char *argv[])
rm->spr_unknowntile[2].init(convertPath("gfx/packs/Classic/tilesets/unknown_tile_thumbnail.png"), colors::MAGENTA);

rm->spr_powerups.init(convertPath("gfx/packs/Classic/powerups/large.png"), colors::MAGENTA);
rm->spr_powerupselector.init(convertPath("gfx/leveleditor/leveleditor_powerup_selector.png"), colors::MAGENTA.withAlpha(128));
rm->spr_powerupselector.init(convertPath("gfx/leveleditor/leveleditor_powerup_selector.png"), colors::MAGENTA, 128);
rm->spr_hidden_marker.init(convertPath("gfx/leveleditor/leveleditor_hidden_marker.png"), colors::MAGENTA);

rm->spr_flagbases.init(convertPath("gfx/packs/Classic/modeobjects/flagbases.png"), colors::MAGENTA);
Expand Down
8 changes: 4 additions & 4 deletions src/worldeditor/worldeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,11 @@ int main(int argc, char *argv[])

spr_path.init(convertPath("gfx/leveleditor/leveleditor_world_path.png"), colors::MAGENTA);

rm->spr_selectedtile.init(convertPath("gfx/leveleditor/leveleditor_selectedtile.png"), {0, 0, 0, 128});
rm->spr_selectedtile.init(convertPath("gfx/leveleditor/leveleditor_selectedtile.png"), colors::BLACK, 128);

spr_dialog.init(convertPath("gfx/leveleditor/leveleditor_dialog.png"), colors::MAGENTA.withAlpha(255));
menu_shade.init(convertPath("gfx/leveleditor/leveleditor_shade.png"), colors::MAGENTA.withAlpha(128));
spr_largedialog.init(convertPath("gfx/leveleditor/leveleditor_platform.png"), colors::MAGENTA.withAlpha(255));
spr_dialog.init(convertPath("gfx/leveleditor/leveleditor_dialog.png"), colors::MAGENTA, 255);
menu_shade.init(convertPath("gfx/leveleditor/leveleditor_shade.png"), colors::MAGENTA, 128);
spr_largedialog.init(convertPath("gfx/leveleditor/leveleditor_platform.png"), colors::MAGENTA, 255);

rm->menu_font_small.init(convertPath("gfx/packs/Classic/fonts/font_small.png"));
rm->menu_font_large.init(convertPath("gfx/packs/Classic/fonts/font_large.png"));
Expand Down

0 comments on commit 4ce02ed

Please sign in to comment.