Jump to content
  • Advertisement


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

SDL tile based background aproach

This topic is 5830 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

I've started working on a background engine for tile based games in SDL. I'm wondering what you guys think would be the best way to manage all your tile surfaces. For example, I'm trying to create an array of SDL_Surfaces but this seems to not be working because of the pointer that's involved, eg. SDL_Surface *backtile[100]; dosent seem to be working for some reason, when I loop through all 100 elements of the array I just get black... so, how would YOU do it? Am I sort of on the right track here? I posted a note about my pointer/sdl surface/array problem, but I can't seem to find it again...man do I look stupid or what? if anyone sees it, lemme know [edited by - zyre on May 31, 2002 11:36:50 PM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Are you certain that each of those pointers are actually pointing to a SDL surface?

Are you updating the screen? I use
"SDL_UpdateRect(the_entire_screen, 0,0,0,0);"

Are you sure that you''re actually blitting to the main surface?

Share this post

Link to post
Share on other sites
Don''t know, yes, and yes. starting with the first one, I pulled the SDL_Surface *backtile; from a tutorial, so I assumed you could use SDL_Surface *backtile[100];

second point, I am updating the screen, because I can draw stuff using the non-array surfaces just fine, and it shows up on the screen ok.

anhd yes, I''m for sure blitting to the main surface, because I''m using the exact same blit function as I did with the non-array surfaces as this one.

I was using:

and to change it to the array I started trying:

this just gives me a compiler error like so:
sdltry1.c:34: cannot convert `*(backtile + +(conf.CONFIG::levelmap[itx][ity] * 60))'' from type `SDL_Surface'' to type `SDL_Surface *''
make: *** [sdltry1.o] Error 1

so I tried:
and this compiles ok, but leaves me with a black screen. I think part of the problem is I can''t rmember what the & did...is it the address that the pointer is pointing to?

also, I think I''m loading up the array ok, I''m using:
wait a sec...I think I found the problem


I thought I loaded up the array...I wrote the code in my head, so I thought I had written it in my proggie, but now that I''m scrolling through my messy code, I can''t find the loop that I made up to load up the array with bitmaps...

I guess that''s it.

now I feel stupid

Share this post

Link to post
Share on other sites
This is not an SDL problem, it''s just that you don''t quite understand the difference between pointers and normal objects. If you have an array of pointers, you still have to create the objects that those pointers will point to, and you need to dereference the pointers with either the -> or * operators to use them. On the other hand, the & operator gives you a pointer from the object you use it on.

The "cannot convert" message tells you that you''re giving a function one type of object when it expects a different type. This is usually because you didn''t dereference it properly, or just used the wrong object entirely.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post

Link to post
Share on other sites
That's what I thought, it was my lack of understanding of pointers.

Dereferencing eh? Maybe I'll go look that up


one more thing...how does SDL_Surface *surface; work as opposed to SDL_Surface *surface[50]; then? it's not pointing to anything in any of the code that I saw in the tutorial, and to blit it it dosent do anything special, it seems to treat it as a normal variable...

I know, I'll go dig up my basic C++ manual again and re-read the pointers section

[edited by - Zyre on June 2, 2002 12:15:19 AM]

Share this post

Link to post
Share on other sites
Well now, this is intresting.

My SDL video wrapper class uses an SDL_Surface array to manage images loaded into the game (I want to make that into a linked list sometime) as does this little transparency test app I made a while ago:



define ERR_CHECK 1 // enable error checking

include "error.h"
include "sdl.h"


define SCREEN_WIDTH 800
define SCREEN_HEIGHT 600
define SCREEN_BPP 0

define BALL 0
define HUD 1


SDL_Surface* psSurface;
SDL_Surface* psBitmap[2];

SDL_Rect rcDst, rcDst2;
SDL_Event event;




int end_app = 0;




// //

int main(int argv, char* argc[])//////////////////////////////////////////////////////////////



// initialize SDL

if (SDL_Failed(SDL_Init(SDL_INIT_VIDEO)))
throw Error("could not initialize SDL", "transparency.cpp", __LINE__);

// set exit function


// create window

throw Error("could not create window", "transparency.cpp", __LINE__);

// load in radar

if (!(psBitmap[HUD] = SDL_LoadBMP("../sdl/radar.bmp")))
throw Error("could not load in radar screen", "transparency.cpp", __LINE__);

// load in ball

if (!(psBitmap[BALL] = SDL_LoadBMP("../sdl/ball.bmp")))
throw Error("could not load in ball", "transparency.cpp", __LINE__);

// set blit dest

rcDst.w = psBitmap[HUD]->w;
rcDst.h = psBitmap[HUD]->h;
rcDst.x = (SCREEN_WIDTH/2) - (rcDst.w/2);
rcDst.y = (SCREEN_HEIGHT/2) - (psBitmap[HUD]->h/2);

// set blit dest

rcDst2.w = psBitmap[BALL]->w;
rcDst2.h = psBitmap[BALL]->h;
rcDst2.x = (SCREEN_WIDTH/2) - (psBitmap[BALL]->w/2);
rcDst2.y = (SCREEN_HEIGHT/2) - (psBitmap[BALL]->h/2);

// set transparent color to black

if (SDL_Failed(SDL_SetColorKey(psSurface, TRANSPARENT_FLAGS, SDL_MapRGB(psSurface->format, 0, 0, 0))))
throw Error("could not set transparency key", "transparency.cpp", __LINE__);




while (true)
while (SDL_PollEvent(&event))
switch (event.type)
case SDL_QUIT:
// exit

end_app = 1;
} break;

// exit?

if (end_app)

// clear screen

if (SDL_Failed(SDL_FillRect(psSurface, NULL, 0)))
throw Error("could not clear screen", "transparency.cpp", __LINE__);

// blit image

if (SDL_Failed(SDL_BlitSurface(psBitmap[HUD], NULL, psSurface, &rcDst)))
throw Error("could not blit radar screen", "transparency.cpp", __LINE__);

if (SDL_Failed(SDL_BlitSurface(psBitmap[BALL], NULL, psSurface, &rcDst2)))
throw Error("could not blit radar screen", "transparency.cpp", __LINE__);

// update display

SDL_UpdateRect(psSurface, 0, 0, 0, 0);





} // end try

// handle exceptions

catch (Error err) { err.Display();}

// end


} // end main

Two suggestions from me:

1) Make sure the array is properly sized. For instance, if you were to change the size of psBitmap from 2 to 1, it would compile and run fine but display squat.

2) Post the code you are using to assign images to the surfaces. That may be your problem

Hope this helps.


Drew Sikora
A.K.A. Gaiiden

ICQ #: 70449988
AOLIM: DarkPylat

Blade Edge Software
Staff Member, GDNet
Public Relations, Game Institute

3-time Contributing author, Game Design Methods , Charles River Media (coming April/May 2002)
Online column - Design Corner at Pixelate

Unnoficial IGDA chat! [polaris.starchat.net -> #igda]
NJ IGDA Chapter - NJ developers unite!! [Chapter Home | Chapter Forum]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!