I'm using SDL & openGL to render a tile-map. The issue is that the tile-map rendering is extremely messed up, and i'm just a bit unsure what i'm doing wrong exactly. Here's an image of the rendering: http://imageshack.us/photo/my-images/853/rendering.png/
& my tilesheet that i'm using: http://imageshack.us/photo/my-images/197/tilesheet2.png/
It should just be the first tile being rendered, but i'm getting this blurred mess :S. My rendering code:
glBindTexture(GL_TEXTURE_2D, texture);
float texscale = 1.0f / (float)tileWidth;
sourceX = sourceX / (float)tileSheetWidth;
glBegin(GL_QUADS);
// Top-left vertex (corner)
glTexCoord2f( sourceX, sourceY);
glVertex2i(x, y);
// Bottom-left vertex (corner)
glTexCoord2f( sourceX + texscale, sourceY);
glVertex2i( x + tileWidth, y);
// Bottom-right vertex (corner)
glTexCoord2f( sourceX + texscale, texscale + sourceY);
glVertex2i( x + tileWidth, y + tileHeight);
// Top-right vertex (corner)
glTexCoord2f( sourceX, texscale + sourceY);
glVertex2i(x, y + tileHeight);
glEnd();
glLoadIdentity();
& my initialization code for openGL
// Set the OpenGL state after creating the context with SDL_SetVideoMode
glClearColor(0, 0, 0, 0);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D); //Enable 2D rendering
glViewport(0, 0, Width, Height); //Set Up openGL viewport (screen)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Width, Height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
& just in-case, here's my image loading code, i think that perhaps this may be were the problem lies somehow...
LoadImage(string filename, bool loadingTileSheet)
{
SDL_Surface *LoadedImage = NULL;
GLuint texture;
Uint32 rmask, gmask, bmask, amask;
LoadedImage = IMG_Load(filename.c_str());
if (loadingTileSheet)
{
tileSheetWidth = LoadedImage->w;
numberOfTiles = (LoadedImage->w / tileWidth) + 1;
}
SDL_PixelFormat *pixf = SDL_GetVideoSurface()->format;
SDL_SetAlpha(LoadedImage, 0, 0);
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
}
else
{
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
}
SDL_Surface *image = SDL_CreateRGBSurface(SDL_SWSURFACE, LoadedImage->w, LoadedImage->h, 32, rmask, gmask, bmask, amask);
SDL_BlitSurface(LoadedImage, NULL, image, NULL);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
return texture;
}