Merge branch 'master' of github.com:Thomas776/SAE102-SpaceInvaders

This commit is contained in:
Djalim Simaila 2022-01-10 16:30:25 +01:00
commit 1ae410638e
8 changed files with 41 additions and 58 deletions

View File

@ -5,7 +5,7 @@
class GoodPixelManager : public PixelManager{
void loadSprites(SpriteTasks& tasks) override;
void loadSprites(vector<Task>& tasks) override;
MySprite player;
MySprite invaderA;

View File

@ -41,22 +41,24 @@ typedef nsGui::GlutFont::GlutFonts Font;
*/
// The convention seems to just add a number to the macro name
#define ADD_TASK(X) ADD_TASK2(X, X)
#define ADD_TASK2(X, Y) tasks.push_back((X).asyncLoad("assets/"#Y".si2"));
#define ADD_SPRITE_TASK(X) ADD_SPRITE_TASK2(X, X)
#define ADD_SPRITE_TASK2(X, Y) tasks.push_back((X).asyncLoad("assets/"#Y".si2"));
typedef vector<future<void>> SpriteTasks;
typedef future<void> Task;
class PixelManager{
public:
MinGL& window;
mutable vector<Task> drawTasks;
/*!
* @brief loads sprites in parallel using multiple threads
* @param[in] vec : We take his ownership, so
* @fn void loadSprites();
*/
virtual void loadSprites(SpriteTasks& tasks);
virtual void loadSprites(vector<Task>& tasks);
/*!
@ -254,20 +256,6 @@ public:
* @fn void drawGodBench(int y) const
*/
void drawGodBench(int y) const;
/*!
* @brief display god's right hand
* @param[in] pos : pixel coordiniates of the hand
* @fn void drawGodRightHand(const Position& pos) const;
*/
void drawGodRightHand(const Position& pos) const;
/*!
* @brief display god's left hand
* @param[in] pos : pixel coordiniates of the hand
* @fn void drawGodLeftHand(const Position& pos) const;
*/
void drawGodLeftHand(const Position& pos) const;
/*!
* @brief display god's face

View File

@ -113,8 +113,8 @@ void Game::displayGod() const {
Position leftHand(GOD_HAND_DISTANCE, god.counter-GOD_BENCH_SIZE);
Position rightHand(pm->getScreenWidth()-GOD_HAND_DISTANCE-GOD_HAND_SIZE, god.counter-GOD_BENCH_SIZE);
pm->drawGodLeftHand(leftHand);
pm->drawGodRightHand(rightHand);
pm->drawSprite(pm->leftHand);
pm->drawSprite(pm->rightHand);
pm->drawGodFace(god.counter - GOD_BENCH_SIZE);
break;
}
@ -122,8 +122,8 @@ void Game::displayGod() const {
pm->drawGodBench(0);
Position leftHand(GOD_HAND_DISTANCE, 0);
Position rightHand(god.getRightHandPos(pm->getScreenWidth()));
pm->drawGodLeftHand(leftHand);
pm->drawGodRightHand(rightHand);
pm->drawSprite(pm->leftHand);
pm->drawSprite(pm->rightHand);
pm->drawGodFace(0);
break;
}
@ -132,7 +132,7 @@ void Game::displayGod() const {
// Bezier curve
// counter goes [0-100]
pm->drawGodBench(0);
pm->drawGodLeftHand(Position(GOD_HAND_DISTANCE, 0));
pm->drawSprite(pm->leftHand, Position(GOD_HAND_DISTANCE, 0));
pm->drawGodFace(0);
Position pos(god.getRightHandPos(pm->getScreenWidth()));
@ -142,7 +142,7 @@ void Game::displayGod() const {
applyBezier(pos, endPos, god.counter / 100.0);
// pos is now the position we need to draw our hand to
pm->drawGodRightHand(pos);
pm->drawSprite(pm->rightHand, pos);
if(god.thrownInvType!=InvaderType::NONE){
pos+=Position(GOD_HAND_SIZE/2, GOD_HAND_SIZE/2);
@ -153,7 +153,7 @@ void Game::displayGod() const {
}
case GodState::THROW:{
pm->drawGodBench(0);
pm->drawGodLeftHand(Position(GOD_HAND_DISTANCE, 0));
pm->drawSprite(pm->leftHand, Position(GOD_HAND_DISTANCE, 0));
pm->drawGodFace(0);
// compute start position (not sure if we should store it or compute it each time ?)
@ -172,7 +172,7 @@ void Game::displayGod() const {
else handCounter = 30-god.counter;
handPos = handPos + god.thrownVector * (handCounter / 100.0);
}
pm->drawGodRightHand(handPos);
pm->drawSprite(pm->rightHand);
break;
}

View File

@ -33,7 +33,7 @@ Game::Game() : WININIT {
"\nValid values are : good,bad");
cout << "Loading sprites..." << endl;
SpriteTasks tasks;
vector<Task> tasks;
chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
pm->loadSprites(tasks);
@ -110,7 +110,7 @@ void Game::initGame(){
}
#define START_TIMER() DEBUG_INSTR(debugTime = chrono::high_resolution_clock::now())
#define PRINT_TIMER(X) DEBUG_MSG((X) << " :" << chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now()-debugTime).count())
#define PRINT_TIMER(X) DEBUG_MSG((X) << ": " << chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now()-debugTime).count())
WinValue Game::enterGameLoop(){ // returns when game is finished
// computed in advance for performance reasons

View File

@ -2,6 +2,7 @@
#include "mySprite.h"
future<void> MySprite::asyncLoad(const string& fname){
DEBUG_MSG("Load file " << fname)
return std::async(std::launch::async, [fname, this]() -> void {
ptr = std::make_unique<nsGui::Sprite>(fname);
});

View File

@ -25,9 +25,9 @@ PixelManager::PixelManager(MinGL& win) : window(win) {
}
void PixelManager::drawHeart(const Position& baseVector) const {
window << Circle(Position(10, 10)+baseVector,10, nsGraphics::KRed);
window << Circle(Position(30, 10)+baseVector,10, nsGraphics::KRed);
window << Triangle(Position(0,10)+baseVector,Position(40,10)+baseVector,Position(20,40)+baseVector,nsGraphics::KRed);
window << Circle(Position(10, 10)+baseVector,10, nsGraphics::KRed);
window << Circle(Position(30, 10)+baseVector,10, nsGraphics::KRed);
window << Triangle(Position(0,10)+baseVector,Position(40,10)+baseVector,Position(20,40)+baseVector,nsGraphics::KRed);
}
void PixelManager::drawInvaderA(const Position& baseVector, unsigned size, const RGBAcolor& color) const {
@ -70,6 +70,7 @@ void PixelManager::drawPlayer(unsigned x, unsigned width, const nsGraphics::RGBA
void PixelManager::drawMissile(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color) const {
window << Rectangle(baseVector, baseVector + Position(width, width * PROJ_LENGTH_FACTOR), color);
}
void PixelManager::drawTorpedo(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color) const {
@ -86,22 +87,14 @@ void PixelManager::drawGodBench(int y) const {
window << Rectangle(Position(0, y), Position(getScreenWidth(), y+GOD_BENCH_SIZE), nsGraphics::KGray);
}
void PixelManager::drawGodRightHand(const Position& pos) const {
drawSprite(rightHand, pos);
}
void PixelManager::drawGodLeftHand(const Position& pos) const {
drawSprite(leftHand, pos);
}
void PixelManager::drawGodFace(int y, bool angry) const {
Text t(
Position(getScreenWidth()/2, y),
angry ? ">w<" : ".w.",
nsGraphics::KBlue,
GlutFont::GlutFonts::BITMAP_TIMES_ROMAN_24,
Text::HorizontalAlignment::ALIGNH_CENTER
);
Position(getScreenWidth()/2, y),
angry ? ">w<" : ".w.",
nsGraphics::KBlue,
GlutFont::GlutFonts::BITMAP_TIMES_ROMAN_24,
Text::HorizontalAlignment::ALIGNH_CENTER
);
// computeHeight() returns a height bigger than the actual text size, that's why there's padding above it(
t.setPosition(t.getPosition()+Position(0, t.computeHeight()));

View File

@ -1,13 +1,13 @@
#include "pixelManager/goodPixelManager.h"
void GoodPixelManager::loadSprites(SpriteTasks& tasks) {
void GoodPixelManager::loadSprites(vector<Task>& tasks) {
PixelManager::loadSprites(tasks);
ADD_TASK(player)
ADD_TASK(invaderA)
ADD_TASK(invaderB)
ADD_TASK(invaderC)
ADD_TASK(missile)
ADD_TASK(torpedo)
ADD_SPRITE_TASK(player)
ADD_SPRITE_TASK(invaderA)
ADD_SPRITE_TASK(invaderB)
ADD_SPRITE_TASK(invaderC)
ADD_SPRITE_TASK(missile)
ADD_SPRITE_TASK(torpedo)
}
GoodPixelManager::GoodPixelManager(MinGL& win) : PixelManager(win) {

View File

@ -1,11 +1,11 @@
#include "pixelManager/pixelManager.h"
void PixelManager::loadSprites(SpriteTasks& tasks){
ADD_TASK(logo)
ADD_TASK(menuBackground)
ADD_TASK(gameBackground)
ADD_TASK2(rightHand, hand)
void PixelManager::loadSprites(vector<Task>& tasks){
ADD_SPRITE_TASK(logo)
ADD_SPRITE_TASK(menuBackground)
ADD_SPRITE_TASK(gameBackground)
ADD_SPRITE_TASK2(rightHand, hand)
}
void PixelManager::startFrame() const {
@ -13,6 +13,7 @@ void PixelManager::startFrame() const {
}
void PixelManager::endFrame() const {
for(Task& t : drawTasks)t.wait();
window.finishFrame();
}