I'm working on a project at the moment which is basically a visualisation of Sort algorithms to explain how they work (rather than an overview). I'm new to using the SFML (or even OpenGL) and have limited experience with the library, but what I'm trying to do is move the drawn sprite to different locations to show the sorting. I've looked over tutorials and examples, but they all take in keyboard input to move the sprite - something that isn't used in this project. Does anyone know exactly how to achieve this?
Here's the current code:
DrawCups.h
class DrawCups
{
public:
DrawCups(sf::RenderWindow& window);
~DrawCups();
void loadImage(const char* pathname, sf::Texture& texture, sf::Sprite& sprite);
void drawCup1();
private:
sf::RenderWindow& _window;
};
DrawCups.cpp (selected function)
void DrawCups::drawCup1()
{
// load our image
sf::Texture cup1; // the texture which will contain our pixel data
sf::Sprite cup1Sprite; // the sprite which will actually draw it
loadImage("./images/InsertionSort/red_cup_1.png", cup1, cup1Sprite);
cup1Sprite.setPosition(sf::Vector2f(150, 230));
_window.draw(cup1Sprite);
}
main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
window.setFramerateLimit(60);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
DrawCups drawToWindow(window);;
drawToWindow.drawCup1();
window.display();
}
return 0;
}
Create the image before the loop and update it before you draw it.
DrawCups drawToWindow(window); //Constructor creates the sprite
while (window.isOpen())
{
...
drawToWindow.update(); //Update the position
//Redraw
window.clear(sf::Color::White);
drawToWindow.drawCup1();
window.display();
}
I'm not sure what type of movement you want but the update function can be something like this:
void DrawCups::update()
{
sf::Vector2f pos = this->cup1Sprite.getPosition();
pos.x++; //Move 1 pixel to the left
this->cup1Sprite.setPosition(pos);
}
Obviously change the movement to suit your needs. Make smaller/larger updates if it's moving too fast or slow.
Related
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.
I am working on a basic game in C++ using SFML for graphics. It is designed to use a grid system with functions to determine what should be displayed in each square. However, the compiler won't recognize the references to the window in the functions.
To keep it easier to expand there are functions for each type of terrain to be displayed, taking the coordinates as inputs (possibly not the correct term).
Good Question. SFML recommends that you create your window in the main function, but, if you want to modify it, you can simply pass it by reference. EX:
#include <SFML/Graphics.hpp>
void doSomething(sf::RenderWindow& window) {
sf::RectangleShape shape(sf::Vector2f(100, 100));
shape.setFillColor(sf::Color::Green);
shape.setPosition(50, 50);
window.draw(shape);
window.display();
}
int main() {
sf::RenderWindow window(sf::VideoMode(500, 500), "Test", sf::Style::Close);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
//draw here
doSomething(window);
}
return 0;
}
You should take a look at https://www.sfml-dev.org/tutorials/2.5/. Specifically I would look at https://www.sfml-dev.org/tutorials/2.5/window-window.php
So, I was trying to create a class in C++ for an animated sprite that uses spritesheets for input, and when trying to move the sprite, it just sort of bounces back. It won't save to the position.
I tried using .setPostion() but that still bounces back to where it was. I have no other methods of moving the sprite implemented, nor updating it or anything else. The part in concern in the member function run, which is void is:
//if statement
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
curranim[2] = Right;
playerSprite.setPosition(playerSprite.getPosition().x + walkingSpeed, 0);
}
window.draw(playerSprite);
//spritesheet.cpp ends here
Now here is my main function:
int main()
{
sf::RenderWindow Window(sf::VideoMode(900, 600), "RPG");
animsprite sprite("dragon.png", 1, Window);
sprite.setWalkSpeed(10);
sf::Event Event;
while (Window.isOpen())
{
while (Window.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
}
}
Window.clear();
sprite.run(Window);
Window.display();
}
return 0;
}
Will I be stuck to repeating my code forever or is there a fix?
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;
}
There are probably millions of these questions but, I can't get the coordinates of the mouse so that they line up with the program coordinate system, when the window is re-sized. I've tried mapPixelToCoords() and getting the mouse coordinates using sf::Event::MouseButton or sf::Mouse, but to no avail.
This problem is most likely from my incompetence, but I can't figure it out for the life of me. Also, I need to do this to change the coordinates of a rectangle, not detecting whether a box is being hovered over, if it was the answer would be a lot easier to figure out I feel.
Edit:
Source Code:
//Standard C++:
#include <iostream>
//SFML:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Example");
sf::Event event;
sf::RectangleShape mousePoint;
mousePoint.setSize(sf::Vector2f(1, 1));
mousePoint.setFillColor(sf::Color::Red);
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) //Close window
{
window.close();
return 0;
}
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
//Get the mouse position:
sf::Vector2i mouse = sf::Mouse::getPosition(window);
//Map Pixel to Coords:
window.mapPixelToCoords(mouse);
//Set position of the mouse to the rectangle:
mousePoint.setPosition(mouse.x, mouse.y);
}
}
}
window.clear();
window.draw(mousePoint);
window.display();
}
}
After some doubt, I have uploaded some simple source code, which demonstrates my point. When you click the LMB, it moves the rectangle to where the program thinks the mouse is. When the screen hasn't been scaled, it is correctly calibrated, but when it is changed, the rectangle moves to a point which is no where near where the mouse is.
As shown in the official documentation and the official tutorial section of SFML, you can use the mapPixelToCoords function to map pixel/screen coordinates to world coordinates.
The signature of the function is as following:
Vector2f sf::RenderTarget::mapPixelToCoords(const Vector2i& point) const
As such the usage would look something like that:
//Get the mouse position:
sf::Vector2i mouse = sf::Mouse::getPosition(window);
//Map Pixel to Coords:
sf::Vecotr2f mouse_world = window.mapPixelToCoords(mouse);
//Set position of the mouse to the rectangle:
mousePoint.setPosition(mouse_world);
In other words, the mapPixelToCoords function, takes a const sf::Vector2i& as parameter and returns a sf::Vector2f and the original vector is not being modified.
It's always recommended to take a closer look at the documentation if something doesn't work as expected.