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;
}
Related
I have to make Quarto game as a GUI program so firstly I wanted to make buttons in menu, but I don't know why it don't fill color when my mouse is at the button.
Code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include<bits/stdc++.h>
using namespace std;
using namespace sf;
class button{
public:
bool isClicked=false;
int Yposition;
int Xposition;
bool isMouseOn=false;
};
int main(){
//Defining variables
Vector2i mouseWindowPosition;
//rendering window "window"
RenderWindow window(VideoMode(800,600), "Quarto game - menu", Style::Default);
window.display();
//setting vsync
window.setVerticalSyncEnabled(true);
//loading textures
Texture backgroundTexture;
backgroundTexture.loadFromFile("Resources/background.png");
//making sprites
Sprite backgroundSprite;
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setPosition(0,0);
//making buttons and their colors
RectangleShape playButton(Vector2f(200.f,50.f));
playButton.setFillColor(Color(128,128,128));
playButton.setOutlineThickness(5.f);
playButton.setOutlineColor(Color(100,100,100));
button play;
//setting position of buttons
play.Xposition=70;
play.Yposition=200;
//game loop
while(window.isOpen()){
Event event;
playButton.setFillColor(Color(128,128,128));
play.isMouseOn=false;
while(window.pollEvent(event)){
if(event.type==Event::Closed){
window.close();
}
}
//Getting mouse position
mouseWindowPosition=Mouse::getPosition(window);
if(mouseWindowPosition.x<=play.Xposition && mouseWindowPosition.y<=play.Yposition && mouseWindowPosition.x>=play.Xposition+200 && mouseWindowPosition.y>=play.Yposition+50){
play.isMouseOn=true;
}
//Drawing to screen
window.clear();
window.draw(backgroundSprite);
if(play.isClicked==false){
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
}
if(play.isMouseOn==true){
playButton.setFillColor(Color(128,128,128));
}
window.display();
}
}
Is there any better way to make buttons in sfml?
The immediate reason why the button doesn't fill is your if the statement has the signs reversed for checking the box. Using this if statement instead should work:
//Getting mouse position
mouseWindowPosition=Mouse::getPosition(window);
if (mouseWindowPosition.x>=play.Xposition && mouseWindowPosition.y>=play.Yposition &&
mouseWindowPosition.x<=play.Xposition+200 && mouseWindowPosition.y<=play.Yposition+50){
//your code here
}
If you are not already aware, the x and y values are arranged like a table where positive y goes down instead of a cartesian coordinate system where positive y is up.
The other problem is the color you are updating your fill with is the same color of the fill, so changing that color will get you your desired fill.
Also, logic wise you should swap the statement
if (play.isMouseOn == true)
{
}
with the statement
if (play.isClicked == false)
{
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
window.display();
}
Here is a working code that changes the fill from grey to red when you hover above it:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
using namespace std;
using namespace sf;
class button
{
public:
bool isClicked = false;
int Yposition;
int Xposition;
bool isMouseOn = false;
};
int main()
{
//Defining variables
Vector2i mouseWindowPosition;
//rendering window "window"
RenderWindow window(VideoMode(800, 600), "Quarto game - menu", Style::Default);
window.display();
//setting vsync
window.setVerticalSyncEnabled(true);
//loading textures
Texture backgroundTexture;
backgroundTexture.loadFromFile("Resources/background.png");
//making sprites
Sprite backgroundSprite;
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setPosition(0, 0);
//making buttons and their colors
RectangleShape playButton(Vector2f(200.f, 50.f));
playButton.setFillColor(Color(100, 100, 100));
playButton.setOutlineThickness(5.f);
playButton.setOutlineColor(Color(100, 100, 100));
button play;
//setting position of buttons
play.Xposition = 70;
play.Yposition = 200;
//game loop
while (window.isOpen())
{
Event event;
playButton.setFillColor(Color(128, 128, 128));
play.isMouseOn = false;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
}
//Getting mouse position
mouseWindowPosition = Mouse::getPosition(window);
if (mouseWindowPosition.x >= play.Xposition && mouseWindowPosition.y >= play.Yposition && mouseWindowPosition.x <= play.Xposition + 200 && mouseWindowPosition.y <= play.Yposition + 50)
{
play.isMouseOn = true;
}
//Drawing to screen
window.clear();
window.draw(backgroundSprite);
if (play.isMouseOn == true)
{
playButton.setFillColor(Color(128, 0, 0));
}
if (play.isClicked == false)
{
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
window.display();
}
}
}
I'm making a Pacman game using SFML. I have a background image for the maze that I'm using and I was wondering how I would be able to use multiple bounding boxes for the background so that when the yellow Pacman character collides with the walls, it stops him. Also, I was wondering if I could use multiple bounding boxes for the background maze image, if so, how?
My code:
#include <SFML/Graphics.hpp>
#include <iostream>
int main(int argc, char** argv)
{
sf::RenderWindow renderWindow(sf::VideoMode(280, 480), "Pacman" /*, sf::Style::Titlebar | sf::Style::Close*/);
sf::Event event;
sf::Texture texture, texture2;
texture.setSmooth (true);
texture2.setSmooth (true);
texture.loadFromFile("images/pacmanbkgd.png");
sf::Sprite background_sprite(texture);
background_sprite.setScale(sf::Vector2f(1.25, 1.25));
texture2.loadFromFile("images/transparentPacman3Sprites.png");
sf::IntRect rectSourceSprite(0, 0, 18, 16);
sf::Sprite pacmanMC_sprite(texture2, rectSourceSprite); // sf::intrect(int rectleft, int recttop, int rectwidth, int rectheight)
// pacmanMC_sprite.setScale(sf::Vector2f(1, 1));
pacmanMC_sprite.move(sf::Vector2f(200, 345)); // set sprite at inital location point (140, 145)
sf::Clock clock;
//sets the origin of the sprite
sf::Rect<float> size = pacmanMC_sprite.getGlobalBounds();
pacmanMC_sprite.setOrigin(sf::Vector2f(size.width / 2, size.height / 2));
while (renderWindow.isOpen())
{
while (renderWindow.pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed)
renderWindow.close();
}
if (clock.getElapsedTime().asSeconds() > 0.75f) // 0.07, used for making the mouth faster
{
if (rectSourceSprite.left == 34)
rectSourceSprite.left = 0;
else
rectSourceSprite.left += 17; // pushes the sprite towards the left using x axis
pacmanMC_sprite.setTextureRect(rectSourceSprite);
clock.restart();
}
// pacmanMC_sprite movement
// sprite keeps on moving to a different position when a the correct key is pressed for its rotation
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
{
pacmanMC_sprite.move(0.0f, -0.015f);
pacmanMC_sprite.setRotation(-90.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
{
pacmanMC_sprite.move(0.0f, 0.015f);
pacmanMC_sprite.setRotation(90.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
pacmanMC_sprite.move(-0.015f, 0.0f);
pacmanMC_sprite.setRotation(-180.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
{
pacmanMC_sprite.move(0.015f, 0.0f);
pacmanMC_sprite.setRotation(0);
}
renderWindow.clear();
renderWindow.draw(background_sprite);
renderWindow.draw(pacmanMC_sprite);
renderWindow.display();
}
return 0;
}
I am currently trying to recreate Chess in SFML. Generating the board normally works as intended but when I am resizing the window I get weird white borders.
Before Resize:
After Resize:
It looks like the view is not aligned with the window properly so I think my problem is not in the board generation but how I am handling resizes. I thought manually updating the window view would help but it only ensured that the squares don't get streched. The border issue remains however so now I am quite clueless as to how I could fix this problem.
Board.h:
#pragma once
#include <SFML/Graphics.hpp>
class Board
{
public:
void createBoard(sf::Vector2u windowSize);
void drawBoard(sf::RenderWindow& window) const;
private:
sf::RenderTexture board;
sf::Color lightColor = sf::Color(159, 144, 176);
sf::Color darkColor = sf::Color(125, 74, 141);
sf::Color backColor = sf::Color(32, 31, 32);
};
Board.cpp:
#include <SFML/Graphics.hpp>
#include "Board.h"
void Board::createBoard(sf::Vector2u windowSize)
{
const float xOffset = static_cast<float>(windowSize.x - windowSize.y) / 2.f;
const float squareSize = static_cast<float>(windowSize.y) / 8.f;
board.create(windowSize.x, windowSize.y);
board.clear(backColor);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
sf::RectangleShape currSquare({ squareSize, squareSize });
currSquare.setFillColor((i + j) % 2 ? lightColor : darkColor);
currSquare.setPosition(xOffset + static_cast<float>(i) * squareSize, (static_cast<float>(j) * squareSize));
board.draw(currSquare);
}
}
}
void Board::drawBoard(sf::RenderWindow& window) const
{
window.draw(sf::Sprite(board.getTexture()));
}
main.cpp:
#include <SFML/Graphics.hpp>
#include "Board.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 300), "Chess");
sf::Event event;
sf::View view = window.getDefaultView();
Board board;
board.createBoard(window.getSize());
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
else if (event.type == sf::Event::Resized) {
view.setSize({ static_cast<float>(event.size.width), static_cast<float>(event.size.height) });
window.setView(view);
board.createBoard({ event.size.width, event.size.height });
}
}
window.clear(sf::Color::White);
board.drawBoard(window);
window.display();
}
return 0;
}
Does anyone know how I could fix this problem?
The problem is that you don't only need to resize the view, but also recenter it. As right now you are not doing it, the center remains where the smaller board was and the bigger view takes a chunk from the outside in the top left corner.
So just change your code like this:
...
else if (event.type == sf::Event::Resized) {
float w = static_cast<float>(event.size.width);
float h = static_cast<float>(event.size.height);
view.setSize({w , h});
view.setCenter({w/2.f , h/2.f}); // <----- !
window.setView(view);
board.createBoard({ event.size.width, event.size.height });
}
...
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();
}
}
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.