new to SDL

Started by
7 comments, last by Drew_Benton 18 years, 8 months ago
i am new to STL and have some questions about it 1. I am planning on using tga textures in my projects and would like my screen objects to be transparent in parts. how do i do that? i assume it looks something like this: SDL_SetAlpha(surf,....) SDL_BlitSurface(surf,...) So what should i put in SDL_SetAlpha just to make sure that the object i draw has transparency in the same parts where the associated targa image has it. 2. is there any way to use SDL inside another app (like a map editor). Can we just pass in something like a window handle (like with Direct3D) and draw stuff just on that particular "surface". 3. Do i need to call SDL_SetAlpha rendering every sprite? Is there a way of just enabling alpha blending for the whole rendering routine? thanx
Advertisement
1. If you use targa images, the image loader should automagically load the surface with the proper RGBA formats and alpha channel. (Assuming you use SDL_Image.)

2. I don't believe so, but I could be wrong.

3. No, you should just have to call it once per surface, but see above note about image loading.

Thanx again SiCrane! Should i just address all of my questions to you? ;)

I have another one. Is this correct as for the rendering routine ?

void Render()
{
if( SDL_MUSTLOCK(screen) )
if( SDL_LockSurface(screen) < 0 )
return;

//draw everything here

if( SDL_MUSTLOCK(screen) )
SDL_UnlockSurface(screen);

SDL_Flip(screen);
}
I am currently working on some SDL tutorials:
Lazy Foo's SDL tutorials

They're not finished yet, but I'm hoping to have all the non-OS/compiler specific stuff finished by the end of next week.

Most of the important tutorials are already up though.
</spam>

[Edited by - Lazy Foo on August 9, 2007 7:48:50 PM]

Learn to make games with my SDL 2 Tutorials

You only need to lock the screen surface for direct pixel manipulation, if all you are doing is blitting images then you can do away with all the locking of the surface.
void Render(){if( SDL_MUSTLOCK(screen) )if( SDL_LockSurface(screen) < 0 )return;//draw everything hereif( SDL_MUSTLOCK(screen) )SDL_UnlockSurface(screen);SDL_Flip(screen);}


What do you mean by "render". The thing you are doing here sets the surface up for individual pixel manipulation. This is only needed if you are working on each pixel.

If you use this when blitting images with SDL's various blit functions, you will get an error, however.

If you have any specific example, it will get you a better answer.
Quote:
1. I am planning on using tga textures in my projects and would like my screen objects to be transparent in parts. how do i do that? i assume it looks something like this:


Unless you need partial transparency then alpha blending isn't what you want to use. In most cases you can specify a single color in an image to be transparent using a colorkey (SDL_SetColorKey). This gets done once when you load the image and will be the equivilent of specifying an alpha value of 0 to all pixels of that color.

Colorkeys will always be much faster in SDL than using alpha blending because the pixels relating to the colorkey are simply ignored and in some cases can be hardware accelerated whereas alpha blending in SDL will always be done via software rendering.

This is not to say alpha blending is impossibly slow via SDL, it depends on how often you use it and a few other things as well but it is by no means not an option for effects.

If on the other hand alpha blending is used heavily, most people here will recommend you use OpenGL or Direct3D for rendering 2D. On modern graphics cards you will see a noticeable speed increase with the down side of having to learn a 3d API or learn to use another 3rd party library.

Hope it helps
Evillive2

hello there

thanx everyone who have posted replies here. your comments were very useful. i got things going. got a bit confused when i tried
using animation. it doesn't look like there's any "ClearDevice" type of routine in SDL so i couldn't get my app to clear screen
before each rendering loop and things looked messy. then i used the following :


SDL_FillRect(m_screen,NULL,0);

//draw all the pics using SDL_BlitSurface

SDL_Flip(m_screen);

it works but i have strong feeling that something is wrong. does this make any sense? if not, then how should it look?

and one more thing... how do you guys scale textures in SDL? in other words, how can i change the size of the surface that
holds loaded image?

Quote:Original post by pnroach
SDL_FillRect(m_screen,NULL,0);
//draw all the pics using SDL_BlitSurface
SDL_Flip(m_screen);

it works but i have strong feeling that something is wrong. does this make any sense? if not, then how should it look?

Looks good to me, but one thing though. If you draw one background image that covers the entire screen, then you do not even need to bother with the FillRect. Just think of it like by drawing the background (which is drawn 1st) it clears out everything already there, so you can save a lot of time by not calling clear.

Quote:
and one more thing... how do you guys scale textures in SDL?


SDL_gfx.

This topic is closed to new replies.

Advertisement