Zooming in and out a picture (sfml) using recursion in c++ - c++

Using the following code I am to add a recursive function with out changing anything in the code for it to work:
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), TITLE);
int stop = 800;
sf::Color colour1(sf::Color::Black), colour2(sf::Color::Red);
// Start the main loop
while (window.isOpen()) {
// Process events
sf::Event event;
while (window.pollEvent(event)) {
// check the type of the event...
switch (event.type) {
// window closed
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseMoved: //mouse moved
stop = mapXtoMinSize(event.mouseMove.x); // convert x coordinate of mouse to minimum size of square to draw
break;
// we don't process other types of events
default:
break;
} //end switch
} //End-Process events
window.clear(colour2);
}
I just would like to know how to go about doing such.

A recursive function generally goes like this:
int recursive(int x) {
if (x == 0) {
return 0;
} else {
return (x + recursive(x - 1));
}
}
This will sum all the integers from 0 to x, by repeatedly calling itself to compute the sum of (x - 1) and adding this to the result. If the base case, x == 0, is encountered the recursion ends and the calls unwind until they reach the original, by which point the sum has been calculated. Please be more specific.
Without knowing what you are trying to achieve with your recursive function I cannot give you any more advice. I do not know what you mean with you comment "with out changing anything in the code".

Related

C++ and SFML - Transforming by decimal causes artifacts

I’m making a game with C++ and SFML. I have delta time in my game by setting it to clock.restart().asSeconds() and then multiplying that by a movement offset. The only issue is that when the final movement value is not an integer (or .0f), it causes rendering artifacts between my sprites. I have a grid of sprites that are all lined up next to each other. When I move by an integer offset, it looks good. However, when moving with a decimal, I can see strange lines between the sprites. Is there any way of fixing this?
note: I don't want to round the deltatime * offset value because when fps is too high, it will be rounded to 0 and the player will not move at all.
delta time code:
void Game::run()
{
init();
Gui::init();
Gui::mainMenu();
sf::Clock deltaClock;
while (window.isOpen())
{
sf::Event loopEvent;
while (window.pollEvent(loopEvent))
{
switch (loopEvent.type)
{
case sf::Event::Closed:
saveGame();
window.close();
break;
case sf::Event::KeyPressed:
if (loopEvent.key.code == Gui::keybinds[Action::EXIT_MENU] && Player::hasSpawned)
{
Gui::inMenu = false;
Gui::currentGameState = GameState::SETTINGS;
}
}
}
deltaTime = deltaClock.restart().asSeconds();
update();
window.setView(camera);
window.clear(sf::Color::White);
draw();
window.display();
Gui::checkGameState();
}
}
const float Game::MAKE_DELTA(const float OFFSET)
{
return OFFSET * deltaTime;
}
Moving with integer offset (or when I round the return value of MAKE_DELTA()):
Moving with decimal offset (unrounded return value from MAKE_DELTA()):

SFML shape rotates infinitely

I'm making a robot arm which moves around a graph but when I try to rotate a shape with my keyboard it rotates infinetly: I want it to rotate just one time when I press the right arrow. How can I solve this?
Transform transform;
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
if (event.type == Event::KeyPressed)
{
switch (event.key.code)
{
case Keyboard::Right:
ang += 1;
}
}
window.clear(Color::White);
window.draw(braccio, transform);
transform.rotate(ang, WIDTH / 2, HEIGHT / 2);
window.draw(assi);
window.display();
}
Right now, this line
transform.rotate(ang, WIDTH / 2, HEIGHT / 2);
Is called on every frame of your program because it's in the main loop, that runs constantly. If you want it to happen only while your game detects some input, put it inside your event polling, like so:
while (window.pollEvent(event))
{
// Do your event handling in here, input, etc.
if (event.type == Event::Closed)
window.close();
// This event case should also be inside here.
if (event.type == Event::KeyPressed)
{
switch (event.key.code)
{
case Keyboard::Right:
transform.rotate(ang, WIDTH / 2, HEIGHT / 2);
break;
}
}
}
Otherwise your arm will continue to rotate indefinitely.
EDIT: Thanks #alseether for pointing out that the Transform::rotate function adds the angle to the shape's current rotation. So incrementing ang each time will gradually make the shape rotate faster and faster... If you don't want this to happen just set ang to a constant value and it will rotate the cube at a constant rate.

sfml animation of explosion after mouseclick

