Sign in to follow this  
Raisor

Loading tiles from one large image

Recommended Posts

Raisor    125
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!

Share this post


Link to post
Share on other sites
tHiSiSbOb    463
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.

Share this post


Link to post
Share on other sites
MitchellH    122
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

Share this post


Link to post
Share on other sites
Spippo    158
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)

Share this post


Link to post
Share on other sites
Rhaal    754
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this