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
16
Graphics / Re: Do these three features exist?
« on: March 08, 2020, 08:28:18 pm »
If u want to move multiple items together, just have all the related items stored somewhere, and when moving one of them, move everyone. The same for rotation. I think I know what u mean that the shape after rotation is not the right one. It has to do with rotation origin. If I'm not wrong, SFML allows u to set origins outside of sprite boundaries, which means u need to set child items origins to the main one. If SFML doesn't allow it, but I doubt, after every rotation u have to reposition the items (which probably is what Sentinent said)

17
System / Re: How to restart the clock when it gets to a value
« on: March 08, 2020, 06:54:14 pm »
On a key press?

When u start moving. If u start on a key press, then yeah

18
Graphics / Re: Do these three features exist?
« on: March 08, 2020, 10:05:31 am »
Hello,
I was very exciting to discover SFML and the world of programming,
Now i have few quick questions if someone expert knows the answer

1) Is it possible to draw an object (with paint) and use some kind of tool/programm to transform it into a real in game item with the exact shape you have drawn?  (meaning the item will have the collision area you have drawn)

2) Is there a function to merge items/objects into 1 item? (example : 2 circles meet each other (one circle hits the other), resulting in making one single item with a shapre of "8")

3) Is it possible to give an item, let's say a rectangle, 4 different kind of interactions with other items depending on which side of the rectangle was hit by another item? (So if a user rotated the rectangle to make it hit with the side number 2 or 3, the result would change, my question is, can SFML differenciate between the 4 sides of the rectangle?)

Thank you
And a have a good day
P-

SFML is more a wrapper to make things easier rather than an engine. With this said:

1) The shape u drawn will always be a perfect rectangle, since there are pixels with alpha 0, but not non-existent, and SFML takes a sprite as a rectangle. On the second hand SFML does not have a collision system. What is rn available is just classic math boundary checking. U should implement ur own system if u want advanced collision

2) I don't see why wouldn't do that urself. Just delete one and modify the other one, no need for such techniques like merging

3) U can check what margin was been hit, taking in account sprite rotation

19
System / Re: How to restart the clock when it gets to a value
« on: March 08, 2020, 08:07:05 am »
I'm multiplying the movement in my game by the time since the last time movement was applied, which causes problems when you don't do anything for a while. Doing this:
if (loopclock.getElapsedTime().asSeconds() > 1){loopclock.restart();}
doesn't work for some reason, another solution to my movement system would also be appreciated.

To me looks like u let the clock still going while u are not moving. Make sure to also restart the clock when u start moving

20
Graphics / Re: Circles and ellipses are too edgy (liny)
« on: March 06, 2020, 06:39:31 am »
Hello,
I'm making program that can draw stuff. Problem comes when I draw circles and ellipses. As long as they're small in size it's ok but when I resize it the lines from which are circles made starts to be very visible. I've tried AA but haven't seen any difference. Is there any way to solve this problem or maybe add more lines to shape ?

Default number of points is 30 (at least on CircleShape I tested). It is normal that when u scale it way bigger to be visible. Increase the number of points when making the obj or use .setPointCount()

21
General / Re: Event problems.
« on: March 05, 2020, 10:08:43 pm »
OMG. Thank you very much. I wrote the code like this because I was in a hurry. But what I want to tell is this: Watch out for 14th and 28th seconds. Why has the position not changed? Look at the text "clicked" .Thanks again.

https://streamable.com/4xdw2

As I mentioned in one of the comments, it's possible that the next random position to be the exact last one, which visually looks like nothing happened, but the "cout" notices u that the change was made

22
General / Re: Event problems.
« on: March 05, 2020, 02:55:38 pm »
My translation skills are not good. So I will summarize my problem again. Thank you for the answer. If you want to find a solution to the problem, you can help me with an edit on the code. My problem is that if we click the mouse, the position of the circle that randomly changes position does not detect that we click on it if it is in the position immediately after the previous one. I hope I explained.

I solved the problem and refactored the code so u will have an example of good practices. Maybe u're a beginner, but u will see that an organized code is suggestive and tells u by itself how to optimize and debug. Make sure to read every comment (if possible copy the code in an editor, don't read there)

