changeset 2695:c04a266c277a gsoc2008_nds

More sprite-driven texture work in the render/video drivers.
author Darren Alton <dalton@stevens.edu>
date Sun, 17 Aug 2008 09:50:28 +0000
parents c1c7cb1b5a47
children 0b395a60deff
files src/video/nds/SDL_ndsrender.c src/video/nds/SDL_ndsvideo.c test/nds-test-progs/sprite2/Makefile test/nds-test-progs/sprite2/source/testsprite2.c
diffstat 4 files changed, 66 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/video/nds/SDL_ndsrender.c	Sat Aug 16 12:58:36 2008 +0000
+++ b/src/video/nds/SDL_ndsrender.c	Sun Aug 17 09:50:28 2008 +0000
@@ -330,6 +330,8 @@
         }
         if(whichspr >= 0) {
             SpriteEntry *sprent = &(data->oam_copy.spriteBuffer[whichspr]);
+            int maxside = texture->w > texture->h ? texture->w : texture->h;
+            int pitch;
 
             texture->driverdata = SDL_calloc(1, sizeof(NDS_TextureData));
             txdat = (NDS_TextureData*)texture->driverdata;
@@ -349,12 +351,44 @@
                 sprent->rsMatrixIdx = whichspr;
             }
 
+            /* containing shape (square or 2:1 rectangles) */
             sprent->objShape = OBJSHAPE_SQUARE;
             if(texture->w/2 >= texture->h) {
                 sprent->objShape = OBJSHAPE_WIDE;
             } else if(texture->h/2 >= texture->w) {
                 sprent->objShape = OBJSHAPE_TALL;
             }
+
+            /* size in pixels */
+            /* FIXME: "pitch" is hardcoded for 2bytes per pixel. */
+            sprent->objSize = OBJSIZE_64;
+            pitch = 128;
+            if(maxside <= 8) {
+                sprent->objSize = OBJSIZE_8;
+                pitch = 16;
+            } else if(maxside <= 16) {
+                sprent->objSize = OBJSIZE_16;
+                pitch = 32;
+            } else if(maxside <= 32) {
+                sprent->objSize = OBJSIZE_32;
+                pitch = 64;
+            }
+
+            /* FIXME: this is hard-coded and will obviously only work for one
+               sprite-texture.  tells it to look at the beginning of SPRITE_GFX
+               for its pixels. */
+            sprent->tileIdx = 0;
+
+            /* now for the texture data */
+            txdat->type = NDSTX_SPR;
+            txdat->hw_index = whichspr;
+            txdat->dim.hdx = 0x100; txdat->dim.hdy = 0;
+            txdat->dim.vdx = 0;     txdat->dim.vdy = 0x100;
+            txdat->dim.pitch = pitch;
+            txdat->dim.bpp = bpp;
+            txdat->vram_pixels = (u16*)(data->sub ?
+                SPRITE_GFX_SUB : SPRITE_GFX); /* FIXME: use tileIdx*boundary
+                                                 to point to proper location */
         } else {
             SDL_SetError("Out of NDS sprites.");
         }
@@ -374,9 +408,7 @@
                 return -1;
             }
 
-            /* TODO: maybe this should be in RenderPresent or RenderCopy
-               instead, copying from a malloc'd system RAM pixel buffer. */
-            /* this is hard-coded to being 256x256 for now. */
+            /* this is hard-coded to being 256x256 ABGR1555 for now. */
             data->bg->control[whichbg] = (bpp == 8) ?
                 BG_BMP8_256x256 : BG_BMP16_256x256;
 
@@ -389,7 +421,7 @@
             txdat->hw_index = whichbg;
             txdat->dim.hdx = 0x100; txdat->dim.hdy = 0;
             txdat->dim.vdx = 0;     txdat->dim.vdy = 0x100;
-            txdat->dim.pitch = texture->w * ((bpp+1)/8);
+            txdat->dim.pitch = 512;
             txdat->dim.bpp = bpp;
             txdat->vram_pixels = (u16*)(data->sub ?
                 BG_BMP_RAM_SUB(base) : BG_BMP_RAM(base));
@@ -538,7 +570,7 @@
         SpriteEntry *spr = &(data->oam_copy.spriteBuffer[txdat->hw_index]);
         spr->posX = dstrect->x;
         spr->posY = dstrect->y;
