Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted the player palette to an enum class #303

Merged
merged 3 commits into from
Apr 17, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/common/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ SDL_Surface * gfx_createskinsurface(
{
int loops = 1;
if (expand)
loops = NUM_SCHEMES;
loops = PlayerPalette::NUM_PALETTES;

//Blit over loaded skin into player image set
SDL_Surface * temp = SDL_CreateRGBSurface(skin->flags, 32 * loops, 32, skin->format->BitsPerPixel, skin->format->Rmask, skin->format->Gmask, skin->format->Bmask, skin->format->Amask);

//Take the loaded skin and colorize it for each state (normal, 3 frames of invincibiliy, shielded, tagged, ztarred. got shine)
//Take the loaded skin and colorize it for each state (normal, 3 frames of invincibility, shielded, tagged, ztarred, got shine, frozen)
if (SDL_MUSTLOCK(temp))
SDL_LockSurface(temp);

Expand Down
13 changes: 13 additions & 0 deletions src/common/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ struct RGB {
Uint8 b;
};

enum PlayerPalette {
normal,
invincibility_1,
invincibility_2,
invincibility_3,
shielded,
tagged,
ztarred,
got_shine,
frozen,
NUM_PALETTES
};

bool gfx_init(int w, int h, bool fullscreen);
void gfx_changefullscreen(bool fullscreen);
void gfx_flipscreen();
Expand Down
8 changes: 4 additions & 4 deletions src/common/gfx/gfxPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gfxPalette::gfxPalette()
colorcodes[k] = NULL;

for (int i = 0; i < 4; i++)
for (int j = 0; j < NUM_SCHEMES; j++)
for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++)
colorschemes[i][j][k] = NULL;
}
}
Expand All @@ -29,7 +29,7 @@ void gfxPalette::clear()
colorcodes[k] = NULL;

for (int i = 0; i < 4; i++) {
for (int j = 0; j < NUM_SCHEMES; j++) {
for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++) {
delete [] colorschemes[i][j][k];
colorschemes[i][j][k] = NULL;
}
Expand Down Expand Up @@ -69,7 +69,7 @@ bool gfxPalette::load(const char* palette_path)
colorcodes[k] = new Uint8[numcolors];

for (int i = 0; i < 4; i++)
for (int j = 0; j < NUM_SCHEMES; j++)
for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++)
colorschemes[i][j][k] = new Uint8[numcolors];
}

Expand All @@ -93,7 +93,7 @@ bool gfxPalette::load(const char* palette_path)
counter += palette->pitch - palette->w * 3;

