SFML: Platforms disappearing after push_back() - c++

New to C++, I'm trying to make "platforms" spawn every 2 seconds and go upwards. However, the platform disappears when a new Platform is pushed onto the platforms vector.
Platform.h:
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <vector>
class Platform
{
private:
sf::RectangleShape shape;
public:
Platform() = default;
Platform(const Platform& platform);
void updatePos();
sf::RectangleShape getShape() { return shape; }
};
Platform.cpp:
#include "Platform.h"
Platform::Platform(const Platform& platform)
{
shape.setSize(sf::Vector2f(300.f, 40.f));
shape.setPosition(1920.f / 2.f - shape.getSize().x / 2.f, 1080.f);
}
void Platform::updatePos()
{
shape.move(sf::Vector2f(0.f, -5.f));
}
Game.h:
#pragma once
#include "Platform.h"
#include <iostream>
class Game
{
private:
sf::RenderWindow window{ sf::VideoMode(1920, 1080), "Dodge the Spikes", sf::Style::Fullscreen };
std::vector<Platform> platforms;
bool gameOver{};
unsigned int platformTimer{};
public:
Game() = default;
void update();
void draw();
void run();
};
Game.cpp:
#include "Game.h"
void Game::update()
{
if (!gameOver)
{
//spawn platforms
platformTimer++;
if (platformTimer >= 120)
{
platforms.push_back(Platform{});
platformTimer = 0;
}
//update platforms
for (auto i{platforms.begin()}; i < platforms.end();)
{
i->updatePos();
i++;
}
}
}
void Game::draw()
{
for (auto i{ platforms.begin() }; i < platforms.end();)
{
window.draw(i->getShape());
i++;
}
}
void Game::run()
{
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == sf::Event::Closed)
{
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
}
this->update();
window.clear();
this->draw();
window.display();
}
}

Related

Why is my SFML sprite not showing texture?

