![]() We use SDL_RenderCopy() to copy the texture to the output device. SDL_RenderCopy(renderer, texture, NULL, NULL) At the end of the while loop, just after the switch statement, add the following: Now that we have a texture, let’s display it in the window. SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image) Ī texture is memory close to the graphics card (see image below), and we use SDL_CreateTextureFromSurface() to directly map our surface (which contains the image we loaded) to a texture. Place the image in your Debug folder, where your executable is located. ![]() I’m using the following photo of the Grand Harbour in Valletta, Malta, which I took back in 2005, and which has a width of 320 pixels and a height of 240 pixels: SDL_Surface * image = SDL_LoadBMP("image.bmp") We can now load the image from disk using the SDL_LoadBMP() function, which takes a path to a bitmap and loads it into an SDL_Surface: If any of this sounds confusing, don’t worry about it and just use SDL_CreateRenderer() as it is here. We can set it to zero to default to hardware rendering. The last parameter allows us to set SDL_RendererFlags to control how the rendering occurs. The second allows us to select different rendering drivers in our case we don’t care, and we can set it to -1 and get the default one. The first is the window where we are drawing. SDL_CreateRenderer() takes three parameters. In fact it’s the final destination of our image, because we’ll be following the steps below: This renderer represents the output device (usually your graphics card) to which your code will be drawing. We use SDL_CreateRenderer() to get an instance of SDL_Renderer. 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", In order to follow along, you’ll need to set up a project to work with SDL2 (see Setting up SDL2 with Visual Studio 2015), and start off with the following basic empty window code, which is practically the same as what we did in Showing an Empty Window in SDL2: In this article we’re going to learn how to load a bitmap image from disk and display it in an SDL2 window. The source code for this article is available at the Gigi Labs BitBucket repository. It has been updated, and the part about detecting and displaying errors has been removed as it is better addressed by configuring the project’s working directory. This article was originally posted as “ SDL2: Displaying an Image in the Window” on 17th November 2013 at Programmer’s Ranch.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |