Skip to content
Open
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
Binary file addedChapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand DownExpand Up@@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand DownExpand Up@@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All@@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand DownExpand Up@@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file addedChapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Linux update and new asteroids by Haruu000 · Pull Request #68 · gameprogcpp/code · GitHub
Skip to content
Open
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
Binary file addedChapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand DownExpand Up@@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand DownExpand Up@@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All@@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand DownExpand Up@@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file addedChapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Linux update and new asteroids by Haruu000 · Pull Request #68 · gameprogcpp/code · GitHub
Skip to content
Open
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
Binary file addedChapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand DownExpand Up@@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand DownExpand Up@@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All@@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand DownExpand Up@@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file addedChapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Linux update and new asteroids by Haruu000 · Pull Request #68 · gameprogcpp/code · GitHub
Skip to content
Open
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
Binary file addedChapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand DownExpand Up@@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand DownExpand Up@@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All@@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand DownExpand Up@@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file addedChapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Linux update and new asteroids by Haruu000 · Pull Request #68 · gameprogcpp/code · GitHub
Skip to content
Open
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
Binary file addedChapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand DownExpand Up@@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand DownExpand Up@@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All@@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand DownExpand Up@@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file addedChapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Linux update and new asteroids by Haruu000 · Pull Request #68 · gameprogcpp/code · GitHub
Skip to content
Open
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
Binary file addedChapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand DownExpand Up@@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand DownExpand Up@@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All@@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand DownExpand Up@@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file addedChapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Linux update and new asteroids by Haruu000 · Pull Request #68 · gameprogcpp/code · GitHub
Skip to content
Open
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
Binary file addedChapter03/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions Chapter03/Game.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,14 +7,14 @@
// ----------------------------------------------------------------

#include "Game.h"
#include "SDL/SDL_image.h"
#include "SDL_image.h"
#include <algorithm>
#include "Actor.h"
#include "SpriteComponent.h"
#include "Ship.h"
#include "Asteroid.h"
#include "Random.h"

#include "NewAsteroid.h"
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
Expand DownExpand Up@@ -171,6 +171,11 @@ void Game::LoadData()
{
new Asteroid(this);
}
const int numNewAsteroids = 10;
for (int i = 0; i < numNewAsteroids; i++)
{
new NewAsteroid(this);
}
}

void Game::UnloadData()
Expand DownExpand Up@@ -238,6 +243,21 @@ void Game::RemoveAsteroid(Asteroid* ast)
}
}

void Game::AddNewAsteroid(NewAsteroid* ast)
{
mNewAsteroids.emplace_back(ast);
}

void Game::RemoveNewAsteroid(NewAsteroid* ast)
{
auto iter = std::find(mNewAsteroids.begin(),
mNewAsteroids.end(), ast);
if (iter != mNewAsteroids.end())
{
mNewAsteroids.erase(iter);
}
}

void Game::Shutdown()
{
UnloadData();
Expand Down
9 changes: 7 additions & 2 deletions Chapter03/Game.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,11 +7,10 @@
// ----------------------------------------------------------------

#pragma once
#include "SDL/SDL.h"
#include "SDL.h"
#include <unordered_map>
#include <string>
#include <vector>

class Game
{
public:
Expand All@@ -32,6 +31,10 @@ class Game
void AddAsteroid(class Asteroid* ast);
void RemoveAsteroid(class Asteroid* ast);
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }

void AddNewAsteroid(class NewAsteroid* ast);
void RemoveNewAsteroid(class NewAsteroid* ast);
std::vector<class NewAsteroid*>& GetNewAsteroids() { return mNewAsteroids; }
private:
void ProcessInput();
void UpdateGame();
Expand DownExpand Up@@ -60,4 +63,6 @@ class Game
// Game-specific
class Ship* mShip; // Player's ship
std::vector<class Asteroid*> mAsteroids;
std::vector<class NewAsteroid*> mNewAsteroids;

};
89 changes: 50 additions & 39 deletions Chapter03/Laser.cpp
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
// Laser.cpp

#include "Laser.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "CircleComponent.h"
#include "Asteroid.h"
#include "NewAsteroid.h"

Laser::Laser(Game* game)
:Actor(game)
,mDeathTimer(1.0f)
: Actor(game)
, mDeathTimer(1.0f)
{
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));
// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/Laser.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);
// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(800.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(11.0f);
}