// Cleaned the headers
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
using namespace sf;
int main() {
    // In this block comes the bug fix, please first accustome to the rest of the code, run it, and when u're done uncomment the line below
    // srand(time(nullptr)); // Maybe u would like to edit the line so u don't get the the warning when compiling
    // There was no randomness because the seed was not set. Permanent random sequence started with 8 9 9 1
    // U may consider to get random positions until it is not equal to the previous one, so u don't get repeated ones

    sf::RenderWindow window({400, 400}, "beta");
    window.setFramerateLimit(60); // Don't burn ur GPU

    sf::CircleShape slot(10);
    auto emptySlotColor = Color::Green;
    slot.setFillColor(emptySlotColor);
    slot.setOrigin(7, 10);

    CircleShape spinSprite(10);
    spinSprite.setFillColor(Color::Red);
    spinSprite.setOrigin(7, 10);

    vector<sf::CircleShape> slots;
    for(auto& pos : vector<Vector2f> {
        {205, 40},
        {286, 62},
        {347, 123},
        {369, 205},
        {348, 287},
        {286, 347},
        {205, 369},
        {122, 347},
        {62, 286},
        {41, 206},
        {64, 124},
        {124, 64}
    }) {
        slot.setPosition(pos);
        slots.push_back(slot);
    }

    int markedCursor = 0;
    int spinCursor = 0;
    float switchEvery = 1; // Secs
    auto switchSlot = [&] { // I'm using a lambda so I won't change the vars arrangement, so u can recognize the code and compare it. A strong typed fct would've been better
        markedCursor = rand() % (slots.size() - 1); // Make the max number dependent, so if u change the number of them u don't have to update here
        cout << markedCursor << endl;
        slots[markedCursor].setFillColor(Color::Cyan);
    };

    switchSlot();
    spinSprite.setPosition(slots[0].getPosition());

    Clock clock;
    Event event;
    while (window.isOpen()) {
        // First time poll events before changing states
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)
                window.close();
            else if(event.type == Event::MouseButtonPressed)
                // This is the right way to check events
                // If the button is left but pos differents, the code will do a useless check for right. Chosed this design for readability
                if(event.mouseButton.button == Mouse::Left && spinSprite.getPosition() == slots[markedCursor].getPosition()) { // Vector2f has == operator
                    slots[markedCursor].setFillColor(emptySlotColor); // If u use the same value in 2 locations, use a var (const how people prefer)

                    // No need to store a int (it should've been a bool), do the stuff right away
                    // It wouldn't work with previous event handling methods
                    switchSlot();
                }
                else if(event.mouseButton.button == Mouse::Right) {
                    switchEvery -= (switchEvery < .1f ? .01f : .1f); // One line
                    cout << switchEvery << endl;
                }
        window.clear();

        // Do the check in one line. Good formatted code doesn't need (in general) explications
        if (clock.getElapsedTime().asSeconds() >= switchEvery) { // I'm quite sure u want >= rather than >
            clock.restart();
            spinCursor = (spinCursor == 11 ? 0 : spinCursor + 1); // One line, easier to read

            // Don't follow this with another "for" loop. U already have everything u need
            // Don't do every frame either, u wanna change the pos only when u're in this "if"
            spinSprite.setPosition(slots[spinCursor].getPosition()); // Interchanging positions doesn't need to explicit both x and y
        }

        for(auto& slot : slots)
            window.draw(slot);
        window.draw(spinSprite); // U should use same technique as the cyan circle

        window.display();
    }
}
 

23
General / Re: Event problems.
« on: March 04, 2020, 07:18:43 am »
Yes. I read what you say. Thanks but this project was a little urgent. Either I need to pay attention to urgent projects or arrange a little while asking for help because those who have difficulties in understanding do not could to help. Also, as far as I understand from your last sentence, holding the mouse down does not triggered tons of times if in the event loop.

Actually I want to say:
Consider two circles. One of them is moving and the other is constant position. When the moving circle over to the fixed one, you click the mouse and the fixed one teleports to a random position. However, if the incoming position is the next position of the circle, it does not detect when you click. It is necessary to wait for the circle to return one turn.

If u have stuff in event loop DOES NOT mean it will be executed only when the desired event triggers. I expect in ur code that if u keep the mouse pressed and move it (which triggers move events), the behaviour that I explained earlier will happen, contrary to what u just mentioned

What u mean by "return"? Like u have to do a useless click before so the next one can count?

24
General / Re: Event problems.
« on: March 03, 2020, 09:35:11 am »
Hello. In the application I am trying to make, I move a circle in certain positions. And there is one blue (anacircle) circle. When we hover over and click on this blue circle, the location of the blue circle will change. Okay, so far, everything is normal. However, when the next position of the blue circle is very close to its previous position and I try to click it, it does not detect the second click. In order to perceive the click, the "gezgin" circle must return one more turn. Why? Sorry for my bad English.

This happens only when I speed up the "gezgin" circle . And I'm sure I clicked, I get output with the cout.

I put their variable names according to my head, they don't represent anything.

