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 - HaS3n

Pages: [1]
1
Network / Re: Packet transmit wrong data (?)
« on: March 12, 2022, 12:38:56 pm »
Okay problem fixed, the topic can be closed
The mistake was here ->> if (!initpacket >> connectionstatus)
combined with not clearing the packet before giving him next data.

2
Network / Re: Packet transmit wrong data (?)
« on: March 10, 2022, 10:28:39 pm »
okay, i found one problem, server added data to the packet without cleaning it so the reply packet contained something like this (photo 1). I have fixed it, but it didn't fix the problem.
Client sends message (photo 2) and then server replies with correct data (photo 3), but then client just..ignores it? its not updating its variable (connectionstatus) so it remains 0 (I have added setting variable to 0 and then updating it with received packet, but it remains 0 for some reason... Server however is working perfectly fine for me).

Client code here:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <windows.h>
#include "TextureHolder.h"

int main()
{      
       
        sf::UdpSocket socket;
        sf::IpAddress sendingip;
        unsigned short sendingport;
        std::cout << "IP: ";
        std::cin >> sendingip;
        std::cout << "Port: ";
        std::cin >> sendingport;
        unsigned int receivingport = socket.getLocalPort();
        if (socket.bind(receivingport) != sf::Socket::Done) {
                std::cout << "Failed to bind a socket!" << std::endl;
                exit(4);
        }
        sf::Packet initpacket;
        int connectionstatus = 1; //0-brak 1-proba 2-nawiazana

       
        initpacket << connectionstatus;
        if (socket.send(initpacket, sendingip, sendingport) != sf::Socket::Done) {
                std::cout << "Failed to send connection initialization packet!" << std::endl;
                exit(2);
        }
       
        sf::IpAddress senderip;
       
        unsigned short senderport;
        Sleep(1000);
        if (socket.receive(initpacket, senderip, senderport) != sf::Socket::Done) {
                std::cout << "Failed to recieve initialization packet!" << std::endl;
                exit(2);
        }
        connectionstatus = 0;
        if (!initpacket >> connectionstatus) {
                std::cout << "Failed to read initpacket!" << std::endl;
                exit(2);
        }
        std::cout << "CONNSTATUS: " << connectionstatus << std::endl;
        if (connectionstatus != 2) {
                std::cout << "Connection not stablilized!" << std::endl;
                exit(3);
        }
       
        //inicjalizacja wszystkiego
        TextureHolder TXHOLDER;
        sf::RenderWindow _window(sf::VideoMode(800, 800 ), "Chess", sf::Style::Titlebar | sf::Style::Close);
        _window.setFramerateLimit(75);
        sf::Sprite bG;
        bG.setTexture(TXHOLDER.bg);
        sf::Event event;

        while (_window.isOpen()) {

                while (_window.pollEvent(event)) {

                        if (event.type == sf::Event::Closed) {

                                _window.close();
                        }






                }


                _window.clear(sf::Color::White);
                _window.draw(bG);
                _window.display();
        }//isopen

        return 0;
}

Server code here:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <windows.h>
int main()
{      
        int connectionstatus = 0;
        sf::Packet initpacket;
        sf::UdpSocket socket1;
        unsigned int receivingport = 57000;
        unsigned short senderport;
        sf::IpAddress senderip;
        if (socket1.bind(receivingport) != sf::Socket::Done) {
                std::cout << "Failed to bind a socket!" << std::endl;
                exit(4);
        }
        if (socket1.receive(initpacket, senderip, senderport) != sf::Socket::Done) {
                std::cout << "Failed to recieve initialization packet!" << std::endl;
                exit(2);
        }
        initpacket >> connectionstatus;
        std::cout << connectionstatus << std::endl;
        std::cout << senderip<<"||"<<senderport<<std::endl;
        if (connectionstatus == 1) {
                connectionstatus = 2;
                initpacket.clear();
                initpacket << connectionstatus;
                if (socket1.send(initpacket, senderip, senderport) != sf::Socket::Done) {
                        std::cout << "Failed to send connection initialization packet!" << std::endl;
                        exit(2);
                }
        }
        system("pause");
        return 0;
}

3
Network / Re: Packet transmit wrong data (?)
« on: March 10, 2022, 08:57:14 pm »
You need to check the status returned by bind() to ensure that it was successful.
Then you also need to handle certain failure states instead of simply terminating the application.

Binding a port may take a bit of time, so when you immediately respond, the client might not be ready yet and it would seem like things fail.

Also don't forget all the fun topics like NAT and port forwarding, when it comes to internet connections: https://www.sfml-dev.org/faq.php#network-internet-network

I highly recommend to use Wireshark, to see what's going on on the wire, that way you'd at least know if/when stuff is or isn't sent.
For now, server and client are on the same computer.
Also, the code will be totally reworked to its final form, but thats my test, because this is my first time with SFML network.
I will check wireshark and this binding problem and I will update this topic.

4
Network / Packet transmit wrong data (?)
« on: March 09, 2022, 05:54:57 pm »
Hi, I would like to do an online chess for only 2 players, however, packet with connectionstatus variable seems like not changing connectionstatus on client side. Where is the problem? I can't find it...
1st attachment is client side
2nd is server side
Take a note that the whole code will be reworked, this is only a test but i cannot get further without solving this now.
TIA

