Skip to content
This repository was archived by the owner on Mar 3, 2022. It is now read-only.

Commit b246dcf

Browse files
authored
Merge pull request #77 from Bobor-dev/contributors-update
Player's animations with one texture for perf and memory
2 parents b3f1aaa + af42972 commit b246dcf

File tree

6 files changed

+31
-27
lines changed

6 files changed

+31
-27
lines changed

src/opmon/controller/AnimationCtrl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ File under GNU GPL 3.0 license.
1212
namespace OpMon {
1313
namespace Controller {
1414
/**
15-
AnimationCtrl is a special game screen which can call all types of animations.
16-
**/
15+
AnimationCtrl is a special game screen which can call all types of animations.
16+
**/
1717
class AnimationCtrl : public AGameScreen {
1818
private:
1919
std::unique_ptr<View::Animations::Animation> view;

src/opmon/model/storage/OverworldData.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ namespace OpMon {
2020

2121
player->addOpToOpTeam(new Model::OpMon("", uidata->getOp(4), 5, {Model::Attacks::newAtk("Tackle"), Model::Attacks::newAtk("Growl"), nullptr, nullptr}, Model::Nature::QUIET));
2222

23-
//PP textures loading
24-
ResourceLoader::load(texturePP[(unsigned int)Side::TO_DOWN], "sprites/chara/pp/pp0.png");
25-
ResourceLoader::load(texturePP[(unsigned int)Side::TO_RIGHT], "sprites/chara/pp/pp1.png");
26-
ResourceLoader::load(texturePP[(unsigned int)Side::TO_LEFT], "sprites/chara/pp/pp2.png");
27-
ResourceLoader::load(texturePP[(unsigned int)Side::TO_UP], "sprites/chara/pp/pp3.png");
28-
ResourceLoader::load(walkingPP[(unsigned int)Side::TO_DOWN], "sprites/chara/pp/mpp0.png");
29-
ResourceLoader::load(walkingPP[(unsigned int)Side::TO_RIGHT], "sprites/chara/pp/mpp1.png");
30-
ResourceLoader::load(walkingPP[(unsigned int)Side::TO_LEFT], "sprites/chara/pp/mpp2.png");
31-
ResourceLoader::load(walkingPP[(unsigned int)Side::TO_UP], "sprites/chara/pp/mpp3.png");
32-
ResourceLoader::load(walkingPP2[(unsigned int)Side::TO_DOWN], "sprites/chara/pp/mpp20.png");
33-
ResourceLoader::load(walkingPP2[(unsigned int)Side::TO_RIGHT], "sprites/chara/pp/mpp21.png");
34-
ResourceLoader::load(walkingPP2[(unsigned int)Side::TO_LEFT], "sprites/chara/pp/mpp22.png");
35-
ResourceLoader::load(walkingPP2[(unsigned int)Side::TO_UP], "sprites/chara/pp/mpp23.png");
23+
//PP texture and rect loading
24+
ResourceLoader::load(texturePP, "sprites/chara/pp/pp_anim.png");
25+
texturePPRect[(unsigned int)Side::TO_DOWN] = sf::IntRect(0, 64, 32, 32);
26+
texturePPRect[(unsigned int)Side::TO_RIGHT] = sf::IntRect(32, 64, 32, 32);
27+
texturePPRect[(unsigned int)Side::TO_LEFT] = sf::IntRect(64, 64, 32, 32);
28+
texturePPRect[(unsigned int)Side::TO_UP] = sf::IntRect(96, 64, 32, 32);
29+
walkingPPRect[(unsigned int)Side::TO_DOWN] = sf::IntRect(0, 0, 32, 32);
30+
walkingPPRect[(unsigned int)Side::TO_RIGHT] = sf::IntRect(32, 0, 32, 32);
31+
walkingPPRect[(unsigned int)Side::TO_LEFT] = sf::IntRect(64, 0, 32, 32);
32+
walkingPPRect[(unsigned int)Side::TO_UP] = sf::IntRect(96, 0, 32, 32);
33+
walkingPP2Rect[(unsigned int)Side::TO_DOWN] = sf::IntRect(0, 32, 32, 32);
34+
walkingPP2Rect[(unsigned int)Side::TO_RIGHT] = sf::IntRect(32, 32, 32, 32);
35+
walkingPP2Rect[(unsigned int)Side::TO_LEFT] = sf::IntRect(64, 32, 32, 32);
36+
walkingPP2Rect[(unsigned int)Side::TO_UP] = sf::IntRect(96, 32, 32, 32);
3637

3738
//Characters' textures initialization
3839
ResourceLoader::loadTextureArray(charaTextures["kid"], "sprites/chara/kid/kid%d.png", 12);

src/opmon/model/storage/OverworldData.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ namespace OpMon {
2525
std::map<std::string, Map *> maps;
2626
std::map<std::string, Map *>::iterator mapsItor;
2727

28-
sf::Texture texturePP[4];
29-
sf::Texture walkingPP[4];
30-
sf::Texture walkingPP2[4];
28+
sf::Texture texturePP;
29+
sf::IntRect texturePPRect[4];
30+
sf::IntRect walkingPPRect[4];
31+
sf::IntRect walkingPP2Rect[4];
3132

3233
UiData *uidata;
3334

@@ -68,9 +69,10 @@ namespace OpMon {
6869
}
6970
}
7071

71-
sf::Texture &getTexturePP(unsigned int id) { return texturePP[((id < 4) ? id : 0)]; }
72-
sf::Texture &getWalkingPP(unsigned int id) { return walkingPP[((id < 4) ? id : 0)]; }
73-
sf::Texture &getWalkingPP2(unsigned int id) { return walkingPP2[((id < 4) ? id : 0)]; }
72+
sf::Texture &getTexturePP() { return texturePP; }
73+
sf::IntRect &getTexturePPRect(unsigned int id) { return texturePPRect[((id < 4) ? id : 0)]; }
74+
sf::IntRect &getWalkingPPRect(unsigned int id) { return walkingPPRect[((id < 4) ? id : 0)]; }
75+
sf::IntRect &getWalkingPP2Rect(unsigned int id) { return walkingPP2Rect[((id < 4) ? id : 0)]; }
7476

7577
UiData *getUiDataPtr() { return uidata; }
7678
Player *getPlayerPtr() { return player; }

src/opmon/model/storage/ResourceLoader.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace OpMon {
2121
/**
2222
* Verify that the resource folder exists.
2323
* @return `true` if it exists; `false` otherwise.
24-
24+
2525
*/
2626
static bool checkResourceFolderExists();
2727

src/opmon/view/Overworld.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ namespace OpMon {
158158
Overworld::Overworld(const std::string &mapId, Model::OverworldData &data)
159159
: data(data) {
160160
current = data.getMap(mapId);
161-
character.setTexture(data.getTexturePP((unsigned int)Side::TO_DOWN));
161+
character.setTexture(data.getTexturePP());
162+
character.setTextureRect(data.getTexturePPRect((unsigned int)Side::TO_DOWN));
162163
data.getPlayer().tp(mapId, sf::Vector2i(2, 4)); //TODO : Add a parameter to configure the default player's position
163164
character.setPosition(2 SQUARES - 16, 4 SQUARES);
164165
camera.setSize(sf::Vector2f(16 SQUARES, 16 SQUARES));
@@ -269,7 +270,7 @@ namespace OpMon {
269270

270271
//Sets the character's texture.
271272
if(data.getPlayer().getPosition().isAnim() && !anims) {
272-
character.setTexture(data.getWalkingPP((unsigned int)data.getPlayer().getPosition().getDir()));
273+
character.setTextureRect(data.getWalkingPPRect((unsigned int)data.getPlayer().getPosition().getDir()));
273274
anims = animsCounter >= 8;
274275
if(anims) {
275276
//Stops the caracter's movement every 8 frames
@@ -279,7 +280,7 @@ namespace OpMon {
279280
animsCounter++;
280281

281282
} else if(data.getPlayer().getPosition().isAnim() && anims) {
282-
character.setTexture(data.getWalkingPP2((unsigned int)data.getPlayer().getPosition().getDir()));
283+
character.setTextureRect(data.getWalkingPP2Rect((unsigned int)data.getPlayer().getPosition().getDir()));
283284
if(animsCounter >= 8) {
284285
//Stops the caracter's movement every 8 frames
285286
data.getPlayer().getPosition().stopMove();
@@ -288,7 +289,7 @@ namespace OpMon {
288289
}
289290
animsCounter++;
290291
} else if(!data.getPlayer().getPosition().isAnim()) {
291-
character.setTexture(data.getTexturePP((unsigned int)data.getPlayer().getPosition().getDir()));
292+
character.setTextureRect(data.getTexturePPRect((unsigned int)data.getPlayer().getPosition().getDir()));
292293
}
293294

294295
//Drawing character

src/opmon/view/Window.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace OpMon {
5454
sf::Sprite sprite(frame.getTexture());
5555

5656
if(fullScreen) {
57-
float coef = window.getSize().y / (sprite.getGlobalBounds().height);
57+
const float coef = window.getSize().y / (sprite.getGlobalBounds().height);
5858
sprite.setScale(coef, coef);
5959
sprite.setPosition(((window.getSize().x / 2) - (sprite.getGlobalBounds().width / 2)), 0);
6060
}

0 commit comments

Comments
 (0)