Skip to content

Commit bc0276e

Browse files
committed
fixed a performance issue
1 parent 6351bb7 commit bc0276e

22 files changed

+130
-97
lines changed

engine/src/engine.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ void Engine::Start(unsigned int width, unsigned int height,
175175
//capture the pressed keys in order to figure out what key has been pressed for the key bindings menu
176176
//only happens in the options scene
177177
if((event.type == Event::TextEntered || event.type == Event::KeyPressed) && _activeScene->_sceneID.compare("options") == 0) {
178-
if(event.text.unicode < 128) {
179-
_keysText.push_back(static_cast<char>(event.text.unicode));
180-
}
181178
_keys.push_back(event.key.code);
182179
//LOG(DEBUG) << "IN OPTIONS SCENE";
183180
}

res/lang/bg.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
title=ЗАГЛАВИЕ
1+
title=Spell Knight
22
play=Нова игра
33
options=Опции
44
exit=Изход
5-
hp=Живот
5+
hp=ТЖ
66
time=Време
77
score=Точки
88
game_over=Край на играта!
99
next_level=следващо ниво
10-
main_menu=меню
11-
restart=рестартиране
12-
language=Bulgarian
10+
main_menu=Меню
11+
restart=Рестартиране
12+
language=Език:
13+
resolution=Резолюция:
14+
winmode=Цял екран:
15+
bindings=Контроли:
16+
toggle=Промяна
17+
walkl=Ляво
18+
walkr=Дясно
19+
jump=Скачане
20+
shoot=Стреляне

res/lang/en.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ hp=HP
66
time=Time
77
score=Score
88
game_over=Game оver!
9-
next_level=next level
10-
main_menu=main menu
11-
restart=restart
12-
language=English
9+
next_level=Next level
10+
main_menu=Main menu
11+
restart=Restart
12+
language=Language:
13+
resolution=Resolution:
14+
winmode=Window mode:
15+
bindings=Key bindings:
16+
toggle=Toggle
17+
walkl=Walk left
18+
walkr=Walk right
19+
jump=Jump
20+
shoot=Shoot

res/settings.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/GUI/option_button.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ void OptionButton::addOptions(const std::vector<std::string> &options) {
3131
const std::string& OptionButton::getSelection() const {
3232
return _options[_currentOption];
3333
}
34+
35+
void OptionButton::setSelection(const std::string &sel) {
36+
auto index = -1;
37+
for(int i = 0; i < _options.size(); ++i) {
38+
if(_options[i] == sel) {
39+
index = i;
40+
break;
41+
}
42+
}
43+
44+
if(index != -1) {
45+
_panel.setTextLocalised(_options[index]);
46+
}
47+
}

src/GUI/option_button.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class OptionButton : public Button {
1616
void addOption(const std::string& option);
1717
void addOptions(const std::vector<std::string>& options);
1818
const std::string& getSelection() const override;
19+
void setSelection(const std::string& sel);
1920
};
2021

2122

src/components/cmp_animated_sprite.cpp

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,30 @@ using namespace std;
66

77
//constructor, initialises default sprite size and default animation speed
88
AnimatedSpriteComponent::AnimatedSpriteComponent(Entity* p, Vector2f size) : Component(p), _size(size), _rotation(0) {
9-
//total accumulated time, used to tell when to switch to the next frame
10-
_totalTime = 0.0f;
11-
//the texture rect's width and height
12-
_currentFrame.width = _size.x;
13-
_currentFrame.height = _size.y;
14-
//current frame, default 0, 0
15-
_currentImage = Vector2u(0, 0);
16-
//default animation speed
17-
_frameTime = 0.2f;
18-
//this boolean is used to decide whether to flip the sprite or not
19-
_facingRight = true;
20-
//set this to the padding between rows in the spritesheet!!
21-
//all online spritesheet packers seem to add padding for some reason
22-
_spriteSheetPadding = 0;
23-
_currentRow = 0;
24-
_hurt = false;
25-
_hurtTimer = 1.0f;
26-
_numOfFrames = 1;
27-
_done = false;
28-
_frameDecline = _numOfFrames;
29-
//set origin
30-
auto origin = _size / 2.f;
31-
origin.x = floor(origin.x);
32-
origin.y = floor(origin.y);
33-
_sprite.setOrigin(origin);
9+
//total accumulated time, used to tell when to switch to the next frame
10+
_totalTime = 0.0f;
11+
//the texture rect's width and height
12+
_currentFrame.width = _size.x;
13+
_currentFrame.height = _size.y;
14+
//current frame, default 0, 0
15+
_currentImage = Vector2u(0, 0);
16+
//default animation speed
17+
_frameTime = 0.2f;
18+
//this boolean is used to decide whether to flip the sprite or not
19+
_facingRight = true;
20+
//set this to the padding between rows in the spritesheet!!
21+
//all online spritesheet packers seem to add padding for some reason
22+
_spriteSheetPadding = 0;
23+
_currentRow = 0;
24+
_hurt = false;
25+
_hurtTimer = 1.0f;
26+
_numOfFrames = 1;
27+
_done = false;
28+
//set origin
29+
auto origin = _size / 2.f;
30+
origin.x = floor(origin.x);
31+
origin.y = floor(origin.y);
32+
_sprite.setOrigin(origin);
3433
}
3534

3635
void AnimatedSpriteComponent::update(double dt) {
@@ -54,18 +53,8 @@ void AnimatedSpriteComponent::update(double dt) {
5453
//unlock
5554
_locked = false;
5655
_done = true;
57-
if(!_stalled){
5856
//restart from the first frame
5957
_currentImage.x = 0;
60-
}
61-
// else{
62-
// if(_frameDecline ==_numOfFrames-1){
63-
// _currentImage.x= _numOfFrames-_frameDecline--;
64-
// _currentImage.x=_numOfFrames-_frameDecline--;
65-
// }
66-
67-
// }
68-
6958
}
7059

7160
}
@@ -81,7 +70,7 @@ void AnimatedSpriteComponent::update(double dt) {
8170
_currentFrame.left = (_currentImage.x + 1) * abs(_currentFrame.width);
8271
_currentFrame.width = -abs(_currentFrame.width);
8372
}
84-
73+
8574
//calculate spritesheet y coordinate, don't forget padding
8675
_currentFrame.top = _currentImage.y * _size.y + _spriteSheetPadding;
8776

@@ -122,15 +111,6 @@ void AnimatedSpriteComponent::lockInAnimation(unsigned int row) {
122111
_locked = true;
123112
}
124113

125-
void AnimatedSpriteComponent::stallAnimation(int row){
126-
_stalled = true;
127-
if(isDone()){
128-
_currentImage.y = row;
129-
_currentImage.x = _numOfFrames-1;
130-
}
131-
132-
}
133-
134114
//sets the spritesheet, using the passed reference to a spritesheet
135115
void AnimatedSpriteComponent::setSpritesheet(const sf::Texture& sh) {
136116
_spritesheet = sh;
@@ -153,7 +133,7 @@ void AnimatedSpriteComponent::setNumberOfFrames(int num) {
153133
void AnimatedSpriteComponent::setCurrentRow(int r) {
154134
//if changing to a new animation
155135
if(_currentRow != r) {
156-
_currentRow = r;
136+
_currentRow = r;
157137
//reset to first frame
158138
_currentImage.x = 0;
159139
}

src/components/cmp_bullet.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#include "cmp_bullet.h"
22
#include "cmp_player_attack.h"
3-
#include "cmp_physics.h"
43
#include "cmp_animated_sprite.h"
5-
#include "cmp_enemy_physics.h"
6-
#include "texture_manager.h"
7-
#include "system_physics.h"
84
#include "../../engine/src/engine.h"
95
#include "cmp_score.h"
106
#include "cmp_enemy_health.h"
@@ -81,8 +77,8 @@ void BulletComponent::checkCollisions(const std::vector<const b2Contact*>& conta
8177
_enemies = _parent->scene->ents.find("enemy");
8278
break;
8379
}
84-
else{
85-
parentPhysics->setMass(0.f);
80+
else {
81+
parentPhysics->setMass(0.f);
8682
}
8783
}
8884
}

src/components/cmp_enemy_health.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ EnemyHealthComponent::EnemyHealthComponent(Entity* p, int health) : Component(p)
3030
_offset.x = _offset.x / 2.f;
3131
_offset.y += anim->getSize().y / 2.f;
3232
_panelAlpha = 180;
33+
34+
TEXT = Config::getLocalisedString("hp") + " ";
35+
_panel.setTextLocalised(TEXT + to_string(_health));
3336
}
3437

3538
void EnemyHealthComponent::update(double dt) {
36-
//update text to current health
37-
_panel.setText(Config::getLocalisedString("hp") + " " + std::to_string(_health));
3839
//update position
3940
_panel.setPosition(_parent->getPosition() - _offset);
4041
//update panel
@@ -69,9 +70,15 @@ void EnemyHealthComponent::decreaseHealth(int num) {
6970
if(_health <= (int)(1.f/4.f * (float)_maxHealth)) {
7071
_panel.setPanelColour(COL_25_PERCENT);
7172
}
73+
74+
//update text to current health
75+
_panel.setTextLocalised(TEXT + std::to_string(_health));
7276
}
7377

7478
//increases health by num
7579
void EnemyHealthComponent::increaseHealth(int num) {
7680
_health += num;
81+
82+
//update text to current health
83+
_panel.setTextLocalised(TEXT + std::to_string(_health));
7784
}

src/components/cmp_enemy_health.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class EnemyHealthComponent : public Component {
88
int _health, _maxHealth;
99
sf::Vector2f _offset;
1010
int _panelAlpha;
11+
std::string TEXT;
12+
1113
public:
1214
EnemyHealthComponent() = delete;
1315
explicit EnemyHealthComponent(Entity *p, int health);

0 commit comments

Comments
 (0)