5
General / Re: Loading texture into class
« on: March 09, 2021, 09:30:07 pm »
Yes! They are right! Everything is working now! Thank you and Discord people! I really appreciate your help. Many thanks! :)

6
General / Re: Loading texture into class
« on: March 09, 2021, 06:16:36 pm »
https://www.sfml-dev.org/tutorials/2.5/start-vc.php
Code from here (this code WORKS, your didn't).
Drivers are latest. Results as expected: nothing :(
Tried different file formats: nothing.
are the VS 2017 version compatible to VS 2019? Maybe that's a problem?
Also i can try setting up SFML one more time in a different project, Can you tell me tutorial how to do this in a proper way? (Or i can pick random from internet...)
TIA

7
General / Re: Loading texture into class
« on: March 09, 2021, 01:56:53 pm »
SFML is 2.5.1
I have followed YT tutorial (I forgotten the video :c). Basically I put SFML folder on C drive and then linked everything to VS. I have downloaded the 2017 version (I am using 2019...). Is it making those troubles? (Guy on video did the same...). I am working on Windows 10 64bit (but Im "developing" SFML on x32). (Note that test code from SFML main page run perfectly)

8
General / Re: Loading texture into class
« on: March 09, 2021, 08:46:45 am »
No,its not working. No matter if file exists or not. If not program should just stop working in a "peaceful" way, but it's crashing instead.Every time exception shows "Access violation while reading localization*HEX NUMBERS*"

9
General / Re: Loading texture into class
« on: March 08, 2021, 11:06:22 pm »
CPP FILES:

10
General / Re: Loading texture into class
« on: March 08, 2021, 11:05:43 pm »
You want my VS project file? Here it is. (note that I am not fully familiar with English nomenclature,so sometimes i can misunderstand some sentences. I really apologise :v)
Also code is slightly different. I moved texture from class (i realized putting it inside class is not very smart-every object will contain texture and this is waste of memory). texture object is now inside main function and Riflemans class constructor takes pointer to texture object and setting sprite texture. But problem is the same. The exception shows off at line of loading texture.
 

11
General / Re: Loading texture into class
« on: March 08, 2021, 05:15:30 pm »
I think no. I did test- I wrote direct path to directory on desktop and tried different images. Still nothing :/
I can make video of it.

12
General / Re: Loading texture into class
« on: March 08, 2021, 12:50:01 pm »
#pragma once
enum class Nation{USA,RUS};
enum class Unittype{INFANTRY,LIGHT,TANK,HELICOPTER};


class Unit
{
public:
        Unit(int at, int pe, int ar, int du, Nation na, Unittype ty);
        //~Unit();
        int PENATK(int &pen);
        bool takeDMG(int pen, int atk);

private:
        int attack;
        int penetration;
        int armor;
        int durability;
        Nation nationality;
        Unittype type;
};

Unit.h

Unit.cpp
#include "Unit.h"

Unit::Unit(int at, int pe, int ar, int du, Nation na, Unittype ty) {
        attack = at;
        penetration = pe;
        armor = ar;
        durability = du;
        nationality = na;
        type = ty;
}
int Unit::PENATK(int& pen) {
        pen = penetration;
        return attack;
}
bool Unit::takeDMG(int pen, int atk) {
        if (pen > armor)
                durability = durability - atk;

        if (durability <= 0)
                return 1;

        else
                return 0;
}


EDIT:
main.cpp will be useful too i believe:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Infantry.h"
#include "Unit.h"

int main() {
        sf::RenderWindow _window(sf::VideoMode(1024,768), "sfml", sf::Style::Close | sf::Style::Titlebar);
        _window.setVerticalSyncEnabled(true);
        sf::Event evnt;
        Riflemans rf(10,10,10,10);

        while (_window.isOpen()) {
                while (_window.pollEvent(evnt)) {
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                _window.close();
                                break;
                        default:
                                break;
                        }//switch






                }//while
       
       
       
       
       
                _window.clear(sf::Color::Blue);
                _window.draw(rf);
                _window.display();
        }//petlagry

       



        return 0;
}

13
General / Loading texture into class
« on: March 08, 2021, 10:49:09 am »
Hey,I have a problem on loading texture inside custom class. Its probably a pure C++ Problem (not SFML) but i am losing my mind trying to solve this.
#pragma once
#include "Unit.h"
#include <SFML\Graphics.hpp>
#include <string>
#include <iostream>

class Riflemans : public Unit, public sf::Drawable
{
public:
        Riflemans() = delete;
        Riflemans(int at, int pe, int ar, int du, Nation na=Nation::USA, Unittype ty=Unittype::INFANTRY)
        : Unit(at, pe, ar, du, na, ty)
        {
                if (!m_texture.loadFromFile("a.png"))
                {
                        std::cout << "BYE" << std::endl;
                        exit(1);
                }
                m_sprite.setTexture(m_texture);
       
       
        }

        ~Riflemans(){}
private:
        sf::Texture m_texture;
        sf::Sprite m_sprite;
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
                target.draw(m_sprite);
        };

};
 

This is Infantry.h file. Everything is compiling,etc but when executed whole program goes down due"Access violation while reading". VS shows the error
if (!m_texture.loadFromFile("a.png"))
<-here

I was trying to debug and if I'm correct it cannot read m_texture properly of something. I am pretty sure this is a very obvious error in code but i am just stuck :v
TIA

Pages: [1]
anything