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

Pages: [1]
1
Network / Re: UDP socket not sending packets.
« on: June 22, 2019, 03:42:19 pm »
First of all, can you provide a minimal code showing the problem?

I'll try it on my own computer ;)

I fixed it... Firstly I had to change ports because I am using the local address for testing.
so now it is like this:
Server: bind to 20000, receive on 20000, send to 20001.
Client: bind to 20001, receive on 20001, send to 20000.
And the core problem
It seems that after udpSocket fails to send\receive data on some determined ip+port, it resets it completely, so I had to create temporary variables. If someone knows how to get rid of it, or optimize the current way, tell me please.

2
Network / Re: UDP socket not sending packets.
« on: June 22, 2019, 12:24:17 pm »
First of all, can you provide a minimal code showing the problem ?

I'll try it on my own computer ;)

Sure, thank you!
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <math.h>
#include <vector>

enum Nodemode : char { SERVER = 's', CLIENT = 'c' };
sf::IpAddress hostIp = sf::IpAddress::getLocalAddress();
sf::UdpSocket udpSocket;
uint16_t port = 51234;
std::string recMsg = "";
std::string msg;

std::istream &operator>>(std::istream &is, Nodemode &i);
std::ostream &operator<<(std::ostream &out, const Nodemode value);

void sendData(std::string msg);
Nodemode nodestate;
sf::Thread secondThread(&sendData, msg);

int main() {

  std::cout << "Type 's' for server, and 'c' for client. If you want to close "
               "the app, type in 'stop' "
            << std::endl;
  std::cin >> nodestate;

  if (nodestate == SERVER) {
          udpSocket.bind(port);
  } else if (nodestate == CLIENT) {
          udpSocket.bind(port);
  }

  std::cout << "Current IP-address: " << hostIp.toString() << std::endl;
  std::cout << "Node state: " << nodestate << std::endl;
  std::cout << recMsg << std::endl;

  secondThread.launch();
  while (msg != "stop") {

    sf::Packet packet2;
        if (udpSocket.receive(packet2, hostIp, port) != sf::Socket::Done)
                std::cout << "~~error receiving data" << std::endl;
        else {
                std::cout << "--data received" << std::endl;
                packet2 >> recMsg;
                if (recMsg != "")
                        std::cout << "Other user: " << recMsg << std::endl;
        }
  }
  return 0;
}

std::istream &operator>>(std::istream &is, Nodemode &i) {
  char tmp;
  if (is >> tmp)
    i = static_cast<Nodemode>(tmp);
  return is;
}

std::ostream &operator<<(std::ostream &out, const Nodemode value) {
  static std::map<Nodemode, std::string> strings;
  if (strings.size() == 0) {
#define INSERT_ELEMENT(p) strings[p] = #p
    INSERT_ELEMENT(SERVER);
    INSERT_ELEMENT(CLIENT);
#undef INSERT_ELEMENT
  }
  return out << strings[value];
}

void sendData(std::string msg) {
  while (msg != "stop") {

    getline(std::cin, msg);
    sf::Packet packet;
    packet << msg;

        if (udpSocket.send(packet, hostIp, port) != sf::Socket::Done)
                std::cout << "~~Could not send data" << std::endl;
  }
}
 

3
Network / Re: UDP socket not sending packets.
« on: June 21, 2019, 08:00:35 pm »
Does someone know what it could be? Maybe code mistakes?

4
Network / UDP socket not sending packets.
« on: June 20, 2019, 11:12:48 pm »
I get error that packet ain't sent. With TCP this code worked.Things I've tried:
1)change port ::AnyPort
2)Use IP from hamachi with my friend
3)put debugging couts. After the first "while msg!=stop" loop it changes port and IP to 0 and 0000000000(Or so would seem)
4)Removing it from thread partially, *exclamation mark* partially fixed the problem, it worked 1 out of 10 times!
Help...I'll answer all questions quick.
sf::IpAddress hostIp = sf::IpAddress::getLocalAddress();
sf::UdpSocket udpSocket;
unsigned short port = 51234;
string recMsg ;
string msg;
void sendData(string msg);
sf::Thread secondThread(&sendData, msg);
int main() {
  udpSocket.bind(port);
  secondThread.launch();
while (msg != "stop") {
   
    if (udpSocket.receive(packet2, hostIp, port) != sf::Socket::Done)
      cout << "~~error receiving data" << endl;
        else {
                cout << "--data received" << endl;
                packet2 >> recMsg;
                if (recMsg != "")
                        cout << "Other user: " << recMsg << endl;
        }
  }

}
void sendData(string msg) {
  while (msg != "stop") {
    getline(cin, msg);
   
    packet << msg;

    if (udpSocket.send(packet, hostIp, port) != sf::Socket::Done)
      std::cout <<"~~Could not send data"<<endl;
}
 

5
General / Re: Texture.update() is not working
« on: June 04, 2019, 12:29:32 am »
Since we don't know how rect.left is calculated, and you don't show the result of your std::cout lines, there's nothing very helpful we can do. Of course I tend to trust the assert more than you ;)
Well, it was correct. The problem is that texture should not be created from part of the window. It is written in the documentation

6
General / Texture.update() is not working
« on: June 03, 2019, 08:00:43 pm »
It says Assertion failed: x + window.getSize().x <= m_size.x
 :-\
Size and x\y are correct
It all breaks on last line. I need it for selection and copy/paste tool
        bufferRect.width = abs(curPos.x - point1.x);
        bufferRect.height = abs(curPos.y - point1.y);
        int width = bufferRect.width;
        int height = bufferRect.height;
        int x = bufferRect.left;
        int y = bufferRect.top;
        std::cout << "width is "<< bufferRect.width << std::endl;
        std::cout << "height is " << bufferRect.height << std::endl;
        std::cout << "left and top are " << bufferRect.left << " + " << bufferRect.top << std::endl;
       
        bufferTexture.create(width, height);
        bufferTexture.update(mainWindow,x ,y );
 

7
Graphics / Re: Why I can't setTexture?
« on: June 01, 2019, 12:30:34 pm »
Not sure if [FIXED] means you guessed but the proper way is &pencilIcon.
Thx, yeah, I did exactly this!

8
Graphics / Why I can't setTexture?
« on: May 31, 2019, 11:11:29 pm »
How can I fix it?
  sf::Texture pencilIcon;
  sf::RectangleShape testShape;
  testShape.setTexture(pencilIcon);
I know, it says about *texture, but I don't know how to pass it(setTexture(*pencilIcon) doesn't work)

[FIXED]

9
General / Re: Dotted line instead of normal
« on: May 29, 2019, 12:59:44 pm »
FOR THOSE WHO WANT TO FIX.

new code
if ((event.type == sf::Event::MouseMoved) &&
               sf::Mouse::isButtonPressed(
                   sf::Mouse::Left)) // MouseMoved and leftClickHeld
      {
        switch (currentTool) {
        case 0: {
          vecLine =
              drawPencil(mainWindow, currentColor,
                         sf::Mouse::getPosition(mainWindow), thickness, 0, prevPos);
                  prevPos = sf::Mouse::getPosition(mainWindow);
          break;
        }
        }
      } else if (event.type == sf::Event::MouseButtonPressed) {
        switch (currentTool) {
        case 0: {
          vecLine =
              drawPencil(mainWindow, currentColor,
                         sf::Mouse::getPosition(mainWindow), thickness, 1, prevPos);
          break;
        }
        case 1: {
          vecLine = drawLine(mainWindow, currentColor,
                             sf::Mouse::getPosition(mainWindow));
          break;
        }
        }
      } else if (event.type == sf::Event::MouseButtonReleased) {
                  switch (currentTool) {
                  case 0: {
                          vecLine =
                                  drawPencil(mainWindow, currentColor,
                                          sf::Mouse::getPosition(mainWindow), thickness, 3, prevPos);
                          break;
                  }
                  }
        canvas.update(mainWindow);
          }

and the draw pencil is
drawPencil(sf::RenderWindow& mainWindow, sf::Color color,
        sf::Vector2i currentPos, unsigned int thickness, int mode, sf::Vector2i prevPos) {
        if (thickness == 0) {
                if (mode == 1) {       
                        line[0].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
                        line[0].color = color;
                        line[1].color = color;
                       
                }
                else if (mode == 0) {

                        line[1].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
                        line[0].color = color;
                        line[1].color = color;
                        vecLine.push_back(std::make_pair(line[0], line[1]));
                        line[0].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
                       

                }
                else {
                                line[1].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
                               
                                vecLine.push_back(std::make_pair(line[0], line[1]));
                        }
                return vecLine;
        }

10
General / Can you loadFromImage to RenderTexture?
« on: May 28, 2019, 11:54:51 am »
image.create(windowWidth, windowHeight, sf::Color::White);
sf::RenderTexture canvas;
sf::IntRect area = sf::IntRect(0, 0, image.getSize.x, image.getSize.y);
canvas.getTexture().loadFromImage(image,area);

I need it so I can open and edit images.

11
General / Re: Dotted line instead of normal
« on: May 28, 2019, 11:33:12 am »
I am not sure, but I think I am saving the previous mouse location.
1)When LMB is pressed, it calls drawPencil in mode 1, so it only registers the mouse pos as the "origin" point of the line
2)When the mouse is moved, it calls drawPencil in mode 0, therefore line's when it calls it for the first time, origin of the line is set to currentPos, when the drawPencil called for 2 time it passes the currentPos again
and the line's final point is set

12
General / Dotted line instead of normal
« on: May 27, 2019, 11:54:37 pm »
Hello fellas! I am working on Paint 2.0 project which is supposed to be simple yet WORKING graphics editor, using sfml. Yesterday I have encountered the problem which I still can't fix.
I am implementing Pencil tool, but instead of creating normal smooth line it draws dotted and separated ones.
At first, I just made it with drawPixel function but right now I am using Vertices to make lines based on the previous mouseMoved register. In both cases, I get the same scenario.
Here is the function call.
while (mainWindow.pollEvent(event)) {
      if (event.type == sf::Event::Closed)
        mainWindow.close();
      else if ((event.type == sf::Event::MouseMoved) &&
               sf::Mouse::isButtonPressed(
                   sf::Mouse::Left)) // MouseMoved and leftClickHeld
      {
        switch (currentTool) {
        case 0: {
          vecLine =
              drawPencil(mainWindow, currentColor,
                         sf::Mouse::getPosition(mainWindow), thickness, 0);
          break;
        }
        }
      } else if (event.type == sf::Event::MouseButtonPressed) {
        switch (currentTool) {
        case 0: {
          vecLine =
              drawPencil(mainWindow, currentColor,
                         sf::Mouse::getPosition(mainWindow), thickness, 1);
          break;
        }
        case 1: {
          vecLine = drawLine(mainWindow, currentColor,
                             sf::Mouse::getPosition(mainWindow));
          break;
        }
        }
 
And here is the function that draws lines

std::vector<std::pair<sf::Vertex, sf::Vertex>>
drawPencil(sf::RenderWindow &mainWindow, sf::Color color,
           sf::Vector2i currentPos, unsigned int thickness, int mode) {
  if (thickness == 0) {

    if (mode == 1) {
      line[0].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
      line[1].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
      line[0].color = color;
      line[1].color = color;

    } else {
      if (firstPoint) {
        line[0].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
        line[1].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
        line[0].color = color;
        line[1].color = color;
        firstPoint = false;
      } else {
        line[1].position = sf::Vector2f(sf::Mouse::getPosition(mainWindow));
        firstPoint = true;
        vecLine.push_back(std::make_pair(line[0], line[1]));
      }
    }
    return vecLine;
  } else {
  }
}

Int mode is to deny lines that could be drawn just by clicking and not dragging.

I suspect it's connected with MouseMoved update rate

Pages: [1]
anything