Skip to main content
gamedev.net
🔒 Locked

Making a tile-based map

Started by RuneLancer 6 replies 550+ views
Original Post
RuneLancer
RuneLancer
Hello! I've pretty much worked this out and everything renders as I want it to. I've written a quick and dirty little test app (with a bunch of hardcoded stuff; I just want to test the end result before I go through with this for my game) to render a tile-based map in OpenGL. Basically I have a texture containing a series of 16x16 tiles (oldschool SNES style :) ) and I map them to a grid of quads. Simple enough, works like a charm, and gives me the results I want. The problem, sloppy just-work-and-we'll-worry-about-writing-good-code-later listing aside, lies in the tediousness associated with mapping the right tile onto my quads. Here's an example (again, everything has been hard-coded in a "just work" way; I don't have my map format up yet).
	glBindTexture(GL_TEXTURE_2D, Texture[0]);
	glBegin(GL_QUADS);
		glTexCoord2f(0.533f, 0.733f); glVertex3f(-10.0f, -2.0f, -10.0f);
		glTexCoord2f(0.466f, 0.733f); glVertex3f( -8.0f, -1.0f, -10.0f);
		glTexCoord2f(0.466f, 0.666f); glVertex3f( -8.0f, -1.0f,  -8.0f);
		glTexCoord2f(0.533f, 0.666f); glVertex3f(-10.0f, -1.0f,  -8.0f);

		glTexCoord2f(0.533f, 0.733f); glVertex3f(-10.0f, -1.0f, -8.0f);
		glTexCoord2f(0.466f, 0.733f); glVertex3f( -8.0f, -1.0f, -8.0f);
		glTexCoord2f(0.466f, 0.666f); glVertex3f( -8.0f,  0.0f, -6.0f);
		glTexCoord2f(0.533f, 0.666f); glVertex3f(-10.0f,  0.0f, -6.0f);

		glTexCoord2f(0.533f, 0.733f); glVertex3f(-8.0f, -1.0f, -10.0f);
		glTexCoord2f(0.466f, 0.733f); glVertex3f(-6.0f,  0.0f, -10.0f);
		glTexCoord2f(0.466f, 0.666f); glVertex3f(-6.0f,  0.0f,  -8.0f);
		glTexCoord2f(0.533f, 0.666f); glVertex3f(-8.0f, -1.0f,  -8.0f);
...
Aside from the fact this is sloppy (and not in a for.. loop, but hey, it's a bloody test ;) ) it feels like I'm wasting a lot of time reassigning texture coordinates all the time like that. The end result will read stuff from a map file. However, am I going to have to reassign the coordinates every tile? Is there a way to just do it once (and if so, should I order tiles by texture? Remember they all use the same texture, just with different coordinates to select the right tile to display, and there's no way I'm gonna drag around a few thousand 16x16 bitmaps ;) ) Any pointers would be quite appreciated. :) Thanks!
RuneLancer
RuneLancer
Huh. Well, that sucks. :/ I was hoping there'd be a way to specify a set of texture coordinates and just render all the tiles I wanted, then move on to the next set. Ah well, can't have everything I guess...

Thanks. :)
Darobat
Darobat
Quote:
Original post by zedzeek
>>However, am I going to have to reassign the coordinates every tile?<<

yes

Um... No you don't. It's called a "Function"

void DrawGrassTile(float x, float y, float z){		glTexCoord2f(0.533f, 0.733f); glVertex3f(x, y, z);		glTexCoord2f(0.466f, 0.733f); glVertex3f(x+2,  y+1, z);		glTexCoord2f(0.466f, 0.666f); glVertex3f(x+2,  y+1,  z+2);		glTexCoord2f(0.533f, 0.666f); glVertex3f(x, y,  z+2);}


You might have to screw with the formula's I used. I haven't tested them.
--------------------C++ Home - Check it out!Lol... - Amazing video
RuneLancer
RuneLancer
Yeah, I'm aware that I can put this in a function. What I meant was wether it had to be assigned everytime (and thus excuted as an instruction) I want to draw a tile or not; any idiot can stick something repetitive in a function. ;)

Thanks regardless, though. :)
Pipo DeClown
Pipo DeClown
What you need is glVertexPointer and glTexCoordPointer, then draw with glDrawElements or glDrawArrays.
Darobat
Darobat
Quote:
Original post by RuneLancer
Yeah, I'm aware that I can put this in a function. What I meant was wether it had to be assigned everytime (and thus excuted as an instruction) I want to draw a tile or not; any idiot can stick something repetitive in a function. ;)

Thanks regardless, though. :)

OOooo... I thought you just didn't like having to type everything each time. OK then./
--------------------C++ Home - Check it out!Lol... - Amazing video
RuneLancer
RuneLancer
Quote:
Original post by Pipo DeClown
What you need is glVertexPointer and glTexCoordPointer, then draw with glDrawElements or glDrawArrays.

Oooh, sounds sweet; I'll look into that. Thanks! :)

I've limited myself to 2D games (never really liked 3D) so far but started working with OpenGL a few months ago and figured I'd use it to make a 3D yet sprite-and-tile-based RPG. So I'm still a little sketchy about certain details. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.