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

Pages: [1] 2
1
Network / Re: Receiving data
« on: December 22, 2017, 06:16:02 pm »
Future Kerachi: There is a getline(std::cin,s); which will block your program, although I can not see any other big mistake, keep up the good work! :)

2
Network / Re: Receiving data
« on: December 22, 2017, 08:57:58 am »
The Client can send data to the server, the client works fine, but the server can not perceive it.

this part:
Code: [Select]
if(client->receive(packetReceive) == sf::Socket::Done){
                        if(packetReceive >> msg)
                        {
                            if(*it != msg && !msg.empty())
                            {
                                std::cout <<std::endl<< client->getRemoteAddress()<<": "<< msg << std::endl << "Me: ";
                                *it = msg;
                            }
                        }
                    }
Won't run, if I do not send data with the server to the client.
Basicly the if is false, even if it must be true.

3
Network / Re: Receiving data
« on: December 21, 2017, 09:23:39 am »
I'm connecting to the server with a client successfully, and the server is printing out:
New Client: 127.0.0.1

If I'm closing a client the server prints out successfully:
Clinet Disconnected: 127.0.0.1

But whenever I send data to the server, the server should print out something like this:
"127.0.0.1: hello
Me:"
But it doesn't, only if I send data to the client before the client does.
Example:

Server send: "Hello Client"
Client send: "Hello Server"
Server print out: "127.0.0.1: Hello Server
Me:"
Client print out: "127.0.0.1: Hello Client
Me:"

However, if I do this:
Client send: "Hello Server"
Server DOES NOT print out: "127.0.0.1: Hello Server
Me:"

So as I experienced, after I send data to the client with the server, then the server will print out the last message, which arrived.

By the way, I'm using Windows 10 with Visual Studio 2017 Community.

I'm still trying to figure out what it the problem, without any success yet :(

4
Network / Re: Receiving data
« on: December 19, 2017, 10:39:56 am »
eXpl0it3r, I read some of https://github.com/SFML/SFML/tree/master/examples
And, yes I did read those tutorials, and I agree... but ehh then lets see my very basic server example:

All it can do:
  • acceptin connections
  • receiving message & print it (Won't work, if you don't send data)
  • delete disconnected sockets

Code: [Select]
#include <iostream>
#include <SFML/Network.hpp>

std::string msgSend;
//Since all the client constantly spamming data to the server I made this:
static std::vector<std::string> oldMessages;
//to keep track of the last sended messages, and don't handle them... (poor solution)
//Basicly it contains the client last message...
sf::Mutex globalMutex;

std::vector<sf::TcpSocket *> clients;
sf::TcpListener listener;
sf::SocketSelector selector;

bool quit = false;

void Receive_Packets(void)
{
while(!quit)
{
    if (selector.wait())
        {
            if (selector.isReady(listener))
            {
//If New client connected:
//- Create a new socket and add to the sockets list
//- Add new socket to the socket selector
//- Add oldMessages list a empty message, since the program won't allow the send the same message more times in row
//- Print new Client IP

                sf::TcpSocket *client = new sf::TcpSocket;
                //Add New Client
                if (listener.accept(*client) == sf::Socket::Done)
                {
                    clients.push_back(client);
                    selector.add(*client);
                    oldMessages.push_back("");
                    std::cout<<std::endl<<"New Client: " <<client->getRemoteAddress()<<std::endl << "Me: ";
                }

                //If listener can not accept the socket:
//- Print Clinet disconnected: Clinet IP
//- Delete newly created socket
//- Print new clients list size

                else
                {
                    std::cout<<std::endl<<"Client disconnected: "<< client->getRemoteAddress()<<std::endl;
                    client->disconnect();
                    delete(client);
                    std::cout<<"Clients: "<<clients.size()<<std::endl << "Me: ";
                }
            }

            short messageIndex=0;
            messageIndex=0; //To make sure...

            for(sf::TcpSocket *client : clients)
            {
                if (selector.isReady(*client))
                {
                    //IF I WON'T SEND SOMETHING, THEN THE RECEIVE PART WON'T RUN
                    //However if you delete the commend part, and run this code too, then it's will work
                    //but it will send data constantly, and eats about 20-25% processor power
/*
                    //Send Message
                    sf::Packet packetSend;
                    globalMutex.lock();
                    packetSend << msgSend;
                    globalMutex.unlock();
                    client->send(packetSend);
                    */

                    sf::Packet packetReceive;
                    std::string msg;

//This iterator keeps track which client what sended last time
                    std::vector<std::string>::iterator it = oldMessages.begin() + messageIndex;

                    //Receive Message
                    if(client->receive(packetReceive) == sf::Socket::Done){
                        if(packetReceive >> msg)
                        {
                            if(*it != msg && !msg.empty())
                            {
                                std::cout <<std::endl<< client->getRemoteAddress()<<": "<< msg << std::endl << "Me: ";
                                *it = msg;
                            }
                        }
                    }
                    //IF the client disconnected:
//- Print Client disconnected: Clinet IP
//- Remove Client from the selector
//- Disconnecting the client
//- Delete client socket from the clients list (vector)
//- Delete the client last message from the oldMessages list (vector)
//- Print new clients list size

                    else if(client->receive(packetReceive) == sf::Socket::Disconnected){
                        std::cout<<std::endl<<"Client disconnected: "<< client->getRemoteAddress()<<std::endl;
                        selector.remove(*client);
                        client->disconnect();
                        clients.erase(std::remove(clients.begin(), clients.end(), client), clients.end());
                        oldMessages.erase(std::remove(oldMessages.begin(), oldMessages.end(), *it), oldMessages.end());
                        std::cout<<"Clients: "<<clients.size()<<std::endl << "Me: ";
                    }
                }
                ++messageIndex;
            }
        }
    }
}

