aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraleksav013 <aleksav013@gmail.com>2021-06-07 16:51:43 +0200
committeraleksav013 <aleksav013@gmail.com>2021-06-07 16:51:43 +0200
commitc01f67b284db9c987680d121c11df2d9da56e2d0 (patch)
tree30ac2b4959b98844f0be11283ea63ffec4c30834
parent2258ca5c7e6166d33c34ce647e656bc335a6acb9 (diff)
Smart pointers
-rw-r--r--src/Game.cpp18
-rw-r--r--src/State.cpp32
-rw-r--r--src/includes/Game.hpp9
-rw-r--r--src/includes/State.hpp8
4 files changed, 28 insertions, 39 deletions
diff --git a/src/Game.cpp b/src/Game.cpp
index c0bad0e..359be55 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -53,13 +53,13 @@ void Game::pwptex()
for(size_t i=0;i<pow.size();i++) switch(pow.at(i).type)
{
case 1:
- pow.at(i).telo.setTexture(tex["health"]);
+ pow.at(i).telo.setTexture(tex["health"].get());
break;
case 2:
- pow.at(i).telo.setTexture(tex["clear"]);
+ pow.at(i).telo.setTexture(tex["clear"].get());
break;
case 3:
- pow.at(i).telo.setTexture(tex["vampiric"]);
+ pow.at(i).telo.setTexture(tex["vampiric"].get());
break;
default:
break;
@@ -67,9 +67,9 @@ void Game::pwptex()
}
void Game::inittex()
{
- for(size_t i=0;i<nep1.size();i++) nep1.at(i).telo.setTexture(tex["neprijatelj"]);
- for(size_t i=0;i<nep2.size();i++) nep2.at(i).telo.setTexture(tex["neprijatelj"]);
- for(size_t i=0;i<nep3.size();i++) nep3.at(i).telo.setTexture(tex["neprijatelj"]);
+ for(size_t i=0;i<nep1.size();i++) nep1.at(i).telo.setTexture(tex["neprijatelj"].get());
+ for(size_t i=0;i<nep2.size();i++) nep2.at(i).telo.setTexture(tex["neprijatelj"].get());
+ for(size_t i=0;i<nep3.size();i++) nep3.at(i).telo.setTexture(tex["neprijatelj"].get());
pwptex();
}
void Game::initent()
@@ -89,9 +89,9 @@ void Game::updatewin()
fps.setPosition(sirina*5.0/6,0);
score.setPosition(sirina*5.0/6,50);
}
-Game::Game(sf::RenderWindow *glprozor,std::map<std::string,sf::Font*> mainfont,std::map<std::string,sf::Texture*> maintex)
+Game::Game(std::shared_ptr<sf::RenderWindow> mainprozor,std::map<std::string,std::shared_ptr<sf::Font>> mainfont,std::map<std::string,std::shared_ptr<sf::Texture>> maintex)
{
- prozor=glprozor;
+ prozor=mainprozor;
font=mainfont;
tex=maintex;
@@ -111,7 +111,7 @@ bool Game::gameover()
void Game::updatedt()
{
dt=sat.restart().asMicroseconds()/1000000.0;
- if(dt>1) dt=0;
+ if(dt>0.5) dt=0;
}
void Game::updateui()
{
diff --git a/src/State.cpp b/src/State.cpp
index 9e63762..9e20035 100644
--- a/src/State.cpp
+++ b/src/State.cpp
@@ -11,19 +11,19 @@ State::State()
}
void State::initwin()
{
- prozor=new sf::RenderWindow(sf::VideoMode::getFullscreenModes()[0],"RPG igra");
- prozor->setFramerateLimit(60);
+ prozor=std::make_shared<sf::RenderWindow>(sf::VideoMode::getFullscreenModes()[0],"RPG igra",sf::Style::Fullscreen);
+ prozor->setVerticalSyncEnabled(true);
visina=prozor->getSize().y;
sirina=prozor->getSize().x;
}
void State::initassets()
{
- font["default"] = new sf::Font;
- tex["neprijatelj"] = new sf::Texture;
- tex["health"] = new sf::Texture;
- tex["clear"] = new sf::Texture;
- tex["vampiric"] = new sf::Texture;
- //tex["djule"] = new sf::Texture;
+ font["default"]=std::make_shared<sf::Font>();
+ tex["neprijatelj"]=std::make_shared<sf::Texture>();
+ tex["health"]=std::make_shared<sf::Texture>();
+ tex["clear"]=std::make_shared<sf::Texture>();
+ tex["vampiric"]=std::make_shared<sf::Texture>();
+ //tex["djule"]=std::make_shared<sf::Texture>();
font["default"]->loadFromFile("assets/fonts/LiberationMono-Regular.ttf");
tex["health"]->loadFromFile("assets/images/healing.png");
tex["neprijatelj"]->loadFromFile("assets/images/nep.png");
@@ -108,7 +108,7 @@ void State::updateui()
}
void State::loop()
{
- Game *igra=new Game(prozor,font,tex);
+ std::unique_ptr<Game> igra=std::make_unique<Game>(prozor,font,tex);
while(prozor->isOpen())
{
events();
@@ -123,8 +123,7 @@ void State::loop()
prozor->draw(krajtext);
if(newgame)
{
- delete igra;
- igra=new Game(prozor,font,tex);
+ igra=std::make_unique<Game>(prozor,font,tex);
}
}
if(pause)
@@ -138,15 +137,4 @@ void State::loop()
ischanged=0;
newgame=0;
}
- delete igra;
-}
-State::~State()
-{
- delete prozor;
- delete font["default"];
- delete tex["neprijatelj"];
- delete tex["health"];
- delete tex["clear"];
- delete tex["vampiric"];
- //delete tex["djule"];
}
diff --git a/src/includes/Game.hpp b/src/includes/Game.hpp
index d893fd8..529b13a 100644
--- a/src/includes/Game.hpp
+++ b/src/includes/Game.hpp
@@ -7,11 +7,12 @@
#include"Enemy3.hpp"
#include"Powerup.hpp"
+#include<memory>
class Game
{
private:
- std::map<std::string,sf::Font*> font;
- std::map<std::string,sf::Texture*> tex;
+ std::map<std::string,std::shared_ptr<sf::Font>> font;
+ std::map<std::string,std::shared_ptr<sf::Texture>> tex;
float dt;
sf::Clock sat,time;
@@ -22,7 +23,7 @@ class Game
std::vector<Powerup> pow;
int visina,sirina;
- sf::RenderWindow *prozor;
+ std::shared_ptr<sf::RenderWindow> prozor;
sf::RectangleShape health,healthblank,stomp,stompblank,vampiric,vampiricblank;
sf::Text healthtext,stomptext,fps,score,vampirictext;
@@ -44,7 +45,7 @@ class Game
void initent();
void updatewin();
public:
- Game(sf::RenderWindow *glprozor,std::map<std::string,sf::Font*> mainfont,std::map<std::string,sf::Texture*> maintex);
+ Game(std::shared_ptr<sf::RenderWindow> mainprozor,std::map<std::string,std::shared_ptr<sf::Font>> mainfont,std::map<std::string,std::shared_ptr<sf::Texture>> maintex);
void loop(bool ischanged,bool pause);
void draw();
bool gameover();
diff --git a/src/includes/State.hpp b/src/includes/State.hpp
index c9dffb9..5623bbf 100644
--- a/src/includes/State.hpp
+++ b/src/includes/State.hpp
@@ -2,15 +2,16 @@
#define STATE_H
#include<SFML/Graphics.hpp>
+#include<memory>
class State
{
private:
- sf::RenderWindow *prozor;
+ std::shared_ptr<sf::RenderWindow> prozor;
int visina,sirina;
bool ischanged=0,newgame=0,pause=0,kraj=0;
- std::map<std::string,sf::Font*> font;
- std::map<std::string,sf::Texture*> tex;
+ std::map<std::string,std::shared_ptr<sf::Font>> font;
+ std::map<std::string,std::shared_ptr<sf::Texture>> tex;
sf::RectangleShape podloga;
sf::Text pausetext,krajtext;
@@ -23,7 +24,6 @@ class State
void keyboard();
public:
State();
- ~State();
void loop();
};