SDL_RenderCopy

Hi guys,

not too sure if anybody here has dealt much with SDL,but I'll give it a shot,

I'm wondering how the SDL_RenderCopy function works - https://wiki.libsdl.org/SDL_RenderCopy

it takes a renderer which is obvious,a texture, two SDL_rects one source one destination, this function is used to scale textures but I'm not sure why and how it does it, why does it take two SDL_Rects by reference as the params,and what is meant by source and destination in this context?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *firstPic;
SDL_Surface *AdamTmp;
SDL_Texture *Adam;
SDL_Rect srcRec,destRec;
SDL_Event event;

bool gameInit(){

   if(SDL_Init(SDL_INIT_EVERYTHING) < 0){

     cout << "error" << endl;
     return false;
   }
   if(IMG_Init(IMG_INIT_PNG) < 0){

     cout << "error" << endl;
     return false;
   }

    window = SDL_CreateWindow("The GTG game",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,670,600,SDL_WINDOW_RESIZABLE);
    renderer = SDL_CreateRenderer(window,-1,0);
    AdamTmp = IMG_Load("M.png");
    Adam = SDL_CreateTextureFromSurface(renderer,AdamTmp);
    SDL_SetRenderDrawColor(renderer,255,255,255,255);

    return true;
}

void render(){

  SDL_RenderClear(renderer);
  SDL_RenderCopy(renderer,Adam,NULL,&destRec);
  SDL_RenderPresent(renderer);

}


for example I resized my image "M.png" using this function but not sure how it works,

thanks



"Source" is the thing that you are drawing from (i.e. the texture).

"Destination" is the thing that you are drawing to (i.e. the window).

srcrect is the portion of the texture that you want to draw. If you want to draw the whole texture you can use null.

dstrect is the portion of the window that should be covered by the part of the texture that you are drawing. If you want to cover the whole window you can use null.
Last edited on
Thanks Peter,

but lets say I wanted to place the texture in the bottom right corner instead of the top left by default how would I do this?
You set the x and y members of the dst parameter to the top-left corner of where on the destination you want to copy the source.
thanks helios =)
Topic archived. No new replies allowed.