-        if(txdat->hw_index < MATRIX_COUNT) {
+        if(txdat->hw_index < MATRIX_COUNT && spr->isRotoscale) {
             SpriteRotation *sprot = &(data->oam_copy.matrixBuffer[txdat->hw_index]);
             sprot->hdx = txdat->dim.hdx;
             sprot->hdy = txdat->dim.hdy;
@@ -573,9 +605,9 @@
 static void
 NDS_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 {
+    NDS_TextureData *txdat = texture->driverdata;
     TRACE("+NDS_DestroyTexture\n");
     /* free anything else allocated for texture */
-    NDS_TextureData *txdat = texture->driverdata;
     /*SDL_FreeDirtyRects(&txdat->dirty);*/
     SDL_free(txdat);
     TRACE("-NDS_DestroyTexture\n");
@@ -585,8 +617,8 @@
 NDS_DestroyRenderer(SDL_Renderer * renderer)
 {
     NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata;
-    /*SDL_Window *window = SDL_GetWindowFromID(renderer->window);
-    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);*/
+    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
+    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
     int i;
 
     TRACE("+NDS_DestroyRenderer\n");
@@ -623,8 +655,8 @@
 NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
                      SDL_Color * colors, int firstcolor, int ncolors)
 {
+    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
     TRACE("+NDS_GetTexturePalette\n");
-    NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
     TRACE("-NDS_GetTexturePalette\n");
     return 0;
 }
--- a/src/video/nds/SDL_ndsvideo.c	Sat Aug 16 12:58:36 2008 +0000
+++ b/src/video/nds/SDL_ndsvideo.c	Sun Aug 17 09:50:28 2008 +0000
@@ -150,6 +150,8 @@
                  DISPLAY_BG3_ACTIVE |
                  DISPLAY_BG_EXT_PALETTE |
                  DISPLAY_SPR_1D_LAYOUT |
+                 DISPLAY_SPR_1D_BMP |
+                 DISPLAY_SPR_1D_BMP_SIZE_256 | /* try 128 if 256 is trouble. */
                  DISPLAY_SPR_ACTIVE |
                  DISPLAY_SPR_EXT_PALETTE); /* display on main core
                                               with lots of flags set for
--- a/test/nds-test-progs/sprite2/Makefile	Sat Aug 16 12:58:36 2008 +0000
+++ b/test/nds-test-progs/sprite2/Makefile	Sun Aug 17 09:50:28 2008 +0000
@@ -119,7 +119,7 @@
 $(OUTPUT).elf	:	$(OFILES)
  
 #---------------------------------------------------------------------------------
-%.pcx.o	:	%.pcx
+%.bin.o	:	%.bin
 #---------------------------------------------------------------------------------
 	@echo $(notdir $<)
 	@$(bin2o)
--- a/test/nds-test-progs/sprite2/source/testsprite2.c	Sat Aug 16 12:58:36 2008 +0000
+++ b/test/nds-test-progs/sprite2/source/testsprite2.c	Sun Aug 17 09:50:28 2008 +0000
@@ -7,6 +7,8 @@
 #include <fat.h>
 #include "common.h"
 
+#include "icon_bmp_bin.h"
+
 #define NUM_SPRITES	10
 #define MAX_SPEED 	1
 
@@ -79,6 +81,24 @@
     return (0);
 }
 
+int LoadSprite2(const u8* ptr, int size) {
+    int i;
+    SDL_Rect r = {0,0,32,32};
+    for (i = 0; i < state->num_windows; ++i) {
+        SDL_SelectRenderer(state->windows[i]);
+        sprites[i] = SDL_CreateTexture(SDL_PIXELFORMAT_ABGR1555,
+                                       SDL_TEXTUREACCESS_STATIC, r.w, r.h);
+        if (!sprites[i]) {
+            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
+            return -1;
+        }
+        SDL_UpdateTexture(sprites[i], &r, ptr, r.w*2);
+        SDL_SetTextureBlendMode(sprites[i], blendMode);
+        SDL_SetTextureScaleMode(sprites[i], scaleMode);
+    }
+    return 0;
+}
+
 void
 MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
 {
@@ -236,7 +256,8 @@
         SDL_SelectRenderer(state->windows[i]);
         SDL_RenderFill(0xA0, 0xA0, 0xA0, 0xFF, NULL);
     }
-    if (LoadSprite("icon.bmp") < 0) {
+    if (LoadSprite2(icon_bmp_bin, icon_bmp_bin_size) < 0) {
+        printf("errored.\n");
         quit(2);
     }