src/render/SDL_render.c
changeset 5297 1800dc39b74c
parent 5289 1916a9e9714d
child 5300 3f825465a14a
equal deleted inserted replaced
5296:48067bfc300c 5297:1800dc39b74c
    87 static int
    87 static int
    88 SDL_RendererEventWatch(void *userdata, SDL_Event *event)
    88 SDL_RendererEventWatch(void *userdata, SDL_Event *event)
    89 {
    89 {
    90     SDL_Renderer *renderer = (SDL_Renderer *)userdata;
    90     SDL_Renderer *renderer = (SDL_Renderer *)userdata;
    91 
    91 
    92     if (event->type == SDL_WINDOWEVENT && renderer->WindowEvent) {
    92     if (event->type == SDL_WINDOWEVENT) {
    93         SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
    93         SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
    94         if (window == renderer->window) {
    94         if (window == renderer->window) {
    95             renderer->WindowEvent(renderer, &event->window);
    95             if (renderer->WindowEvent) {
       
    96                 renderer->WindowEvent(renderer, &event->window);
       
    97             }
       
    98 
       
    99             if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
       
   100                 /* Try to keep the previous viewport centered */
       
   101                 int w, h;
       
   102                 SDL_Rect viewport;
       
   103 
       
   104                 SDL_GetWindowSize(window, &w, &h);
       
   105                 viewport.x = (w - renderer->viewport.w) / 2;
       
   106                 viewport.y = (h - renderer->viewport.h) / 2;
       
   107                 viewport.w = renderer->viewport.w;
       
   108                 viewport.h = renderer->viewport.h;
       
   109                 SDL_RenderSetViewport(renderer, &viewport);
       
   110             }
    96         }
   111         }
    97     }
   112     }
    98     return 0;
   113     return 0;
    99 }
   114 }
   100 
   115 
   158 
   173 
   159     if (renderer) {
   174     if (renderer) {
   160         renderer->magic = &renderer_magic;
   175         renderer->magic = &renderer_magic;
   161         renderer->window = window;
   176         renderer->window = window;
   162 
   177 
       
   178         SDL_RenderSetViewport(renderer, NULL);
       
   179 
   163         SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
   180         SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
   164 
   181 
   165         SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
   182         SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
   166                     "Created renderer: %s", renderer->info.name);
   183                     "Created renderer: %s", renderer->info.name);
   167     }
   184     }
   170 
   187 
   171 SDL_Renderer *
   188 SDL_Renderer *
   172 SDL_CreateSoftwareRenderer(SDL_Surface * surface)
   189 SDL_CreateSoftwareRenderer(SDL_Surface * surface)
   173 {
   190 {
   174 #if !SDL_RENDER_DISABLED
   191 #if !SDL_RENDER_DISABLED
   175     return SW_CreateRendererForSurface(surface);
   192     SDL_Renderer *renderer;
       
   193 
       
   194     renderer = SW_CreateRendererForSurface(surface);
       
   195 
       
   196     if (renderer) {
       
   197         renderer->magic = &renderer_magic;
       
   198 
       
   199         SDL_RenderSetViewport(renderer, NULL);
       
   200     }
       
   201     return renderer;
   176 #else
   202 #else
   177     SDL_SetError("SDL not built with rendering support");
   203     SDL_SetError("SDL not built with rendering support");
   178     return NULL;
   204     return NULL;
   179 #endif /* !SDL_RENDER_DISABLED */
   205 #endif /* !SDL_RENDER_DISABLED */
   180 }
   206 }
   340             SDL_UnlockSurface(surface);
   366             SDL_UnlockSurface(surface);
   341         } else {
   367         } else {
   342             SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
   368             SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
   343         }
   369         }
   344     } else {
   370     } else {
   345         SDL_PixelFormat dst_fmt;
   371         SDL_PixelFormat *dst_fmt;
   346         SDL_Surface *temp = NULL;
   372         SDL_Surface *temp = NULL;
   347 
   373 
   348         /* Set up a destination surface for the texture update */
   374         /* Set up a destination surface for the texture update */
   349         SDL_InitFormat(&dst_fmt, format);
   375         dst_fmt = SDL_AllocFormat(format);
   350         temp = SDL_ConvertSurface(surface, &dst_fmt, 0);
   376         temp = SDL_ConvertSurface(surface, dst_fmt, 0);
       
   377         SDL_FreeFormat(dst_fmt);
   351         if (temp) {
   378         if (temp) {
   352             SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
   379             SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
   353             SDL_FreeSurface(temp);
   380             SDL_FreeSurface(temp);
   354         } else {
   381         } else {
   355             SDL_DestroyTexture(texture);
   382             SDL_DestroyTexture(texture);
   731         renderer = texture->renderer;
   758         renderer = texture->renderer;
   732         renderer->UnlockTexture(renderer, texture);
   759         renderer->UnlockTexture(renderer, texture);
   733     }
   760     }
   734 }
   761 }
   735 
   762 
       
   763 int
       
   764 SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
       
   765 {
       
   766     CHECK_RENDERER_MAGIC(renderer, -1);
       
   767 
       
   768     if (rect) {
       
   769         renderer->viewport = *rect;
       
   770     } else {
       
   771         renderer->viewport.x = 0;
       
   772         renderer->viewport.y = 0;
       
   773         if (renderer->window) {
       
   774             SDL_GetWindowSize(renderer->window,
       
   775                               &renderer->viewport.w, &renderer->viewport.h);
       
   776         } else {
       
   777             /* This will be filled in by UpdateViewport() */
       
   778             renderer->viewport.w = 0;
       
   779             renderer->viewport.h = 0;
       
   780         }
       
   781     }
       
   782     return renderer->UpdateViewport(renderer);
       
   783 }
       
   784 
   736 void
   785 void
   737 SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
   786 SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
   738 {
   787 {
   739     CHECK_RENDERER_MAGIC(renderer, );
   788     CHECK_RENDERER_MAGIC(renderer, );
   740 
   789 
   741     renderer->SetClipRect(renderer, rect);
   790     *rect = renderer->viewport;
   742 }
   791 }
   743 
   792 
   744 int
   793 int
   745 SDL_SetRenderDrawColor(SDL_Renderer * renderer,
   794 SDL_SetRenderDrawColor(SDL_Renderer * renderer,
   746                        Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   795                        Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   882     if (!rect) {
   931     if (!rect) {
   883         SDL_Window *window = renderer->window;
   932         SDL_Window *window = renderer->window;
   884 
   933 
   885         full_rect.x = 0;
   934         full_rect.x = 0;
   886         full_rect.y = 0;
   935         full_rect.y = 0;
   887         SDL_GetWindowSize(window, &full_rect.w, &full_rect.h);
   936         full_rect.w = renderer->viewport.w;
       
   937         full_rect.h = renderer->viewport.h;
   888         rect = &full_rect;
   938         rect = &full_rect;
   889     }
   939     }
   890 
   940 
   891     points[0].x = rect->x;
   941     points[0].x = rect->x;
   892     points[0].y = rect->y;
   942     points[0].y = rect->y;
   901     return SDL_RenderDrawLines(renderer, points, 5);
   951     return SDL_RenderDrawLines(renderer, points, 5);
   902 }
   952 }
   903 
   953 
   904 int
   954 int
   905 SDL_RenderDrawRects(SDL_Renderer * renderer,
   955 SDL_RenderDrawRects(SDL_Renderer * renderer,
   906                     const SDL_Rect ** rects, int count)
   956                     const SDL_Rect * rects, int count)
   907 {
   957 {
   908     int i;
   958     int i;
   909 
   959 
   910     CHECK_RENDERER_MAGIC(renderer, -1);
   960     CHECK_RENDERER_MAGIC(renderer, -1);
   911 
   961 
   915     }
   965     }
   916     if (count < 1) {
   966     if (count < 1) {
   917         return 0;
   967         return 0;
   918     }
   968     }
   919 
   969 
   920     /* Check for NULL rect, which means fill entire window */
       
   921     for (i = 0; i < count; ++i) {
   970     for (i = 0; i < count; ++i) {
   922         if (SDL_RenderDrawRect(renderer, rects[i]) < 0) {
   971         if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
   923             return -1;
   972             return -1;
   924         }
   973         }
   925     }
   974     }
   926     return 0;
   975     return 0;
   927 }
   976 }
   928 
   977 
   929 int
   978 int
   930 SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
   979 SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
   931 {
   980 {
   932     return SDL_RenderFillRects(renderer, &rect, 1);
   981     return SDL_RenderFillRects(renderer, rect, 1);
   933 }
   982 }
   934 
   983 
   935 int
   984 int
   936 SDL_RenderFillRects(SDL_Renderer * renderer,
   985 SDL_RenderFillRects(SDL_Renderer * renderer,
   937                     const SDL_Rect ** rects, int count)
   986                     const SDL_Rect * rects, int count)
   938 {
   987 {
   939     int i;
       
   940 
       
   941     CHECK_RENDERER_MAGIC(renderer, -1);
   988     CHECK_RENDERER_MAGIC(renderer, -1);
   942 
   989 
   943     if (!rects) {
   990     if (!rects) {
   944         SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
   991         SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
   945         return -1;
   992         return -1;
   946     }
   993     }
   947     if (count < 1) {
   994     if (count < 1) {
   948         return 0;
   995         return 0;
   949     }
       
   950 
       
   951     /* Check for NULL rect, which means fill entire window */
       
   952     for (i = 0; i < count; ++i) {
       
   953         if (rects[i] == NULL) {
       
   954             SDL_Window *window = renderer->window;
       
   955             SDL_Rect full_rect;
       
   956             const SDL_Rect *rect;
       
   957 
       
   958             full_rect.x = 0;
       
   959             full_rect.y = 0;
       
   960             SDL_GetWindowSize(window, &full_rect.w, &full_rect.h);
       
   961             rect = &full_rect;
       
   962             return renderer->RenderFillRects(renderer, &rect, 1);
       
   963         }
       
   964     }
   996     }
   965     return renderer->RenderFillRects(renderer, rects, count);
   997     return renderer->RenderFillRects(renderer, rects, count);
   966 }
   998 }
   967 
   999 
   968 int
  1000 int
   992         }
  1024         }
   993     }
  1025     }
   994 
  1026 
   995     real_dstrect.x = 0;
  1027     real_dstrect.x = 0;
   996     real_dstrect.y = 0;
  1028     real_dstrect.y = 0;
   997     SDL_GetWindowSize(window, &real_dstrect.w, &real_dstrect.h);
  1029     real_dstrect.w = renderer->viewport.w;
       
  1030     real_dstrect.h = renderer->viewport.h;
   998     if (dstrect) {
  1031     if (dstrect) {
   999         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  1032         if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
  1000             return 0;
  1033             return 0;
  1001         }
  1034         }
  1002         /* Clip srcrect by the same amount as dstrect was clipped */
  1035         /* Clip srcrect by the same amount as dstrect was clipped */
  1041         format = SDL_GetWindowPixelFormat(window);
  1074         format = SDL_GetWindowPixelFormat(window);
  1042     }
  1075     }
  1043 
  1076 
  1044     real_rect.x = 0;
  1077     real_rect.x = 0;
  1045     real_rect.y = 0;
  1078     real_rect.y = 0;
  1046     SDL_GetWindowSize(window, &real_rect.w, &real_rect.h);
  1079     real_rect.w = renderer->viewport.w;
       
  1080     real_rect.h = renderer->viewport.h;
  1047     if (rect) {
  1081     if (rect) {
  1048         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1082         if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  1049             return 0;
  1083             return 0;
  1050         }
  1084         }
  1051         if (real_rect.y > rect->y) {
  1085         if (real_rect.y > rect->y) {