This will become a problem when we start doing animation in a later HOWTO so we might as well start making life easier for ourselves now. I could have used only one but that would mean I would need to change the x,y every time we redraw the scene. You will notice at this point that I've added a separate SDL_Rect for each surface as the places they will be drawn. So now we've told SDL our special color, we can now begin layering/rendering our scene. Its basically that the image surface gets optimized so that the whole color keying process gets done faster. Its a bit outside the scope of this HOWTO to go into details of RLE but a good five minutes on Google typing in run length encoding will get you a good explanation if you really want one. ![]() The other two SDL_RLEACCEL and SDL_SRCCOLORKEY specify how the images will be drawn. Hence black, which is no color at all really, is red=0,green=0 and blue=0. ![]() In the SDL_MapRGB function we can tell SDL three numbers that form a color with Red, Green and Blue parts. SDL_SRCCOLORKEY tells SDL that this color key applies to the source image SDL_RLEACCEL is run lenght encoded acceleration to speed up the colorkeying SDL_SetColorKey( compatible_image, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey) Uint32 colorkey = SDL_MapRGB( compatible_image->format, 0, 0, 0) // choose black specify a colour that will be used to signify 'transparent' pixels So how can we specify what color SDL can treat as transparent? Well, when we load an image we can add a little code to take care of this: Granted, you would still see the little guy but it takes quite a bit of convincing that this is a rendered scene and not something you knocked up in windows paint! If we draw this as is, you will get a nasty black rectangle over the top of our lovely hills and sky!. If you download our example code and take a look at the image file 'sprite.bmp' you can see that our sprite is draw on a black background. By that we mean that a specially selected color will be deliberately NOT drawn when we apply the different layers to our scene. That means that all other layers infront have to be drawn as if they were a transparent film over the top. If you look closely at any 2d game you will find that most backgrounds are not ever in front of anything. Draw any scenery that goes behind your sprites So whats the standard procedure for this I hear you cry! Thats exactly how 2d games generate their scenes! er. Then if you want to change or add anything, you add an new layer on top and place new image data on it. Usually in GIMP or Photoshop you start by creating a background layer which is usually white. The reason this is true is that they both use a system of layers to create a composite image that is then displayed at the end of the process. Rendering or 'Drawing' a scene in a game believe it or not works a lot like most image manipulation packages like GIMP or Photoshop. This HOWTO covers the following subjects:Ģ) Seeing in the dark or how color keying worksģ) How to tell SDL which color to make transparent This function is available since SDL_image 2.0.0.This is the fourth HOWTO in a series of HOWTOs about game development using Simple DirectMedia Layer. When done with the returned texture, the app should dispose of it with a call to SDL_DestroyTexture(). If you would rather decode an image to an SDL_Surface (a buffer of pixels in CPU memory), call IMG_Load() instead. There is also IMG_LoadTextureTyped_RW(), which is equivalent to this function except a file extension (like "BMP", "JPG", etc) can be specified, in case SDL_image cannot autodetect the file format. There is a separate function to read files from disk without having to deal with SDL_RWops: IMG_LoadTexture(renderer, "filename.jpg") will call this function and manage those details for you, determining the file type from the filename's extension. SDL_image reads everything it needs from the RWops during this call in any case. If freesrc is non-zero, the RWops will be closed before returning, whether this function succeeds or not. Otherwise, SDL_image will attempt to create an SDL_Texture in the most format that most reasonably represents the image data (but in many cases, this will just end up being 32-bit RGB or 32-bit RGBA). If the loaded image has transparency or a colorkey, a texture with an alpha channel will be created. This can be significantly more efficient than using a CPU-bound SDL_Surface if you don't need to manipulate the image directly after loading it. RemarksĪn SDL_Texture represents an image in GPU memory, usable by SDL's 2D Render API. Non-zero to close/free the SDL_RWops before returning, zero to leave it open. The SDL_Renderer to use to create the GPU texture.Īn SDL_RWops that data will be read from. SDL_Texture * IMG_LoadTexture_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc) Function Parameters renderer
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |