Skip to content

Commit 3afd75f

Browse files
committed
composite . it does not work
1 parent a1fbf8b commit 3afd75f

12 files changed

+135
-45
lines changed

Game.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "SoundManager.h"
1515
#include "GameOverState.h"
1616

17-
#include <utils/Effect.h>
17+
#include <utils/ValueEffect.h>
1818
#include <iostream>
1919

2020
using namespace std;

GameObject.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**************************************************************************
2+
** Qt Creator license header template
3+
** Special keywords: dpinol 08/03/2014 2014
4+
** Environment variables:
5+
** To protect a percent sign, use '%'.
6+
**************************************************************************/
7+
#include "GameObject.h"
8+
#include <utils/Effect.h>
9+
10+
GameObject::GameObject() : m_pixel(0,0),
11+
m_velocity(0,0),
12+
m_acceleration(0,0),
13+
m_width(0),
14+
m_height(0),
15+
m_currentRow(0),
16+
m_currentFrame(0),
17+
m_bUpdating(false),
18+
m_bDead(false),
19+
m_bDying(false),
20+
m_dyingTime(0),
21+
m_dyingCounter(0),
22+
m_angle(0),
23+
m_alpha(255),
24+
m_effects(new dani::CompositeEffect)
25+
{
26+
}
27+
28+
GameObject::~GameObject()
29+
{
30+
31+
}
32+
33+
void GameObject::update()
34+
{
35+
m_effects->update();
36+
}

GameObject.h

+9-19
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@
1717
#include <memory>
1818
#include <vector>
1919

20+
namespace dani
21+
{
22+
class CompositeEffect;
23+
}
2024

2125
class GameObject
2226
{
2327
public:
2428

2529
// base class needs virtual destructor
26-
virtual ~GameObject() {}
30+
virtual ~GameObject();
2731

2832
// load from file - int x, int y, int width, int height, std::string textureID, int numFrames, int callbackID = 0, int animSpeed = 0
2933
virtual void load(std::unique_ptr<LoaderParams> const &pParams) = 0;
3034

3135
// draw the object
3236
virtual void draw() = 0;
3337

34-
// do update stuf
35-
virtual void update() = 0;
38+
// do update stuff
39+
virtual void update();
3640

3741
// remove anything that needs to be deleted
3842
virtual void clean() = 0;
@@ -75,22 +79,7 @@ class GameObject
7579
protected:
7680

7781
// constructor with default initialisation list
78-
GameObject() : m_pixel(0,0),
79-
m_velocity(0,0),
80-
m_acceleration(0,0),
81-
m_width(0),
82-
m_height(0),
83-
m_currentRow(0),
84-
m_currentFrame(0),
85-
m_bUpdating(false),
86-
m_bDead(false),
87-
m_bDying(false),
88-
m_dyingTime(0),
89-
m_dyingCounter(0),
90-
m_angle(0),
91-
m_alpha(255)
92-
{
93-
}
82+
GameObject() ;
9483

9584
// movement variables
9685
Vector2D m_pixel;
@@ -120,6 +109,7 @@ class GameObject
120109

121110
// blending
122111
int m_alpha;
112+
std::unique_ptr<dani::CompositeEffect> m_effects;
123113

124114
};
125115

GameState.cpp

+28-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77

88
#include "GameState.h"
99
#include "GameObject.h"
10+
#include <utils/Effect.h>
11+
12+
GameState::GameState()
13+
: m_loadingComplete(false),
14+
m_exiting(false),
15+
m_effects(new dani::CompositeEffect)
16+
{
17+
18+
}
19+
20+
GameState::~GameState()
21+
{
22+
23+
}
24+
25+
void GameState::resume()
26+
{
27+
28+
}
1029

