Jump to content

  • Log In with Google      Sign In   
  • Create Account

Simple Tilemap Scrolling

By Brandon H | Published Oct 22 2007 09:49 PM in Game Programming

offsetx offsety add float(y offsety 04f 0x80 key
If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

Hi there, and welcome to my little article on simple tile scrolling. A lot of articles on scrolling here at GDNet are somewhat difficult, but here is a more direct method. Now first off, you need a tile-rendering system. Now, declare 2 variables: offsetx and offsety:

float offsetx = 0; float offsety = 0; I am using the Tiling in OpenGL tutorial here at GDNet by Martin Estevao. Now, just add offsetx to every x of each tile's vertices, and add offsety to every y of every tile's vertices. void draw_tiles(GLvoid) { int tile; for (int y = 0; y < MAP_SIZEY; y++) { for (int x = 0; x < MAP_SIZEX; x++) { tile = map[y][x]; glBindTexture(GL_TEXTURE_2D, texture[tile]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x+offsetx), float(y + offsety), 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1 + offsetx), float(y + offsety), 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1 + offsetx), float(y + 1 + offsety), 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x + offsetx), float(y + 1 + offsety), 0.0f); glEnd(); } } } There, easier to understand in code than words. Next, go to wherever your input is and check to see if the Right key is held down. If it is, subtract 0.04 from offsetx. Check the Up key and if it is held down, subtract 0.04 from offsety. If the Down key is held down, add 0.04 to offsety. If the Left key is held down, add 0.04 to offsetx. I used 0.04 because it worked well with my computer speed and FPS I was getting. if((GetKeyState(VK_LEFT) & 0x80)) { offsetx += 0.04f; } if((GetKeyState(VK_RIGHT) & 0x80)) { offsetx -= 0.04f; } if((GetKeyState(VK_UP) & 0x80)) { offsety -= 0.04f; } if((GetKeyState(VK_DOWN) & 0x80)) { offsety += 0.04f; } And that's it! I hope you liked my super-simple scrolling, it is easily portable to directx from opengl and is easy to understand and implement. Another advantage is that now you don't need a playerx and playery variable, since the player is always at 0,0! This is where I leave you to go out and do some tile-based programming on your own. Good luck with whatever tile-based stuff you do, if you have comments or hating, send me an email at brandonmanrules@gmail.com, contact me on the forums as brandonman or post a comment in the feedback thread. Also, I'd be proud to know if this was used in any games you make, so tell me if it is! Thanks for taking the time to read this. Don't forget to download the source code as well!


Note: GameDev.net moderates article comments.