Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL_RenderPresent vs SDL_UpdateWindowSurface

Tags:

c++

sdl

I have successfully created and drawn both a bitmap image and drawn a green line using a renderer to my SDL window. The problem is I am unsure of how to do both at once on the same window.

void draw::image(){
    SDL_Surface *bmp = SDL_LoadBMP("C:\\Users\\Joe\\Documents\\Visual Studio 2013\\Projects\\SDL_APP1\\map1.bmp");
    SDL_BlitSurface(bmp, 0, SDL_GetWindowSurface(_window), 0);

    SDL_Renderer * renderer = SDL_CreateRenderer(_window, -1, 0);
    // draw green line across screen
    SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
    SDL_RenderDrawLine(renderer, 0, 0, 640, 320);
    SDL_RenderPresent(renderer);
    SDL_UpdateWindowSurface(_window);
    SDL_Delay(20000);

    // free resources
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(_window);
}

This version of my code draws the bmp file to the window because SDL_UpdateWindowSurface(); is after SDL_RenderPresent(); however when I flip these it draws a green line to the screen. How would I draw the green line over my BMP?

like image 709
Joe Aulicino Avatar asked Oct 28 '15 22:10

Joe Aulicino


2 Answers

If you store your images in RAM and use the CPU for rendering(this is called software rendering) you use SDL_UpdateWindowSurface.

By calling this function you tell the CPU to update the screen and draw using software rendering.

You can store your textures in RAM by using SDL_Surface, but software rendering is inefficent. You can give draw calls by using SDL_BlitSurface.

SDL_UpdateWindowSurface is equivalent to the SDL 1.2 API SDL_Flip().

On the other side when you use the GPU to render textures and you store your texture on the GPU(this is called hardware accelerated rendering), which you should, you use SDL_RenderPresent.

This function tells the GPU to render to the screen.

You store texture on the GPU using SDL_Texture. When using this you can give draw calls by using SDL_RenderCopy or if you want transformations SDL_RenderCopyEx

Therefore, when using SDL's rendering API, one does all drawing intended for the frame, and then calls this function once per frame to present the final drawing to the user.

You should you use hardware rendering it's far more efficent, than software rendering! Even if the user running the program hasn't got a GPU (which is rare, because most CPU's have an integrated GPU) SDL will switch to software rendering by it self!

By the way you can load an image as SDL_Texture without the need to load an image as an SDL_Surface and convert it to an SDL_Texture using the SDL_image library, which you should because it supports several image formats not just BMP, like pure SDL. (SDL_image is made by the creators of SDL)

Just use the IMG_LoadTexture from SDL_image!

like image 88
kovacsmarcell Avatar answered Sep 19 '22 04:09

kovacsmarcell


You cannot use both methods at the same time and must choose one or the other. I would recommend going with SDL_Renderer. Create an SDL_Texture from your SDL_Surface and render it with SDL_RenderCopy.

like image 32
Jason Norris Avatar answered Sep 22 '22 04:09

Jason Norris