Mercurial > sdl-ios-xcode
changeset 5150:ad50b3db78bd
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.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 01 Feb 2011 19:19:43 -0800 |
parents | 3052772b59db |
children | 5429daf5e3f9 |
files | 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 |
diffstat | 15 files changed, 626 insertions(+), 699 deletions(-) [+] |
line wrap: on
line diff
--- a/include/SDL_render.h Tue Feb 01 19:15:42 2011 -0800 +++ b/include/SDL_render.h Tue Feb 01 19:19:43 2011 -0800 @@ -86,6 +86,12 @@ } SDL_TextureModulate; /** + * \brief A structure representing rendering state + */ +struct SDL_Renderer; +typedef struct SDL_Renderer SDL_Renderer; + +/** * \brief An efficient driver-specific representation of pixel data */ struct SDL_Texture; @@ -123,37 +129,29 @@ SDL_RendererInfo * info); /** - * \brief Create and make active a 2D rendering context for a window. + * \brief Create a 2D rendering context for a window. * * \param window The window where rendering is displayed. * \param index The index of the rendering driver to initialize, or -1 to * initialize the first one supporting the requested flags. * \param flags ::SDL_RendererFlags. * - * \return 0 on success, -1 if there was an error creating the renderer. + * \return A valid rendering context or NULL if there was an error. * - * \sa SDL_SelectRenderer() * \sa SDL_GetRendererInfo() * \sa SDL_DestroyRenderer() */ -extern DECLSPEC int SDLCALL SDL_CreateRenderer(SDL_Window * window, +extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags); /** - * \brief Select the rendering context for a particular window. - * - * \return 0 on success, -1 if the selected window doesn't have a - * rendering context. + * \brief Get information about a rendering context. */ -extern DECLSPEC int SDLCALL SDL_SelectRenderer(SDL_Window * window); +extern DECLSPEC int SDLCALL SDL_GetRendererInfo(SDL_Renderer * renderer, + SDL_RendererInfo * info); /** - * \brief Get information about the current rendering context. - */ -extern DECLSPEC int SDLCALL SDL_GetRendererInfo(SDL_RendererInfo * info); - -/** - * \brief Create a texture for the current rendering context. + * \brief Create a texture for a rendering context. * * \param format The format of the texture. * \param access One of the enumerated values in ::SDL_TextureAccess. @@ -167,7 +165,7 @@ * \sa SDL_QueryTexture() * \sa SDL_DestroyTexture() */ -extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(Uint32 format, +extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h); @@ -186,10 +184,7 @@ * \sa SDL_QueryTexture() * \sa SDL_DestroyTexture() */ -extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(Uint32 - format, - SDL_Surface - * surface); +extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTextureFromSurface(SDL_Renderer * renderer, Uint32 format, SDL_Surface * surface); /** * \brief Query the attributes of a texture @@ -419,9 +414,10 @@ * \param a The alpha value used to draw on the rendering target, usually * ::SDL_ALPHA_OPAQUE (255). * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, +extern DECLSPEC int SDL_SetRenderDrawColor(SDL_Renderer * renderer, + Uint8 r, Uint8 g, Uint8 b, Uint8 a); /** @@ -433,9 +429,10 @@ * \param a A pointer to the alpha value used to draw on the rendering target, * usually ::SDL_ALPHA_OPAQUE (255). * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, +extern DECLSPEC int SDL_GetRenderDrawColor(SDL_Renderer * renderer, + Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a); /** @@ -443,30 +440,32 @@ * * \param blendMode ::SDL_BlendMode to use for blending. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error * * \note If the blend mode is not supported, the closest supported mode is * chosen. * * \sa SDL_GetRenderDrawBlendMode() */ -extern DECLSPEC int SDLCALL SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode); +extern DECLSPEC int SDLCALL SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, + SDL_BlendMode blendMode); /** * \brief Get the blend mode used for drawing operations. * * \param blendMode A pointer filled in with the current blend mode. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error * * \sa SDL_SetRenderDrawBlendMode() */ -extern DECLSPEC int SDLCALL SDL_GetRenderDrawBlendMode(SDL_BlendMode *blendMode); +extern DECLSPEC int SDLCALL SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, + SDL_BlendMode *blendMode); /** * \brief Clear the current rendering target with the drawing color */ -extern DECLSPEC int SDLCALL SDL_RenderClear(void); +extern DECLSPEC int SDLCALL SDL_RenderClear(SDL_Renderer * renderer); /** * \brief Draw a point on the current rendering target. @@ -474,9 +473,10 @@ * \param x The x coordinate of the point. * \param y The y coordinate of the point. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderDrawPoint(int x, int y); +extern DECLSPEC int SDLCALL SDL_RenderDrawPoint(SDL_Renderer * renderer, + int x, int y); /** * \brief Draw multiple points on the current rendering target. @@ -484,9 +484,10 @@ * \param points The points to draw * \param count The number of points to draw * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderDrawPoints(const SDL_Point * points, +extern DECLSPEC int SDLCALL SDL_RenderDrawPoints(SDL_Renderer * renderer, + const SDL_Point * points, int count); /** @@ -497,9 +498,10 @@ * \param x2 The x coordinate of the end point. * \param y2 The y coordinate of the end point. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderDrawLine(int x1, int y1, int x2, int y2); +extern DECLSPEC int SDLCALL SDL_RenderDrawLine(SDL_Renderer * renderer, + int x1, int y1, int x2, int y2); /** * \brief Draw a series of connected lines on the current rendering target. @@ -507,9 +509,10 @@ * \param points The points along the lines * \param count The number of points, drawing count-1 lines * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderDrawLines(const SDL_Point * points, +extern DECLSPEC int SDLCALL SDL_RenderDrawLines(SDL_Renderer * renderer, + const SDL_Point * points, int count); /** @@ -517,9 +520,10 @@ * * \param rect A pointer to the destination rectangle, or NULL to outline the entire rendering target. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderDrawRect(const SDL_Rect * rect); +extern DECLSPEC int SDLCALL SDL_RenderDrawRect(SDL_Renderer * renderer, + const SDL_Rect * rect); /** * \brief Draw some number of rectangles on the current rendering target. @@ -527,9 +531,11 @@ * \param rects A pointer to an array of destination rectangles. * \param count The number of rectangles. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderDrawRects(const SDL_Rect ** rects, int count); +extern DECLSPEC int SDLCALL SDL_RenderDrawRects(SDL_Renderer * renderer, + const SDL_Rect ** rects, + int count); /** * \brief Fill a rectangle on the current rendering target with the drawing color. @@ -537,9 +543,10 @@ * \param rect A pointer to the destination rectangle, or NULL for the entire * rendering target. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderFillRect(const SDL_Rect * rect); +extern DECLSPEC int SDLCALL SDL_RenderFillRect(SDL_Renderer * renderer, + const SDL_Rect * rect); /** * \brief Fill some number of rectangles on the current rendering target with the drawing color. @@ -547,9 +554,11 @@ * \param rects A pointer to an array of destination rectangles. * \param count The number of rectangles. * - * \return 0 on success, or -1 if there is no rendering context current. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderFillRects(const SDL_Rect ** rect, int count); +extern DECLSPEC int SDLCALL SDL_RenderFillRects(SDL_Renderer * renderer, + const SDL_Rect ** rect, + int count); /** * \brief Copy a portion of the texture to the current rendering target. @@ -560,10 +569,10 @@ * \param dstrect A pointer to the destination rectangle, or NULL for the * entire rendering target. * - * \return 0 on success, or -1 if there is no rendering context current, or the - * driver doesn't support the requested operation. + * \return 0 on success, or -1 on error */ -extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Texture * texture, +extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_Renderer * renderer, + SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect); @@ -581,7 +590,8 @@ * * \warning This is a very slow operation, and should not be used frequently. */ -extern DECLSPEC int SDLCALL SDL_RenderReadPixels(const SDL_Rect * rect, +extern DECLSPEC int SDLCALL SDL_RenderReadPixels(SDL_Renderer * renderer, + const SDL_Rect * rect, Uint32 format, void *pixels, int pitch); @@ -599,7 +609,8 @@ * * \warning This is a very slow operation, and should not be used frequently. */ -extern DECLSPEC int SDLCALL SDL_RenderWritePixels(const SDL_Rect * rect, +extern DECLSPEC int SDLCALL SDL_RenderWritePixels(SDL_Renderer * renderer, + const SDL_Rect * rect, Uint32 format, const void *pixels, int pitch); @@ -607,7 +618,7 @@ /** * \brief Update the screen with rendering performed. */ -extern DECLSPEC void SDLCALL SDL_RenderPresent(void); +extern DECLSPEC void SDLCALL SDL_RenderPresent(SDL_Renderer * renderer); /** * \brief Destroy the specified texture. @@ -623,7 +634,7 @@ * * \sa SDL_CreateRenderer() */ -extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Window * window); +extern DECLSPEC void SDLCALL SDL_DestroyRenderer(SDL_Renderer * renderer); /* Ends C function definitions when using C++ */
--- a/src/SDL_compat.c Tue Feb 01 19:15:42 2011 -0800 +++ b/src/SDL_compat.c Tue Feb 01 19:19:43 2011 -0800 @@ -31,7 +31,7 @@ #include "video/SDL_yuv_sw_c.h" static SDL_Window *SDL_VideoWindow = NULL; -static SDL_RendererInfo SDL_VideoRendererInfo; +static SDL_Renderer *SDL_VideoRenderer = NULL; static SDL_Texture *SDL_VideoTexture = NULL; static SDL_Surface *SDL_VideoSurface = NULL; static SDL_Surface *SDL_ShadowSurface = NULL; @@ -467,7 +467,8 @@ /* Destroy the screen texture and recreate it */ SDL_QueryTexture(SDL_VideoTexture, &format, &access, &w, &h); SDL_DestroyTexture(SDL_VideoTexture); - SDL_VideoTexture = SDL_CreateTexture(format, access, width, height); + SDL_VideoTexture = SDL_CreateTexture(SDL_VideoRenderer, format, + access, width, height); if (!SDL_VideoTexture) { return -1; } @@ -667,20 +668,20 @@ } /* Create a renderer for the window */ - if (SDL_CreateRenderer(SDL_VideoWindow, -1, 0) < 0) { + SDL_VideoRenderer = SDL_CreateRenderer(SDL_VideoWindow, -1, 0); + if (!SDL_VideoRenderer) { return NULL; } - SDL_GetRendererInfo(&SDL_VideoRendererInfo); /* Create a texture for the screen surface */ - SDL_VideoTexture = - SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_STREAMING, width, - height); + SDL_VideoTexture = SDL_CreateTexture(SDL_VideoRenderer, desired_format, + SDL_TEXTUREACCESS_STREAMING, + width, height); if (!SDL_VideoTexture) { - SDL_VideoTexture = - SDL_CreateTexture(desktop_format, - SDL_TEXTUREACCESS_STREAMING, width, height); + SDL_VideoTexture = SDL_CreateTexture(SDL_VideoRenderer, desktop_format, + SDL_TEXTUREACCESS_STREAMING, + width, height); } if (!SDL_VideoTexture) { return NULL; @@ -890,8 +891,8 @@ rect.y = 0; rect.w = screen->w; rect.h = screen->h; - SDL_RenderCopy(SDL_VideoTexture, &rect, &rect); - SDL_RenderPresent(); + SDL_RenderCopy(SDL_VideoRenderer, SDL_VideoTexture, &rect, &rect); + SDL_RenderPresent(SDL_VideoRenderer); } } @@ -1584,7 +1585,8 @@ } overlay->hwdata->texture = - SDL_CreateTexture(texture_format, SDL_TEXTUREACCESS_STREAMING, w, h); + SDL_CreateTexture(SDL_VideoRenderer, texture_format, + SDL_TEXTUREACCESS_STREAMING, w, h); if (overlay->hwdata->texture) { overlay->hwdata->sw = NULL; } else { @@ -1600,7 +1602,7 @@ SDL_GetCurrentDisplayMode(¤t_mode); texture_format = current_mode.format; overlay->hwdata->texture = - SDL_CreateTexture(texture_format, + SDL_CreateTexture(SDL_VideoRenderer, texture_format, SDL_TEXTUREACCESS_STREAMING, w, h); } if (!overlay->hwdata->texture) { @@ -1688,10 +1690,10 @@ SDL_SetError("Passed a NULL overlay or dstrect"); return -1; } - if (SDL_RenderCopy(overlay->hwdata->texture, NULL, dstrect) < 0) { + if (SDL_RenderCopy(SDL_VideoRenderer, overlay->hwdata->texture, NULL, dstrect) < 0) { return -1; } - SDL_RenderPresent(); + SDL_RenderPresent(SDL_VideoRenderer); return 0; }
--- a/src/events/SDL_windowevents.c Tue Feb 01 19:15:42 2011 -0800 +++ b/src/events/SDL_windowevents.c Tue Feb 01 19:19:43 2011 -0800 @@ -106,7 +106,6 @@ } window->w = data1; window->h = data2; - SDL_OnWindowResized(window); break; case SDL_WINDOWEVENT_MINIMIZED: if (window->flags & SDL_WINDOW_MINIMIZED) {
--- a/src/video/SDL_renderer_gl.c Tue Feb 01 19:15:42 2011 -0800 +++ b/src/video/SDL_renderer_gl.c Tue Feb 01 19:19:43 2011 -0800 @@ -66,8 +66,8 @@ static const float inv255f = 1.0f / 255.0f; static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags); -static int GL_ActivateRenderer(SDL_Renderer * renderer); -static int GL_DisplayModeChanged(SDL_Renderer * renderer); +static void GL_WindowEvent(SDL_Renderer * renderer, + const SDL_WindowEvent *event); static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); static int GL_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture, void **pixels, @@ -277,8 +277,7 @@ return NULL; } - renderer->ActivateRenderer = GL_ActivateRenderer; - renderer->DisplayModeChanged = GL_DisplayModeChanged; + renderer->WindowEvent = GL_WindowEvent; renderer->CreateTexture = GL_CreateTexture; renderer->QueryTexturePixels = GL_QueryTexturePixels; renderer->SetTexturePalette = GL_SetTexturePalette; @@ -408,14 +407,19 @@ return renderer; } +static SDL_GLContext SDL_CurrentContext = NULL; + static int GL_ActivateRenderer(SDL_Renderer * renderer) { GL_RenderData *data = (GL_RenderData *) renderer->driverdata; SDL_Window *window = renderer->window; - if (SDL_GL_MakeCurrent(window, data->context) < 0) { - return -1; + if (SDL_CurrentContext != data->context) { + if (SDL_GL_MakeCurrent(window, data->context) < 0) { + return -1; + } + SDL_CurrentContext = data->context; } if (data->updateSize) { data->glMatrixMode(GL_PROJECTION); @@ -430,14 +434,16 @@ return 0; } -static int -GL_DisplayModeChanged(SDL_Renderer * renderer) +static void +GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event) { GL_RenderData *data = (GL_RenderData *) renderer->driverdata; - /* Rebind the context to the window area and update matrices */ - data->updateSize = SDL_TRUE; - return GL_ActivateRenderer(renderer); + if (event->event == SDL_WINDOWEVENT_RESIZED) { + /* Rebind the context to the window area and update matrices */ + SDL_CurrentContext = NULL; + data->updateSize = SDL_TRUE; + } } static __inline__ int @@ -717,6 +723,8 @@ GLuint shader = 0; GLenum result; + GL_ActivateRenderer(renderer); + if (!convert_format(renderdata, texture->format, &internalFormat, &format, &type)) { SDL_SetError("Texture format %s not supported by OpenGL", @@ -874,6 +882,8 @@ GL_TextureData *data = (GL_TextureData *) texture->driverdata; Uint8 *palette; + GL_ActivateRenderer(renderer); + if (!data->palette) { SDL_SetError("Texture doesn't have a palette"); return -1; @@ -938,6 +948,8 @@ GL_TextureData *data = (GL_TextureData *) texture->driverdata; GLenum result; + GL_ActivateRenderer(renderer); + renderdata->glGetError(); SetupTextureUpdate(renderdata, texture, pitch); renderdata->glEnable(data->type); @@ -1018,6 +1030,8 @@ { GL_RenderData *data = (GL_RenderData *) renderer->driverdata; + GL_ActivateRenderer(renderer); + data->glClearColor((GLfloat) renderer->r * inv255f, (GLfloat) renderer->g * inv255f, (GLfloat) renderer->b * inv255f, @@ -1035,6 +1049,8 @@ GL_RenderData *data = (GL_RenderData *) renderer->driverdata; int i; + GL_ActivateRenderer(renderer); + GL_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -1058,6 +1074,8 @@ GL_RenderData *data = (GL_RenderData *) renderer->driverdata; int i; + GL_ActivateRenderer(renderer); + GL_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -1126,6 +1144,8 @@ GL_RenderData *data = (GL_RenderData *) renderer->driverdata; int i, x, y; + GL_ActivateRenderer(renderer); + GL_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -1164,6 +1184,8 @@ GL_RenderData *data = (GL_RenderData *) renderer->driverdata; int i; + GL_ActivateRenderer(renderer); + GL_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -1189,6 +1211,8 @@ int minx, miny, maxx, maxy; GLfloat minu, maxu, minv, maxv; + GL_ActivateRenderer(renderer); + if (texturedata->dirty.list) { SDL_DirtyRect *dirty; void *pixels; @@ -1276,6 +1300,8 @@ Uint8 *src, *dst, *tmp; int length, rows; + GL_ActivateRenderer(renderer); + if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { /* FIXME: Do a temp copy to a format that is supported */ SDL_SetError("Unsupported pixel format"); @@ -1323,6 +1349,8 @@ Uint8 *src, *dst, *tmp; int length, rows; + GL_ActivateRenderer(renderer); + if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { /* FIXME: Do a temp copy to a format that is supported */ SDL_SetError("Unsupported pixel format"); @@ -1360,6 +1388,8 @@ static void GL_RenderPresent(SDL_Renderer * renderer) { + GL_ActivateRenderer(renderer); + SDL_GL_SwapWindow(renderer->window); } @@ -1369,6 +1399,8 @@ GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; GL_TextureData *data = (GL_TextureData *) texture->driverdata; + GL_ActivateRenderer(renderer); + if (!data) { return; }
--- a/src/video/SDL_renderer_gles.c Tue Feb 01 19:15:42 2011 -0800 +++ b/src/video/SDL_renderer_gles.c Tue Feb 01 19:19:43 2011 -0800 @@ -54,8 +54,8 @@ static const float inv255f = 1.0f / 255.0f; static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags); -static int GLES_ActivateRenderer(SDL_Renderer * renderer); -static int GLES_DisplayModeChanged(SDL_Renderer * renderer); +static void GLES_WindowEvent(SDL_Renderer * renderer, + const SDL_WindowEvent *event); static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); static int GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture, void **pixels, @@ -218,8 +218,7 @@ return NULL; } - renderer->ActivateRenderer = GLES_ActivateRenderer; - renderer->DisplayModeChanged = GLES_DisplayModeChanged; + renderer->WindowEvent = GLES_WindowEvent; renderer->CreateTexture = GLES_CreateTexture; renderer->QueryTexturePixels = GLES_QueryTexturePixels; renderer->SetTexturePalette = GLES_SetTexturePalette; @@ -311,6 +310,8 @@ return renderer; } +static SDL_GLContext SDL_CurrentContext = NULL; + static int GLES_ActivateRenderer(SDL_Renderer * renderer) { @@ -318,8 +319,11 @@ GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; SDL_Window *window = renderer->window; - if (SDL_GL_MakeCurrent(window, data->context) < 0) { - return -1; + if (SDL_CurrentContext != data->context) { + if (SDL_GL_MakeCurrent(window, data->context) < 0) { + return -1; + } + SDL_CurrentContext = data->context; } if (data->updateSize) { data->glMatrixMode(GL_PROJECTION); @@ -334,13 +338,16 @@ return 0; } -static int -GLES_DisplayModeChanged(SDL_Renderer * renderer) +static void +GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event) { GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; - data->updateSize = SDL_TRUE; - return 0; + if (event->event == SDL_WINDOWEVENT_RESIZED) { + /* Rebind the context to the window area and update matrices */ + SDL_CurrentContext = NULL; + data->updateSize = SDL_TRUE; + } } static __inline__ int @@ -364,6 +371,8 @@ int texture_w, texture_h; GLenum result; + GLES_ActivateRenderer(renderer); + switch (texture->format) { case SDL_PIXELFORMAT_RGB24: internalFormat = GL_RGB; @@ -498,6 +507,8 @@ void * temp_ptr; int i; + GLES_ActivateRenderer(renderer); + renderdata->glGetError(); renderdata->glEnable(data->type); SetupTextureUpdate(renderdata, texture, pitch); @@ -599,6 +610,8 @@ int i; GLshort *vertices; + GLES_ActivateRenderer(renderer); + GLES_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -626,6 +639,8 @@ int i; GLshort *vertices; + GLES_ActivateRenderer(renderer); + GLES_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -659,6 +674,8 @@ GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; int i; + GLES_ActivateRenderer(renderer); + GLES_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -696,6 +713,8 @@ GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; int i; + GLES_ActivateRenderer(renderer); + GLES_SetBlendMode(data, renderer->blendMode); data->glColor4f((GLfloat) renderer->r * inv255f, @@ -739,6 +758,8 @@ void *temp_buffer; /* used for reformatting dirty rect pixels */ void *temp_ptr; + GLES_ActivateRenderer(renderer); + data->glEnable(GL_TEXTURE_2D); if (texturedata->dirty.list) { @@ -859,6 +880,8 @@ static void GLES_RenderPresent(SDL_Renderer * renderer) { + GLES_ActivateRenderer(renderer); + SDL_GL_SwapWindow(renderer->window); } @@ -867,6 +890,8 @@ { GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; + GLES_ActivateRenderer(renderer); + if (!data) { return; }
--- a/src/video/SDL_renderer_sw.c Tue Feb 01 19:15:42 2011 -0800 +++ b/src/video/SDL_renderer_sw.c Tue Feb 01 19:19:43 2011 -0800 @@ -32,8 +32,8 @@ /* SDL surface based renderer implementation */ static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags); -static int SW_ActivateRenderer(SDL_Renderer * renderer); -static int SW_DisplayModeChanged(SDL_Renderer * renderer); +static void SW_WindowEvent(SDL_Renderer * renderer, + const SDL_WindowEvent *event); static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); static int SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture, void **pixels, @@ -212,8 +212,7 @@ SDL_OutOfMemory(); return NULL; } - renderer->ActivateRenderer = SW_ActivateRenderer; - renderer->DisplayModeChanged = SW_DisplayModeChanged; + renderer->WindowEvent = SW_WindowEvent; renderer->RenderDrawPoints = SW_RenderDrawPoints; renderer->RenderDrawLines = SW_RenderDrawLines; @@ -287,47 +286,34 @@ return renderer; } -static int +static SDL_Texture * SW_ActivateRenderer(SDL_Renderer * renderer) { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; SDL_Window *window = renderer->window; - int i, n; - if (data->renderer && data->renderer->ActivateRenderer) { - if (data->renderer->ActivateRenderer(data->renderer) < 0) { - return -1; - } - } if (data->updateSize) { /* Recreate the textures for the new window size */ if (data->texture) { DestroyTexture(data->renderer, data->texture); - data->texture = 0; } data->texture = CreateTexture(data->renderer, data->format, window->w, window->h); - if (!data->texture) { - return -1; + if (data->texture) { + data->updateSize = SDL_FALSE; } - data->updateSize = SDL_FALSE; } - return 0; + return data->texture; } -static int -SW_DisplayModeChanged(SDL_Renderer * renderer) +static void +SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event) { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; - if (data->renderer && data->renderer->DisplayModeChanged) { - if (data->renderer->DisplayModeChanged(data->renderer) < 0) { - return -1; - } + if (event->event == SDL_WINDOWEVENT_RESIZED) { + data->updateSize = SDL_TRUE; } - /* Rebind the context to the window area */ - data->updateSize = SDL_TRUE; - return SW_ActivateRenderer(renderer); } static int @@ -496,12 +482,16 @@ int count) { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; - SDL_Texture *texture = data->texture; + SDL_Texture *texture = SW_ActivateRenderer(renderer); SDL_Rect rect; int i; int x, y; int status = 0; + if (!texture) { + return -1; + } + /* Get the smallest rectangle that contains everything */ rect.x = 0; rect.y = 0; @@ -555,12 +545,16 @@ int count) { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; - SDL_Texture *texture = data->texture; + SDL_Texture *texture = SW_ActivateRenderer(renderer); SDL_Rect clip, rect; int i; int x1, y1, x2, y2; int status = 0; + if (!texture) { + return -1; + } + /* Get the smallest rectangle that contains everything */ clip.x = 0; clip.y = 0; @@ -619,12 +613,16 @@ int count) { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; - SDL_Texture *texture = data->texture; + SDL_Texture *texture = SW_ActivateRenderer(renderer); SDL_Rect clip, rect; Uint32 color = 0; int i; int status = 0; + if (!texture) { + return -1; + } + clip.x = 0; clip.y = 0; clip.w = texture->w; @@ -671,12 +669,16 @@ int count) { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; - SDL_Texture *texture = data->texture; + SDL_Texture *texture = SW_ActivateRenderer(renderer); SDL_Rect clip, rect; Uint32 color = 0; int i; int status = 0; + if (!texture) { + return -1; + } + clip.x = 0; clip.y = 0; clip.w = texture->w; @@ -724,6 +726,10 @@ SW_RenderData *data = (SW_RenderData *) renderer->driverdata; int status; + if (!SW_ActivateRenderer(renderer)) { + return -1; + } + if (data->renderer->LockTexture(data->renderer, data->texture, dstrect, 1, &data->surface.pixels, &data->surface.pitch) < 0) { @@ -760,6 +766,10 @@ { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; + if (!SW_ActivateRenderer(renderer)) { + return -1; + } + if (data->renderer->LockTexture(data->renderer, data->texture, rect, 0, &data->surface.pixels, &data->surface.pitch) < 0) { @@ -780,6 +790,10 @@ { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; + if (!SW_ActivateRenderer(renderer)) { + return -1; + } + if (data->renderer->LockTexture(data->renderer, data->texture, rect, 1, &data->surface.pixels, &data->surface.pitch) < 0) { @@ -797,9 +811,13 @@ SW_RenderPresent(SDL_Renderer * renderer) { SW_RenderData *data = (SW_RenderData *) renderer->driverdata; - SDL_Texture *texture = data->texture; + SDL_Texture *texture = SW_ActivateRenderer(renderer); SDL_Rect rect; + if (!texture) { + return; + } + /* Send the data to the display */ rect.x = 0; rect.y = 0;
--- a/src/video/SDL_sysvideo.h Tue Feb 01 19:15:42 2011 -0800 +++ b/src/video/SDL_sysvideo.h Tue Feb 01 19:19:43 2011 -0800 @@ -24,6 +24,7 @@ #ifndef _SDL_sysvideo_h #define _SDL_sysvideo_h +#include "SDL_events.h" #include "SDL_mouse.h" #include "SDL_keysym.h" #include "SDL_render.h" @@ -31,7 +32,6 @@ /* The SDL video driver */ -typedef struct SDL_Renderer SDL_Renderer; typedef struct SDL_RenderDriver SDL_RenderDriver; typedef struct SDL_WindowShaper SDL_WindowShaper; typedef struct SDL_ShapeDriver SDL_ShapeDriver; @@ -61,8 +61,9 @@ /* Define the SDL renderer structure */ struct SDL_Renderer { - int (*ActivateRenderer) (SDL_Renderer * renderer); - int (*DisplayModeChanged) (SDL_Renderer * renderer); + const void *magic; + + void (*WindowEvent) (SDL_Renderer * renderer, const SDL_WindowEvent *event); int (*CreateTexture) (SDL_Renderer * renderer, SDL_Texture * texture); int (*QueryTexturePixels) (SDL_Renderer * renderer, SDL_Texture * texture, void **pixels, int *pitch); @@ -168,7 +169,6 @@ Uint32 flags; SDL_VideoDisplay *display; - SDL_Renderer *renderer; SDL_DisplayMode fullscreen_mode; @@ -208,8 +208,6 @@ SDL_Window *windows; SDL_Window *fullscreen_window; - SDL_Renderer *current_renderer; - SDL_VideoDevice *device; void *driverdata; @@ -344,6 +342,7 @@ SDL_VideoDisplay *displays; int current_display; Uint8 window_magic; + Uint8 renderer_magic; Uint8 texture_magic; Uint32 next_object_id; char * clipboard_text; @@ -439,7 +438,6 @@ #endif #define SDL_CurrentDisplay (&_this->displays[_this->current_display]) -#define SDL_CurrentRenderer (SDL_CurrentDisplay->current_renderer) extern SDL_VideoDevice *SDL_GetVideoDevice(void); extern int SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode); @@ -461,7 +459,6 @@ extern void SDL_OnWindowShown(SDL_Window * window); extern void SDL_OnWindowHidden(SDL_Window * window); -extern void SDL_OnWindowResized(SDL_Window * window); extern void SDL_OnWindowMinimized(SDL_Window * window); extern void SDL_OnWindowRestored(SDL_Window * window); extern void SDL_OnWindowFocusGained(SDL_Window * window);
--- a/src/video/SDL_video.c Tue Feb 01 19:15:42 2011 -0800 +++ b/src/video/SDL_video.c Tue Feb 01 19:19:43 2011 -0800 @@ -109,6 +109,12 @@ return retval; \ } +#define CHECK_RENDERER_MAGIC(renderer, retval) \ + if (!renderer || renderer->magic != &_this->renderer_magic) { \ + SDL_SetError("Invalid renderer"); \ + return retval; \ + } + #define CHECK_TEXTURE_MAGIC(texture, retval) \ if (!_this) { \ SDL_UninitializedVideo(); \ @@ -1023,33 +1029,6 @@ return 0; } -static __inline__ SDL_Renderer * -SDL_GetCurrentRenderer(SDL_bool create) -{ - if (!_this) { - SDL_UninitializedVideo(); - return NULL; - } - if (!SDL_CurrentRenderer) { - SDL_Window *window = NULL; - - if (!create) { - SDL_SetError("Use SDL_CreateRenderer() to create a renderer"); - return NULL; - } - - /* Get the first window on the first display */ - if (_this->num_displays > 0) { - window = _this->displays[0].windows; - } - - if (SDL_CreateRenderer(window, -1, 0) < 0) { - return NULL; - } - } - return SDL_CurrentRenderer; -} - Uint32 SDL_GetWindowID(SDL_Window * window) { @@ -1184,7 +1163,7 @@ if (_this->SetWindowSize) { _this->SetWindowSize(_this, window); } - SDL_OnWindowResized(window); + SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h); } void @@ -1367,16 +1346,6 @@ } void -SDL_OnWindowResized(SDL_Window * window) -{ - SDL_Renderer *renderer = window->renderer; - - if (renderer && renderer->DisplayModeChanged) { - renderer->DisplayModeChanged(renderer); - } -} - -void SDL_OnWindowMinimized(SDL_Window * window) { SDL_UpdateFullscreenMode(window, SDL_FALSE); @@ -1451,9 +1420,6 @@ if (window->title) { SDL_free(window->title); } - if (window->renderer) { - SDL_DestroyRenderer(window); - } /* Restore video mode, etc. */ SDL_UpdateFullscreenMode(window, SDL_FALSE); @@ -1523,13 +1489,26 @@ return 0; } -int +static int +SDL_RendererEventWatch(void *userdata, SDL_Event *event) +{ + SDL_Renderer *renderer = (SDL_Renderer *)userdata; + + if (event->type == SDL_WINDOWEVENT && renderer->WindowEvent) { + SDL_Window *window = SDL_GetWindowFromID(event->window.windowID); + if (window == renderer->window) { + renderer->WindowEvent(renderer, &event->window); + } + } + return 0; +} + +SDL_Renderer * SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags) { - CHECK_WINDOW_MAGIC(window, -1); - - /* Free any existing renderer */ - SDL_DestroyRenderer(window); + SDL_Renderer *renderer = NULL; + + CHECK_WINDOW_MAGIC(window, NULL); if (index < 0) { char *override = SDL_getenv("SDL_VIDEO_RENDERER"); @@ -1552,7 +1531,7 @@ if (SDL_strcasecmp(override, driver->info.name) == 0) { /* Create a new renderer instance */ - window->renderer = driver->CreateRenderer(window, flags); + renderer = driver->CreateRenderer(window, flags); break; } } @@ -1563,8 +1542,8 @@ if ((driver->info.flags & flags) == flags) { /* Create a new renderer instance */ - window->renderer = driver->CreateRenderer(window, flags); - if (window->renderer) { + renderer = driver->CreateRenderer(window, flags); + if (renderer) { /* Yay, we got one! */ break; } @@ -1573,74 +1552,42 @@ } if (index == n) { SDL_SetError("Couldn't find matching render driver"); - return -1; + return NULL; } } else { if (index >= SDL_GetNumRenderDrivers()) { SDL_SetError("index must be -1 or in the range of 0 - %d", SDL_GetNumRenderDrivers() - 1); - return -1; + return NULL; } /* Create a new renderer instance */ - window->renderer = SDL_CurrentDisplay->render_drivers[index].CreateRenderer(window, flags); + renderer = SDL_CurrentDisplay->render_drivers[index].CreateRenderer(window, flags); } - if (window->renderer == NULL) { - /* Assuming renderer set its error */ - return -1; + if (renderer) { + renderer->magic = &_this->renderer_magic; + + SDL_AddEventWatch(SDL_RendererEventWatch, renderer); } - - SDL_SelectRenderer(window); - - return 0; + return renderer; } int -SDL_SelectRenderer(SDL_Window * window) +SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info) { - SDL_Renderer *renderer; - - CHECK_WINDOW_MAGIC(window, -1); - - renderer = window->renderer; - if (!renderer) { - SDL_SetError("Use SDL_CreateRenderer() to create a renderer"); - return -1; - } - if (renderer->ActivateRenderer) { - if (renderer->ActivateRenderer(renderer) < 0) { - return -1; - } - } - SDL_CurrentDisplay->current_renderer = renderer; - return 0; -} - -int -SDL_GetRendererInfo(SDL_RendererInfo * info) -{ - SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + *info = renderer->info; return 0; } SDL_Texture * -SDL_CreateTexture(Uint32 format, int access, int w, int h) +SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h) { - SDL_Renderer *renderer; SDL_Texture *texture; - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return 0; - } - if (!renderer->CreateTexture) { - SDL_Unsupported(); - return 0; - } + CHECK_RENDERER_MAGIC(renderer, NULL); + if (w <= 0 || h <= 0) { SDL_SetError("Texture dimensions can't be 0"); return 0; @@ -1674,26 +1621,22 @@ } SDL_Texture * -SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface) +SDL_CreateTextureFromSurface(SDL_Renderer * renderer, Uint32 format, SDL_Surface * surface) { SDL_Texture *texture; Uint32 requested_format = format; SDL_PixelFormat *fmt; - SDL_Renderer *renderer; int bpp; Uint32 Rmask, Gmask, Bmask, Amask; + CHECK_RENDERER_MAGIC(renderer, NULL); + if (!surface) { SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface"); - return 0; + return NULL; } fmt = surface->format; - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return 0; - } - if (format) { if (!SDL_PixelFormatEnumToMasks (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) { @@ -1902,15 +1845,14 @@ } texture = - SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w, - surface->h); + SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC, + surface->w, surface->h); if (!texture && !requested_format) { SDL_DisplayMode desktop_mode; SDL_GetDesktopDisplayMode(&desktop_mode); format = desktop_mode.format; - texture = - SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w, - surface->h); + texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC, + surface->w, surface->h); } if (!texture) { return 0; @@ -2246,14 +2188,11 @@ } int -SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) +SDL_SetRenderDrawColor(SDL_Renderer * renderer, + Uint8 r, Uint8 g, Uint8 b, Uint8 a) { - SDL_Renderer *renderer; - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + renderer->r = r; renderer->g = g; renderer->b = b; @@ -2262,14 +2201,11 @@ } int -SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a) +SDL_GetRenderDrawColor(SDL_Renderer * renderer, + Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a) { - SDL_Renderer *renderer; - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + if (r) { *r = renderer->r; } @@ -2286,52 +2222,40 @@ } int -SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode) +SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode) { - SDL_Renderer *renderer; - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + renderer->blendMode = blendMode; return 0; } int -SDL_GetRenderDrawBlendMode(SDL_BlendMode *blendMode) +SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode) { - SDL_Renderer *renderer; - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + *blendMode = renderer->blendMode; return 0; } int -SDL_RenderClear() +SDL_RenderClear(SDL_Renderer * renderer) { - SDL_Renderer *renderer; - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + if (!renderer->RenderClear) { SDL_BlendMode blendMode = renderer->blendMode; int status; if (blendMode >= SDL_BLENDMODE_BLEND) { - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); } - status = SDL_RenderFillRect(NULL); + status = SDL_RenderFillRect(renderer, NULL); if (blendMode >= SDL_BLENDMODE_BLEND) { - SDL_SetRenderDrawBlendMode(blendMode); + SDL_SetRenderDrawBlendMode(renderer, blendMode); } return status; } @@ -2339,33 +2263,25 @@ } int -SDL_RenderDrawPoint(int x, int y) +SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y) { SDL_Point point; point.x = x; point.y = y; - return SDL_RenderDrawPoints(&point, 1); + return SDL_RenderDrawPoints(renderer, &point, 1); } int -SDL_RenderDrawPoints(const SDL_Point * points, int count) +SDL_RenderDrawPoints(SDL_Renderer * renderer, + const SDL_Point * points, int count) { - SDL_Renderer *renderer; + CHECK_RENDERER_MAGIC(renderer, -1); if (!points) { SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points"); return -1; } - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } - if (!renderer->RenderDrawPoints) { - SDL_Unsupported(); - return -1; - } if (count < 1) { return 0; } @@ -2373,7 +2289,7 @@ } int -SDL_RenderDrawLine(int x1, int y1, int x2, int y2) +SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2) { SDL_Point points[2]; @@ -2381,27 +2297,19 @@ points[0].y = y1; points[1].x = x2; points[1].y = y2; - return SDL_RenderDrawLines(points, 2); + return SDL_RenderDrawLines(renderer, points, 2); } int -SDL_RenderDrawLines(const SDL_Point * points, int count) +SDL_RenderDrawLines(SDL_Renderer * renderer, + const SDL_Point * points, int count) { - SDL_Renderer *renderer; + CHECK_RENDERER_MAGIC(renderer, -1); if (!points) { SDL_SetError("SDL_RenderDrawLines(): Passed NULL points"); return -1; } - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } - if (!renderer->RenderDrawLines) { - SDL_Unsupported(); - return -1; - } if (count < 2) { return 0; } @@ -2409,33 +2317,27 @@ } int -SDL_RenderDrawRect(const SDL_Rect * rect) +SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect) { - return SDL_RenderDrawRects(&rect, 1); + return SDL_RenderDrawRects(renderer, &rect, 1); } int -SDL_RenderDrawRects(const SDL_Rect ** rects, int count) +SDL_RenderDrawRects(SDL_Renderer * renderer, + const SDL_Rect ** rects, int count) { - SDL_Renderer *renderer; int i; + CHECK_RENDERER_MAGIC(renderer, -1); + if (!rects) { SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects"); return -1; } - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } - if (!renderer->RenderDrawRects) { - SDL_Unsupported(); - return -1; - } if (count < 1) { return 0; } + /* Check for NULL rect, which means fill entire window */ for (i = 0; i < count; ++i) { if (rects[i] == NULL) { @@ -2455,33 +2357,27 @@ } int -SDL_RenderFillRect(const SDL_Rect * rect) +SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect) { - return SDL_RenderFillRects(&rect, 1); + return SDL_RenderFillRects(renderer, &rect, 1); } int -SDL_RenderFillRects(const SDL_Rect ** rects, int count) +SDL_RenderFillRects(SDL_Renderer * renderer, + const SDL_Rect ** rects, int count) { - SDL_Renderer *renderer; int i; + CHECK_RENDERER_MAGIC(renderer, -1); + if (!rects) { SDL_SetError("SDL_RenderFillRects(): Passed NULL rects"); return -1; } - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } - if (!renderer->RenderFillRects) { - SDL_Unsupported(); - return -1; - } if (count < 1) { return 0; } + /* Check for NULL rect, which means fill entire window */ for (i = 0; i < count; ++i) { if (rects[i] == NULL) { @@ -2501,28 +2397,20 @@ } int -SDL_RenderCopy(SDL_Texture * texture, const SDL_Rect * srcrect, - const SDL_Rect * dstrect) +SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, + const SDL_Rect * srcrect, const SDL_Rect * dstrect) { - SDL_Renderer *renderer; SDL_Window *window; SDL_Rect real_srcrect; SDL_Rect real_dstrect; + CHECK_RENDERER_MAGIC(renderer, -1); CHECK_TEXTURE_MAGIC(texture, -1); - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } - if (texture->renderer != renderer) { + if (renderer != texture->renderer) { SDL_SetError("Texture was not created with this renderer"); return -1; } - if (!renderer->RenderCopy) { - SDL_Unsupported(); - return -1; - } window = renderer->window; real_srcrect.x = 0; @@ -2563,17 +2451,14 @@ } int -SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format, - void * pixels, int pitch) +SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, + Uint32 format, void * pixels, int pitch) { - SDL_Renderer *renderer; SDL_Window *window; SDL_Rect real_rect; - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + if (!renderer->RenderReadPixels) { SDL_Unsupported(); return -1; @@ -2607,17 +2492,14 @@ } int -SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format, - const void * pixels, int pitch) +SDL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect, + Uint32 format, const void * pixels, int pitch) { - SDL_Renderer *renderer; SDL_Window *window; SDL_Rect real_rect; - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer) { - return -1; - } + CHECK_RENDERER_MAGIC(renderer, -1); + if (!renderer->RenderWritePixels) { SDL_Unsupported(); return -1; @@ -2651,14 +2533,10 @@ } void -SDL_RenderPresent(void) +SDL_RenderPresent(SDL_Renderer * renderer) { - SDL_Renderer *renderer; - - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if (!renderer || !renderer->RenderPresent) { - return; - } + CHECK_RENDERER_MAGIC(renderer, ); + #if SDL_VIDEO_DRIVER_WINDOWS IME_Present((SDL_VideoData *)_this->driverdata); #endif @@ -2688,30 +2566,22 @@ } void -SDL_DestroyRenderer(SDL_Window * window) +SDL_DestroyRenderer(SDL_Renderer * renderer) { - SDL_Renderer *renderer; - - CHECK_WINDOW_MAGIC(window, ); - - renderer = window->renderer; - if (!renderer) { - return; - } + CHECK_RENDERER_MAGIC(renderer, ); + + SDL_DelEventWatch(SDL_RendererEventWatch, renderer); /* Free existing textures for this renderer */ while (renderer->textures) { SDL_DestroyTexture(renderer->textures); } + /* It's no longer magical... */ + renderer->magic = NULL; + /* Free the renderer instance */ renderer->DestroyRenderer(renderer); - - /* Clear references */ - window->renderer = NULL; - if (SDL_CurrentDisplay->current_renderer == renderer) { - SDL_CurrentDisplay->current_renderer = NULL; - } } SDL_bool
--- a/test/common.c Tue Feb 01 19:15:42 2011 -0800 +++ b/test/common.c Tue Feb 01 19:19:43 2011 -0800 @@ -646,7 +646,10 @@ state->windows = (SDL_Window **) SDL_malloc(state->num_windows * sizeof(*state->windows)); - if (!state->windows) { + state->renderers = + (SDL_Renderer **) SDL_malloc(state->num_windows * + sizeof(*state->renderers)); + if (!state->windows || !state->renderers) { fprintf(stderr, "Out of memory!\n"); return SDL_FALSE; } @@ -685,6 +688,8 @@ SDL_ShowWindow(state->windows[i]); + state->renderers[i] = NULL; + if (!state->skip_renderer && (state->renderdriver || !(state->window_flags & SDL_WINDOW_OPENGL))) { @@ -707,8 +712,9 @@ return SDL_FALSE; } } - if (SDL_CreateRenderer - (state->windows[i], m, state->render_flags) < 0) { + state->renderers[i] = SDL_CreateRenderer(state->windows[i], + m, state->render_flags); + if (!state->renderers[i]) { fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError()); return SDL_FALSE; @@ -717,12 +723,11 @@ SDL_RendererInfo info; fprintf(stderr, "Current renderer:\n"); - SDL_GetRendererInfo(&info); + SDL_GetRendererInfo(state->renderers[i], &info); PrintRenderer(&info); } } } - SDL_SelectRenderer(state->windows[0]); } if (state->flags & SDL_INIT_AUDIO) { @@ -1012,15 +1017,25 @@ void CommonQuit(CommonState * state) { + int i; + + if (state->windows) { + SDL_free(state->windows); + } + if (state->renderers) { + for (i = 0; i < state->num_windows; ++i) { + if (state->renderers[i]) { + SDL_DestroyRenderer(state->renderers[i]); + } + } + SDL_free(state->renderers); + } if (state->flags & SDL_INIT_VIDEO) { SDL_VideoQuit(); } if (state->flags & SDL_INIT_AUDIO) { SDL_AudioQuit(); } - if (state->windows) { - SDL_free(state->windows); - } SDL_free(state); }
--- a/test/common.h Tue Feb 01 19:15:42 2011 -0800 +++ b/test/common.h Tue Feb 01 19:19:43 2011 -0800 @@ -39,6 +39,7 @@ const char *renderdriver; Uint32 render_flags; SDL_bool skip_renderer; + SDL_Renderer **renderers; /* Audio info */ const char *audiodriver;
--- a/test/testdraw2.c Tue Feb 01 19:15:42 2011 -0800 +++ b/test/testdraw2.c Tue Feb 01 19:19:43 2011 -0800 @@ -19,7 +19,7 @@ static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; void -DrawPoints(SDL_Window * window) +DrawPoints(SDL_Window * window, SDL_Renderer * renderer) { int i; int x, y; @@ -28,7 +28,6 @@ /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); - SDL_SetRenderDrawBlendMode(blendMode); for (i = 0; i < num_objects * 4; ++i) { /* Cycle the color and alpha, if desired */ if (cycle_color) { @@ -53,18 +52,17 @@ cycle_direction = -cycle_direction; } } - SDL_SetRenderDrawColor(255, (Uint8) current_color, + SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, (Uint8) current_color, (Uint8) current_alpha); x = rand() % window_w; y = rand() % window_h; - SDL_RenderDrawPoint(x, y); + SDL_RenderDrawPoint(renderer, x, y); } - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } void -DrawLines(SDL_Window * window) +DrawLines(SDL_Window * window, SDL_Renderer * renderer) { int i; int x1, y1, x2, y2; @@ -73,7 +71,6 @@ /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); - SDL_SetRenderDrawBlendMode(blendMode); for (i = 0; i < num_objects; ++i) { /* Cycle the color and alpha, if desired */ if (cycle_color) { @@ -98,27 +95,26 @@ cycle_direction = -cycle_direction; } } - SDL_SetRenderDrawColor(255, (Uint8) current_color, + SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, (Uint8) current_color, (Uint8) current_alpha); if (i == 0) { - SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1); - SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0); - SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2); - SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1); + SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1); + SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0); + SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2); + SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1); } else { x1 = (rand() % (window_w*2)) - window_w; x2 = (rand() % (window_w*2)) - window_w; y1 = (rand() % (window_h*2)) - window_h; y2 = (rand() % (window_h*2)) - window_h; - SDL_RenderDrawLine(x1, y1, x2, y2); + SDL_RenderDrawLine(renderer, x1, y1, x2, y2); } } - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } void -DrawRects(SDL_Window * window) +DrawRects(SDL_Window * window, SDL_Renderer * renderer) { int i; SDL_Rect rect; @@ -127,7 +123,6 @@ /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); - SDL_SetRenderDrawBlendMode(blendMode); for (i = 0; i < num_objects / 4; ++i) { /* Cycle the color and alpha, if desired */ if (cycle_color) { @@ -152,16 +147,15 @@ cycle_direction = -cycle_direction; } } - SDL_SetRenderDrawColor(255, (Uint8) current_color, + SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, (Uint8) current_color, (Uint8) current_alpha); rect.w = rand() % (window_h / 2); rect.h = rand() % (window_h / 2); rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2); rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2); - SDL_RenderFillRect(&rect); + SDL_RenderFillRect(renderer, &rect); } - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } int @@ -223,9 +217,10 @@ /* Create the windows and initialize the renderers */ for (i = 0; i < state->num_windows; ++i) { - SDL_SelectRenderer(state->windows[i]); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); + SDL_Renderer *renderer = state->renderers[i]; + SDL_SetRenderDrawBlendMode(renderer, blendMode); + SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(renderer); } srand((unsigned int)time(NULL)); @@ -239,30 +234,17 @@ ++frames; while (SDL_PollEvent(&event)) { CommonEvent(state, &event, &done); - switch (event.type) { - case SDL_WINDOWEVENT: - switch (event.window.event) { - case SDL_WINDOWEVENT_EXPOSED: - SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID)); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); - break; - } - break; - default: - break; - } } for (i = 0; i < state->num_windows; ++i) { - SDL_SelectRenderer(state->windows[i]); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); + SDL_Renderer *renderer = state->renderers[i]; + SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(renderer); - DrawRects(state->windows[i]); - DrawLines(state->windows[i]); - DrawPoints(state->windows[i]); + DrawRects(state->windows[i], renderer); + DrawLines(state->windows[i], renderer); + DrawPoints(state->windows[i], renderer); - SDL_RenderPresent(); + SDL_RenderPresent(renderer); } }
--- a/test/testintersections.c Tue Feb 01 19:15:42 2011 -0800 +++ b/test/testintersections.c Tue Feb 01 19:19:43 2011 -0800 @@ -20,7 +20,7 @@ static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; void -DrawPoints(SDL_Window * window) +DrawPoints(SDL_Window * window, SDL_Renderer * renderer) { int i; int x, y; @@ -29,7 +29,6 @@ /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); - SDL_SetRenderDrawBlendMode(blendMode); for (i = 0; i < num_objects * 4; ++i) { /* Cycle the color and alpha, if desired */ if (cycle_color) { @@ -54,14 +53,13 @@ cycle_direction = -cycle_direction; } } - SDL_SetRenderDrawColor(255, (Uint8) current_color, + SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color, (Uint8) current_color, (Uint8) current_alpha); x = rand() % window_w; y = rand() % window_h; - SDL_RenderDrawPoint(x, y); + SDL_RenderDrawPoint(renderer, x, y); } - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } #define MAX_LINES 16 @@ -86,7 +84,7 @@ void -DrawLines(SDL_Window * window) +DrawLines(SDL_Window * window, SDL_Renderer * renderer) { int i; int x1, y1, x2, y2; @@ -95,20 +93,18 @@ /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); - SDL_SetRenderDrawBlendMode(blendMode); for (i = 0; i < num_lines; ++i) { - SDL_SetRenderDrawColor(255, 255, 255, 255); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); if (i == -1) { - SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1); - SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0); - SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2); - SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1); + SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1); + SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0); + SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2); + SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1); } else { - SDL_RenderDrawLine(lines[i].x, lines[i].y, lines[i].w, lines[i].h); + SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h); } } - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } #define MAX_RECTS 16 @@ -139,7 +135,7 @@ } static void -DrawRects(SDL_Window * window) +DrawRects(SDL_Window * window, SDL_Renderer * renderer) { int i; int window_w, window_h; @@ -147,24 +143,20 @@ /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); for (i = 0; i < num_rects; ++i) { - SDL_SetRenderDrawColor(255, 127, 0, 255); - SDL_RenderFillRect(&rects[i]); + SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255); + SDL_RenderFillRect(renderer, &rects[i]); } - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } static void -DrawRectLineIntersections(SDL_Window * window) +DrawRectLineIntersections(SDL_Window * window, SDL_Renderer * renderer) { int i, j, window_w, window_h; /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); - for (i = 0; i < num_rects; i++) for (j = 0; j < num_lines; j++) { int x1, y1, x2, y2; @@ -177,31 +169,25 @@ y2 = lines[j].h; if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) { - SDL_SetRenderDrawColor(0, 255, 55, 255); - SDL_RenderDrawLine(x1, y1, x2, y2); + SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255); + SDL_RenderDrawLine(renderer, x1, y1, x2, y2); } } - - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } static void -DrawRectRectIntersections(SDL_Window * window) +DrawRectRectIntersections(SDL_Window * window, SDL_Renderer * renderer) { int i, j; - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); - for (i = 0; i < num_rects; i++) for (j = i + 1; j < num_rects; j++) { SDL_Rect r; if (SDL_IntersectRect(&rects[i], &rects[j], &r)) { - SDL_SetRenderDrawColor(255, 200, 0, 255); - SDL_RenderFillRect(&r); + SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255); + SDL_RenderFillRect(renderer, &r); } } - - SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); } int @@ -264,9 +250,10 @@ /* Create the windows and initialize the renderers */ for (i = 0; i < state->num_windows; ++i) { - SDL_SelectRenderer(state->windows[i]); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); + SDL_Renderer *renderer = state->renderers[i]; + SDL_SetRenderDrawBlendMode(renderer, blendMode); + SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(renderer); } srand(time(NULL)); @@ -311,31 +298,22 @@ break; } break; - case SDL_WINDOWEVENT: - switch (event.window.event) { - case SDL_WINDOWEVENT_EXPOSED: - SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID)); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); - break; - } - break; default: break; } } for (i = 0; i < state->num_windows; ++i) { - SDL_SelectRenderer(state->windows[i]); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); + SDL_Renderer *renderer = state->renderers[i]; + SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(renderer); - DrawRects(state->windows[i]); - DrawPoints(state->windows[i]); - DrawRectRectIntersections(state->windows[i]); - DrawLines(state->windows[i]); - DrawRectLineIntersections(state->windows[i]); + DrawRects(state->windows[i], renderer); + DrawPoints(state->windows[i], renderer); + DrawRectRectIntersections(state->windows[i], renderer); + DrawLines(state->windows[i], renderer); + DrawRectLineIntersections(state->windows[i], renderer); - SDL_RenderPresent(); + SDL_RenderPresent(renderer); } }
--- a/test/testshape.c Tue Feb 01 19:15:42 2011 -0800 +++ b/test/testshape.c Tue Feb 01 19:19:43 2011 -0800 @@ -11,177 +11,183 @@ #define TICK_INTERVAL 1000/10 typedef struct LoadedPicture { - SDL_Surface *surface; - SDL_Texture *texture; - SDL_WindowShapeMode mode; + SDL_Surface *surface; + SDL_Texture *texture; + SDL_WindowShapeMode mode; } LoadedPicture; -void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) { - SDL_SelectRenderer(window); - - //Clear render-target to blue. - SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff); - SDL_RenderClear(); - - //Render the texture. - SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions); - - SDL_RenderPresent(); +void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions) +{ + //Clear render-target to blue. + SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff); + SDL_RenderClear(renderer); + + //Render the texture. + SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions); + + SDL_RenderPresent(renderer); } static Uint32 next_time; -Uint32 time_left() { +Uint32 time_left() +{ Uint32 now = SDL_GetTicks(); if(next_time <= now) return 0; - else + else return next_time - now; } -int main(int argc,char** argv) { - Uint8 num_pictures; - LoadedPicture* pictures; - int i, j; - SDL_PixelFormat* format = NULL; - SDL_Window *window; - SDL_Color black = {0,0,0,0xff}; - SDL_Event event; - int event_pending = 0; - int should_exit = 0; - unsigned int current_picture; - int button_down; - Uint32 pixelFormat = 0; - int access = 0; - SDL_Rect texture_dimensions;; +int main(int argc,char** argv) +{ + Uint8 num_pictures; + LoadedPicture* pictures; + int i, j; + SDL_PixelFormat* format = NULL; + SDL_Window *window; + SDL_Renderer *renderer; + SDL_Color black = {0,0,0,0xff}; + SDL_Event event; + int event_pending = 0; + int should_exit = 0; + unsigned int current_picture; + int button_down; + Uint32 pixelFormat = 0; + int access = 0; + SDL_Rect texture_dimensions;; - if(argc < 2) { - printf("SDL_Shape requires at least one bitmap file as argument.\n"); - exit(-1); + if(argc < 2) { + printf("SDL_Shape requires at least one bitmap file as argument.\n"); + exit(-1); } - - if(SDL_VideoInit(NULL) == -1) { - printf("Could not initialize SDL video.\n"); - exit(-2); - } - - num_pictures = argc - 1; - pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures); - for(i=0;i<num_pictures;i++) - pictures[i].surface = NULL; - for(i=0;i<num_pictures;i++) { - pictures[i].surface = SDL_LoadBMP(argv[i+1]); - if(pictures[i].surface == NULL) { - j = 0; - for(j=0;j<num_pictures;j++) - if(pictures[j].surface != NULL) - SDL_FreeSurface(pictures[j].surface); - free(pictures); - SDL_VideoQuit(); - printf("Could not load surface from named bitmap file.\n"); - exit(-3); - } + + if(SDL_VideoInit(NULL) == -1) { + printf("Could not initialize SDL video.\n"); + exit(-2); + } + + num_pictures = argc - 1; + pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures); + for(i=0;i<num_pictures;i++) + pictures[i].surface = NULL; + for(i=0;i<num_pictures;i++) { + pictures[i].surface = SDL_LoadBMP(argv[i+1]); + if(pictures[i].surface == NULL) { + j = 0; + for(j=0;j<num_pictures;j++) + if(pictures[j].surface != NULL) + SDL_FreeSurface(pictures[j].surface); + free(pictures); + SDL_VideoQuit(); + printf("Could not load surface from named bitmap file.\n"); + exit(-3); + } - format = pictures[i].surface->format; - if(format->Amask != 0) { - pictures[i].mode.mode = ShapeModeBinarizeAlpha; - pictures[i].mode.parameters.binarizationCutoff = 255; - } - else { - pictures[i].mode.mode = ShapeModeColorKey; - pictures[i].mode.parameters.colorKey = black; - } - } - - window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN); - if(window == NULL) { - for(i=0;i<num_pictures;i++) - SDL_FreeSurface(pictures[i].surface); - free(pictures); - SDL_VideoQuit(); - printf("Could not create shaped window for SDL_Shape.\n"); - exit(-4); - } - if(SDL_CreateRenderer(window,-1,0) == -1) { - SDL_DestroyWindow(window); - for(i=0;i<num_pictures;i++) - SDL_FreeSurface(pictures[i].surface); - free(pictures); - SDL_VideoQuit(); - printf("Could not create rendering context for SDL_Shape window.\n"); - exit(-5); - } - - for(i=0;i<num_pictures;i++) - pictures[i].texture = NULL; - for(i=0;i<num_pictures;i++) { - pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface); - if(pictures[i].texture == NULL) { - j = 0; - for(j=0;j<num_pictures;i++) - if(pictures[i].texture != NULL) - SDL_DestroyTexture(pictures[i].texture); - for(i=0;i<num_pictures;i++) - SDL_FreeSurface(pictures[i].surface); - free(pictures); - SDL_DestroyRenderer(window); - SDL_DestroyWindow(window); - SDL_VideoQuit(); - printf("Could not create texture for SDL_shape.\n"); - exit(-6); - } - } - - event_pending = 0; - should_exit = 0; - event_pending = SDL_PollEvent(&event); - current_picture = 0; - button_down = 0; - texture_dimensions.h = 0; - texture_dimensions.w = 0; - texture_dimensions.x = 0; - texture_dimensions.y = 0; - SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h); - SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); - SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); - next_time = SDL_GetTicks() + TICK_INTERVAL; - while(should_exit == 0) { - event_pending = SDL_PollEvent(&event); - if(event_pending == 1) { - if(event.type == SDL_KEYDOWN) { - button_down = 1; - if(event.key.keysym.sym == SDLK_ESCAPE) - should_exit = 1; - } - if(button_down && event.type == SDL_KEYUP) { - button_down = 0; - current_picture += 1; - if(current_picture >= num_pictures) - current_picture = 0; - SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h); - SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); - SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); - } - if(event.type == SDL_QUIT) - should_exit = 1; - event_pending = 0; - } - render(window,pictures[current_picture].texture,texture_dimensions); - SDL_Delay(time_left()); - next_time += TICK_INTERVAL; - } - - //Free the textures. - for(i=0;i<num_pictures;i++) - SDL_DestroyTexture(pictures[i].texture); - //Destroy the window. - SDL_DestroyWindow(window); - //Free the original surfaces backing the textures. - for(i=0;i<num_pictures;i++) - SDL_FreeSurface(pictures[i].surface); - free(pictures); - //Call SDL_VideoQuit() before quitting. - SDL_VideoQuit(); + format = pictures[i].surface->format; + if(format->Amask != 0) { + pictures[i].mode.mode = ShapeModeBinarizeAlpha; + pictures[i].mode.parameters.binarizationCutoff = 255; + } + else { + pictures[i].mode.mode = ShapeModeColorKey; + pictures[i].mode.parameters.colorKey = black; + } + } + + window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN); + if(window == NULL) { + for(i=0;i<num_pictures;i++) + SDL_FreeSurface(pictures[i].surface); + free(pictures); + SDL_VideoQuit(); + printf("Could not create shaped window for SDL_Shape.\n"); + exit(-4); + } + renderer = SDL_CreateRenderer(window,-1,0); + if (!renderer) { + SDL_DestroyWindow(window); + for(i=0;i<num_pictures;i++) + SDL_FreeSurface(pictures[i].surface); + free(pictures); + SDL_VideoQuit(); + printf("Could not create rendering context for SDL_Shape window.\n"); + exit(-5); + } + + for(i=0;i<num_pictures;i++) + pictures[i].texture = NULL; + for(i=0;i<num_pictures;i++) { + pictures[i].texture = SDL_CreateTextureFromSurface(renderer,0,pictures[i].surface); + if(pictures[i].texture == NULL) { + j = 0; + for(j=0;j<num_pictures;i++) + if(pictures[i].texture != NULL) + SDL_DestroyTexture(pictures[i].texture); + for(i=0;i<num_pictures;i++) + SDL_FreeSurface(pictures[i].surface); + free(pictures); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_VideoQuit(); + printf("Could not create texture for SDL_shape.\n"); + exit(-6); + } + } + + event_pending = 0; + should_exit = 0; + event_pending = SDL_PollEvent(&event); + current_picture = 0; + button_down = 0; + texture_dimensions.h = 0; + texture_dimensions.w = 0; + texture_dimensions.x = 0; + texture_dimensions.y = 0; + SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h); + SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); + SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); + next_time = SDL_GetTicks() + TICK_INTERVAL; + while(should_exit == 0) { + event_pending = SDL_PollEvent(&event); + if(event_pending == 1) { + if(event.type == SDL_KEYDOWN) { + button_down = 1; + if(event.key.keysym.sym == SDLK_ESCAPE) + should_exit = 1; + } + if(button_down && event.type == SDL_KEYUP) { + button_down = 0; + current_picture += 1; + if(current_picture >= num_pictures) + current_picture = 0; + SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h); + SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); + SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); + } + if(event.type == SDL_QUIT) + should_exit = 1; + event_pending = 0; + } + render(renderer,pictures[current_picture].texture,texture_dimensions); + SDL_Delay(time_left()); + next_time += TICK_INTERVAL; + } + + //Free the textures. + for(i=0;i<num_pictures;i++) + SDL_DestroyTexture(pictures[i].texture); + SDL_DestroyRenderer(renderer); + //Destroy the window. + SDL_DestroyWindow(window); + //Free the original surfaces backing the textures. + for(i=0;i<num_pictures;i++) + SDL_FreeSurface(pictures[i].surface); + free(pictures); + //Call SDL_VideoQuit() before quitting. + SDL_VideoQuit(); - return 0; + return 0; } + +/* vi: set ts=4 sw=4 expandtab: */
--- a/test/testsprite2.c Tue Feb 01 19:15:42 2011 -0800 +++ b/test/testsprite2.c Tue Feb 01 19:19:43 2011 -0800 @@ -6,8 +6,8 @@ #include "common.h" -#define NUM_SPRITES 100 -#define MAX_SPEED 1 +#define NUM_SPRITES 100 +#define MAX_SPEED 1 static CommonState *state; static int num_sprites; @@ -76,12 +76,12 @@ /* Create textures from the image */ for (i = 0; i < state->num_windows; ++i) { - SDL_SelectRenderer(state->windows[i]); - sprites[i] = SDL_CreateTextureFromSurface(0, temp); + SDL_Renderer *renderer = state->renderers[i]; + sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp); if (!sprites[i]) { - SDL_SetColorKey(temp, 0, 0); - sprites[i] = SDL_CreateTextureFromSurface(0, temp); - } + SDL_SetColorKey(temp, 0, 0); + sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp); + } if (!sprites[i]) { fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError()); SDL_FreeSurface(temp); @@ -96,15 +96,13 @@ } void -MoveSprites(SDL_Window * window, SDL_Texture * sprite) +MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite) { int i, n; int window_w, window_h; SDL_Rect temp; SDL_Rect *position, *velocity; - SDL_SelectRenderer(window); - /* Query the sizes */ SDL_GetWindowSize(window, &window_w, &window_h); @@ -136,55 +134,55 @@ } /* Draw a gray background */ - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); + SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(renderer); /* Test points */ - SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF); - SDL_RenderDrawPoint(0, 0); - SDL_RenderDrawPoint(window_w-1, 0); - SDL_RenderDrawPoint(0, window_h-1); - SDL_RenderDrawPoint(window_w-1, window_h-1); + SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF); + SDL_RenderDrawPoint(renderer, 0, 0); + SDL_RenderDrawPoint(renderer, window_w-1, 0); + SDL_RenderDrawPoint(renderer, 0, window_h-1); + SDL_RenderDrawPoint(renderer, window_w-1, window_h-1); /* Test horizontal and vertical lines */ - SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF); - SDL_RenderDrawLine(1, 0, window_w-2, 0); - SDL_RenderDrawLine(1, window_h-1, window_w-2, window_h-1); - SDL_RenderDrawLine(0, 1, 0, window_h-2); - SDL_RenderDrawLine(window_w-1, 1, window_w-1, window_h-2); + SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF); + SDL_RenderDrawLine(renderer, 1, 0, window_w-2, 0); + SDL_RenderDrawLine(renderer, 1, window_h-1, window_w-2, window_h-1); + SDL_RenderDrawLine(renderer, 0, 1, 0, window_h-2); + SDL_RenderDrawLine(renderer, window_w-1, 1, window_w-1, window_h-2); /* Test fill and copy */ - SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF); + SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); temp.x = 1; temp.y = 1; temp.w = sprite_w; temp.h = sprite_h; - SDL_RenderFillRect(&temp); - SDL_RenderCopy(sprite, NULL, &temp); + SDL_RenderFillRect(renderer, &temp); + SDL_RenderCopy(renderer, sprite, NULL, &temp); temp.x = window_w-sprite_w-1; temp.y = 1; temp.w = sprite_w; temp.h = sprite_h; - SDL_RenderFillRect(&temp); - SDL_RenderCopy(sprite, NULL, &temp); + SDL_RenderFillRect(renderer, &temp); + SDL_RenderCopy(renderer, sprite, NULL, &temp); temp.x = 1; temp.y = window_h-sprite_h-1; temp.w = sprite_w; temp.h = sprite_h; - SDL_RenderFillRect(&temp); - SDL_RenderCopy(sprite, NULL, &temp); + SDL_RenderFillRect(renderer, &temp); + SDL_RenderCopy(renderer, sprite, NULL, &temp); temp.x = window_w-sprite_w-1; temp.y = window_h-sprite_h-1; temp.w = sprite_w; temp.h = sprite_h; - SDL_RenderFillRect(&temp); - SDL_RenderCopy(sprite, NULL, &temp); + SDL_RenderFillRect(renderer, &temp); + SDL_RenderCopy(renderer, sprite, NULL, &temp); /* Test diagonal lines */ - SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF); - SDL_RenderDrawLine(sprite_w, sprite_h, + SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF); + SDL_RenderDrawLine(renderer, sprite_w, sprite_h, window_w-sprite_w-2, window_h-sprite_h-2); - SDL_RenderDrawLine(window_w-sprite_w-2, sprite_h, + SDL_RenderDrawLine(renderer, window_w-sprite_w-2, sprite_h, sprite_w, window_h-sprite_h-2); /* Move the sprite, bounce at the wall, and draw */ @@ -204,11 +202,11 @@ } /* Blit the sprite onto the screen */ - SDL_RenderCopy(sprite, NULL, position); + SDL_RenderCopy(renderer, sprite, NULL, position); } /* Update the screen! */ - SDL_RenderPresent(); + SDL_RenderPresent(renderer); } int @@ -276,9 +274,9 @@ quit(2); } for (i = 0; i < state->num_windows; ++i) { - SDL_SelectRenderer(state->windows[i]); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); + SDL_Renderer *renderer = state->renderers[i]; + SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(renderer); } if (LoadSprite("icon.bmp") < 0) { quit(2); @@ -314,22 +312,9 @@ ++frames; while (SDL_PollEvent(&event)) { CommonEvent(state, &event, &done); - switch (event.type) { - case SDL_WINDOWEVENT: - switch (event.window.event) { - case SDL_WINDOWEVENT_EXPOSED: - SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID)); - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); - break; - } - break; - default: - break; - } } for (i = 0; i < state->num_windows; ++i) { - MoveSprites(state->windows[i], sprites[i]); + MoveSprites(state->windows[i], state->renderers[i], sprites[i]); } } @@ -340,7 +325,7 @@ printf("%2.2f frames per second\n", fps); } quit(0); - return 0; + return 0; } /* vi: set ts=4 sw=4 expandtab: */
--- a/test/testspriteminimal.c Tue Feb 01 19:15:42 2011 -0800 +++ b/test/testspriteminimal.c Tue Feb 01 19:19:43 2011 -0800 @@ -24,7 +24,7 @@ } int -LoadSprite(char *file) +LoadSprite(char *file, SDL_Renderer *renderer) { SDL_Surface *temp; @@ -60,10 +60,10 @@ } /* Create textures from the image */ - sprite = SDL_CreateTextureFromSurface(0, temp); + sprite = SDL_CreateTextureFromSurface(renderer, 0, temp); if (!sprite) { SDL_SetColorKey(temp, 0, 0); - sprite = SDL_CreateTextureFromSurface(0, temp); + sprite = SDL_CreateTextureFromSurface(renderer, 0, temp); } if (!sprite) { fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError()); @@ -77,7 +77,7 @@ } void -MoveSprites(SDL_Window * window, SDL_Texture * sprite) +MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite) { int i; int window_w = WINDOW_WIDTH; @@ -85,8 +85,8 @@ SDL_Rect *position, *velocity; /* Draw a gray background */ - SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); - SDL_RenderClear(); + SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); + SDL_RenderClear(renderer); /* Move the sprite, bounce at the wall, and draw */ for (i = 0; i < NUM_SPRITES; ++i) { @@ -104,17 +104,18 @@ } /* Blit the sprite onto the screen */ - SDL_RenderCopy(sprite, NULL, position); + SDL_RenderCopy(renderer, sprite, NULL, position); } /* Update the screen! */ - SDL_RenderPresent(); + SDL_RenderPresent(renderer); } int main(int argc, char *argv[]) { SDL_Window *window; + SDL_Renderer *renderer; int i, done; SDL_Event event; @@ -127,7 +128,12 @@ quit(2); } - if (LoadSprite("icon.bmp") < 0) { + renderer = SDL_CreateRenderer(window, -1, 0); + if (!renderer) { + quit(2); + } + + if (LoadSprite("icon.bmp", renderer) < 0) { quit(2); } @@ -155,7 +161,7 @@ done = 1; } } - MoveSprites(window, sprite); + MoveSprites(window, renderer, sprite); } quit(0);