Refactor of structs

Refactored Size and Position
Placed them in different .h files
This commit is contained in:
2024-12-04 14:47:50 +01:00
parent 7ecb90af1f
commit 72e2116dc7
9 changed files with 35 additions and 28 deletions

View File

@@ -23,7 +23,9 @@ add_executable(LotoStatek main.cpp
headers/Projectile.h headers/Projectile.h
sources/Projectile.cpp sources/Projectile.cpp
headers/Rocket.h headers/Rocket.h
sources/Rocket.cpp) sources/Rocket.cpp
headers/Size.h
headers/Position.h)
if(WIN32) if(WIN32)
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/lib/SFML") set(SFML_ROOT "${CMAKE_SOURCE_DIR}/lib/SFML")

View File

@@ -4,11 +4,8 @@
#include "SFML/Graphics/Sprite.hpp" #include "SFML/Graphics/Sprite.hpp"
#include "SFML/Graphics/Texture.hpp" #include "SFML/Graphics/Texture.hpp"
#include "Bullet.h" #include "Bullet.h"
#include "Position.h"
struct Position {
int x;
int y;
};
class Actor { class Actor {
public: public:

View File

@@ -3,13 +3,9 @@
#include "SFML/Graphics/Texture.hpp" #include "SFML/Graphics/Texture.hpp"
#include "SFML/Graphics/Sprite.hpp" #include "SFML/Graphics/Sprite.hpp"
#include "Position.h"
class Meteor { class Meteor {
struct Position {
int x;
int y;
};
public: public:
Meteor(float x, float y, sf::Texture &texture); Meteor(float x, float y, sf::Texture &texture);
sf::Sprite &getSprite(); sf::Sprite &getSprite();

View File

@@ -1,28 +1,24 @@
#ifndef PLANSZA_H #ifndef PLANSZA_H
#define PLANSZA_H #define PLANSZA_H
#include "Meteor.h" #include "Meteor.h"
#include "RandomNumberGenerator.h" #include "RandomNumberGenerator.h"
#include "SFML/System/Clock.hpp" #include "SFML/System/Clock.hpp"
#include "Size.h"
class Plansza { class Plansza {
struct Size {
int height;
int width;
};
public: public:
Plansza(unsigned int windowHeight, unsigned int windowWidth); Plansza(unsigned int windowHeight, unsigned int windowWidth);
void spawn_meteor();
Size getSize(); Size getSize();
std::vector<Meteor> &getMeteors(); std::vector<Meteor> &getMeteors();
void spawn_meteor();
void update_meteors(); void update_meteors();
private: private:
std::vector<Meteor> meteors;
Size size; Size size;
sf::Texture meteorTexture1; sf::Texture meteorTexture1;
sf::Texture meteorTexture2; sf::Texture meteorTexture2;
sf::Clock spawnClock; sf::Clock spawnClock;
std::vector<Meteor> meteors;
}; };
#endif //PLANSZA_H #endif //PLANSZA_H

9
headers/Position.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef LOTOSTATEK_POSITION_H
#define LOTOSTATEK_POSITION_H
struct Position {
int x;
int y;
};
#endif //LOTOSTATEK_POSITION_H

View File

@@ -2,13 +2,9 @@
#define PROJECTILE_H #define PROJECTILE_H
#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Sprite.hpp>
#include "Position.h"
class Projectile { class Projectile {
struct Position {
int x;
int y;
};
public: public:
Projectile(float x, float y, sf::Texture &texture); Projectile(float x, float y, sf::Texture &texture);
sf::Sprite &getSprite(); sf::Sprite &getSprite();
@@ -18,7 +14,7 @@ public:
protected: protected:
static sf::Texture texture; static sf::Texture texture;
sf::Sprite sprite; sf::Sprite sprite;
Position position{}; Position position;
float speed; float speed;
bool outOfBounds; bool outOfBounds;
~Projectile() = default; ~Projectile() = default;

9
headers/Size.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef LOTOSTATEK_SIZE_H
#define LOTOSTATEK_SIZE_H
struct Size {
int height;
int width;
};
#endif //LOTOSTATEK_SIZE_H

View File

@@ -14,8 +14,10 @@ int main()
sf::RenderWindow mainWindow(sf::VideoMode(600, 800), "LotoStatek"); sf::RenderWindow mainWindow(sf::VideoMode(600, 800), "LotoStatek");
mainWindow.setVerticalSyncEnabled(true); mainWindow.setVerticalSyncEnabled(true);
mainWindow.setFramerateLimit(60); mainWindow.setFramerateLimit(60);
sf::Image icon; sf::Image icon;
icon.loadFromFile("../assets/img/icon/ikonka.png"); icon.loadFromFile("../assets/img/icon/ikonka.png");
mainWindow.setIcon(128, 128, icon.getPixelsPtr()); mainWindow.setIcon(128, 128, icon.getPixelsPtr());
Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x); Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x);
@@ -40,10 +42,6 @@ int main()
while (mainWindow.isOpen()) { while (mainWindow.isOpen()) {
mainWindow.clear(); mainWindow.clear();
// tło
background.update();
background.draw(mainWindow);
// Tu są handlowane eventy // Tu są handlowane eventy
sf::Event event{}; sf::Event event{};
while (mainWindow.pollEvent(event)) { while (mainWindow.pollEvent(event)) {
@@ -51,6 +49,10 @@ int main()
mainWindow.close(); mainWindow.close();
} }
// tło
background.update();
background.draw(mainWindow);
// poruszanie się statkiem // poruszanie się statkiem
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
if(ship.getPosition().x > 50) { if(ship.getPosition().x > 50) {

View File

@@ -23,7 +23,7 @@ void Plansza::spawn_meteor() {
} }
} }
Plansza::Size Plansza::getSize() { Size Plansza::getSize() {
return size; return size;
} }