I'm trying to create a game. I have a class GameObjects that many classes will inherit from. The class GameObject has a draw method that takes the window address to the display. Why does the sprite show up without the texture when displayed like this?
This is the texture, more of a placeholder. I was able to load it in other files directly creating the sprite and drawing it.
Here is my main.cpp
#include <iostream>
#include "Game.h"
int main()
{
//window setup
Game game;
//Initialize game
game.start();
return 0;
}
Here is my Game.h
#pragma once
#include "GameObject.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
class Game
{
private:
//window variables
int w_width;
int w_height;
sf::RenderWindow window;
sf::Event ev;
bool isRunning;
std::vector<GameObject> o_vector;
void initVariables();
void initWindow();
void initObjects();
void drawObjects();
public:
void start();
void run();
void input();
void update();
void render();
};
And Game.cpp
#include "Game.h"
#include "GameObject.h"
void Game::start()
{
this->initVariables();
this->initWindow();
this->initObjects();
this->run();
}
//Initializations
void Game::initVariables()
{
isRunning = true;
//window variables
w_width = 1280;
w_height = 720;
}
void Game::drawObjects()
{
for (int o_count = 0; o_count < o_vector.size(); o_count++)
{
o_vector.at(o_count).draw(window);
}
}
void Game::initWindow()
{
//window creation
window.create(sf::VideoMode(w_width, w_height), "The Dungeon Crawler", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);
}
void Game::initObjects()
{
GameObject baseObject;
o_vector.push_back(baseObject);
}
void Game::run()
{
while (isRunning)
{
input();
update();
render();
}
}
void Game::input()
{
while (window.pollEvent(ev))
{
switch (ev.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch (ev.key.code)
{
case sf::Keyboard::Escape:
window.close();
break;
case sf::Keyboard::A:
break;
}
}
}
}
void Game::update()
{
}
void Game::render()
{
window.clear(sf::Color(0, 0, 128));
drawObjects();
window.display();
}
Here is GameObject.h
#pragma once
#include <vector>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
class GameObject
{
private:
std::string name;
std::vector<int> o_position;
std::vector<int> o_size;
std::string t_path;
sf::Sprite o_sprite;
sf::Texture o_texture;
void initVariables();
public:
//Constructors & Destructors
GameObject();
~GameObject();
//Getters
std::vector<int> getPos();
std::vector<int> getSize();
sf::Sprite getSprite();
//Setters
void setPos(std::vector<int>pos_in);
void setSize(std::vector<int> size_in);
//Graphics
void draw(sf::RenderWindow &displayWindow);
};
And GameObject.cpp
#include "GameObject.h"
GameObject::GameObject()
{
initVariables();
}
GameObject::~GameObject()
{
}
void GameObject::initVariables()
{
o_position = { 0,0 };
o_size = { 64,64 };
t_path = "Resources/EmptySquare.png";
if (!o_texture.loadFromFile(t_path))
{
std::cout << "Failed to load" << std::endl;
return;
}
o_sprite.setTexture(o_texture);
}
std::vector<int> GameObject::getPos()
{
return o_position;
}
std::vector<int> GameObject::getSize()
{
return o_size;
}
sf::Sprite GameObject::getSprite()
{
return o_sprite;
}
void GameObject::setPos(std::vector<int> pos_in)
{
o_position = pos_in;
}
void GameObject::setSize(std::vector<int> size_in)
{
o_size = size_in;
}
void GameObject::draw(sf::RenderWindow &displayWindow)
{
displayWindow.draw(o_sprite);
}
Thank you for any help!
the problem is this line in Game::initObjects()
GameObject baseObject;
o_vector.push_back(baseObject);
you creata a copy of GameObject.
so it will make a copy of o_sprite.
the o_sprite copies the texture pointer.
after this function ends, the texture pointer is no longer valid.
sf::Sprite has the member const Texture* m_texture; and when calling sf::Sprite::setTexture(const Texture& texture) this member m_texture will in fact point to the texture. That texture will be destructed after initObjects() ends, and your element at position 0 will have a o_sprite.m_texture that points to this destructed object. SFML/Sprite.hpp
you could define the correct operator=(GameObject) that implements the correct sprite copy - or in this case simply say:
void Game::initObjects()
{
o_vector.resize(1);
}
resize will call the constructor of GameObject and you will have no copies and therefore dangling pointers.
Another way would be for you to allocate game object on the heap and store a pointer to it in the vector. This way you will avoid the cost of copying.
To make these changes, declare baseObject as private member of Game class and in initObjects method allocate it on the heap.
class Game
{
private:
GameObject *baseObject;
std::vector<GameObject *> o_vector;
};
void Game::initObjects()
{
baseObject = new GameObject;
o_vector.push_back(baseObject);
}
void Game::drawObjects()
{
for (int o_count = 0; o_count < o_vector.size(); o_count++)
{
o_vector.at(o_count)->draw(window); // use -> operator to call draw
}
}
PS: Instead of raw pointer, you can also use C++11 smart pointer.

unhandled exception C++ SFML

im currently making on my 2D game and i have a error that i dont understand, here is screenshot of the rror:
enter image description here
here is code for main:
#include "Game.h"
#include "Player.h"
void Run(RenderWindow &window, Sprite &player, Texture &PlayerTex);
int main(RenderWindow &window, Sprite &player, Texture &PlayerTex) {
Run(window, player, PlayerTex);
return 0;
}
void InitWindow() {
RenderWindow window(VideoMode(1920, 1080), "Discord Game");
}
void Update() {
}
void Render(RenderWindow &window, Sprite &player) {
player.setScale(0.3f, 0.3f);
window.clear(Color::White);
window.draw(player);
window.display();
}
void Run(RenderWindow &window, Sprite &player, Texture &PlayerTex) {
InitWindow();
while (window.isOpen()) {
Event sfEvent;
while (window.pollEvent(sfEvent)) {
if (sfEvent.type == Event::Closed)
window.close();
}
Update();
Render(window, player);
Player P;
P.PlayerMovement(player);
}
}
code for Player.H:
#pragma once
#include "Game.h"
class Player {
public:
Sprite player;
Texture PlayerTex;
void PlayerTexture(Sprite &Player, Texture &PlayerTex);
void PlayerMovement(Sprite &Player);
};
code for Player.cpp:
#include "Game.h"
#include "Player.h"
void Player::PlayerTexture(Sprite& player, Texture& PlayerTex) {
PlayerTex.loadFromFile("textures/Player/PlayerFront.png");
player.setTexture(PlayerTex);
}
void Player::PlayerMovement(Sprite &player) {
if (Keyboard::isKeyPressed(Keyboard::A)) {
player.move(-5.f, 0.f);
}
if (Keyboard::isKeyPressed(Keyboard::D)) {
player.move(5.f, 0.f);
}
if (Keyboard::isKeyPressed(Keyboard::W)) {
player.move(0.f, -5.f);
}
if (Keyboard::isKeyPressed(Keyboard::S)) {
player.move(0.f, 5.f);
}
}
and then i have Game.H just to include stuff and for the future, here is code for Game.h:
#pragma once
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace sf;
class Game {
};
and then empty Game.cpp:
#include "Game.h"

