src/SDL12_compat.c
changeset 70 aa6bf2a4a8c3
parent 69 a2c5f6a01a55
child 71 7a5593f0cd40
     1.1 --- a/src/SDL12_compat.c	Mon Feb 18 00:26:53 2019 -0500
     1.2 +++ b/src/SDL12_compat.c	Mon Feb 18 00:28:06 2019 -0500
     1.3 @@ -441,6 +441,8 @@
     1.4  static SDL12_Surface *VideoSurface12 = NULL;
     1.5  static SDL_Surface *VideoConvertSurface20 = NULL;
     1.6  static SDL_GLContext *VideoGLContext20 = NULL;
     1.7 +static int VideoNumDirtyRects = 0;
     1.8 +static SDL_Rect VideoDirtyRects[16];
     1.9  static char *WindowTitle = NULL;
    1.10  static char *WindowIconTitle = NULL;
    1.11  static SDL12_Surface *VideoIcon12;
    1.12 @@ -920,12 +922,25 @@
    1.13      return GetDriverName(SDL20_GetCurrentVideoDriver(), namebuf, maxlen);
    1.14  }
    1.15  
    1.16 +static void PresentScreen(void);
    1.17 +
    1.18 +DECLSPEC void SDLCALL
    1.19 +SDL_PumpEvents(void)
    1.20 +{
    1.21 +    /* catch things that want to do dirty rectangles but ignore DOUBLEBUF.
    1.22 +       Since we have to compose the whole scene at once, catch it in here. */
    1.23 +    if (VideoSurface12 && VideoNumDirtyRects) {
    1.24 +        PresentScreen();
    1.25 +    }
    1.26 +    SDL20_PumpEvents();
    1.27 +}
    1.28 +
    1.29  DECLSPEC int SDLCALL
    1.30  SDL_PollEvent(SDL12_Event *event12)
    1.31  {
    1.32      EventQueueType *next;
    1.33  
    1.34 -    SDL20_PumpEvents();  /* this will run our filter and build our 1.2 queue. */
    1.35 +    SDL_PumpEvents();  /* this will run our filter and build our 1.2 queue. */
    1.36  
    1.37      if (EventQueueHead == NULL)
    1.38          return 0;  /* no events at the moment. */
    1.39 @@ -1855,6 +1870,8 @@
    1.40          }
    1.41      }
    1.42  
    1.43 +    VideoNumDirtyRects = 0;
    1.44 +
    1.45      VideoSurface12->surface20->flags |= SDL_PREALLOC;
    1.46      VideoSurface12->flags |= SDL12_PREALLOC;
    1.47      VideoSurface12->pixels = VideoSurface12->surface20->pixels = NULL;
    1.48 @@ -2009,11 +2026,78 @@
    1.49      return NULL;
    1.50  }
    1.51  
    1.52 +static void
    1.53 +PresentScreen(void)
    1.54 +{
    1.55 +    void *pixels = NULL;
    1.56 +    int pitch = 0;
    1.57 +    int i;
    1.58 +
    1.59 +    SDL_assert(VideoSurface12 != NULL);
    1.60 +    SDL_assert(VideoNumDirtyRects > 0);  /* only called internally from places that add rects */
    1.61 +    SDL_assert(VideoNumDirtyRects <= SDL_arraysize(VideoDirtyRects));
    1.62 +
    1.63 +    FIXME("Maybe lock a subset of the texture if only one dirty rectangle?");
    1.64 +
    1.65 +    if (SDL20_LockTexture(VideoTexture20, NULL, &pixels, &pitch) < 0) {
    1.66 +        return;  /* oh well */
    1.67 +    }
    1.68 +
    1.69 +    FIXME("Maybe lock texture always, until present, if no conversion needed?");
    1.70 +    VideoConvertSurface20->pixels = pixels;
    1.71 +    VideoConvertSurface20->pitch = pitch;
    1.72 +
    1.73 +    for (i = 0; i < VideoNumDirtyRects; i++) {
    1.74 +        SDL20_UpperBlit(VideoSurface12->surface20, &VideoDirtyRects[i], VideoConvertSurface20, &VideoDirtyRects[i]);
    1.75 +    }
    1.76 +
    1.77 +    VideoConvertSurface20->pixels = NULL;
    1.78 +    VideoConvertSurface20->pitch = 0;
    1.79 +    VideoNumDirtyRects = 0;
    1.80 +
    1.81 +    SDL20_UnlockTexture(VideoTexture20);
    1.82 +    SDL20_RenderCopy(VideoRenderer20, VideoTexture20, NULL, NULL);
    1.83 +    SDL20_RenderPresent(VideoRenderer20);
    1.84 +}
    1.85 +
    1.86 +static void
    1.87 +AddDirtyRect(const SDL_Rect *r)
    1.88 +{
    1.89 +    const int sw = VideoSurface12->w;
    1.90 +    const int sh = VideoSurface12->h;
    1.91 +    FIXME("Clip against the surface geometry");
    1.92 +    if (r == NULL) {
    1.93 +        VideoNumDirtyRects = -1;  /* NULL == "replace whole screen" */
    1.94 +    } else if ((r->x <= 0) && (r->y <= 0) && (r->w >= VideoSurface12->w) && (r->h >= VideoSurface12->h)) {
    1.95 +        VideoNumDirtyRects = -1;  /* this rect covers whole screen */
    1.96 +    } else if (VideoNumDirtyRects >= SDL_arraysize(VideoDirtyRects)) {
    1.97 +        VideoNumDirtyRects = -1;  /* too many rects, just do it all */
    1.98 +    } else if ((r->x >= sw) || (r->y >= sh)) {
    1.99 +        return;  /* nothing to do */
   1.100 +    } else if (VideoNumDirtyRects >= 0) {
   1.101 +        FIXME("Clip against other rectangles?");
   1.102 +        SDL_memcpy(&VideoDirtyRects[VideoNumDirtyRects], r, sizeof (*r));
   1.103 +        VideoNumDirtyRects++;
   1.104 +    }
   1.105 +}
   1.106 +
   1.107  DECLSPEC void SDLCALL
   1.108 -SDL_UpdateRects(SDL12_Surface * screen12, int numrects, SDL_Rect * rects)
   1.109 +SDL_UpdateRects(SDL12_Surface *surface12, int numrects, SDL_Rect *rects)
   1.110  {
   1.111 -    FIXME("write me");
   1.112 -    SDL20_Unsupported();
   1.113 +    int i;
   1.114 +
   1.115 +    /* strangely, SDL 1.2 doesn't check if surface12 is NULL before touching it */
   1.116 +    /* (UpdateRect, singular, does...) */
   1.117 +    if (surface12->flags & SDL12_OPENGL) {
   1.118 +        SDL20_SetError("Use SDL_GL_SwapBuffers() on OpenGL surfaces");
   1.119 +        return;
   1.120 +    }
   1.121 +
   1.122 +    if (surface12 == VideoSurface12) {
   1.123 +        for (i = 0; i < numrects; i++) {
   1.124 +            AddDirtyRect(&rects[i]);
   1.125 +        }
   1.126 +    }
   1.127  }
   1.128  
   1.129  DECLSPEC void SDLCALL
   1.130 @@ -2030,9 +2114,17 @@
   1.131  }
   1.132  
   1.133  DECLSPEC int SDLCALL
   1.134 -SDL_Flip(SDL12_Surface *screen12)
   1.135 +SDL_Flip(SDL12_Surface *surface12)
   1.136  {
   1.137 -    SDL_UpdateRect(screen12, 0, 0, 0, 0);
   1.138 +    if (surface12->flags & SDL12_OPENGL) {
   1.139 +        return SDL20_SetError("Use SDL_GL_SwapBuffers() on OpenGL surfaces");
   1.140 +    }
   1.141 +
   1.142 +    if (surface12 == VideoSurface12) {
   1.143 +        AddDirtyRect(NULL);
   1.144 +        PresentScreen();
   1.145 +    }
   1.146 +
   1.147      return 0;
   1.148  }
   1.149