Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

xg0blin

display list/glTranslate trouble with glut KeyboardFunc

This topic is 5865 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

I''m having a little piece of trouble with my code, I have a tile map (loaded from a text file into a 2d vector), then outputting it into a display list with a couple of for loops, then calling it in my display function. The problem is, I try to scroll it with glTranslate in my glutKeyboardFunc(Keyboard) and it won''t move. I also tried it without the display lists as just a function to load tiles, and still won''t translate. If I put glTranslatef(-100.0, 0.0, 0.0) right above my glCallList(DLid) it translates, but I can''t use the keyboard func to move it......I''ve been messing with it for two days and can''t seem to figure out what I''m doing wrong. Any help would be appreciated

Share this post


Link to post
Share on other sites
Advertisement
#include "GlutInit.h"

vector > GameBoard;
int width, height;
static int map_start_x = 0, map_start_y = 0;
unsigned int g_Textures[];
GLuint DLid;
static float tran_x = 0.0f, tran_y = 0.0f, tran_z = 0.0f;

void LoadTextures(void)
{
CreateTexture(g_Textures, "grass.bmp", 0);
CreateTexture(g_Textures, "grass0001.bmp", 1);
CreateTexture(g_Textures, "grass0010.bmp", 2);
CreateTexture(g_Textures, "grass0011.bmp", 3);
CreateTexture(g_Textures, "grass0100.bmp", 4);
CreateTexture(g_Textures, "water.bmp", 31);
glEnable(GL_TEXTURE_2D);
}


void FillVector(void)
{
ReadMap(&GameBoard, "level2.txt", width, height);
DLid = MakeMap();
}


void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(tran_x, tran_y, tran_z);
glCallList(DLid);
glutSwapBuffers();
}



void Reshape(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glOrtho(0, 1024, 768, 0, -10, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


void CreateTexture(unsigned int textureArray[], char *strFileName, int textureID)
{
AUX_RGBImageRec *pBitmap = NULL;

if(!strFileName)
return;

pBitmap = auxDIBImageLoad(strFileName);

if(pBitmap == NULL)
exit(0);

glGenTextures(1, &textureArray[textureID]);
glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB,
GL_UNSIGNED_BYTE, pBitmap->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

if(pBitmap)
{
if(pBitmap->data)
{
free(pBitmap->data);
}
free(pBitmap);
}
}



GLuint MakeMap(void)
{
GLuint MapDL;
MapDL = glGenLists(1);
glTranslatef(tran_x, tran_y, tran_z);
glNewList(MapDL, GL_COMPILE);
for(int i = map_start_x; i < width; i++)
{
for(int j = map_start_y; j < height; j++)
{
if( GameBoard[j] == ''1'' ||
GameBoard[j][i] == ''2'' ||
GameBoard[j][i] == ''3'' ||
GameBoard[j][i] == ''4'')
{
if(GameBoard[j][i] == ''1'')
glBindTexture(GL_TEXTURE_2D, g_Textures[0]);
else if(GameBoard[j][i] == ''2'')
glBindTexture(GL_TEXTURE_2D, g_Textures[1]);
else if(GameBoard[j][i] == ''3'')
glBindTexture(GL_TEXTURE_2D, g_Textures[2]);
else if(GameBoard[j][i] == ''4'')
glBindTexture(GL_TEXTURE_2D, g_Textures[3]);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0, 0);
glVertex3f( (float) i * 32, (float) j * 32, 0);
glTexCoord2f(1, 0);
glVertex3f( (float) i * 32 + 32, (float) j * 32, 0);
glTexCoord2f(1, 1);
glVertex3f( (float) i * 32 + 32, (float) j * 32 + 32, 0);
glTexCoord2f(0, 1);
glVertex3f( (float) i * 32, (float) j * 32 + 32, 0);
glEnd();
}
else if(GameBoard[j][i] == ''v'')
{
glBindTexture(GL_TEXTURE_2D, g_Textures[31]);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0, 0);
glVertex3f( (float) i * 32, (float) j * 32, 0);
glTexCoord2f(1, 0);
glVertex3f( (float) i * 32 + 32, (float) j * 32, 0);
glTexCoord2f(1, 1);
glVertex3f( (float) i * 32 + 32, (float) j * 32 + 32, 0);
glTexCoord2f(0, 1);
glVertex3f( (float) i * 32, (float) j * 32 + 32, 0);
glEnd();
}
}

}
glEndList();
return MapDL;
}


void KeyPressed(unsigned char key, int x, int y)
{
switch(key)
{
case VK_RIGHT:
tran_x -= 100;
Display();
break;
}
return;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!