I need your piece of advice. I'm using SFML and I need to play animation from the spritesheet(f.e. 64 frames and 40px width/height of each frame) after mouseclick event. The only solution I've come to is:
if (event.type == sf::Event::MouseButtonPressed) {
if (event.key.code == sf::Mouse::Left) {
float frame = 0;
float frameCount = 64;
float animSpeed = 0.005;
while (frame < frameCount) {
spriteAnimation->setTextureRect(sf::IntRect(int(frame)*w, 0, w, w));
frame += animSpeed;
window->draw(rect); // clear the area of displaying animation
window->draw(*spriteAnimation);
window->display();
}
...
But calling window->display() so many times is really not good;
Can you suggest better variants?
Instead of jamming all of your code for the animation into the event block you should spread it out.
Your design here is very inflexible in that if you ever want to display anything other than an animation you are going to have to call window->display() again outside of your event loop.
Generally in SFML your game loop proceeds similarly to as follows:
initialize();
while(running)
{
checkEvents();
clear();
update();
display();
}
Instead of performing all of the calculations and displaying for your animation inside the event's if statement you should set a bool or call a doAnimation() function of some sort. I've written a rough example below:
bool doAnimation = 0;
//declare frame, framespeed, etc
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.key.code == sf::Mouse::Left)
{
doAnimation = true;
//reset frame, framespeed, etc
}
}
clear();
if(doAnimation)
{
sprite->setTexture(...);
if(frame == endFrame)
{
doAnimation = 0;
}
drawSprite();
}
window->display();
There are tons of ways to solve your problem. My example is less flexible than I would think is ideal but depending on the needs of your program it may work fine. If you wanted to take the next step moving the animation into a class of some sort would make your life a lot easier in the long run.

SFML extremly slow/irregular framerate