Code:
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <Windows.h>
#include <iostream>
 
 
using namespace std;;
using namespace sf;;
 
 
 
int main()
{
        sf::RenderWindow window(sf::VideoMode(400, 400), "beta");
 
        sf::CircleShape shape(10.f);
        shape.setFillColor(sf::Color::Green);
        shape.setOrigin(7.f, 10.f);
 
        CircleShape gezgin(10.f);
        gezgin.setFillColor(Color::Red);
        gezgin.setOrigin(7.f, 10.f);
 
        vector <sf::CircleShape> anacircle;
        shape.setPosition(205, 40);
        anacircle.push_back(shape);
        shape.setPosition(286, 62);
        anacircle.push_back(shape);
        shape.setPosition(347, 123);
        anacircle.push_back(shape);
        shape.setPosition(369, 205);
        anacircle.push_back(shape);
        shape.setPosition(348, 287);
        anacircle.push_back(shape);
        shape.setPosition(286, 347);
        anacircle.push_back(shape);
        shape.setPosition(205, 369);
        anacircle.push_back(shape);
        shape.setPosition(122, 347);
        anacircle.push_back(shape);
        shape.setPosition(62, 286);
        anacircle.push_back(shape);
        shape.setPosition(41, 206);
        anacircle.push_back(shape);
        shape.setPosition(64, 124);
        anacircle.push_back(shape);
        shape.setPosition(124, 64);
        anacircle.push_back(shape);
 
        Clock clock;
        int random = 0;
        int que = 0;
        int degis = 0;
        int zamanlayici = 1000;
        while (window.isOpen())
        {
                Event olay;
 
                Time zaman = clock.getElapsedTime();
                int speed = zaman.asMilliseconds();
 
                if (que == 0)
                {
                        random = rand() % 11;
                        cout << random << endl;
                        que = 1;
                        anacircle[random].setFillColor(Color::Cyan);
                }
 
                if (speed >= zamanlayici)
                {
                        clock.restart();
                        degis++;
                        if (degis == 12)
                                degis = 0;
 
                }
                for (int i = 0; i < 12; i++) {
                       
                        if (degis == i) {
                                gezgin.setPosition(anacircle[i].getPosition().x, anacircle[i].getPosition().y);
                               
                       
                        }
 
                        else if (zaman.asMilliseconds() >= 12000)
                        clock.restart();
 
        }
 
 
                sf::Event event;
                while (window.pollEvent(event))
                {
                       
                        //      if (event.type == sf::Event::MouseButtonPressed)
                //      {
                        //      if (event.mouseButton.button == sf::Mouse::Left){
                        if(Mouse::isButtonPressed(Mouse::Left)){
                                        if (gezgin.getPosition().x == anacircle[random].getPosition().x && gezgin.getPosition().y == anacircle[random].getPosition().y) {
 
                                                que = 0;
                                                anacircle[random].setFillColor(Color::Green);
//
                                        }
                           }
                        //      if (event.mouseButton.button == sf::Mouse::Right)
                                                if(Mouse::isButtonPressed(Mouse::Right))
                                {
                                        if (zamanlayici <= 100)
                                                zamanlayici -= 10;
 
                                        else
                                                zamanlayici -= 100;;
                                        cout << zamanlayici << endl;
                                }
                       
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
 
 
                window.clear();        
                for (int i = 0; i < anacircle.size(); i++) {
                        window.draw(anacircle[i]);
 
                }
                window.draw(gezgin);
                window.display();
 
 
        }
 
 
        return 0;
}

Before pointing what could be wrong is worth to note that the way u are developing is faulty and may make someone who reads the post to go insane or point wrong solution. Before responding back, please change ur codebase by following these tips which "in general" are good practice:
- don't write code in ur native language; this is the perfect example why u shouldn't: others can't understand, be they helpers on forums or people in a possibly company
- don't name variables with descriptive names if they don't describe the object; is better to just name "a", "b" or "tmp" so someone who reads the code doesn't associate by mistake; these names shall be temporary and changed when u found out a good description for them
- try to avoid excesive nesting when u can; most of times u can combine 2 "if" statements in only 1
- for boolean vars use "bool", not "int"; don't threat memory as inexistent (4 bytes rather than 1 bit), but don't do premature optimizations either (short - 2 bytes)

Ur uncommented way of checking if a mouse button is clicked is faulty, as u're not making use of "event" at all. If u keep it pressed, doesn't it gets triggered tons of times? It says in documentation that Mouse class is independent of events

25
General / Re: Debug works perfectly, Release works halfway?!
« on: March 01, 2020, 08:17:19 am »
Hi everyone!
I'm developing a raycasting game engine in CodeBlocks with SFML, surely it's not perfect but it works well, at least the Debug executable AND in "Debug mode" in CodeBlocks as you can see here:
(click to show/hide)

But in Release executable and "release mode" of CodeBlocks it works half way, in fact the wall rendering works perfectly and the framerate doubles but floor and ceil isn't rendered, to be precise
its rendered only maybe ten pixel height stripe in the middle of the window:
(click to show/hide)

Searching around i read that can depend on which libraries are are used, here's mine:
(click to show/hide)
Previously the first five was followed by "-d" (for "debug" i think) but it works the same without too.
There's no other libraries in the linker of "build options.." of the project.


I will be very glad if someone can help me to make a working Release!

PS
Please don't mind the swastika, i'm using, for testing, the textures of the old PC game "Wolfenstein 3D" by "id software"!

Do use the "-d" libraries only for debug. Even if they work viceversa as u said, it's still a good practice to use the right ones, otherwise sometimes out of nowhere things don't work anymore

Make sure the libs are the right ones, then take a look at the console, it could show u what failed

26
Network / Re: Ignore packet
« on: February 29, 2020, 04:09:03 pm »
The code:
struct PacketData
{
 ClientData clientData;
 uint8_t header[1];
 sf::Packet packet;
 sf::Socket::Status status;
 time_point recieveTime;
};

...

while (!m_shutdown)
{
 if (!m_selector.wait(sf::seconds(1.0f)))
 {
  continue;
 }

 {
  size_t recieved;
  std::lock_guard<std::mutex> lock(m_socketMutex);
  packetData.status = m_socket.receive(packetData.header, 1, recieved, packetData.clientData.address, packetData.clientData.port);
 }

 const auto& isOnIgnoreList = std::find_if(m_ignoreList.begin(), m_ignoreList.end(), [&packetData](const ClientData& clientData) {return packetData.clientData == clientData; }) != m_ignoreList.end();

 if (isOnIgnoreList)
 {
  // HERE! How to ignore the packet without recieving it?
  continue;
 }

 ...
}
 

I'm trying to implement a "IP ban", and my question is: does SFML allow to somehow ignore the packet without actually receiving it? Because if I just ignore the packet, the selector.wait will return true all the time as there's data to read.

Also just noticed, the first .recieve returns an error and reads 0 bytes... why? There's only one socket in selector.

The documentation doesn't say anything about blocking. U can do one of these 2 things:
- check the ip manually (not recommended due speed; it could been the way SFML would do that anyway if this was a core feature)
- modify one of the active firewalls (the closer to the global infrastructure the better; example: block on the router rather than ur pc if the router is dedicated for the server, so the packet won't reach the pc)

27
Thx for helping! I forgot to check my code with a minimal example. Seems like the problem was since I was doing my window recreation after stuff was already drawn on it:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <imgui/imgui.h>
#include <imguiSFML/imgui-SFML.h>
int main() {
sf::RenderWindow window(sf::VideoMode(1600, 900), "Window");
ImGui::SFML::Init(window);
auto bg = new sf::Sprite;
{
auto tex = new sf::Texture;
tex->loadFromFile("resources/bg.png");
bg->setTexture(*tex);
}
sf::Event event;
sf::Clock clock;
while (window.isOpen()) {
while (window.pollEvent(event))
ImGui::SFML::ProcessEvent(event);
ImGui::SFML::Update(window, clock.restart());
window.clear();
window.draw(*bg);
ImGui::Begin("Inside window");
if (ImGui::Button("Test"))
window.create(sf::VideoMode(800, 450), "Window");
ImGui::End();
window.pushGLStates();
ImGui::SFML::Render();
window.popGLStates();
window.display();
}
}

Changing it to this solved the problem:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <imgui/imgui.h>
#include <imguiSFML/imgui-SFML.h>
int main() {
sf::RenderWindow window(sf::VideoMode(1600, 900), "Window");
ImGui::SFML::Init(window);
auto bg = new sf::Sprite;
{
auto tex = new sf::Texture;
tex->loadFromFile("resources/bg.png");
bg->setTexture(*tex);
}
sf::Event event;
sf::Clock clock;
while (window.isOpen()) {
while (window.pollEvent(event))
ImGui::SFML::ProcessEvent(event);
ImGui::SFML::Update(window, clock.restart());
window.clear();
ImGui::Begin("Inside window");
if (ImGui::Button("Test"))
window.create(sf::VideoMode(800, 450), "Window");
ImGui::End();
window.draw(*bg);
window.pushGLStates();
ImGui::SFML::Render();
window.popGLStates();
window.display();
}
}

I also forgot to mention that on my original code the window was recreated in the main thread, not the network one. I will update the title for this post to be more suggestive about the problem so other people can find it easier

28
General / Re:basic text box
« on: February 14, 2020, 07:38:32 am »
Just when I thought it was safe to go back into the water...<i'm returning this to 'unsolved'>. I originally thought I was doing just that with the line "if(text.unicode < 128)", until I saw that the Unicode for the 'BackSpace' key is '8'. And nothing I've seen yet on the internet is very clear, so just how DO I filter out non-printable characters?

Take a look at the ASCII table

29
So, here's the whole code: https://github.com/Neroooooooo/sfmlNetworking

I posted the github repo link because it's probably more organized. And there are just about 200 lines among 5 files, some of them being comments and cout's, so it shouldn't be hard to understand what's there.

The thing I want to achieve is a server that can receive packets from clients through a TCP connection. Currently, it only works if both the client and the server are on the same machine and I use the local IP to connect. It doesn't even work on 2 different machines connected to the same router.

I tried to forward the port 20000 (used in the code), and connect to the server using the public IP address, but it doesn't work.
https://imgur.com/a/QDhlwAy

I disabled my windows 10 firewalls, it still didn't work.

Can you guys help me here? I'm stuck and I searched everywhere for a solution, but I can't find one. I really want to learn about multiplayer games and networking, but I can't see what I'm doing wrong.


EDIT: I played some more with firewalls, and now it works locally, on 2 different machines connected to the same router. But it does not work with someone that's not connected to the same router. Ideas?

Haven't took a very deep look, but since it works on same router and not globally the problems may be:
- ur server is not good connected (I listen to local router address and forward it, not connect to public one, deppending on my setup; try both)
- ur client doesn't connect to the public address
- the router is not forwarding the port
- client and server ports doesn't match
U could also try a scenario with a real server probably bought (VPS)

30
Window / How to recreate the window when using SFML ImGui?
« on: February 11, 2020, 07:46:40 pm »
I want to add to my game the functionality to change window stuff, and in this example I will refer to resolution. I did it some months / years ago, but now I'm failing. Below is some code to understand what I'm doing. Comments before each block exaplains where the calls happen

Code: [Select]
// Beginning of main()
scene::window = new sf::RenderWindow(sf::VideoMode(1600, 900), "Test");
scene::view = new sf::View(scene::window->getView());
scene::view->setSize(sf::Vector2f(scene::size.x, scene::size.y)); // Pixel-art game, so I want bigger units (size.x < 1600)

// In a network thread
void setPos(float x) {
player->sprite->setPosition(x, scene::band * 1.5f);
scene::view->setCenter(x, 0);
scene::window->setView(*scene::view); // All of these makes the camera follow the local player; also is the first one who sets a custom sf::View on the window
}

// After handling window events, clearing the screen, drawing SFML related stuff, but before drawing (ImGUI is a lib that handles button clicks at the same time with drawing, and I draw ImGui stuff after all my sprites); happens on a click
scene::window->create(sf::VideoMode(900, 900), "Test");

The usage of network *thread* doesn't seem to have negative impact, since before pressing everything works right. What happens: after the last block is ran, the window is recreated with the right resolution, but everything is black. I tried resetting the view:

Code: [Select]
scene::window->create(sf::VideoMode(900, 900), "Test");
scene::view = new sf::View(scene::window->getView());
scene::view->setSize(sf::Vector2f(scene::size.x, scene::size.y));
scene::window->setView(*scene::view); // I assure that at (0, 0) is stuff drawn

I tried recreatting a sprite with its texture:

Code: [Select]
scene::window->create(sf::VideoMode(900, 900), "Test");
scene::view = new sf::View(scene::window->getView());
scene::view->setSize(sf::Vector2f(scene::size.x, scene::size.y));
scene::window->setView(*scene::view);

// The block is copied from the initial generation, only the final instruction is changed from push_back() to modifying the original one, so I can assure there's no problems with loading it
auto bgTex = new sf::Texture;
bgTex->loadFromFile("texs/bg.png");
auto bg = new sf::Sprite(*bgTex);
bg->setPosition(sf::Vector2f(-scene::size.x / 2.f, -scene::size.y / 2.f));
scene::metal[0] = bg; // The background is supposed to take the whole screen on (1600, 900)

The same result. If it helps, ImGui actually still works like nothing happened, it's just on top of a black screen. Everything is up to date, running on VS2019, SFML_STATIC. I have another SFML instance in the system background since the server uses its network capabilities (no window)

Pages: 1 [2] 3