![]() If we now try to draw the above on the default black background, we’re not going to see anything, are we? Fortunately, it’s easy to change the background colour, and we’ve done it before in “ Handling Keyboard and Mouse Events in SDL2“. Here’s the same image, without borders and with transparency: Now, those ugly borders in the image above are just for demonstration purposes. If we can rapidly render each image in quick succession, just like a cartoon, then we have an animation! □ It consists of 4 sub-images (called sprites or frames), each 32 pixels wide. This image is 128 pixels wide and 64 pixels high. SDL_RenderCopy(renderer, texture, NULL, NULL) SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image) SDL_Surface * image = IMG_Load("spritesheet.png") SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0) SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0) SDL_Window * window = SDL_CreateWindow("SDL2 Sprite Sheets", Our project setup for this article is just the same as in “ Loading Images in SDL2 with SDL_image“, and in fact our starting code is adapted from that article: In this article, we’re going to take this to the next level, using a very simple technique to animate our images and make them feel more alive. Many of the previous SDL2 tutorials have involved working with images. The source code is available at the Gigi Labs BitBucket repository. If ( event->active.state & SDL_APPACTIVE ) else if ( event->active.This is an updated version of “ SDL2: Animations with Sprite Sheets“, originally posted on 30th March 2014 at Programmer’s Ranch. Printf( "app %s ", event->active.gain ? "gained" : "lost" ) Printf("Ctrl-G: toggling input grab!\n") Printf("Unable to toggle fullscreen mode\n") ![]() (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed") Printf("Toggled fullscreen mode - now %s\n", Static SDL_bool USE_DEPRECATED_OPENGLBLIT = SDL_FALSE The code is still available for benchmark purposes though. * The SDL_OPENGLBLIT interface is deprecated. * Define this to be the name of the logo image to use with -logo */ * Undefine this if you want a flat cube instead of a rainbow cube */ The source is in the sdl folder under test/testgl.c, but i'll post it here anyway. Games, examples, libraries, scripting, sound, music, graphics. David Olofson - Consultant, Developer, Artist, Open Source Advocate If you still can't make it work, I'm quite sure someone on this list will spot the problem quickly with some code to look at. You may want to add RGB support if it matters, though it only affects the intermediate surfaces - not the OpenGL textures. Also note that this will only ever return RGBA surfaces, as that's all my image processing filters support. Tmp = SDL_ConvertSurface(img->surface, &fmt, SDL_SWSURFACE) Īnd as you can see, this is where that SG_IMAGE_ALPHA flag is coming from. SG_enum sgf_Prepare(SG_image *img, SG_filter_args *args) Speaking of endian and stuff, you can't really depend on IMG_Load() to return a surface with any particular pixel format, so doing something like this might be a good idea: SG_IMAGE_ALPHA is a flag that is 1 if the alpha channel is to actually be *used* - and if the alpha channel is not desired, it's just dropped in the upload by selecting an internal texture format without an alpha channel. This expects an SDL surface that is either 24 or 32 bit, in the endian format expected by OpenGL. Img->flags & SG_IMAGE_ALPHA ? GL_RGBA8 : GL_RGB8, GlBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) įor the alpha channel part to work (and not just vertex alpha modulation), you also need to get the alpha channel into the texture something like: Obviously, you need to enable blending, and you need to set up a suitable blending mode, for example: There are a few things you need to do on the OpenGL side. I made the transparent png file with the gimp. Testgl.c program from the tests folder in the SDL source package.īut the image no long shows up transparent, it has a black backgroundĪround it. I replaced the line SDL_LoadBMP with SDL-image's IMG_Load function in
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |