Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Messages - Fx8qkaoy

Pages: [1] 2 3
General / Re: Shape animations
« on: April 30, 2020, 05:56:01 pm »
The response shall be where u found the gif. Guessing, I think it's made in CSS. Look at the CSS properties and implement them. I would say they are:
- interpolating size
- interpolating position
Given a specific time, u calculate the pos of the moving circle, and the size of 8 fixed circles. The size shall be a single ecuation, and for every circle u out of phase it with a constant time

I did not try any libs for this, but searching "SFML animation libs" seems to yield good results. U could go with specific libs for making true HTML / CSS interface, as SFML allows u to control the OpenGL, but maybe it's overkill

General / Re: How to fire enemy bullet towards player
« on: April 23, 2020, 11:19:13 am »
Code: [Select]
_parent->setPosition(diff * (float)dt);

U have problems doing the interpolation. "setPosition" sets the absolute position which means that the bullet is independent of its initial or last pos, which is wrong. U have to take in account the position of the previous frames. I'm not really sure how u handle the whole algorithm, but from what u sent the following line shall solve the problem or at least be suggestive of what u have to do:

Code: [Select]
_parent->setPosition(bPos + diff * (float) dt);

Network / Re: Ignore packet
« on: April 20, 2020, 12:43:12 pm »
There is not easy solution, maybe you can send two packets one with the game information and other only with the Ip address, you receive first the one with the Ip, if its on the list, you disconnect or remove that client.

Why you dont want to receive the packet? Its because of protection?

If u avoid receiving the packet, things would work way faster. Depends also on the size of the packet, and how big of bandwidth u wanna handle. As example for a game u would probably send relative small packets, so it doesn't matter anyway, which means checking the ip on the packet which also contains data is not a bad idea

Protection? It depends on ur app. As example, agains a DDOS, the firewall will show better results rather than checking the ip in ur code

System / Re: checking for events
« on: March 30, 2020, 08:26:55 am »
iam trying to make a menu
i need to check if the user pressed a button to move throw the menu
when i tried doing this function then calling it in main
if (Keyboard::isKeyPressed(Keyboard::S))
the it goes throw all menu then the program crash
else if i use
if (event.type == Event::KeyPressed && event.key.code == Keyboard::S)
it works fine
 so what is the difference between these two ways what did ido wrong

In the first code u're not relying on events. Probably ur index goes too high and this is why it crashes. The same happens on the second, but since it reacts on-event (not on-frame) it is executed fewer times and index doesn't reach the end

Strange. When I compiled and ran my code, this happened:

I would bet it's because u're not loading the font from the memory the right way, it is corruped, etc. This should've been the single different thing from our sources

Here's the "simplified" version:



Although I rewrote the code, the problem is the same. (What I want to achieve is creating a list of buttons.)

I was able to run the program and while it doesn't throw any exception, it also displays the text (see attachment). When stuff is trivial, the simple fact that u're trying to build a minimal code reveals the problem on the way

Hi there!
I'm a C++ newbie, and I'm new to the forum as well. Recently I've been trying to write my own GUI classes, so I can use them in my later projects. While writing a combo box, I stumbled something strange. When I constructed a button I'd written, it was drawn 'perfectly fine' to the window. But when I pushed my button object to std::vector and called it to draw to the screen. This happened: https://drive.google.com/file/d/1QclBCkOzErf6zx9jbKtUh1X36Ox3PzFb/view?usp=sharing

It would've been also useful to provide some code. Try these:
- use a debugger and break before drawing
- make sure u use the "=" operator right
- prepare for us a minimal code which reproduces the problem

Graphics / Re: Problem with drawing shapes
« on: March 21, 2020, 11:19:22 am »
Thank you!
It's my C++ understanding, very low.
I managed to get them drawn now! It was in the constructor to just define them correctly.

Could you also help me understand how to make methods for keyboard input and sight rotation to work in main method?

As of now  they are not reacting to input when I call them as class methods. I would think the event scope is different now, might have something to do with it.?

The way u are handling keyboard and rotation are valid. Doesn't seem to be a syntax problem, but rather ur logic if it doesn't work. I don't have ur modified code. Use a debugger to detect was is not working

I have been researching this issue for a couple weeks now until I finally decided to post the question myself.

I am having an issue where when I move the view the sprites in the scene sometimes stutter or "shake". It happens every few seconds or so. Sometimes there could be perfectly smooth movement then next it's stuttering like crazy.

I have looked into Fixed Time steps, interpolation, Clamping positions to pixel perfect and other such things in the various forums posts I've seen that also have the issue. Nothing seems to work.

