From 72e2116dc72d3febf56e73b9e1bc2827f82c5e47 Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Wed, 4 Dec 2024 14:47:50 +0100 Subject: [PATCH] Refactor of structs Refactored Size and Position Placed them in different .h files --- CMakeLists.txt | 4 +++- headers/Actor.h | 5 +---- headers/Meteor.h | 6 +----- headers/Plansza.h | 10 +++------- headers/Position.h | 9 +++++++++ headers/Projectile.h | 8 ++------ headers/Size.h | 9 +++++++++ main.cpp | 10 ++++++---- sources/Plansza.cpp | 2 +- 9 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 headers/Position.h create mode 100644 headers/Size.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 97ef1c6..99ca51a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,9 @@ add_executable(LotoStatek main.cpp headers/Projectile.h sources/Projectile.cpp headers/Rocket.h - sources/Rocket.cpp) + sources/Rocket.cpp + headers/Size.h + headers/Position.h) if(WIN32) set(SFML_ROOT "${CMAKE_SOURCE_DIR}/lib/SFML") diff --git a/headers/Actor.h b/headers/Actor.h index 56b704a..7fc1ec6 100644 --- a/headers/Actor.h +++ b/headers/Actor.h @@ -4,11 +4,8 @@ #include "SFML/Graphics/Sprite.hpp" #include "SFML/Graphics/Texture.hpp" #include "Bullet.h" +#include "Position.h" -struct Position { - int x; - int y; -}; class Actor { public: diff --git a/headers/Meteor.h b/headers/Meteor.h index 8b42356..e283b82 100644 --- a/headers/Meteor.h +++ b/headers/Meteor.h @@ -3,13 +3,9 @@ #include "SFML/Graphics/Texture.hpp" #include "SFML/Graphics/Sprite.hpp" +#include "Position.h" class Meteor { - struct Position { - int x; - int y; - }; - public: Meteor(float x, float y, sf::Texture &texture); sf::Sprite &getSprite(); diff --git a/headers/Plansza.h b/headers/Plansza.h index 3aae063..ceee778 100644 --- a/headers/Plansza.h +++ b/headers/Plansza.h @@ -1,28 +1,24 @@ #ifndef PLANSZA_H #define PLANSZA_H - #include "Meteor.h" #include "RandomNumberGenerator.h" #include "SFML/System/Clock.hpp" +#include "Size.h" class Plansza { - struct Size { - int height; - int width; - }; public: Plansza(unsigned int windowHeight, unsigned int windowWidth); - void spawn_meteor(); Size getSize(); std::vector &getMeteors(); + void spawn_meteor(); void update_meteors(); private: - std::vector meteors; Size size; sf::Texture meteorTexture1; sf::Texture meteorTexture2; sf::Clock spawnClock; + std::vector meteors; }; #endif //PLANSZA_H diff --git a/headers/Position.h b/headers/Position.h new file mode 100644 index 0000000..6f69670 --- /dev/null +++ b/headers/Position.h @@ -0,0 +1,9 @@ +#ifndef LOTOSTATEK_POSITION_H +#define LOTOSTATEK_POSITION_H + +struct Position { + int x; + int y; +}; + +#endif //LOTOSTATEK_POSITION_H diff --git a/headers/Projectile.h b/headers/Projectile.h index c50a518..74f4b0f 100644 --- a/headers/Projectile.h +++ b/headers/Projectile.h @@ -2,13 +2,9 @@ #define PROJECTILE_H #include +#include "Position.h" class Projectile { - struct Position { - int x; - int y; - }; - public: Projectile(float x, float y, sf::Texture &texture); sf::Sprite &getSprite(); @@ -18,7 +14,7 @@ public: protected: static sf::Texture texture; sf::Sprite sprite; - Position position{}; + Position position; float speed; bool outOfBounds; ~Projectile() = default; diff --git a/headers/Size.h b/headers/Size.h new file mode 100644 index 0000000..2b8842c --- /dev/null +++ b/headers/Size.h @@ -0,0 +1,9 @@ +#ifndef LOTOSTATEK_SIZE_H +#define LOTOSTATEK_SIZE_H + +struct Size { + int height; + int width; +}; + +#endif //LOTOSTATEK_SIZE_H diff --git a/main.cpp b/main.cpp index 26b98aa..df9ff0b 100644 --- a/main.cpp +++ b/main.cpp @@ -14,8 +14,10 @@ int main() sf::RenderWindow mainWindow(sf::VideoMode(600, 800), "LotoStatek"); mainWindow.setVerticalSyncEnabled(true); mainWindow.setFramerateLimit(60); + sf::Image icon; icon.loadFromFile("../assets/img/icon/ikonka.png"); + mainWindow.setIcon(128, 128, icon.getPixelsPtr()); Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x); @@ -40,10 +42,6 @@ int main() while (mainWindow.isOpen()) { mainWindow.clear(); - // tło - background.update(); - background.draw(mainWindow); - // Tu są handlowane eventy sf::Event event{}; while (mainWindow.pollEvent(event)) { @@ -51,6 +49,10 @@ int main() mainWindow.close(); } + // tło + background.update(); + background.draw(mainWindow); + // poruszanie się statkiem if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { if(ship.getPosition().x > 50) { diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 822c62f..5fd8e37 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -23,7 +23,7 @@ void Plansza::spawn_meteor() { } } -Plansza::Size Plansza::getSize() { +Size Plansza::getSize() { return size; }