![]() SDL_WINDOWPOS_UNDEFINED, 1280, 720, 0) // specify window ![]() SDL_Window *window = SDL_CreateWindow(“My Window”, SDL_WINDOWPOS_UNDEFINED, SDL_Init(SDL_INIT_VIDEO) // initialize the window I tried following the IMG_Load tutorial yet it also shows a black screen, no image at all. Game Development Programmer's Ranch SDL2 Post navigation We also saw the difference between copying the image to a region of the window, and making it fill the entire window. We used these to load bitmaps and display them on the screen. When we run the program, we get this:Įxcellent! □ In this article we looked at how to create renderers, surfaces and textures. We set it to an SDL_Rect, with x and y set to 5, and width and height corresponding to the dimensions of the image. If it’s set to NULL, then the image is stretched to fill the texture – which is what happened above.īy setting dstrect (the last parameter), we can render to only a portion of the window. Remember those two parameters at the end of SDL_RenderCopy() that we were setting to NULL? If you look at the documentation for SDL_RenderCopy(), you’ll see that the last one defines a destination region (which part of the texture will the image be copied to). SDL_RenderCopy(renderer, texture, NULL, &dstrect) Let’s change our call to SDL_RenderCopy() to fix this: That’s sometimes convenient, because we get scaling for free. If you now run the program from within Visual Studio, you should see the image in your window:įantastic! The image is right there in the window! But… can you see what happened? The image, which has dimensions 320×240, has been stretched to fill the window, which has dimensions 640×480. In our case, that means adding the following just before the call to SDL_Quit(): Finally, SDL_RenderPresent() is what commits the texture to the video memory, displaying the image.īefore we run the program, we must always remember to clean up every resource that we initialise. There are also a couple of other parameters that we’re setting to NULL – more on these in a minute. ![]() 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |