Before you run it, though, make sure that SDL2.dll is in the same folder as your executable. You should now be able to build this program (Ctrl+Shift+B). SDL_Surface * image = IMG_Load("PICT3159.JPG") …and before the call to SDL_Quit(), we shut down SDL_Image using IMG_Quit():Īll we have left to do now is replace the line calling SDL_LoadBMP() with one that uses IMG_Load() instead: Then, we initialise SDL_image by calling IMG_Init() right after the call to SDL_Init(): First, we need to include the appropriate header: Now, we’re going to load this nice photo (.jpg format) taken in Gardaland in 2006:ĭoing this is quite simple. The entry there should now look like this: SDL2_image.lib), the first thing we need to do is add it to the Linker -> Input in the project properties: Now since we’ve introduced a new statically-linked library (i.e. SDL_RenderCopy(renderer, texture, NULL, NULL) SDL_RenderCopy(renderer, texture, NULL, &dstrect) SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image) SDL_Surface * image = SDL_LoadBMP("image.bmp") SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0) SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0) SDL_Window * window = SDL_CreateWindow("SDL2 Displaying Image", I’ve modified the code so that the image fills the window: We’ll start off with the same code we had in “ Displaying an Image in an SDL2 Window” (you can just grab the source code if you’re lazy). You should get an SDL_image.h file in your include folder, and in your lib\x86 and lib\圆4 folders you should get SDL2_image.lib and a whole bunch of DLLs including SDL2_image.dll. You need to go to the SDL_image homepage and download the development libraries:Įxtract the include and lib folders inside the zip file over the ones you have in your sdl2 folder. Setting up SDL_image is not very different from setting up SDL 2.0 itself. In this article, we will learn how to set up SDL_image and also how to use it to load other image formats. However, such functionality is provided by an extension library called SDL_image. Unfortunately, working with bitmaps is very limiting, and the core SDL2 library does not provide the means to work with other image formats. In “ SDL2: Displaying an Image in the Window“, we saw how we could load bitmaps using the SDL_LoadBMP() function. The source code for this article is available at the Gigi Labs BitBucket repository. This function is available since SDL_image 2.0.0.This article was originally posted as “ SDL2: Loading Images with SDL_image” on 25th November 2013 at Programmer’s Ranch, and has been updated before being reposted here. 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_LoadTyped_RW() instead. There is also IMG_LoadTexture_RW(), which is equivalent to this function except that it will rely on SDL_image to determine what type of data it is loading, much like passing a NULL for type. There is a separate function to read files from disk without having to deal with SDL_RWops: IMG_LoadTexture("filename.jpg") will call this function and manage those details for you, determining the file type from the filename's extension. If type is NULL, SDL_image will rely solely on its ability to guess the format. SDL_image reads everything it needs from the RWops during this call in any case.Įven though this function accepts a file type, SDL_image may still try other decoders that are capable of detecting file type from the contents of the image data, but may rely on the caller-provided type string for formats that it cannot autodetect. 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.Ī filename extension that represent this data ("BMP", "GIF", "PNG", etc). The SDL_Renderer to use to create the GPU texture.Īn SDL_RWops that data will be read from. SDL_Texture * IMG_LoadTextureTyped_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc, const char *type) Function Parameters renderer
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |