Skip to content

Commit

Permalink
Style renamed multiple methods so they better represent what they do
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalM31252 committed Dec 8, 2024
1 parent d368a94 commit 9012aa8
Show file tree
Hide file tree
Showing 48 changed files with 101 additions and 118 deletions.
Empty file removed Ladder.cpp
Empty file.
2 changes: 0 additions & 2 deletions Ladder.h

This file was deleted.

8 changes: 4 additions & 4 deletions libs/Barrel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ void Barrel::updatePosition() {
// move to animation manager ? Fix in the future
void Barrel::loadNextSprite() {
if (this->currentSpriteId == 1) {
ScreenManager::loadTexture(this, BARREL_1_FILENAME);
ScreenManager::setSurface(this, BARREL_1_FILENAME);
this->currentSpriteId++;
}
else if (this->currentSpriteId == 2) {
ScreenManager::loadTexture(this, BARREL_2_FILENAME);
ScreenManager::setSurface(this, BARREL_2_FILENAME);
this->currentSpriteId++;
}
else if (this->currentSpriteId == 3) {
ScreenManager::loadTexture(this, BARREL_3_FILENAME);
ScreenManager::setSurface(this, BARREL_3_FILENAME);
this->currentSpriteId++;
}
else {
ScreenManager::loadTexture(this, BARREL_4_FILENAME);
ScreenManager::setSurface(this, BARREL_4_FILENAME);
this->currentSpriteId = 1;
}
}
2 changes: 1 addition & 1 deletion libs/BarrelFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void BarrelFactory::addBarrelToContainer() {
auto barrel = std::make_shared<Barrel>();

// WHY IS THIS HERE // Fix in the future
ScreenManager::loadTexture(barrel.get(), BARREL_1_FILENAME);
ScreenManager::setSurface(barrel.get(), BARREL_1_FILENAME);

barrel->setPosition(STARTING_X_DONKEY_KONG + DONKEY_KONG_WIDTH, STARTING_Y_DONKEY_KONG);
barrel->createSrcRect();
Expand Down
3 changes: 1 addition & 2 deletions libs/CollisionDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ bool CollisionDetector::isGameObjectOnTopOfPlatform(const std::shared_ptr<const
int bottomY = gameObjectRect.y + gameObjectRect.h;

// Check if the bottom-center point is on or near the platform
return isPointAPartOfLine(bottomCenterX, bottomY, platform) &&
isXWithinWidthOfPlatform(bottomCenterX, platform);
return isPointAPartOfLine(bottomCenterX, bottomY, platform) && isXWithinWidthOfPlatform(bottomCenterX, platform);
}


Expand Down
4 changes: 2 additions & 2 deletions libs/GameObject.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "GameObject.h"

void GameObject::createSrcRect() {
srcRect.w = sprite->w;
srcRect.h = sprite->h;
srcRect.w = surface->w;
srcRect.h = surface->h;
}

void GameObject::setSrcRect(int w, int h) {
Expand Down
2 changes: 1 addition & 1 deletion libs/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GameObject {
int ypos;
SDL_Rect destRect;
SDL_Rect srcRect; // destRect is the physical representation of the game object // srcRect is whats going to be mapped onto the destRect
SDL_Surface* sprite; // the image of the game object
SDL_Surface* surface; // the image of the game object

void createSrcRect();
void createDestRect();
Expand Down
24 changes: 12 additions & 12 deletions libs/LevelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LevelLoader::LevelLoader(std::shared_ptr<GameObjectContainer> container)

void LevelLoader::createPlayer() {
auto player = std::make_shared<Player>();
ScreenManager::loadTexture(player.get(), PLAYER_1_FILENAME);
ScreenManager::setSurface(player.get(), PLAYER_1_FILENAME);

player->setPosition(STARTING_X_PLAYER, STARTING_Y_PLAYER);
player->createSrcRect();
Expand All @@ -21,7 +21,7 @@ void LevelLoader::createDonkeyKong() {
gameObjectContainer->barrelContainer = std::make_shared<BarrelContainer>();

auto donkeyKong = std::make_shared<Gorilla>(gameObjectContainer->barrelContainer);
ScreenManager::loadTexture(donkeyKong.get(), DONKEY_KONG_FILENAME);
ScreenManager::setSurface(donkeyKong.get(), DONKEY_KONG_FILENAME);

donkeyKong->setPosition(STARTING_X_DONKEY_KONG, STARTING_Y_DONKEY_KONG);
donkeyKong->createSrcRect();
Expand All @@ -32,7 +32,7 @@ void LevelLoader::createDonkeyKong() {

void LevelLoader::createPrincess() {
auto princess = std::make_shared<GameObject>();
ScreenManager::loadTexture(princess.get(), PRINCESS_FILENAME);
ScreenManager::setSurface(princess.get(), PRINCESS_FILENAME);

princess->setPosition(STARTING_X_PRINCESS, STARTING_Y_PRINCESS);
princess->createSrcRect();
Expand Down Expand Up @@ -73,7 +73,7 @@ void LevelLoader::createPlatforms1() {

// Platform 1: Horizontal
auto plat1 = std::make_shared<Platform>(1, 400, 100, 8, 0); // x, y, width, height, angle
ScreenManager::loadTexture(plat1.get(), PLATFORM_FILENAME);
ScreenManager::setSurface(plat1.get(), PLATFORM_FILENAME);
platformContainer->addPlatform(plat1);

// ADD THEM
Expand All @@ -83,13 +83,13 @@ void LevelLoader::createPlatforms1() {

void LevelLoader::createLadders1() {
auto ladd1 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd1.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd1.get(), LADDER_FILENAME);
ladd1->setPosition(525, 129);
ladd1->setSrcRect(LADDER_WIDTH, 170);
ladd1->setDestRect(LADDER_WIDTH, 170);

auto ladd2 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd2.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd2.get(), LADDER_FILENAME);
ladd2->setPosition(250, 79);
ladd2->setSrcRect(LADDER_WIDTH, 50);
ladd2->setDestRect(LADDER_WIDTH, 50);
Expand Down Expand Up @@ -132,13 +132,13 @@ void LevelLoader::createLadders1() {

void LevelLoader::createLadders2() {
auto ladd1 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd1.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd1.get(), LADDER_FILENAME);
ladd1->setPosition(SCREEN_WIDTH - LARGE_MARGIN - LADDER_WIDTH, 229);
ladd1->setSrcRect(LADDER_WIDTH, 170);
ladd1->setDestRect(LADDER_WIDTH, 170);

auto ladd2 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd2.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd2.get(), LADDER_FILENAME);
ladd2->setPosition(SCREEN_WIDTH - LARGE_MARGIN - LADDER_WIDTH - LADDER_WIDTH, 79);
ladd2->setSrcRect(LADDER_WIDTH, 150);
ladd2->setDestRect(LADDER_WIDTH, 150);
Expand Down Expand Up @@ -178,25 +178,25 @@ void LevelLoader::createLadders2() {
void LevelLoader::createLadders3() {
// Creating each ladder with a shared pointer
auto ladd1 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd1.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd1.get(), LADDER_FILENAME);
ladd1->setPosition(SCREEN_WIDTH - LARGE_MARGIN - LADDER_WIDTH, 299);
ladd1->setSrcRect(LADDER_WIDTH, LARGE_MARGIN);
ladd1->setDestRect(LADDER_WIDTH, LARGE_MARGIN);

auto ladd2 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd2.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd2.get(), LADDER_FILENAME);
ladd2->setPosition(LARGE_MARGIN, 199);
ladd2->setSrcRect(LADDER_WIDTH, LARGE_MARGIN);
ladd2->setDestRect(LADDER_WIDTH, LARGE_MARGIN);

auto ladd3 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd3.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd3.get(), LADDER_FILENAME);
ladd3->setPosition(SCREEN_WIDTH - LARGE_MARGIN - LADDER_WIDTH, 129);
ladd3->setSrcRect(LADDER_WIDTH, 70);
ladd3->setDestRect(LADDER_WIDTH, 70);

auto ladd4 = std::make_shared<GameObject>();
ScreenManager::loadTexture(ladd4.get(), LADDER_FILENAME);
ScreenManager::setSurface(ladd4.get(), LADDER_FILENAME);
ladd4->setPosition(250, 79);
ladd4->setSrcRect(LADDER_WIDTH, 50);
ladd4->setDestRect(LADDER_WIDTH, 50);
Expand Down
2 changes: 1 addition & 1 deletion libs/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Platform {
public:
SDL_Rect rect; // Position and size of the platform
double angle; // Tilt angle in degrees
SDL_Surface* sprite; // the image of the game object
SDL_Surface* surface; // the image of the game object

// Constructor
Platform(int x, int y, int width, int height, double angle);
Expand Down
22 changes: 11 additions & 11 deletions libs/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,39 @@ void Player::initJump() {

void Player::loadNextRunningSprite() {
if (this->currentRunningSpriteId == 1) {
ScreenManager::loadTexture(this, PLAYER_1_FILENAME);
ScreenManager::setSurface(this, PLAYER_1_FILENAME);
this->currentRunningSpriteId++;
}
else if (this->currentRunningSpriteId == 2) {
ScreenManager::loadTexture(this, PLAYER_2_FILENAME);
ScreenManager::setSurface(this, PLAYER_2_FILENAME);
this->currentRunningSpriteId++;
}
else {
ScreenManager::loadTexture(this, PLAYER_3_FILENAME);
ScreenManager::setSurface(this, PLAYER_3_FILENAME);
this->currentRunningSpriteId = 1;
}

if (currentDirectionOfMovementX == DirectionX::LEFT) {
ScreenManager::flipTextureHorizontally(this->sprite);
ScreenManager::flipSurfaceHorizontally(this->surface);
}
}

void Player::loadJumpingSprite() {
ScreenManager::loadTexture(this, PLAYER_3_FILENAME);
ScreenManager::setSurface(this, PLAYER_3_FILENAME);
if (currentDirectionOfMovementX == DirectionX::LEFT) {
ScreenManager::flipTextureHorizontally(this->sprite);
ScreenManager::flipSurfaceHorizontally(this->surface);
}
}

void Player::loadClimbingSprite() {
ScreenManager::loadTexture(this, PLAYER_CLIMB_1);
ScreenManager::setSurface(this, PLAYER_CLIMB_1);
currentClimbingSpriteId = 1;
}

void Player::loadNextClimbingSprite(){
ScreenManager::loadTexture(this, PLAYER_CLIMB_1);
ScreenManager::setSurface(this, PLAYER_CLIMB_1);
if (currentClimbingSpriteId == 2) {
ScreenManager::flipTextureHorizontally(this->sprite);
ScreenManager::flipSurfaceHorizontally(this->surface);
currentClimbingSpriteId = 1;
}
else {
Expand All @@ -131,9 +131,9 @@ void Player::loadNextClimbingSprite(){
}

void Player::loadIdleSprite() {
ScreenManager::loadTexture(this, PLAYER_1_FILENAME);
ScreenManager::setSurface(this, PLAYER_1_FILENAME);
if (currentDirectionOfMovementX == DirectionX::LEFT) {
ScreenManager::flipTextureHorizontally(this->sprite);
ScreenManager::flipSurfaceHorizontally(this->surface);
}
currentRunningSpriteId = 1;
}
Expand Down
97 changes: 25 additions & 72 deletions libs/ScreenManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ void ScreenManager::checkSDL() const {
}
}

// maybe add window and renderer as parameters
void ScreenManager::createWindowAndRenderer() {
rc = SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer);
int rc = SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer);
if (rc != 0) {
printf("SDL_CreateWindowAndRenderer error: %s\n", SDL_GetError());
}
}

void ScreenManager::setHint() const { // use this to recieve input from window
// use this to recieve input from window
void ScreenManager::setHint() const {
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
}

Expand Down Expand Up @@ -99,26 +101,16 @@ void ScreenManager::drawAdditionalInfo(double deltaTime) const {
drawString(screen->w / 2 - text.length() * 8 / 2, 7, text, 1);
}

// draw a surface sprite on a surface screen in point (x, y)
// (x, y) is the center of sprite on screen
// draw a surface from the gameObject on surface screen, starting from the point (xpos, ypos)
void ScreenManager::drawSurface(std::shared_ptr<GameObject> gameObject, int xpos, int ypos) const {
SDL_Rect dest = gameObject->destRect;
dest.x = xpos;
dest.y = ypos;
dest.w = gameObject->sprite->w;
dest.h = gameObject->sprite->h;
SDL_BlitSurface(gameObject->sprite, nullptr, screen, &dest);
dest.w = gameObject->surface->w;
dest.h = gameObject->surface->h;
SDL_BlitSurface(gameObject->surface, nullptr, screen, &dest);
}

//void ScreenManager::drawSurfaceLadder(std::shared_ptr<GameObject> ladder, int xpos, int ypos) const {
// SDL_Rect dest = ladder->destRect;
// dest.x = xpos;
// dest.y = ypos;
// ladder->sprite->w = dest.w;
// ladder->sprite->h = dest.h;
// SDL_BlitSurface(ladder->sprite, nullptr, screen, &dest);
//}

void ScreenManager::drawSurfaceLadder(std::shared_ptr<GameObject> ladder, int xpos, int ypos) const {
int tileHeight = 16; // Original height of each ladder BMP section
int tileWidth = 16; // Original width of the ladder BMP
Expand All @@ -137,14 +129,10 @@ void ScreenManager::drawSurfaceLadder(std::shared_ptr<GameObject> ladder, int xp
destRect.h = currentTileHeight; // Set height for current tile

srcRect.h = currentTileHeight; // Match source height for partial tiles
SDL_BlitScaled(ladder->sprite, &srcRect, screen, &destRect);
SDL_BlitScaled(ladder->surface, &srcRect, screen, &destRect);
}
}

//void ScreenManager::drawSurfacePlatform(std::shared_ptr<Platform> platform) const {
// SDL_BlitSurface(platform->sprite, nullptr, screen, &platform->rect);
//}

// draw a text txt on surface screen, starting from the point (x, y)
// charset is a 128x128 bitmap containing character images
void ScreenManager::drawString(int x, int y, const std::string& text, int scale = 1) const {
Expand Down Expand Up @@ -189,42 +177,8 @@ void ScreenManager::drawLine(int x, int y, int l, int dx, int dy, Uint32 color)
}
}

//void ScreenManager::drawPlatorm(std::shared_ptr<Platform> platform) {
// int length = sqrt(pow(platform->x2pos - platform->x1pos, 2) + pow(platform->y2pos - platform->y1pos, 2));
// if (platform->y1pos != platform->y2pos) { // xdddddddddddddddddd
// int differenceBetweenX = sqrt(pow(platform->x2pos - platform->x1pos, 2));
// int x = platform->x1pos;
// int y = platform->y1pos;
// if (platform->y1pos > platform->y2pos) {
// for (int i = 0; i < differenceBetweenX; i++) {
// drawPixel(screen, x, y, 0xffffffff);
// x++;
// y--;
// }
// }
// if (platform->y1pos < platform->y2pos) {
// for (int i = 0; i < differenceBetweenX; i++) {
// drawPixel(screen, x, y, 0xffffffff);
// x++;
// y++;
// }
// }
// }
// else {
// drawLine(platform->x1pos, platform->y1pos, length, 1, 0, 0xffffffff);
// }
//}

//// Render the platform
//void Platform::render() {
// // Render the platform with tilt
// SDL_RenderCopyEx(renderer, texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
//}

void ScreenManager::drawPlatform(std::shared_ptr<Platform> platform) {
// Render the platform texture to match the platform->rect dimensions
SDL_Texture* scrtex = SDL_CreateTextureFromSurface(renderer, platform->sprite);
SDL_RenderCopyEx(renderer, scrtex, nullptr, &platform->rect, platform->angle, nullptr, SDL_FLIP_NONE);
}

void ScreenManager::clearScreen() {
Expand All @@ -241,30 +195,29 @@ void ScreenManager::drawRectangle(int x, int y, int widthOfRectangle, int height
}
}

// RENAME THIS. THIS IS NOT A TEXTURE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
template <typename T>
void ScreenManager::loadTexture(T* gameObject, const char* fileName) {
gameObject->sprite = SDL_LoadBMP(fileName);
if (gameObject->sprite == nullptr) {
void ScreenManager::setSurface(T* gameObject, const char* fileName) {
gameObject->surface = SDL_LoadBMP(fileName);
if (gameObject->surface == nullptr) {
printf("SDL_LoadBMP error: %s\n", SDL_GetError());
return;
}
}

template void ScreenManager::loadTexture<GameObject>(GameObject* gameObject, const char* fileName);
template void ScreenManager::loadTexture<MovableGameObject>(MovableGameObject* gameObject, const char* fileName);
template void ScreenManager::loadTexture<Barrel>(Barrel* gameObject, const char* fileName);
template void ScreenManager::loadTexture<Player>(Player* gameObject, const char* fileName);
template void ScreenManager::loadTexture<Gorilla>(Gorilla* gameObject, const char* fileName);
template void ScreenManager::loadTexture<Platform>(Platform* gameObject, const char* fileName);

void ScreenManager::flipTextureHorizontally(SDL_Surface* sprite) {
int width = sprite->w;
int height = sprite->h;
int pitch = sprite->pitch;
int bpp = sprite->format->BytesPerPixel;
template void ScreenManager::setSurface<GameObject>(GameObject* gameObject, const char* fileName);
template void ScreenManager::setSurface<MovableGameObject>(MovableGameObject* gameObject, const char* fileName);
template void ScreenManager::setSurface<Barrel>(Barrel* gameObject, const char* fileName);
template void ScreenManager::setSurface<Player>(Player* gameObject, const char* fileName);
template void ScreenManager::setSurface<Gorilla>(Gorilla* gameObject, const char* fileName);
template void ScreenManager::setSurface<Platform>(Platform* gameObject, const char* fileName);

void ScreenManager::flipSurfaceHorizontally(SDL_Surface* surface) {
int width = surface->w;
int height = surface->h;
int pitch = surface->pitch;
int bpp = surface->format->BytesPerPixel;

auto* pixels = static_cast<unsigned char*>(sprite->pixels);
auto* pixels = static_cast<unsigned char*>(surface->pixels);

for (int y = 0; y < height; y++) {
unsigned char* row = pixels + y * pitch;
Expand Down
Loading

0 comments on commit 9012aa8

Please sign in to comment.