void Laser::UpdateActor(float deltaTime)
{
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}
}
// If we run out of time, laser is dead
mDeathTimer -= deltaTime;
if (mDeathTimer <= 0.0f)
{
SetState(EDead);
}
else
{
// Do we intersect with an asteroid?
for (auto ast : GetGame()->GetAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// The first asteroid we intersect with,
// set ourselves and the asteroid to dead
SetState(EDead);
ast->SetState(EDead);
break;
}
}

for (auto ast : GetGame()->GetNewAsteroids())
{
if (Intersect(*mCircle, *(ast->GetCircle())))
{
// Reduce the new asteroid's HP
ast->mHp -= 1;
if (ast->mHp <= 0)
{
ast->SetState(EDead);
}
// Set the laser to dead regardless
SetState(EDead);
break;
}
}
}
}
46 changes: 46 additions & 0 deletions Chapter03/NewAsteroid.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "NewAsteroid.h"
#include "SpriteComponent.h"
#include "MoveComponent.h"
#include "Game.h"
#include "Random.h"
#include "CircleComponent.h"
NewAsteroid::NewAsteroid(Game* game)
: Actor(game)
, mCircle(nullptr)
, mHp(3) // Initialisiere Lebenspunkte auf 3
{
// Initialize to random position/orientation
Vector2 randPos = Random::GetVector(Vector2::Zero,
Vector2(1024.0f, 768.0f));
SetPosition(randPos);

SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));

// Create a sprite component
SpriteComponent* sc = new SpriteComponent(this);
sc->SetTexture(game->GetTexture("Assets/NewAsteroid.png"));

// Create a move component, and set a forward speed
MoveComponent* mc = new MoveComponent(this);
mc->SetForwardSpeed(250.0f);

// Create a circle component (for collision)
mCircle = new CircleComponent(this);
mCircle->SetRadius(15.0f);

// Add to mNewAsteroids in game
game->AddNewAsteroid(this);
}

NewAsteroid::~NewAsteroid()
{
GetGame()->RemoveNewAsteroid(this);
}
23 changes: 23 additions & 0 deletions Chapter03/NewAsteroid.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include "Actor.h"
#include "CircleComponent.h"
class NewAsteroid : public Actor
{
public:
NewAsteroid(Game* game);
~NewAsteroid();
CircleComponent* GetCircle() { return mCircle; }

private:
CircleComponent* mCircle;
int mHp; // hp
friend class Laser;
};
2 changes: 1 addition & 1 deletion Chapter03/SpriteComponent.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@

#pragma once
#include "Component.h"
#include "SDL/SDL.h"
#include "SDL.h"
class SpriteComponent : public Component
{
public:
Expand Down
Binary file addedChapter03/build/Assets/Asteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/NewAsteroid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/Ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/ShipWithThrust.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedChapter03/build/Assets/StartScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Chapter03/makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
CC = g++ #GNU C++ Compiler
CFLAGS = -std=c++17 -Wall #Compiler:c++17 Standard with warnings
LDFLAGS = -lGLEW -lSDL2 -lSOIL -lglfw -lSDL2_image #Libraries

INCDIR = -I/usr/include/GLFW -I/usr/include/SDL2 #Include directories, Path to header Files.

SRCS = $(wildcard *.cpp) #All .cpp -> SRCS
OBJS = $(SRCS:.cpp=.o) #.cpp -> .o
EXEC = spaceship_game
ASSETDIR = Assets
TARGETDIR = build

all: assets $(EXEC)

#EXEC depends on OBJS
$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(TARGETDIR)/$(EXEC) $(LDFLAGS) #-o flag -> place where it will be saved

#.cpp-> .o
.cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ #-c flag instructs compiler to compile the source code into object code without linking it -> .o

assets:
@echo "Copying assets folder..."
@mkdir -p $(TARGETDIR)/Assets #-p flag = parent -> recursive creation of directories
@cp -r $(ASSETDIR)/* $(TARGETDIR)/Assets #copies recursively ASSETDIR into Assets

clean:
rm -f $(OBJS) $(TARGETDIR)/$(EXEC)
@echo "Cleaning up assets folder..."
@rm -rf $(TARGETDIR)/Assets
#.Phony -> no files only executable
.PHONY: all clean assets