Setting up the card position on the window screen with class function - c++

I don't want to hardcode the position for the card to be in the middle of the screen and we did a project like this without class. So though would be easy to just put what I did to make the card to be in the center but no matter what I did, the card stays at the top left corner.
I even notice at times if I put the rectSize or something the rectangle proportions changes and look like a square when maximizing the screen.
What am I doing wrong?
This is my background cpp file:
#include "background.h"
background::background() : background(450, 750)
{
}
background::background(float x, float y)
{
sf::RenderWindow window;
sf::RectangleShape rectangle;
sf::RectangleShape::setSize({x, y});
// sf::Vector2f center;
//
// sf::RectangleShape::setPosition({});
}
void setPostioning (sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y)
{
sf::Vector2f rectSize ={x,y};
rectangle.setSize(rectSize);
sf::Vector2f center;
rectangle.setPosition({
center.x = window.getSize().x/2 - rectSize.x/2,
center.y = window.getSize().y/2 - rectSize.y/2
});
}
This is my header file of what I have done:
#include <SFML/Graphics.hpp>
class background : public sf::RectangleShape
{
public:
background();
background(float x, float y);
void setPostioning(sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y);
};
And now this is my main main file
int main()
{
//set up of the window
sf::VideoMode videoMode(1280,1024,32);
sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
window.setFramerateLimit(15);//frame rate
background b;
rank r;
Card Joker;
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
//when window is running they can close it with the close button
if (event.type == sf::Event::Closed)
{
window.close();
}
//this if statement will make our card stay in the same ratio no matter what
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(),
sf::Vector2f((float) event.size.width, (float) event.size.height)));
}
}
//invoking and set up to be drawn and display on the window when running
window.clear(sf::Color::Black);
window.draw(Joker);
window.draw(r);
window.display();
}
So yes unsure why the position is not being set up or being taken from the window size or maybe it has to do with the rectSize that I did and being misread. I also think it has to do with the x and y as I set them up already with 450 nd 750.

It is tricky to help you without full code, cause I don't know how exactly did you want to use setPostioning.
After a small workaround, It finally appeared in the center of the screen.
Feel free to comment, if my example still doesn't satisfy your needs.
In the header file I added a reference to sf::RenderWindow, to use it in setPostioning.
Updated background.h:
class background : public sf::RectangleShape
{
public:
background(sf::RenderWindow &window);
background(sf::RenderWindow &window, float x, float y);
void setPostioning(float x, float y);
private:
// Added a refence to original window to have possibility use it in setPositioning
sf::RenderWindow& m_window;
};
In .cpp file I removed some redundant refs to sf::RectangleShape (cause you already inherited from it), and to sf::RenderWindod (cause a referene to it is stored inside class).
Updated background.cpp:
background::background(sf::RenderWindow &window) : background(window, 450, 750)
{
}
background::background(sf::RenderWindow &window, float x, float y) : m_window(window)
{
// sf::RectangleShape rectangle;
sf::RectangleShape::setSize({ x, y });
}
void background::setPostioning(float x, float y)
{
sf::Vector2f rectSize = { x,y };
// don't use rectangle from param, because you already inherited from sf::RectangleShape
//rectangle.setSize(rectSize);
setSize(rectSize);
sf::Vector2f center;
// again, don't use rectangle from param, because you already inherited from sf::RectangleShape
//rectangle.setPosition({
setPosition({
center.x = m_window.getSize().x / 2 - rectSize.x / 2,
center.y = m_window.getSize().y / 2 - rectSize.y / 2
});
}
In main function I called setPostioning before the event loop and added window.draw(b); to render your background.
Updated main function:
int main()
{
//set up of the window
sf::VideoMode videoMode(1280, 1024, 32);
sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
window.setFramerateLimit(15);//frame rate
background b(window);
// use setPostioning
b.setPostioning(200.f, 200.f);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
//when window is running they can close it with the close button
if (event.type == sf::Event::Closed)
{
window.close();
}
//this if statement will make our card stay in the same ratio no matter what
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(),
sf::Vector2f((float)event.size.width, (float)event.size.height)));
}
}
//invoking and set up to be drawn and display on the window when running
window.clear(sf::Color::Black);
window.draw(b);
window.display();
}
}