I've recently been on the lookout for a C++ game-developement framework and decided to give SFML a try. I've implemented an early example from the book 'SFML game developement' to see if things work properly. Sadly they don't.
The code is supposed to make a circular object move rapidly when pressing one of the 'wasd' keys. In reality the circle begins moving slowly for a distance of maybe ten pixels, then stops and if I hold the respective key down further the circle jumps another few pixels whenever it feels like it.
//the header game.h
class Game
{
public:
Game();
void run();
private:
void processEvents();
void handlePlayerInput(sf::Keyboard::Key, bool);
void update();
void render();
sf::RenderWindow mWindow;
sf::CircleShape mPlayer;
bool mIsMovingUp;
bool mIsMovingDown;
bool mIsMovingLeft;
bool mIsMovingRight;
};
//the implementation
#include<SFML/Graphics.hpp>
#include "game.h"
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
, mIsMovingUp(false), mIsMovingDown(false), mIsMovingLeft(false), mIsMovingRight(false)
, mPlayer()
{
mPlayer.setRadius(40.f);
mPlayer.setPosition(100.f, 100.f);
mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run()
{
while (mWindow.isOpen())
{
processEvents();
update();
render();
}
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
void Game::processEvents()
{
sf::Event event;
while (mWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
mIsMovingDown = isPressed;
else if (key == sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::D)
mIsMovingRight = isPressed;
}
void Game::update()
{
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= 1.0f;
if (mIsMovingDown)
movement.y += 1.0f;
if (mIsMovingLeft)
movement.x -= 1.0f;
if (mIsMovingRight)
movement.x += 1.0f;
mPlayer.move(movement);
}
//main file
#include <SFML/Graphics.hpp>
#include "game.h"
int main()
{
Game game;
game.run();
return 0;
}
I honestly dont't even know where to begin looking for a solution.
I guess this could be driver-related, I will test it on another Computer tomorrow.
Any advice on this would be appreciated.
Edit in response to Ike:
I got the SFML libraries from the Debian repository, running
`dpkg -l | grep sfml`
returns:
ii libsfml-audio2:amd64 2.1+dfsg2-1+b2
ii libsfml-dev:amd64 2.1+dfsg2-1+b2
ii libsfml-graphics2:amd64 2.1+dfsg2-1+b2
ii libsfml-network2:amd64 2.1+dfsg2-1+b2
ii libsfml-system2:amd64 2.1+dfsg2-1+b2
ii libsfml-window2:amd64 2.1+dfsg2-1+b2
I compiled the code above with g++ using the -O2 flag.
I can't imagine that this is an optimization problem since the game-loop seems to go through maybe two dozen iterations per second (at most). I can't say this for certain but I believe that number should be in the thousands.
Another Edit: Alright, so I've compiled the same code in an Ubuntu-VM again using the repo-libraries. The circle now moves reasonably fast which is better than nothing I guess (and also suggest some issue with debian, possibly driver-related?). On another Computer (which is admittedly faster than my laptop) running arch and using sfml 2.3.2 the program runs easily ten times as fast
A couple of things to try:
A) Set mWindow.setKeyRepeatEnabled(false). This will disable auto-repeat.
B) Try changing your Game::processEvents to use an if instead of a while, like so:
void Game::processEvents()
{
sf::Event event;
if (mWindow.pollEvent(event))
{
...
}
}
What I suspect you're seeing is the result of the event queue being spammed with auto-repeating key events. This will then cut into the frequency at which functions like update and render are being called and destroy the smooth frame rate.
The first suggestion tries to eliminate this potential of jamming up the queue. The second would make it so updates/refreshes are interleaved in between event processing and not only performed when the event queue becomes empty.
Judging from my related question https://gamedev.stackexchange.com/questions/185873/sfml-test-app-feels-slow-clunky/185880#185880 the issue could be that you were using keyboard events rather than real-time keyboard input.
i.e. sf::Keyboard::isKeyPressed
See https://www.sfml-dev.org/tutorials/2.5/window-inputs.php

How can I set gravity using this code? SFML/C++ [duplicate]

This question already has an answer here:
How can I set gravity using this code?
(1 answer)
Closed 8 years ago.
I am trying to make a game and am stuck on gravity..... In the following code a rectangle stands for a player and when I press up key it moves in y-axis but when I activate gravity on it (i.e resetting it's previous position) it does not animate (i.e. it does not jumps) instead it just stays in it's position.I know why it it happening. Because it is just staying in its position because when I press Up key it executes the code rectangle.setPosition(0, 350). Yeah I want it to do that but I also want to see my player in movement. I am using SFML library of C++. Please Help!
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Gravity");
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(100, 100));
rectangle.setFillColor(sf::Color::Black);
rectangle.setPosition(sf::Vector2f(10, 350));
while(window.isOpen())
{
sf::Event Event;
while(window.pollEvent(Event))
{
if(Event.type == sf::Event::Closed)
{
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
rectangle.move(0, -1);
}
if(rectangle.getPosition().y >= 350-1)
{
rectangle.setPosition(0, 350);
}
window.display();
window.clear(sf::Color::Cyan);
window.draw(rectangle);
}
}
Gravity is an acceleration: that is, the double derivative of displacement. So you can't directly set the displacement (as you're currently doing) to get a nice representation of gravity.
An approach would be to create an entity class of your own, adding members for velocity, acceleration; alongside sf::RectangleShape's internal displacement; then have member functions operate on initialization/every frame - a quick & dirty example (untested):
class SomeEntity {
public:
SomeEntity( float x_pos, float y_pos ) : position(x_pos, y_pos) {
m_shape.setSize(sf::Vector2f(100, 100));
m_shape.setFillColor(sf::Color::Black);
m_shape.setPosition(sf::Vector2f(x, y));
// Constants at the moment (initial velocity up, then gravity pulls down)
velocity = sf::Vector2f(0, -30);
acceleration = sf::Vector2f(0, 9.81); //Earth's acceleration
}
void Step() { // TODO: use delta time
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
velocity.y -= 1;
}
velocity.x += acceleration.x;
velocity.y += acceleration.y;
x += velocity.x;
y += velocity.y;
m_shape.setPosition(x, y);
}
void Draw(sf::RenderWindow &window) {
window.draw(m_shape);
}
private:
sf::RectangleShape m_shape;
sf::Vector2f position, velocity, acceleration;
}
Which also means you can rewrite your application so it's a little cleaner:
SomeEntity ent(360, 0);
while(window.isOpen()) {
sf::Event Event;
while(window.pollEvent(Event)) {
if(Event.type == sf::Event::Closed) {
window.close();
}
}
ent.Step();
window.display();
window.clear(sf::Color::Cyan);
ent.Draw();
}
Seeing as you're setting your rectangle to x = 0, y = 350 repeatedly, I'll work under the assumption that that's your 'ground plane'. To achieve that, you just want to check whether the entity is under the ground plane, and reset it's position to the ground plane if it is - either in the entity's 'Step' function or directly in your main loop. In the long run, you might be better off using an entity manager/third party physics engine to do this sort of thing (a la box2D, for example)
You can do something like this instead :
if(rectangle.getPosition().y > 350)
{
rectangle.move(0, 0.01);
}