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
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
//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);
}
}
}
//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
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.