//Get a row from the user and later on the Thread
//will send it to the clients
void GetInput(void)
{
    std::cout<<"Me: ";
std::string s;
getline(std::cin,s);
if(s == "exit")
quit = true;
globalMutex.lock();
msgSend = s;
globalMutex.unlock();
}

int main()
{
    const unsigned short PORT = 53000;

    if(listener.listen(PORT) != sf::Socket::Done){
        std::cout<<"Can not use "<<PORT<<" port"<<std::endl;
        return 0;
    }
    selector.add(listener);

    sf::Thread *thread = 0;
    thread = new sf::Thread(&Receive_Packets);
    thread->launch();

//MAIN LOOP:
//- Runs while the user doesn't type "exit"

    std::cout<<"Server is ready."<<std::endl;
    while(!quit){
        GetInput();
    }

    std::cout<<"Server shut down..."<<std::endl;

//Close thread

    if(thread)
{
thread->terminate();
delete thread;
}

//Delete all the sockets from the heap

    for(int i=0;i<clients.size();++i)
        delete clients[i];
}

Try to copy and paste it somewhere, it doesn't looks so great :(

btw I didn't want to post it here, since it took some time to read, meanwhile https://github.com/SFML/SFML/wiki/Source:-Network-Chat-Example has simmilar "issue" and shorter.

Edited: I added some comments to the code

5
Network / Re: Receiving data
« on: December 19, 2017, 09:13:29 am »
"The "socket" example of the SDK is a good starting point."
I'm not sure what is the SDK, in this situation (Socket Mobile Developing?).

"Did you have a look at it?"
Not yet.

Yes, I'm looking for more complex stuff, since I'm developing an RPG game, and I'm trying to make it client-server, but I don't know why I can't receive data, without sending some data.
Or should I send useless data, just to keep the connection open? I mean if a client sending data to my server, then my server won't deal with it until it doesn't send anything.

Example:
Code: [Select]
//Let say I'm sending data in every second
sf::Packet packetSend;
globalMutex.lock();
packetSend << msgSend;
globalMutex.unlock();

socket.send(packetSend);

//Then this part will run in every second, but I want it to run when data arraives to the server
//However I don't want it to run constantly, cuz that would eat a lot of processing power (about 20-25%)
std::string msg;
sf::Packet packetReceive;

socket.receive(packetReceive);
if ((packetReceive >> msg) && oldMsg != msg && !msg.empty())
{
std::cout << msg << std::endl;
oldMsg = msg;
}
Because this code right now runs while(!quit), which means it runs some million times per second. (of course it depends on hardware, but it not the thing what I would like to discuss here)

I would appreciate a fast server example, even if it complicated, that would save a lot of time. (At least I hope so)

6
Network / Receiving data
« on: December 18, 2017, 06:07:04 pm »
I'm trying to learn sfml network, but there are so few example, and even if I find some, those are wrong or just weird.
For example, this code:
https://github.com/SFML/SFML/wiki/Source:-Network-Chat-Example
seems okay, but in the DoStuff thread, the
Code: [Select]
sf::Packet packetSend;
globalMutex.lock();
packetSend << msgSend;
globalMutex.unlock();

socket.send(packetSend);
part constantly sending(spamming) data and it's eats a ton of processing power, however if I delete that part, then the socket.receive(packetReceive); simply won't run, even if I send data, from another client.

