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

Author Topic: texture rectangle problem  (Read 944 times)

0 Members and 1 Guest are viewing this topic.

croux

  • Newbie
  • *
  • Posts: 15
    • View Profile
texture rectangle problem
« on: April 02, 2014, 12:29:49 am »
Hi,

I am making 2d voxel game and i have the following problem.
Instead of view i just draw the voxels around camera.
But the problem is with the voxels which are not fully visible.

Since sfml cannot draw below 0 and its more effective to just draw the visible part i choosed to use setTextureRect function. So here is my code:

void Draw::drawEarth(sf::Vector2i pos,char id){

   sf::Sprite s;
   s.setTexture(*((*data).getTexEarth(id)),true);

   if(pos.x<0){ //i have to cut voxel
//for example if pos.x=-8 then i have to draw 8 to 20 -->   12 pixels
//my voxels scale is 20*20 pixels
      s.setTextureRect(sf::IntRect(-pos.x,0,20,20));

      s.setPosition(0,pos.y);
      (*window).draw(s);
   }
   else{ //the whole voxel will be visible
      s.setTextureRect(sf::IntRect(0,0,20,20));
      s.setPosition(pos.x,pos.y);
   }
   (*window).draw(s);
}
But the output is this:

http://postimg.org/image/k5fzdw8vh/
(the right, bottom part of picture is my voxel texture)

So u can see there is full sized sprite with texture which is transformed, but not cutted as it have to be.
The yellow part of texture should not be printed.

So my question is what am i doing wrong.
I would appreciate any help :)






math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: texture rectangle problem
« Reply #1 on: April 02, 2014, 12:44:25 am »
It does what you asked to do.

 s.setTextureRect(sf::IntRect(-pos.x,0,20,20));
 

You take a rectangle of 20 by 20 rather than (20 + pos.x) x 20, pos.x < 0 here. The correct code would be:

 s.setTextureRect(sf::IntRect(-pos.x,0,20+pos.x,20));