Can't draw sf::RectangleShape s stored in vector (Tetris clone)

I am trying to storesf::RectangleShape's into thestd::vectorand then draw each of them into thesf::RenderWindow.
Single rectangle shape is representing 1x1 tetromino and i would like to store it into the vector each time it reaches the bottom of the window. Then I would like to reset the position of the current tetromino to the default position.
I think I'm not even to able store it correctly. Each time the tetromino reaches the bottom it gives me this error message:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Bellow please find my current code. I just started working on it and already got stuck.
Definitions.h
#pragma once
// Point structure
struct Point
{
int dim_x;
int dim_y;
};
// Field Dimensions
const int fieldRows = 10;
const int fieldColumns = 9;
const int pointSize = 50.f;
// For checkingEdges funntion within the Tetrnomino.h
enum Edge
{
leftEdge,
rightEdge,
noneEdge
};
Game.h
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Definitions.h"
#include "Tetromino.h"
class Game
{
public:
Game();
~Game();
// Game starter
void run();
// Accessors
bool running();
private:
// Updating and rendering the game window
void update();
void render();
// Initialization
void initVariables();
void initWindow();
void initBacgroundMusic();
// Polling
void pollEvents();
// Window logic stuff
sf::RenderWindow* _window;
sf::Event _event;
void drawStack();
// Bacground Music
sf::Music _ost;
// Tetromino + Its logic
Tetromino _T;
sf::Time delayTime = sf::milliseconds(300);
sf::Clock clock;
};
Tetromino.h
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include "Definitions.h"
class Tetromino
{
public:
Tetromino();
~Tetromino();
// Initialization
void initTetromino();
// Tetromonino logic
void moveTetromino();
Edge checkEdges();
// Getters & Setters
sf::RectangleShape getTetromino();
sf::RectangleShape getStackPart(int part);
int getStackSize();
void setTetromino(sf::RectangleShape &t);
private:
// The current tetromino
sf::RectangleShape _tetromino;
std::vector<sf::RectangleShape> _stack;
};
Game.cpp
#include "Game.h"
//-----Consturcotrs and Destructors-----//
Game::Game()
{
//Basic Initialization
_T.initTetromino();
initVariables();
}
Game::~Game()
{
delete _window;
}
//-----Private Functions-----//
void Game::run()
{
update();
render();
}
bool Game::running()
{
return _window->isOpen();
}
void Game::update()
{
sf::Time elapsed = clock.getElapsedTime();
pollEvents();
if (elapsed >= delayTime)
{
_T.moveTetromino();
clock.restart();
}
}
void Game::render()
{
_window->clear(sf::Color::White);
_window->draw(_T.getTetromino());
drawStack();
_window->display();
}
void Game::initVariables()
{
_window = nullptr;
initWindow();
initBacgroundMusic();
}
void Game::initWindow()
{
_window = new sf::RenderWindow(sf::VideoMode(fieldColumns * pointSize, fieldRows * pointSize), "Tetris v0.2", sf::Style::Default);
_window->setVerticalSyncEnabled(true);
_window->setFramerateLimit(60);
}
void Game::initBacgroundMusic()
{
_ost.openFromFile("../QT_SFML_Tetris/Music.ogg");
_ost.play();
_ost.setLoop(true);
_ost.setVolume(50.f);
}
void Game::pollEvents()
{
while (_window->pollEvent(_event))
{
if (_event.type == sf::Event::Closed) {_window->close();}
if (_event.type == sf::Event::KeyPressed)
{
if (_event.key.code == sf::Keyboard::Escape){_window->close();}
if (_event.key.code == sf::Keyboard::Left && _T.checkEdges() != leftEdge)
{
sf::RectangleShape t = _T.getTetromino();
t.setPosition(t.getPosition().x - pointSize, t.getPosition().y);
_T.setTetromino(t);
render();
}
if (_event.key.code == sf::Keyboard::Right && _T.checkEdges() != rightEdge)
{
sf::RectangleShape t = _T.getTetromino();
t.setPosition(t.getPosition().x + pointSize, t.getPosition().y);
_T.setTetromino(t);
render();
}
if (_event.key.code == sf::Keyboard::Down)
{
sf::RectangleShape t = _T.getTetromino();
t.setPosition(t.getPosition().x, t.getPosition().y+ pointSize);
_T.setTetromino(t);
render();
}
}
}
}
**void Game::drawStack()**
{
for (unsigned int i = _T.getStackSize(); i > 0; --i)
{
_window->draw(_T.getStackPart(i));
}
}
main.cpp
#include <Game.h>
int main()
{
Game game;
while (game.running())
{
game.run();
}
return 0;
}
Tetromino.cpp
#include "Tetromino.h"
//-----Consturcotrs and Destructors-----//
Tetromino::Tetromino()
{
}
Tetromino::~Tetromino()
{
}
//-----Public Functions-----//
void Tetromino::initTetromino()
{
_tetromino.setPosition(sf::Vector2f((fieldColumns * pointSize - pointSize) / 2, 0.f));
_tetromino.setSize(sf::Vector2f(pointSize, pointSize));
_tetromino.setFillColor(sf::Color::Red);
}
void Tetromino::moveTetromino()
{
_tetromino.move(0.f, pointSize);
if (_tetromino.getPosition().y > fieldRows * pointSize - pointSize)
{
_stack.push_back(_tetromino);
_tetromino.setPosition(sf::Vector2f((fieldColumns * pointSize - pointSize) / 2, 0.f));
}
}
Edge Tetromino::checkEdges()
{
if (_tetromino.getPosition().x == 0)
{
return leftEdge;
}
else if (_tetromino.getPosition().x == (fieldColumns * pointSize) - pointSize)
{
return rightEdge;
}
else return noneEdge;
}
sf::RectangleShape Tetromino::getTetromino()
{
return _tetromino;
}
sf::RectangleShape Tetromino::getStackPart(int part)
{
return _stack[part];
}
int Tetromino::getStackSize()
{
return _stack.size();
}
void Tetromino::setTetromino(sf::RectangleShape &t)
{
_tetromino = t;
}
I think the main issue could be within this line:
_stack.push_back(_tetromino);
In the drawStack() method you try to iterate backwards.
There is a reverse iterator doing this for you.
you have an off-by one error in your index calculation, which works only with an empty vector (exactly to prevent these errors you should use the iterator!)
You may want to read about iterators in C++ here is a small example.
According to your code it will look like (note that you need also a getStack()-method in Tetromino.hpp, returning the reference of the vector):
void Game::drawStack()
{
for (auto it = _T.getStack().rbegin(); it != _T.getStack().rend(); it++)
{
_window->draw(*it);
}
}
If you want to keep the index, I this is a fix:
void Game::drawStack()
{
for (int i = _T.getStackSize()-1; i >= 0; --i)
{
_window->draw(_T.getStackPart(i));
}
}

