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

Author Topic: Drawing form custom class lags the program up...  (Read 3214 times)

0 Members and 1 Guest are viewing this topic.

chrbarrol

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing form custom class lags the program up...
« on: January 30, 2011, 06:30:12 pm »
I'm currently making a 2d game and I'm currently in the process of making the map class that draws on the screen when the function drawMap() is called in the main class.

But when I run the program it starts lagging to the point when I can't almost press escape to quit...

Here's the code used to draw and get Images:

In ImageHolder.h
Code: [Select]
sf::Image *loadImage(char *path)
{
//Preload the Image
sf::Image imgHold;
if(!imgHold.LoadFromFile(path))
{
//Error
}
//If success:
else
{
//Checks for matching Image and returns
//100 if no image was found and the Image's location if it did.
int checkres = checkForImageMatch(path);
if(checkres == 100)
{
//If no match was found, find empty space to put image
//Returns 100 if there aren't any space
int elem = checkForNullImage();
if(elem != 100)
{
//Do the stuff to put the Image in
//the right space and catalouge the filename.
FileNameArray[elem] = path;
imgArray[elem].Copy(imgHold,0,0);
std::cout << "Image copy success! At: " << elem << std::endl;
//Return the address of that newly stored Image.
return &imgArray[elem];
}
else
{
//Error if there wasn't any space left...
std::cout << "NullCheck returned 100" << std::endl;

}
}
else
{
//If Image is already stored in memory, return the address
//to that image.
return &imgArray[checkres];
}

}

}


In map.h
Code: [Select]
//Get the ImageHolder in main.cpp
map::map(ImageHolder *ImgHold)
{
//Make pointers point to the grass and loam images in ImageHolder.h
iGrass = ImgHold->loadImage("C:\\Users\\Christian\\Documents\\Visual Studio 2008\\Projects\\NostrumEditor\\Debug\\grass.png");
iLoam = ImgHold->loadImage("C:\\Users\\Christian\\Documents\\Visual Studio 2008\\Projects\\NostrumEditor\\Debug\\loam.jpg");
//Make everything grass since I haven't made loading yet.
for(int i = 0;i<30;i++)
{
for(int j = 0;j<30;j++)
{
mapArray[i][j] = GRASS;
}

}
//For every element in the Sprite array set images and their positions
//Note: 0 is grass and 1 is loam
for(int i = 0;i<30;i++)
{
for(int j = 0;j<30;j++)
{
if(mapArray[i][j] == GRASS)
SpriteArray[i][j].SetImage(*iGrass);
else
SpriteArray[i][j].SetImage(*iLoam);

SpriteArray[i][j].SetPosition(i*32,j*32);

}

}
}


Code: [Select]
//Draw map by getting RenderWindow from main
//and using the sprites from this class.
//This is where I suspect the problem is.
void drawMap(sf::RenderWindow *App)
{
for(int i = 0;i<30;i++)
{
for(int j = 0;j<30;j++)
{

App->Draw(SpriteArray[i][j]);
}

}
}


In main.cpp
Code: [Select]
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 800, 32),"Editor");
ImageHolder vImageHolder;
map curMap(&vImageHolder);
while(App.IsOpened())
{
//Event Handler
while (App.GetEvent(Event))
{
if(getKeyPressed(256))
{
App.Close();
}
}

curMap.drawMap(&App);
App.Display();
}



}


What I suspect the problem is, is that for some reason it copies the images I use in every loop which I don't understand why it does...

Might have something to do with "App->Draw(SpriteArray[j]);"

Also even though it lags, I can see all the images and their positions are correct.

Also please excuse my poor code syntax I've only coding for half a year.

chrbarrol

  • Newbie
  • *
  • Posts: 7
    • View Profile
Found bug possibly...
« Reply #1 on: January 30, 2011, 09:35:33 pm »
I don't know but I think I might have stumbled upon a bug...

When I changed the picture format of loam.jpg to loam.png all the lag disappeared. I decided to try it out again so I changed it back to jpg form again and guess what, it started lagging again.

Any idea why this might happen?

Code changed from:
 
Code: [Select]
//Make pointers point to the grass and loam images in ImageHolder.h
iGrass = ImgHold->loadImage("C:\\Users\\Christian\\Documents\\Visual Studio 2008\\Projects\\NostrumEditor\\Debug\\grass.png");
iLoam = ImgHold->loadImage("C:\\Users\\Christian\\Documents\\Visual Studio 2008\\Projects\\NostrumEditor\\Debug\\loam.jpg");

