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.


Topics - dixondean25

Pages: [1]
1
So I implemented networking in my game about 2 weeks ago. I knew the way I set it up probably wasn't that good, but all I wanted to see is if it would work from computer to computer. And it did! So then I wanted to test it over the internet from my house to my brother's house to see if would work, and it did! It was really laggy though, and hardly playable. So now I'm at the point where I want to clean it up and make it faster/more efficient.

I'll start out by posting the entire client and server code. It's really not that complicated.
Both the client and server code is in a thread called runNet

online setup
void runNet(void* UserData)
{

        SoccerLevelsClass* obj = static_cast<SoccerLevelsClass*>(UserData);

        obj->netQuit=false;

        obj->netType = 0;

        bool good = false;

        while(!good)
        {

                cout<<"What are you? 0 for server, 1 for client : ";

                cin>>obj->netType; //store network type( 0 for sever, 1 for client)

                if(obj->netType!=0 && obj->netType!=1)
                {
                        cout<<"\n.Invalid input. Try again.\n";
                }else{good=true;}
        }

        good = false;
        string myName="";

        while(!good)
        {


                cout<<"\n";

                cout<<"Enter Online ID (must be atleast 1 character): ";

                cin>>myName; //store network type( 0 for sever, 1 for client)

                if(myName=="")
                {
                        cout<<"\n.Please enter atleast one character.\n";
                }
                else
                {
                        good=true;
                }
        }
        cout<<"\n";

        obj->connected=false;
        obj->numMultiplayers=0;

from there it runs the server or client code based on the 'netType' value (0 = server, 1 = client)

server
// start of server code
        if(obj->netType==0)
        {
                obj->numMultiplayers++;
                obj->onlinePlayers[0].host=true;
                obj->onlinePlayers[0].name=myName;
                obj->onlinePlayers[0].playerNumber=0;
                obj->onlinePlayers[0].rank=0;
                obj->myOnlineNumber=0;
                obj->connected=true;

                // Create a socket to listen to new connections
                sf::TcpListener listener;
                listener.listen(2000);
                // Create a list to store the future clients
                std::vector<sf::TcpSocket*> clients;
                // Create a selector
                sf::SocketSelector selector;
                // Add the listener to the selector
                selector.add(listener);

                //listener.setBlocking(false);
                // Endless loop that waits for new connections


                int ticks = 0;                  //amount of loops the thread makes every second
                int bytesSent=0;                //amount of bytes sent every second
                int bytesRecieved=0;    //amount of bytes recieved second
                int totalBytes=0;               //amount of totalBytes sent and recieved every second
                sf::Clock timeTest;             //keeps track of time

                while (obj->netQuit==false)
                {
                        //if the clock is greater than 1 second
                        if(timeTest.getElapsedTime().asSeconds()>1)
                        {
                                //output and reset everything
                                totalBytes = bytesSent + bytesRecieved;
                                cout<<"ticks = "<<ticks<<". bytesSent = "<<bytesSent<<". bytesReceived = "<<bytesRecieved<<". total = "<<totalBytes<<".\n";
                                bytesSent=0;
                                bytesRecieved=0;
                                ticks=0;
                                timeTest.restart();
                        }

                        // Make the selector wait for data on any socket
                        if (selector.wait(sf::milliseconds(1)))
                        {
                                //cout<<"done waiting. listening.\n";
                                // Test the listener
                                if (selector.isReady(listener))
                                {
                                        //cout<<"trying to add client.\n";
                                        // The listener is ready: there is a pending connection
                                        sf::TcpSocket* client = new sf::TcpSocket;
                                        if (listener.accept(*client) == sf::Socket::Done)
                                        {
                                                //client->setBlocking(false);

                                                // Add the new client to the clients list
                                                clients.push_back(client);

                                                // Add the new client to the selector so that we will
                                                // be notified when he sends something

                                                selector.add(*client);
                                                sf::Packet packet;
                                                while(client->receive(packet)!= sf::Socket::Done)
                                                {

                                                }
                                                sf::Uint8 header;
                                                packet >> header;
                                                if(header==1)
                                                {
                                                        //make a new player
                                                        string newName;
                                                        packet >> newName;
                                                        obj->onlinePlayers[obj->numMultiplayers].name = newName;
                                                        obj->onlinePlayers[obj->numMultiplayers].playerNumber = obj->numMultiplayers;
                                                        obj->onlinePlayers[obj->numMultiplayers].host = false;
                                                        obj->onlinePlayers[obj->numMultiplayers].rank = 0;
                                                }

                                                for (int i = 0; i<clients.size(); i++)
                                                {
                                                        sf::Packet sendPacket;
                                                        sf::Uint8 newHeader=0;

                                                        //send new player to all players
                                                        for(int x = 0; x < obj->numMultiplayers+1; x++)
                                                        {
                                                                sendPacket.clear();
                                                                sendPacket << newHeader;
                                                                sendPacket << obj->onlinePlayers[x];
                                                                while(clients[i]->send(sendPacket) != sf::Socket::Done)
                                                                {

                                                                }
                                                        }

                                                }
                                                obj->numMultiplayers++; //increment number of players
                                        }
                                        else
                                        {
                                                // Error, we won't get a new connection, delete the socket
                                                delete client;
                                        }
                                }
                                else
                                {
                                        // The listener socket is not ready, test all other sockets (the clients)
                                        for (int i = 0; i<clients.size(); i++)
                                        {
                                                if (selector.isReady(*clients[i]))
                                                {

                                                        sf::Packet packet, sendPacket;
                                                        if(clients[i]->receive(packet) == sf::Socket::Done)
                                                        {
                                                                sf::Uint8 recHeader;
                                                                packet >> recHeader;
                                                                switch(recHeader){

                                                                        // recieved controller input
                                                                case 3:
                                                                        {
                                                                                SoccerLevelsClass::controllerStruct tempController;
                                                                                packet >> tempController;
                                                                                obj->contStructs[tempController.playerNumber]=tempController;

                                                                                bytesRecieved += sizeof(tempController);        //increment bytes recieved

                                                                                sendPacket << recHeader;
                                                                                sendPacket << tempController;

                                                                                //send input to rest of clients
                                                                                for(int j = 0; j<clients.size(); j++)
                                                                                {
                                                                                        clients[j]->send(sendPacket);
                                                                                        bytesSent += sizeof(sendPacket);        //increment bytesSent
                                                                                }
                                                                                break;
                                                                        }


                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                        else
                        {
                                //tells clients to start their game (this only runs once)
                                if(obj->startGame)
                                {
                                        sf::Uint8 gameHeader=2;
                                        sf::Packet gamePacket;
                                        gamePacket << gameHeader;

                                        //iterate through clients
                                        for (int i = 0; i<clients.size(); i++)
                                        {

                                                while(clients[i]->send(gamePacket) !=sf::Socket::Done)
                                                {
                                                        bytesSent += sizeof(gamePacket);
                                                }

                                        }
                                        obj->startGame=false;
                                }

                                //this if statement runs when every client loads the level and is ready to play
                                if(obj->inOnlineMatch==true)
                                {
                                        //if the server's controller input changed, send it to all clients
                                        if(obj->mgControllerChanged==true)
                                        {
                                                //iterate through clients
                                                for (int i = 0; i<clients.size(); i++)
                                                {

                                                        sf::Packet packet;
                                                        sf::Uint8 heads = 3;
                                                        packet << heads;
                                                        obj->myContStruct.playerNumber=0;
                                                        packet << obj->myContStruct;

                                                        //send to all clients
                                                        if(clients[i]->send(packet) == sf::Socket::Done)
                                                        {
                                                                obj->mgPlayer1->hasChanged=false;
                                                                obj->mgControllerChanged=false;

                                                                bytesSent += sizeof(packet);                            //increment bytes sent
                                                        }  
                                                }
                                        }

                                        //the onlineUpdate value turns true everytime the game loop finishes
                                        if(obj->onlineUpdate==true)
                                        {
                                                //iterate through every client socket
                                                for (int i = 0; i<clients.size(); i++)
                                                {
                                                        //iterate through every player
                                                        for(int x = 0; x<obj->numMultiplayers; x++)
                                                        {
                                                                sf::Packet packet;
                                                                sf::Uint8 heads = 6;
                                                                packet << heads;
                                                                obj->onlinePositions[x].playerNumber=x;
                                                                packet << obj->onlinePositions[x];

                                                                clients[i]->setBlocking(false);
                                                                clients[i]->send(packet);

                                                                bytesSent += sizeof(packet);                    //increment bytes sent

                                                                //this statement is true when one of the player's points change
                                                                if(obj->updateStats[x]==true)
                                                                {
                                                                        packet.clear();
                                                                        heads = 7;
                                                                        packet << heads;
                                                                        obj->playerStats[x].playerNumber=x;
                                                                        packet << obj->playerStats[x];
                                                                        clients[i]->send(packet);

                                                                        bytesSent += sizeof(packet);            //increment bytes sent
                                                                }
                                                        }
                                                }
                                                obj->onlineUpdate=false;
                                        }



                                        //iterate through players to see if they died
                                        for(int x = 0;x<8;x++)
                                        {
                                                //if a player died
                                                if(obj->onlineDeaths[x].activateDeath)
                                                {
                                                        sf::Packet packet;
                                                        sf::Uint8 header = 5;
                                                        packet << header;
                                                        obj->onlineDeaths[x].playerNumber=x;
                                                        packet << obj->onlineDeaths[x];
                                                        for (int i = 0; i<clients.size(); i++)
                                                        {
                                                                while(clients[i]->send(packet) !=sf::Socket::Done)
                                                                {
                                                                        bytesSent += sizeof(packet);                                    //increment bytes sent
                                                                }
                                                        }
                                                        obj->onlineDeaths[x].activateDeath=false;
                                                }
                                        }
                                }
                        }

                        ticks++;
                        sf::sleep(sf::milliseconds(5));

                }
        }
        // end of server code

client
// start of client code
        if(obj->netType==1)
        {
                sf::IpAddress server;
                do
                {
                        std::cout << "Type the address or name of the server to connect to: ";
                        std::cin  >> server;
                }
                while (server == sf::IpAddress::None);


                //cout<<"here1\n";
                sf::TcpSocket socket;

                while(socket.connect(server,2000) != sf::Socket::Done)
                {

                }
                cout<<"connected.\n";
                obj->connected=true;
                {
                        sf::Packet packet;
                        sf::Uint8 header = 1;
                        packet << header;
                        packet << myName;
                        while(socket.send(packet) != sf::Socket::Done)
                        {

                        }
                }

                socket.setBlocking(false);

                int ticks = 0;                  //amount of loops the thread makes every second
                int bytesSent=0;                //amount of bytes sent every second
                int bytesRecieved=0;    //amount of bytes recieved second
                int totalBytes=0;               //amount of totalBytes sent and recieved every second
                sf::Clock timeTest;             //keeps track of time

                while (obj->netQuit==false)
                {
                        //if the clock is greater than 1 second
                        if(timeTest.getElapsedTime().asSeconds()>1)
                        {
                                //output and reset everything
                                totalBytes = bytesSent + bytesRecieved;
                                cout<<"ticks = "<<ticks<<". bytesSent = "<<bytesSent<<". bytesReceived = "<<bytesRecieved<<". total = "<<totalBytes<<".\n";
                                bytesSent=0;
                                bytesRecieved=0;
                                ticks=0;
                                timeTest.restart();
                        }

                        sf::Packet packet;
                        sf::Socket::Status status = socket.receive(packet);
                        if (status == sf::Socket::Done)
                        {
                                sf::Uint8 header;
                                packet >> header;

                                //packet control
                                switch(header){

                                        //recieve a new player
                                case 0:
                                        {      
                                                SoccerLevelsClass::onlinePlayerStruct tempStruct;
                                                packet >> tempStruct;
                                                obj->onlinePlayers[tempStruct.playerNumber]=tempStruct;
                                                if(obj->onlinePlayers[tempStruct.playerNumber].name == myName){obj->myOnlineNumber=tempStruct.playerNumber;}

                                                bytesRecieved += sizeof(packet);                //increment bytes recieved
                                                break;
                                        }

                                        //told to start the game
                                case 2:
                                        {
                                                obj->startGame=true;
                                                //only recieve once so don't increment
                                                break;
                                        }
                                       
                                        //recieve and update contoller input
                                case 3:
                                        {

                                                SoccerLevelsClass::controllerStruct tempController;
                                                packet >> tempController;
                                                obj->contStructs[tempController.playerNumber]=tempController;

                                                bytesRecieved += sizeof(packet);                //increment bytes recieved
                                                break;
                                        }

                                        //recieved a enw player death
                                case 5:
                                        {
                                                SoccerLevelsClass::onlineDeathBody deathStruct;
                                                packet >> deathStruct;
                                                obj->onlineDeaths[deathStruct.playerNumber]=deathStruct;

                                                bytesRecieved += sizeof(packet);                //increment bytes recieved
                                                break;
                                        }

                                        //recieved an updated player position
                                case 6:
                                        {
                                                SoccerLevelsClass::playerPosition plPosish;
                                                packet >> plPosish;
                                                obj->onlinePositions[plPosish.playerNumber]=plPosish;
                                                obj->onlineUpdate=true;
                                                obj->updatePlayerPos[plPosish.playerNumber]=true;

                                                bytesRecieved += sizeof(packet);                //increment bytes recieved
                                                break;
                                        }

                                        //recieved update player points
                                case 7:
                                        {
                                                SoccerLevelsClass::playerStat stat;
                                                packet >> stat;
                                                obj->playerStats[stat.playerNumber]=stat;
                                                obj->onlineUpdate=true;

                                                bytesRecieved += sizeof(packet);                //increment bytes recieved
                                                break;
                                        }
                                }

                                //cout<<"herh\n";
                        }
                        else if(status == sf::Socket::NotReady)
                        {
                                //cout<<"not ready.\n";
                        }
                        else if(status == sf::Socket::Disconnected)
                        {
                                //cout<<"disconnected.\n";
                        }
                        else if(status == sf::Socket::Error)
                        {
                                //cout<<"error.\n";
                        }

                        //if this client's controller input has changed
                        if(obj->mgControllerChanged==true)
                        {
                                sf::Packet packet, sendPacket;
                                sf::Uint8 heads = 3;
                                packet << heads;
                                obj->myContStruct.playerNumber=obj->myOnlineNumber;
                                packet << obj->myContStruct;

                                if(socket.send(packet) == sf::Socket::Done)
                                {
                                        obj->mgPlayer1->hasChanged=false;
                                        obj->mgControllerChanged=false;
                                        bytesSent += sizeof(packet);                                    //increment bytesSent
                                }  
                        }

                        ticks++;                                                        //increment ticks
                        sf::sleep(sf::milliseconds(5));
                }

        }// end of client code

That's the entire networking code.

So I'm testing all of this on two computers in my room, my laptop, and my desktop.

But there is some weird behavior.

Scenario 1: Host on Desktop
Everything runs fine on both computers. At first, the laptop shows no signs of latency. After a while though, the latency begins and slowly gets worse. But even then, the desktop receives the input from the laptop with no noticeable latency.

Scenario 2: Host on Laptop
Right out of the gate, the latency is horrible on the desktop, and slowly gets worse.But even then, the laptop receives the input from the desktop with no noticeable latency.

So basically, the server gets the input from the client immediately no matter who's hosting and how much time has been spent in the game. But the lag continues to get worse and worse at a slow rate no matter what on the client side.

Below I've posted screenshots of the output of the ticks, bytesSent, bytesRecieved, and totalBytes values after each second, for each computer.

The laptop runs through the thread about 2-3 times as much as the Desktop. Is this the problem?

2
Network / Identifying types of packets
« on: August 08, 2014, 05:46:45 pm »
I was going to post here http://en.sfml-dev.org/forums/index.php?topic=3172.0, but it's too old so I thought I'd start a new topic.

Basically the original poster wanted to check the type of packet he sent before he opened it on the receiving end, and his idea was to put a header at the beginning of each packet to correctly identify how to proceed with opening it and storing it correctly.

Laurent replied with this code for the sender and receiver.

Sending:
// sending
sf::Packet packet;
packet << header << ... specific data ... ;
socket.Send(packet);

Receiving:
// receiving
sf::Packet packet;
socket.Receive(packet);

sf::Uint8 header;
packet >> header;

switch (header)
{
    case ConnectionRequest:
        packet >> ... specific data ...;
        connect(...);
        break;

    case MoveObject:
        packet >> ... specific data ...;
        moveObject(...);
        break;

    ...
}

I think this is a great idea, but I wanted to make sure with you guys if you all think it's the right way to do this before I start.

Also, on the receiving side, inside the switch, each case has:
packet >> ...specific data...;

I'm wondering if that"specific data" could be a user type?

For example:

struct Character
{
    sf::Uint8 age;
    std::string name;
    float height;
};

sf::Packet& operator <<(sf::Packet& packet, const Character& character)
{
    return packet << character.age << character.name << character.height;
}

sf::Packet& operator >>(sf::Packet& packet, Character& character)
{
    return packet >> character.age >> character.name >> character.height;
}

struct Enemy
{
        sf::Uint16 health;
        sf::Uint16 power;
        sf::Uint8 type;
};

sf::Packet& operator <<(sf::Packet& packet, const Enemy& enemy)
{
    return packet << enemy.health << enemy.power << enemy.type;
}

sf::Packet& operator >>(sf::Packet& packet, Enemy& enemy)
{
    return packet >> enemy.health >> enemy.power >> enemy.type;
}

// receiving
sf::Packet packet;
socket.Receive(packet);

sf::Uint8 header;
packet >> header;

switch (header)
{
    case isCharacter:
                Character billy;
        packet >> billy;
        break;

    case isEnemy:
                Enemy boss;
        packet >> boss;
        break;

    ...
}

I guess what I'm asking is if the header value could be separate from the user type?

3
Network / TcpSockets, Selector, Listner, Frame Rate Dropping
« on: August 05, 2014, 02:50:37 pm »
I'm at the point in my game where I want to start doing networking to play with my friends. My server and client code is minimal, and they're not even sending or receiving from each other yet. For now, I'm controlling the environment by only allowing one client to connect, and I'm running the client and server on the same laptop.

The server/client code is run in a separate thread than all my game code. The thread first asks what the user is, a sever or a client, then it goes from there.

The problem is, once the client connects to the server, the server's game frame rate drops drastically, and only sometimes the client does too. But mostly just the server. I'm not sending anything back and forth yet, I only allow the client to connect to the server.

Right when I disconnect the client, the server then runs perfectly normal. Its frame rate only drops when connected.

Here is the entire code for the server and clients.
void runNet(void* UserData)
  {

          SoccerLevelsClass* obj = static_cast<SoccerLevelsClass*>(UserData);

          obj->netQuit=false;

          obj->netType = 0;

          cout<<"What are you? 0 for server, 1 for client : ";
         
          cin>>obj->netType; //store network type( 0 for sever, 1 for client)
         
          cout<<"\n";

          // start of server code
          if(obj->netType==0)
          {
                  // Create a socket to listen to new connections
                  sf::TcpListener listener;
                  listener.listen(2000);

                  // Create a list to store the future clients
                  std::vector<sf::TcpSocket*> clients;

                  // Create a selector
                  sf::SocketSelector selector;

                  // Add the listener to the selector
                  selector.add(listener);

                  listener.setBlocking(false);
                  // Endless loop that waits for new connections
                  while (obj->netQuit==false)
                  {
                         // cout<<"waiting.\n";

                          // Make the selector wait for data on any socket
                          if (selector.wait(sf::milliseconds(1)))
                          {
                                  cout<<"done waiting. listening.\n";

                                  // Test the listener
                                  if (selector.isReady(listener))
                                  {
                                          cout<<"trying to add client.\n";

                                          // The listener is ready: there is a pending connection
                                          sf::TcpSocket* client = new sf::TcpSocket;
                                          if (listener.accept(*client) == sf::Socket::Done)
                                          {
                                                  client->setBlocking(false);
                                                 
                                                  // Add the new client to the clients list
                                                  clients.push_back(client);

                                                  // Add the new client to the selector so that we will
                                                  // be notified when he sends something
                                                 
                                                  selector.add(*client);
                                                  cout<<"added client.\n";
                                          }
                                          else
                                          {
                                                  // Error, we won't get a new connection, delete the socket
                                                  delete client;
                                          }
                                  }
                                  else
                                  {
                                          cout<<"testing clients.\n";

                                          // The listener socket is not ready, test all other sockets (the clients)
                                          for (std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
                                          {
                                                  sf::TcpSocket& client = **it;
                                                  if (selector.isReady(client))
                                                  {
                                                   /*
                                                          // The client has sent some data, we can receive it
                                                          sf::Packet packet;
                                                          packet << obj->playerOneStruct;
                                                          client.send(packet);
                                                          packet.clear();
                                                          if (client.receive(packet) == sf::Socket::Done)
                                                          {
                                                                 // ...
                                                          }
                                                   */

                                                  }
                                          }
                                  }
                          }
                  }
          }// end of server code

          // start of client code
          if(obj->netType==1)
          {
                  sf::IpAddress server;
                  do
                  {
                          std::cout << "Type the address or name of the server to connect to: ";
                          std::cin  >> server;
                  }
                  while (server == sf::IpAddress::None);


                  cout<<"here1\n";
                  sf::TcpSocket socket;

                  while(socket.connect(server,2000) != sf::Socket::Done)
                  {

                  }
                  cout<<"connected.\n";
                  socket.setBlocking(false);

                  while(obj->netQuit==false)
                  {
                   /*
                         sf::Packet packet;
                          sf::Socket::Status status = socket.receive(packet);
                          if (status == sf::Socket::Done)
                          {
                                  packet >> obj->playerOneStruct;
                                  cout<<"herh\n";
                          }
                          else if(status == sf::Socket::NotReady)
                          {
                                  cout<<"not ready.\n";
                          }
                          else if(status == sf::Socket::Disconnected)
                          {
                                  cout<<"disconnected.\n";
                          }
                          else if(status == sf::Socket::Error)
                          {
                                  cout<<"error.\n";
                          }
                         
                   */

                  }

          }// end of client code
  }// end of thread function

I commented out all the code I thought would cause my server to lag, but it's still lagging. I don't understand why it's causing my game to drop its frame rate, especially when it runs in a separate thread.

So here are my questions:

1. Is this code okay? Do you see anything that would make it lag the computer?

2.Do you think it's because I'm running the client and server on the same machine? Could that be the reason why the frame rate is dropping?

Thank You

4
For example: a 500 x 500 image saved as .png will require more memory than if it was a .jpg.

So do images that require less space (in terms of memory) make the game
1. run faster? (as in draw faster, or just anything related to fps)
2. load faster?
3. or nothing at all?

5
Audio / Best way to handle sounds?
« on: June 16, 2014, 04:52:28 pm »
I'm getting to the point where I think I should start managing my sounds better. While I don't have many sounds now, I will eventually, and I want to start a good sound system before it's too late. I have 2 ideas for managing sounds.

First idea : manage them like a sprite sheet. Put all sounds into one file (soudbuffer) like a texture, and just set the playing offset to whichever sound I need, and stop it when that specific sound ends, like setting the texture rectangle. The thing is I have to have a sf::Time variable to comper to where the sound is constantly to stop it. But it's doable.

Second idea : make a lot of soundbuffers for each sound, and just have one sound that switches between them.

I understand that both of these methods mean you can't play multiple sounds at once, but that's how i want it. I will have multiple sounds, but each sound buffer or group of soundbuffers will be related somehow. So...anybody have any ideas or suggestions?

6
Graphics / Clear or reset a texture?
« on: March 27, 2014, 08:42:24 pm »
I'm having issues with textures. I want to use a group of textures over and over again. This is what i'm doing.
1. Create all textures of certain size.
2. update textures from image takes up the whole texture space.
3. update textures from a different image that is smaller than the textures.
~~~~~this keeps the un-updated parts of the texture unaltered(because the new image is smaller), and the space the new image takes up update correctly.

So basically the update function doesn't clear the texture. Is there anyway to clear a texture? I tried the loadFromImage() function, but it stretches the image to fit the texture size sometimes. I don't know why it does that, but it looks very weird.

7
Graphics / Copy only part of an image. Help with copy function.
« on: March 10, 2014, 05:47:06 pm »
Is there a way to copy only part of an image into another image?
I don't get the copy function.
What is DestX and DestY? is it where the new image will stop copying pixels? If so, then i don't get the IntRect that is also passed and its purpose.

For example, i'm trying to create 3 different images from one image.
I'm setting the DestX and DestY to 0, which is probably wrong.
And then i'm setting the IntRect to the area i want the new image to copy from the image Maps.

Doing this result in an error in my command prompt: "Trying to access the pixels of an empty image"

I will keep trying though.

sf::Image Maps;
Maps.loadFromFile("MapSelect.png");
sf::Image map1;
map1.copy(Maps,0,0,sf::IntRect(0,0,640,360));
sf::Image map2;
map2.copy(Maps,0,0,sf::IntRect(640,0,640,360));
sf::Image map3;
map3.copy(Maps,0,0,sf::IntRect(1280,0,640,360));

8
Graphics / Shader is running really slow. Not sure what it is.
« on: February 17, 2014, 02:10:57 am »
here is the entire code...

#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
sf::Shader testShader;
sf::RectangleShape testShape;
sf::RenderTexture renderTexture;
sf::Texture texture;
sf::Sprite sprite;
sf::Clock time1;
float lastTime;
int main(){
   sf::RenderWindow Window;
   Window.create(sf::VideoMode(1280, 720, 32), "Test");
   //Window.setFramerateLimit(60);
   sf::Event event;
   testShape.setSize(sf::Vector2f(500,500));
   testShader.loadFromFile("wave2.vert",sf::Shader::Fragment);
   renderTexture.create(500,500);
   while (Window.isOpen())
    {
      while (Window.pollEvent(event))
      {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
               Window.close();
      }
         /** When the user left-mouse-click, add a box into the world */
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
      {
          Window.close();
      }
      Window.clear(sf::Color::Black);
      testShader.setParameter("resolution",100,100);
      testShader.setParameter("time",time1.getElapsedTime().asSeconds());
      renderTexture.clear();
      renderTexture.draw(testShape,&testShader);
      renderTexture.display();
      texture = renderTexture.getTexture();
      sprite.setTexture(texture);
      Window.draw(sprite);
      Window.display();
      float currentTime = time1.getElapsedTime().asSeconds();
        float fps = 1.f / (currentTime - lastTime);
        lastTime = currentTime;
      cout<<"FPS: "<<fps<<"\n";
   }
   return 0;
}

does this look wrong to anybody? the fps I'm getting is about 7 without a framerate limit. If this looks right I guess it could be the shader itself. I implented the one shown here:
http://glsl.heroku.com/e#8067.3
i just wanted to see if i could get a shader working at all, but it is really slow. Does anybody have answers?

9
Graphics / possible to make setColor have opposite effect?
« on: January 28, 2014, 06:17:00 am »
I want to know if there's a way to make the setColor method for sprites change the blacks instead of whites. For example i want the whites of the image to stay white, and the blacks to turn to the color i pass.

Pages: [1]