Archived

This topic is now archived and is closed to further replies.

Using SDL to "tileize" a .BMP

This topic is 5004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone... Say I want to make a rpg... I''m using SDL and have my .BMP loaded into a surface...now, do I make some sort of handy-dandy function to make a 2-d array of SDL_Rect''s? Or am I still too inexperienced in programming to be doing a game, and should go read more on C++...*cough*please don''t sentence me to that one*wink*

Share this post


Link to post
Share on other sites
You don''t give out much information. We can''t help if you don''t
tell us what exactly it is you want to accomplish. Just please
don’t say “I want to create the next big mmorpg!”

Also, we can’t tell you if you know how to make a role playing
game. I’d guess that some more programming knowledge wouldn’t
hurt though. You wouldn’t be asking other ways…

-----------------------------


Share this post


Link to post
Share on other sites
Oops, sorry, I was thinking, but not typing everything I was thinking... sometimes my elevator doesn''t go all the way to the top... anyways, I''m wanting to figure out how I take my pic with all the tiles on it, once it''s loaded into a surface, and the computer would know "the square from (1,1) to (32,32)is tile 0. wherever there''s a zero in the map file,(which I suppose I''ll have to work out next)this tile should be there. Is this accomplished by taking every 32x32 square and making a SDL_Rect with each? I hope I''m not forgetting something this time, sorry for not being very exact... Oh yeah, and I don''t really want to make the "Next Big MMORPG" at this moment, although I do plan on eventually making A mmorpg...once I have more programming knowledge...

Share this post


Link to post
Share on other sites
Heres some code i wrote for a tile based game - it lets you just call 'draw' on the class saying what tile and it does the maths for you. it keeps the image all in one bitmap. It assumes that all the tiles have the same width and height (although the widths dont have to be the same as the heights)

its a C++ class - it shouldn't be too hard to convert to whatever you use

include:

// ImageList.h: interface for the ImageList class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(IMAGELIST_H_INCLUDED_)
#define IMAGELIST_H_INCLUDED_

#include "sdl.h"
#include "enums.h" // Added by ClassView

class ImageList
{
SDL_Surface * m_image;
int m_x;
int m_y;
public:
void SetTransparency(Uint8 r, Uint8 g, Uint8 b);
ImageList(char * file, Uint8 x, Uint8 y);
ImageList(SDL_Surface * image, Uint8 x, Uint8 y);
virtual ~ImageList();

void Draw(int index, SDL_Surface * dest, SDL_Rect * offset);
};

#endif // !defined(IMAGELIST_H_INCLUDED_)



Main part:


// ImageList.cpp: implementation of the ImageList class.

//

//////////////////////////////////////////////////////////////////////


#include <sdl.h>
#include "ImageList.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


ImageList::ImageList(char * file, Uint8 x, Uint8 y)
{
SDL_Surface *image = SDL_LoadBMP(file);
m_image = NULL;
if(image != NULL)
{
m_x = x;
m_y = y;
m_image = image;
}
SDL_SetColorKey(m_image,SDL_SRCCOLORKEY ,0);
}

ImageList::ImageList(SDL_Surface * image, Uint8 x, Uint8 y)
{
m_image = NULL;
if(image != NULL)
{
m_x = x;
m_y = y;
m_image = image;
}
}

ImageList::~ImageList()
{
}


void ImageList::Draw(int index, SDL_Surface * dest, SDL_Rect * offset)
{
if(index >= m_x * m_y) return;
if(m_image!=NULL)
{
SDL_Rect rect;
rect.w = m_image->w / m_x;
rect.h = m_image->h / m_y;
rect.x = (index % m_x) * rect.w;
rect.y = (index / m_x) * rect.h;

SDL_BlitSurface(m_image, &rect, dest, offset);
}
}


void ImageList::SetTransparency(Uint8 r, Uint8 g, Uint8 b)
{
if(m_image != NULL)
{
SDL_SetColorKey(m_image, SDL_SRCCOLORKEY, SDL_MapRGB(m_image->format, r,g,b));
}
}


There are two ways to initialise the class : either pass it the filename to load (a BMP), or pass it the SDL surface.
The other parameters at load say how many tiles wide and high the image is.

Once you've got it in memory, the draw takes an index, the surface to draw to (ie, your screen) and the offset into that surface - its passed as a rect, but it only uses the x and y coordinates to set the top left corner of your tile.

let me know if that helps

- Wyzfen

ps: this code can be found in my entry to the LudumDare 48-hour comp #3 ( www.ludumdare.com ) - called RoboMaze.
It is a very simple tile based game written in SDL.
direct link: http://ludumdare.com/user/98/ROBOMAZE.zip

[edited by - wyzfen on March 26, 2004 10:34:42 AM]

[edited by - wyzfen on March 26, 2004 10:35:40 AM]

[edited by - wyzfen on March 26, 2004 10:37:46 AM]

Share this post


Link to post
Share on other sites
Hmm... It seems I tried to get the horse before the chariot(or the other way around, whatever)...Perhaps I need to program a map saving/loading ...for lack of a better word in my brain... thing? What all should I be looking in for programming that?

Share this post


Link to post
Share on other sites
I must still be too inexperienced...either that, or Dev-C++ doesn't like me(or my code!)...I can't really understand it all, but thanks for the resources, maybe when I understand it all better I can use them.Oh yeah, When I try to compile the function i made (which takes the surface the BMP is on, the name to call the array of SDL_Rects made, and the width and height of each tile)it says "implicit declaration of int Tilize()" which more than likely means I need to learn to use functions better. Thanks again.

[EDIT]Starnge, isn't it, but two minutes later, I decide to #include my file with Tilize() in it, and remove it from the actual project, then got rid of the char value,(don't think it would have worked anyways) and what do ya know... it compiled! I had spent all weekend trying to figure it out... and 3 seconds of typing fixed it!However, working and compiling when I'm within the area of 50 yards or so are two completely different things... Maybe now I can try to figure out making/editing/using map files?Some help on that would be nice too...

[edited by - Klarh on March 29, 2004 6:37:14 PM]

Share this post


Link to post
Share on other sites
Wyzfen, where did you use the Draw function in robomaze's source?I can't find it...[EDIT] Dunno why I even post, just figured it out...

[edited by - Klarh on March 30, 2004 10:14:59 PM]

Share this post


Link to post
Share on other sites