I am working on my laptop and have tried using both integrated graphics and nvidia GPU (GeForce GTX 1050 Ti Max-Q Design)

You can see here from the video what I mean: https://www.youtube.com/watch?v=HewJ9oUtpNU

and here is my current code:

#include <SFML/Graphics.hpp>

#include <time.h>
#include <iostream>

sf::Vector2f linearInterpolation(sf::Vector2f start, sf::Vector2f target, float alpha)
    return (target * alpha + start * (1.0f - alpha));

sf::Vector2f PixelPerfectClamp(sf::Vector2f position, float pixelsPerUnit)
        sf::Vector2f inPixels((int)std::round(position.x * pixelsPerUnit), (int)std::round(position.y * pixelsPerUnit));

        return inPixels / pixelsPerUnit;

static const float PPU = 16.0f;

const float MOVE_SPEED = 64.0f;

int main()
    const std::string windowTitle{ "Demo" };
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "Shiver", sf::Style::Default);
        // Tried with V-SYNC true and Framerate limit off.

    sf::View view(sf::Vector2f(0.0f, 0.0f), sf::Vector2f(320, 180));

        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        const sf::Time TimePerFrame = sf::seconds(1.0f / 120.0f); // Fixed Time Update rate.


    sf::Texture treeTexture;
    if (!treeTexture.loadFromFile("Assets/evergreen-tree.png"))
        return 0;

    sf::Sprite player;
    player.setTextureRect(sf::IntRect(0, 0, 48, 48));

    std::vector<sf::Sprite> trees;
    for (unsigned int i = 0; i < 100; ++i)
        sf::Sprite tree;
        tree.setTextureRect(sf::IntRect(0, 0, 48, 48));
        tree.setPosition(rand() % 10 * i, rand() % 10 * i);

        // Positions for interpolation.
    sf::Vector2f previousPos = player.getPosition();
    sf::Vector2f currentPos = player.getPosition();
        while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)

                auto frameTime = clock.restart();

                if (frameTime.asSeconds() > 0.25f)
                        frameTime = sf::seconds(0.25f);

                timeSinceLastUpdate += frameTime;

                //while (timestep.isUpdateRequired()) // this is true as long as there are unprocessed timesteps.
                while (timeSinceLastUpdate >= TimePerFrame)
                        timeSinceLastUpdate -= TimePerFrame;
            previousPos = currentPos;

            sf::Vector2f velocity;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
                                velocity.y = -MOVE_SPEED;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
                                velocity.x = -MOVE_SPEED;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
                                velocity.y = MOVE_SPEED;
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
                                velocity.x = MOVE_SPEED;

                        if ((velocity.y > 0.0f || velocity.y < 0.0f) && (velocity.x > 0.0f || velocity.x < 0.0f))
                                velocity *= sin(45.0f);

                        currentPos += velocity * TimePerFrame.asSeconds();

                float interpolationAlpha =      timeSinceLastUpdate / TimePerFrame;

                // Tried with and without interpolating.
                sf::Vector2f newPosition = currentPos;
        //sf::Vector2f newPosition = linearInterpolation(previousPos, currentPos, interpolationAlpha);


        window.clear(sf::Color(150, 150, 150));
        for (auto& tree : trees)


    return 0;

Any help would be appreciated. I have attached the sprite image as well.


