Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: geting pixel  (Read 5703 times)

0 Members and 2 Guests are viewing this topic.

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
geting pixel
« on: March 19, 2013, 03:45:17 pm »
So, I have this map image. Some of it is dark some bright. And the players can't go onto the dark parts.
The player is a rectangle which is always in the center of the map, but it rotates.
Any ideas how to check is player on a dark part even a pixel?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: geting pixel
« Reply #1 on: March 19, 2013, 03:53:59 pm »
A more efficient way would be to work with bounding boxes, but if you need the pixel perfect precision, you'll need to access the image information, which you can do by calling texture.copyToImage() and then call image.getPixel(). But make sure you call copyToImage only once or only when the map changes, because it's a heavy operation.

Afterwards you'll have to apply the transformation of your player manually onto the image, to see whether he's over a dark or bright place.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #2 on: March 19, 2013, 05:27:42 pm »
Thanks, I think I got it.

This project is going to be much more complicated than I though.

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #3 on: March 19, 2013, 05:45:47 pm »
I was just thinking about it. You said it is a heavy operation to move texture to image, but I already have the image and I put that image into the texture for every frame, because the map is way too big to fit into the texture and the player is moving most of the time. So I could just use that image and somehow calculate which pixels are needed.

By the way, since I oftenly use different parts of the image I, in the while loop, declate new texture and set the part of the image for displaying to it with loadFromImage. But is there a way to do that only if player moved?

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: geting pixel
« Reply #4 on: March 19, 2013, 06:03:57 pm »
By the way, since I oftenly use different parts of the image I, in the while loop, declate new texture and set the part of the image for displaying to it with loadFromImage. But is there a way to do that only if player moved?


Make a function wich creates the needed textures for the current view, save the textures in a container/array so they wont get deleted after the frame.
And then only call the function if the player moves (maybe in or after the event-queue or in the logic part)


If you do it this way dont forget to clear the container before you call the function


AlexAUT

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #5 on: March 19, 2013, 08:43:02 pm »
Now that I don't set the texture for every frame and set it for elements everyframe, but only when it changes I can realy notice the difference in the task manager.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: geting pixel
« Reply #6 on: March 19, 2013, 10:34:52 pm »
Converting an sf::Image to sf::Texture or the other way around, is still a heavy operation, since your PC will have to upload/download the whole data to/from your GPU.

If the whole map doesn't fit into one sane texture, then you can use multiple textures and just show the needed ones. But it's kind of hard to tell without further context, but I just wanted to point it out. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #7 on: March 19, 2013, 11:58:41 pm »
I can't store the map in mutiple textures since change eeds to be linear.
But I could have a bigger texture that the window I chose and use parts of it and only change when the player comes to the edge of that part, unlike what I use now (change for every pixel, actually 4 pixels for frame is the max speed for now). That would save a lot of time, wouldn't it?

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #8 on: March 21, 2013, 11:21:48 am »
Wouldn't it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: geting pixel
« Reply #9 on: March 21, 2013, 12:02:01 pm »
Well that's what I've kind of told you to do.


So the red part is what the player gets to see and the black part are the different textures.

The nicest solution would be to have just one big texture, but then your game won't run on a bit older or not high-end machines, since their texture size limit is lower. So you can go with x different textures and just 'tile' them next to each other.

For the logic part you could probably use just one big sf::Image and if you properly decouple the rendering and the logic, you won't have a problem at all with the logic part.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #10 on: March 22, 2013, 07:23:08 pm »
I just looked at the map size and a realy good gpu could have it all in one peace, but I don't have so good gpu and probably many other people don't have it either.

But what is bothering me is that I don't know how to use multiple textures as one in sfml?

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #11 on: March 23, 2013, 01:26:53 pm »
So, how do I use multiple textures as one?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: geting pixel
« Reply #12 on: March 23, 2013, 02:10:06 pm »
So, how do I use multiple textures as one?
I don't get your question...

Just use multiple sprites, give each of them a texture and set the position in a way, that their edges are next to each other (as seen in my image).
Or what exactly do you have problems with?
Have you been playing around with it, or have you just been waiting for an answer?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #13 on: March 24, 2013, 12:07:24 pm »
I don't think sprites will work for me.
You see I have those circleshapes around the player and the cursor position and I set a part of the texture to them, so that only that part is visible, because it is underground.
And I have been trying things out.
Now I only have to get 4 pixels of the picture, the corners of the tank picture (the player) and some bot enemies and the game is ready to go.
But it woryes me that the tank rotates much slower when moving, because I reload the texture when moving.

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: geting pixel
« Reply #14 on: March 25, 2013, 03:00:04 pm »
Well looks like I'll have to go with my idea, get max texture size and use that for the map, checnge it only when close to edge.

 

anything