1130
bool GameState::onEnter()
1231
{
@@ -19,21 +38,22 @@ void GameState::render()
1938
{
2039
if(m_loadingComplete)
2140
{
22-
for(int i = 0; i < m_gameObjects.size(); i++)
23-
{
24-
m_gameObjects[i]->draw();
25-
}
41+
for(GameObject * o: m_gameObjects)
42+
{
43+
o->draw();
44+
}
2645
}
2746
}
2847

2948
void GameState::update()
3049
{
3150
if(m_loadingComplete)
3251
{
33-
for(int i = 0; i < m_gameObjects.size(); i++)
34-
{
35-
m_gameObjects[i]->update();
36-
}
52+
m_effects->update();
53+
for(GameObject * o: m_gameObjects)
54+
{
55+
o->update();
56+
}
3757
}
3858
}
3959

GameState.h

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,42 @@
99
#ifndef SDL_Game_Programming_Book_IGameState_h
1010
#define SDL_Game_Programming_Book_IGameState_h
1111

12+
1213
#include <string>
1314
#include <vector>
15+
#include <memory>
16+
17+
namespace dani
18+
{
19+
class CompositeEffect;
20+
}
1421

1522
class GameObject;
1623
class GameState
1724
{
1825
public:
19-
20-
virtual ~GameState() {}
26+
virtual ~GameState();
2127

2228
virtual void update();
2329
virtual void render();
2430

2531
virtual bool onEnter();
2632
virtual bool onExit() = 0;
2733

28-
virtual void resume() {}
34+
virtual void resume();
2935

3036
virtual std::string getStateID() const = 0;
3137

3238
protected:
3339
virtual bool onEnterImpl() = 0;
3440

35-
GameState() : m_loadingComplete(false), m_exiting(false)
36-
{
37-
38-
}
39-
41+
GameState();
42+
//@todo put everything on pimpl
4043
bool m_loadingComplete;
4144
bool m_exiting;
4245
std::vector<GameObject*> m_gameObjects;
43-
4446
std::vector<std::string> m_textureIDList;
47+
std::unique_ptr<dani::CompositeEffect> m_effects;
4548
};
4649

4750
#endif

JewelObject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <SDL.h>
1313
#include "BoardObject.h"
14-
#include <utils/Effect.h>
14+
#include <utils/ValueEffect.h>
1515
#include "model/Jewel.h"
1616
#include "JewelMove.h"
1717
#include <SDL_timer.h>

MovingObject.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "TextureManager.h"
1111
#include "Game.h"
1212
#include <utils/utils.h>
13+
#include <utils/Effect.h>
1314

1415
#include <numeric>
1516
#include <SDL2/SDL_render.h>
@@ -24,6 +25,8 @@ MovingObject::MovingObject(const std::string &imgFilename, int totalTimeMs, bool
2425
m_angle(0),
2526
m_trajectoryIndex(0)
2627
{
28+
m_effects->addChild(m_alpha);
29+
m_effects->addChild(m_scale);
2730
TheTextureManager::Instance()->load(imgFilename, "spark");
2831
int access;
2932
Uint32 format;
@@ -111,6 +114,7 @@ void MovingObject::update()
111114
{
112115
if (m_done)
113116
return;
117+
m_effects->update();
114118
float dist = (m_pixel - m_trajectory[m_trajectoryIndex]).length();
115119
if (dist < 2)
116120
{

MovingObject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "GameObject.h"
1212
#include <vector>
1313
#include <utils/Vector2D.h>
14-
#include <utils/Effect.h>
14+
#include <utils/ValueEffect.h>
1515
#include <memory>
1616

1717
class MovingObject : public GameObject

SDL_test.pro

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ SOURCES += *.cpp \
1212
model/test/*.cpp \
1313
utils/init.cpp \
1414
MovingObject.cpp \
15-
utils/Disturber.cpp
15+
utils/Disturber.cpp \
16+
GameObject.cpp
1617

1718

1819
OTHER_FILES += \

utils/Effect.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Effect.h"
99
#include <utils/utils.h>
1010
#include <stdexcept>
11+
#include <numeric>
1112

1213
namespace dani
1314
{
@@ -84,9 +85,24 @@ namespace dani
8485
}
8586
}
8687

87-
void CompositeEffect::addChid(std::unique_ptr<Effect> child)
88+
void CompositeEffect::addChild(Effect &child)
8889
{
89-
m_children.push_back(std::move(child));
90+
m_children.push_back(&child);
91+
}
92+
93+
bool CompositeEffect::isDone() const
94+
{
95+
return !std::all_of(m_children.begin(), m_children.end(),
96+
[](Effect* e)
97+
{return e->isDone();});
98+
}
99+
100+
void CompositeEffect::restart()
101+
{
102+
for(auto &effect: m_children)
103+
{
104+
effect->restart();
105+
}
90106
}
91107

92108
}

utils/Effect.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,18 @@ namespace dani
7373

7474
class CompositeEffect: public Effect
7575
{
76-
void addChid(std::unique_ptr<Effect>);
76+
public:
77+
/**
78+
* @brief addChid
79+
* @param effect ownwership is not transferred
80+
*/
81+
void addChild(Effect &effect);
7782
virtual void update() override;
7883
virtual void setPaused(bool paused = true) override;
84+
virtual bool isDone() const override;
85+
virtual void restart() override;
7986
private:
80-
std::vector<std::unique_ptr<Effect>> m_children;
87+
std::vector<Effect*> m_children;
8188
};
8289
}
8390
#endif // Effect_H

utils/ValueEffect.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace dani
2020
template <class T>
2121
class ValueEffect : public Effect
2222
{
23+
public:
2324
inline T const &get() const
2425
{
2526
return m_value;
@@ -67,6 +68,7 @@ namespace dani
6768
m_maxDisturbance = maxDisturbance;
6869
}
6970

71+
7072
void setRange(T const &min, T const &max)
7173
{
7274
m_mean = (min + max) / 2.0;
@@ -78,7 +80,7 @@ namespace dani
7880
T m_maxDisturbance;
7981
//cache so that we can return by ref
8082
//T m_lastValue;
81-
};
83+
}; //RangeEffect
8284

8385

8486
template <class T>
@@ -87,6 +89,10 @@ namespace dani
8789
typedef RangeEffect<T> Parent;
8890
public:
8991

92+
void restart() override
93+
{
94+
}
95+
9096
constexpr bool isDone() const override
9197
{
9298
return false;
@@ -107,6 +113,7 @@ namespace dani
107113
{
108114
typedef RangeEffect<T> Parent;
109115
//int m_periodMs;
116+
float m_startRadian;
110117
mutable float m_curRadian;
111118
//larger than 10 for non stop
112119
float m_stopRadian;
@@ -115,6 +122,7 @@ namespace dani
115122
OscilleEffect()
116123
{
117124
setPeriod();
125+
m_startRadian = 0;
118126
m_curRadian = 0;
119127
m_stopRadian = 42;
120128
}
@@ -135,6 +143,11 @@ namespace dani
135143
return m_curRadian > m_stopRadian;
136144
}
137145

146+
void restart()
147+
{
148+
m_curRadian = m_startRadian;
149+
}
150+
138151

139152
/**
140153
* @brief setStartPhase

0 commit comments

Comments
 (0)