src/video/nds/SDL_ndsrender.c
branchgsoc2008_nds
changeset 2678 3895761db26a
parent 2677 6386764eb222
child 2679 bc3e3e889f6d
     1.1 --- a/src/video/nds/SDL_ndsrender.c	Wed Jul 02 13:59:30 2008 +0000
     1.2 +++ b/src/video/nds/SDL_ndsrender.c	Thu Jul 10 23:35:01 2008 +0000
     1.3 @@ -79,10 +79,16 @@
     1.4  
     1.5  SDL_RenderDriver NDS_RenderDriver = {
     1.6      NDS_CreateRenderer,
     1.7 -    {"nds", SDL_RENDERER_PRESENTCOPY}
     1.8 -/*   (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
     1.9 -      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
    1.10 -      SDL_RENDERER_PRESENTDISCARD),*/
    1.11 +    {"nds", SDL_RENDERER_SINGLEBUFFER}
    1.12 +/*SDL_RENDERER_ values
    1.13 +SINGLEBUFFER   Render directly to the window, if possible
    1.14 +PRESENTCOPY    Present uses a copy from back buffer to the front buffer
    1.15 +PRESENTFLIP2   Present uses a flip, swapping back buffer and front buffer
    1.16 +PRESENTFLIP3   Present uses a flip, rotating two back buf.s and a front buf.
    1.17 +PRESENTDISCARD Present leaves the contents of the backbuffer undefined
    1.18 +PRESENTVSYNC   Present is synchronized with the refresh rate
    1.19 +ACCELERATED    The renderer uses hardware acceleration
    1.20 +*/
    1.21  };
    1.22  
    1.23  typedef struct
    1.24 @@ -91,6 +97,12 @@
    1.25      u16* fb;
    1.26  } NDS_RenderData;
    1.27  
    1.28 +typedef struct
    1.29 +{
    1.30 +    enum { NDSTX_BG, NDSTX_SPR } type;
    1.31 +    struct { int w, h, pitch, bpp; } dim;
    1.32 +    u16 *vram;
    1.33 +} NDS_TextureData;
    1.34  
    1.35  
    1.36  /* this is mainly hackish testing/debugging stuff to get cleaned up soon
    1.37 @@ -143,7 +155,7 @@
    1.38      sdlds_print_pixfmt_info(s->format);
    1.39  }
    1.40  
    1.41 -
    1.42 +/* again the above shouldn't make it into the stable version */
    1.43  
    1.44  SDL_Renderer *
    1.45  NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
    1.46 @@ -199,7 +211,8 @@
    1.47      } else {
    1.48          renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
    1.49          n = 1;
    1.50 -    }/*
    1.51 +    }
    1.52 +    /*
    1.53      for (i = 0; i < n; ++i) {
    1.54          data->screens[i] =
    1.55              SDL_CreateRGBSurface(0, 256, 192, bpp, Rmask, Gmask, Bmask,
    1.56 @@ -218,6 +231,182 @@
    1.57  }
    1.58  
    1.59  static int
    1.60 +NDS_ActivateRenderer(SDL_Renderer * renderer)
    1.61 +{
    1.62 +    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
    1.63 +    /* stub. TODO: figure out what needs to be done, if anything. */
    1.64 +    return 0;
    1.65 +}
    1.66 +
    1.67 +static int
    1.68 +NDS_DisplayModeChanged(SDL_Renderer * renderer)
    1.69 +{
    1.70 +    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
    1.71 +    /* stub. TODO: figure out what needs to be done */
    1.72 +    return 0;
    1.73 +}
    1.74 +
    1.75 +static int
    1.76 +NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
    1.77 +{
    1.78 +    NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
    1.79 +
    1.80 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
    1.81 +        SDL_SetError("Unsupported texture format");
    1.82 +        return -1;
    1.83 +    } else {
    1.84 +        int bpp;
    1.85 +        Uint32 Rmask, Gmask, Bmask, Amask;
    1.86 +
    1.87 +        if (!SDL_PixelFormatEnumToMasks
    1.88 +            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
    1.89 +            SDL_SetError("Unknown texture format");
    1.90 +            return -1;
    1.91 +        }
    1.92 +        /* TODO: appropriate checks for ABGR1555 */
    1.93 +        texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
    1.94 +        /* TODO: conditional statements on w/h to place it as bg/sprite */
    1.95 +    }
    1.96 +
    1.97 +    if (!texture->driverdata) {
    1.98 +        return -1;
    1.99 +    }
   1.100 +    return 0;
   1.101 +}
   1.102 +
   1.103 +static int
   1.104 +NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   1.105 +                      void **pixels, int *pitch)
   1.106 +{
   1.107 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.108 +        SDL_SetError("Unsupported texture format");
   1.109 +        return -1;
   1.110 +    } else {
   1.111 +        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.112 +
   1.113 +        *pixels = txdat->vram;
   1.114 +        *pitch = txdat->dim.pitch;
   1.115 +        return 0;
   1.116 +    }
   1.117 +}
   1.118 +
   1.119 +static int
   1.120 +NDS_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.121 +                     const SDL_Color * colors, int firstcolor, int ncolors)
   1.122 +{
   1.123 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.124 +        SDL_SetError("YUV textures don't have a palette");
   1.125 +        return -1;
   1.126 +    } else {
   1.127 +        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.128 +        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
   1.129 +        return 0;
   1.130 +    }
   1.131 +}
   1.132 +
   1.133 +static int
   1.134 +NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.135 +                     SDL_Color * colors, int firstcolor, int ncolors)
   1.136 +{
   1.137 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.138 +        SDL_SetError("YUV textures don't have a palette");
   1.139 +        return -1;
   1.140 +    } else {
   1.141 +        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.142 +        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
   1.143 +        return 0;
   1.144 +    }
   1.145 +}
   1.146 +
   1.147 +static int
   1.148 +NDS_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.149 +{
   1.150 +    /* stub. TODO: figure out what needs to be done, if anything */
   1.151 +    return 0;
   1.152 +}
   1.153 +
   1.154 +static int
   1.155 +NDS_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.156 +{
   1.157 +    /* stub. TODO: figure out what needs to be done, if anything */
   1.158 +    return 0;
   1.159 +}
   1.160 +
   1.161 +static int
   1.162 +NDS_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.163 +{
   1.164 +    /* stub. TODO: figure out what needs to be done, if anything */
   1.165 +    return 0;
   1.166 +}
   1.167 +
   1.168 +static int
   1.169 +NDS_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.170 +{
   1.171 +    /* stub. TODO: figure out what needs to be done.
   1.172 +       (NDS hardware scaling is nearest neighbor.) */
   1.173 +    return 0;
   1.174 +}
   1.175 +
   1.176 +static int
   1.177 +NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.178 +                 const SDL_Rect * rect, const void *pixels, int pitch)
   1.179 +{
   1.180 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.181 +        SDL_SetError("Unsupported texture format");
   1.182 +        return -1;
   1.183 +    } else {
   1.184 +        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.185 +        Uint8 *src, *dst;
   1.186 +        int row;
   1.187 +        size_t length;
   1.188 +        /* IMPORTANT! copy the new pixels into the sprite or bg. */
   1.189 +        src = (Uint8 *) pixels;
   1.190 +        dst =
   1.191 +            (Uint8 *) txdat->vram + rect->y * txdat->dim.pitch +
   1.192 +            rect->x * (txdat->dim.bpp/8);
   1.193 +        length = rect->w * (txdat->dim.bpp/8);
   1.194 +        for (row = 0; row < rect->h; ++row) {
   1.195 +            SDL_memcpy(dst, src, length);
   1.196 +            src += pitch;
   1.197 +            dst += surface->pitch;
   1.198 +        }
   1.199 +        return 0;
   1.200 +    }
   1.201 +}
   1.202 +
   1.203 +static int
   1.204 +NDS_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.205 +               const SDL_Rect * rect, int markDirty, void **pixels,
   1.206 +               int *pitch)
   1.207 +{
   1.208 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.209 +        SDL_SetError("Unsupported texture format");
   1.210 +        return -1;
   1.211 +    } else {
   1.212 +        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.213 +
   1.214 +        *pixels = (void *) ((u8 *)txdat->vram + rect->y * txdat->dim.pitch
   1.215 +                            + rect->x * (txdat->dim.bpp/8));
   1.216 +        *pitch = txdat->dim.pitch;
   1.217 +        return 0;
   1.218 +    }
   1.219 +}
   1.220 +
   1.221 +static void
   1.222 +NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.223 +{
   1.224 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.225 +        SDL_SetError("Unsupported texture format");
   1.226 +    }
   1.227 +}
   1.228 +
   1.229 +static void
   1.230 +NDS_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.231 +                int numrects, const SDL_Rect * rects)
   1.232 +{ /* stub */
   1.233 +}
   1.234 +
   1.235 +static int
   1.236  NDS_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
   1.237                     Uint8 a, const SDL_Rect * rect)
   1.238  {
   1.239 @@ -235,6 +424,7 @@
   1.240      }
   1.241      return 0;
   1.242  }
   1.243 +
   1.244  static int
   1.245  NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.246                     const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   1.247 @@ -297,19 +487,40 @@
   1.248  }
   1.249  
   1.250  static void
   1.251 +NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.252 +{
   1.253 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.254 +        SDL_SetError("Unsupported texture format");
   1.255 +    } else {
   1.256 +        /* TODO: free anything allocated for texture */
   1.257 +        /*SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.258 +        SDL_FreeSurface(surface);*/
   1.259 +    }
   1.260 +}
   1.261 +
   1.262 +static void
   1.263  NDS_DestroyRenderer(SDL_Renderer * renderer)
   1.264  {
   1.265      NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.266 +    /*SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.267 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
   1.268      int i;
   1.269  
   1.270 -    printf("NDS_DestroyRenderer(renderer)\n");
   1.271 -    printf(" renderer: %s\n", renderer->info.name);
   1.272      if (data) {
   1.273 -        /*for (i = 0; i < SDL_arraysize(data->screens); ++i) {
   1.274 -            if (data->screens[i]) {
   1.275 -                SDL_FreeSurface(data->screens[i]);
   1.276 +        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
   1.277 +            if (data->texture[i]) {
   1.278 +                DestroyTexture(data->renderer, data->texture[i]);
   1.279              }
   1.280 -        }*/
   1.281 +        }
   1.282 +        if (data->surface.format) {
   1.283 +            SDL_SetSurfacePalette(&data->surface, NULL);
   1.284 +            SDL_FreeFormat(data->surface.format);
   1.285 +        }
   1.286 +        if (display->palette) {
   1.287 +            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
   1.288 +                                data);
   1.289 +        }
   1.290 +        SDL_FreeDirtyRects(&data->dirty);
   1.291          SDL_free(data);
   1.292      }
   1.293      SDL_free(renderer);