The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
authorSam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 19:19:43 -0800
changeset 5147ad50b3db78bd
parent 5146 3052772b59db
child 5148 5429daf5e3f9
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
include/SDL_render.h
src/SDL_compat.c
src/events/SDL_windowevents.c
src/video/SDL_renderer_gl.c
src/video/SDL_renderer_gles.c
src/video/SDL_renderer_sw.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
test/common.c
test/common.h
test/testdraw2.c
test/testintersections.c
test/testshape.c
test/testsprite2.c
test/testspriteminimal.c
     1.1 --- a/include/SDL_render.h	Tue Feb 01 19:15:42 2011 -0800
     1.2 +++ b/include/SDL_render.h	Tue Feb 01 19:19:43 2011 -0800
     1.3 @@ -86,6 +86,12 @@
     1.4  } SDL_TextureModulate;
     1.5  
     1.6  /**
     1.7 + *  \brief A structure representing rendering state
     1.8 + */
     1.9 +struct SDL_Renderer;
    1.10 +typedef struct SDL_Renderer SDL_Renderer;
    1.11 +
    1.12 +/**
    1.13   *  \brief An efficient driver-specific representation of pixel data
    1.14   */
    1.15  struct SDL_Texture;
    1.16 @@ -123,37 +129,29 @@
    1.17                                                      SDL_RendererInfo * info);
    1.18  
    1.19  /**
    1.20 - *  \brief Create and make active a 2D rendering context for a window.
    1.21 + *  \brief Create a 2D rendering context for a window.
    1.22   *  
    1.23   *  \param window The window where rendering is displayed.
    1.24   *  \param index    The index of the rendering driver to initialize, or -1 to 
    1.25   *                  initialize the first one supporting the requested flags.
    1.26   *  \param flags    ::SDL_RendererFlags.
    1.27   *  
    1.28 - *  \return 0 on success, -1 if there was an error creating the renderer.
    1.29 + *  \return A valid rendering context or NULL if there was an error.
    1.30   *  
    1.31 - *  \sa SDL_SelectRenderer()
    1.32   *  \sa SDL_GetRendererInfo()
    1.33   *  \sa SDL_DestroyRenderer()
    1.34   */
    1.35 -extern DECLSPEC int SDLCALL SDL_CreateRenderer(SDL_Window * window,
    1.36 +extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window * window,
    1.37                                                 int index, Uint32 flags);
    1.38  
    1.39  /**
    1.40 - *  \brief Select the rendering context for a particular window.
    1.41 - *  
    1.42 - *  \return 0 on success, -1 if the selected window doesn't have a
    1.43 - *          rendering context.
    1.44 + *  \brief Get information about a rendering context.
    1.45   */
    1.46 -extern DECLSPEC int SDLCALL SDL_SelectRenderer(SDL_Window * window);
    1.47 +extern DECLSPEC int SDLCALL SDL_GetRendererInfo(SDL_Renderer * renderer,
    1.48 +                                                SDL_RendererInfo * info);
    1.49  
    1.50  /**
    1.51 - *  \brief Get information about the current rendering context.
    1.52 - */
    1.53 -extern DECLSPEC int SDLCALL SDL_GetRendererInfo(SDL_RendererInfo * info);
    1.54 -
    1.55 -/**
    1.56 - *  \brief Create a texture for the current rendering context.
    1.57 + *  \brief Create a texture for a rendering context.
    1.58   *  
    1.59   *  \param format The format of the texture.
    1.60   *  \param access One of the enumerated values in ::SDL_TextureAccess.
    1.61 @@ -167,7 +165,7 @@
    1.62   *  \sa SDL_QueryTexture()
    1.63   *  \sa SDL_DestroyTexture()
    1.64   */
    1.65 -extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(Uint32 format,
    1.66 +extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer,                                                        Uint32 format,
    1.67                                                          int access, int w,
    1.68                                                          int h);
    1.69  
    1.70 @@ -186,10 +184,7 @@
    1.71   *  \sa SDL_QueryTexture()
    1.72   *  \sa SDL_DestroyTexture()
    1.73   */
    1.74 -extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(Uint32
    1.75 -                                                                   format,
    1.76 -                                                                   SDL_Surface
    1.77 -                                                                   * surface);
    1.78 +extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(SDL_Renderer * renderer, Uint32 format, SDL_Surface * surface);
    1.79  
    1.80  /**
    1.81   *  \brief Query the attributes of a texture
    1.82 @@ -419,9 +414,10 @@
    1.83   *  \param a The alpha value used to draw on the rendering target, usually 
    1.84   *           ::SDL_ALPHA_OPAQUE (255).
    1.85   *  
    1.86 - *  \return 0 on success, or -1 if there is no rendering context current.
    1.87 + *  \return 0 on success, or -1 on error
    1.88   */
    1.89 -extern DECLSPEC int SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b,
    1.90 +extern DECLSPEC int SDL_SetRenderDrawColor(SDL_Renderer * renderer,
    1.91 +                                           Uint8 r, Uint8 g, Uint8 b,
    1.92                                             Uint8 a);
    1.93  
    1.94  /**
    1.95 @@ -433,9 +429,10 @@
    1.96   *  \param a A pointer to the alpha value used to draw on the rendering target, 
    1.97   *           usually ::SDL_ALPHA_OPAQUE (255).
    1.98   *  
    1.99 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.100 + *  \return 0 on success, or -1 on error
   1.101   */
   1.102 -extern DECLSPEC int SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b,
   1.103 +extern DECLSPEC int SDL_GetRenderDrawColor(SDL_Renderer * renderer,
   1.104 +                                           Uint8 * r, Uint8 * g, Uint8 * b,
   1.105                                             Uint8 * a);
   1.106  
   1.107  /**
   1.108 @@ -443,30 +440,32 @@
   1.109   *  
   1.110   *  \param blendMode ::SDL_BlendMode to use for blending.
   1.111   *  
   1.112 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.113 + *  \return 0 on success, or -1 on error
   1.114   *  
   1.115   *  \note If the blend mode is not supported, the closest supported mode is 
   1.116   *        chosen.
   1.117   *  
   1.118   *  \sa SDL_GetRenderDrawBlendMode()
   1.119   */
   1.120 -extern DECLSPEC int SDLCALL SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode);
   1.121 +extern DECLSPEC int SDLCALL SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer,
   1.122 +                                                       SDL_BlendMode blendMode);
   1.123  
   1.124  /**
   1.125   *  \brief Get the blend mode used for drawing operations.
   1.126   *  
   1.127   *  \param blendMode A pointer filled in with the current blend mode.
   1.128   *  
   1.129 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.130 + *  \return 0 on success, or -1 on error
   1.131   *  
   1.132   *  \sa SDL_SetRenderDrawBlendMode()
   1.133   */
   1.134 -extern DECLSPEC int SDLCALL SDL_GetRenderDrawBlendMode(SDL_BlendMode *blendMode);
   1.135 +extern DECLSPEC int SDLCALL SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer,
   1.136 +                                                       SDL_BlendMode *blendMode);
   1.137  
   1.138  /**
   1.139   *  \brief Clear the current rendering target with the drawing color
   1.140   */
   1.141 -extern DECLSPEC int SDLCALL SDL_RenderClear(void);
   1.142 +extern DECLSPEC int SDLCALL SDL_RenderClear(SDL_Renderer * renderer);
   1.143  
   1.144  /**
   1.145   *  \brief Draw a point on the current rendering target.
   1.146 @@ -474,9 +473,10 @@
   1.147   *  \param x The x coordinate of the point.
   1.148   *  \param y The y coordinate of the point.
   1.149   *  
   1.150 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.151 + *  \return 0 on success, or -1 on error
   1.152   */
   1.153 -extern DECLSPEC int SDLCALL SDL_RenderDrawPoint(int x, int y);
   1.154 +extern DECLSPEC int SDLCALL SDL_RenderDrawPoint(SDL_Renderer * renderer,
   1.155 +                                                int x, int y);
   1.156  
   1.157  /**
   1.158   *  \brief Draw multiple points on the current rendering target.
   1.159 @@ -484,9 +484,10 @@
   1.160   *  \param points The points to draw
   1.161   *  \param count The number of points to draw
   1.162   *  
   1.163 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.164 + *  \return 0 on success, or -1 on error
   1.165   */
   1.166 -extern DECLSPEC int SDLCALL SDL_RenderDrawPoints(const SDL_Point * points,
   1.167 +extern DECLSPEC int SDLCALL SDL_RenderDrawPoints(SDL_Renderer * renderer,
   1.168 +                                                 const SDL_Point * points,
   1.169                                                   int count);
   1.170  
   1.171  /**
   1.172 @@ -497,9 +498,10 @@
   1.173   *  \param x2 The x coordinate of the end point.
   1.174   *  \param y2 The y coordinate of the end point.
   1.175   *  
   1.176 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.177 + *  \return 0 on success, or -1 on error
   1.178   */
   1.179 -extern DECLSPEC int SDLCALL SDL_RenderDrawLine(int x1, int y1, int x2, int y2);
   1.180 +extern DECLSPEC int SDLCALL SDL_RenderDrawLine(SDL_Renderer * renderer,
   1.181 +                                               int x1, int y1, int x2, int y2);
   1.182  
   1.183  /**
   1.184   *  \brief Draw a series of connected lines on the current rendering target.
   1.185 @@ -507,9 +509,10 @@
   1.186   *  \param points The points along the lines
   1.187   *  \param count The number of points, drawing count-1 lines
   1.188   *  
   1.189 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.190 + *  \return 0 on success, or -1 on error
   1.191   */
   1.192 -extern DECLSPEC int SDLCALL SDL_RenderDrawLines(const SDL_Point * points,
   1.193 +extern DECLSPEC int SDLCALL SDL_RenderDrawLines(SDL_Renderer * renderer,
   1.194 +                                                const SDL_Point * points,
   1.195                                                  int count);
   1.196  
   1.197  /**
   1.198 @@ -517,9 +520,10 @@
   1.199   *  
   1.200   *  \param rect A pointer to the destination rectangle, or NULL to outline the entire rendering target.
   1.201   *  
   1.202 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.203 + *  \return 0 on success, or -1 on error
   1.204   */
   1.205 -extern DECLSPEC int SDLCALL SDL_RenderDrawRect(const SDL_Rect * rect);
   1.206 +extern DECLSPEC int SDLCALL SDL_RenderDrawRect(SDL_Renderer * renderer,
   1.207 +                                               const SDL_Rect * rect);
   1.208  
   1.209  /**
   1.210   *  \brief Draw some number of rectangles on the current rendering target.
   1.211 @@ -527,9 +531,11 @@
   1.212   *  \param rects A pointer to an array of destination rectangles.
   1.213   *  \param count The number of rectangles.
   1.214   *  
   1.215 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.216 + *  \return 0 on success, or -1 on error
   1.217   */
   1.218 -extern DECLSPEC int SDLCALL SDL_RenderDrawRects(const SDL_Rect ** rects, int count);
   1.219 +extern DECLSPEC int SDLCALL SDL_RenderDrawRects(SDL_Renderer * renderer,
   1.220 +                                                const SDL_Rect ** rects,
   1.221 +                                                int count);
   1.222  
   1.223  /**
   1.224   *  \brief Fill a rectangle on the current rendering target with the drawing color.
   1.225 @@ -537,9 +543,10 @@
   1.226   *  \param rect A pointer to the destination rectangle, or NULL for the entire 
   1.227   *              rendering target.
   1.228   *  
   1.229 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.230 + *  \return 0 on success, or -1 on error
   1.231   */
   1.232 -extern DECLSPEC int SDLCALL SDL_RenderFillRect(const SDL_Rect * rect);
   1.233 +extern DECLSPEC int SDLCALL SDL_RenderFillRect(SDL_Renderer * renderer,
   1.234 +                                               const SDL_Rect * rect);
   1.235  
   1.236  /**
   1.237   *  \brief Fill some number of rectangles on the current rendering target with the drawing color.
   1.238 @@ -547,9 +554,11 @@
   1.239   *  \param rects A pointer to an array of destination rectangles.
   1.240   *  \param count The number of rectangles.
   1.241   *  
   1.242 - *  \return 0 on success, or -1 if there is no rendering context current.
   1.243 + *  \return 0 on success, or -1 on error
   1.244   */
   1.245 -extern DECLSPEC int SDLCALL SDL_RenderFillRects(const SDL_Rect ** rect, int count);
   1.246 +extern DECLSPEC int SDLCALL SDL_RenderFillRects(SDL_Renderer * renderer,
   1.247 +                                                const SDL_Rect ** rect,
   1.248 +                                                int count);
   1.249  
   1.250  /**
   1.251   *  \brief Copy a portion of the texture to the current rendering target.
   1.252 @@ -560,10 +569,10 @@
   1.253   *  \param dstrect   A pointer to the destination rectangle, or NULL for the 
   1.254   *                   entire rendering target.
   1.255   *  
   1.256 - *  \return 0 on success, or -1 if there is no rendering context current, or the
   1.257 - *          driver doesn't support the requested operation.
   1.258 + *  \return 0 on success, or -1 on error
   1.259   */
   1.260 -extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Texture * texture,
   1.261 +extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer,
   1.262 +                                           SDL_Texture * texture,
   1.263                                             const SDL_Rect * srcrect,
   1.264                                             const SDL_Rect * dstrect);
   1.265  
   1.266 @@ -581,7 +590,8 @@
   1.267   *  
   1.268   *  \warning This is a very slow operation, and should not be used frequently.
   1.269   */
   1.270 -extern DECLSPEC int SDLCALL SDL_RenderReadPixels(const SDL_Rect * rect,
   1.271 +extern DECLSPEC int SDLCALL SDL_RenderReadPixels(SDL_Renderer * renderer,
   1.272 +                                                 const SDL_Rect * rect,
   1.273                                                   Uint32 format,
   1.274                                                   void *pixels, int pitch);
   1.275  
   1.276 @@ -599,7 +609,8 @@
   1.277   *  
   1.278   *  \warning This is a very slow operation, and should not be used frequently.
   1.279   */
   1.280 -extern DECLSPEC int SDLCALL SDL_RenderWritePixels(const SDL_Rect * rect,
   1.281 +extern DECLSPEC int SDLCALL SDL_RenderWritePixels(SDL_Renderer * renderer,
   1.282 +                                                  const SDL_Rect * rect,
   1.283                                                    Uint32 format,
   1.284                                                    const void *pixels,
   1.285                                                    int pitch);
   1.286 @@ -607,7 +618,7 @@
   1.287  /**
   1.288   *  \brief Update the screen with rendering performed.
   1.289   */
   1.290 -extern DECLSPEC void SDLCALL SDL_RenderPresent(void);
   1.291 +extern DECLSPEC void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer);
   1.292  
   1.293  /**
   1.294   *  \brief Destroy the specified texture.
   1.295 @@ -623,7 +634,7 @@
   1.296   *  
   1.297   *  \sa SDL_CreateRenderer()
   1.298   */
   1.299 -extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Window * window);
   1.300 +extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Renderer * renderer);
   1.301  
   1.302  
   1.303  /* Ends C function definitions when using C++ */
     2.1 --- a/src/SDL_compat.c	Tue Feb 01 19:15:42 2011 -0800
     2.2 +++ b/src/SDL_compat.c	Tue Feb 01 19:19:43 2011 -0800
     2.3 @@ -31,7 +31,7 @@
     2.4  #include "video/SDL_yuv_sw_c.h"
     2.5  
     2.6  static SDL_Window *SDL_VideoWindow = NULL;
     2.7 -static SDL_RendererInfo SDL_VideoRendererInfo;
     2.8 +static SDL_Renderer *SDL_VideoRenderer = NULL;
     2.9  static SDL_Texture *SDL_VideoTexture = NULL;
    2.10  static SDL_Surface *SDL_VideoSurface = NULL;
    2.11  static SDL_Surface *SDL_ShadowSurface = NULL;
    2.12 @@ -467,7 +467,8 @@
    2.13      /* Destroy the screen texture and recreate it */
    2.14      SDL_QueryTexture(SDL_VideoTexture, &format, &access, &w, &h);
    2.15      SDL_DestroyTexture(SDL_VideoTexture);
    2.16 -    SDL_VideoTexture = SDL_CreateTexture(format, access, width, height);
    2.17 +    SDL_VideoTexture = SDL_CreateTexture(SDL_VideoRenderer, format,
    2.18 +                                         access, width, height);
    2.19      if (!SDL_VideoTexture) {
    2.20          return -1;
    2.21      }
    2.22 @@ -667,20 +668,20 @@
    2.23      }
    2.24  
    2.25      /* Create a renderer for the window */
    2.26 -    if (SDL_CreateRenderer(SDL_VideoWindow, -1, 0) < 0) {
    2.27 +    SDL_VideoRenderer = SDL_CreateRenderer(SDL_VideoWindow, -1, 0);
    2.28 +    if (!SDL_VideoRenderer) {
    2.29          return NULL;
    2.30      }
    2.31 -    SDL_GetRendererInfo(&SDL_VideoRendererInfo);
    2.32  
    2.33      /* Create a texture for the screen surface */
    2.34 -    SDL_VideoTexture =
    2.35 -        SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_STREAMING, width,
    2.36 -                          height);
    2.37 +    SDL_VideoTexture = SDL_CreateTexture(SDL_VideoRenderer, desired_format,
    2.38 +                                         SDL_TEXTUREACCESS_STREAMING,
    2.39 +                                         width, height);
    2.40  
    2.41      if (!SDL_VideoTexture) {
    2.42 -        SDL_VideoTexture =
    2.43 -            SDL_CreateTexture(desktop_format,
    2.44 -                              SDL_TEXTUREACCESS_STREAMING, width, height);
    2.45 +        SDL_VideoTexture = SDL_CreateTexture(SDL_VideoRenderer, desktop_format,
    2.46 +                                             SDL_TEXTUREACCESS_STREAMING,
    2.47 +                                             width, height);
    2.48      }
    2.49      if (!SDL_VideoTexture) {
    2.50          return NULL;
    2.51 @@ -890,8 +891,8 @@
    2.52          rect.y = 0;
    2.53          rect.w = screen->w;
    2.54          rect.h = screen->h;
    2.55 -        SDL_RenderCopy(SDL_VideoTexture, &rect, &rect);
    2.56 -        SDL_RenderPresent();
    2.57 +        SDL_RenderCopy(SDL_VideoRenderer, SDL_VideoTexture, &rect, &rect);
    2.58 +        SDL_RenderPresent(SDL_VideoRenderer);
    2.59      }
    2.60  }
    2.61  
    2.62 @@ -1584,7 +1585,8 @@
    2.63      }
    2.64  
    2.65      overlay->hwdata->texture =
    2.66 -        SDL_CreateTexture(texture_format, SDL_TEXTUREACCESS_STREAMING, w, h);
    2.67 +        SDL_CreateTexture(SDL_VideoRenderer, texture_format,
    2.68 +                          SDL_TEXTUREACCESS_STREAMING, w, h);
    2.69      if (overlay->hwdata->texture) {
    2.70          overlay->hwdata->sw = NULL;
    2.71      } else {
    2.72 @@ -1600,7 +1602,7 @@
    2.73          SDL_GetCurrentDisplayMode(&current_mode);
    2.74          texture_format = current_mode.format;
    2.75          overlay->hwdata->texture =
    2.76 -            SDL_CreateTexture(texture_format,
    2.77 +            SDL_CreateTexture(SDL_VideoRenderer, texture_format,
    2.78                                SDL_TEXTUREACCESS_STREAMING, w, h);
    2.79      }
    2.80      if (!overlay->hwdata->texture) {
    2.81 @@ -1688,10 +1690,10 @@
    2.82          SDL_SetError("Passed a NULL overlay or dstrect");
    2.83          return -1;
    2.84      }
    2.85 -    if (SDL_RenderCopy(overlay->hwdata->texture, NULL, dstrect) < 0) {
    2.86 +    if (SDL_RenderCopy(SDL_VideoRenderer, overlay->hwdata->texture, NULL, dstrect) < 0) {
    2.87          return -1;
    2.88      }
    2.89 -    SDL_RenderPresent();
    2.90 +    SDL_RenderPresent(SDL_VideoRenderer);
    2.91      return 0;
    2.92  }
    2.93  
     3.1 --- a/src/events/SDL_windowevents.c	Tue Feb 01 19:15:42 2011 -0800
     3.2 +++ b/src/events/SDL_windowevents.c	Tue Feb 01 19:19:43 2011 -0800
     3.3 @@ -106,7 +106,6 @@
     3.4          }
     3.5          window->w = data1;
     3.6          window->h = data2;
     3.7 -        SDL_OnWindowResized(window);
     3.8          break;
     3.9      case SDL_WINDOWEVENT_MINIMIZED:
    3.10          if (window->flags & SDL_WINDOW_MINIMIZED) {
     4.1 --- a/src/video/SDL_renderer_gl.c	Tue Feb 01 19:15:42 2011 -0800
     4.2 +++ b/src/video/SDL_renderer_gl.c	Tue Feb 01 19:19:43 2011 -0800
     4.3 @@ -66,8 +66,8 @@
     4.4  static const float inv255f = 1.0f / 255.0f;
     4.5  
     4.6  static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
     4.7 -static int GL_ActivateRenderer(SDL_Renderer * renderer);
     4.8 -static int GL_DisplayModeChanged(SDL_Renderer * renderer);
     4.9 +static void GL_WindowEvent(SDL_Renderer * renderer,
    4.10 +                           const SDL_WindowEvent *event);
    4.11  static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    4.12  static int GL_QueryTexturePixels(SDL_Renderer * renderer,
    4.13                                   SDL_Texture * texture, void **pixels,
    4.14 @@ -277,8 +277,7 @@
    4.15          return NULL;
    4.16      }
    4.17  
    4.18 -    renderer->ActivateRenderer = GL_ActivateRenderer;
    4.19 -    renderer->DisplayModeChanged = GL_DisplayModeChanged;
    4.20 +    renderer->WindowEvent = GL_WindowEvent;
    4.21      renderer->CreateTexture = GL_CreateTexture;
    4.22      renderer->QueryTexturePixels = GL_QueryTexturePixels;
    4.23      renderer->SetTexturePalette = GL_SetTexturePalette;
    4.24 @@ -408,14 +407,19 @@
    4.25      return renderer;
    4.26  }
    4.27  
    4.28 +static SDL_GLContext SDL_CurrentContext = NULL;
    4.29 +
    4.30  static int
    4.31  GL_ActivateRenderer(SDL_Renderer * renderer)
    4.32  {
    4.33      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    4.34      SDL_Window *window = renderer->window;
    4.35  
    4.36 -    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
    4.37 -        return -1;
    4.38 +    if (SDL_CurrentContext != data->context) {
    4.39 +        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
    4.40 +            return -1;
    4.41 +        }
    4.42 +        SDL_CurrentContext = data->context;
    4.43      }
    4.44      if (data->updateSize) {
    4.45          data->glMatrixMode(GL_PROJECTION);
    4.46 @@ -430,14 +434,16 @@
    4.47      return 0;
    4.48  }
    4.49  
    4.50 -static int
    4.51 -GL_DisplayModeChanged(SDL_Renderer * renderer)
    4.52 +static void
    4.53 +GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
    4.54  {
    4.55      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    4.56  
    4.57 -    /* Rebind the context to the window area and update matrices */
    4.58 -    data->updateSize = SDL_TRUE;
    4.59 -    return GL_ActivateRenderer(renderer);
    4.60 +    if (event->event == SDL_WINDOWEVENT_RESIZED) {
    4.61 +        /* Rebind the context to the window area and update matrices */
    4.62 +        SDL_CurrentContext = NULL;
    4.63 +        data->updateSize = SDL_TRUE;
    4.64 +    }
    4.65  }
    4.66  
    4.67  static __inline__ int
    4.68 @@ -717,6 +723,8 @@
    4.69      GLuint shader = 0;
    4.70      GLenum result;
    4.71  
    4.72 +    GL_ActivateRenderer(renderer);
    4.73 +
    4.74      if (!convert_format(renderdata, texture->format, &internalFormat,
    4.75                          &format, &type)) {
    4.76          SDL_SetError("Texture format %s not supported by OpenGL",
    4.77 @@ -874,6 +882,8 @@
    4.78      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
    4.79      Uint8 *palette;
    4.80  
    4.81 +    GL_ActivateRenderer(renderer);
    4.82 +
    4.83      if (!data->palette) {
    4.84          SDL_SetError("Texture doesn't have a palette");
    4.85          return -1;
    4.86 @@ -938,6 +948,8 @@
    4.87      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
    4.88      GLenum result;
    4.89  
    4.90 +    GL_ActivateRenderer(renderer);
    4.91 +
    4.92      renderdata->glGetError();
    4.93      SetupTextureUpdate(renderdata, texture, pitch);
    4.94      renderdata->glEnable(data->type);
    4.95 @@ -1018,6 +1030,8 @@
    4.96  {
    4.97      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    4.98  
    4.99 +    GL_ActivateRenderer(renderer);
   4.100 +
   4.101      data->glClearColor((GLfloat) renderer->r * inv255f,
   4.102                         (GLfloat) renderer->g * inv255f,
   4.103                         (GLfloat) renderer->b * inv255f,
   4.104 @@ -1035,6 +1049,8 @@
   4.105      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.106      int i;
   4.107  
   4.108 +    GL_ActivateRenderer(renderer);
   4.109 +
   4.110      GL_SetBlendMode(data, renderer->blendMode);
   4.111  
   4.112      data->glColor4f((GLfloat) renderer->r * inv255f,
   4.113 @@ -1058,6 +1074,8 @@
   4.114      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.115      int i;
   4.116  
   4.117 +    GL_ActivateRenderer(renderer);
   4.118 +
   4.119      GL_SetBlendMode(data, renderer->blendMode);
   4.120  
   4.121      data->glColor4f((GLfloat) renderer->r * inv255f,
   4.122 @@ -1126,6 +1144,8 @@
   4.123      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.124      int i, x, y;
   4.125  
   4.126 +    GL_ActivateRenderer(renderer);
   4.127 +
   4.128      GL_SetBlendMode(data, renderer->blendMode);
   4.129  
   4.130      data->glColor4f((GLfloat) renderer->r * inv255f,
   4.131 @@ -1164,6 +1184,8 @@
   4.132      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   4.133      int i;
   4.134  
   4.135 +    GL_ActivateRenderer(renderer);
   4.136 +
   4.137      GL_SetBlendMode(data, renderer->blendMode);
   4.138  
   4.139      data->glColor4f((GLfloat) renderer->r * inv255f,
   4.140 @@ -1189,6 +1211,8 @@
   4.141      int minx, miny, maxx, maxy;
   4.142      GLfloat minu, maxu, minv, maxv;
   4.143  
   4.144 +    GL_ActivateRenderer(renderer);
   4.145 +
   4.146      if (texturedata->dirty.list) {
   4.147          SDL_DirtyRect *dirty;
   4.148          void *pixels;
   4.149 @@ -1276,6 +1300,8 @@
   4.150      Uint8 *src, *dst, *tmp;
   4.151      int length, rows;
   4.152  
   4.153 +    GL_ActivateRenderer(renderer);
   4.154 +
   4.155      if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
   4.156          /* FIXME: Do a temp copy to a format that is supported */
   4.157          SDL_SetError("Unsupported pixel format");
   4.158 @@ -1323,6 +1349,8 @@
   4.159      Uint8 *src, *dst, *tmp;
   4.160      int length, rows;
   4.161  
   4.162 +    GL_ActivateRenderer(renderer);
   4.163 +
   4.164      if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
   4.165          /* FIXME: Do a temp copy to a format that is supported */
   4.166          SDL_SetError("Unsupported pixel format");
   4.167 @@ -1360,6 +1388,8 @@
   4.168  static void
   4.169  GL_RenderPresent(SDL_Renderer * renderer)
   4.170  {
   4.171 +    GL_ActivateRenderer(renderer);
   4.172 +
   4.173      SDL_GL_SwapWindow(renderer->window);
   4.174  }
   4.175  
   4.176 @@ -1369,6 +1399,8 @@
   4.177      GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   4.178      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   4.179  
   4.180 +    GL_ActivateRenderer(renderer);
   4.181 +
   4.182      if (!data) {
   4.183          return;
   4.184      }
     5.1 --- a/src/video/SDL_renderer_gles.c	Tue Feb 01 19:15:42 2011 -0800
     5.2 +++ b/src/video/SDL_renderer_gles.c	Tue Feb 01 19:19:43 2011 -0800
     5.3 @@ -54,8 +54,8 @@
     5.4  static const float inv255f = 1.0f / 255.0f;
     5.5  
     5.6  static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
     5.7 -static int GLES_ActivateRenderer(SDL_Renderer * renderer);
     5.8 -static int GLES_DisplayModeChanged(SDL_Renderer * renderer);
     5.9 +static void GLES_WindowEvent(SDL_Renderer * renderer,
    5.10 +                             const SDL_WindowEvent *event);
    5.11  static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    5.12  static int GLES_QueryTexturePixels(SDL_Renderer * renderer,
    5.13                                     SDL_Texture * texture, void **pixels,
    5.14 @@ -218,8 +218,7 @@
    5.15          return NULL;
    5.16      }
    5.17  
    5.18 -    renderer->ActivateRenderer = GLES_ActivateRenderer;
    5.19 -    renderer->DisplayModeChanged = GLES_DisplayModeChanged;
    5.20 +    renderer->WindowEvent = GLES_WindowEvent;
    5.21      renderer->CreateTexture = GLES_CreateTexture;
    5.22      renderer->QueryTexturePixels = GLES_QueryTexturePixels;
    5.23      renderer->SetTexturePalette = GLES_SetTexturePalette;
    5.24 @@ -311,6 +310,8 @@
    5.25      return renderer;
    5.26  }
    5.27  
    5.28 +static SDL_GLContext SDL_CurrentContext = NULL;
    5.29 +
    5.30  static int
    5.31  GLES_ActivateRenderer(SDL_Renderer * renderer)
    5.32  {
    5.33 @@ -318,8 +319,11 @@
    5.34      GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
    5.35      SDL_Window *window = renderer->window;
    5.36  
    5.37 -    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
    5.38 -        return -1;
    5.39 +    if (SDL_CurrentContext != data->context) {
    5.40 +        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
    5.41 +            return -1;
    5.42 +        }
    5.43 +        SDL_CurrentContext = data->context;
    5.44      }
    5.45      if (data->updateSize) {
    5.46          data->glMatrixMode(GL_PROJECTION);
    5.47 @@ -334,13 +338,16 @@
    5.48      return 0;
    5.49  }
    5.50  
    5.51 -static int
    5.52 -GLES_DisplayModeChanged(SDL_Renderer * renderer)
    5.53 +static void
    5.54 +GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
    5.55  {
    5.56      GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
    5.57  
    5.58 -    data->updateSize = SDL_TRUE;
    5.59 -    return 0;
    5.60 +    if (event->event == SDL_WINDOWEVENT_RESIZED) {
    5.61 +        /* Rebind the context to the window area and update matrices */
    5.62 +        SDL_CurrentContext = NULL;
    5.63 +        data->updateSize = SDL_TRUE;
    5.64 +    }
    5.65  }
    5.66  
    5.67  static __inline__ int
    5.68 @@ -364,6 +371,8 @@
    5.69      int texture_w, texture_h;
    5.70      GLenum result;
    5.71  
    5.72 +    GLES_ActivateRenderer(renderer);
    5.73 +
    5.74      switch (texture->format) {
    5.75      case SDL_PIXELFORMAT_RGB24:
    5.76          internalFormat = GL_RGB;
    5.77 @@ -498,6 +507,8 @@
    5.78      void * temp_ptr;
    5.79      int i;
    5.80  
    5.81 +    GLES_ActivateRenderer(renderer);
    5.82 +
    5.83      renderdata->glGetError();
    5.84      renderdata->glEnable(data->type);
    5.85      SetupTextureUpdate(renderdata, texture, pitch);
    5.86 @@ -599,6 +610,8 @@
    5.87      int i;
    5.88      GLshort *vertices;
    5.89  
    5.90 +    GLES_ActivateRenderer(renderer);
    5.91 +
    5.92      GLES_SetBlendMode(data, renderer->blendMode);
    5.93  
    5.94      data->glColor4f((GLfloat) renderer->r * inv255f,
    5.95 @@ -626,6 +639,8 @@
    5.96      int i;
    5.97      GLshort *vertices;
    5.98  
    5.99 +    GLES_ActivateRenderer(renderer);
   5.100 +
   5.101      GLES_SetBlendMode(data, renderer->blendMode);
   5.102  
   5.103      data->glColor4f((GLfloat) renderer->r * inv255f,
   5.104 @@ -659,6 +674,8 @@
   5.105      GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   5.106      int i;
   5.107  
   5.108 +    GLES_ActivateRenderer(renderer);
   5.109 +
   5.110      GLES_SetBlendMode(data, renderer->blendMode);
   5.111  
   5.112      data->glColor4f((GLfloat) renderer->r * inv255f,
   5.113 @@ -696,6 +713,8 @@
   5.114      GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   5.115      int i;
   5.116  
   5.117 +    GLES_ActivateRenderer(renderer);
   5.118 +
   5.119      GLES_SetBlendMode(data, renderer->blendMode);
   5.120  
   5.121      data->glColor4f((GLfloat) renderer->r * inv255f,
   5.122 @@ -739,6 +758,8 @@
   5.123      void *temp_buffer;          /* used for reformatting dirty rect pixels */
   5.124      void *temp_ptr;
   5.125  
   5.126 +    GLES_ActivateRenderer(renderer);
   5.127 +
   5.128      data->glEnable(GL_TEXTURE_2D);
   5.129  
   5.130      if (texturedata->dirty.list) {
   5.131 @@ -859,6 +880,8 @@
   5.132  static void
   5.133  GLES_RenderPresent(SDL_Renderer * renderer)
   5.134  {
   5.135 +    GLES_ActivateRenderer(renderer);
   5.136 +
   5.137      SDL_GL_SwapWindow(renderer->window);
   5.138  }
   5.139  
   5.140 @@ -867,6 +890,8 @@
   5.141  {
   5.142      GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   5.143  
   5.144 +    GLES_ActivateRenderer(renderer);
   5.145 +
   5.146      if (!data) {
   5.147          return;
   5.148      }
     6.1 --- a/src/video/SDL_renderer_sw.c	Tue Feb 01 19:15:42 2011 -0800
     6.2 +++ b/src/video/SDL_renderer_sw.c	Tue Feb 01 19:19:43 2011 -0800
     6.3 @@ -32,8 +32,8 @@
     6.4  /* SDL surface based renderer implementation */
     6.5  
     6.6  static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
     6.7 -static int SW_ActivateRenderer(SDL_Renderer * renderer);
     6.8 -static int SW_DisplayModeChanged(SDL_Renderer * renderer);
     6.9 +static void SW_WindowEvent(SDL_Renderer * renderer,
    6.10 +                           const SDL_WindowEvent *event);
    6.11  static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    6.12  static int SW_QueryTexturePixels(SDL_Renderer * renderer,
    6.13                                   SDL_Texture * texture, void **pixels,
    6.14 @@ -212,8 +212,7 @@
    6.15          SDL_OutOfMemory();
    6.16          return NULL;
    6.17      }
    6.18 -    renderer->ActivateRenderer = SW_ActivateRenderer;
    6.19 -    renderer->DisplayModeChanged = SW_DisplayModeChanged;
    6.20 +    renderer->WindowEvent = SW_WindowEvent;
    6.21  
    6.22      renderer->RenderDrawPoints = SW_RenderDrawPoints;
    6.23      renderer->RenderDrawLines = SW_RenderDrawLines;
    6.24 @@ -287,47 +286,34 @@
    6.25      return renderer;
    6.26  }
    6.27  
    6.28 -static int
    6.29 +static SDL_Texture *
    6.30  SW_ActivateRenderer(SDL_Renderer * renderer)
    6.31  {
    6.32      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
    6.33      SDL_Window *window = renderer->window;
    6.34 -    int i, n;
    6.35  
    6.36 -    if (data->renderer && data->renderer->ActivateRenderer) {
    6.37 -        if (data->renderer->ActivateRenderer(data->renderer) < 0) {
    6.38 -            return -1;
    6.39 -        }
    6.40 -    }
    6.41      if (data->updateSize) {
    6.42          /* Recreate the textures for the new window size */
    6.43          if (data->texture) {
    6.44              DestroyTexture(data->renderer, data->texture);
    6.45 -            data->texture = 0;
    6.46          }
    6.47          data->texture = CreateTexture(data->renderer, data->format,
    6.48                                        window->w, window->h);
    6.49 -        if (!data->texture) {
    6.50 -            return -1;
    6.51 +        if (data->texture) {
    6.52 +            data->updateSize = SDL_FALSE;
    6.53          }
    6.54 -        data->updateSize = SDL_FALSE;
    6.55      }
    6.56 -    return 0;
    6.57 +    return data->texture;
    6.58  }
    6.59  
    6.60 -static int
    6.61 -SW_DisplayModeChanged(SDL_Renderer * renderer)
    6.62 +static void
    6.63 +SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
    6.64  {
    6.65      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
    6.66  
    6.67 -    if (data->renderer && data->renderer->DisplayModeChanged) {
    6.68 -        if (data->renderer->DisplayModeChanged(data->renderer) < 0) {
    6.69 -            return -1;
    6.70 -        }
    6.71 +    if (event->event == SDL_WINDOWEVENT_RESIZED) {
    6.72 +        data->updateSize = SDL_TRUE;
    6.73      }
    6.74 -    /* Rebind the context to the window area */
    6.75 -    data->updateSize = SDL_TRUE;
    6.76 -    return SW_ActivateRenderer(renderer);
    6.77  }
    6.78  
    6.79  static int
    6.80 @@ -496,12 +482,16 @@
    6.81                      int count)
    6.82  {
    6.83      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
    6.84 -    SDL_Texture *texture = data->texture;
    6.85 +    SDL_Texture *texture = SW_ActivateRenderer(renderer);
    6.86      SDL_Rect rect;
    6.87      int i;
    6.88      int x, y;
    6.89      int status = 0;
    6.90  
    6.91 +    if (!texture) {
    6.92 +        return -1;
    6.93 +    }
    6.94 +
    6.95      /* Get the smallest rectangle that contains everything */
    6.96      rect.x = 0;
    6.97      rect.y = 0;
    6.98 @@ -555,12 +545,16 @@
    6.99                     int count)
   6.100  {
   6.101      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   6.102 -    SDL_Texture *texture = data->texture;
   6.103 +    SDL_Texture *texture = SW_ActivateRenderer(renderer);
   6.104      SDL_Rect clip, rect;
   6.105      int i;
   6.106      int x1, y1, x2, y2;
   6.107      int status = 0;
   6.108  
   6.109 +    if (!texture) {
   6.110 +        return -1;
   6.111 +    }
   6.112 +
   6.113      /* Get the smallest rectangle that contains everything */
   6.114      clip.x = 0;
   6.115      clip.y = 0;
   6.116 @@ -619,12 +613,16 @@
   6.117                     int count)
   6.118  {
   6.119      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   6.120 -    SDL_Texture *texture = data->texture;
   6.121 +    SDL_Texture *texture = SW_ActivateRenderer(renderer);
   6.122      SDL_Rect clip, rect;
   6.123      Uint32 color = 0;
   6.124      int i;
   6.125      int status = 0;
   6.126  
   6.127 +    if (!texture) {
   6.128 +        return -1;
   6.129 +    }
   6.130 +
   6.131      clip.x = 0;
   6.132      clip.y = 0;
   6.133      clip.w = texture->w;
   6.134 @@ -671,12 +669,16 @@
   6.135                     int count)
   6.136  {
   6.137      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   6.138 -    SDL_Texture *texture = data->texture;
   6.139 +    SDL_Texture *texture = SW_ActivateRenderer(renderer);
   6.140      SDL_Rect clip, rect;
   6.141      Uint32 color = 0;
   6.142      int i;
   6.143      int status = 0;
   6.144  
   6.145 +    if (!texture) {
   6.146 +        return -1;
   6.147 +    }
   6.148 +
   6.149      clip.x = 0;
   6.150      clip.y = 0;
   6.151      clip.w = texture->w;
   6.152 @@ -724,6 +726,10 @@
   6.153      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   6.154      int status;
   6.155  
   6.156 +    if (!SW_ActivateRenderer(renderer)) {
   6.157 +        return -1;
   6.158 +    }
   6.159 +
   6.160      if (data->renderer->LockTexture(data->renderer, data->texture,
   6.161                                      dstrect, 1, &data->surface.pixels,
   6.162                                      &data->surface.pitch) < 0) {
   6.163 @@ -760,6 +766,10 @@
   6.164  {
   6.165      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   6.166  
   6.167 +    if (!SW_ActivateRenderer(renderer)) {
   6.168 +        return -1;
   6.169 +    }
   6.170 +
   6.171      if (data->renderer->LockTexture(data->renderer, data->texture,
   6.172                                      rect, 0, &data->surface.pixels,
   6.173                                      &data->surface.pitch) < 0) {
   6.174 @@ -780,6 +790,10 @@
   6.175  {
   6.176      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   6.177  
   6.178 +    if (!SW_ActivateRenderer(renderer)) {
   6.179 +        return -1;
   6.180 +    }
   6.181 +
   6.182      if (data->renderer->LockTexture(data->renderer, data->texture,
   6.183                                      rect, 1, &data->surface.pixels,
   6.184                                      &data->surface.pitch) < 0) {
   6.185 @@ -797,9 +811,13 @@
   6.186  SW_RenderPresent(SDL_Renderer * renderer)
   6.187  {
   6.188      SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
   6.189 -    SDL_Texture *texture = data->texture;
   6.190 +    SDL_Texture *texture = SW_ActivateRenderer(renderer);
   6.191      SDL_Rect rect;
   6.192  
   6.193 +    if (!texture) {
   6.194 +        return;
   6.195 +    }
   6.196 +
   6.197      /* Send the data to the display */
   6.198      rect.x = 0;
   6.199      rect.y = 0;
     7.1 --- a/src/video/SDL_sysvideo.h	Tue Feb 01 19:15:42 2011 -0800
     7.2 +++ b/src/video/SDL_sysvideo.h	Tue Feb 01 19:19:43 2011 -0800
     7.3 @@ -24,6 +24,7 @@
     7.4  #ifndef _SDL_sysvideo_h
     7.5  #define _SDL_sysvideo_h
     7.6  
     7.7 +#include "SDL_events.h"
     7.8  #include "SDL_mouse.h"
     7.9  #include "SDL_keysym.h"
    7.10  #include "SDL_render.h"
    7.11 @@ -31,7 +32,6 @@
    7.12  
    7.13  /* The SDL video driver */
    7.14  
    7.15 -typedef struct SDL_Renderer SDL_Renderer;
    7.16  typedef struct SDL_RenderDriver SDL_RenderDriver;
    7.17  typedef struct SDL_WindowShaper SDL_WindowShaper;
    7.18  typedef struct SDL_ShapeDriver SDL_ShapeDriver;
    7.19 @@ -61,8 +61,9 @@
    7.20  /* Define the SDL renderer structure */
    7.21  struct SDL_Renderer
    7.22  {
    7.23 -    int (*ActivateRenderer) (SDL_Renderer * renderer);
    7.24 -    int (*DisplayModeChanged) (SDL_Renderer * renderer);
    7.25 +    const void *magic;
    7.26 +
    7.27 +    void (*WindowEvent) (SDL_Renderer * renderer, const SDL_WindowEvent *event);
    7.28      int (*CreateTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
    7.29      int (*QueryTexturePixels) (SDL_Renderer * renderer, SDL_Texture * texture,
    7.30                                 void **pixels, int *pitch);
    7.31 @@ -168,7 +169,6 @@
    7.32      Uint32 flags;
    7.33  
    7.34      SDL_VideoDisplay *display;
    7.35 -    SDL_Renderer *renderer;
    7.36  
    7.37      SDL_DisplayMode fullscreen_mode;
    7.38      
    7.39 @@ -208,8 +208,6 @@
    7.40      SDL_Window *windows;
    7.41      SDL_Window *fullscreen_window;
    7.42  
    7.43 -    SDL_Renderer *current_renderer;
    7.44 -
    7.45      SDL_VideoDevice *device;
    7.46  
    7.47      void *driverdata;
    7.48 @@ -344,6 +342,7 @@
    7.49      SDL_VideoDisplay *displays;
    7.50      int current_display;
    7.51      Uint8 window_magic;
    7.52 +    Uint8 renderer_magic;
    7.53      Uint8 texture_magic;
    7.54      Uint32 next_object_id;
    7.55      char * clipboard_text;
    7.56 @@ -439,7 +438,6 @@
    7.57  #endif
    7.58  
    7.59  #define SDL_CurrentDisplay	(&_this->displays[_this->current_display])
    7.60 -#define SDL_CurrentRenderer	(SDL_CurrentDisplay->current_renderer)
    7.61  
    7.62  extern SDL_VideoDevice *SDL_GetVideoDevice(void);
    7.63  extern int SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode);
    7.64 @@ -461,7 +459,6 @@
    7.65  
    7.66  extern void SDL_OnWindowShown(SDL_Window * window);
    7.67  extern void SDL_OnWindowHidden(SDL_Window * window);
    7.68 -extern void SDL_OnWindowResized(SDL_Window * window);
    7.69  extern void SDL_OnWindowMinimized(SDL_Window * window);
    7.70  extern void SDL_OnWindowRestored(SDL_Window * window);
    7.71  extern void SDL_OnWindowFocusGained(SDL_Window * window);
     8.1 --- a/src/video/SDL_video.c	Tue Feb 01 19:15:42 2011 -0800
     8.2 +++ b/src/video/SDL_video.c	Tue Feb 01 19:19:43 2011 -0800
     8.3 @@ -109,6 +109,12 @@
     8.4          return retval; \
     8.5      }
     8.6  
     8.7 +#define CHECK_RENDERER_MAGIC(renderer, retval) \
     8.8 +    if (!renderer || renderer->magic != &_this->renderer_magic) { \
     8.9 +        SDL_SetError("Invalid renderer"); \
    8.10 +        return retval; \
    8.11 +    }
    8.12 +
    8.13  #define CHECK_TEXTURE_MAGIC(texture, retval) \
    8.14      if (!_this) { \
    8.15          SDL_UninitializedVideo(); \
    8.16 @@ -1023,33 +1029,6 @@
    8.17      return 0;
    8.18  }
    8.19  
    8.20 -static __inline__ SDL_Renderer *
    8.21 -SDL_GetCurrentRenderer(SDL_bool create)
    8.22 -{
    8.23 -    if (!_this) {
    8.24 -        SDL_UninitializedVideo();
    8.25 -        return NULL;
    8.26 -    }
    8.27 -    if (!SDL_CurrentRenderer) {
    8.28 -        SDL_Window *window = NULL;
    8.29 -
    8.30 -        if (!create) {
    8.31 -            SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
    8.32 -            return NULL;
    8.33 -        }
    8.34 -
    8.35 -        /* Get the first window on the first display */
    8.36 -        if (_this->num_displays > 0) {
    8.37 -            window = _this->displays[0].windows;
    8.38 -        }
    8.39 -
    8.40 -        if (SDL_CreateRenderer(window, -1, 0) < 0) {
    8.41 -            return NULL;
    8.42 -        }
    8.43 -    }
    8.44 -    return SDL_CurrentRenderer;
    8.45 -}
    8.46 -
    8.47  Uint32
    8.48  SDL_GetWindowID(SDL_Window * window)
    8.49  {
    8.50 @@ -1184,7 +1163,7 @@
    8.51      if (_this->SetWindowSize) {
    8.52          _this->SetWindowSize(_this, window);
    8.53      }
    8.54 -    SDL_OnWindowResized(window);
    8.55 +    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
    8.56  }
    8.57  
    8.58  void
    8.59 @@ -1367,16 +1346,6 @@
    8.60  }
    8.61  
    8.62  void
    8.63 -SDL_OnWindowResized(SDL_Window * window)
    8.64 -{
    8.65 -    SDL_Renderer *renderer = window->renderer;
    8.66 -
    8.67 -    if (renderer && renderer->DisplayModeChanged) {
    8.68 -        renderer->DisplayModeChanged(renderer);
    8.69 -    }
    8.70 -}
    8.71 -
    8.72 -void
    8.73  SDL_OnWindowMinimized(SDL_Window * window)
    8.74  {
    8.75      SDL_UpdateFullscreenMode(window, SDL_FALSE);
    8.76 @@ -1451,9 +1420,6 @@
    8.77      if (window->title) {
    8.78          SDL_free(window->title);
    8.79      }
    8.80 -    if (window->renderer) {
    8.81 -        SDL_DestroyRenderer(window);
    8.82 -    }
    8.83  
    8.84      /* Restore video mode, etc. */
    8.85      SDL_UpdateFullscreenMode(window, SDL_FALSE);
    8.86 @@ -1523,13 +1489,26 @@
    8.87      return 0;
    8.88  }
    8.89  
    8.90 -int
    8.91 +static int
    8.92 +SDL_RendererEventWatch(void *userdata, SDL_Event *event)
    8.93 +{
    8.94 +    SDL_Renderer *renderer = (SDL_Renderer *)userdata;
    8.95 +
    8.96 +    if (event->type == SDL_WINDOWEVENT && renderer->WindowEvent) {
    8.97 +        SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
    8.98 +        if (window == renderer->window) {
    8.99 +            renderer->WindowEvent(renderer, &event->window);
   8.100 +        }
   8.101 +    }
   8.102 +    return 0;
   8.103 +}
   8.104 +
   8.105 +SDL_Renderer *
   8.106  SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
   8.107  {
   8.108 -    CHECK_WINDOW_MAGIC(window, -1);
   8.109 -
   8.110 -    /* Free any existing renderer */
   8.111 -    SDL_DestroyRenderer(window);
   8.112 +    SDL_Renderer *renderer = NULL;
   8.113 +
   8.114 +    CHECK_WINDOW_MAGIC(window, NULL);
   8.115  
   8.116      if (index < 0) {
   8.117          char *override = SDL_getenv("SDL_VIDEO_RENDERER");
   8.118 @@ -1552,7 +1531,7 @@
   8.119  
   8.120                  if (SDL_strcasecmp(override, driver->info.name) == 0) {
   8.121                      /* Create a new renderer instance */
   8.122 -                    window->renderer = driver->CreateRenderer(window, flags);
   8.123 +                    renderer = driver->CreateRenderer(window, flags);
   8.124                      break;
   8.125                  }
   8.126              }
   8.127 @@ -1563,8 +1542,8 @@
   8.128  
   8.129                  if ((driver->info.flags & flags) == flags) {
   8.130                      /* Create a new renderer instance */
   8.131 -                    window->renderer = driver->CreateRenderer(window, flags);
   8.132 -                    if (window->renderer) {
   8.133 +                    renderer = driver->CreateRenderer(window, flags);
   8.134 +                    if (renderer) {
   8.135                          /* Yay, we got one! */
   8.136                          break;
   8.137                      }
   8.138 @@ -1573,74 +1552,42 @@
   8.139          }
   8.140          if (index == n) {
   8.141              SDL_SetError("Couldn't find matching render driver");
   8.142 -            return -1;
   8.143 +            return NULL;
   8.144          }
   8.145      } else {
   8.146          if (index >= SDL_GetNumRenderDrivers()) {
   8.147              SDL_SetError("index must be -1 or in the range of 0 - %d",
   8.148                           SDL_GetNumRenderDrivers() - 1);
   8.149 -            return -1;
   8.150 +            return NULL;
   8.151          }
   8.152          /* Create a new renderer instance */
   8.153 -        window->renderer = SDL_CurrentDisplay->render_drivers[index].CreateRenderer(window, flags);
   8.154 +        renderer = SDL_CurrentDisplay->render_drivers[index].CreateRenderer(window, flags);
   8.155      }
   8.156  
   8.157 -    if (window->renderer == NULL) {
   8.158 -        /* Assuming renderer set its error */
   8.159 -        return -1;
   8.160 +    if (renderer) {
   8.161 +        renderer->magic = &_this->renderer_magic;
   8.162 +
   8.163 +        SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
   8.164      }
   8.165 -
   8.166 -    SDL_SelectRenderer(window);
   8.167 -
   8.168 -    return 0;
   8.169 +    return renderer;
   8.170  }
   8.171  
   8.172  int
   8.173 -SDL_SelectRenderer(SDL_Window * window)
   8.174 +SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
   8.175  {
   8.176 -    SDL_Renderer *renderer;
   8.177 -
   8.178 -    CHECK_WINDOW_MAGIC(window, -1);
   8.179 -
   8.180 -    renderer = window->renderer;
   8.181 -    if (!renderer) {
   8.182 -        SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
   8.183 -        return -1;
   8.184 -    }
   8.185 -    if (renderer->ActivateRenderer) {
   8.186 -        if (renderer->ActivateRenderer(renderer) < 0) {
   8.187 -            return -1;
   8.188 -        }
   8.189 -    }
   8.190 -    SDL_CurrentDisplay->current_renderer = renderer;
   8.191 -    return 0;
   8.192 -}
   8.193 -
   8.194 -int
   8.195 -SDL_GetRendererInfo(SDL_RendererInfo * info)
   8.196 -{
   8.197 -    SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
   8.198 -    if (!renderer) {
   8.199 -        return -1;
   8.200 -    }
   8.201 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.202 +
   8.203      *info = renderer->info;
   8.204      return 0;
   8.205  }
   8.206  
   8.207  SDL_Texture *
   8.208 -SDL_CreateTexture(Uint32 format, int access, int w, int h)
   8.209 +SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
   8.210  {
   8.211 -    SDL_Renderer *renderer;
   8.212      SDL_Texture *texture;
   8.213  
   8.214 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.215 -    if (!renderer) {
   8.216 -        return 0;
   8.217 -    }
   8.218 -    if (!renderer->CreateTexture) {
   8.219 -        SDL_Unsupported();
   8.220 -        return 0;
   8.221 -    }
   8.222 +    CHECK_RENDERER_MAGIC(renderer, NULL);
   8.223 +
   8.224      if (w <= 0 || h <= 0) {
   8.225          SDL_SetError("Texture dimensions can't be 0");
   8.226          return 0;
   8.227 @@ -1674,26 +1621,22 @@
   8.228  }
   8.229  
   8.230  SDL_Texture *
   8.231 -SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
   8.232 +SDL_CreateTextureFromSurface(SDL_Renderer * renderer, Uint32 format, SDL_Surface * surface)
   8.233  {
   8.234      SDL_Texture *texture;
   8.235      Uint32 requested_format = format;
   8.236      SDL_PixelFormat *fmt;
   8.237 -    SDL_Renderer *renderer;
   8.238      int bpp;
   8.239      Uint32 Rmask, Gmask, Bmask, Amask;
   8.240  
   8.241 +    CHECK_RENDERER_MAGIC(renderer, NULL);
   8.242 +
   8.243      if (!surface) {
   8.244          SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
   8.245 -        return 0;
   8.246 +        return NULL;
   8.247      }
   8.248      fmt = surface->format;
   8.249  
   8.250 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.251 -    if (!renderer) {
   8.252 -        return 0;
   8.253 -    }
   8.254 -
   8.255      if (format) {
   8.256          if (!SDL_PixelFormatEnumToMasks
   8.257              (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   8.258 @@ -1902,15 +1845,14 @@
   8.259      }
   8.260  
   8.261      texture =
   8.262 -        SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
   8.263 -                          surface->h);
   8.264 +        SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
   8.265 +                          surface->w, surface->h);
   8.266      if (!texture && !requested_format) {
   8.267          SDL_DisplayMode desktop_mode;
   8.268          SDL_GetDesktopDisplayMode(&desktop_mode);
   8.269          format = desktop_mode.format;
   8.270 -        texture =
   8.271 -            SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
   8.272 -                              surface->h);
   8.273 +        texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
   8.274 +                                    surface->w, surface->h);
   8.275      }
   8.276      if (!texture) {
   8.277          return 0;
   8.278 @@ -2246,14 +2188,11 @@
   8.279  }
   8.280  
   8.281  int
   8.282 -SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   8.283 +SDL_SetRenderDrawColor(SDL_Renderer * renderer,
   8.284 +                       Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   8.285  {
   8.286 -    SDL_Renderer *renderer;
   8.287 -
   8.288 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.289 -    if (!renderer) {
   8.290 -        return -1;
   8.291 -    }
   8.292 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.293 +
   8.294      renderer->r = r;
   8.295      renderer->g = g;
   8.296      renderer->b = b;
   8.297 @@ -2262,14 +2201,11 @@
   8.298  }
   8.299  
   8.300  int
   8.301 -SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
   8.302 +SDL_GetRenderDrawColor(SDL_Renderer * renderer,
   8.303 +                       Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
   8.304  {
   8.305 -    SDL_Renderer *renderer;
   8.306 -
   8.307 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.308 -    if (!renderer) {
   8.309 -        return -1;
   8.310 -    }
   8.311 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.312 +
   8.313      if (r) {
   8.314          *r = renderer->r;
   8.315      }
   8.316 @@ -2286,52 +2222,40 @@
   8.317  }
   8.318  
   8.319  int
   8.320 -SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode)
   8.321 +SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
   8.322  {
   8.323 -    SDL_Renderer *renderer;
   8.324 -
   8.325 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.326 -    if (!renderer) {
   8.327 -        return -1;
   8.328 -    }
   8.329 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.330 +
   8.331      renderer->blendMode = blendMode;
   8.332      return 0;
   8.333  }
   8.334  
   8.335  int
   8.336 -SDL_GetRenderDrawBlendMode(SDL_BlendMode *blendMode)
   8.337 +SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
   8.338  {
   8.339 -    SDL_Renderer *renderer;
   8.340 -
   8.341 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.342 -    if (!renderer) {
   8.343 -        return -1;
   8.344 -    }
   8.345 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.346 +
   8.347      *blendMode = renderer->blendMode;
   8.348      return 0;
   8.349  }
   8.350  
   8.351  int
   8.352 -SDL_RenderClear()
   8.353 +SDL_RenderClear(SDL_Renderer * renderer)
   8.354  {
   8.355 -    SDL_Renderer *renderer;
   8.356 -
   8.357 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.358 -    if (!renderer) {
   8.359 -        return -1;
   8.360 -    }
   8.361 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.362 +
   8.363      if (!renderer->RenderClear) {
   8.364          SDL_BlendMode blendMode = renderer->blendMode;
   8.365          int status;
   8.366  
   8.367          if (blendMode >= SDL_BLENDMODE_BLEND) {
   8.368 -            SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   8.369 +            SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
   8.370          }
   8.371  
   8.372 -        status = SDL_RenderFillRect(NULL);
   8.373 +        status = SDL_RenderFillRect(renderer, NULL);
   8.374  
   8.375          if (blendMode >= SDL_BLENDMODE_BLEND) {
   8.376 -            SDL_SetRenderDrawBlendMode(blendMode);
   8.377 +            SDL_SetRenderDrawBlendMode(renderer, blendMode);
   8.378          }
   8.379          return status;
   8.380      }
   8.381 @@ -2339,33 +2263,25 @@
   8.382  }
   8.383  
   8.384  int
   8.385 -SDL_RenderDrawPoint(int x, int y)
   8.386 +SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
   8.387  {
   8.388      SDL_Point point;
   8.389  
   8.390      point.x = x;
   8.391      point.y = y;
   8.392 -    return SDL_RenderDrawPoints(&point, 1);
   8.393 +    return SDL_RenderDrawPoints(renderer, &point, 1);
   8.394  }
   8.395  
   8.396  int
   8.397 -SDL_RenderDrawPoints(const SDL_Point * points, int count)
   8.398 +SDL_RenderDrawPoints(SDL_Renderer * renderer,
   8.399 +                     const SDL_Point * points, int count)
   8.400  {
   8.401 -    SDL_Renderer *renderer;
   8.402 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.403  
   8.404      if (!points) {
   8.405          SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
   8.406          return -1;
   8.407      }
   8.408 -
   8.409 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.410 -    if (!renderer) {
   8.411 -        return -1;
   8.412 -    }
   8.413 -    if (!renderer->RenderDrawPoints) {
   8.414 -        SDL_Unsupported();
   8.415 -        return -1;
   8.416 -    }
   8.417      if (count < 1) {
   8.418          return 0;
   8.419      }
   8.420 @@ -2373,7 +2289,7 @@
   8.421  }
   8.422  
   8.423  int
   8.424 -SDL_RenderDrawLine(int x1, int y1, int x2, int y2)
   8.425 +SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
   8.426  {
   8.427      SDL_Point points[2];
   8.428  
   8.429 @@ -2381,27 +2297,19 @@
   8.430      points[0].y = y1;
   8.431      points[1].x = x2;
   8.432      points[1].y = y2;
   8.433 -    return SDL_RenderDrawLines(points, 2);
   8.434 +    return SDL_RenderDrawLines(renderer, points, 2);
   8.435  }
   8.436  
   8.437  int
   8.438 -SDL_RenderDrawLines(const SDL_Point * points, int count)
   8.439 +SDL_RenderDrawLines(SDL_Renderer * renderer,
   8.440 +                    const SDL_Point * points, int count)
   8.441  {
   8.442 -    SDL_Renderer *renderer;
   8.443 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.444  
   8.445      if (!points) {
   8.446          SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
   8.447          return -1;
   8.448      }
   8.449 -
   8.450 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.451 -    if (!renderer) {
   8.452 -        return -1;
   8.453 -    }
   8.454 -    if (!renderer->RenderDrawLines) {
   8.455 -        SDL_Unsupported();
   8.456 -        return -1;
   8.457 -    }
   8.458      if (count < 2) {
   8.459          return 0;
   8.460      }
   8.461 @@ -2409,33 +2317,27 @@
   8.462  }
   8.463  
   8.464  int
   8.465 -SDL_RenderDrawRect(const SDL_Rect * rect)
   8.466 +SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
   8.467  {
   8.468 -    return SDL_RenderDrawRects(&rect, 1);
   8.469 +    return SDL_RenderDrawRects(renderer, &rect, 1);
   8.470  }
   8.471  
   8.472  int
   8.473 -SDL_RenderDrawRects(const SDL_Rect ** rects, int count)
   8.474 +SDL_RenderDrawRects(SDL_Renderer * renderer,
   8.475 +                    const SDL_Rect ** rects, int count)
   8.476  {
   8.477 -    SDL_Renderer *renderer;
   8.478      int i;
   8.479  
   8.480 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.481 +
   8.482      if (!rects) {
   8.483          SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
   8.484          return -1;
   8.485      }
   8.486 -
   8.487 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.488 -    if (!renderer) {
   8.489 -        return -1;
   8.490 -    }
   8.491 -    if (!renderer->RenderDrawRects) {
   8.492 -        SDL_Unsupported();
   8.493 -        return -1;
   8.494 -    }
   8.495      if (count < 1) {
   8.496          return 0;
   8.497      }
   8.498 +
   8.499      /* Check for NULL rect, which means fill entire window */
   8.500      for (i = 0; i < count; ++i) {
   8.501          if (rects[i] == NULL) {
   8.502 @@ -2455,33 +2357,27 @@
   8.503  }
   8.504  
   8.505  int
   8.506 -SDL_RenderFillRect(const SDL_Rect * rect)
   8.507 +SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
   8.508  {
   8.509 -    return SDL_RenderFillRects(&rect, 1);
   8.510 +    return SDL_RenderFillRects(renderer, &rect, 1);
   8.511  }
   8.512  
   8.513  int
   8.514 -SDL_RenderFillRects(const SDL_Rect ** rects, int count)
   8.515 +SDL_RenderFillRects(SDL_Renderer * renderer,
   8.516 +                    const SDL_Rect ** rects, int count)
   8.517  {
   8.518 -    SDL_Renderer *renderer;
   8.519      int i;
   8.520  
   8.521 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.522 +
   8.523      if (!rects) {
   8.524          SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
   8.525          return -1;
   8.526      }
   8.527 -
   8.528 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.529 -    if (!renderer) {
   8.530 -        return -1;
   8.531 -    }
   8.532 -    if (!renderer->RenderFillRects) {
   8.533 -        SDL_Unsupported();
   8.534 -        return -1;
   8.535 -    }
   8.536      if (count < 1) {
   8.537          return 0;
   8.538      }
   8.539 +
   8.540      /* Check for NULL rect, which means fill entire window */
   8.541      for (i = 0; i < count; ++i) {
   8.542          if (rects[i] == NULL) {
   8.543 @@ -2501,28 +2397,20 @@
   8.544  }
   8.545  
   8.546  int
   8.547 -SDL_RenderCopy(SDL_Texture * texture, const SDL_Rect * srcrect,
   8.548 -               const SDL_Rect * dstrect)
   8.549 +SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   8.550 +               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   8.551  {
   8.552 -    SDL_Renderer *renderer;
   8.553      SDL_Window *window;
   8.554      SDL_Rect real_srcrect;
   8.555      SDL_Rect real_dstrect;
   8.556  
   8.557 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.558      CHECK_TEXTURE_MAGIC(texture, -1);
   8.559  
   8.560 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.561 -    if (!renderer) {
   8.562 -        return -1;
   8.563 -    }
   8.564 -    if (texture->renderer != renderer) {
   8.565 +    if (renderer != texture->renderer) {
   8.566          SDL_SetError("Texture was not created with this renderer");
   8.567          return -1;
   8.568      }
   8.569 -    if (!renderer->RenderCopy) {
   8.570 -        SDL_Unsupported();
   8.571 -        return -1;
   8.572 -    }
   8.573      window = renderer->window;
   8.574  
   8.575      real_srcrect.x = 0;
   8.576 @@ -2563,17 +2451,14 @@
   8.577  }
   8.578  
   8.579  int
   8.580 -SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
   8.581 -                     void * pixels, int pitch)
   8.582 +SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   8.583 +                     Uint32 format, void * pixels, int pitch)
   8.584  {
   8.585 -    SDL_Renderer *renderer;
   8.586      SDL_Window *window;
   8.587      SDL_Rect real_rect;
   8.588  
   8.589 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.590 -    if (!renderer) {
   8.591 -        return -1;
   8.592 -    }
   8.593 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.594 +
   8.595      if (!renderer->RenderReadPixels) {
   8.596          SDL_Unsupported();
   8.597          return -1;
   8.598 @@ -2607,17 +2492,14 @@
   8.599  }
   8.600  
   8.601  int
   8.602 -SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
   8.603 -                      const void * pixels, int pitch)
   8.604 +SDL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   8.605 +                      Uint32 format, const void * pixels, int pitch)
   8.606  {
   8.607 -    SDL_Renderer *renderer;
   8.608      SDL_Window *window;
   8.609      SDL_Rect real_rect;
   8.610  
   8.611 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.612 -    if (!renderer) {
   8.613 -        return -1;
   8.614 -    }
   8.615 +    CHECK_RENDERER_MAGIC(renderer, -1);
   8.616 +
   8.617      if (!renderer->RenderWritePixels) {
   8.618          SDL_Unsupported();
   8.619          return -1;
   8.620 @@ -2651,14 +2533,10 @@
   8.621  }
   8.622  
   8.623  void
   8.624 -SDL_RenderPresent(void)
   8.625 +SDL_RenderPresent(SDL_Renderer * renderer)
   8.626  {
   8.627 -    SDL_Renderer *renderer;
   8.628 -
   8.629 -    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   8.630 -    if (!renderer || !renderer->RenderPresent) {
   8.631 -        return;
   8.632 -    }
   8.633 +    CHECK_RENDERER_MAGIC(renderer, );
   8.634 +
   8.635  #if SDL_VIDEO_DRIVER_WINDOWS
   8.636      IME_Present((SDL_VideoData *)_this->driverdata);
   8.637  #endif
   8.638 @@ -2688,30 +2566,22 @@
   8.639  }
   8.640  
   8.641  void
   8.642 -SDL_DestroyRenderer(SDL_Window * window)
   8.643 +SDL_DestroyRenderer(SDL_Renderer * renderer)
   8.644  {
   8.645 -    SDL_Renderer *renderer;
   8.646 -
   8.647 -    CHECK_WINDOW_MAGIC(window, );
   8.648 -
   8.649 -    renderer = window->renderer;
   8.650 -    if (!renderer) {
   8.651 -        return;
   8.652 -    }
   8.653 +    CHECK_RENDERER_MAGIC(renderer, );
   8.654 +
   8.655 +    SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
   8.656  
   8.657      /* Free existing textures for this renderer */
   8.658      while (renderer->textures) {
   8.659          SDL_DestroyTexture(renderer->textures);
   8.660      }
   8.661  
   8.662 +    /* It's no longer magical... */
   8.663 +    renderer->magic = NULL;
   8.664 +
   8.665      /* Free the renderer instance */
   8.666      renderer->DestroyRenderer(renderer);
   8.667 -
   8.668 -    /* Clear references */
   8.669 -    window->renderer = NULL;
   8.670 -    if (SDL_CurrentDisplay->current_renderer == renderer) {
   8.671 -        SDL_CurrentDisplay->current_renderer = NULL;
   8.672 -    }
   8.673  }
   8.674  
   8.675  SDL_bool
     9.1 --- a/test/common.c	Tue Feb 01 19:15:42 2011 -0800
     9.2 +++ b/test/common.c	Tue Feb 01 19:19:43 2011 -0800
     9.3 @@ -646,7 +646,10 @@
     9.4          state->windows =
     9.5              (SDL_Window **) SDL_malloc(state->num_windows *
     9.6                                          sizeof(*state->windows));
     9.7 -        if (!state->windows) {
     9.8 +        state->renderers =
     9.9 +            (SDL_Renderer **) SDL_malloc(state->num_windows *
    9.10 +                                        sizeof(*state->renderers));
    9.11 +        if (!state->windows || !state->renderers) {
    9.12              fprintf(stderr, "Out of memory!\n");
    9.13              return SDL_FALSE;
    9.14          }
    9.15 @@ -685,6 +688,8 @@
    9.16  
    9.17              SDL_ShowWindow(state->windows[i]);
    9.18  
    9.19 +            state->renderers[i] = NULL;
    9.20 +
    9.21              if (!state->skip_renderer
    9.22                  && (state->renderdriver
    9.23                      || !(state->window_flags & SDL_WINDOW_OPENGL))) {
    9.24 @@ -707,8 +712,9 @@
    9.25                          return SDL_FALSE;
    9.26                      }
    9.27                  }
    9.28 -                if (SDL_CreateRenderer
    9.29 -                    (state->windows[i], m, state->render_flags) < 0) {
    9.30 +                state->renderers[i] = SDL_CreateRenderer(state->windows[i],
    9.31 +                                            m, state->render_flags);
    9.32 +                if (!state->renderers[i]) {
    9.33                      fprintf(stderr, "Couldn't create renderer: %s\n",
    9.34                              SDL_GetError());
    9.35                      return SDL_FALSE;
    9.36 @@ -717,12 +723,11 @@
    9.37                      SDL_RendererInfo info;
    9.38  
    9.39                      fprintf(stderr, "Current renderer:\n");
    9.40 -                    SDL_GetRendererInfo(&info);
    9.41 +                    SDL_GetRendererInfo(state->renderers[i], &info);
    9.42                      PrintRenderer(&info);
    9.43                  }
    9.44              }
    9.45          }
    9.46 -        SDL_SelectRenderer(state->windows[0]);
    9.47      }
    9.48  
    9.49      if (state->flags & SDL_INIT_AUDIO) {
    9.50 @@ -1012,15 +1017,25 @@
    9.51  void
    9.52  CommonQuit(CommonState * state)
    9.53  {
    9.54 +    int i;
    9.55 +
    9.56 +    if (state->windows) {
    9.57 +        SDL_free(state->windows);
    9.58 +    }
    9.59 +    if (state->renderers) {
    9.60 +        for (i = 0; i < state->num_windows; ++i) {
    9.61 +            if (state->renderers[i]) {
    9.62 +                SDL_DestroyRenderer(state->renderers[i]);
    9.63 +            }
    9.64 +        }
    9.65 +        SDL_free(state->renderers);
    9.66 +    }
    9.67      if (state->flags & SDL_INIT_VIDEO) {
    9.68          SDL_VideoQuit();
    9.69      }
    9.70      if (state->flags & SDL_INIT_AUDIO) {
    9.71          SDL_AudioQuit();
    9.72      }
    9.73 -    if (state->windows) {
    9.74 -        SDL_free(state->windows);
    9.75 -    }
    9.76      SDL_free(state);
    9.77  }
    9.78  
    10.1 --- a/test/common.h	Tue Feb 01 19:15:42 2011 -0800
    10.2 +++ b/test/common.h	Tue Feb 01 19:19:43 2011 -0800
    10.3 @@ -39,6 +39,7 @@
    10.4      const char *renderdriver;
    10.5      Uint32 render_flags;
    10.6      SDL_bool skip_renderer;
    10.7 +    SDL_Renderer **renderers;
    10.8  
    10.9      /* Audio info */
   10.10      const char *audiodriver;
    11.1 --- a/test/testdraw2.c	Tue Feb 01 19:15:42 2011 -0800
    11.2 +++ b/test/testdraw2.c	Tue Feb 01 19:19:43 2011 -0800
    11.3 @@ -19,7 +19,7 @@
    11.4  static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    11.5  
    11.6  void
    11.7 -DrawPoints(SDL_Window * window)
    11.8 +DrawPoints(SDL_Window * window, SDL_Renderer * renderer)
    11.9  {
   11.10      int i;
   11.11      int x, y;
   11.12 @@ -28,7 +28,6 @@
   11.13      /* Query the sizes */
   11.14      SDL_GetWindowSize(window, &window_w, &window_h);
   11.15  
   11.16 -    SDL_SetRenderDrawBlendMode(blendMode);
   11.17      for (i = 0; i < num_objects * 4; ++i) {
   11.18          /* Cycle the color and alpha, if desired */
   11.19          if (cycle_color) {
   11.20 @@ -53,18 +52,17 @@
   11.21                  cycle_direction = -cycle_direction;
   11.22              }
   11.23          }
   11.24 -        SDL_SetRenderDrawColor(255, (Uint8) current_color,
   11.25 +        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
   11.26                                 (Uint8) current_color, (Uint8) current_alpha);
   11.27  
   11.28          x = rand() % window_w;
   11.29          y = rand() % window_h;
   11.30 -        SDL_RenderDrawPoint(x, y);
   11.31 +        SDL_RenderDrawPoint(renderer, x, y);
   11.32      }
   11.33 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   11.34  }
   11.35  
   11.36  void
   11.37 -DrawLines(SDL_Window * window)
   11.38 +DrawLines(SDL_Window * window, SDL_Renderer * renderer)
   11.39  {
   11.40      int i;
   11.41      int x1, y1, x2, y2;
   11.42 @@ -73,7 +71,6 @@
   11.43      /* Query the sizes */
   11.44      SDL_GetWindowSize(window, &window_w, &window_h);
   11.45  
   11.46 -    SDL_SetRenderDrawBlendMode(blendMode);
   11.47      for (i = 0; i < num_objects; ++i) {
   11.48          /* Cycle the color and alpha, if desired */
   11.49          if (cycle_color) {
   11.50 @@ -98,27 +95,26 @@
   11.51                  cycle_direction = -cycle_direction;
   11.52              }
   11.53          }
   11.54 -        SDL_SetRenderDrawColor(255, (Uint8) current_color,
   11.55 +        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
   11.56                                 (Uint8) current_color, (Uint8) current_alpha);
   11.57  
   11.58          if (i == 0) {
   11.59 -            SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1);
   11.60 -            SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0);
   11.61 -            SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2);
   11.62 -            SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1);
   11.63 +            SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1);
   11.64 +            SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0);
   11.65 +            SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2);
   11.66 +            SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1);
   11.67          } else {
   11.68              x1 = (rand() % (window_w*2)) - window_w;
   11.69              x2 = (rand() % (window_w*2)) - window_w;
   11.70              y1 = (rand() % (window_h*2)) - window_h;
   11.71              y2 = (rand() % (window_h*2)) - window_h;
   11.72 -            SDL_RenderDrawLine(x1, y1, x2, y2);
   11.73 +            SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
   11.74          }
   11.75      }
   11.76 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   11.77  }
   11.78  
   11.79  void
   11.80 -DrawRects(SDL_Window * window)
   11.81 +DrawRects(SDL_Window * window, SDL_Renderer * renderer)
   11.82  {
   11.83      int i;
   11.84      SDL_Rect rect;
   11.85 @@ -127,7 +123,6 @@
   11.86      /* Query the sizes */
   11.87      SDL_GetWindowSize(window, &window_w, &window_h);
   11.88  
   11.89 -    SDL_SetRenderDrawBlendMode(blendMode);
   11.90      for (i = 0; i < num_objects / 4; ++i) {
   11.91          /* Cycle the color and alpha, if desired */
   11.92          if (cycle_color) {
   11.93 @@ -152,16 +147,15 @@
   11.94                  cycle_direction = -cycle_direction;
   11.95              }
   11.96          }
   11.97 -        SDL_SetRenderDrawColor(255, (Uint8) current_color,
   11.98 +        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
   11.99                                 (Uint8) current_color, (Uint8) current_alpha);
  11.100  
  11.101          rect.w = rand() % (window_h / 2);
  11.102          rect.h = rand() % (window_h / 2);
  11.103          rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2);
  11.104          rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2);
  11.105 -        SDL_RenderFillRect(&rect);
  11.106 +        SDL_RenderFillRect(renderer, &rect);
  11.107      }
  11.108 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  11.109  }
  11.110  
  11.111  int
  11.112 @@ -223,9 +217,10 @@
  11.113  
  11.114      /* Create the windows and initialize the renderers */
  11.115      for (i = 0; i < state->num_windows; ++i) {
  11.116 -        SDL_SelectRenderer(state->windows[i]);
  11.117 -        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  11.118 -        SDL_RenderClear();
  11.119 +        SDL_Renderer *renderer = state->renderers[i];
  11.120 +        SDL_SetRenderDrawBlendMode(renderer, blendMode);
  11.121 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  11.122 +        SDL_RenderClear(renderer);
  11.123      }
  11.124  
  11.125      srand((unsigned int)time(NULL));
  11.126 @@ -239,30 +234,17 @@
  11.127          ++frames;
  11.128          while (SDL_PollEvent(&event)) {
  11.129              CommonEvent(state, &event, &done);
  11.130 -            switch (event.type) {
  11.131 -            case SDL_WINDOWEVENT:
  11.132 -                switch (event.window.event) {
  11.133 -                case SDL_WINDOWEVENT_EXPOSED:
  11.134 -                    SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
  11.135 -                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  11.136 -                    SDL_RenderClear();
  11.137 -                    break;
  11.138 -                }
  11.139 -                break;
  11.140 -            default:
  11.141 -                break;
  11.142 -            }
  11.143          }
  11.144          for (i = 0; i < state->num_windows; ++i) {
  11.145 -            SDL_SelectRenderer(state->windows[i]);
  11.146 -            SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  11.147 -            SDL_RenderClear();
  11.148 +            SDL_Renderer *renderer = state->renderers[i];
  11.149 +            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  11.150 +            SDL_RenderClear(renderer);
  11.151  
  11.152 -            DrawRects(state->windows[i]);
  11.153 -            DrawLines(state->windows[i]);
  11.154 -            DrawPoints(state->windows[i]);
  11.155 +            DrawRects(state->windows[i], renderer);
  11.156 +            DrawLines(state->windows[i], renderer);
  11.157 +            DrawPoints(state->windows[i], renderer);
  11.158  
  11.159 -            SDL_RenderPresent();
  11.160 +            SDL_RenderPresent(renderer);
  11.161          }
  11.162      }
  11.163  
    12.1 --- a/test/testintersections.c	Tue Feb 01 19:15:42 2011 -0800
    12.2 +++ b/test/testintersections.c	Tue Feb 01 19:19:43 2011 -0800
    12.3 @@ -20,7 +20,7 @@
    12.4  static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    12.5  
    12.6  void
    12.7 -DrawPoints(SDL_Window * window)
    12.8 +DrawPoints(SDL_Window * window, SDL_Renderer * renderer)
    12.9  {
   12.10      int i;
   12.11      int x, y;
   12.12 @@ -29,7 +29,6 @@
   12.13      /* Query the sizes */
   12.14      SDL_GetWindowSize(window, &window_w, &window_h);
   12.15  
   12.16 -    SDL_SetRenderDrawBlendMode(blendMode);
   12.17      for (i = 0; i < num_objects * 4; ++i) {
   12.18          /* Cycle the color and alpha, if desired */
   12.19          if (cycle_color) {
   12.20 @@ -54,14 +53,13 @@
   12.21                  cycle_direction = -cycle_direction;
   12.22              }
   12.23          }
   12.24 -        SDL_SetRenderDrawColor(255, (Uint8) current_color,
   12.25 +        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
   12.26                                 (Uint8) current_color, (Uint8) current_alpha);
   12.27  
   12.28          x = rand() % window_w;
   12.29          y = rand() % window_h;
   12.30 -        SDL_RenderDrawPoint(x, y);
   12.31 +        SDL_RenderDrawPoint(renderer, x, y);
   12.32      }
   12.33 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   12.34  }
   12.35  
   12.36  #define MAX_LINES 16
   12.37 @@ -86,7 +84,7 @@
   12.38  
   12.39  
   12.40  void
   12.41 -DrawLines(SDL_Window * window)
   12.42 +DrawLines(SDL_Window * window, SDL_Renderer * renderer)
   12.43  {
   12.44      int i;
   12.45      int x1, y1, x2, y2;
   12.46 @@ -95,20 +93,18 @@
   12.47      /* Query the sizes */
   12.48      SDL_GetWindowSize(window, &window_w, &window_h);
   12.49  
   12.50 -    SDL_SetRenderDrawBlendMode(blendMode);
   12.51      for (i = 0; i < num_lines; ++i) {
   12.52 -        SDL_SetRenderDrawColor(255, 255, 255, 255);
   12.53 +        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
   12.54  
   12.55          if (i == -1) {
   12.56 -            SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1);
   12.57 -            SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0);
   12.58 -            SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2);
   12.59 -            SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1);
   12.60 +            SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1);
   12.61 +            SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0);
   12.62 +            SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2);
   12.63 +            SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1);
   12.64          } else {
   12.65 -            SDL_RenderDrawLine(lines[i].x, lines[i].y, lines[i].w, lines[i].h);
   12.66 +            SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
   12.67          }
   12.68      }
   12.69 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   12.70  }
   12.71  
   12.72  #define MAX_RECTS 16
   12.73 @@ -139,7 +135,7 @@
   12.74  }
   12.75  
   12.76  static void
   12.77 -DrawRects(SDL_Window * window)
   12.78 +DrawRects(SDL_Window * window, SDL_Renderer * renderer)
   12.79  {
   12.80      int i;
   12.81      int window_w, window_h;
   12.82 @@ -147,24 +143,20 @@
   12.83      /* Query the sizes */
   12.84      SDL_GetWindowSize(window, &window_w, &window_h);
   12.85  
   12.86 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   12.87      for (i = 0; i < num_rects; ++i) {
   12.88 -        SDL_SetRenderDrawColor(255, 127, 0, 255);
   12.89 -        SDL_RenderFillRect(&rects[i]);
   12.90 +        SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
   12.91 +        SDL_RenderFillRect(renderer, &rects[i]);
   12.92      }
   12.93 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   12.94  }
   12.95  
   12.96  static void
   12.97 -DrawRectLineIntersections(SDL_Window * window)
   12.98 +DrawRectLineIntersections(SDL_Window * window, SDL_Renderer * renderer)
   12.99  {
  12.100      int i, j, window_w, window_h;
  12.101  
  12.102      /* Query the sizes */
  12.103      SDL_GetWindowSize(window, &window_w, &window_h);
  12.104  
  12.105 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  12.106 -
  12.107      for (i = 0; i < num_rects; i++)
  12.108          for (j = 0; j < num_lines; j++) {
  12.109              int x1, y1, x2, y2;
  12.110 @@ -177,31 +169,25 @@
  12.111              y2 = lines[j].h;
  12.112  
  12.113              if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
  12.114 -                SDL_SetRenderDrawColor(0, 255, 55, 255);
  12.115 -                SDL_RenderDrawLine(x1, y1, x2, y2);
  12.116 +                SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
  12.117 +                SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
  12.118              }
  12.119          }
  12.120 -
  12.121 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  12.122  }
  12.123  
  12.124  static void
  12.125 -DrawRectRectIntersections(SDL_Window * window)
  12.126 +DrawRectRectIntersections(SDL_Window * window, SDL_Renderer * renderer)
  12.127  {
  12.128      int i, j;
  12.129  
  12.130 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  12.131 -
  12.132      for (i = 0; i < num_rects; i++)
  12.133          for (j = i + 1; j < num_rects; j++) {
  12.134              SDL_Rect r;
  12.135              if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
  12.136 -                SDL_SetRenderDrawColor(255, 200, 0, 255);
  12.137 -                SDL_RenderFillRect(&r);
  12.138 +                SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
  12.139 +                SDL_RenderFillRect(renderer, &r);
  12.140              }
  12.141          }
  12.142 -
  12.143 -    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
  12.144  }
  12.145  
  12.146  int
  12.147 @@ -264,9 +250,10 @@
  12.148  
  12.149      /* Create the windows and initialize the renderers */
  12.150      for (i = 0; i < state->num_windows; ++i) {
  12.151 -        SDL_SelectRenderer(state->windows[i]);
  12.152 -        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  12.153 -        SDL_RenderClear();
  12.154 +        SDL_Renderer *renderer = state->renderers[i];
  12.155 +        SDL_SetRenderDrawBlendMode(renderer, blendMode);
  12.156 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  12.157 +        SDL_RenderClear(renderer);
  12.158      }
  12.159  
  12.160      srand(time(NULL));
  12.161 @@ -311,31 +298,22 @@
  12.162                      break;
  12.163                  }
  12.164                  break;
  12.165 -            case SDL_WINDOWEVENT:
  12.166 -                switch (event.window.event) {
  12.167 -                case SDL_WINDOWEVENT_EXPOSED:
  12.168 -                    SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
  12.169 -                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  12.170 -                    SDL_RenderClear();
  12.171 -                    break;
  12.172 -                }
  12.173 -                break;
  12.174              default:
  12.175                  break;
  12.176              }
  12.177          }
  12.178          for (i = 0; i < state->num_windows; ++i) {
  12.179 -            SDL_SelectRenderer(state->windows[i]);
  12.180 -            SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  12.181 -            SDL_RenderClear();
  12.182 +            SDL_Renderer *renderer = state->renderers[i];
  12.183 +            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  12.184 +            SDL_RenderClear(renderer);
  12.185  
  12.186 -            DrawRects(state->windows[i]);
  12.187 -            DrawPoints(state->windows[i]);
  12.188 -            DrawRectRectIntersections(state->windows[i]);
  12.189 -            DrawLines(state->windows[i]);
  12.190 -            DrawRectLineIntersections(state->windows[i]);
  12.191 +            DrawRects(state->windows[i], renderer);
  12.192 +            DrawPoints(state->windows[i], renderer);
  12.193 +            DrawRectRectIntersections(state->windows[i], renderer);
  12.194 +            DrawLines(state->windows[i], renderer);
  12.195 +            DrawRectLineIntersections(state->windows[i], renderer);
  12.196  
  12.197 -            SDL_RenderPresent();
  12.198 +            SDL_RenderPresent(renderer);
  12.199          }
  12.200      }
  12.201  
    13.1 --- a/test/testshape.c	Tue Feb 01 19:15:42 2011 -0800
    13.2 +++ b/test/testshape.c	Tue Feb 01 19:19:43 2011 -0800
    13.3 @@ -11,177 +11,183 @@
    13.4  #define TICK_INTERVAL 1000/10
    13.5  
    13.6  typedef struct LoadedPicture {
    13.7 -	SDL_Surface *surface;
    13.8 -	SDL_Texture *texture;
    13.9 -	SDL_WindowShapeMode mode;
   13.10 +    SDL_Surface *surface;
   13.11 +    SDL_Texture *texture;
   13.12 +    SDL_WindowShapeMode mode;
   13.13  } LoadedPicture;
   13.14  
   13.15 -void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
   13.16 -	SDL_SelectRenderer(window);
   13.17 -	
   13.18 -	//Clear render-target to blue.
   13.19 -	SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
   13.20 -	SDL_RenderClear();
   13.21 -	
   13.22 -	//Render the texture.
   13.23 -	SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions);
   13.24 -	
   13.25 -	SDL_RenderPresent();
   13.26 +void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
   13.27 +{
   13.28 +    //Clear render-target to blue.
   13.29 +    SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
   13.30 +    SDL_RenderClear(renderer);
   13.31 +    
   13.32 +    //Render the texture.
   13.33 +    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
   13.34 +    
   13.35 +    SDL_RenderPresent(renderer);
   13.36  }
   13.37  
   13.38  static Uint32 next_time;
   13.39  
   13.40 -Uint32 time_left() {
   13.41 +Uint32 time_left()
   13.42 +{
   13.43      Uint32 now = SDL_GetTicks();
   13.44      if(next_time <= now)
   13.45          return 0;
   13.46 -	else
   13.47 +    else
   13.48          return next_time - now;
   13.49  }
   13.50  
   13.51 -int main(int argc,char** argv) {
   13.52 -	Uint8 num_pictures;
   13.53 -	LoadedPicture* pictures;
   13.54 -	int i, j;
   13.55 -	SDL_PixelFormat* format = NULL;
   13.56 -	SDL_Window *window;
   13.57 -	SDL_Color black = {0,0,0,0xff};
   13.58 -	SDL_Event event;
   13.59 -	int event_pending = 0;
   13.60 -	int should_exit = 0;
   13.61 -	unsigned int current_picture;
   13.62 -	int button_down;
   13.63 -	Uint32 pixelFormat = 0;
   13.64 -	int access = 0;
   13.65 -	SDL_Rect texture_dimensions;;
   13.66 +int main(int argc,char** argv)
   13.67 +{
   13.68 +    Uint8 num_pictures;
   13.69 +    LoadedPicture* pictures;
   13.70 +    int i, j;
   13.71 +    SDL_PixelFormat* format = NULL;
   13.72 +    SDL_Window *window;
   13.73 +    SDL_Renderer *renderer;
   13.74 +    SDL_Color black = {0,0,0,0xff};
   13.75 +    SDL_Event event;
   13.76 +    int event_pending = 0;
   13.77 +    int should_exit = 0;
   13.78 +    unsigned int current_picture;
   13.79 +    int button_down;
   13.80 +    Uint32 pixelFormat = 0;
   13.81 +    int access = 0;
   13.82 +    SDL_Rect texture_dimensions;;
   13.83  
   13.84 -	if(argc < 2) {
   13.85 -    	printf("SDL_Shape requires at least one bitmap file as argument.\n");
   13.86 -    	exit(-1);
   13.87 +    if(argc < 2) {
   13.88 +        printf("SDL_Shape requires at least one bitmap file as argument.\n");
   13.89 +        exit(-1);
   13.90      }
   13.91 -	
   13.92 -	if(SDL_VideoInit(NULL) == -1) {
   13.93 -		printf("Could not initialize SDL video.\n");
   13.94 -		exit(-2);
   13.95 -	}
   13.96 -	
   13.97 -	num_pictures = argc - 1;
   13.98 -	pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
   13.99 -	for(i=0;i<num_pictures;i++)
  13.100 -		pictures[i].surface = NULL;
  13.101 -	for(i=0;i<num_pictures;i++) {
  13.102 -		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
  13.103 -		if(pictures[i].surface == NULL) {
  13.104 -			j = 0;
  13.105 -			for(j=0;j<num_pictures;j++)
  13.106 -				if(pictures[j].surface != NULL)
  13.107 -					SDL_FreeSurface(pictures[j].surface);
  13.108 -			free(pictures);
  13.109 -			SDL_VideoQuit();
  13.110 -			printf("Could not load surface from named bitmap file.\n");
  13.111 -			exit(-3);
  13.112 -		}
  13.113 +    
  13.114 +    if(SDL_VideoInit(NULL) == -1) {
  13.115 +        printf("Could not initialize SDL video.\n");
  13.116 +        exit(-2);
  13.117 +    }
  13.118 +    
  13.119 +    num_pictures = argc - 1;
  13.120 +    pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
  13.121 +    for(i=0;i<num_pictures;i++)
  13.122 +        pictures[i].surface = NULL;
  13.123 +    for(i=0;i<num_pictures;i++) {
  13.124 +        pictures[i].surface = SDL_LoadBMP(argv[i+1]);
  13.125 +        if(pictures[i].surface == NULL) {
  13.126 +            j = 0;
  13.127 +            for(j=0;j<num_pictures;j++)
  13.128 +                if(pictures[j].surface != NULL)
  13.129 +                    SDL_FreeSurface(pictures[j].surface);
  13.130 +            free(pictures);
  13.131 +            SDL_VideoQuit();
  13.132 +            printf("Could not load surface from named bitmap file.\n");
  13.133 +            exit(-3);
  13.134 +        }
  13.135  
  13.136 -		format = pictures[i].surface->format;
  13.137 -		if(format->Amask != 0) {
  13.138 -			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
  13.139 -			pictures[i].mode.parameters.binarizationCutoff = 255;
  13.140 -		}
  13.141 -		else {
  13.142 -			pictures[i].mode.mode = ShapeModeColorKey;
  13.143 -			pictures[i].mode.parameters.colorKey = black;
  13.144 -		}
  13.145 -	}
  13.146 -	
  13.147 -	window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
  13.148 -	if(window == NULL) {
  13.149 -		for(i=0;i<num_pictures;i++)
  13.150 -			SDL_FreeSurface(pictures[i].surface);
  13.151 -		free(pictures);
  13.152 -		SDL_VideoQuit();
  13.153 -		printf("Could not create shaped window for SDL_Shape.\n");
  13.154 -		exit(-4);
  13.155 -	}
  13.156 -	if(SDL_CreateRenderer(window,-1,0) == -1) {
  13.157 -		SDL_DestroyWindow(window);
  13.158 -		for(i=0;i<num_pictures;i++)
  13.159 -			SDL_FreeSurface(pictures[i].surface);
  13.160 -		free(pictures);
  13.161 -		SDL_VideoQuit();
  13.162 -		printf("Could not create rendering context for SDL_Shape window.\n");
  13.163 -		exit(-5);
  13.164 -	}
  13.165 -	
  13.166 -	for(i=0;i<num_pictures;i++)
  13.167 -		pictures[i].texture = NULL;
  13.168 -	for(i=0;i<num_pictures;i++) {
  13.169 -		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
  13.170 -		if(pictures[i].texture == NULL) {
  13.171 -			j = 0;
  13.172 -			for(j=0;j<num_pictures;i++)
  13.173 -				if(pictures[i].texture != NULL)
  13.174 -					SDL_DestroyTexture(pictures[i].texture);
  13.175 -			for(i=0;i<num_pictures;i++)
  13.176 -				SDL_FreeSurface(pictures[i].surface);
  13.177 -			free(pictures);
  13.178 -			SDL_DestroyRenderer(window);
  13.179 -			SDL_DestroyWindow(window);
  13.180 -			SDL_VideoQuit();
  13.181 -			printf("Could not create texture for SDL_shape.\n");
  13.182 -			exit(-6);
  13.183 -		}
  13.184 -	}
  13.185 -	
  13.186 -	event_pending = 0;
  13.187 -	should_exit = 0;
  13.188 -	event_pending = SDL_PollEvent(&event);
  13.189 -	current_picture = 0;
  13.190 -	button_down = 0;
  13.191 -	texture_dimensions.h = 0;
  13.192 -	texture_dimensions.w = 0;
  13.193 -	texture_dimensions.x = 0;
  13.194 -	texture_dimensions.y = 0;
  13.195 -	SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
  13.196 -	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
  13.197 -	SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
  13.198 -	next_time = SDL_GetTicks() + TICK_INTERVAL;
  13.199 -	while(should_exit == 0) {
  13.200 -		event_pending = SDL_PollEvent(&event);
  13.201 -		if(event_pending == 1) {
  13.202 -			if(event.type == SDL_KEYDOWN) {
  13.203 -				button_down = 1;
  13.204 -				if(event.key.keysym.sym == SDLK_ESCAPE)
  13.205 -					should_exit = 1;
  13.206 -			}
  13.207 -			if(button_down && event.type == SDL_KEYUP) {
  13.208 -				button_down = 0;
  13.209 -				current_picture += 1;
  13.210 -				if(current_picture >= num_pictures)
  13.211 -					current_picture = 0;
  13.212 -				SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
  13.213 -				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
  13.214 -				SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
  13.215 -			}
  13.216 -			if(event.type == SDL_QUIT)
  13.217 -				should_exit = 1;
  13.218 -			event_pending = 0;
  13.219 -		}
  13.220 -		render(window,pictures[current_picture].texture,texture_dimensions);
  13.221 -		SDL_Delay(time_left());
  13.222 -		next_time += TICK_INTERVAL;
  13.223 -	}
  13.224 -	
  13.225 -	//Free the textures.
  13.226 -	for(i=0;i<num_pictures;i++)
  13.227 -		SDL_DestroyTexture(pictures[i].texture);
  13.228 -	//Destroy the window.
  13.229 -	SDL_DestroyWindow(window);
  13.230 -	//Free the original surfaces backing the textures.
  13.231 -	for(i=0;i<num_pictures;i++)
  13.232 -		SDL_FreeSurface(pictures[i].surface);
  13.233 -	free(pictures);
  13.234 -	//Call SDL_VideoQuit() before quitting.
  13.235 -	SDL_VideoQuit();
  13.236 +        format = pictures[i].surface->format;
  13.237 +        if(format->Amask != 0) {
  13.238 +            pictures[i].mode.mode = ShapeModeBinarizeAlpha;
  13.239 +            pictures[i].mode.parameters.binarizationCutoff = 255;
  13.240 +        }
  13.241 +        else {
  13.242 +            pictures[i].mode.mode = ShapeModeColorKey;
  13.243 +            pictures[i].mode.parameters.colorKey = black;
  13.244 +        }
  13.245 +    }
  13.246 +    
  13.247 +    window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
  13.248 +    if(window == NULL) {
  13.249 +        for(i=0;i<num_pictures;i++)
  13.250 +            SDL_FreeSurface(pictures[i].surface);
  13.251 +        free(pictures);
  13.252 +        SDL_VideoQuit();
  13.253 +        printf("Could not create shaped window for SDL_Shape.\n");
  13.254 +        exit(-4);
  13.255 +    }
  13.256 +    renderer = SDL_CreateRenderer(window,-1,0);
  13.257 +    if (!renderer) {
  13.258 +        SDL_DestroyWindow(window);
  13.259 +        for(i=0;i<num_pictures;i++)
  13.260 +            SDL_FreeSurface(pictures[i].surface);
  13.261 +        free(pictures);
  13.262 +        SDL_VideoQuit();
  13.263 +        printf("Could not create rendering context for SDL_Shape window.\n");
  13.264 +        exit(-5);
  13.265 +    }
  13.266 +    
  13.267 +    for(i=0;i<num_pictures;i++)
  13.268 +        pictures[i].texture = NULL;
  13.269 +    for(i=0;i<num_pictures;i++) {
  13.270 +        pictures[i].texture = SDL_CreateTextureFromSurface(renderer,0,pictures[i].surface);
  13.271 +        if(pictures[i].texture == NULL) {
  13.272 +            j = 0;
  13.273 +            for(j=0;j<num_pictures;i++)
  13.274 +                if(pictures[i].texture != NULL)
  13.275 +                    SDL_DestroyTexture(pictures[i].texture);
  13.276 +            for(i=0;i<num_pictures;i++)
  13.277 +                SDL_FreeSurface(pictures[i].surface);
  13.278 +            free(pictures);
  13.279 +            SDL_DestroyRenderer(renderer);
  13.280 +            SDL_DestroyWindow(window);
  13.281 +            SDL_VideoQuit();
  13.282 +            printf("Could not create texture for SDL_shape.\n");
  13.283 +            exit(-6);
  13.284 +        }
  13.285 +    }
  13.286 +    
  13.287 +    event_pending = 0;
  13.288 +    should_exit = 0;
  13.289 +    event_pending = SDL_PollEvent(&event);
  13.290 +    current_picture = 0;
  13.291 +    button_down = 0;
  13.292 +    texture_dimensions.h = 0;
  13.293 +    texture_dimensions.w = 0;
  13.294 +    texture_dimensions.x = 0;
  13.295 +    texture_dimensions.y = 0;
  13.296 +    SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
  13.297 +    SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
  13.298 +    SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
  13.299 +    next_time = SDL_GetTicks() + TICK_INTERVAL;
  13.300 +    while(should_exit == 0) {
  13.301 +        event_pending = SDL_PollEvent(&event);
  13.302 +        if(event_pending == 1) {
  13.303 +            if(event.type == SDL_KEYDOWN) {
  13.304 +                button_down = 1;
  13.305 +                if(event.key.keysym.sym == SDLK_ESCAPE)
  13.306 +                    should_exit = 1;
  13.307 +            }
  13.308 +            if(button_down && event.type == SDL_KEYUP) {
  13.309 +                button_down = 0;
  13.310 +                current_picture += 1;
  13.311 +                if(current_picture >= num_pictures)
  13.312 +                    current_picture = 0;
  13.313 +                SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
  13.314 +                SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
  13.315 +                SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
  13.316 +            }
  13.317 +            if(event.type == SDL_QUIT)
  13.318 +                should_exit = 1;
  13.319 +            event_pending = 0;
  13.320 +        }
  13.321 +        render(renderer,pictures[current_picture].texture,texture_dimensions);
  13.322 +        SDL_Delay(time_left());
  13.323 +        next_time += TICK_INTERVAL;
  13.324 +    }
  13.325 +    
  13.326 +    //Free the textures.
  13.327 +    for(i=0;i<num_pictures;i++)
  13.328 +        SDL_DestroyTexture(pictures[i].texture);
  13.329 +    SDL_DestroyRenderer(renderer);
  13.330 +    //Destroy the window.
  13.331 +    SDL_DestroyWindow(window);
  13.332 +    //Free the original surfaces backing the textures.
  13.333 +    for(i=0;i<num_pictures;i++)
  13.334 +        SDL_FreeSurface(pictures[i].surface);
  13.335 +    free(pictures);
  13.336 +    //Call SDL_VideoQuit() before quitting.
  13.337 +    SDL_VideoQuit();
  13.338  
  13.339 -	return 0;
  13.340 +    return 0;
  13.341  }
  13.342 +
  13.343 +/* vi: set ts=4 sw=4 expandtab: */
    14.1 --- a/test/testsprite2.c	Tue Feb 01 19:15:42 2011 -0800
    14.2 +++ b/test/testsprite2.c	Tue Feb 01 19:19:43 2011 -0800
    14.3 @@ -6,8 +6,8 @@
    14.4  
    14.5  #include "common.h"
    14.6  
    14.7 -#define NUM_SPRITES	100
    14.8 -#define MAX_SPEED 	1
    14.9 +#define NUM_SPRITES    100
   14.10 +#define MAX_SPEED     1
   14.11  
   14.12  static CommonState *state;
   14.13  static int num_sprites;
   14.14 @@ -76,12 +76,12 @@
   14.15  
   14.16      /* Create textures from the image */
   14.17      for (i = 0; i < state->num_windows; ++i) {
   14.18 -        SDL_SelectRenderer(state->windows[i]);
   14.19 -        sprites[i] = SDL_CreateTextureFromSurface(0, temp);
   14.20 +        SDL_Renderer *renderer = state->renderers[i];
   14.21 +        sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
   14.22          if (!sprites[i]) {
   14.23 -		SDL_SetColorKey(temp, 0, 0);
   14.24 -		sprites[i] = SDL_CreateTextureFromSurface(0, temp);
   14.25 -	}
   14.26 +            SDL_SetColorKey(temp, 0, 0);
   14.27 +            sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
   14.28 +        }
   14.29          if (!sprites[i]) {
   14.30              fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
   14.31              SDL_FreeSurface(temp);
   14.32 @@ -96,15 +96,13 @@
   14.33  }
   14.34  
   14.35  void
   14.36 -MoveSprites(SDL_Window * window, SDL_Texture * sprite)
   14.37 +MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
   14.38  {
   14.39      int i, n;
   14.40      int window_w, window_h;
   14.41      SDL_Rect temp;
   14.42      SDL_Rect *position, *velocity;
   14.43  
   14.44 -    SDL_SelectRenderer(window);
   14.45 -
   14.46      /* Query the sizes */
   14.47      SDL_GetWindowSize(window, &window_w, &window_h);
   14.48  
   14.49 @@ -136,55 +134,55 @@
   14.50      }
   14.51  
   14.52      /* Draw a gray background */
   14.53 -    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   14.54 -    SDL_RenderClear();
   14.55 +    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   14.56 +    SDL_RenderClear(renderer);
   14.57  
   14.58      /* Test points */
   14.59 -    SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF);
   14.60 -    SDL_RenderDrawPoint(0, 0);
   14.61 -    SDL_RenderDrawPoint(window_w-1, 0);
   14.62 -    SDL_RenderDrawPoint(0, window_h-1);
   14.63 -    SDL_RenderDrawPoint(window_w-1, window_h-1);
   14.64 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
   14.65 +    SDL_RenderDrawPoint(renderer, 0, 0);
   14.66 +    SDL_RenderDrawPoint(renderer, window_w-1, 0);
   14.67 +    SDL_RenderDrawPoint(renderer, 0, window_h-1);
   14.68 +    SDL_RenderDrawPoint(renderer, window_w-1, window_h-1);
   14.69  
   14.70      /* Test horizontal and vertical lines */
   14.71 -    SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
   14.72 -    SDL_RenderDrawLine(1, 0, window_w-2, 0);
   14.73 -    SDL_RenderDrawLine(1, window_h-1, window_w-2, window_h-1);
   14.74 -    SDL_RenderDrawLine(0, 1, 0, window_h-2);
   14.75 -    SDL_RenderDrawLine(window_w-1, 1, window_w-1, window_h-2);
   14.76 +    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
   14.77 +    SDL_RenderDrawLine(renderer, 1, 0, window_w-2, 0);
   14.78 +    SDL_RenderDrawLine(renderer, 1, window_h-1, window_w-2, window_h-1);
   14.79 +    SDL_RenderDrawLine(renderer, 0, 1, 0, window_h-2);
   14.80 +    SDL_RenderDrawLine(renderer, window_w-1, 1, window_w-1, window_h-2);
   14.81  
   14.82      /* Test fill and copy */
   14.83 -    SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
   14.84 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
   14.85      temp.x = 1;
   14.86      temp.y = 1;
   14.87      temp.w = sprite_w;
   14.88      temp.h = sprite_h;
   14.89 -    SDL_RenderFillRect(&temp);
   14.90 -    SDL_RenderCopy(sprite, NULL, &temp);
   14.91 +    SDL_RenderFillRect(renderer, &temp);
   14.92 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
   14.93      temp.x = window_w-sprite_w-1;
   14.94      temp.y = 1;
   14.95      temp.w = sprite_w;
   14.96      temp.h = sprite_h;
   14.97 -    SDL_RenderFillRect(&temp);
   14.98 -    SDL_RenderCopy(sprite, NULL, &temp);
   14.99 +    SDL_RenderFillRect(renderer, &temp);
  14.100 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
  14.101      temp.x = 1;
  14.102      temp.y = window_h-sprite_h-1;
  14.103      temp.w = sprite_w;
  14.104      temp.h = sprite_h;
  14.105 -    SDL_RenderFillRect(&temp);
  14.106 -    SDL_RenderCopy(sprite, NULL, &temp);
  14.107 +    SDL_RenderFillRect(renderer, &temp);
  14.108 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
  14.109      temp.x = window_w-sprite_w-1;
  14.110      temp.y = window_h-sprite_h-1;
  14.111      temp.w = sprite_w;
  14.112      temp.h = sprite_h;
  14.113 -    SDL_RenderFillRect(&temp);
  14.114 -    SDL_RenderCopy(sprite, NULL, &temp);
  14.115 +    SDL_RenderFillRect(renderer, &temp);
  14.116 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
  14.117  
  14.118      /* Test diagonal lines */
  14.119 -    SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
  14.120 -    SDL_RenderDrawLine(sprite_w, sprite_h,
  14.121 +    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  14.122 +    SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
  14.123                         window_w-sprite_w-2, window_h-sprite_h-2);
  14.124 -    SDL_RenderDrawLine(window_w-sprite_w-2, sprite_h,
  14.125 +    SDL_RenderDrawLine(renderer, window_w-sprite_w-2, sprite_h,
  14.126                         sprite_w, window_h-sprite_h-2);
  14.127  
  14.128      /* Move the sprite, bounce at the wall, and draw */
  14.129 @@ -204,11 +202,11 @@
  14.130          }
  14.131  
  14.132          /* Blit the sprite onto the screen */
  14.133 -        SDL_RenderCopy(sprite, NULL, position);
  14.134 +        SDL_RenderCopy(renderer, sprite, NULL, position);
  14.135      }
  14.136  
  14.137      /* Update the screen! */
  14.138 -    SDL_RenderPresent();
  14.139 +    SDL_RenderPresent(renderer);
  14.140  }
  14.141  
  14.142  int
  14.143 @@ -276,9 +274,9 @@
  14.144          quit(2);
  14.145      }
  14.146      for (i = 0; i < state->num_windows; ++i) {
  14.147 -        SDL_SelectRenderer(state->windows[i]);
  14.148 -        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  14.149 -        SDL_RenderClear();
  14.150 +        SDL_Renderer *renderer = state->renderers[i];
  14.151 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  14.152 +        SDL_RenderClear(renderer);
  14.153      }
  14.154      if (LoadSprite("icon.bmp") < 0) {
  14.155          quit(2);
  14.156 @@ -314,22 +312,9 @@
  14.157          ++frames;
  14.158          while (SDL_PollEvent(&event)) {
  14.159              CommonEvent(state, &event, &done);
  14.160 -            switch (event.type) {
  14.161 -            case SDL_WINDOWEVENT:
  14.162 -                switch (event.window.event) {
  14.163 -                case SDL_WINDOWEVENT_EXPOSED:
  14.164 -                    SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
  14.165 -                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  14.166 -                    SDL_RenderClear();
  14.167 -                    break;
  14.168 -                }
  14.169 -                break;
  14.170 -            default:
  14.171 -                break;
  14.172 -            }
  14.173          }
  14.174          for (i = 0; i < state->num_windows; ++i) {
  14.175 -            MoveSprites(state->windows[i], sprites[i]);
  14.176 +            MoveSprites(state->windows[i], state->renderers[i], sprites[i]);
  14.177          }
  14.178      }
  14.179  
  14.180 @@ -340,7 +325,7 @@
  14.181          printf("%2.2f frames per second\n", fps);
  14.182      }
  14.183      quit(0);
  14.184 -	return 0;
  14.185 +    return 0;
  14.186  }
  14.187  
  14.188  /* vi: set ts=4 sw=4 expandtab: */
    15.1 --- a/test/testspriteminimal.c	Tue Feb 01 19:15:42 2011 -0800
    15.2 +++ b/test/testspriteminimal.c	Tue Feb 01 19:19:43 2011 -0800
    15.3 @@ -24,7 +24,7 @@
    15.4  }
    15.5  
    15.6  int
    15.7 -LoadSprite(char *file)
    15.8 +LoadSprite(char *file, SDL_Renderer *renderer)
    15.9  {
   15.10      SDL_Surface *temp;
   15.11  
   15.12 @@ -60,10 +60,10 @@
   15.13      }
   15.14  
   15.15      /* Create textures from the image */
   15.16 -    sprite = SDL_CreateTextureFromSurface(0, temp);
   15.17 +    sprite = SDL_CreateTextureFromSurface(renderer, 0, temp);
   15.18      if (!sprite) {
   15.19          SDL_SetColorKey(temp, 0, 0);
   15.20 -        sprite = SDL_CreateTextureFromSurface(0, temp);
   15.21 +        sprite = SDL_CreateTextureFromSurface(renderer, 0, temp);
   15.22      }
   15.23      if (!sprite) {
   15.24          fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
   15.25 @@ -77,7 +77,7 @@
   15.26  }
   15.27  
   15.28  void
   15.29 -MoveSprites(SDL_Window * window, SDL_Texture * sprite)
   15.30 +MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
   15.31  {
   15.32      int i;
   15.33      int window_w = WINDOW_WIDTH;
   15.34 @@ -85,8 +85,8 @@
   15.35      SDL_Rect *position, *velocity;
   15.36  
   15.37      /* Draw a gray background */
   15.38 -    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   15.39 -    SDL_RenderClear();
   15.40 +    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   15.41 +    SDL_RenderClear(renderer);
   15.42  
   15.43      /* Move the sprite, bounce at the wall, and draw */
   15.44      for (i = 0; i < NUM_SPRITES; ++i) {
   15.45 @@ -104,17 +104,18 @@
   15.46          }
   15.47  
   15.48          /* Blit the sprite onto the screen */
   15.49 -        SDL_RenderCopy(sprite, NULL, position);
   15.50 +        SDL_RenderCopy(renderer, sprite, NULL, position);
   15.51      }
   15.52  
   15.53      /* Update the screen! */
   15.54 -    SDL_RenderPresent();
   15.55 +    SDL_RenderPresent(renderer);
   15.56  }
   15.57  
   15.58  int
   15.59  main(int argc, char *argv[])
   15.60  {
   15.61      SDL_Window *window;
   15.62 +    SDL_Renderer *renderer;
   15.63      int i, done;
   15.64      SDL_Event event;
   15.65  
   15.66 @@ -127,7 +128,12 @@
   15.67          quit(2);
   15.68      }
   15.69  
   15.70 -    if (LoadSprite("icon.bmp") < 0) {
   15.71 +    renderer = SDL_CreateRenderer(window, -1, 0);
   15.72 +    if (!renderer) {
   15.73 +        quit(2);
   15.74 +    }
   15.75 +
   15.76 +    if (LoadSprite("icon.bmp", renderer) < 0) {
   15.77          quit(2);
   15.78      }
   15.79  
   15.80 @@ -155,7 +161,7 @@
   15.81                  done = 1;
   15.82              }
   15.83          }
   15.84 -        MoveSprites(window, sprite);
   15.85 +        MoveSprites(window, renderer, sprite);
   15.86      }
   15.87  
   15.88      quit(0);