Hello, im new using SFML. I'm doing a proyect for my carreer, the proyect consist in a 2D game, where the user plays as a spaceship, and you have to destroy or avoid asteroids.
The program compiles and execute normally, but it has a problem, and is that the asteroids does not draw in the screen, and I don`t now how to fix it.
For the asteroids I use 2 classes, Entidades(entity) and entidadManager(EntityManager).
///ENTIDAD
#ifndef ENTIDAD_H
#define ENTIDAD_H
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
using namespace std;
using namespace sf;
class entidad {
public:
entidad();
void actualizar();
void dibujar (RenderWindow &w);
Vector2f verposi();
private:
Vector2f m_pos;
Sprite m_sprite;
};
#endif
#include "entidad.h"
#include <SFML/System/Vector2.hpp>
#include <SFML/System/Clock.hpp>
#include <vector>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <cstdlib>
using namespace std;
entidad::entidad() {
Texture tex;
Sprite sp;
tex.loadFromFile("meteorito.png");
sp.setTexture(tex);
m_sprite = sp;
m_sprite.setScale(0.70f,0.70f);
m_pos.x = rand () % 800 + 30;;
m_pos.y = 0;
m_sprite.setPosition(m_pos.x,m_pos.y);
}
void entidad::actualizar(){
m_sprite.move(0,-5);
}
void entidad::dibujar(RenderWindow &w){
w.draw(m_sprite);
}
Vector2f entidad::verposi(){
Vector2f charpos = m_sprite.getPosition();
return charpos;
}
///ENTIDAD
///ENTIDADMANAGER
#ifndef ENTIDADMANAGER_H
#define ENTIDADMANAGER_H
#include <vector>
#include "entidad.h"
#include <SFML/Graphics/RenderWindow.hpp>
using namespace std;
class entidadmanager {
public:
entidadmanager();
void actualizar();
void dibujar(RenderWindow &w);
private:
vector<entidad> m_v;
};
#endif
#include "entidadmanager.h"
#include "entidad.h"
#include <vector>
#include <iterator>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
using namespace std;
entidadmanager::entidadmanager() {
vector<entidad> v;
m_v = v;
}
void entidadmanager::actualizar(){
Clock reloj;
if(reloj.getElapsedTime().asMilliseconds() == 1000){
entidad aux;
m_v.push_back(aux);
reloj.restart();
}
for(size_t i=0;i<m_v.size();i++) {
m_v[i].actualizar();
Vector2f charaux;
charaux = m_v[i].verposi();
if(charaux.y > 900){
auto it = next(m_v.begin(),i);
m_v.erase(it);
}
}
}
void entidadmanager::dibujar(RenderWindow &w){
for(size_t i=0;i<m_v.size();i++) {
entidad aux;
aux = m_v[i];
aux.dibujar(w);
}
}
///ENTIDADMANAGER
///PARTIDA
Partida::Partida() {
nave n1;
entidadmanager met;
}
bool fuera(disparo &d){
Vector2f p = d.verPosicion();
if(p.x<0 or p.x>1600) return true;
if(p.y<0 or p.y>900) return true;
return false;
}
void Partida::actualizar(juego &j) {
n1.actualizar();
met.actualizar();
if(n1.dispara()) vd.push_back(n1.dispGen());
for(disparo &d : vd) d.actualizar();
auto it2 = remove_if(vd.begin(),vd.end(),fuera);
vd.erase(it2,vd.end());
}
void Partida::dibujar(RenderWindow &w, Sprite fondo) {
w.clear(Color(220,220,180,255));
w.draw(fondo);
n1.dibujar(w);
met.dibujar(w);
for(disparo &d : vd) d.dibujar(w);
}
///PARTIDA
Sorry if the post it`s a bit long (a bit too long haha) but it`s my first one.
Any help is appreciated