I am coding a terminal based game in c++ using the ncurses library, and I would like the enemy class to be able to access the players location which I can access from within my main function using p->yLoc and p->xLoc, however, I cannot access these variables from within my enemy::move function, as it just returns the error p was not declared in this scope.
Here is my code:
enemy.h:
#ifndef _ENEMY_H_
#define _ENEMY_H_
#include "player.h"
#include <unistd.h>
void Enemy::move(){
int playerY, playerX;
int yMv, xMv;
while (1){
playerY = p->yLoc;
playerX = p->xLoc;
if (playerY > yLoc) {
mvdown();
} else if (playerY < yLoc) {
mvup();
}
if (playerX > xLoc){
mvright();
} else if (playerX < xLoc) {
mvleft();
}
sleep(1);
}
}
player.h
#ifndef _PLAYER_H_
#define _PLAYER_H_
class Player {
public:
Player(WINDOW * win, int y, int x, char c);
void mvup();
void mvdown();
void mvleft();
void mvright();
int getmv();
void display();
int xLoc, yLoc, xMax, yMax;
private:
char character;
WINDOW * curwin;
};
Player::Player(WINDOW * win, int y, int x, char c){
curwin = win;
yLoc = y;
xLoc = x;
getmaxyx(curwin, yMax, xMax);
keypad(curwin, true);
character = c;
}
main.cpp
#include <ncurses.h>
#include "player.h"
#include <string>
#include <ctime>
#include <cstdlib>
#include "enemy.h"
#include <thread>
using namespace std; // I understand this is bad practice
int main(){
// starts ncurses
initscr();
noecho();
cbreak();
int yMax, xMax;
getmaxyx(stdscr, yMax, xMax);
WINDOW * playwin = newwin(20, 50, (yMax/2)-10, 10);
box(playwin, 0, 0);
refresh();
wrefresh(playwin);
if (nodelay (playwin, 1) == ERR) {
// an error has occurred
}
Player * p = new Player(playwin, 1, 1, '#'); // params: (window to display in, starting y, starting x, character to display)
Enemy * e = new Enemy(playwin, 10, 10, 'x'); // same here
thread enemyLoop(&Enemy::move, e); // start the movement for the enemy in a different thread
while(p->getmv() != 'x'){ // leave the game with the key x
// updates the location of the two sprites, and refreshes the window.
p->display();
e->display();
wrefresh(playwin);
}
endwin();
return 0;
}
If Enemy::move is supposed to know about the other players location then you have to pass the player as argument:
void Enemy::move(const Player &p)
e->move(*p);
Then the function can access the players position.
Note: why are you allocating the player and enemy on the heap. there really is no need for that.
Related
I am writing a simple isometric game - simulator of town development. The main classes are Player class and World class. So, the problem is, that when I launching my game, all seems to be going as planned, but when I want to click "X" to close it, at the moment cursor reach place, in which red arrow points, program is being breaked this error.
It is not happening, when Player object is not created, so I think some problem in it.
Player.hpp:
#ifndef PLAYER_HEADER
#define PLAYER_HEADER
#include "Main.hpp"
#include "ToScreenF.hpp"
#include <iostream>
class Player
{
public:
sf::Vector2i MousePos;
sf::Vector2i inCell;
sf::Vector2i cellOffset;
sf::Vector2i cellSelected;
sf::Vector2f cellSelectedInWorldSpace;
sf::Image selectedTileImage;
sf::Texture selectedTileTexture;
sf::Sprite selectedTileSprite;
sf::Image CheatImage;
sf::Color color;
Player(std::string FILE);
~Player();
void Update(sf::RenderWindow* Window, sf::Vector2f TileSize, sf::Vector2f vOrigin, int WorldWidth, int WorldHeight);
};
#endif PLAYER_HEADER
Player.cpp:
#include "Player.hpp"
Player::Player(std::string FILE)
{
selectedTileImage.loadFromFile(FILE);
selectedTileImage.createMaskFromColor(sf::Color::White);
selectedTileTexture.loadFromImage(selectedTileImage);
selectedTileSprite.setTexture(selectedTileTexture);
CheatImage.loadFromFile("tileCheat.png");
}
void Player::Update(sf::RenderWindow* Window, sf::Vector2f TileSize, sf::Vector2f vOrigin, int WorldWidth, int WorldHeight)
{
MousePos = {sf::Mouse::getPosition((*Window))};
inCell = {(int)(MousePos.x / TileSize.x), (int)(MousePos.y / TileSize.y) };
cellOffset = { MousePos.x % (int)TileSize.x, MousePos.y % (int)TileSize.y };
cellSelected = {
(inCell.y - (int)vOrigin.y) + (inCell.x - (int)vOrigin.x),
(inCell.y - (int)vOrigin.y) - (inCell.x - (int)vOrigin.x)
};
color = CheatImage.getPixel(cellOffset.x, cellOffset.y);
if (color == sf::Color::Red) { cellSelected.x += -1; cellSelected.y += 0; };
if (color == sf::Color::Blue) { cellSelected.x += 0; cellSelected.y += -1; };
if (color == sf::Color::Green) { cellSelected.x += 0; cellSelected.y += 1; };
if (color == sf::Color::Yellow) { cellSelected.x += 1; cellSelected.y += 0; };
if (cellSelected.x < 0) cellSelected.x = 0;
if (cellSelected.x > (WorldWidth - 1)) cellSelected.x = 19;
if (cellSelected.y < 0) cellSelected.y = 0;
if (cellSelected.y > (WorldHeight - 1)) cellSelected.y = 19;
cellSelectedInWorldSpace = ToScreen(cellSelected.x, cellSelected.y, TileSize, vOrigin);
selectedTileSprite.setPosition(cellSelectedInWorldSpace);
Window->draw(selectedTileSprite);
std::cout << cellSelected.x << " " << cellSelected.y << std::endl;
}
ToScreenF.cpp:
#include "ToScreenF.hpp"
sf::Vector2f ToScreen(int x, int y, sf::Vector2f TileSize, sf::Vector2f vOrigin)
{
return sf::Vector2f
{
(vOrigin.x * TileSize.x) + (x - y) * (TileSize.x / 2),
(vOrigin.y * TileSize.y) + (x + y) * (TileSize.y / 2)
};
}
TApplication.hpp:
#ifndef TAPPLICATION_HEADER
#define TAPPLICATION_HEADER
#include "Main.hpp"
#include "World.hpp"
#include "Player.hpp"
namespace Application
{
class TApplication
{
protected:
sf::RenderWindow *Window;
World *GameWorld;
Player* _Player;
public:
TApplication();
~TApplication();
void Init();
void Run();
void End();
};
}
TApplication.cpp:
#include "TApplication.hpp"
namespace Application
{
TApplication::TApplication() : Window(nullptr)
{
}
TApplication:: ~TApplication()
{
}
void TApplication::Init()
{
if (Window == nullptr)
Window = new sf::RenderWindow(sf::VideoMode(1200, 800), "Town Builder Simulator");
GameWorld = new World("BasicTile.png", 100, 100);
_Player = new Player("selectedTile.png");
}
void TApplication::Run()
{
sf::Event event;
while (Window->isOpen())
{
while(Window->pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
Window->close();
}
}
Window->clear();
GameWorld->Draw(Window);
_Player->Update(Window, sf::Vector2f(40, 20), sf::Vector2f(10, 10), GameWorld->WorldWidth, GameWorld->WorldHeight);
Window->display();
}
}
void TApplication::End()
{
if (Window != nullptr)
{
delete Window;
delete GameWorld;
Window = nullptr;
}
}
}
**Main.hpp**
#ifndef MAIN_HEADER
#define MAIN_HEADER
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#endif
Thanks in advance all who helped me, I am very grateful!)
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.
Im am trying to make a clean and organized way of making sprites. The problem is that the position of the sprites are being read as zero and they are just being drawn in the top left corner.
Sprite.h
#ifndef Sprite_h
#define Sprite_h
#endif /* Sprite_h */
using namespace std;
bool show = true;
class Sprite{
public:
int Spritex;
int Spritey;
string name;
sf::Texture texture;
Sprite(string image, int x, int y){
x = Spritex;
y = Spritey;
texture.loadFromFile(image);
}
sf::Sprite getSprite() {
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(Spritex, Spritey);
return sprite;
}
void changeimage(string image);
};
void Sprite:: changeimage(string image){
texture.loadFromFile(image);
}
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Character.h"
#include "Projectile.h"
#include "Sprite.h"
//Use std
using namespace std;
//Boolean to determine if screen will scroll
bool scroll = false;
//player that is part of Character class and computer that is part of Sprite class
Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/Player.png");
Sprite computer("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/CompSprite.png", 1200, 100);
Sprite battery("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery4.png", 0, 0);
//boolean for whether to show weapon or not
bool showweapon;
//main loop
int main() {
int windowWidth = 5000;//width of window
int windowHeight = 5000;//height of window
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );//Make the window
//Setting up the dungeon back-round
sf::Texture dungeon;
dungeon.loadFromFile("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/DungeonBack.png");
sf::Sprite backround;
backround.setTexture(dungeon);
while (window.isOpen()){
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event)){
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
//Movement
if (moveChar == true){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
player.left();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
player.right();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
player.forward();
}
if (sf:: Keyboard::isKeyPressed(sf::Keyboard::Down)){
player.backward();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{
player.Highspeed();
}
else{
player.Lowspeed();
}
}
//If player intersects with comp sprite, pick up comp sprite
if (player.getSprite().getGlobalBounds().intersects(computer.getSprite().getGlobalBounds())){
show = false;
player.hascomp = true;
}
//draw and window stuff
window.clear(sf::Color(255, 255, 255));
window.draw(backround);
if (show == true){
window.draw(computer.getSprite());
}
if (show == false){
window.draw(battery.getSprite());
}
window.draw(player.getSprite());
window.display();
window.setFramerateLimit(70);
}
}
If you have a question I will do my best to answer. Everything works except the spritex and spritey are being read as 0 for some reason. Thanks for any help.
You are writing to variables x and y here, which you never read from:
Sprite(string image, int x, int y){
x = Spritex;
y = Spritey;
texture.loadFromFile(image);
}
I assume you flipped the assignments, and should write
Sprite(string image, int x, int y){
Spritex = x;
Spritey = y;
}
or
Sprite(string image, int x, int y) : Spritex(x), Spritey(y) {
texture.loadFromFile(image);
}
If you turn up your warning levels, you will be warned about things like this, and also the non-initialized member you still have (name)
I'm trying to make a library to simplify the ncurses use to display colors. I'm doing it object-oriented so it'll easy to handle changes in the future. But the problem is that I can't get to work this code.
#include <ncurses.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <iostream>
using namespace std;
class ColorWindow {
private:
bool canColor;
WINDOW* container;
int height, width, startx, starty;
public:
ColorWindow() {
if(has_colors()) {
canColor=true;
}
this->height = 20;
this->width = 84;
this->starty = (LINES - height) / 2; /* Calculating for a center placement */
this->startx = (COLS - width) / 2;
}
bool writeStringWithColor(int x, int y, const char* message) {
if(!canColor) {
writeString(3, 5, "Sorry, your term can't show colors.");
return false;
}
init_pair(1, COLOR_RED, COLOR_BLACK);
writeString(0, 10, "aaaaaaaaa");
wattron(getContainer(), COLOR_PAIR(1));
writeString(x, y, message);
wattroff(getContainer(), COLOR_PAIR(1));
}
void writeString(int x, int y, const char* message) {
mvwprintw(getContainer(), x, y, message);
}
WINDOW* createNewContainer() {
this->container = newwin(height, width, starty, startx);
wrefresh(this->container); /* Show that box */
return getContainer();
}
WINDOW* getContainer() {
return this->container;
}
void refreshContainer() {
refresh();
wrefresh(this->container); /* Show that box */
}
};
int main() {
ColorWindow cw = ColorWindow();
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everything to me */
keypad(stdscr, TRUE);
start_color();
cw.createNewContainer();
bool success = cw.writeStringWithColor(0, 10, "Hello everyone in color!!");
if(!success)
cw.writeString(0, 10, "Write with color failed :(");
cw.refreshContainer();
sleep(2);
endwin();
return 0;
}
Thanks in Advance.
There are a couple of bugs in your code:
You don't initialize canColor and container, so copying the fields into cw in main has undefined behavior. Fixed by:
ColorWindow() : canColor(false), container(nullptr) {
writeStringWithColor is missing a return statement at the end, also leading to undefined behavior in main. Fixed by:
return true;
at the end of writeStringWithColor.
Your x and y arguments are swapped in the call to mvwprintw in writeString. Fixed by:
mvwprintw(getContainer(), y, x, message);
LINES and COLS are only valid after ncurses is initialized, so your starty and startx values are garbage. Fixed by moving the initialization of cw after the ncurses init code in main:
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everything to me */
keypad(stdscr, TRUE);
start_color();
ColorWindow cw = ColorWindow();
Full program:
#include <ncurses.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <iostream>
using namespace std;
class ColorWindow {
private:
bool canColor;
WINDOW* container;
int height, width, startx, starty;
public:
ColorWindow() : canColor(false), container(nullptr) {
if(has_colors()) {
canColor=true;
}
this->height = 20;
this->width = 84;
this->starty = (LINES - height) / 2; /* Calculating for a center placement */
this->startx = (COLS - width) / 2;
}
bool writeStringWithColor(int x, int y, const char* message) {
if(!canColor) {
writeString(3, 5, "Sorry, your term can't show colors.");
return false;
}
init_pair(1, COLOR_RED, COLOR_BLACK);
writeString(0, 10, "aaaaaaaaa");
wattron(getContainer(), COLOR_PAIR(1));
writeString(x, y, message);
wattroff(getContainer(), COLOR_PAIR(1));
return true;
}
void writeString(int x, int y, const char* message) {
mvwprintw(getContainer(), y, x, message);
}
WINDOW* createNewContainer() {
this->container = newwin(height, width, starty, startx);
wrefresh(this->container); /* Show that box */
return getContainer();
}
WINDOW* getContainer() {
return this->container;
}
void refreshContainer() {
refresh();
wrefresh(this->container); /* Show that box */
}
};
int main() {
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everything to me */
keypad(stdscr, TRUE);
start_color();
ColorWindow cw = ColorWindow();
cw.createNewContainer();
bool success = cw.writeStringWithColor(0, 10, "Hello everyone in color!!");
if(!success)
cw.writeString(0, 10, "Write with color failed :(");
cw.refreshContainer();
sleep(2);
endwin();
return 0;
}
Hello I add Text Class to my project , where text is fallowing the moving ball
i build is successful, but when i trying debuging i receive
Unhandled exception at 0x0F58155F (sfml-graphics-d-2.dll) in Lotto.exe: 0xC0000005: Access violation reading location 0xCCCCCD24.
My main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Ball.h"
#include "Line.h"
#include "TextBall.h"
using namespace std;
using namespace sf;
int main()
{
RenderWindow win(VideoMode(800, 600), L"RozdziaĆ 1");
Clock stoper;
Font font;
font.loadFromFile("arial.ttf");
float ySpeed = -100;
float xSpeed = -100;
Ball CircleOne(win);
Line linia1(win);
linia1.sie(500, 500);
linia1.position(20, 20);
linia1.thiness(2);
TextBall text(win);
text.dFont(font);
text.dString("Hello");
text.dCharacterSize(40);
text.dColor(sf::Color::Black);
text.dPosition(CircleOne.GetPosition().x + 5, CircleOne.GetPosition().y);
CircleOne.SetPozition(100, 100);
// CircleOne.SetOutlineColor(Color::Red);
CircleOne.Fil(Color::Yellow);
CircleOne.Promien(15);
// CircleOne.thinesS();
while (win.isOpen())
{
win.clear(Color::White);
Event e;
while (win.pollEvent(e))
{
if (e.type == Event::Closed)
win.close();
}
auto dt = stoper.restart().asSeconds();
CircleOne.Move(xSpeed *dt , ySpeed * dt);
//text.Move(xSpeed *dt, ySpeed * dt);
linia1.draw();
CircleOne.Draw();
text.Draw();
int positionY = CircleOne.GetPosition().y;
if (positionY <= 0){ ySpeed = -ySpeed; }
if (positionY >= 600.0-2*CircleOne.radius()){ ySpeed = -ySpeed; }
int positionX = CircleOne.GetPosition().x;
if (positionX <= 0){ xSpeed = -xSpeed; }
if (positionX >= 800.0-2*CircleOne.radius()){xSpeed = -xSpeed; }
win.display();
}
}
and TextBall class
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
using namespace std;
using namespace sf;
class TextBall{
public:
TextBall(RenderWindow&);
void dString(String);
void dCharacterSize(int);
void dColor(Color);
void dPosition(float, float);
void Move(float, float);
void Draw();
void dFont(Font);
private:
Text text;
RenderWindow& Render;
};
TextBall::TextBall(sf::RenderWindow& app) : Render(app)
{
}
void TextBall::dString(String liczba)
{
text.setString(liczba);
}
void TextBall::dCharacterSize(int size)
{
text.setCharacterSize(size);
}
void TextBall::dColor(Color kolor)
{
text.setColor(kolor);
}
void TextBall::dPosition(float x, float y)
{
text.setPosition(x, y);
}
void TextBall::Move(float a, float b)
{
text.move(a, b);
}
void TextBall::Draw()
{
Render.draw(text);
}
void TextBall::dFont(Font fon)
{
text.setFont(fon);
}
When i add text without a additional class its working fine, i afraid it object sending in TextBall is faulty. Plaese how can i solve this project