variable in main cpp bug in class

I have created a class takeDommage for apply a number or dmg and activate a countdown for create the invincibility frame.
And i us my "int pv; in my class so then i check the "int pv;" he didn't move
What's wrong ? pv is initialized before main and it's working then i do pv-=1; manually Oo
main :
//{ Include
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include<vector>
#include <collision.h>
//}
//{ Constantes
//Constantes ecran
int tailleEcranX = 1280;
int tailleEcranY = 720;
//Constantes perso
int scalePerso = 3;
int tailleSpriteX = 32;
int tailleSpriteY = 48;
int speed(4);
int speedSprinte(10);
int milieuSpriteX = (tailleSpriteX/2)*scalePerso;
int milieuSpriteY = (tailleSpriteY/2)*scalePerso;
int pv = 100;
unsigned int pvMax = 100;
int eSpeed = 1;
//}
//{ Initialisation
//Initiation des dessins
sf::RenderWindow window;
sf::RectangleShape rect;
sf::Texture perso;
sf::Texture ennemis;
sf::Sprite sprite_perso;
sf::Sprite sprite_ennemis;
sf::View view;
sf::RectangleShape rectCol;
sf::RectangleShape pvBar;
sf::RectangleShape pvMaxBar;
enum Dir{Down,Left,Right,Up};
sf::Vector2i anim (1,Down);
#include "gestion_clavier.h"
#include <takeDommage.h>
//}
//{ Main
int main()
{
//{ Positionnement des objets
window.create(sf::VideoMode(tailleEcranX , tailleEcranY), "The Game I");
window.setPosition(sf::Vector2i(500,250));
window.setFramerateLimit(120);
//Fond d'ecran
rect.setFillColor(sf::Color(110,155,255));
rect.setSize(sf::Vector2f(tailleEcranX-10,tailleEcranY-10));
rect.setPosition(5,5);
rect.setOutlineColor(sf::Color(255,255,255));
rect.setOutlineThickness(3);
//rectangle de collision test
rectCol.setFillColor(sf::Color(0,0,200));
rectCol.setSize(sf::Vector2f(50,50));
rectCol.setPosition(400,500);
rectCol.setOutlineColor(sf::Color(255,255,255));
rectCol.setOutlineThickness(1);
//Bar pv
pvBar.setFillColor(sf::Color(20,255,30));
pvBar.setPosition(20,20);
pvMaxBar.setFillColor(sf::Color(0,0,0));
pvMaxBar.setPosition(20,20);
pvMaxBar.setOutlineColor(sf::Color(255,255,255));
pvMaxBar.setOutlineThickness(2);
//Perso
sprite_perso.setTexture(perso);
sprite_perso.setPosition(tailleEcranX/2-milieuSpriteX,tailleEcranY/2-milieuSpriteY);
sprite_perso.setScale(scalePerso,scalePerso);
//Ennemis
sprite_ennemis.setTexture(ennemis);
sprite_ennemis.setPosition(tailleEcranX/2-milieuSpriteX,tailleEcranY/2-milieuSpriteY);
sprite_ennemis.setTextureRect(sf::IntRect(anim.x*tailleSpriteX,anim.y*tailleSpriteY,tailleSpriteX,tailleSpriteY));
sprite_ennemis.setScale(scalePerso,scalePerso);
//Ennemis
sf::RectangleShape enemy;
enemy.setFillColor(sf::Color(200,0,0));
enemy.setSize(sf::Vector2f(50.f, 50.f));
takeDommage obj;
//Clock
sf::Clock time;
//sf::Clock takeDammageClock;
//}
//{Chargement des Sprites
if (!perso.loadFromFile("link/srpite.png",sf::IntRect(0,0,96,192)))
{
std::cout<<"erreur chargement player image"<<std::endl;
}
if (!ennemis.loadFromFile("link/srpite.png",sf::IntRect(288,0,96,192)))
{
std::cout<<"erreur chargement player image"<<std::endl;
}
//}
//{ Game Loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//Stat avec rafrechisement
//Bar pv
pvMaxBar.setSize(sf::Vector2f(4*pvMax,10));
pvBar.setSize(sf::Vector2f(4*pv,10));
//Perso
sprite_perso.setTextureRect(sf::IntRect(anim.x*tailleSpriteX,anim.y*tailleSpriteY,tailleSpriteX,tailleSpriteY));
//Ennemy
std::vector<sf::RectangleShape> enemies;
enemies.push_back(sf::RectangleShape(enemy));
int enemySpawnTimer = 0;
ProcessInput();
//gestion_clavier();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)||sf::Keyboard::isKeyPressed(sf::Keyboard::S)||sf::Keyboard::isKeyPressed(sf::Keyboard::D)||sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
if (time.getElapsedTime().asMilliseconds()>= 50)
{
anim.x++;
if(anim.x*tailleSpriteX >= perso.getSize().x)
anim.x=0;
time.restart();
}
}
//Boucle Pv
if(pv>=pvMax)
{
pv=pvMax;
}
if(pv<=0)
{
pv=0;
}
//ENEMIES
if(enemySpawnTimer < 50)
enemySpawnTimer++;
if (enemySpawnTimer >= 50)
{
enemy.setPosition((rand() % int(window.getSize().x - enemy.getSize().x)), 0.f);
enemies.push_back(sf::RectangleShape(enemy));
enemySpawnTimer = 0;
}
for (size_t i = 0; i < enemies.size(); i++)
{
enemies[i].move(0, eSpeed);
if (enemies[i].getPosition().y > window.getSize().y)
enemies.erase(enemies.begin() + i);
}
//Collision
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
//std::cout<<"collision pp"<<std::endl;
obj.prendreDegat(50 ,pv);
//std::cout<<pv<<std::endl;
}
//Dessinage
window.draw(rect);
window.draw(rectCol);
window.draw(sprite_perso);
window.draw(sprite_ennemis);
window.draw(pvMaxBar);
window.draw(pvBar);
//Boucle dessinage
for (size_t i = 0; i < enemies.size(); i++)
{
window.draw(enemies[i]);
}
window.display();
window.clear();
}
//}
return 0;
}
takeDommage.h :
#ifndef TAKEDOMMAGE_H
#define TAKEDOMMAGE_H
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
class takeDommage
{
public:
takeDommage();
void prendreDegat(int Dommage, int pv);
//virtual ~takeDommage();
protected:
sf::Clock takeDammageClock;
int Dommage;
private:
};
#endif // TAKEDOMMAGE_H
takeDommage.cpp :
#include "takeDommage.h"
takeDommage::takeDommage()
{
}
void takeDommage::prendreDegat(int Dommage, int pv)
{
if(takeDammageClock.getElapsedTime().asSeconds()>3)
{
std::cout << "bite" << std::endl;
pv -= Dommage;
takeDammageClock.restart();
}
}
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
//std::cout<<"collision pp"<<std::endl;
obj.prendreDegat(50 ,pv);
//std::cout<<pv<<std::endl;
}
you are passing the pv by value.
this causes the void takeDommage::prendreDegat(int Dommage, int pv) to make a local copy of of the pv and decrements the value by Dommage.
what you have to do is pass it by reference. in c++ you do that by Type&
so in your case you change your takeDommage to void takeDommage::prendreDegat(int Dommage, int& pv)
since the pv is now passed by reference, any operation you make in takeDommage::prendreDegat will affect the global pv.
Edit: passing the global variables is not considered a good code practice. But im not going to go into details into that since you just starting c++. Try moving your std::cout<<pv<<std::endl from your main and place it into void takeDommage::prendreDegat(int Dommage, int pv) without making changes and see what happens.