Related

Falling sand simulation collision detection C++ and SFML

I could really use some help. I am trying to make a falling sand simulation and have some basic code down, but I can't figure how to do collision detection. Here is my code so far:
//SFML Include
#include <SFML/Graphics.hpp>
//Include Vectors
#include <vector>
//Main Loop
int main()
{
//Window Init
sf::RenderWindow window(sf::VideoMode(720, 480, 32), "Yip Yip Physics", sf::Style::Default);
//Global Envoirmental Variables
static float gravity = 0.25;
static float frict = 3;
//Particle Structures
struct Particle
{
//Shape
sf::RectangleShape rect;
//Cell Position
sf::Vector2f cell_pos;
//Vel and frict
float slide_vel = 3;
float grv_vel = 0;
//Init Particle (size, color and origin)
void init()
{
rect.setSize(sf::Vector2f(3, 3));
rect.setFillColor(sf::Color::Green);
rect.setOrigin(rect.getSize());
}
};
//Particle Setup
std::vector<Particle> particles;
//Window Loop
while (window.isOpen())
{
//Update
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
//Make and Position Particle
Particle part;
part.rect.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
//Initalize the Particle and add to vector
part.init();
particles.push_back(part);
}
//Pixel Update
for (auto& p : particles)
{
//Draw in window
window.draw(p.rect);
}
//Display
window.display();
//Event Variable
sf::Event event;
//Window Event System
while (window.pollEvent(event))
{
//Close Window
if (event.type == sf::Event::Closed)
{
window.close();
}
}
//Window Clear
window.clear(sf::Color::Black);
}
//Return 0
return 0;
}
The code works in making the particles, putting them in a vector and drawing them to the screen, but I don't know where to go for collision. I don't know where to look and would really appreciate help on it or direction on where to find it. I have heard a bit about cellular automata but have no idea how to do it or if it's the best option.

SFML white background underneath texture when moving

I am maing a small game using SFML, anyway, my issue is that when rendering the sprite, and moving with float values. The sprite has a white background that 1 pixel one whichever side is moving gets shown.
Here is my Spritesheet class:
Spritesheet::Spritesheet(std::string t) {
this->texture.loadFromFile(t);
this->sprite.setTexture(this->texture);
}
sf::Sprite Spritesheet::getSprite(int x, int y, int width, int height) {
sf::Sprite spt;
spt.setTexture(this->texture);
spt.setTextureRect(sf::IntRect(x, y, width, height));
return spt;
}
void Spritesheet::setSprite(std::string t) {
this->texture.loadFromFile(t);
this->sprite.setTexture(this->texture);
}
And then the player class which is the class that draws the sprite:
Player::Player(int x, int y) {
// Some other code
this->spritesheet.setSprite("./res/img/tiles.png");
this->sprite = this->spritesheet.getSprite(48, 48, 16, 16);
this->sprite.setPosition(x, y);
this->sprite.scale(4, 4);
}
// Further down
void Player::render(RenderWindow& g) {
g.draw(this->sprite);
}
I have also tried using the sprite function setColor but that changes the texture color aswell.
I've had a problem similar to this before.
To fix it I had added a clock before the display so that I could be sure that the display was finished before I could display it again.
sf::Clock clock;
while (clock.getElapsedTime().asMilliseconds() < 100);
clock.restart();
maybe its FPS, i had a similar issue when i did not set a max fps, try window.setFramerateLimit(60); maybe it would fix it

Using vectors to create shapes

