Loading tiles from one large image

Started by
4 comments, last by Raisor 19 years, 5 months ago
I am currently in the process of writing a tile-based RPG game and have a pretty good start but I have one question. I know how to load images from individual files for each tile I am using but how would I go about loading tiles from a larger image? For example, I currently use a bunch of 64x64 images in seperate files. I want to combine them in to one image file and load each tile from that file. How would this be accomplished? I am using Dev-C++ and SDL currently and am still learning but the best way for me to learn is hands-on trial and error. Any help is appreciated!
Thanks,Tom Sapphttp://www.sappsworld.com
Advertisement
Well I am not familiar with SDL, but, I may have something for you. When painting and image to the screen from memory, is there anyway to specify which part of the image you want to display? I use OpenGL for similar things, and (I know, it is a 3d api, but this may still apply) I can tell it to texture with only part of an image.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
I am not used to windows programmer or familiar with the SDL API but I can say that the easiest way would be to clip the rectangle of your tile. I dont know how this is done but its usually done along the lines of:

1.) Specifying the top-corner x and y values of the tile. Also specifying the width and height, in this case being 64 and 64.

2.) Running tha tthrough a function to copy it to another surface

3.) Doing whatever with that

EDIT: thisisbob posted same time as me
SDL_Surface *screen; // You should already have this one
SDL_Sufrace *tile;

tile = SDl_LoadBMP("images/tiles.bmp"); // or in what directory you have your tiles.

int index1, index2; // row and column of the tile you want.

SDL_Rect rect1 = { 64*index1, 64*index2, 64, 64} // if you have 64x64 pxl's pictures
SDL_Rect rect2 = { 0, 0, 64, 64}; // Or wherever you want your tile in the screen.

SDL_BlitSurface(tile, $rect1, screen, $rect2);
SDl_Flip(screen)
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
Quote:Original post by Spippo
SDL_Surface *screen; // You should already have this one
SDL_Sufrace *tile;

tile = SDl_LoadBMP("images/tiles.bmp"); // or in what directory you have your tiles.

int index1, index2; // row and column of the tile you want.

SDL_Rect rect1 = { 64*index1, 64*index2, 64, 64} // if you have 64x64 pxl's pictures
SDL_Rect rect2 = { 0, 0, 64, 64}; // Or wherever you want your tile in the screen.

SDL_BlitSurface(tile, $rect1, screen, $rect2);
SDl_Flip(screen)


What he said, and here's why...

Quote:http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fBlitSurface
#include "SDL.h"int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);


Description
This performs a fast blit from the source surface to the destination surface.

The width and height in srcrect determine the size of the copied rectangle. Only the position is used in the dstrect (the width and height are ignored).

If srcrect is NULL, the entire surface is copied. If dstrect is NULL, then the destination position (upper left corner) is (0, 0).

The final blit rectangle is saved in dstrect after all clipping is performed (srcrect is not modified).

The blit function should not be called on a locked surface.


Basically you have two rectangles, destination, and source. The source is the surface containing your bitmap file. If you call SDL_BlitSurface with the *src paramter NULL, the entire surface is copied. If you create an SDL_Rect and pass it's address into the *src parameter, it'll copy the content in your source surface within that rectangle.
- A momentary maniac with casual delusions.
Thank you all for your responses. I guess I need to do a little more reading on the SDL stuff! :D
Thanks,Tom Sapphttp://www.sappsworld.com

This topic is closed to new replies.

Advertisement