To:
Code: [Select]
//Make pointers point to the grass and loam images in ImageHolder.h
iGrass = ImgHold->loadImage("C:\\Users\\Christian\\Documents\\Visual Studio 2008\\Projects\\NostrumEditor\\Debug\\grass.png");
iLoam = ImgHold->loadImage("C:\\Users\\Christian\\Documents\\Visual Studio 2008\\Projects\\NostrumEditor\\Debug\\loam.png");

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Drawing form custom class lags the program up...
« Reply #2 on: January 30, 2011, 09:46:27 pm »
Jpg sucks : ) I dont know what the problem is, but really, if you use PNG for everything you will be well-served. PNG does great! ;D

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Drawing form custom class lags the program up...
« Reply #3 on: January 31, 2011, 01:23:14 am »
Jpg is not compressed so it looks really nice up close but that also means it has a lot to load. Png is almost as great and doesn't usually lag..

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Drawing form custom class lags the program up...
« Reply #4 on: January 31, 2011, 02:23:14 am »
Why are you using jpeg?


Quote
Jpg is not compressed so it looks really nice up close but that also means it has a lot to load. Png is almost as great and doesn't usually lag..


PNG is lossless
JPEG is lossy

Therefore a PNG encoding will always look better* than a jpeg encoding of the same image.  Unless you get a lossless encoding of a jpeg (unlikely)...  maybe at "best" compression settings jpeg will be lossless, but I'm not sure.

On top of that, PNG has transparency/alpha layers.

The only reason to choose jpeg over png is filesize.  A lossy jpeg will typically compress to a much smaller file than a lossless png.  But is that a concern here?  How big is the image in question?



* by "better" I mean "closer to the original image".  A png will be idenential to-the-pixel of the original image, whereas the jpeg will create artifacts.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Drawing form custom class lags the program up...
« Reply #5 on: January 31, 2011, 02:32:35 am »
Quote from: "Fierce_Dutch"
Jpg is not compressed so it looks really nice up close but that also means it has a lot to load. Png is almost as great and doesn't usually lag..

o.o

You should read a bit more about the JPEG format before saying that...
http://en.wikipedia.org/wiki/JPEG
Want to play movies in your SFML application? Check out sfeMovie!

chrbarrol

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing form custom class lags the program up...
« Reply #6 on: January 31, 2011, 08:11:32 am »
Quote from: "Disch"
Why are you using jpeg?


It was only a dev build never would've thought it would make such a difference. Just thought I'd get it out there just in case you guys didn't know(which you did)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Drawing form custom class lags the program up...
« Reply #7 on: January 31, 2011, 08:44:22 am »
This is technically impossible, once loaded there's no more format, just an array of RGBA pixels. The only reason why the image format could have such an impact, is that you're loading your images before every draw, instead of once at startup. From the code above this doesn't seem to be true, but can you show us a minimal and complete code that reproduces this problem? Just to be sure ;)

By the way, you should avoid "magic" numbers like 30, 100, 256. Use named constants instead.
Laurent Gomila - SFML developer

chrbarrol

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing form custom class lags the program up...
« Reply #8 on: January 31, 2011, 09:16:52 pm »
Here's the most lightweight version I bothered making:
http://dl.dropbox.com/u/5168131/source.zip

Just put the images in the same folder as the exe I'm guessing you can figure the rest out by yourselves.

Hope someone has the time to look over it. :)
Also for some reason even though I started using only PNG again it started lagging now I'm really confused since I can't remember doing anything to it...

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Drawing form custom class lags the program up...
« Reply #9 on: January 31, 2011, 09:39:04 pm »
Code: [Select]
sf::RenderWindow App(sf::VideoMode(800, 800, 32),"Editor");
ImageHolder vImageHolder;
//Our test map.
map curMap(&vImageHolder);
while(App.IsOpened())
{
//Event Handler(We don't need this now...)
sf::Event Event;
while (App.GetEvent(Event))
{
/*if(getKeyPressed(256))
{
App.Close();
}*/
}


//Draw functions
curMap.drawMap(&App);
App.Display();
}


Uh, it does work fine for me, dont know whats wrong for you, you do need to handle events or the window will be screwd :D

chrbarrol

  • Newbie
  • *
  • Posts: 7
    • View Profile
Drawing form custom class lags the program up...
« Reply #10 on: January 31, 2011, 10:40:16 pm »
:shock: Oh wow can't see how I missed that, then there's really no problem, still kinda strange about that whole jpg thing though.

 

anything