Why is that?
Can someone give me a good source code for client-server game program? (I don't mind if it complex)
I think I already know the basics theory, but that would be great, if someone share with me a good free article, book or video about sfml network.

I hope it's something obvious thing (just not for me :) )

7
General / Re: A noob can't handle lists
« on: December 01, 2017, 05:50:35 pm »
Thanks, but I figured out a while ago, thanks to the irc help, I know it's doesn't make sense to create a texture for every single Wall, but still, I couldn't figure out why it isn't working.

"First, understand how sf::Texture works." I don't know how they works, but if I have time I will read the sfml source code, and I hope I will know :)

"I compiled your code and for me all 7 rectangles are white" those are sf::Sprite-s  >:( ... :)
and they white because they got a nullptr as texture (I think).

"Also, if I replace the std::list of walls with a std::vector" good idea, I did, but I come from C# and Java, so that should explain why I used list :)

Shortly thanks, those are good tips, I would have been glad, if I got your answer about 8-10 days earlier :D

8
System / Re: SFML With no DLLS?
« on: November 27, 2017, 10:07:49 am »
Game Maker Studio can archive your game files into one exe file and run it properly, because Game Maker uses (as far as I know) Java, C and C++ dll-s, and those dll-s was already installed on most of the computers.

But if you have no installed Java (JRE,JDK), and or C++ Runtime Redistributable, then I assume those Game Maker exe-s won't work.

If you manage somehow to install all the sfml dll-s on every computer, then sfml could do the same thing easily.

9
General / Re: Parsing "Collection of Images" with json doesn't work
« on: November 26, 2017, 10:31:29 pm »
If you allow me I have some question about this.

  • Where do you load the texture (image(s))?
    • I see you loaded the width, height and set the collision, but I can't see where do you load the image.
    • I assume the "data" is the image in some kind of form, but still, how it will be an image?

    Maybe you should add to your code rows like:
Code: [Select]
imageName = layer["tiles"]["1"]["image"];
imageWidth = layer["tiles"]["1"]["imagewidth"];
imageHeight = layer["tiles"]["1"]["imageheight"];
//and other attributions...
//after that, you could load the image(s)...

  • I'm not sure what is tmp->tileMap, I mean it's probably an array or vector, but what is this? Can you explicate it for me, or show the code of MapLayer? Is it for storing the map(s)?

  • Where do you display (draw) the image(s)?


10
General / A noob can't handle lists
« on: November 26, 2017, 04:12:43 pm »
I have a Wall Class, and I store them in a list:
std::list<Wall> walls;

But when I add a wall like:

Code: [Select]
texture.loadFromFile("wall.png");
walls.push_back(Wall(texture, 500.f, 490.f, 4)); //arguments: texture, position X, postion Y, direction 4 = down

Then it's works, but the texture shown like it would be a nullptr. (So it's a white rectangle)
I draw it the walls this way:

Code: [Select]
for (Wall const &wal : walls)
wal.draw(window);

But if I add another wall to the list, then the first one is appear fine, but the second one is white, like:


And if I add different textured walls like:
Code: [Select]
texture.loadFromFile("wall.png");
walls.push_back(Wall(texture, 500.f, 490.f, 4));
walls.push_back(Wall(texture, 563.f, 490.f, 4));
walls.push_back(Wall(texture, 626.f, 490.f, 4));
walls.push_back(Wall(texture, 500.f, 600.f, 4));
walls.push_back(Wall(texture, 563.f, 600.f, 4));
walls.push_back(Wall(texture, 626.f, 600.f, 4));

texture.loadFromFile("wall2.png");
walls.push_back(Wall(texture, 720.f, 540.f, 0));

then 6. (the one before texture changes) will be corrupted:



I spend about 6 days of free time to figure out what could be the problem, and so far I managed to identify that the poblem is with my list, but I don't know what.

Because, if I create walls without adding to the a list, and draw them, then everything works, as intended.
(Of course I tried vectors , arrays, and so on, but I thing it would be reasonable to store them in a list)

The entire source code could be found at my GitHub: https://github.com/Kerachi/Kerachi

Any idea?

11
General / Re: Why do I get this error with 'sf::clock' and other sf:: codes
« on: November 24, 2017, 11:11:07 pm »
If you have readed the turorial about time: https://www.sfml-dev.org/tutorials/2.4/system-time.php, then you would know that you can't do this:

sf::Time elapsed = float;

but rather:

sf::Time timee = sf::seconds(clock.restart().asSeconds());

Because time variable, was already defined.

Or just simply:

float time = clock.restart();

12
General / Re: text.setColor declared deprecated
« on: November 24, 2017, 09:11:49 pm »
I do rather use sf::Text::setFillColor(), but thanks, it's good to know, how can I use deprecated stuff  :)

13
General / Re: text.setColor declared deprecated
« on: November 24, 2017, 08:59:20 am »
ehhh

I see, thanks.

14
General / text.setColor declared deprecated
« on: November 23, 2017, 09:47:36 pm »
Hi everyone!

It worked perfectly in CodeBlocks, but I keep getting this error in Visual Studio:


Any idea what am I doing wrong?

15
Graphics / Re: One Big Image VS Lot Little
« on: November 18, 2017, 09:17:20 pm »
Thank you Hapax and achpile, those comments helped me a lot.

So far I did this: https://github.com/Kerachi/Kerachi (sorry if it is unreadable, I tried my best :( )

I run into some problem with detecting player is before or behind the wall, and moving the collision Rectangle.
(Because if the collison Rectangle isn't moving then the player would be unable to move "behind the wall")

EDITED: I solved, but I'm pretty sure that there is a way better solution then mine...

Some weeks ago I couldn't even code in c++, so I'm think I'm in the right way :D

(I just assumed that I can right now? lol)

Pages: [1] 2
anything