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

Pages: [1]
1
Graphics / Re: GetPixel() in 2.1?
« on: January 20, 2014, 12:51:50 am »
I did this and in release configuration everything works much better. Also now I use const& every time when possible. But I think it can be optimised further and it may start lagging when thee will be like 3 layers on the map.

And you should make sure you don't copy unnecessarily. For example, pass sf::Image and std::string by const-reference, not by value. You could also make isClickable() const, i.e. use map::find() instead of map::operator[].

Isn't operator[] faster than find()?

TY again!

2
Graphics / Re: GetPixel() in 2.1?
« on: January 18, 2014, 10:55:37 pm »
Hi Nexus, thank you for your reply.
I thought about 1bpp data and I've tried but FPS actually lower than with simple Image->getPixel(x,y). Maybe I'm doing something wrong?

std::map<std::string, std::vector<bool>> bitsets; //string contains texture name

after loading the image & texture I create the bitset for this texture:
bitsets[texturename] = calculateBitset(images[texturename]);

std::vector<bool> calculateBitset(sf::Image img){
        sf::Vector2u imgSize = img.getSize();
        std::vector<bool> retVal;
        for(int i = 0; i < imgSize.x; i++){
                for(int j = 0; j < imgSize.y; j++){
                        retVal.push_back(img.getPixel(i,j).a != 255); //false = transparent
                }
        }
        return retVal;
}
then I check if given point is clickable:
bool isClickable(std::string textureName, sf::Vector2u pos){
        int w = images[textureName].getSize().x;
        return !bitsets[textureName][w*pos.y+pos.x];
}
With this code I'm experiencing 6-20 FPS drop (depending on map indexes)

If I load and store only sf::Images and call getPixel() every time, FPS drop is only 1-6 and equally lower cpu load.

3
Graphics / Re: GetPixel() in 2.1?
« on: January 18, 2014, 07:27:05 pm »
I see. Then you could try:
http://sfml-dev.org/documentation/2.1/classsf_1_1Texture.php#aefc19bcd95565dd2348fd4cec0facddc
Ha-ha, actually I came to such idea by myself like 40 minutes ago but I kinda missed the copyToImage() :D
Thanks, I'll give it a try!
---
                        if(tiles[i][j].getGlobalBounds().contains(pos)){
                                sf::Image img = tiles[i][j].getTexture()->copyToImage();
                                sf::Vector2f tilePos = tiles[i][j].getPosition();
                                sf::Color c = img.getPixel(pos.x - tilePos.x, pos.y - tilePos.y);
                                if(c.a != 0){
                                        std::cout<<"Selected tile @ "<<i<<", "<<j<<std::endl;
                                        return;
                                }
                        }
Idk how this solution impacts performance-wise, with 10k tiles it works well. Maybe I should store all images for all of my textures separately.
Thanks again!~

4
Graphics / Re: GetPixel() in 2.1?
« on: January 18, 2014, 02:37:51 pm »
If I understand this correctly, it'll work out just for tiles. But they're all the same size. What if there are different shaped objects? for ex. a tree sprite? Bounding box wont let to click behind and will catch clicks on that tree - that's why I thought about excluding transparent pixels.

5
Graphics / GetPixel() in 2.1?
« on: January 18, 2014, 01:17:36 pm »
Hello!
What am I trying to do is to invent a bicycle make a click detection on isometric tiles. My current approach:
Map::ClickOn(sf::Vector2f pos){
        for(int i = 0; i < 100; i++){
                for(int j = 0; j < 100; j++){
                        if(tiles[i][j].getGlobalBounds().contains(pos)){
                                std::cout<<"Selected tile @ "<<i<<", "<<j<<std::endl;
                                return;
                        }
                }
        }
where 'pos' is the mouse click position. In 3D I'd simply cast a ray but with 2D there are overlapping bounding boxes so it often detects neighbouring tiles.
I thought that I can get all sprites with their bounding boxes are within 'pos' and omit the ones with transparent pixels on click position. sf::Sprite had GetPixel(x,y) method back in 1.6 but now it's gone. Is there any ways to check the color of sprite's each pixel? Or maybe a simplier approach to solve this problem?

TY!

6
Network / How to make my program wait for establishing the connection?
« on: December 13, 2011, 02:27:32 pm »
Lol!
Fixed that by
Code: [Select]
   Client.SetBlocking(true);
    if(Client.Connect(7777, host, 5.f)!=sf::Socket::Done)
        std::cout<<"Connection Error"<<std::endl;
    Client.SetBlocking(false);

and now everything is OK!

7
Network / How to make my program wait for establishing the connection?
« on: December 13, 2011, 01:16:25 pm »
My socket is set to non-blocking.
Client.SetBlocking(false);
When I set it to true, the client hangs. How do I correctly receive packets?
I have HandlePackets() in my main loop:
Code: [Select]
void HandlePackets(){
    while(Client.Receive(packet)==Socket::Done){
            unsigned short PacketID;
            packet >> PacketID;
            switch(PacketID){
                case 0:
                    Func0();
                    break;
                case 1:
                    Func1();
                    break;
                case 2:
                    Func2();
                    break;
                case 3:
                    Func3();
                    break;
                default:
                     break;
            }
        }
}

8
Network / How to make my program wait for establishing the connection?
« on: December 07, 2011, 08:40:30 pm »
Thanks for reply.

Well, this is what happens when I run code 1:
I press "Login".
Nothing happens but my server says that client has connected.
I press "Login" one more time.
The packet with username and password is delivered successfully.

and with code 2:
I press "Login".
Server says that client has connected.
3-4 seconds passess while counting to 5000.
The packet is delivered.

or you suggested to make something like while(client.connect(port,host)!=Socket::Done) waiting loop?
please forgive my noobing :roll:

9
Network / How to make my program wait for establishing the connection?
« on: December 07, 2011, 02:59:53 pm »
Hello.
My SFML-Network works fine on LAN but there is a problem connecting to remote host on the Internet. It seems that my code trying to send a packet immediately after the connection attempt (program needs more time to connect).
My code:
Code: [Select]
if(Client.Connect(7777, host)!=Socket::Done){
  std::cout<<"Connection Error"<<std::endl;
}

Next goes the sending the first packet.
There is no problem with this code (print to 5000 delays my app for 3-4 seconds):
Code: [Select]
   if(Client.Connect(7777, host)){
       while(i<5000){
           std::cout<<i<<std::endl;
           i++;
       }
    }

How do I delay my program for the time needed to connect to remote host?

Pages: [1]