gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > more alpha blending fun!

#18516 - TheMikaus - Sun Mar 28, 2004 7:44 am

This is my code (sorry about the huge block)

Code:

//include a graphic
#include "circle.h"

#define SetMode(mode) Register_DisplayController = (mode)

#define Register_DisplayController  *(volatile unsigned short *)0x4000000
#define Register_ScanlineCounter *(volatile unsigned short *)0x4000006
#define Register_BlendModifier *(unsigned short *) 0x4000050
#define Register_ColorEvaluation *(unsigned short *) 0x4000052
#define Register_Controller (volatile unsigned int *) 0x04000130

#define SpriteState_Memory ((unsigned short *) 0x7000000)
#define SpriteImage_Memory ((unsigned short *) 0x6010000)
#define SpritePal_Memory ((unsigned short *) 0x5000200)

#define setTransparency(a,b,c) { Register_BlendModifier = ((1 << (a)) | (1 << (b))); Register_ColorEvaluation = (c) + (c << 8);}

unsigned short * spriteData3 = SpriteState_Memory + 8192;

typedef struct tagBlender
{
    unsigned source_bg0 : 1;
    unsigned source_bg1 : 1;
    unsigned source_bg2 : 1;
    unsigned source_bg3 : 1;
    unsigned source_sprites : 1;
    unsigned source_backdrop : 1;
   
    unsigned blend_mode : 2;
   
    unsigned dest_bg0 : 1;
    unsigned dest_bg1 : 1;
    unsigned dest_bg2 : 1;
    unsigned dest_bg3 : 1;
    unsigned dest_sprites : 1;
    unsigned dest_backdrop : 1;
} blender;


typedef struct tagButtons
{
    unsigned a : 1;
    unsigned b : 1;
    unsigned select : 1;
    unsigned start : 1;
    unsigned right : 1;
    unsigned left : 1;
    unsigned up : 1;
    unsigned down : 1;
    unsigned r : 1;
    unsigned l : 1;
} Buttons;

typedef struct tagSprite
{
    unsigned short at0;
    unsigned short at1;
    unsigned short at2;
    unsigned short at3;
} Sprite, *pSprite;


typedef struct tagSprited
{
    //at0
    unsigned y : 8;

    unsigned rotation : 1;
    unsigned DoubleSize : 1;
    unsigned translucent : 1;
    unsigned windowed : 1;
    unsigned mosaic : 1;
    unsigned color_256 : 1;
    unsigned tall : 1;
    unsigned wide : 1;

    //at1
    unsigned x : 9;
    unsigned unknowna : 3;
    unsigned hflip : 1;
    unsigned vflip : 1;
    unsigned spriteSize:2;
   
    //at2
    unsigned name : 10;
    unsigned ground : 2;
    unsigned pallette : 4;
   
    unsigned short at3;
   
} sSprite;

Sprite sprites[128];

sSprite  * s2prites = (sSprite *)sprites;

#define vsync() while((Register_ScanlineCounter < 160));

void updateSpriteMemory()
{
    int n;
    unsigned short * temp;
    temp = (unsigned short *)sprites;
   
    for(n = 0; n < 128 * 4; n++)
    {
        SpriteState_Memory[n] = temp[n];
    }
}

int main(void)
{
    unsigned short x;
    unsigned char y;
   
     Buttons * controller = (Buttons *)Register_Controller;
    signed short xdir = 1, ydir = 1;
   
    int char_number = 0;
    int n;
    int xd,yd;
   
    xd=yd=1;
    x= 10;
     y = 10;
//obj en, mode2, 1d ,bg2en
    SetMode( 0x1000 | 2 | 0x40 | 0x400);
   
    for(n = 0; n < 128; n++)
    {
        //set everything to zero
        sprites[n].at0 = 0;
        sprites[n].at1 = 0;
        sprites[n].at2 = 0;
        sprites[n].at3 = 0;
       
        //move everything off screen
        s2prites[n].y = 160;
        s2prites[n].x = 240;
    }
   
    //Set the Palette
   
    for ( n = 0; n < 256; n ++)
    {
        SpritePal_Memory[n] = circlePalette[n];
    }

    for ( n = 0; n < 256 * 8; n ++)
    {
        SpriteImage_Memory[n] = circleData[n];

    }
   
    //set up first sprite

    s2prites[0].name = 0;
    s2prites[0].color_256 = 1;
    s2prites[0].spriteSize = 3;
    s2prites[0].y = 0;
    s2prites[0].x = 150;
    s2prites[0].translucent = 1;
 
    s2prites[0].vflip = 0;
    s2prites[0].hflip = 0;
    s2prites[0].ground = 0;

    blender * bre = (blender *) Register_BlendModifier;
    bre->source_sprites = 1;
    bre->blend_mode = 1;
    bre->dest_bg2 = 1;
    Register_ColorEvaluation = (8) + (8 << 8);
   
    int atime = 0;
    int btime = 0;
    int uptime = 0;
    int downtime = 0;
   
   
    while(1)
    {
//delay so vga doesn't go a-wall
        for(n = 0; n < 16000; n++);
        y += yd;
        x += xd;
        if ( y > 160 - 64) yd = -1;
        else if ( y == 0) yd = 1;
        if ( x > 240 - 64) xd = -1;
        else if (x == 0) xd = 1;
        s2prites[0].x=x;
        s2prites[0].y=y;
        if(!controller->a) atime++;
        else atime =0;
       
        if(!controller->b) btime++;
        else btime =0;
       
        if(!controller->up) uptime++;
        else uptime =0;
       
        if(!controller->down) downtime++;
        else downtime =0;
       
       
       
        if(uptime == 1)
        {
            s2prites[0].translucent = !s2prites[0].translucent;
        }
       
        if(atime == 1)
        {
            s2prites[0].vflip = !s2prites[0].vflip;
        }
        if(btime == 1)
        {
            s2prites[0].hflip = !s2prites[0].hflip;
        }
       
        vsync();
       
        updateSpriteMemory();
    }
}


I'm trying to get alpha blending to work, and I don't really know what I'm doing wrong. It just displays the sprite as normal. a and b change the flip and up changes the translucency bit on the sprite.

But I'm lost, it doesn't make the sprite blend at all! :( help!

#18532 - DekuTree64 - Sun Mar 28, 2004 4:42 pm

Are you setting up the BG anywhere? It looks like you're only doing the sprite, so the BG tiles and screen data should still be all zeroed out, which would be transparent. No blending is performed if it's between a color and a transparent pixel.
Try loading in some tiles and see if it works.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#18539 - tepples - Sun Mar 28, 2004 7:19 pm

DekuTree64 wrote:
the BG tiles and screen data should still be all zeroed out, which would be transparent. No blending is performed if it's between a color and a transparent pixel.

If you tell the GBA to blend between the backdrop (or "sky" as some docs call it) and a layer, wouldn't that work?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#18599 - TheMikaus - Mon Mar 29, 2004 3:28 pm

Setting the background worked.

The use of backdrop does wierd blending. I have this background picture of a red sky and a red sprite bouncing. and on backdrop it only blends the darker colors together. Looks kinda wierd. Anyway. Thanks again!