sfml draw from another class

I am trying to make my first pong game in c++ with sfml but I have some problems
when I try to call the window.draw() function I think my code will explain the most.
This is my Game.h
#pragma once
#include <SFML/Graphics.hpp>
class Game
{
public:
Game();
void run();
private:
void processEvents();
void update();
void render();
sf::RenderWindow mWindow;
};
My game.cpp
#pragma once
#include "Game.h"
#include "Paddle.h"
Game::Game()
: mWindow(sf::VideoMode(640,480), "Pong")
{
}
void Game::run()
{
while (mWindow.isOpen())
{
processEvents();
update();
render();
}
}
void Game::processEvents()
{
sf::Event event;
while(mWindow.pollEvent(event))
{
if(event.type == sf::Event::Closed)
mWindow.close();
}
}
void Game::render()
{
mWindow.clear();
mWindow.draw(Paddle::player1);
mWindow.display();
}
void Game::update()
{
}
my Paddle.h and paddle.cpp
#pragma once
#include <SFML/Graphics.hpp>
class Paddle
{
public:
Paddle(int width, int height);
sf::RectangleShape player1(sf::Vector2f(int width,int height));
sf::RectangleShape player2(sf::Vector2f(int width,int height));
private:
};
My paddle.h
#include "Paddle.h"
Paddle::Paddle(int width,int height)
{
}
My main.cpp
#include "Game.h"
#include "Paddle.h"
int main()
{
Game game;
Paddle player1(10,60);
Paddle player2(10,60);
game.run();
}
That was all my code.
The problem is that I don't know how to draw the paddles in my Game.cpp
I think I should use some sort of pointers or reference-argument.
When I do it like this:
void Game::render()
{
mWindow.clear();
mWindow.draw(Paddle::player1);
mWindow.display();
}
I get an error. How do I solve this?
You need something like
class Game
{
public:
Game(Paddle & p1, Paddle & p2);
...
private:
...
Paddle & mP1;
Paddle & mP2;
};
Game::Game(Paddle & p1, Paddle & p2)
: mWindow(sf::VideoMode(640,480), "Pong"),
mP1(p1), mP2(p2)
{
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mP1);
mWindow.display();
}
int main()
{
Paddle player1(10,60);
Paddle player2(10,60);
Game game(player1, player1);
game.run();
}