Jump to content
  • Advertisement
Sign in to follow this  
nhold

OpenGL Tiling Halp! [Solved]

This topic is 4051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all and thanks for taking the time to read about my problem. I am trying to implement a simple tile drawing function, I have searched Gdnet and found: http://www.gamedev.net/reference/articles/article1256.asp however I already had pretty much the same as this. The problem is that my program becomes somewhat non-responsive when I use the function. I have tried using a Sleep (1); and changed a bunch of things around and still not working : /.
// I made this function to make it a little easier to draw a sqaure
void DrawTile(GLfloat x,GLfloat y)
{
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(x + 1, y, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x + 1, y + 1, 0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + 1, 0.0f);
    glEnd();

}


// This is the function to draw the tiles (duh)
bool TileDrawer(GLvoid)
{ 
int tile; // What tile are we drawing!?

// Display the map.

  for (int y=0; y < MAP_SIZEY; y++) 
  {
    for (int x=0; x < MAP_SIZEX; x++)
    {
     tile = TileMapA[y][x];
     
     switch (tile)
     {
     case 1:
     WallA.LoadToTexture();
     DrawTile(x,y);
     break;
     case 2:
     FloorA.LoadToTexture();
     DrawTile(x,y);
     }
    }
  }
  return (true);
}


// Here is the main loop.
while(!done) // Loop That Runs While done=FALSE
	{
..... // Code you dont need to see

// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
if ((active && !DrawGLScene()) || keys[VK_ESCAPE])// Active?  Was There A Quit Received?
{
  done=TRUE;    // ESC or DrawGLScene Signalled A Quit
}
else	// Not Time To Quit, Update Screen
{
  SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}


Also I'm doing this in Dev-C++, It's a personal preference. [Edited by - CRYP7IK on July 13, 2007 9:33:16 PM]

Share this post


Link to post
Share on other sites
Advertisement
And what is the result when this runs? DrawTile looks fine, but it doesn't allow for smooth scrolling ( also, why not just use glVertex2* ). The second case isn't breaking properly. And... what do you mean it isn't unresponsive? Post a pic of the output. Also, the dimensions of the tiles aren't being considered ( unless they are all 1x1 ), which would cause a lot of overlapping tiles...

ahayweh

Share this post


Link to post
Share on other sites
I mean when you try to close it, resize it, move it around after a while it becomes unresponsive and you have to alt+ctrl+del end task.


Getting some screenshots for you.

http://img166.imageshack.us/img166/7421/tileglgh5.png - 2 seconds in.

http://img178.imageshack.us/img178/3788/tileglbxl5.png - ~10 seconds in, trying to close been resized.

Share this post


Link to post
Share on other sites
Quote:
after a while it becomes unresponsive and you have to alt+ctrl+del


So the program starts off responsive and then gradually becomes less responsive? Have a look at your program's memory usage. You may have a bad memory leak which is causing everything to go to swap after a while.

Share this post


Link to post
Share on other sites
Yes, but this problem only occurs when i use TileDrawer function and so i don't know what i can do...:S

Share this post


Link to post
Share on other sites
Strange... Is there any problem w/LoadToTexture? If not, then I would have to agree w/cryp7ik. And, try with the break properly added, not that that would change anything major...

Share this post


Link to post
Share on other sites
I really appreciate the help guys!

LoadToTexture just does this:


GLuint TGAImage::LoadToTexture()
{

glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, GetWidth(), GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, GetImage());

return (m_texture);
}




I can put WallA.LoadToTexture 10+ times without it becoming unresponsive.

Share this post


Link to post
Share on other sites
10 times,but what about GL_MAX_TEXTURES_SGIS times? Everything looks fine, I can't really see the problem based on what you've posted... Still thinking though..

Share this post


Link to post
Share on other sites
You indeed have a huge memory leak, not to mention the efficiency problems! On every frame, you're allocating space for a *new* texture, then uploading the texture image from RAM to the card, then computing mipmaps for the texture. Your video card's RAM gets filled up with identical textures that aren't used (or even accessible!) anywhere, and when it's full, it starts getting paged to main RAM and back, slowing things down tremendously.

Create the textures you need *once* at the program start, and save the texture names GL gives you. Then, when you need to use a given texture, just call glBindTexture(texname) and draw the stuff you want.

EDIT: It's even a lot worse than I first thought; you're creating a new texture for every TILE! If MAP_SIZE_Y and MAP_SIZE_X are both 20, and your program is running at 20fps, that's *8000* totally unnecessary new textures each second!

[Edited by - Sharlin on July 13, 2007 9:49:50 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by ahayweh
10 times,but what about GL_MAX_TEXTURES_SGIS times? Everything looks fine, I can't really see the problem based on what you've posted... Still thinking though..


Well I wont even be using 10 textures with this game so I dont need to test that ^_^. I have gotten this working properly before on my computer but lost the source.

Quote:

You indeed have a huge memory leak, not to mention the efficiency problems! On every frame, you're allocating space for a *new* texture, then uploading the texture image from RAM to the card, then computing mipmaps for the texture. Your video card's RAM gets filled up with identical textures that aren't used (or even accessible!) anywhere, and when it's full, it starts getting paged to main RAM and back, slowing things down tremendously.

Create the textures you need *once* at the program start, and save the texture names GL gives you. Then, when you need to use a given texture, just call glBindTexture(texname) and draw the stuff you want.


Ok, that works! Thanks for all the help and now I feel like an idiot...
I guess thats what I get for being up at 12:21 AM eh? rating++!

[Edited by - CRYP7IK on July 13, 2007 9:50:14 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!