   Tile Engine Animation
  Hello everyone, I just finished putting a character into a tilemap last night. It is supposed to be animated, but the character won't animate. Can someone tell me how to get the character to animate right?#include <windows.h>#include <gl/gl.h>#include <gl/glext.h>LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);bool loadBMP(LPTSTR filename, GLuint &texid);int initGL();void draw();GLuint textures[4];GLuint person[4];int map[10][10] = {{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 1, 2, 0, 1, 2, 0, 2, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}};int x = 0;int y = 0;int left, right, up, down;float px = -0.7;float py = -0.3;int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){    WNDCLASS wc;    HWND hWnd;    HDC hDC;    HGLRC hRC;    MSG msg;    BOOL bQuit = FALSE;    wc.style = CS_OWNDC;    wc.lpfnWndProc = WndProc;    wc.cbClsExtra = 0;    wc.cbWndExtra = 0;    wc.hInstance = hInstance;    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wc.hCursor = LoadCursor (NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);    wc.lpszMenuName = NULL;    wc.lpszClassName = "taa";    RegisterClass (&wc);    hWnd = CreateWindow ( "taa", "Tiling and Animation", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0, 0, 640, 640, NULL, NULL, hInstance, NULL);    EnableOpenGL (hWnd, &hDC, &hRC);    initGL();    while (!bQuit)    {   if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))   {  if (msg.message == WM_QUIT)  { bQuit = TRUE;  }  else  { TranslateMessage (&msg); DispatchMessage (&msg);  }   }   else   {  draw();  SwapBuffers (hDC);  Sleep (1);   }    }    DisableOpenGL (hWnd, hDC, hRC);    DestroyWindow (hWnd);    return msg.wParam;}LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    switch (message)    {    case WM_CREATE:   return 0;    case WM_CLOSE:   PostQuitMessage (0);   return 0;    case WM_DESTROY:   return 0;    case WM_KEYDOWN:   switch (wParam)   {   case VK_ESCAPE:  PostQuitMessage(0);  return 0;   case VK_UP:  //y += 4;  up =1;  break;   case VK_DOWN:  //y -= 4;  down = 1;  break;   case VK_LEFT:  //x -= 4;  left = 1;  break;   case VK_RIGHT:  //x += 4;  right = 1;  break;   }   return 0;    default:   return DefWindowProc (hWnd, message, wParam, lParam);    }}void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC){    PIXELFORMATDESCRIPTOR pfd;    int iFormat;    *hDC = GetDC (hWnd);    ZeroMemory (&pfd, sizeof (pfd));    pfd.nSize = sizeof (pfd);    pfd.nVersion = 1;    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;    pfd.iPixelType = PFD_TYPE_RGBA;    pfd.cColorBits = 24;    pfd.cDepthBits = 16;    pfd.iLayerType = PFD_MAIN_PLANE;    iFormat = ChoosePixelFormat (*hDC, &pfd);    SetPixelFormat (*hDC, iFormat, &pfd);    *hRC = wglCreateContext( *hDC );    wglMakeCurrent( *hDC, *hRC );}void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC){    wglMakeCurrent (NULL, NULL);    wglDeleteContext (hRC);    ReleaseDC (hWnd, hDC);}bool loadBMP(LPTSTR filename, GLuint &texid){HBITMAP hBMP;BITMAPBMP;glGenTextures(4, &texid);hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), filename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );if (!hBMP)return FALSE;GetObject(hBMP, sizeof(BMP), &BMP);glPixelStorei(GL_UNPACK_ALIGNMENT, 4);glBindTexture(GL_TEXTURE_2D, texid);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, BMP.bmBits);DeleteObject(hBMP);}int initGL(){    glEnable(GL_TEXTURE_2D);    if(!loadBMP("texture0.bmp", textures[0]) || !loadBMP("texture1.bmp", textures[1]) ||  !loadBMP("texture2.bmp", textures[2]) || !loadBMP("texture3.bmp", textures[3]) ||  !loadBMP("person0.bmp", person[0]) || !loadBMP("Person1.bmp", person[1]) ||  !loadBMP("person2.bmp", person[2]) || !loadBMP("Person3.bmp", person[3]))    {   MessageBox(NULL, "H", "H", NULL);   return 0;    }    glShadeModel(GL_SMOOTH);    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    return 1;}void draw(){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glLoadIdentity();    glPushMatrix();    for(float y=0; y<10; y++)    {   for(float x=0; x<10; x++)   {  glBindTexture(GL_TEXTURE_2D, textures[map[(int)x][(int)y]]);  glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2f((x / 10) - 1, (y / 10) - 1); glTexCoord2f(0.0, 1.0); glVertex2f((x / 10) - 1, ((y + 1) / 10) - 1); glTexCoord2f(1.0, 1.0); glVertex2f(((x + 1) / 10) - 1, ((y + 1) / 10 - 1)); glTexCoord2f(1.0, 0.0); glVertex2f(((x + 1) / 10) - 1, (y / 10) - 1);  glEnd();   }    }    if(left == 1)    {   for(int i=0; i<32768; i++)   {  if(i%8192 == 0)  {  px -= 0.025;  glBindTexture(GL_TEXTURE_2D, person);  glBegin(GL_QUADS); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);  glEnd();  }   }   left = <span class="cpp-number">0</span>;    }    <span class="cpp-keyword">if</span>(right == <span class="cpp-number">1</span>)    {   <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i&lt;<span class="cpp-number">32768</span>; i++)   {  <span class="cpp-keyword">if</span>(i%<span class="cpp-number">8192</span> == <span class="cpp-number">0</span>)  {  px += <span class="cpp-number">0</span>.<span class="cpp-number">025</span>;  glBindTexture(GL_TEXTURE_2D, person);  glBegin(GL_QUADS); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);  glEnd();  }   }   right = <span class="cpp-number">0</span>;    }    <span class="cpp-keyword">if</span>(up == <span class="cpp-number">1</span>)    {   <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i&lt;<span class="cpp-number">32768</span>; i++)   {  <span class="cpp-keyword">if</span>(i%<span class="cpp-number">8192</span> == <span class="cpp-number">0</span>)  {  py += <span class="cpp-number">0</span>.<span class="cpp-number">025</span>;  glBindTexture(GL_TEXTURE_2D, person);  glBegin(GL_QUADS); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);  glEnd();  }   }   up = <span class="cpp-number">0</span>;    }    <span class="cpp-keyword">if</span>(down == <span class="cpp-number">1</span>)    {   <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i&lt;<span class="cpp-number">32768</span>; i++)   {  <span class="cpp-keyword">if</span>(i%<span class="cpp-number">8192</span> == <span class="cpp-number">0</span>)  {  py -= <span class="cpp-number">0</span>.<span class="cpp-number">025</span>;  glBindTexture(GL_TEXTURE_2D, person);  glBegin(GL_QUADS); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py); glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f); glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);  glEnd();  }   }   down = <span class="cpp-number">0</span>;    }    glBindTexture(GL_TEXTURE_2D, person[<span class="cpp-number">0</span>]);    glBegin(GL_QUADS);   glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py);   glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f);   glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f);   glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);    glEnd();    glViewport(x, y, <span class="cpp-number">1280</span>, <span class="cpp-number">1280</span>);    glPopMatrix();}</pre></div><!–ENDSCRIPT–>Thanks alot!
  As a general note, posting entire program listings is a quick way to have your post ignored.  Nobody wants to read all that--your question doesn't have anything to do with 90% of that code (creating the window, initing openGL, etc).That said, I perused your code, and I don't see any hint of an attempt at character animation anywhere in there.  It looks like you've attempted to move the tilemap itself around, but I can't see anything to move the character.  I don't have OpenGL libraries, so I can't compile your code, but I'm just not sure what's going wrong here from your post.What, exactly, is going wrong?   ;Quote:for(int i=0; i<32768; i++)What are these giant loops for?   ;  This code is where the animation (should) take place. The loop is used for the animation. When The iterator in the loop is perfectly divided by 8192, then it draws a frame in the animation. This happens four times in 32768, so there should be four frames drawn. When it binds the texture, it takes the iterator divided by 8192 to get the frame from the texture container. It then commences drawing. Left, right, up, and down are all set by winProc. If VK_LEFT is pressed left = 1, and so on. Px and py are changed by 0.025 each frame, to go 1/4 of a tile each time it draws an animation. The up, down, left, and right variables are reset after the loop executes to keep the tile from going on and on.After a tile moves, if it does move, the sprites frame is set back to the first one. The sprite's default frame is also this.    if(left == 1)    {   for(int i=0; i<32768; i++)   {  if(i%8192 == 0)  {  px -= 0.025;  glBindTexture(GL_TEXTURE_2D, person);<br>  glBegin(GL_QUADS);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);<br>  glEnd();<br>  }<br>   }<br>   left = <span class="cpp-number">0</span>;<br>    }<br>    <span class="cpp-keyword">if</span>(right == <span class="cpp-number">1</span>)<br>    {<br>   <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i&lt;<span class="cpp-number">32768</span>; i++)<br>   {<br>  <span class="cpp-keyword">if</span>(i%<span class="cpp-number">8192</span> == <span class="cpp-number">0</span>)<br>  {<br>  px += <span class="cpp-number">0</span>.<span class="cpp-number">025</span>;<br>  glBindTexture(GL_TEXTURE_2D, person);<br>  glBegin(GL_QUADS);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);<br>  glEnd();<br>  }<br>   }<br>   right = <span class="cpp-number">0</span>;<br>    }<br>    <span class="cpp-keyword">if</span>(up == <span class="cpp-number">1</span>)<br>    {<br>   <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i&lt;<span class="cpp-number">32768</span>; i++)<br>   {<br>  <span class="cpp-keyword">if</span>(i%<span class="cpp-number">8192</span> == <span class="cpp-number">0</span>)<br>  {<br>  py += <span class="cpp-number">0</span>.<span class="cpp-number">025</span>;<br>  glBindTexture(GL_TEXTURE_2D, person);<br>  glBegin(GL_QUADS);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);<br>  glEnd();<br>  }<br>   }<br>   up = <span class="cpp-number">0</span>;<br>    }<br>    <span class="cpp-keyword">if</span>(down == <span class="cpp-number">1</span>)<br>    {<br>   <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i&lt;<span class="cpp-number">32768</span>; i++)<br>   {<br>  <span class="cpp-keyword">if</span>(i%<span class="cpp-number">8192</span> == <span class="cpp-number">0</span>)<br>  {<br>  py -= <span class="cpp-number">0</span>.<span class="cpp-number">025</span>;<br>  glBindTexture(GL_TEXTURE_2D, person);<br>  glBegin(GL_QUADS);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py);<br> glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f);<br> glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);<br>  glEnd();<br>  }<br>   }<br>   down = <span class="cpp-number">0</span>;<br>    }<br>    glBindTexture(GL_TEXTURE_2D, person[<span class="cpp-number">0</span>]);<br>    glBegin(GL_QUADS);<br>   glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px, py);<br>   glTexCoord2f(<span class="cpp-number">0</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px, py + <span class="cpp-number">0</span>.1f);<br>   glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">1</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py + <span class="cpp-number">0</span>.1f);<br>   glTexCoord2f(<span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>.0f); glVertex2f(px + <span class="cpp-number">0</span>.1f, py);<br>    glEnd();<br><br></pre></div><!–ENDSCRIPT–><br>Hope this helps a little more to find an answer.;  crazyyyy....what about trying this?? ..for(int i=0; i<4; i++)   {  px -= 0.025;  glBindTexture(GL_TEXTURE_2D, person);  glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f(px, py); glTexCoord2f(0.0f, 1.0f); glVertex2f(px, py + 0.1f); glTexCoord2f(1.0f, 1.0f); glVertex2f(px + 0.1f, py + 0.1f); glTexCoord2f(1.0f, 0.0f); glVertex2f(px + 0.1f, py);  glEnd();   };  Then you can't see the animation, it will zoom by. I had that at first, and was like.. oh I can't see that. It made me sad hehe.   ;  The issue is that you want some kind of delay in the animation, and you are doing this 32768 loop thing to try to do that.  The problem is that what you have done is incorrect.  The way you have it set up, in a single frame of display it will do the 32768 loop thing, drawing all 4 frames of the character right on top of eachother (well, marginally offset from eachother, actually).  You need to move the animation delay outside of the single-frame rendering.  You really should look into the timing facilities of whichever OS you are using.  You can track the time and use that each frame to determine which part of the animation to display.  On windows, use QueryPerformanceCounter (or timeGetTime if you hate accuracy).   ;  There I fixed it with this code:    if(left == 1)    {   px -= 0.0005;   if(counter == 200)   {  left = 0;  counter = 0;   }   counter++;    }    if(right == 1)    {   px += 0.0005;   if(counter == 200)   {  right = 0;  counter = 0;   }   counter++;    }    if(up == 1)    {   py += 0.0005;   if(counter == 200)   {  up = 0;  counter = 0;   }   counter++;    }    if(down == 1)    {   py -= 0.0005;   if(counter == 200)   {  down = 0;  counter = 0;   }   counter++;    }   if(counter <= 50 && counter >= 0)  drawPerson(0);   if(counter > 50 && counter <= 100)  drawPerson(1);   if(counter > 100 && counter <= 150)  drawPerson(2);   if(counter > 150 && counter <= 200)  drawPerson(3);;  well its a bit better, but its still not good, your counter will run at different speeds on different computers.you really really need a timer.   ;  Indeed, you should a timer based animation. It isn't that hard to create. Just determine how far your person should move (I guess your question is related to moving him?) per second and then you can write your code based on that information (thus also per 100 msecs for example).   