src/video/win32/SDL_d3drender.c
branchSDL-1.3
changeset 1730 e70477157db9
child 1898 f89e49e51e89
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/win32/SDL_d3drender.c	Sat Jul 08 18:06:02 2006 +0000
     1.3 @@ -0,0 +1,385 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#if SDL_VIDEO_RENDER_D3D
    1.28 +
    1.29 +#include "SDL_win32video.h"
    1.30 +#include "../SDL_yuv_sw_c.h"
    1.31 +
    1.32 +/* Direct3D renderer implementation */
    1.33 +
    1.34 +static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window,
    1.35 +                                            Uint32 flags);
    1.36 +static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
    1.37 +                                 SDL_Texture * texture);
    1.38 +static int SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer,
    1.39 +                                      SDL_Texture * texture, void **pixels,
    1.40 +                                      int *pitch);
    1.41 +static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
    1.42 +                                     SDL_Texture * texture,
    1.43 +                                     const SDL_Color * colors, int firstcolor,
    1.44 +                                     int ncolors);
    1.45 +static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
    1.46 +                                     SDL_Texture * texture,
    1.47 +                                     SDL_Color * colors, int firstcolor,
    1.48 +                                     int ncolors);
    1.49 +static int SDL_D3D_UpdateTexture(SDL_Renderer * renderer,
    1.50 +                                 SDL_Texture * texture, const SDL_Rect * rect,
    1.51 +                                 const void *pixels, int pitch);
    1.52 +static int SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.53 +                               const SDL_Rect * rect, int markDirty,
    1.54 +                               void **pixels, int *pitch);
    1.55 +static void SDL_D3D_UnlockTexture(SDL_Renderer * renderer,
    1.56 +                                  SDL_Texture * texture);
    1.57 +static void SDL_D3D_DirtyTexture(SDL_Renderer * renderer,
    1.58 +                                 SDL_Texture * texture, int numrects,
    1.59 +                                 const SDL_Rect * rects);
    1.60 +static void SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer,
    1.61 +                                        SDL_Texture * texture);
    1.62 +static int SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.63 +                              Uint32 color);
    1.64 +static int SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    1.65 +                              const SDL_Rect * srcrect,
    1.66 +                              const SDL_Rect * dstrect, int blendMode,
    1.67 +                              int scaleMode);
    1.68 +static int SDL_D3D_RenderReadPixels(SDL_Renderer * renderer,
    1.69 +                                    const SDL_Rect * rect, void *pixels,
    1.70 +                                    int pitch);
    1.71 +static int SDL_D3D_RenderWritePixels(SDL_Renderer * renderer,
    1.72 +                                     const SDL_Rect * rect,
    1.73 +                                     const void *pixels, int pitch);
    1.74 +static void SDL_D3D_RenderPresent(SDL_Renderer * renderer);
    1.75 +static void SDL_D3D_DestroyTexture(SDL_Renderer * renderer,
    1.76 +                                   SDL_Texture * texture);
    1.77 +static void SDL_D3D_DestroyRenderer(SDL_Renderer * renderer);
    1.78 +
    1.79 +
    1.80 +SDL_RenderDriver SDL_D3D_RenderDriver = {
    1.81 +    SDL_D3D_CreateRenderer,
    1.82 +    {
    1.83 +     "d3d",
    1.84 +     (SDL_Renderer_PresentDiscard |
    1.85 +      SDL_Renderer_PresentCopy | SDL_Renderer_RenderTarget),
    1.86 +     (SDL_TextureBlendMode_None |
    1.87 +      SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
    1.88 +     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    1.89 +     11,
    1.90 +     {
    1.91 +      SDL_PixelFormat_Index8,
    1.92 +      SDL_PixelFormat_RGB555,
    1.93 +      SDL_PixelFormat_RGB565,
    1.94 +      SDL_PixelFormat_RGB888,
    1.95 +      SDL_PixelFormat_BGR888,
    1.96 +      SDL_PixelFormat_ARGB8888,
    1.97 +      SDL_PixelFormat_RGBA8888,
    1.98 +      SDL_PixelFormat_ABGR8888,
    1.99 +      SDL_PixelFormat_BGRA8888,
   1.100 +      SDL_PixelFormat_YUY2,
   1.101 +      SDL_PixelFormat_UYVY},
   1.102 +     0,
   1.103 +     0}
   1.104 +};
   1.105 +
   1.106 +typedef struct
   1.107 +{
   1.108 +    IDirect3DDevice9 *device;
   1.109 +} SDL_D3D_RenderData;
   1.110 +
   1.111 +typedef struct
   1.112 +{
   1.113 +    SDL_SW_YUVTexture *yuv;
   1.114 +} SDL_D3D_TextureData;
   1.115 +
   1.116 +static void
   1.117 +UpdateYUVTextureData(SDL_Texture * texture)
   1.118 +{
   1.119 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.120 +    SDL_Rect rect;
   1.121 +
   1.122 +    rect.x = 0;
   1.123 +    rect.y = 0;
   1.124 +    rect.w = texture->w;
   1.125 +    rect.h = texture->h;
   1.126 +    //SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
   1.127 +    //                    texture->h, data->pixels, data->pitch);
   1.128 +}
   1.129 +
   1.130 +void
   1.131 +D3D_AddRenderDriver(_THIS)
   1.132 +{
   1.133 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   1.134 +
   1.135 +    if (data->d3d) {
   1.136 +        SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver);
   1.137 +    }
   1.138 +}
   1.139 +
   1.140 +SDL_Renderer *
   1.141 +SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.142 +{
   1.143 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.144 +    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   1.145 +    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   1.146 +    SDL_Renderer *renderer;
   1.147 +    SDL_D3D_RenderData *data;
   1.148 +
   1.149 +    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   1.150 +    if (!renderer) {
   1.151 +        SDL_OutOfMemory();
   1.152 +        return NULL;
   1.153 +    }
   1.154 +    SDL_zerop(renderer);
   1.155 +
   1.156 +    data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data));
   1.157 +    if (!data) {
   1.158 +        SDL_D3D_DestroyRenderer(renderer);
   1.159 +        SDL_OutOfMemory();
   1.160 +        return NULL;
   1.161 +    }
   1.162 +    SDL_zerop(data);
   1.163 +
   1.164 +    //data->device = IDirect3D9_CreateDevice(videodata->d3d,
   1.165 +
   1.166 +    renderer->CreateTexture = SDL_D3D_CreateTexture;
   1.167 +    renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
   1.168 +    renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   1.169 +    renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
   1.170 +    renderer->UpdateTexture = SDL_D3D_UpdateTexture;
   1.171 +    renderer->LockTexture = SDL_D3D_LockTexture;
   1.172 +    renderer->UnlockTexture = SDL_D3D_UnlockTexture;
   1.173 +    renderer->DirtyTexture = SDL_D3D_DirtyTexture;
   1.174 +    renderer->SelectRenderTexture = SDL_D3D_SelectRenderTexture;
   1.175 +    renderer->RenderFill = SDL_D3D_RenderFill;
   1.176 +    renderer->RenderCopy = SDL_D3D_RenderCopy;
   1.177 +    renderer->RenderReadPixels = SDL_D3D_RenderReadPixels;
   1.178 +    renderer->RenderWritePixels = SDL_D3D_RenderWritePixels;
   1.179 +    renderer->RenderPresent = SDL_D3D_RenderPresent;
   1.180 +    renderer->DestroyTexture = SDL_D3D_DestroyTexture;
   1.181 +    renderer->DestroyRenderer = SDL_D3D_DestroyRenderer;
   1.182 +    renderer->info = SDL_D3D_RenderDriver.info;
   1.183 +    renderer->window = window->id;
   1.184 +    renderer->driverdata = data;
   1.185 +
   1.186 +    renderer->info.flags = SDL_Renderer_RenderTarget;
   1.187 +
   1.188 +    return renderer;
   1.189 +}
   1.190 +
   1.191 +static int
   1.192 +SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.193 +{
   1.194 +    SDL_D3D_RenderData *renderdata =
   1.195 +        (SDL_D3D_RenderData *) renderer->driverdata;
   1.196 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.197 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.198 +    SDL_D3D_TextureData *data;
   1.199 +
   1.200 +    data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   1.201 +    if (!data) {
   1.202 +        SDL_OutOfMemory();
   1.203 +        return -1;
   1.204 +    }
   1.205 +    SDL_zerop(data);
   1.206 +
   1.207 +    texture->driverdata = data;
   1.208 +
   1.209 +    return 0;
   1.210 +}
   1.211 +
   1.212 +static int
   1.213 +SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   1.214 +                           void **pixels, int *pitch)
   1.215 +{
   1.216 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.217 +
   1.218 +    if (data->yuv) {
   1.219 +        return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
   1.220 +    } else {
   1.221 +        return 0;
   1.222 +    }
   1.223 +}
   1.224 +
   1.225 +static int
   1.226 +SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.227 +                          const SDL_Color * colors, int firstcolor,
   1.228 +                          int ncolors)
   1.229 +{
   1.230 +    SDL_D3D_RenderData *renderdata =
   1.231 +        (SDL_D3D_RenderData *) renderer->driverdata;
   1.232 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.233 +
   1.234 +    if (data->yuv) {
   1.235 +        SDL_SetError("YUV textures don't have a palette");
   1.236 +        return -1;
   1.237 +    } else {
   1.238 +        return 0;
   1.239 +    }
   1.240 +}
   1.241 +
   1.242 +static int
   1.243 +SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.244 +                          SDL_Color * colors, int firstcolor, int ncolors)
   1.245 +{
   1.246 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.247 +
   1.248 +    if (data->yuv) {
   1.249 +        SDL_SetError("YUV textures don't have a palette");
   1.250 +        return -1;
   1.251 +    } else {
   1.252 +        return 0;
   1.253 +    }
   1.254 +}
   1.255 +
   1.256 +static int
   1.257 +SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.258 +                      const SDL_Rect * rect, const void *pixels, int pitch)
   1.259 +{
   1.260 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.261 +
   1.262 +    if (data->yuv) {
   1.263 +        if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
   1.264 +            return -1;
   1.265 +        }
   1.266 +        UpdateYUVTextureData(texture);
   1.267 +        return 0;
   1.268 +    } else {
   1.269 +        SDL_D3D_RenderData *renderdata =
   1.270 +            (SDL_D3D_RenderData *) renderer->driverdata;
   1.271 +
   1.272 +        return 0;
   1.273 +    }
   1.274 +}
   1.275 +
   1.276 +static int
   1.277 +SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.278 +                    const SDL_Rect * rect, int markDirty, void **pixels,
   1.279 +                    int *pitch)
   1.280 +{
   1.281 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.282 +
   1.283 +    if (data->yuv) {
   1.284 +        return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   1.285 +                                     pitch);
   1.286 +    } else {
   1.287 +        return 0;
   1.288 +    }
   1.289 +}
   1.290 +
   1.291 +static void
   1.292 +SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.293 +{
   1.294 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.295 +
   1.296 +    if (data->yuv) {
   1.297 +        SDL_SW_UnlockYUVTexture(data->yuv);
   1.298 +        UpdateYUVTextureData(texture);
   1.299 +    }
   1.300 +}
   1.301 +
   1.302 +static void
   1.303 +SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.304 +                     int numrects, const SDL_Rect * rects)
   1.305 +{
   1.306 +}
   1.307 +
   1.308 +static void
   1.309 +SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.310 +{
   1.311 +    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.312 +}
   1.313 +
   1.314 +static int
   1.315 +SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
   1.316 +                   Uint32 color)
   1.317 +{
   1.318 +    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.319 +    Uint8 r, g, b;
   1.320 +
   1.321 +    r = (Uint8) ((color >> 16) & 0xFF);
   1.322 +    g = (Uint8) ((color >> 8) & 0xFF);
   1.323 +    b = (Uint8) (color & 0xFF);
   1.324 +
   1.325 +    return 0;
   1.326 +}
   1.327 +
   1.328 +static int
   1.329 +SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.330 +                   const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   1.331 +                   int blendMode, int scaleMode)
   1.332 +{
   1.333 +    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.334 +    SDL_D3D_TextureData *texturedata =
   1.335 +        (SDL_D3D_TextureData *) texture->driverdata;
   1.336 +
   1.337 +    return 0;
   1.338 +}
   1.339 +
   1.340 +static int
   1.341 +SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   1.342 +                         void *pixels, int pitch)
   1.343 +{
   1.344 +    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.345 +
   1.346 +    return 0;
   1.347 +}
   1.348 +
   1.349 +static int
   1.350 +SDL_D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   1.351 +                          const void *pixels, int pitch)
   1.352 +{
   1.353 +    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.354 +
   1.355 +    return 0;
   1.356 +}
   1.357 +
   1.358 +static void
   1.359 +SDL_D3D_RenderPresent(SDL_Renderer * renderer)
   1.360 +{
   1.361 +}
   1.362 +
   1.363 +static void
   1.364 +SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.365 +{
   1.366 +    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.367 +
   1.368 +    if (!data) {
   1.369 +        return;
   1.370 +    }
   1.371 +    SDL_free(data);
   1.372 +    texture->driverdata = NULL;
   1.373 +}
   1.374 +
   1.375 +void
   1.376 +SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
   1.377 +{
   1.378 +    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.379 +
   1.380 +    if (data) {
   1.381 +        SDL_free(data);
   1.382 +    }
   1.383 +    SDL_free(renderer);
   1.384 +}
   1.385 +
   1.386 +#endif /* SDL_VIDEO_RENDER_D3D */
   1.387 +
   1.388 +/* vi: set ts=4 sw=4 expandtab: */