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

Author Topic: Weird drawing issue  (Read 1847 times)

0 Members and 1 Guest are viewing this topic.

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Weird drawing issue
« on: November 10, 2011, 12:10:29 am »
Hello.  I've been creating a tile engine based off of Nick Gravelyn's "old" tile engine tutorials(which are made with C#/XNA).  I'm doing mine in C++/SFML but I seem to be having some issues drawing the tiles.  The code below is the function that I'm currently using to draw the tiles.  I'm encountering a few problems:
1.  First of all, it takes at least four seconds to draw these 70 tiles
2.When it's done drawing, I end up with a checkerboard of empty spaces and tiles(alternating)
3.While drawing, all the tiles blink, even where there should be a tile in a blank spot.

Hopefully these can all be fixed in one fell swoop :)

Code: [Select]
void drawTiles(sf::RenderWindow &app)
 {

for(int x = 0; x < tileMapWidth; x++)
{
for (int y = 0; y < tileMapHeight; y++)
{
int* textureIndex = tileMap[y, x];
sf::Texture texture = tileTextures[0]; //[*textureIndex+1];

sf::Sprite sprite;
sprite.Scale(.1875, .1875); // 48/256 (tileWidth/size of original image)
sprite.SetTexture(texture);
sprite.SetPosition(x*tileWidth,y*tileHeight);
app.Draw(sprite);
app.Display();


}
}
 }


If you have any questions/comments on the code, feel free to ask.  I can post the whole thing if needed, but I'll see if anyone can tell what's wrong by just seeing this function.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Weird drawing issue
« Reply #1 on: November 10, 2011, 01:39:46 am »
I guess you're you using separate images for each tile... Am I right?
You can use one big image with all tiles on it, but if you are following a tutorial that will give you some trouble.

Why are you scaling the sprites before drawing?

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Weird drawing issue
« Reply #2 on: November 10, 2011, 03:07:21 am »
Quote from: "Tex Killer"
I guess you're you using separate images for each tile... Am I right?
You can use one big image with all tiles on it, but if you are following a tutorial that will give you some trouble.

Why are you scaling the sprites before drawing?


Yeah, I'm using separate images at the moment.  I'm using the tutorial as more of a guideline; I think I could easily just use one image and divide it up for the sprites I need.  I'm not sure if this will fix my problem though.

As for the scaling, I'm not sure heh.  I've been trying all different orders in which the drawing lines are organized and nothing seems to work.  I'm quite sure the problem lies in this code too but perhaps not.

EDIT:Also, if I take the
Code: [Select]
sf::texture Texture = tileTextures[0];

part out of the function then it executes over twice as fast, however I need to keep it inside the function as when I draw the texture I first get it from the array so I know which tile to draw.  Perhaps having one image could speed this up somehow.

Bones

  • Newbie
  • *
  • Posts: 24
    • View Profile
Weird drawing issue
« Reply #3 on: November 10, 2011, 03:21:23 am »
Okay, I moved the App.display() part out of the loop so it only has to execute once.  I swore I've tried this before but it seems to work now.  I'll keep you guys posted :P

EDIT:Also noticed my arrays are weird...I'll fix that.

EDIT2:Alright I had a few more problems that I fixed.  Everything's perfect now!  Programming is addicting in a weird way...anyways here's the fixed code.  If you have any more suggestions for improvement, fire away!

Code: [Select]
void drawTiles(sf::RenderWindow &app)
 {
for(int x = 0; x < tileMapWidth-1; x++)
{
for (int y = 0; y < tileMapHeight-1; y++)
{
int textureIndex = tileMap[y][x];
sf::Texture texture = tileTextures[textureIndex];
sf::Sprite sprite;
sprite.Scale(.1875, .1875);
sprite.SetTexture(texture);
sprite.SetPosition(x*tileWidth,y*tileHeight);

app.Draw(sprite);
}
}
app.Display();
 }

TheCake

  • Newbie
  • *
  • Posts: 19
    • View Profile
Weird drawing issue
« Reply #4 on: November 11, 2011, 02:04:23 pm »
Hi

I don't think copying a texture in the loop is a good idea. Maybe you could use a reference ?

Also, what is the purpose of the sprite scaling ?