Skip to content

Commit d220bd5

Browse files
committed
Chapter 2 code, I think (need to test Mac still)?
1 parent ab0e6a1 commit d220bd5

42 files changed

Lines changed: 2960 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Chapter02/Actor.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE.txt for full details.
7+
// ----------------------------------------------------------------
8+
9+
#include "Actor.h"
10+
#include "Game.h"
11+
#include "Component.h"
12+
#include <algorithm>
13+
14+
Actor::Actor(Game* game)
15+
:mState(EActive)
16+
, mPosition(Vector2::Zero)
17+
, mScale(1.0f)
18+
, mRotation(0.0f)
19+
, mGame(game)
20+
{
21+
mGame->AddActor(this);
22+
}
23+
24+
Actor::~Actor()
25+
{
26+
mGame->RemoveActor(this);
27+
// Need to delete components
28+
// Because ~Component calls RemoveComponent, need a different style loop
29+
while (!mComponents.empty())
30+
{
31+
delete mComponents.back();
32+
}
33+
}
34+
35+
void Actor::Update(float deltaTime)
36+
{
37+
if (mState == EActive)
38+
{
39+
UpdateComponents(deltaTime);
40+
UpdateActor(deltaTime);
41+
}
42+
}
43+
44+
void Actor::UpdateComponents(float deltaTime)
45+
{
46+
for (auto comp : mComponents)
47+
{
48+
comp->Update(deltaTime);
49+
}
50+
}
51+
52+
void Actor::UpdateActor(float deltaTime)
53+
{
54+
}
55+
56+
void Actor::AddComponent(Component* component)
57+
{
58+
// Find the insertion point in the sorted vector
59+
// (The first element with a order higher than me)
60+
int myOrder = component->GetUpdateOrder();
61+
auto iter = mComponents.begin();
62+
for (;
63+
iter != mComponents.end();
64+
++iter)
65+
{
66+
if (myOrder < (*iter)->GetUpdateOrder())
67+
{
68+
break;
69+
}
70+
}
71+
72+
// Inserts element before position of iterator
73+
mComponents.insert(iter, component);
74+
}
75+
76+
void Actor::RemoveComponent(Component* component)
77+
{
78+
auto iter = std::find(mComponents.begin(), mComponents.end(), component);
79+
if (iter != mComponents.end())
80+
{
81+
mComponents.erase(iter);
82+
}
83+
}

Chapter02/Actor.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE.txt for full details.
7+
// ----------------------------------------------------------------
8+
9+
#pragma once
10+
#include <vector>
11+
#include "Math.h"
12+
class Actor
13+
{
14+
public:
15+
enum State
16+
{
17+
EActive,
18+
EPaused,
19+
EDead
20+
};
21+
22+
Actor(class Game* game);
23+
virtual ~Actor();
24+
25+
// Update function called from Game (not overridable)
26+
void Update(float deltaTime);
27+
// Updates all the components attached to the actor (not overridable)
28+
void UpdateComponents(float deltaTime);
29+
// Any actor-specific update code (overridable)
30+
virtual void UpdateActor(float deltaTime);
31+
32+
// Getters/setters
33+
const Vector2& GetPosition() const { return mPosition; }
34+
void SetPosition(const Vector2& pos) { mPosition = pos; }
35+
float GetScale() const { return mScale; }
36+
void SetScale(float scale) { mScale = scale; }
37+
float GetRotation() const { return mRotation; }
38+
void SetRotation(float rotation) { mRotation = rotation; }
39+
40+
State GetState() const { return mState; }
41+
void SetState(State state) { mState = state; }
42+
43+
class Game* GetGame() { return mGame; }
44+
45+
46+
// Add/remove components
47+
void AddComponent(class Component* component);
48+
void RemoveComponent(class Component* component);
49+
private:
50+
// Actor's state
51+
State mState;
52+
53+
// Transform
54+
Vector2 mPosition;
55+
float mScale;
56+
float mRotation;
57+
58+
std::vector<class Component*> mComponents;
59+
class Game* mGame;
60+
};

Chapter02/AnimSpriteComponent.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE.txt for full details.
7+
// ----------------------------------------------------------------
8+
9+
#include "AnimSpriteComponent.h"
10+
#include "Math.h"
11+
12+
AnimSpriteComponent::AnimSpriteComponent(Actor* owner, int drawOrder)
13+
:SpriteComponent(owner, drawOrder)
14+
, mCurrFrame(0.0f)
15+
, mAnimFPS(24.0f)
16+
{
17+
}
18+
19+
void AnimSpriteComponent::Update(float deltaTime)
20+
{
21+
SpriteComponent::Update(deltaTime);
22+
23+
if (mAnimTextures.size() > 0)
24+
{
25+
// Update the current frame based on frame rate
26+
// and delta time
27+
mCurrFrame += mAnimFPS * deltaTime;
28+
29+
// Wrap current frame if needed
30+
while (mCurrFrame >= mAnimTextures.size())
31+
{
32+
mCurrFrame -= mAnimTextures.size();
33+
}
34+
35+
// Set the current texture
36+
SetTexture(mAnimTextures[static_cast<int>(mCurrFrame)]);
37+
}
38+
}
39+
40+
void AnimSpriteComponent::SetAnimTextures(const std::vector<SDL_Texture*>& textures)
41+
{
42+
mAnimTextures = textures;
43+
if (mAnimTextures.size() > 0)
44+
{
45+
// Set the active texture to first frame
46+
mCurrFrame = 0.0f;
47+
SetTexture(mAnimTextures[0]);
48+
}
49+
}

Chapter02/AnimSpriteComponent.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE.txt for full details.
7+
// ----------------------------------------------------------------
8+
9+
#pragma once
10+
#include "SpriteComponent.h"
11+
#include <vector>
12+
class AnimSpriteComponent : public SpriteComponent
13+
{
14+
public:
15+
AnimSpriteComponent(class Actor* owner, int drawOrder = 100);
16+
// Update animation every frame (overriden from component)
17+
void Update(float deltaTime) override;
18+
// Set the textures used for animation
19+
void SetAnimTextures(const std::vector<SDL_Texture*>& textures);
20+
// Set/get the animation FPS
21+
float GetAnimFPS() const { return mAnimFPS; }
22+
void SetAnimFPS(float fps) { mAnimFPS = fps; }
23+
private:
24+
// All textures in the animation
25+
std::vector<SDL_Texture*> mAnimTextures;
26+
// Current frame displayed
27+
float mCurrFrame;
28+
// Animation frame rate
29+
float mAnimFPS;
30+
};

Chapter02/Assets/Enemy01.png

10.2 KB
Loading

Chapter02/Assets/Enemy02.png

10.1 KB
Loading

Chapter02/Assets/Enemy03.png

9.98 KB
Loading

Chapter02/Assets/Enemy04.png

9.96 KB
Loading

Chapter02/Assets/Enemy05.png

9.98 KB
Loading

Chapter02/Assets/Enemy06.png

10.1 KB
Loading

0 commit comments

Comments
 (0)