Skip to content

Commit

Permalink
Refactored Classes to include const functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumflr committed Nov 11, 2019
1 parent 40a1368 commit 0b88e6e
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 73 deletions.
Binary file modified BloonsTD5.tns
Binary file not shown.
10 changes: 5 additions & 5 deletions Charachters/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void Character::deselect() {

#define FULL_UPGRADE_BAR 91

void Character::upgradeSideBar(const char* title, SDL_Surface* upgrade1, unsigned char addx, char* title1, SDL_Surface* upgrade2, char* title2, SDL_Surface* avatar, bool* avail) {
void Character::upgradeSideBar(const char* title, SDL_Surface* upgrade1, unsigned char addx, char* title1, SDL_Surface* upgrade2, char* title2, SDL_Surface* avatar, bool* avail) const {
if (hud_x < FULL_UPGRADE_BAR) {
hud_x += 13;
}
Expand All @@ -53,17 +53,17 @@ void Character::upgradeSideBar(const char* title, SDL_Surface* upgrade1, unsigne

SDL_Surface* upgrade_title = nSDL_LoadImage(hud_upgrade_title_back);
SDL_SetColorKey(upgrade_title, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(upgrade_title->format, 0, 0, 0));
SDL_CustomBlitCorner(upgrade_title, lefty ? S_WIDTH - (hud_x * 7 / 8) : (hud_x / 7) - (FULL_UPGRADE_BAR / 7), 5);
SDL_CustomBlitCorner(upgrade_title, lefty ? S_WIDTH - (hud_x * 7 / 8) : hud_x + 7 - FULL_UPGRADE_BAR, 5);
SDL_FreeSurface(upgrade_title);

nSDL_DrawString(screen, font, lefty ? S_WIDTH - (hud_x * 6 / 7) : (hud_x / 7) - (short)(FULL_UPGRADE_BAR / 7), 10, title);
if ((hud_x + 8 - FULL_UPGRADE_BAR >= 0 && lefty) || !lefty) nSDL_DrawString(screen, font, lefty ? S_WIDTH - (hud_x * 6 / 7) : hud_x + 8 - FULL_UPGRADE_BAR, 10, title);

SDL_Rect border = {lefty ? S_WIDTH - hud_x + 8 : hud_x + 8 - FULL_UPGRADE_BAR, 31, 42, 42};
SDL_FillRect(screen, &border, SDL_MapRGB(screen->format, 72, 42, 2));

SDL_CustomBlitCorner(avatar, lefty ? (hud_x * 7 / 8) - S_WIDTH : hud_x + 11 - FULL_UPGRADE_BAR, 35);
SDL_CustomBlitCorner(avatar, lefty ? S_WIDTH - (hud_x * 7 / 8) : hud_x + 11 - FULL_UPGRADE_BAR, 35);

short xpos = lefty ? S_WIDTH - hud_x + 5 : hud_x + 1 - FULL_UPGRADE_BAR;
signed short xpos = lefty ? S_WIDTH - hud_x + 5 : hud_x + 1 - FULL_UPGRADE_BAR;
for (unsigned char i = 0; i < 2; i++) {
SDL_Surface* upgrade_slot = avail[i] ? nSDL_LoadImage(hud_upgrade_slot) : nSDL_LoadImage(hud_upgrade_slot_unavailable);
SDL_SetColorKey(upgrade_slot, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(upgrade_slot->format, 0, 0, 0));
Expand Down
2 changes: 1 addition & 1 deletion Charachters/Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Character {
static unsigned char hud_x;
public:
Character(short inx, short iny);
void upgradeSideBar(const char* title, SDL_Surface* upgrade1, unsigned char addx, char* title1, SDL_Surface* upgrade2, char* title2, SDL_Surface* avatar, bool* avail);
void upgradeSideBar(const char* title, SDL_Surface* upgrade1, unsigned char addx, char* title1, SDL_Surface* upgrade2, char* title2, SDL_Surface* avatar, bool* avail) const;
virtual ~Character();
virtual unsigned short getUpgradeCost(unsigned char path) = 0;
virtual void upGrade(unsigned char path) = 0;
Expand Down
16 changes: 8 additions & 8 deletions Interface/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ HUD::HUD() {
lives = 150; //https://bloons.fandom.com/wiki/Lives
}

void HUD::draw(float fElapsedTime) {
void HUD::draw(float fElapsedTime) const {
SDL_Rect hud_rect = {0, LEVEL_HEIGHT + 1};
SDL_BlitSurface(hud, NULL, screen, &hud_rect);
//every plank starting at x = 100
Expand Down Expand Up @@ -144,39 +144,39 @@ void HUD::addMoney(unsigned int add) {
money += add;
}

signed int HUD::getLives() {
signed int HUD::getLives() const {
return lives;
}

void HUD::setLives(signed int inlives) {
lives = inlives;
}

signed char HUD::getSelected() {
signed char HUD::getSelected() const {
return selecting;
}

unsigned int HUD::getMoney() {
unsigned int HUD::getMoney() const {
return money;
}

void HUD::setMoney(unsigned int mon) {
money = mon;
}

float HUD::getSpeed() {
float HUD::getSpeed() const {
return sped[speed];
}

float HUD::getInvSpeed() {
float HUD::getInvSpeed() const {
return 1.0 / sped[speed];
}

unsigned char HUD::getRound() {
unsigned char HUD::getRound() const {
return round - 1; //Round is offset by one for top left HUD
}

char HUD::getCharachterOffset() {
char HUD::getCharachterOffset() const {
return charachterOffset;
}

Expand Down
16 changes: 8 additions & 8 deletions Interface/HUD.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class HUD {
public:
HUD();
~HUD();
void draw(float fElapsedTime);
void draw(float fElapsedTime) const;
void select(long oT, unsigned char object, Level* l);
void deselect(Level* l);
signed char getSelected();
unsigned int getMoney();
signed char getSelected() const;
unsigned int getMoney() const;
void addMoney(signed int add);
void addMoney(unsigned int add);
float getSpeed();
float getInvSpeed();
float getSpeed() const;
float getInvSpeed() const;
void setSpeed(unsigned char inspd);
signed int getLives();
char getCharachterOffset();
unsigned char getRound();
signed int getLives() const;
char getCharachterOffset() const;
unsigned char getRound() const;
void setLives(signed int inlives);
void setMoney(unsigned int mon);
private:
Expand Down
8 changes: 4 additions & 4 deletions Interface/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Level::Level(LevelName l) {
}
}

void Level::draw() {
void Level::draw() const {
SDL_BlitSurface(level, NULL, screen, NULL);

if (isKeyPressed(KEY_NSPIRE_ENTER)) {
Expand All @@ -86,21 +86,21 @@ void Level::draw() {
}
}

void Level::drawUp(HUD* h) {
void Level::drawUp(HUD* h) const {
for (short i = 0; i < charchters.size(); i++) {
charchters[i]->drawUp(h);
}
}

bool Level::hasRoundCleared(unsigned char currentRound) {
bool Level::hasRoundCleared(unsigned char currentRound) const {
unsigned char count = 0;
for (unsigned short i = 0; i < sizeof(rounds[currentRound])/sizeof(rounds[currentRound][0]); i++) {
if (rounds[currentRound][i][0] <= 0) count++;
}
return count == numBloonTypes;
}

constexpr const static float timeBetween = 0.072;
constexpr static float timeBetween = 0.072;

void Level::loop(float fElapsedTime, HUD* h) {
if (h->getRound() < 16 and h->getSpeed() > PAUSE) {
Expand Down
8 changes: 4 additions & 4 deletions Interface/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class Level {
public:
Level(LevelName l);
~Level();
bool hasRoundCleared(unsigned char currentRound);
void draw();
void drawUp(HUD* h);
bool hasRoundCleared(unsigned char currentRound) const;
void draw() const;
void drawUp(HUD* h) const;
void loop(float fElapsedTime, HUD* h);
void addCharachter(Mouse* m, HUD* h);
std::vector<Character*>& getCharachters();
std::vector<SDL_Rect>* getTrackCheckpoints();
std::vector<SDL_Rect>* getTrackCheckpoints() const;
private:
float timePassed;

Expand Down
14 changes: 7 additions & 7 deletions Interface/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Mouse::Mouse() {
SDL_SetColorKey(cursor, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(cursor->format, 0, 0, 0));
}

bool Mouse::intersects(std::vector<SDL_Rect>* rects) {
bool Mouse::intersects(std::vector<SDL_Rect>* rects) const {
SDL_Rect mRect = {x, y, 1, 1};
SDL_Rect intersection;
for (unsigned char i = 0; i < rects->size(); i++) {
Expand All @@ -21,7 +21,7 @@ bool Mouse::intersects(std::vector<SDL_Rect>* rects) {
return false;
}

void Mouse::draw(HUD* h) {
void Mouse::draw(HUD* h) const {
signed char touching = whichInteractable(h);
signed char touching_off = touching + h->getCharachterOffset();
signed char selecting = h->getSelected() + h->getCharachterOffset();
Expand Down Expand Up @@ -65,7 +65,7 @@ bool Mouse::hasMoved() {
return false;
}

bool Mouse::isTouchingInteractable() {
bool Mouse::isTouchingInteractable() const {
SDL_Rect mouse = {x, y, 1, 1};
SDL_Rect intersection; //used to find intersection area of rects
for (unsigned char i = 0; i < interactables.size(); i++) {
Expand All @@ -76,7 +76,7 @@ bool Mouse::isTouchingInteractable() {
return false;
}

signed char Mouse::whichInteractable(HUD* h) {
signed char Mouse::whichInteractable(HUD* h) const {
SDL_Rect mouse = {x, y, 1, 1};
SDL_Rect intersection; //used to find intersection area of rects
for (unsigned char i = 0; i < interactables.size(); i++) {
Expand All @@ -87,15 +87,15 @@ signed char Mouse::whichInteractable(HUD* h) {
return -1;
}

bool Mouse::hasClicked() {
bool Mouse::hasClicked() const {
return tpad_report.pressed == true;
}

short Mouse::getX() {
short Mouse::getX() const {
return x;
}

short Mouse::getY() {
short Mouse::getY() const {
return y;
}

Expand Down
14 changes: 7 additions & 7 deletions Interface/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class Mouse {
SDL_Surface* cursor;
public:
Mouse();
void draw(HUD* h);
void draw(HUD* h) const;
void update();
signed char whichInteractable(HUD* h);
bool isTouchingInteractable();
bool intersects(std::vector<SDL_Rect>* rects);
bool hasClicked();
signed char whichInteractable(HUD* h) const;
bool isTouchingInteractable() const;
bool intersects(std::vector<SDL_Rect>* rects) const;
bool hasClicked() const;
bool hasMoved();
short getX();
short getY();
short getX() const;
short getY() const;
void quit();
};
4 changes: 2 additions & 2 deletions Projectile/Projectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Projectile::~Projectile() {
//??
}

float Projectile::getX() {
float Projectile::getX() const {
return x;
}

float Projectile::getY() {
float Projectile::getY() const {
return y;
}
4 changes: 2 additions & 2 deletions Projectile/Projectile.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Projectile {
Bloon* lastPopped;
public:
Projectile(short inx, short iny, double inangle);
float getX();
float getY();
float getX() const;
float getY() const;
virtual ~Projectile();
virtual void draw() = 0;
virtual bool gameLoop(float fElapsedTime, std::vector<Bloon*>* bloons, HUD* h) = 0;
Expand Down
33 changes: 18 additions & 15 deletions utils.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#include "utils.h"

double radians(double degrees) {
return degrees * PI / 180.0;
double radians(double degree) {
return degree * PI / 180.0;
}

double degrees(double radian) {
return radian * 180.0 / PI;
}

double distBetween(short x1, short y1, short x2, short y2) {
short sideH = abs(x1 - x2);
short sideV = abs(y1 - y2);
return hypot(sideH, sideV);
}

double angleBetween(short x1, short y1, short x2, short y2) {
short sideH = x1 - x2;
short sideV = y1 - y2;

return 90 - degrees(std::atan2(sideV, sideH));
}

//From http://sdl.beuc.net/sdl.wiki/Pixel_Access
void put_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) {
Expand Down Expand Up @@ -200,19 +216,6 @@ bool SDL_IntersectRect(SDL_Rect *A, SDL_Rect *B, SDL_Rect *intersection) {
return (intersection->w && intersection->h);
} //Copied from https://github.com/hoffa/nSDL/blob/de58382bd540c26cbd614259541f967277662b72/src/video/SDL_surface.c

double distBetween(short x1, short y1, short x2, short y2) {
short sideH = abs(x1 - x2);
short sideV = abs(y1 - y2);
return hypot(sideH, sideV);
}

double angleBetween(short x1, short y1, short x2, short y2) {
short sideH = x1 - x2;
short sideV = y1 - y2;

return 90 - std::atan2(sideV, sideH) * 180 / PI;
}

//Functions with Images
SDL_Surface* load_Level(LevelName choice) {
SDL_Surface* result;
Expand Down
21 changes: 11 additions & 10 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,38 @@ extern std::vector<SDL_Rect> trackCheckpoints;
static const touchpad_info_t *tpad_info = touchpad_getinfo();

//initial range of charachters
static const short charRange[] = {40, 0, 0, 60};
static constexpr short charRange[] = {40, 0, 0, 60};
//cost of charachters
static const short charCost[] = {200, 0, 0, 380};
static constexpr short charCost[] = {200, 0, 0, 380};
//health bloons take away
static const short bloonHealth[] = {1, 2, 3, 4, 5};
static constexpr short bloonHealth[] = {1, 2, 3, 4, 5};
//bloons speeds
static const float bloonSpeed[] = {1, 1.33, 1.66, 3.33, 3.66};
static constexpr float bloonSpeed[] = {1, 1.33, 1.66, 3.33, 3.66};
//bloon worth in money
static const unsigned int bloonWorth[] = {1, 2, 3, 4, 5};
static constexpr unsigned int bloonWorth[] = {1, 2, 3, 4, 5};
//upgrade pricess per charchter
static const unsigned short Dart_Monkey_upgradePrices[2][4] = {
static constexpr unsigned short Dart_Monkey_upgradePrices[2][4] = {
{90, 120, 500, 1500},
{140, 170, 330, 8000}
};
static const unsigned short Boomerang_Monkey_upgradePrices[2][4] = {
static constexpr unsigned short Boomerang_Monkey_upgradePrices[2][4] = {
{250, 280, 1400, 9000},
{100, 150, 1600, 3000}
};

double radians(double degree);
double degrees(double radian);
double distBetween(short x1, short y1, short x2, short y2);
double angleBetween(short x1, short y1, short x2, short y2);
void put_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
Uint32 get_pixel( SDL_Surface *surface, int x, int y);
SDL_Surface* flip_surface( SDL_Surface *surface, int flags);
double radians(double degrees);
void SDL_CustomBlit(SDL_Surface* img, short x, short y, double angle);
void SDL_CustomBlit(SDL_Surface* img, float x, float y, double angle);
void SDL_CustomBlitCorner(SDL_Surface* img, short x, short y);
void SDL_RotateBlitAroundPoint(SDL_Surface* img, double angle, short dist, short x, short y);
void SDL_RotateBlitAroundPoint(SDL_Surface* img, double angle, short dist, short x, short y, short offsetY);
bool SDL_IntersectRect(SDL_Rect *A, SDL_Rect *B, SDL_Rect *intersection);
double distBetween(short x1, short y1, short x2, short y2);
double angleBetween(short x1, short y1, short x2, short y2);
SDL_Surface* load_Level(LevelName choice);
SDL_Surface* load_Charachter(Charachter choice);
SDL_Surface* load_Bloon(BloonType choice);

0 comments on commit 0b88e6e

Please sign in to comment.