for (int i = 0; i < 4; i++) {
for (int j = 0; j < NUM_SCHEMES; j++) {
for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++) {
for (int m = 0; m < numcolors; m++) {
colorschemes[i][j][iRedIndex][m] = pixels[counter++];
colorschemes[i][j][iGreenIndex][m] = pixels[counter++];
Expand Down
4 changes: 2 additions & 2 deletions src/common/gfx/gfxPalette.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdint.h>

#define NUM_SCHEMES 9
#include "gfx.h"

class gfxPalette {
public:
Expand All @@ -22,7 +22,7 @@ class gfxPalette {
uint8_t* colorcodes[3]; //[colorcomponents][numcolors]

//[numplayers][colorscheme][colorcomponents][numcolors]
uint8_t* colorschemes[4][NUM_SCHEMES][3];
uint8_t* colorschemes[4][PlayerPalette::NUM_PALETTES][3];
unsigned short numcolors;
};

Expand Down
2 changes: 1 addition & 1 deletion src/smw/gamemodes/Star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void CGM_Star::playerextraguy(CPlayer &player, short iType)
}
}

bool CGM_Star::isplayerstar(CPlayer * player)
bool CGM_Star::isplayerstar(const CPlayer * player)
{
for (short iPlayer = 0; iPlayer < list_players_cnt - 1; iPlayer++) {
if (starPlayer[iPlayer] == player)
Expand Down
2 changes: 1 addition & 1 deletion src/smw/gamemodes/Star.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CGM_Star : public CGM_TimeLimit
PlayerKillType playerkilledself(CPlayer &player, KillStyle style);
void playerextraguy(CPlayer &player, short iType);

bool isplayerstar(CPlayer * player);
bool isplayerstar(const CPlayer * player);
CPlayer * swapplayer(short id, CPlayer * player);
CPlayer * getstarplayer(short id) {
return starPlayer[id];
Expand Down
44 changes: 18 additions & 26 deletions src/smw/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,33 +858,28 @@ void CPlayer::update_usePowerup()
triggerPowerup();
}

void CPlayer::update_spriteColor()
PlayerPalette CPlayer::getPlayerPalette() const
{
iSrcOffsetX = 0;

bool fColorChosen = false;
if (isInvincible()) {
iSrcOffsetX = animationstate;
fColorChosen = true;
} else {
if (game_values.gamemode->gamemode == game_mode_star) {
CGM_Star * starmode = (CGM_Star*) game_values.gamemode;
StarStyle starmodetype = starmode->getcurrentmodetype();
if (starmodetype != StarStyle::Multi && starmode->isplayerstar(this)) {
iSrcOffsetX = (starmodetype == StarStyle::Ztar) ? 192 : 224;
fColorChosen = true;
}
return invincibility.getPlayerPalette();
}
if (game_values.gamemode->gamemode == game_mode_star) {
CGM_Star * starmode = (CGM_Star*) game_values.gamemode;
StarStyle starmodetype = starmode->getcurrentmodetype();
if (starmodetype != StarStyle::Multi && starmode->isplayerstar(this)) {
return (starmodetype == StarStyle::Ztar) ? PlayerPalette::ztarred : PlayerPalette::got_shine;
}
}

if (!fColorChosen) {
if (game_values.gamemode->tagged == this)
iSrcOffsetX = 160;
else if (frozen)
iSrcOffsetX = 256;
else if (isShielded())
iSrcOffsetX = 128;
if (game_values.gamemode->tagged == this) {
return PlayerPalette::tagged;
}
if (frozen) {
return PlayerPalette::frozen;
}
if (isShielded()) {
return PlayerPalette::shielded;
}
return PlayerPalette::normal;
}

void CPlayer::tryFallingThroughPlatform(short movement_direction)
Expand Down Expand Up @@ -1224,7 +1219,7 @@ void CPlayer::move()
}
}

update_spriteColor();
iSrcOffsetX = 32 * getPlayerPalette();

if (!isready()) {
if (state == PlayerState::Waiting) {
Expand Down Expand Up @@ -1725,9 +1720,6 @@ void CPlayer::SetupNewPlayer()
frozen = false;
frozentimer = 0;

animationstate = 0;
animationtimer = 0;

killsinrow = 0;
killsinrowinair = 0;
extrajumps = 0;
Expand Down
4 changes: 1 addition & 3 deletions src/smw/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class CPlayer
short getGlobalID() const { return globalID; }
short getTeamID() const { return teamID; }
short getColorID() const { return colorID; }
PlayerPalette getPlayerPalette() const;

short leftX() const { return ix; }
short rightX() const { return ix + PW; }
Expand Down Expand Up @@ -189,7 +190,6 @@ class CPlayer
void update_waitingForRespawn();
void update_respawning();
void update_usePowerup();
void update_spriteColor();
void updateFrozenStatus(int keymask);

void tryFallingThroughPlatform(short);
Expand Down Expand Up @@ -314,8 +314,6 @@ class CPlayer
CPlayerAI * pPlayerAI;

gfxSprite *sprites[PGFX_LAST];
short animationstate;
short animationtimer;

uint8_t sprite_state;
short sprswitch;
Expand Down
32 changes: 17 additions & 15 deletions src/smw/player_components/PlayerInvincibility.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "PlayerInvincibility.h"

#include <array>
#include "GameMode.h"
#include "GameValues.h"
#include "player.h"
Expand All @@ -8,6 +9,12 @@
extern CGameValues game_values;
extern CResourceManager* rm;

static const std::array<PlayerPalette, 4> INVINCIBILITY_ANIMATION_STATES = {normal, invincibility_1, invincibility_2, invincibility_3};
static const short INVINCIBILITY_TIME = 580;
static const short INVINCIBILITY_HURRYUP_TIME = 100;
static const short INVINCIBILITY_ANIMATION_DELAY = 4;
static const short INVINCIBILITY_HURRYUP_ANIMATION_DELAY = 7;

void PlayerInvincibility::reset()
{
invincible = false;
Expand All @@ -18,8 +25,6 @@ void PlayerInvincibility::turn_on(CPlayer& player)
{
invincible = true;
timer = 0;
player.animationstate = 0;
player.animationtimer = 0;
player.shield.abort();

//Stop the invincible music if a player is already invincible
Expand All @@ -42,21 +47,18 @@ bool PlayerInvincibility::is_on() const
void PlayerInvincibility::update(CPlayer& player)
{
if (invincible) {
player.animationtimer++;

if ((player.animationtimer > 3 && timer < 480) || player.animationtimer > 6) {
player.animationtimer = 0;

player.animationstate += 32;
if (player.animationstate > 96)
player.animationstate = 0;
}

if (++timer > 580) {
player.animationstate = 0;
player.animationtimer = 0;
if (++timer > INVINCIBILITY_TIME) {
timer = 0;
invincible = false;
}
}
}

PlayerPalette PlayerInvincibility::getPlayerPalette() const
{
if (timer < INVINCIBILITY_TIME - INVINCIBILITY_HURRYUP_TIME) {
return INVINCIBILITY_ANIMATION_STATES[(timer / INVINCIBILITY_ANIMATION_DELAY) % INVINCIBILITY_ANIMATION_STATES.size()];
} else {
return INVINCIBILITY_ANIMATION_STATES[((timer - (INVINCIBILITY_TIME - INVINCIBILITY_HURRYUP_TIME)) / INVINCIBILITY_HURRYUP_ANIMATION_DELAY) % INVINCIBILITY_ANIMATION_STATES.size()];
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way we could make the indices more readable, that would explain why these numbers are used? for example,

  • If you use std::array for the animation states, you can then use its size() in the % 4 part
  • The / 4 and / 7 parts seem to control the speed of the animation – you could move them to a named variable
  • Why do we subtract - 4 in the second branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

}
3 changes: 3 additions & 0 deletions src/smw/player_components/PlayerInvincibility.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef PLAYER_INVINCIBILITY
#define PLAYER_INVINCIBILITY

#include "gfx.h"

class CPlayer;

class PlayerInvincibility
Expand All @@ -10,6 +12,7 @@ class PlayerInvincibility
void update(CPlayer& player);

bool is_on() const;
PlayerPalette getPlayerPalette() const;
void turn_on(CPlayer& player);

private:
Expand Down