I have recently been playing around with the C++ sfml library and i tried using vectors to create multiples of the same shape but i simply do not know how to implement vectors with sfml shapes.
Here is the code i am working with, I am attempting to create more BlueTiles Behind the Blue square everytime it moves, but i don't know how. I would also like to know how to shorten the grid segment.
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
int main()
{
// Window Declarations Start
sf::RenderWindow Window(sf::VideoMode(673,673), "TileEat");
Window.setFramerateLimit(5);
sf::Event event;
// Window Declarataions End
// The Blue Start
sf::RectangleShape Blue(sf::Vector2f(32,32));
Blue.setPosition(0,0);
Blue.setFillColor(sf::Color(0,0,255));
sf::RectangleShape BlueTile(sf::Vector2f(32,32));
BlueTile.setFillColor(sf::Color(0,0,255));
std::vector<sf::RectangleShape>BlueTileVector;
// The Blue End
// The Grid Start
sf::RectangleShape VerticalLine(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine2(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine3(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine4(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine5(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine6(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine7(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine8(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine9(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine10(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine11(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine12(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine13(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine14(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine15(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine16(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine17(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine18(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine19(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine20(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine21(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine22(sf::Vector2f(1,700));
VerticalLine.setPosition(32,0);
VerticalLine2.setPosition(64,0);
VerticalLine3.setPosition(96,0);
VerticalLine4.setPosition(128,0);
VerticalLine5.setPosition(160,0);
VerticalLine6.setPosition(192,0);
VerticalLine8.setPosition(224,0);
VerticalLine9.setPosition(256,0);
VerticalLine10.setPosition(288,0);
VerticalLine11.setPosition(320,0);
VerticalLine12.setPosition(352,0);
VerticalLine13.setPosition(384,0);
VerticalLine14.setPosition(416,0);
VerticalLine15.setPosition(448,0);
VerticalLine16.setPosition(480,0);
VerticalLine17.setPosition(512,0);
VerticalLine18.setPosition(544,0);
VerticalLine19.setPosition(576,0);
VerticalLine20.setPosition(608,0);
VerticalLine21.setPosition(640,0);
VerticalLine22.setPosition(672,0);
sf::RectangleShape HorizontalLine(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine2(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine3(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine4(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine5(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine6(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine7(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine8(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine9(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine10(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine11(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine12(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine13(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine14(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine15(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine16(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine17(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine18(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine19(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine20(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine21(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine22(sf::Vector2f(700,1));
HorizontalLine.setPosition(0,32);
HorizontalLine2.setPosition(0,64);
HorizontalLine3.setPosition(0,96);
HorizontalLine4.setPosition(0,128);
HorizontalLine5.setPosition(0,160);
HorizontalLine6.setPosition(0,192);
HorizontalLine7.setPosition(0,224);
HorizontalLine8.setPosition(0,256);
HorizontalLine9.setPosition(0,288);
HorizontalLine10.setPosition(0,320);
HorizontalLine11.setPosition(0,352);
HorizontalLine12.setPosition(0,384);
HorizontalLine13.setPosition(0,416);
HorizontalLine14.setPosition(0,448);
HorizontalLine15.setPosition(0,480);
HorizontalLine16.setPosition(0,512);
HorizontalLine17.setPosition(0,544);
HorizontalLine18.setPosition(0,576);
HorizontalLine19.setPosition(0,608);
HorizontalLine20.setPosition(0,640);
HorizontalLine21.setPosition(0,672);
// The Grid End
// Game Loop Start
while (Window.isOpen())
{
while (Window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
Window.close();
}
// Blue Movement Start
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){Blue.move(0,32);BlueTileVector.push_back(BlueTile);}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){Blue.move(0,-32);}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){Blue.move(32,0);}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){Blue.move(-32,0);}
if(Blue.getPosition().x >= 672){Blue.setPosition(Blue.getPosition().x-32,Blue.getPosition().y);}
else if(Blue.getPosition().x <= -32){Blue.setPosition(Blue.getPosition().x+32,Blue.getPosition().y);}
if(Blue.getPosition().y >= 672){Blue.setPosition(Blue.getPosition().x,Blue.getPosition().y-32);}
else if(Blue.getPosition().y <= -32){Blue.setPosition(Blue.getPosition().x,Blue.getPosition().y+32);}
// Blue Movement End
// Drawing Table Start
Window.clear();
Window.draw(Blue);
Window.draw(VerticalLine);
Window.draw(VerticalLine2);
Window.draw(VerticalLine3);
Window.draw(VerticalLine4);
Window.draw(VerticalLine5);
Window.draw(VerticalLine6);
Window.draw(VerticalLine7);
Window.draw(VerticalLine8);
Window.draw(VerticalLine9);
Window.draw(VerticalLine10);
Window.draw(VerticalLine11);
Window.draw(VerticalLine12);
Window.draw(VerticalLine13);
Window.draw(VerticalLine14);
Window.draw(VerticalLine15);
Window.draw(VerticalLine16);
Window.draw(VerticalLine17);
Window.draw(VerticalLine18);
Window.draw(VerticalLine19);
Window.draw(VerticalLine20);
Window.draw(VerticalLine21);
Window.draw(VerticalLine22);
Window.draw(HorizontalLine);
Window.draw(HorizontalLine2);
Window.draw(HorizontalLine3);
Window.draw(HorizontalLine4);
Window.draw(HorizontalLine5);
Window.draw(HorizontalLine6);
Window.draw(HorizontalLine7);
Window.draw(HorizontalLine8);
Window.draw(HorizontalLine9);
Window.draw(HorizontalLine10);
Window.draw(HorizontalLine11);
Window.draw(HorizontalLine12);
Window.draw(HorizontalLine13);
Window.draw(HorizontalLine14);
Window.draw(HorizontalLine15);
Window.draw(HorizontalLine16);
Window.draw(HorizontalLine17);
Window.draw(HorizontalLine18);
Window.draw(HorizontalLine19);
Window.draw(HorizontalLine20);
Window.draw(HorizontalLine21);
Window.draw(HorizontalLine22);
Window.display();
// Drawing Table End
}
std::cout << "BlueTileVector Size: " << BlueTileVector.size();
return 0;
// Game Loop End
}
Create a vector to hold your objects:
std::vector<sf::RectangleShape> rectangles;
And add elements to your vector in a loop:
// Create 20 rectangle shapes
for (int i = 0; i != 20; ++i)
rectangles.emplace_back(sf::Vector2f(1, 700));
You can also set their position in the above loop or in it's own loop:
for (auto& i : rectangles)
i.setPosition(200.f, 200.f);
And you can also draw the shapes by using a loop:
for (const auto& rect : rectangles)
window.draw(rect);
FYI. The above examples need -std=c++11 flag to compile. Otherwise regular for loop and push_back(sf::RectangleShape(sf::Vector2f(1, 700))) can be used instead.
You may want to learn about loops and containers. They are really helpful:
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
int main()
{
const size_t numberOfRowsAndColunms = 20;
sf::VideoMode videoMode = sf::VideoMode(600, 600);
sf::RenderWindow Window(videoMode, "TileEat");
Window.setFramerateLimit(30);
sf::Event event;
const sf::Vector2f gridSize = sf::Vector2f(videoMode.width / numberOfRowsAndColunms, videoMode.height / numberOfRowsAndColunms);
// The Blue Start
sf::RectangleShape Blue(gridSize);
Blue.setPosition(0, 0);
Blue.setFillColor(sf::Color(0, 0, 255));
std::vector<sf::RectangleShape> shadows;
// The Blue End
std::vector<sf::RectangleShape> grid;
for (int x = 0; x < numberOfRowsAndColunms; x++)
{
for (int y = 0; y < numberOfRowsAndColunms; y++)
{
sf::RectangleShape cell(gridSize);
cell.setFillColor(sf::Color::Transparent);
cell.setOutlineColor(sf::Color::White);
cell.setOutlineThickness(2.0f);
cell.setPosition(gridSize.x * x, gridSize.y * y);
grid.push_back(cell);
}
}
// Game Loop Start
while (Window.isOpen())
{
while (Window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
Window.close();
}
// Blue Movement Start
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
Blue.move(0, gridSize.y);
shadows.push_back(Blue);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
Blue.move(0, -gridSize.y);
shadows.push_back(Blue);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
Blue.move(gridSize.x, 0);
shadows.push_back(Blue);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
Blue.move(-gridSize.x, 0);
shadows.push_back(Blue);
}
// Blue bounds check
auto position = Blue.getPosition();
position.x = std::min(videoMode.width - gridSize.x, std::max(0.0f, position.x));
position.y = std::min(videoMode.height - gridSize.y, std::max(0.0f, position.y));
Blue.setPosition(position);
// Drawing
Window.clear();
int shadowCount = 0;
for (auto shadow : shadows)
{
shadowCount++;
shadow.setScale(sf::Vector2f(shadowCount / (float)shadows.size(), shadowCount / (float)shadows.size()));
Window.draw(shadow);
}
Window.draw(Blue);
for (auto line : grid)
{
Window.draw(line);
}
Window.display();
}
return 0;
}

SFML update function not moving sprite

The current update function should be moving the sprite to the left 10 pixels every time it's called, but the sprite (cup1Sprite) is static and not moving. I tried using sprite.rotate(); and the sprite rotated every frame (which was expected), so something is wrong with the way I've written the update function for updating the sprite position. Can anyone suggest what's going wrong?
Code:
DrawCups.h
class DrawCups
{
public:
DrawCups(sf::RenderWindow& window);
~DrawCups();
void update();
void drawCup1();
private:
sf::RenderWindow& _window;
//Cup1
sf::Texture _cup1; // the texture which will contain our pixel data
sf::Sprite _cup1Sprite; // the sprite which will actually draw it
};
Update function in DrawCups.cpp
void DrawCups::update()
{
sf::Vector2f pos = this->_cup1Sprite.getPosition();
pos.x+=10;
this->_cup1Sprite.setPosition(pos);
}
main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
window.setFramerateLimit(60);
DrawCups drawToWindow(window);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
drawToWindow.update();
window.clear(sf::Color::White);
drawToWindow.drawBench();
drawToWindow.drawCup1();
window.display();
}
return 0;
}

I'm making a Animal Crossing clone, but I'm having problems rendering the Player

Recently, I decided to make an Animal Crossing clone in
C++ and SFML 2.1. But I'm having some issues. The Player won't show up when commanded to be rendered. The program will compile and run just fine but the player just won't show up.
Here's my code:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
RenderWindow window(VideoMode(700, 500), "Animal Crossing: Old oak");
View view(FloatRect(1000, 1000, 300, 200));
class Villager{
public:
int x, y, w, h;
Sprite pl;
string loadDir;
Villager(int x, int y, int w, int h, Color c, string loadDir){
this->x = x;
this->y = y;
this->w = w;
this->h = h;
Image image;
image.loadFromFile(loadDir);
image.createMaskFromColor(Color::Magenta);
Texture tex;
tex.loadFromImage(image);
pl.setTexture(tex);
}
}
};
int main(){
Villager villager(1100, 1000, 100, 100, Color::Blue, "player.png");
view.zoom(5);
Image grasstexloader;
grasstexloader.loadFromFile("grass.png");
Texture grasstex;
grasstex.loadFromImage(grasstexloader);
Sprite grass;
grass.setTexture(grasstex);
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
if(event.type == Event::Closed)
window.close();
if(Keyboard::isKeyPressed(Keyboard::Up))
villager.moveUp();
if(Keyboard::isKeyPressed(Keyboard::Down))
villager.moveDown();
if(Keyboard::isKeyPressed(Keyboard::Left))
villager.moveLeft();
if(Keyboard::isKeyPressed(Keyboard::Right))
villager.moveRight();
if(Keyboard::isKeyPressed(Keyboard::Escape))
window.close();
}
window.setView(view);
window.draw(grass);
window.draw(villager.pl);
window.display();
window.clear();
}
}
I've been staring at this code for an hour now. But I just can't find an error!
Please help!
Edit: I solved the problem with the sprite not being visible, but the sprite is just white instead of the appropriate colors. It proboably has something to do with how I load the file. Please post any suggestions you have on how to fix this new problem!
Your sprite is rendered white because in your Villager constructor, you're giving a local Texture variable to setTexture, which then gets destructed at the end of the constructor scope.