Cleaning up the renderer code to hopefully squish any problems. Starting to incorporate sprites, as well gsoc2008_nds
authorDarren Alton <dalton@stevens.edu>
Fri, 15 Aug 2008 10:17:07 +0000
branchgsoc2008_nds
changeset 268871c56e900f8b
parent 2687 5166b19b6808
child 2689 e065c9f6a393
Cleaning up the renderer code to hopefully squish any problems. Starting to incorporate sprites, as well
src/video/nds/SDL_ndsrender.c
src/video/nds/SDL_ndsvideo.c
test/nds-test-progs/sprite/source/testsprite.c
     1.1 --- a/src/video/nds/SDL_ndsrender.c	Thu Aug 14 04:33:55 2008 +0000
     1.2 +++ b/src/video/nds/SDL_ndsrender.c	Fri Aug 15 10:17:07 2008 +0000
     1.3 @@ -23,6 +23,9 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <nds.h>
     1.7 +#include <nds/arm9/video.h>
     1.8 +#include <nds/arm9/sprite.h>
     1.9 +#include <nds/arm9/trig_lut.h>
    1.10  
    1.11  #include "SDL_config.h"
    1.12  
    1.13 @@ -32,6 +35,66 @@
    1.14  #include "../SDL_renderer_sw.h"
    1.15  
    1.16  
    1.17 +
    1.18 +/* NDS sprite-related functions */
    1.19 +#define SPRITE_DMA_CHANNEL 3
    1.20 +#define SPRITE_ANGLE_MASK 0x01FF
    1.21 +
    1.22 +void
    1.23 +NDS_OAM_Update(tOAM *oam)
    1.24 +{
    1.25 +    DC_FlushAll();
    1.26 +    dmaCopyHalfWords(SPRITE_DMA_CHANNEL, oam->spriteBuffer, OAM,
    1.27 +                     SPRITE_COUNT * sizeof(SpriteEntry));
    1.28 +}
    1.29 +
    1.30 +void
    1.31 +NDS_OAM_RotateSprite(SpriteRotation *spriteRotation, u16 angle)
    1.32 +{
    1.33 +    s16 s = SIN[angle & SPRITE_ANGLE_MASK] >> 4;
    1.34 +    s16 c = COS[angle & SPRITE_ANGLE_MASK] >> 4;
    1.35 +
    1.36 +    spriteRotation->hdx = c;
    1.37 +    spriteRotation->hdy = s;
    1.38 +    spriteRotation->vdx = -s;
    1.39 +    spriteRotation->vdy = c;
    1.40 +}
    1.41 +
    1.42 +void
    1.43 +NDS_OAM_Init(tOAM *oam)
    1.44 +{
    1.45 +    int i;
    1.46 +    for(i = 0; i < SPRITE_COUNT; i++) {
    1.47 +        oam->spriteBuffer[i].attribute[0] = ATTR0_DISABLED;
    1.48 +        oam->spriteBuffer[i].attribute[1] = 0;
    1.49 +        oam->spriteBuffer[i].attribute[2] = 0;
    1.50 +    }
    1.51 +    for(i = 0; i < MATRIX_COUNT; i++) {
    1.52 +        NDS_OAM_RotateSprite(&(oam->matrixBuffer[i]), 0);
    1.53 +    }
    1.54 +    swiWaitForVBlank();
    1.55 +    NDS_OAM_Update(oam);
    1.56 +}
    1.57 +
    1.58 +void
    1.59 +NDS_OAM_HideSprite(SpriteEntry *spriteEntry)
    1.60 +{
    1.61 +    spriteEntry->isRotoscale = 0;
    1.62 +    spriteEntry->isHidden = 1;
    1.63 +}
    1.64 +
    1.65 +void
    1.66 +NDS_OAM_ShowSprite(SpriteEntry *spriteEntry, int affine, int double_bound)
    1.67 +{
    1.68 +    if (affine) {
    1.69 +        spriteEntry->isRotoscale = 1;
    1.70 +        spriteEntry->rsDouble = double_bound;
    1.71 +    } else {
    1.72 +        spriteEntry->isHidden = 0;
    1.73 +    }
    1.74 +}
    1.75 +
    1.76 +
    1.77  /* SDL NDS renderer implementation */
    1.78  
    1.79  static SDL_Renderer *NDS_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.80 @@ -97,9 +160,9 @@
    1.81  
    1.82  typedef struct
    1.83  {
    1.84 -    bg_attribute *bg;
    1.85 +    bg_attribute *bg; /* backgrounds */
    1.86 +    tOAM oam_copy; /* sprites */
    1.87      u8 bg_taken[4];
    1.88 -    /* todo for sprites: pSpriteRotation and pSpriteEntry. pointers to OAM */
    1.89      int sub;
    1.90  } NDS_RenderData;
    1.91  
    1.92 @@ -114,21 +177,6 @@
    1.93  } NDS_TextureData;
    1.94  
    1.95  
    1.96 -/* this is mainly hackish testing/debugging stuff to get cleaned up soon
    1.97 -   anything named sdlds_blah shouldn't make it into the stable version
    1.98 - */
    1.99 -
   1.100 -u16
   1.101 -sdlds_rgb2bgr(u16 c)
   1.102 -{
   1.103 -/* hack to get the proper colors until I actually get BGR555 to work right */
   1.104 -    u16 Rmask = 0x7C00, Bmask = 0x001F, GAmask = 0x83E0, r, b;
   1.105 -    r = (c & Rmask) >> 10;
   1.106 -    b = (c & Bmask) << 10;
   1.107 -    return (c & GAmask) | r | b;
   1.108 -}
   1.109 -
   1.110 -/* again the above shouldn't make it into the stable version */
   1.111  
   1.112  SDL_Renderer *
   1.113  NDS_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.114 @@ -138,19 +186,25 @@
   1.115      SDL_Renderer *renderer;
   1.116      NDS_RenderData *data;
   1.117      int i, n;
   1.118 -    int bpp = 15;
   1.119 +    int bpp;
   1.120      Uint32 Rmask, Gmask, Bmask, Amask;
   1.121 -/*  Uint32 Rmask = 0x7C00, Gmask = 0x03E0, Bmask = 0x001F, Amask = 0x8000;
   1.122 -    Uint32 Rmask = 0x001F, Gmask = 0x03E0, Bmask = 0x7C00, Amask = 0x8000;
   1.123 -*/
   1.124 +
   1.125  printf("+NDS_CreateRenderer\n");
   1.126 -
   1.127 -    /* hard coded this to BGR555 for now */
   1.128 -    if (!SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_BGR555, &bpp,
   1.129 +    if (!SDL_PixelFormatEnumToMasks(displayMode->format, &bpp,
   1.130                                      &Rmask, &Gmask, &Bmask, &Amask)) {
   1.131          SDL_SetError("Unknown display format");
   1.132          return NULL;
   1.133      }
   1.134 +    switch(displayMode->format) {
   1.135 +        case SDL_PIXELFORMAT_INDEX8:
   1.136 +        case SDL_PIXELFORMAT_ABGR1555:
   1.137 +        case SDL_PIXELFORMAT_BGR555:
   1.138 +            /* okay */
   1.139 +            break;
   1.140 +        default:
   1.141 +            printf("DEBUG: wrong display format!\n");
   1.142 +            break;
   1.143 +    }
   1.144  
   1.145      renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   1.146      if (!renderer) {
   1.147 @@ -200,10 +254,13 @@
   1.148      renderer->info.max_texture_height =
   1.149          NDS_RenderDriver.info.max_texture_height;
   1.150  
   1.151 -    /*data->fb = (u16*)0x06020000;*/
   1.152 -    data->bg = &BACKGROUND;
   1.153 +    data->bg = &BACKGROUND; /* BACKGROUND_SUB for second screen. */
   1.154      data->bg_taken[2] = data->bg_taken[3] = 0;
   1.155 -    data->sub = 0;
   1.156 +    NDS_OAM_Init(&(data->oam_copy)); /* init sprites. */
   1.157 +
   1.158 +    data->sub = 0; /* TODO: this is hard-coded to the "main" screen.
   1.159 +                            figure out how to detect whether to set it to
   1.160 +                            "sub" screen.  window->id, perhaps? */
   1.161  
   1.162  printf("-NDS_CreateRenderer\n");
   1.163      return renderer;
   1.164 @@ -213,8 +270,7 @@
   1.165  NDS_ActivateRenderer(SDL_Renderer * renderer)
   1.166  {
   1.167      NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.168 -    /* stub. TODO: figure out what needs to be done, if anything. */
   1.169 -printf("!NDS_ActivateRenderer\n");
   1.170 +    printf("!NDS_ActivateRenderer\n");
   1.171      return 0;
   1.172  }
   1.173  
   1.174 @@ -222,8 +278,7 @@
   1.175  NDS_DisplayModeChanged(SDL_Renderer * renderer)
   1.176  {
   1.177      NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.178 -    /* stub. TODO: figure out what needs to be done */
   1.179 -printf("!NDS_DisplayModeChanged\n");
   1.180 +    printf("!NDS_DisplayModeChanged\n");
   1.181      return 0;
   1.182  }
   1.183  
   1.184 @@ -233,59 +288,63 @@
   1.185      NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
   1.186      NDS_TextureData *txdat = NULL;
   1.187      int i;
   1.188 -printf("+NDS_CreateTexture\n");
   1.189 -    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.190 -        SDL_SetError("Unsupported texture format");
   1.191 +    int bpp;
   1.192 +    Uint32 Rmask, Gmask, Bmask, Amask;
   1.193 +
   1.194 +    printf("+NDS_CreateTexture\n");
   1.195 +    if (!SDL_PixelFormatEnumToMasks
   1.196 +        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   1.197 +        SDL_SetError("Unknown texture format");
   1.198          return -1;
   1.199 -    } else {
   1.200 -        int bpp;
   1.201 -        Uint32 Rmask, Gmask, Bmask, Amask;
   1.202 -
   1.203 -        if (!SDL_PixelFormatEnumToMasks
   1.204 -            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   1.205 -            SDL_SetError("Unknown texture format");
   1.206 -            return -1;
   1.207 -        }
   1.208 -        /* conditional statements on w/h to place it as bg/sprite */
   1.209 -        /*if(texture->w <= 64 && texture->h <= 64) {
   1.210 -            sprites not implemented yet.  elegant, I know.
   1.211 -        } else*/ if(texture->w <= 256 && texture->h <= 256) {
   1.212 -            int whichbg = -1;
   1.213 -            if(!data->bg_taken[2]) {
   1.214 -                whichbg = 2;
   1.215 -            } else if(!data->bg_taken[3]) {
   1.216 -                whichbg = 3;
   1.217 -            }
   1.218 -            if(whichbg >= 0) {
   1.219 -                data->bg->control[whichbg] = (bpp == 8) ?
   1.220 -                    BG_BMP8_256x256 : BG_BMP16_256x256;
   1.221 -                data->bg->scroll[whichbg].x = 0;
   1.222 -                data->bg->scroll[whichbg].y = 0;
   1.223 -                texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
   1.224 -                txdat = (NDS_TextureData*)texture->driverdata;
   1.225 -                txdat->type = NDSTX_BG;
   1.226 -                txdat->hw_index = whichbg;
   1.227 -                txdat->dim.hdx = 0x100; txdat->dim.hdy = 0;
   1.228 -                txdat->dim.vdx = 0;     txdat->dim.vdy = 0x100;
   1.229 -                txdat->dim.pitch = 256 * (bpp/8);
   1.230 -                txdat->dim.bpp = bpp;
   1.231 -                txdat->vram = (u16*)(data->sub ?
   1.232 -                    BG_BMP_RAM_SUB(whichbg) : BG_BMP_RAM(whichbg));
   1.233 -                txdat->size = txdat->dim.pitch * texture->h;
   1.234 -                txdat->system_ram_copy = SDL_malloc(txdat->size);
   1.235 -            } else {
   1.236 -                SDL_SetError("Out of NDS backgrounds.");
   1.237 -                printf("ran out.\n");
   1.238 -            }
   1.239 -        } else {
   1.240 -            SDL_SetError("Texture too big for NDS hardware.");
   1.241 -        }
   1.242      }
   1.243  
   1.244 -printf("-NDS_CreateTexture\n");
   1.245 +    /* conditional statements on w/h to place it as bg/sprite
   1.246 +       depending on which one it fits. */
   1.247 +    if(texture->w <= 64 && texture->h <= 64) {
   1.248 +        /* TODO: implement sprites similar to how BG's are.
   1.249 +           they have a similar affine transformation matrix
   1.250 +           (hdx,hdy,vdx,vdy) as the backgrounds, so it should
   1.251 +           be similar enough to handle with the same driverdata. */
   1.252 +        printf("Tried to make a sprite.\n");
   1.253 +    } else if(texture->w <= 256 && texture->h <= 256) {
   1.254 +        int whichbg = -1;
   1.255 +        if(!data->bg_taken[2]) {
   1.256 +            whichbg = 2;
   1.257 +        } else if(!data->bg_taken[3]) {
   1.258 +            whichbg = 3;
   1.259 +        }
   1.260 +        if(whichbg >= 0) {
   1.261 +            /* TODO: maybe this should be in RenderPresent or RenderCopy
   1.262 +               instead, copying from a malloc'd system RAM pixel buffer. */
   1.263 +            data->bg->control[whichbg] = (bpp == 8) ?
   1.264 +                BG_BMP8_256x256 : BG_BMP16_256x256;
   1.265 +            data->bg->scroll[whichbg].x = 0;
   1.266 +            data->bg->scroll[whichbg].y = 0;
   1.267 +            texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
   1.268 +            txdat = (NDS_TextureData*)texture->driverdata;
   1.269 +            txdat->type = NDSTX_BG;
   1.270 +            txdat->hw_index = whichbg;
   1.271 +            txdat->dim.hdx = 0x100; txdat->dim.hdy = 0;
   1.272 +            txdat->dim.vdx = 0;     txdat->dim.vdy = 0x100;
   1.273 +            txdat->dim.pitch = 256 * (bpp/8);
   1.274 +            txdat->dim.bpp = bpp;
   1.275 +            txdat->vram = (u16*)(data->sub ?
   1.276 +                BG_BMP_RAM_SUB(whichbg) : BG_BMP_RAM(whichbg));
   1.277 +            txdat->size = txdat->dim.pitch * texture->h;
   1.278 +        } else {
   1.279 +            SDL_SetError("Out of NDS backgrounds.");
   1.280 +            printf("ran out.\n");
   1.281 +        }
   1.282 +    } else {
   1.283 +        SDL_SetError("Texture too big for NDS hardware.");
   1.284 +    }
   1.285 +
   1.286 +    printf("-NDS_CreateTexture\n");
   1.287      if (!texture->driverdata) {
   1.288 +        SDL_SetError("Couldn't create NDS render driver data.");
   1.289          return -1;
   1.290      }
   1.291 +
   1.292      return 0;
   1.293  }
   1.294  
   1.295 @@ -293,47 +352,36 @@
   1.296  NDS_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   1.297                        void **pixels, int *pitch)
   1.298  {
   1.299 -printf("+NDS_QueryTexturePixels\n");
   1.300 -    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.301 -        SDL_SetError("Unsupported texture format");
   1.302 -        return -1;
   1.303 -    } else {
   1.304 -        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.305 -
   1.306 -        *pixels = txdat->vram;
   1.307 -        *pitch = txdat->dim.pitch;
   1.308 -printf("-NDS_QueryTexturePixels\n");
   1.309 -        return 0;
   1.310 -    }
   1.311 +    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.312 +    printf("+NDS_QueryTexturePixels\n");
   1.313 +    *pixels = txdat->vram;
   1.314 +    *pitch = txdat->dim.pitch;
   1.315 +    printf("-NDS_QueryTexturePixels\n");
   1.316 +    return 0;
   1.317  }
   1.318  
   1.319  static int
   1.320  NDS_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.321                   const SDL_Rect * rect, const void *pixels, int pitch)
   1.322  {
   1.323 -printf("+NDS_UpdateTexture\n");
   1.324 -    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.325 -        SDL_SetError("Unsupported texture format");
   1.326 -        return -1;
   1.327 -    } else {
   1.328 -        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.329 -        Uint8 *src, *dst;
   1.330 -        int row;
   1.331 -        size_t length;
   1.332 -        /* IMPORTANT! copy the new pixels into the sprite or bg. */
   1.333 -        src = (Uint8 *) pixels;
   1.334 -        dst =
   1.335 -            (Uint8 *) txdat->system_ram_copy + rect->y * txdat->dim.pitch +
   1.336 -            rect->x * (txdat->dim.bpp/8);
   1.337 -        length = rect->w * (txdat->dim.bpp/8);
   1.338 -        for (row = 0; row < rect->h; ++row) {
   1.339 -            SDL_memcpy(dst, src, length);
   1.340 -            src += pitch;
   1.341 -            dst += txdat->dim.pitch;
   1.342 -        }
   1.343 -printf("-NDS_UpdateTexture\n");
   1.344 -        return 0;
   1.345 +    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.346 +    Uint8 *src, *dst;
   1.347 +    int row; size_t length;
   1.348 +    printf("+NDS_UpdateTexture\n");
   1.349 +
   1.350 +    src = (Uint8 *) pixels;
   1.351 +    dst =
   1.352 +        (Uint8 *) txdat->system_ram_copy + rect->y * txdat->dim.pitch +
   1.353 +        rect->x * (txdat->dim.bpp/8);
   1.354 +    length = rect->w * (txdat->dim.bpp/8);
   1.355 +    for (row = 0; row < rect->h; ++row) {
   1.356 +        SDL_memcpy(dst, src, length);
   1.357 +        src += pitch;
   1.358 +        dst += txdat->dim.pitch;
   1.359      }
   1.360 +
   1.361 +    printf("-NDS_UpdateTexture\n");
   1.362 +    return 0;
   1.363  }
   1.364  
   1.365  static int
   1.366 @@ -341,41 +389,39 @@
   1.367                 const SDL_Rect * rect, int markDirty, void **pixels,
   1.368                 int *pitch)
   1.369  {
   1.370 -printf("+NDS_LockTexture\n");
   1.371 -    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.372 -        SDL_SetError("Unsupported texture format");
   1.373 -        return -1;
   1.374 -    } else {
   1.375 -        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.376 +    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.377 +    int i;
   1.378 +    printf("+NDS_LockTexture\n");
   1.379 +    if (markDirty) {
   1.380 +        printf("wanted to mark dirty\n");
   1.381 +        /* TODO: figure out how to handle this! */
   1.382 +        /*SDL_AddDirtyRect(&txdat->dirty, rect);*/
   1.383 +    }
   1.384  
   1.385 -        if (markDirty) {
   1.386 -            printf("wanted to mark dirty\n");
   1.387 -            /*SDL_AddDirtyRect(&txdat->dirty, rect);*/
   1.388 -        }
   1.389 -
   1.390 -        *pixels = (void *) ((u8 *)txdat->system_ram_copy + rect->y
   1.391 -                            * txdat->dim.pitch + rect->x * (txdat->dim.bpp/8));
   1.392 -        *pitch = txdat->dim.pitch;
   1.393 -        printf("  pixels = %08x\n", (u32)*pixels);
   1.394 -printf("-NDS_LockTexture\n");
   1.395 -        return 0;
   1.396 -    }
   1.397 +    *pixels = (void *) ((u8 *)txdat->vram + rect->y
   1.398 +                        * txdat->dim.pitch + rect->x * (txdat->dim.bpp/8));
   1.399 +    *pitch = txdat->dim.pitch;
   1.400 +    printf("  pixels = %08x\n", (u32)*pixels);
   1.401 +    printf("  vram = %08x\n", (u32)(txdat->vram));
   1.402 +    printf("-NDS_LockTexture\n");
   1.403 +    return 0;
   1.404  }
   1.405  
   1.406  static void
   1.407  NDS_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.408  {
   1.409 -    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.410 -        SDL_SetError("Unsupported texture format");
   1.411 -    }
   1.412 -printf("+NDS_UnlockTexture\n-NDS_UnlockTexture\n");
   1.413 +    printf("+NDS_UnlockTexture\n");
   1.414 +    /* TODO: should I be doing something here, somehow, now that the pixels
   1.415 +             should have been "written" between LockTexture and this? */
   1.416 +    printf("-NDS_UnlockTexture\n");
   1.417  }
   1.418  
   1.419  static void
   1.420  NDS_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.421                  int numrects, const SDL_Rect * rects)
   1.422 -{ /* stub */
   1.423 -printf("!NDS_DirtyTexture\n");
   1.424 +{
   1.425 +    /* stub */
   1.426 +    printf("!NDS_DirtyTexture\n");
   1.427  }
   1.428  
   1.429  static int
   1.430 @@ -387,16 +433,18 @@
   1.431      u16 color;
   1.432      int i, j;
   1.433  
   1.434 -printf("+NDS_RenderFill\n");
   1.435 +    printf("+NDS_RenderFill\n");
   1.436 +    color = RGB8(r,g,b); /* <-- macro in libnds that makes an ARGB1555 pixel */
   1.437      /* TODO: make a single-color sprite and stretch it.
   1.438 -    color = RGB15(r>>3,g>>3,b>>3);
   1.439 -    for (i = real_rect.x; i < real_rect.x+real_rect.w; ++i) {
   1.440 -        for (j = real_rect.y; j < real_rect.y+real_rect.h; ++j) {
   1.441 -            data->fb[(j + real_rect.y) * 256 + i + real_rect.x] = 
   1.442 -                0x8000 | color;
   1.443 -        }
   1.444 -    }*/
   1.445 -printf("-NDS_RenderFill\n");
   1.446 +       calculate the "HDX" width modifier of the sprite by:
   1.447 +         let S be the actual sprite's width (like, 32 pixels for example)
   1.448 +         let R be the rectangle's width (maybe 50 pixels)
   1.449 +         HDX = (R<<8) / S;
   1.450 +         (it's fixed point, hence the bit shift.  same goes for vertical.
   1.451 +         be sure to use 32-bit int's for the bit shift before the division!)
   1.452 +     */
   1.453 +
   1.454 +    printf("-NDS_RenderFill\n");
   1.455      return 0;
   1.456  }
   1.457  
   1.458 @@ -411,7 +459,7 @@
   1.459      int i;
   1.460      int bpp = SDL_BYTESPERPIXEL(texture->format);
   1.461      int pitch = txdat->dim.pitch;
   1.462 -printf("+NDS_RenderCopy\n");
   1.463 +    printf("+NDS_RenderCopy\n");
   1.464      if(txdat->type == NDSTX_BG) {
   1.465          bg_rotation *tmpbg = (txdat->hw_index == 2) ?
   1.466              &(data->bg->bg2_rotation) : &(data->bg->bg3_rotation);
   1.467 @@ -425,7 +473,7 @@
   1.468          /* sprites not implemented yet */
   1.469      }
   1.470      printf("  txdat->hw_index = %d\n", txdat->hw_index);
   1.471 -printf("-NDS_RenderCopy\n");
   1.472 +    printf("-NDS_RenderCopy\n");
   1.473      return 0;
   1.474  }
   1.475  
   1.476 @@ -437,28 +485,29 @@
   1.477      SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.478      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.479      int i;
   1.480 -    /* Send the data to the display TODO :
   1.481 -       shouldn't it already be there at this point?
   1.482 -       I guess set the BG's and sprites "visible" flags here. */
   1.483 -printf("+NDS_RenderPresent\n");
   1.484 +    printf("+NDS_RenderPresent\n");
   1.485  
   1.486 -    for(i = 0; i < 64; ++i) {
   1.487 +    {
   1.488          SDL_Texture * tx = display->textures[i];
   1.489          NDS_TextureData * txdat = (NDS_TextureData*)tx->driverdata;
   1.490 -        SDL_memcpy(txdat->vram, txdat->system_ram_copy, txdat->size);
   1.491 +        /* Send the data to the display TODO :
   1.492 +           shouldn't it already be there at this point?  from lock/unlock
   1.493 +           giving it the direct address in VRAM of the bg's.
   1.494 +           I guess set the BG's and sprites "visible" flags here,
   1.495 +           if applicable. */
   1.496      }
   1.497  
   1.498      /* vsync for NDS */
   1.499      if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
   1.500          swiWaitForVBlank();
   1.501      }
   1.502 -printf("-NDS_RenderPresent\n");
   1.503 +    printf("-NDS_RenderPresent\n");
   1.504  }
   1.505  
   1.506  static void
   1.507  NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.508  {
   1.509 -printf("+NDS_DestroyTexture\n");
   1.510 +    printf("+NDS_DestroyTexture\n");
   1.511      if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.512          SDL_SetError("Unsupported texture format");
   1.513      } else {
   1.514 @@ -468,7 +517,7 @@
   1.515          SDL_free(txdat->system_ram_copy);
   1.516          SDL_free(txdat);
   1.517      }
   1.518 -printf("-NDS_DestroyTexture\n");
   1.519 +    printf("-NDS_DestroyTexture\n");
   1.520  }
   1.521  
   1.522  static void
   1.523 @@ -479,9 +528,9 @@
   1.524      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
   1.525      int i;
   1.526  
   1.527 -printf("+NDS_DestroyRenderer\n");
   1.528 +    printf("+NDS_DestroyRenderer\n");
   1.529      if (data) {
   1.530 -        /* TODO: free anything relevant. */
   1.531 +        /* TODO: free anything else relevant. */
   1.532          /*for (i = 0; i < SDL_arraysize(data->texture); ++i) {
   1.533              if (data->texture[i]) {
   1.534                  DestroyTexture(data->renderer, data->texture[i]);
   1.535 @@ -499,46 +548,36 @@
   1.536          SDL_free(data);
   1.537      }
   1.538      SDL_free(renderer);
   1.539 -printf("-NDS_DestroyRenderer\n");
   1.540 +    printf("-NDS_DestroyRenderer\n");
   1.541  }
   1.542  
   1.543  static int
   1.544  NDS_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.545                       const SDL_Color * colors, int firstcolor, int ncolors)
   1.546  {
   1.547 -printf("+NDS_SetTexturePalette\n");
   1.548 -    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.549 -        SDL_SetError("YUV textures don't have a palette");
   1.550 -        return -1;
   1.551 -    } else {
   1.552 -        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.553 -        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
   1.554 -printf("-NDS_SetTexturePalette\n");
   1.555 -        return 0;
   1.556 -    }
   1.557 +    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.558 +    printf("+NDS_SetTexturePalette\n");
   1.559 +    /* set 8-bit modes in the background control registers
   1.560 +       for backgrounds, BGn_CR |= BG_256_COLOR */
   1.561 +    printf("-NDS_SetTexturePalette\n");
   1.562 +    return 0;
   1.563  }
   1.564  
   1.565  static int
   1.566  NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.567                       SDL_Color * colors, int firstcolor, int ncolors)
   1.568  {
   1.569 -printf("+NDS_GetTexturePalette\n");
   1.570 -    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.571 -        SDL_SetError("YUV textures don't have a palette");
   1.572 -        return -1;
   1.573 -    } else {
   1.574 -        NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.575 -printf("-NDS_GetTexturePalette\n");
   1.576 -        /* TODO: mess with 8-bit modes and/or 16-color palette modes */
   1.577 -        return 0;
   1.578 -    }
   1.579 +    printf("+NDS_GetTexturePalette\n");
   1.580 +    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
   1.581 +    printf("-NDS_GetTexturePalette\n");
   1.582 +    return 0;
   1.583  }
   1.584  
   1.585  static int
   1.586  NDS_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.587  {
   1.588 -printf("!NDS_SetTextureColorMod\n");
   1.589 -    /* stub. TODO: figure out what needs to be done, if anything */
   1.590 +    printf("!NDS_SetTextureColorMod\n");
   1.591 +    /* stub! */
   1.592      return 0;
   1.593  }
   1.594  
   1.595 @@ -546,7 +585,7 @@
   1.596  NDS_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   1.597  {
   1.598  printf("!NDS_SetTextureAlphaMod\n");
   1.599 -    /* stub. TODO: figure out what needs to be done, if anything */
   1.600 +    /* stub! */
   1.601      return 0;
   1.602  }
   1.603  
   1.604 @@ -554,7 +593,7 @@
   1.605  NDS_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.606  {
   1.607  printf("!NDS_SetTextureBlendMode\n");
   1.608 -    /* stub. TODO: figure out what needs to be done, if anything */
   1.609 +    /* stub! */
   1.610      return 0;
   1.611  }
   1.612  
   1.613 @@ -562,8 +601,7 @@
   1.614  NDS_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   1.615  {
   1.616  printf("!NDS_SetTextureScaleMode\n");
   1.617 -    /* stub. TODO: figure out what needs to be done.
   1.618 -       (NDS hardware scaling is nearest neighbor.) */
   1.619 +    /* stub! (note: NDS hardware scaling is nearest neighbor.) */
   1.620      return 0;
   1.621  }
   1.622  
     2.1 --- a/src/video/nds/SDL_ndsvideo.c	Thu Aug 14 04:33:55 2008 +0000
     2.2 +++ b/src/video/nds/SDL_ndsvideo.c	Fri Aug 15 10:17:07 2008 +0000
     2.3 @@ -31,8 +31,6 @@
     2.4  #include <stdio.h>
     2.5  #include <stdlib.h>
     2.6  #include <nds.h>
     2.7 -#include <nds/arm9/sprite.h>
     2.8 -#include <nds/arm9/trig_lut.h>
     2.9  #include <nds/arm9/video.h>
    2.10  
    2.11  #include "SDL_video.h"
    2.12 @@ -52,65 +50,6 @@
    2.13  static int NDS_SetDisplayMode(_THIS, SDL_DisplayMode * mode);
    2.14  static void NDS_VideoQuit(_THIS);
    2.15  
    2.16 -/* NDS sprite-related functions */
    2.17 -
    2.18 -#define SPRITE_DMA_CHANNEL 3
    2.19 -#define SPRITE_ANGLE_MASK 0x01FF
    2.20 -
    2.21 -void
    2.22 -NDS_OAM_Update(tOAM *oam)
    2.23 -{
    2.24 -    DC_FlushAll();
    2.25 -    dmaCopyHalfWords(SPRITE_DMA_CHANNEL, oam->spriteBuffer, OAM,
    2.26 -                     SPRITE_COUNT * sizeof(SpriteEntry));
    2.27 -}
    2.28 -
    2.29 -void
    2.30 -NDS_OAM_RotateSprite(SpriteRotation *spriteRotation, u16 angle)
    2.31 -{
    2.32 -    s16 s = SIN[angle & SPRITE_ANGLE_MASK] >> 4;
    2.33 -    s16 c = COS[angle & SPRITE_ANGLE_MASK] >> 4;
    2.34 -
    2.35 -    spriteRotation->hdx = c;
    2.36 -    spriteRotation->hdy = s;
    2.37 -    spriteRotation->vdx = -s;
    2.38 -    spriteRotation->vdy = c;
    2.39 -}
    2.40 -
    2.41 -void
    2.42 -NDS_OAM_Init(tOAM *oam)
    2.43 -{
    2.44 -    int i;
    2.45 -    for(i = 0; i < SPRITE_COUNT; i++) {
    2.46 -        oam->spriteBuffer[i].attribute[0] = ATTR0_DISABLED;
    2.47 -        oam->spriteBuffer[i].attribute[1] = 0;
    2.48 -        oam->spriteBuffer[i].attribute[2] = 0;
    2.49 -    }
    2.50 -    for(i = 0; i < MATRIX_COUNT; i++) {
    2.51 -        NDS_OAM_RotateSprite(&(oam->matrixBuffer[i]), 0);
    2.52 -    }
    2.53 -    swiWaitForVBlank();
    2.54 -    NDS_OAM_Update(oam);
    2.55 -}
    2.56 -
    2.57 -void
    2.58 -NDS_OAM_HideSprite(SpriteEntry *spriteEntry)
    2.59 -{
    2.60 -    spriteEntry->isRotoscale = 0;
    2.61 -    spriteEntry->isHidden = 1;
    2.62 -}
    2.63 -
    2.64 -void
    2.65 -NDS_OAM_ShowSprite(SpriteEntry *spriteEntry, int affine, int double_bound)
    2.66 -{
    2.67 -    if (affine) {
    2.68 -        spriteEntry->isRotoscale = 1;
    2.69 -        spriteEntry->rsDouble = double_bound;
    2.70 -    } else {
    2.71 -        spriteEntry->isHidden = 0;
    2.72 -    }
    2.73 -}
    2.74 -
    2.75  
    2.76  /* SDL NDS driver bootstrap functions */
    2.77  static int
    2.78 @@ -169,7 +108,7 @@
    2.79      SDL_DisplayMode mode;
    2.80      int i;
    2.81  
    2.82 -printf("+NDS_VideoInit\n");
    2.83 +    printf("+NDS_VideoInit\n");
    2.84      /* simple 256x192x16x60 for now */
    2.85      mode.w = 256;
    2.86      mode.h = 192;
    2.87 @@ -189,40 +128,58 @@
    2.88      powerON(POWER_ALL_2D);
    2.89     irqInit();
    2.90      irqEnable(IRQ_VBLANK);
    2.91      NDS_SetDisplayMode(_this, &mode);
    2.92 -printf("-NDS_VideoInit\n");
    2.93 +    printf("-NDS_VideoInit\n");
    2.94      return 0;
    2.95  }
    2.96  
    2.97  static int
    2.98  NDS_SetDisplayMode(_THIS, SDL_DisplayMode * mode)
    2.99  {
   2.100 -printf("+NDS_SetDisplayMode\n");
   2.101 +    printf("+NDS_SetDisplayMode\n");
   2.102      /* right now this function is just hard-coded for 256x192 ABGR1555 */
   2.103      videoSetMode(MODE_5_2D |
   2.104 -        DISPLAY_BG2_ACTIVE | DISPLAY_BG3_ACTIVE |
   2.105 -        DISPLAY_SPR_1D_LAYOUT | DISPLAY_SPR_ACTIVE); /* display on main core */
   2.106 -    videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); /* debug text on sub */
   2.107 -    vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_MAIN_BG_0x06020000,
   2.108 -                     VRAM_C_SUB_BG_0x06200000,  VRAM_D_MAIN_BG_0x06040000);
   2.109 +                 DISPLAY_BG2_ACTIVE |
   2.110 +                 DISPLAY_BG3_ACTIVE |
   2.111 +                 DISPLAY_BG_EXT_PALETTE |
   2.112 +                 DISPLAY_SPR_1D_LAYOUT |
   2.113 +                 DISPLAY_SPR_ACTIVE |
   2.114 +                 DISPLAY_SPR_EXT_PALETTE); /* display on main core
   2.115 +                                              with lots of flags set for
   2.116 +                                              flexibility/capacity to render */
   2.117 +
   2.118 +    /* hopefully these cover all the various things we might need to do */
   2.119 +    vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
   2.120 +    vramSetBankB(VRAM_B_MAIN_BG_0x06020000);
   2.121 +    vramSetBankC(VRAM_C_SUB_BG_0x06200000);
   2.122 +    vramSetBankD(VRAM_D_MAIN_BG_0x06040000); /* not a typo. vram d can't sub */
   2.123      vramSetBankE(VRAM_E_MAIN_SPRITE);
   2.124 -    /* set up console for debug text 'n stuff */
   2.125 -    SUB_BG0_CR = BG_MAP_BASE(31);
   2.126 -    BG_PALETTE_SUB[255] = RGB15(31, 31, 31);
   2.127 -   /* debugging purposes, uncomment this later.  then remove it & add 2screen.
   2.128 -    consoleInitDefault((u16 *) SCREEN_BASE_BLOCK_SUB(31),
   2.129 -                       (u16 *) CHAR_BASE_BLOCK_SUB(0), 16);*/
   2.130 -printf("-NDS_SetDisplayMode\n");
   2.131 +    vramSetBankF(VRAM_F_OBJ_EXT_PALETTE);
   2.132 +    vramSetBankG(VRAM_G_BG_EXT_PALETTE);
   2.133 +    vramSetBankH(VRAM_H_SUB_BG_EXT_PALETTE);
   2.134 +    vramSetBankI(VRAM_I_SUB_SPRITE);
   2.135 +
   2.136 +    videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); /* debug text on sub
   2.137 +                                                        TODO: this will change
   2.138 +                                                        when multi-head is
   2.139 +                                                        introduced in render */
   2.140 +
   2.141 +    printf("-NDS_SetDisplayMode\n");
   2.142      return 0;
   2.143  }
   2.144  
   2.145  void
   2.146  NDS_VideoQuit(_THIS)
   2.147  {
   2.148 -printf("+NDS_VideoQuit\n");
   2.149 +    printf("+NDS_VideoQuit\n");
   2.150      videoSetMode(DISPLAY_SCREEN_OFF);
   2.151      videoSetModeSub(DISPLAY_SCREEN_OFF);
   2.152      vramSetMainBanks(VRAM_A_LCD, VRAM_B_LCD, VRAM_C_LCD, VRAM_D_LCD);
   2.153 -printf("-NDS_VideoQuit\n");
   2.154 +    vramSetBankE(VRAM_E_LCD);
   2.155 +    vramSetBankF(VRAM_F_LCD);
   2.156 +    vramSetBankG(VRAM_G_LCD);
   2.157 +    vramSetBankH(VRAM_H_LCD);
   2.158 +    vramSetBankI(VRAM_I_LCD);
   2.159 +    printf("-NDS_VideoQuit\n");
   2.160  }
   2.161  
   2.162  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/test/nds-test-progs/sprite/source/testsprite.c	Thu Aug 14 04:33:55 2008 +0000
     3.2 +++ b/test/nds-test-progs/sprite/source/testsprite.c	Fri Aug 15 10:17:07 2008 +0000
     3.3 @@ -4,8 +4,8 @@
     3.4  #include <stdio.h>
     3.5  #include <time.h>
     3.6  #include <math.h>
     3.7 -
     3.8 -#include "SDL.h"
     3.9 +#include <fat.h>
    3.10 +#include <SDL/SDL.h>
    3.11  
    3.12  #define NUM_SPRITES	100
    3.13  #define MAX_SPEED 	1
    3.14 @@ -97,8 +97,8 @@
    3.15  
    3.16              Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
    3.17              SDL_Rect r;
    3.18 -            r.x =
    3.19 -                (sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20);
    3.20 +            r.x = t;
    3.21 +/* (sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20); */
    3.22              r.y = 0;
    3.23              r.w = 20;
    3.24              r.h = screen->h;
    3.25 @@ -162,7 +162,7 @@
    3.26      int i, done;
    3.27      SDL_Event event;
    3.28      Uint32 then, now, frames;
    3.29 -
    3.30 +    fatInitDefault();
    3.31      /* Initialize SDL */
    3.32      if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    3.33          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());