Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
More sprite-driven texture work in the render/video drivers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Alton committed Aug 17, 2008
1 parent eee4eee commit 9f8db75
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
50 changes: 41 additions & 9 deletions src/video/nds/SDL_ndsrender.c
Expand Up @@ -330,6 +330,8 @@ NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
}
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;
Expand All @@ -349,12 +351,44 @@ NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
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.");
}
Expand All @@ -374,9 +408,7 @@ NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
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;

Expand All @@ -389,7 +421,7 @@ NDS_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
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));
Expand Down Expand Up @@ -538,7 +570,7 @@ NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
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;
Expand Down Expand Up @@ -573,9 +605,9 @@ NDS_RenderPresent(SDL_Renderer * renderer)
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");
Expand All @@ -585,8 +617,8 @@ static void
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");
Expand Down Expand Up @@ -623,8 +655,8 @@ static int
NDS_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
SDL_Color * colors, int firstcolor, int ncolors)
{
TRACE("+NDS_GetTexturePalette\n");
NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata;
TRACE("+NDS_GetTexturePalette\n");
TRACE("-NDS_GetTexturePalette\n");
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions src/video/nds/SDL_ndsvideo.c
Expand Up @@ -150,6 +150,8 @@ NDS_SetDisplayMode(_THIS, SDL_DisplayMode * mode)
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
Expand Down
2 changes: 1 addition & 1 deletion test/nds-test-progs/sprite2/Makefile
Expand Up @@ -119,7 +119,7 @@ $(OUTPUT).arm9 : $(OUTPUT).elf
$(OUTPUT).elf : $(OFILES)

#---------------------------------------------------------------------------------
%.pcx.o : %.pcx
%.bin.o : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
Expand Down
23 changes: 22 additions & 1 deletion test/nds-test-progs/sprite2/source/testsprite2.c
Expand Up @@ -7,6 +7,8 @@
#include <fat.h>
#include "common.h"

#include "icon_bmp_bin.h"

#define NUM_SPRITES 10
#define MAX_SPEED 1

Expand Down Expand Up @@ -79,6 +81,24 @@ LoadSprite(char *file)
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)
{
Expand Down Expand Up @@ -236,7 +256,8 @@ main(int argc, char *argv[])
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);
}

Expand Down

0 comments on commit 9f8db75

Please sign in to comment.