It quite bothers me the way u're calculating "currentPos". Let's consider that 60 fps (vsync (vsync probably doesn't have any effect if not fullscreen) or limited) is 16 ms, and "TimePerFrame" is 8 ms (they are a bit bigger but doesn't matter for my explain). Every frame is quite likely to get like 16.01 ms let's say. It means that u enter "while (timeSinceLastUpdate >= TimePerFrame)" 2 times every frame for some time. "timeSinceLastUpdate" will reach at some time 23.99. U will enter the "while" loop mentioned earlier 2 times, and the next frame u will enter 3 times, which means the player is gonna move a bit faster in some frames (aka slower in other frames). This effect is invisible if 2 or 3 moves will make the player not move more than 1 pixel. If let's say one time moves 1 pixel and other time 2 pixels, then theoretically is visible. Depending on the speed of the player, as example, 5 vs 8 pixels can show a bit of difference

This is what I think happens. It is a problem with fixed steps: garbage adds up (small ms) and when it reach an enough big number it is released disturbing the player speed between the affected frame and its neighbors. In general is not visible to the naked eye. If u're really focusing on it, it may be observed even with the vsync. Bigger fps should solve the situation

I made a project with the sprite and source code provided. I used "timeSinceLastUpdate" rather than "TimePerFrame" when calculating "currentPos", removed the "while (timeSinceLastUpdate >= TimePerFrame)" loop with its "timeSinceLastUpdate -= TimePerFrame;", lowered the speed, and got way more consistent results since there's no steps, but all distance delta is discharged into the "currentPos" every frame right away and no left over are allowed (the 0.01 ms I talked earlier)

Please when u're posting code make sure ur indenting is correctly used because it's painful to read until u copy the code into an IDE

Window / Re: Render Window Attempting to Reference deleted function
« on: March 21, 2020, 08:19:04 am »
So I know from other posts that this error occurs when you try to copy something uncountable. Problem is obviously that when I push back the window it copys it. Theoretically the solution to that is a vector of refrances. When I make it a vector of refrances and obviously push back refrances files xmemory and vector freak out, give >20 errors all saying that pointers to refrances are illegal.  How show should I go about fixing this or what is a different method to use.

If this doesn't work I am trying to make a void that spawns a new window and adds it to the vector. The vector runs in a void and functions like the while loop in just one window. I run the void from the while loop of a main window.

The Struct I've made
typedef struct KeyStruct {
    sf::Image Img;
    sf::Texture Tex;
    sf::Sprite Sprite;
    //there is more but its not necessary

Step Windows
static vector <sf::RenderWindow> WindowArray;
static vector <NewKey> KeyArray;
static void StepWindows()
    sf::Clock clock;
    int i;
    for (i = 0; i > WindowArray.size(); i++)
    if (clock.getElapsedTime().asMicroseconds() > 1000)
        WindowArray[i].setPosition(MakeKey::Gravity(WindowArray[i], KeyArray[i]));

Draw Key
static void DrawKey(string input)
    //Declair Key
    NewKey Key;
    NewKey& KeyRef = Key;
    if (input == "A")
    else if (input == "D")

    sf::RenderWindow window(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);
    sf::RenderWindow& windowRef = window;

And My Main
int main()
    sf::RenderWindow window(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            //Key Presses
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::A)
                else if (event.key.code == sf::Keyboard::D)
            if (event.type == sf::Event::Closed)
    return EXIT_SUCCESS;

Thanks for the help

In the code u provided u make a vector out of sf::RenderWindow. U could use pointers (sf::RenderWindow*) as one of solutions. In C++ the "=" is very vague to call "assigment operator" and this mostly used to explain stuff to beginners. Think of "=" as copy operator. It doesn't make sense to copy whole sf::RenderWindow, as u shown us

Graphics / Re: Problem with drawing shapes
« on: March 20, 2020, 10:51:20 am »

Im quite new to C++ and SFML. I was following some tutorials to draw shapes and get keyboard input to make those shapes move. I got everything working nice, at that point I had everything declared and executed in main() method.

Then I thought to make my code more modular and OOP, as soon as I did that -> placed shapes in Player class and keyboard input as a method in Player class and then called those methods exactly where that code was previously in main() method, shapes wont draw.

It must be something simple, there is no errors, but I cant figure it out. Am hoping someone could point out what is wrong. Drawing shapes in main still works as I tried to draw circle and its fine, just drawing from my Player class seems faulty.

Heres my code:

main() method

#include <iostream>
#include <SFML/Graphics.hpp>
#include <math.h>
#include "Player.h"

using namespace std;

int main() {

        sf::RenderWindow window1(sf::VideoMode(1024, 768), "Tutorial", sf::Style::Close | sf::Style::Resize);
        Player player;

        while (window1.isOpen())
                sf::Event evnt1;

                while (window1.pollEvent(evnt1))
                        switch (evnt1.type) {
                        case sf::Event::Closed:

                window1.clear(sf::Color(0, 150, 150));
                sf::Vector2i mousePos = sf::Mouse::getPosition(window1);


        return 0;

And Player class

#pragma once
#include <SFML/Graphics.hpp>
#include <math.h>
#define PI 3.14159265

class Player
        sf::CircleShape player;
        sf::RectangleShape sigth;
        void movePlayer();
        void sigthRotation(sf::Vector2i mousePos);

and Player.cpp

#include "Player.h"

Player::Player() {
        sf::CircleShape player (25);
        sf::RectangleShape sigth (sf::Vector2f(150.0f, 2.0f));

        player.setPosition(206.0f, 206.0f);
        sigth.setPosition(231.0f, 231.0f);

void Player::movePlayer() {

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                player.move(-0.1f, 0.0f);
                sigth.move(-0.1f, 0.0f);

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                player.move(0.1f, 0.0f);
                sigth.move(0.1f, 0.0f);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                player.move(0.0f, -0.1f);
                sigth.move(0.0f, -0.1f);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                player.move(0.0f, 0.1f);
                sigth.move(0.0f, 0.1f);

void Player::sigthRotation(sf::Vector2i mousePos) {
        sf::Vector2f linePos = sigth.getPosition();

        float dx = linePos.x - mousePos.x;
        float dy = linePos.y - mousePos.y;
        double rotation = (atan2(dy, dx)) * 180 / PI;

        sigth.setRotation((float)rotation + 180);


U're drawing the members of the Player obj, but they are not configured. When u're doing as example "sf::CircleShape player (25);" in the Player(), u're actually making a new sf::CircleShape, and not use the class member

General / Re: Is it possible to have an array/vector of Textures?
« on: March 18, 2020, 09:54:04 am »
Is it possible to have an array/vector of Textures or do they have to be Sprites?

U can have array / vector of sf::Texture. Pay attention at how u use the copy constructor. U may want a vector of pointers

If I'm not wrong, theoretically u can have a vector of any SFML resource

Graphics / Re: Drawing a class object before creation
« on: March 14, 2020, 02:01:24 pm »
I know that logically I have to create something before using it, but do I always have to use global containers in cases like this?

Is there some way to draw the object only if it exists? (bools don't help here) Can you point me to an example where it is shown how a forward declaration of a class object in a case like this looks like.  With variables and functions it's easy.....but....

There are no similar examples on the internet for what I like to do. My searches always lead to examples, which don't help. Certainly it's some specific syntax. If I had found it, I wouldn't have asked my question in the first place.

As I said before, "forward declaration" is a completely different concept, and most probably won't lead to desired results. Class objects are variables themselves. This is a solution to ur code (example):

Code: [Select]
UserInterface* uio1 = nullptr; // Declare the variable before using it
while(window.isOpen()) {
    // When pressing A
    uio1 = new UserInterface("ui.png", 100, 100);

    // When pressing B (u edited the post as I remember)
    delete uio1;
    uio1 = nullptr;

    // When drawing
    if(uio1) // Aka is not nullptr

Probably the problem u faced is that when u declare it, an obj is made instantly. That's true, and to go around this I made use of pointers

Graphics / Re: Drawing a class object before creation
« on: March 14, 2020, 12:18:22 pm »
I'm struggling with drawing a class object, which is to be created during runtime by hitting key A.

Creating is no problem, but how to draw it as it is not created yet. With vectors it's no problem, but how would you handle this case? As I understand it I have to forward declare the object somehow, but how?

#include <SFML/Graphics.hpp>
#include <iostream>

class UserInterface
        sf::Texture uitexture;
        sf::Sprite uisprite;

        UserInterface(std::string loadFromFileString, float x, float y)
                this->uisprite.setPosition(x, y);

                std::cout << "deleted" << std::endl;

        void draw(sf::RenderTarget& target)

int main()
        sf::RenderWindow window(sf::VideoMode(800, 500), "Map", sf::Style::Close);

        while (window.isOpen())
                sf::Event event;
                while (window.pollEvent(event))
                        switch (event.type)
                        case sf::Event::Closed:
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::A)
                                        UserInterface* uio1 = new UserInterface("ui.png", 100, 100);
                uio1.draw(window);           //   how to draw that?

U indeed have to forward declare it, but it has nothing to do with forward declaration concept, so what u need is just the fact that u need to declare a variable before u can use it. Logic right? U declare ur variable inside an "if" statement. When the "if" statement ends, "uio1" will be deleted (the pointer, not the underlying obj) because it goes "out of scope", which means that when u do "uio1.draw(window)", "uio1" does not exist. U should read more about C++, or do tutorials, or ask ur tutor / teacher if u have one, since this is a simple concept and the already given information shall point u in the right direction. Try to research the problem and come back with questions

Graphics / Re: my animation function is not working
« on: March 14, 2020, 11:10:03 am »
"source1" is never 0, which means the first frame will never be hit, maybe except when u construct ur player

If entering the "walk()" "source1" is 300, it will increase to 350, which will result in a rectangle with left margin 350 and right margin 400, exactly 1 frame after ur spritesheet ended. Use "if(source1 == 300)" rather than "if(source1 == 350)"

U're updating the current frame when 1 sec has elapsed. To make animation faster u need to update the frame faster, like using .5f rather than 1.f

Pages: [1] 2 3