src/render/SDL_render.c
changeset 5368 f0ee2c086931
parent 5342 d4b691de3b8f
child 5369 e049a7b85fd1
equal deleted inserted replaced
5367:71fe70373e58 5368:f0ee2c086931
   997 SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   997 SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   998                const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   998                const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   999 {
   999 {
  1000     SDL_Window *window;
  1000     SDL_Window *window;
  1001     SDL_Rect real_srcrect;
  1001     SDL_Rect real_srcrect;
       
  1002     SDL_Rect clip_dstrect;
  1002     SDL_Rect real_dstrect;
  1003     SDL_Rect real_dstrect;
  1003 
  1004 
  1004     CHECK_RENDERER_MAGIC(renderer, -1);
  1005     CHECK_RENDERER_MAGIC(renderer, -1);
  1005     CHECK_TEXTURE_MAGIC(texture, -1);
  1006     CHECK_TEXTURE_MAGIC(texture, -1);
  1006 
  1007 
  1007     if (renderer != texture->renderer) {
  1008     if (renderer != texture->renderer) {
  1008         SDL_SetError("Texture was not created with this renderer");
  1009         SDL_SetError("Texture was not created with this renderer");
  1009         return -1;
  1010         return -1;
  1010     }
  1011     }
  1011     window = renderer->window;
  1012     window = renderer->window;
       
  1013 
       
  1014     if (dstrect) {
       
  1015         clip_dstrect = *dstrect;
       
  1016     } else {
       
  1017         clip_dstrect.x = 0;
       
  1018         clip_dstrect.y = 0;
       
  1019         clip_dstrect.w = renderer->viewport.w;
       
  1020         clip_dstrect.h = renderer->viewport.h;
       
  1021     }
       
  1022     dstrect = &clip_dstrect;
  1012 
  1023 
  1013     real_srcrect.x = 0;
  1024     real_srcrect.x = 0;
  1014     real_srcrect.y = 0;
  1025     real_srcrect.y = 0;
  1015     real_srcrect.w = texture->w;
  1026     real_srcrect.w = texture->w;
  1016     real_srcrect.h = texture->h;
  1027     real_srcrect.h = texture->h;
  1017     if (srcrect) {
  1028     if (srcrect) {
  1018         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  1029         if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  1019             return 0;
  1030             return 0;
  1020         }
  1031         }
       
  1032         /* Clip dstrect by the same amount as srcrect was clipped */
       
  1033         if (srcrect->w != real_srcrect.w) {
       
  1034             int deltax = (real_srcrect.x - srcrect->x);
       
  1035             int deltaw = (real_srcrect.w - srcrect->w);
       
  1036             clip_dstrect.x += (deltax * clip_dstrect.w) / srcrect->w;
       
  1037             clip_dstrect.w += (deltaw * clip_dstrect.w) / srcrect->w;
       
  1038         }
       
  1039         if (srcrect->h != real_srcrect.h) {
       
  1040             int deltay = (real_srcrect.y - srcrect->y);
       
  1041             int deltah = (real_srcrect.h - srcrect->h);
       
  1042             clip_dstrect.y += (deltay * clip_dstrect.h) / srcrect->h;
       
  1043             clip_dstrect.h += (deltah * clip_dstrect.h) / srcrect->h;
       
  1044         }
  1021     }
  1045     }
  1022 
  1046 
  1023     real_dstrect.x = 0;
  1047     real_dstrect.x = 0;
  1024     real_dstrect.y = 0;
  1048     real_dstrect.y = 0;
  1025     real_dstrect.w = renderer->viewport.w;
  1049     real_dstrect.w = renderer->viewport.w;
  1026     real_dstrect.h = renderer->viewport.h;
  1050     real_dstrect.h = renderer->viewport.h;
  1027     if (dstrect) {
  1051     if (!SDL_IntersectRect(&clip_dstrect, &real_dstrect, &real_dstrect)) {
  1028         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  1052         return 0;
  1029             return 0;
  1053     }
  1030         }
  1054     /* Clip srcrect by the same amount as dstrect was clipped */
  1031         /* Clip srcrect by the same amount as dstrect was clipped */
  1055     if (dstrect->w != real_dstrect.w) {
  1032         if (dstrect->w != real_dstrect.w) {
  1056         int deltax = (real_dstrect.x - dstrect->x);
  1033             int deltax = (real_dstrect.x - dstrect->x);
  1057         int deltaw = (real_dstrect.w - dstrect->w);
  1034             int deltaw = (real_dstrect.w - dstrect->w);
  1058         real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  1035             real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
  1059         real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  1036             real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
  1060     }
  1037         }
  1061     if (dstrect->h != real_dstrect.h) {
  1038         if (dstrect->h != real_dstrect.h) {
  1062         int deltay = (real_dstrect.y - dstrect->y);
  1039             int deltay = (real_dstrect.y - dstrect->y);
  1063         int deltah = (real_dstrect.h - dstrect->h);
  1040             int deltah = (real_dstrect.h - dstrect->h);
  1064         real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  1041             real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
  1065         real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
  1042             real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
       
  1043         }
       
  1044     }
  1066     }
  1045 
  1067 
  1046     if (texture->native) {
  1068     if (texture->native) {
  1047         texture = texture->native;
  1069         texture = texture->native;
  1048     }
  1070     }