In order to run a game without using the CPU/GPU too much, I want to only update the section of the window that has changed.
Does this goal even make sense? How does the texture objects/renderer work when updating the screen? My idea is that the renderer builds a matrix of ints (pixel colours) and when SDL_RenderPresent is called, this edits a matrix stored on the memory that controls the colours for each pixel on the screen.
If what I've said so far makes sense, then is my implementation below correct (not updating more of the screen than needed)? I've tried to keep my code very, very simple so that it's easy to read.
The programme jumps the character (picture of a prince) from the top-left of the window to the bottom-left, and visa-versa, whenever the user inputs anything. This is just to keep the code simple for the example.
#include <iostream> #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> using namespace std; int main () { // Screen dimension constants const int WINDOW_WIDTH = 639; const int WINDOW_HEIGHT = 1015; const int PRINCE_WIDTH = 74; const int PRINCE_HEIGHT = 114; SDL_Init ( SDL_INIT_EVERYTHING ); SDL_Window * window = SDL_CreateWindow ( "example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN ); SDL_Renderer * renderer = SDL_CreateRenderer ( window, -1, SDL_RENDERER_ACCELERATED ); SDL_Texture * background = IMG_LoadTexture ( renderer, "./images/background.bmp" ); SDL_Rect rect_background = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT }; SDL_RenderCopy ( renderer, background, & rect_background, & rect_background ); const SDL_Rect rect_place_one = { 400, 600, PRINCE_WIDTH, PRINCE_HEIGHT }; const SDL_Rect rect_place_two = { 40, 60, PRINCE_WIDTH, PRINCE_HEIGHT }; SDL_Texture * prince = IMG_LoadTexture ( renderer, "./images/prince.bmp" ); SDL_RenderCopy ( renderer, prince, NULL, & rect_place_one ); SDL_RenderPresent ( renderer ); SDL_Event input; for ( uint8_t count = 0; SDL_WaitEvent( & input ); ++count ) { // User requests quit if ( input.type == SDL_QUIT ) break; if ( input.type != SDL_KEYDOWN ) continue; if ( ( count % 2 ) ) { SDL_RenderCopy ( renderer, prince, NULL, & rect_place_one ); SDL_RenderCopy ( renderer, background, & rect_place_two, & rect_place_two ); } else { SDL_RenderCopy ( renderer, prince, NULL, & rect_place_two ); SDL_RenderCopy ( renderer, background, & rect_place_one, & rect_place_one ); } SDL_RenderPresent ( renderer ); } SDL_DestroyTexture ( prince ); SDL_DestroyTexture ( background ); SDL_RenderClear (renderer); SDL_DestroyWindow ( window ); SDL_Quit (); return 0; }