You are not logged in.

#1 2008-07-22 03:08:08

bmic
Member

Various functions

Below some functions implemented in GRRLIB:

-- GRRLIB_DrawImg_FadeInOut (width, height, image, scale X, Y scale, speed);

Performs a fade in and out of selected images.


-- GRRLIB_DrawImg_FadeIn (width, height, image, scale X, Y scale, speed);

Performs fade in the image selected.


-- GRRLIB_DrawImg_FadeOut (width, height, image, scale X, Y scale, speed);

Performs fade out in selected image.


-- GRRLIB_FadeScr ();

It should be used to capture the screen of the current system and make a fade out.




The next functions were withdrawn from this forum

-- GRRLIB_ScrShot (const char *);

Save a PNG file of the screen capture.


-- GRRLIB_ScreenCap ();

Used to capture a screen and send a variable u32 to be used later, its use is given as follows: u32 tex_scrTmp = GRRLIB_ScreenCap ();


Code:

inline void GRRLIB_DrawImg_FadeInOut(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    GRRLIB_DrawImg_FadeIn(width, height, data, scaleX, scaleY, speed);
    GRRLIB_DrawImg_FadeOut(width, height, data, scaleX, scaleY, speed);
}

inline void GRRLIB_DrawImg_FadeIn(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    alpha=0;

    for (alpha=0 ; alpha < 255 ; )
    {
        GRRLIB_DrawImg((640-width)/2, (480-height)/2, width, height, data, 0, scaleX, scaleY, alpha );
        GRRLIB_Render();
        alpha = alpha + speed;
    }

}

inline void GRRLIB_DrawImg_FadeOut(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    alpha=0;

    for (alpha=255 ; alpha > 0 ; --alpha)
    {
        GRRLIB_DrawImg((640-width)/2, (480-height)/2, width, height, data, 0, scaleX, scaleY, alpha );
        GRRLIB_Render();
        alpha = alpha - speed;
    }

}

u8 * GRRLIB_LoadTextureFromFile(const char *filename) {

   PNGUPROP imgProp;
   IMGCTX ctx;
   void *my_texture;

       ctx = PNGU_SelectImageFromDevice(filename);
        PNGU_GetImageProperties (ctx, &imgProp);
        my_texture = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 4);
        PNGU_DecodeTo4x4RGBA8 (ctx, imgProp.imgWidth, imgProp.imgHeight, my_texture, 255);
        PNGU_ReleaseImageContext (ctx);
        DCFlushRange (my_texture, imgProp.imgWidth * imgProp.imgHeight * 4);
    return my_texture;
}



bool GRRLIB_ScrShot(const char* File)
{
    IMGCTX pngContext;
    int ErrorCode = -1;

    if(fatInitDefault() && (pngContext = PNGU_SelectImageFromDevice(File)))
    {
        ErrorCode = PNGU_EncodeFromYCbYCr(pngContext, 640, 480, xfb[fb], 0);
        PNGU_ReleaseImageContext(pngContext);

        if(!fatUnmount(PI_INTERNAL_SD))
        {    // I can only hope it's better than nothing
            fatUnsafeUnmount(PI_INTERNAL_SD);
        }
    }
    return !ErrorCode;
}


void GRRLIB_FadeScr()
{
    tex_scrTmp = GRRLIB_ScreenCap();
    GRRLIB_DrawImg_FadeOut(640, 480, tex_scrTmp, 1, 1, 5);
}

u8 *GRRLIB_ScreenCap() {
    void *my_texture;
    int i;

    for (i=0;i<2;++i)
    {
        GX_SetTexCopySrc(0, 0, 640, 480);
        GX_SetTexCopyDst(640, 480, GX_TF_RGBA8, GX_FALSE);
        my_texture = memalign(32, 640 * 480 * 4); // GX_GetTexBufferSize(640, 480, GX_TF_RGBA8, GX_FALSE, 1)
        GX_CopyTex(my_texture, GX_FALSE);
    }
    return my_texture;
}

bmic

Offline

 

#2 2008-07-22 06:36:48

Crayon
Bad Mother Fucker

Re: Various functions

Hi, I just want to give you my modify Fade In/Out function. xpos and ypos are calculate once instead of a maximum of 256. So there is 511 less division and 511 less subtraction. I added some information directly in the for loop and some Doxygen comments. I hope you like it!

Code:

/**
 * Fade in, than fade out
 * @param width     Texture width.
 * @param height Texture height.
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeInOut(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    GRRLIB_DrawImg_FadeIn(width, height, data, scaleX, scaleY, speed);
    GRRLIB_DrawImg_FadeOut(width, height, data, scaleX, scaleY, speed);
}

/**
 * Fade in
 * @param width     Texture width.
 * @param height Texture height.
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeIn(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    int alpha;
    f32 xpos = (640 - width) / 2;
    f32 ypos = (480 - height) / 2;

    for(alpha = 0; alpha < 255; alpha += speed)
    {
        GRRLIB_DrawImg(xpos, ypos, width, height, data, 0, scaleX, scaleY, alpha);
        GRRLIB_Render();
    }
}

/**
 * Fade out
 * @param width     Texture width.
 * @param height Texture height.
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeOut(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    int alpha;
    f32 xpos = (640 - width) / 2;
    f32 ypos = (480 - height) / 2;

    for(alpha = 255; alpha > 0; alpha -= speed)
    {
        GRRLIB_DrawImg(xpos, ypos, width, height, data, 0, scaleX, scaleY, alpha);
        GRRLIB_Render();
    }
}

PS: Could you explain me why you did a loop in the GRRLIB_ScreenCap function?

Offline

 

#3 2008-07-22 07:03:36

RedShade
Member

Re: Various functions

So... you are locking everything else from drawing while you fade, even the CPU?

If you are going to just be doing that texture, you could do something about just locking it to TEX0, and not resetting it to a new TexObj pointer every time with TexInit (though I don't think that is really hurting, since the memory location of the texture data is the same each time. I think that is what the texture cache uses to check if it should send a new texture to the chip)

*Note about the screen capture: That's a 1.2 mb leak :-(

Last edited by RedShade (2008-07-22 07:09:19)

Offline

 

#4 2008-07-22 13:10:06

bmic
Member

Re: Various functions

Crayon wrote:

Hi, I just want to give you my modify Fade In/Out function. xpos and ypos are calculate once instead of a maximum of 256. So there is 511 less division and 511 less subtraction. I added some information directly in the for loop and some Doxygen comments. I hope you like it!

Code:

/**
 * Fade in, than fade out
 * @param width     Texture width.
 * @param height Texture height.
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeInOut(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    GRRLIB_DrawImg_FadeIn(width, height, data, scaleX, scaleY, speed);
    GRRLIB_DrawImg_FadeOut(width, height, data, scaleX, scaleY, speed);
}

/**
 * Fade in
 * @param width     Texture width.
 * @param height Texture height.
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeIn(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    int alpha;
    f32 xpos = (640 - width) / 2;
    f32 ypos = (480 - height) / 2;

    for(alpha = 0; alpha < 255; alpha += speed)
    {
        GRRLIB_DrawImg(xpos, ypos, width, height, data, 0, scaleX, scaleY, alpha);
        GRRLIB_Render();
    }
}

/**
 * Fade out
 * @param width     Texture width.
 * @param height Texture height.
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeOut(u16 width, u16 height, u8 data[], float scaleX, f32 scaleY, u16 speed)
{
    int alpha;
    f32 xpos = (640 - width) / 2;
    f32 ypos = (480 - height) / 2;

    for(alpha = 255; alpha > 0; alpha -= speed)
    {
        GRRLIB_DrawImg(xpos, ypos, width, height, data, 0, scaleX, scaleY, alpha);
        GRRLIB_Render();
    }
}

PS: Could you explain me why you did a loop in the GRRLIB_ScreenCap function?

Becouse without the loop, the tex come out with a little garbage in the top, that was the only way i could make it work fine.

Any improvement is welcome wink

bmic

Offline

 

#5 2008-07-22 13:19:31

bmic
Member

Re: Various functions

RedShade wrote:

So... you are locking everything else from drawing while you fade, even the CPU?

If you are going to just be doing that texture, you could do something about just locking it to TEX0, and not resetting it to a new TexObj pointer every time with TexInit (though I don't think that is really hurting, since the memory location of the texture data is the same each time. I think that is what the texture cache uses to check if it should send a new texture to the chip)

*Note about the screen capture: That's a 1.2 mb leak :-(

I was realy thinking that the memory location of the texture data is the same, but maybe improve quality and stability, after my vacations i will see that.

bmic

Offline

 

#6 2008-07-22 19:44:03

RedShade
Member

Re: Various functions

Code:

my_texture = memalign(32, 640 * 480 * 4);

the memory is pulled from memalign, which is called twice, thus two different memory locations are created, and the first loop through location is left without destroying the memory allocation, so it's leaked.

Code:

        GX_SetTexCopySrc(0, 0, 640, 480);
        GX_SetTexCopyDst(640, 480, GX_TF_RGBA8, GX_FALSE);

<-- unles you are planning on copying from another size, or to another size, or using a different format than RGBA8, you never need to call these more than once in a program. If you do change, you only need to call it then, and nowhere else.

I've tried to mention a function like this, but kept getting the name wrong :-/... the syncronization thing on the fifo that guarentee's full copy:

Code:

void     GX_PixModeSync ()
     Inserts a synchronization command into the graphics FIFO. When the GPU sees this command
 it will allow the rest of the pipe to flush before continuing. This command is useful in certain situation
 such as after a GX_CopyTex() command and before a primitive that uses the copied texture.

Last edited by RedShade (2008-07-22 19:45:43)

Offline

 

#7 2008-07-22 20:11:04

Crayon
Bad Mother Fucker

Re: Various functions

RedShade wrote:

Code:

        GX_SetTexCopySrc(0, 0, 640, 480);
        GX_SetTexCopyDst(640, 480, GX_TF_RGBA8, GX_FALSE);

<-- unles you are planning on copying from another size, or to another size, or using a different format than RGBA8, you never need to call these more than once in a program. If you do change, you only need to call it then, and nowhere else.

So normally those two lines should be in GRRLIB_InitVideo if no other size are needed:

Code:

void GRRLIB_InitVideo () {

...

        GX_SetTexCopySrc(0, 0, 640, 480);
        GX_SetTexCopyDst(640, 480, GX_TF_RGBA8, GX_FALSE);
}

whit that in mind, the function should be like this:

Code:

/**
 * Make a snapshot of the sreen in a texture.
 * @return A texture representing the screen.
 */
u8 *GRRLIB_ScreenCap() {
    void *my_texture;

    my_texture = memalign(32, 640 * 480 * 4);
    GX_CopyTex(my_texture, GX_FALSE);
    GX_PixModeSync();
    return my_texture;
}

Thanks again RedShade wink

Offline

 

#8 2008-10-04 23:49:50

Crayon
Bad Mother Fucker

Re: Various functions

Crayon wrote:

So normally those two lines should be in GRRLIB_InitVideo if no other size are needed:

Code:

void GRRLIB_InitVideo () {

...

        GX_SetTexCopySrc(0, 0, 640, 480);
        GX_SetTexCopyDst(640, 480, GX_TF_RGBA8, GX_FALSE);
}

whit that in mind, the function should be like this:

Code:

/**
 * Make a snapshot of the sreen in a texture.
 * @return A texture representing the screen.
 */
u8 *GRRLIB_ScreenCap() {
    void *my_texture;

    my_texture = memalign(32, 640 * 480 * 4);
    GX_CopyTex(my_texture, GX_FALSE);
    GX_PixModeSync();
    return my_texture;
}

I tried to move those two lines from the GRRLIB_ScreenCap and it seems like it's not working. I needed to reset my Wii smile

Offline

 

#9 2008-11-25 22:44:59

Crayon
Bad Mother Fucker

Re: Various functions

I think I found a way to correct the garbage with the GRRLIB_ScreenCap() function, check this message:
http://grrlib.santo.fr/forum/viewtopic.php?pid=376#p376

Offline

 

#10 2009-02-08 18:24:30

Crayon
Bad Mother Fucker

Re: Various functions

Just in case there are other people out there using GRRLIB 3.0.5 alpha. This is the modified Fading functions.

Code:

/**
 * Fade in, than fade out
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeInOut(GRRLIB_texImg tex, float scaleX, f32 scaleY, u16 speed)
{
    GRRLIB_DrawImg_FadeIn(tex, scaleX, scaleY, speed);
    GRRLIB_DrawImg_FadeOut(tex, scaleX, scaleY, speed);
}

/**
 * Fade in
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeIn(GRRLIB_texImg tex, float scaleX, f32 scaleY, u16 speed)
{
    int alpha;
    f32 xpos = (640 - tex.w) / 2;
    f32 ypos = (480 - tex.h) / 2;

    for(alpha = 0; alpha < 255; alpha += speed)
    {
        if(alpha > 255) alpha = 255;
        GRRLIB_DrawImg(xpos, ypos, tex, 0, scaleX, scaleY, 0xFFFFFF00 | alpha);
        GRRLIB_Render();
    }
}

/**
 * Fade out
 * @param data   Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeOut(GRRLIB_texImg tex, float scaleX, f32 scaleY, u16 speed)
{
    int alpha;
    f32 xpos = (640 - tex.w) / 2;
    f32 ypos = (480 - tex.h) / 2;

    for(alpha = 255; alpha > 0; alpha -= speed)
    {
        if(alpha < 0) alpha = 0;
        GRRLIB_DrawImg(xpos, ypos, tex, 0, scaleX, scaleY, 0xFFFFFF00 | alpha);
        GRRLIB_Render();
    }
}

Last edited by Crayon (2009-02-10 05:26:38)

Offline

 

#11 2009-02-08 21:20:25

Cid2mizard
Member

Re: Various functions

Code:

f32 xpos = (640 - tex.w) / 2;
f32 ypos = (480 - tex.h) / 2;

humm and if my Img isn't on center of screen hmm ????


www.nintendomax.com - 100% Hack  0% Warez

Offline

 

#12 2009-02-09 06:04:00

Crayon
Bad Mother Fucker

Re: Various functions

Cid2mizard wrote:

humm and if my Img isn't on center of screen hmm ????

I know it's not flexible, but you just have to change the code for your needs smile
I only adapted the function given by bmic. And that's what I needed for my game.

Offline

 

#13 2010-08-14 07:02:33

Crayon
Bad Mother Fucker

Re: Various functions

This is my last version of GRRLIB_DrawImg_FadeInOut, GRRLIB_DrawImg_FadeIn and GRRLIB_DrawImg_FadeOut. The only difference is that I'm using rmode->fbWidth and rmode->efbHeight instead of hard-coded value 640x480. For alpha I'm using s16 instead of int wink

Code:

/**
 * Fade in, than fade out.
 * @param tex    Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeInOut(struct GRRLIB_texImg *tex, f32 scaleX, f32 scaleY, u16 speed)
{
    GRRLIB_DrawImg_FadeIn(tex, scaleX, scaleY, speed);
    GRRLIB_DrawImg_FadeOut(tex, scaleX, scaleY, speed);
}

/**
 * Fade in.
 * @param tex    Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeIn(struct GRRLIB_texImg *tex, f32 scaleX, f32 scaleY, u16 speed)
{
    s16 alpha;
    f32 xpos = (rmode->fbWidth - tex->w) / 2;
    f32 ypos = (rmode->efbHeight - tex->h) / 2;

    for(alpha = 0; alpha < 255; alpha += speed) {
        if(alpha > 255) alpha = 255;
        GRRLIB_DrawImg(xpos, ypos, tex, 0, scaleX, scaleY, 0xFFFFFF00 | alpha);
        GRRLIB_Render();
    }
}

/**
 * Fade out.
 * @param tex    Texture.
 * @param scaleX Texture X scale.
 * @param scaleY Texture Y scale.
 * @param speed  Fade speed (1 is the normal speed, 2 is two time the normal speed, etc).
 */
void GRRLIB_DrawImg_FadeOut(struct GRRLIB_texImg *tex, f32 scaleX, f32 scaleY, u16 speed)
{
    s16 alpha;
    f32 xpos = (rmode->fbWidth - tex->w) / 2;
    f32 ypos = (rmode->efbHeight - tex->h) / 2;

    for(alpha = 255; alpha > 0; alpha -= speed) {
        if(alpha < 0) alpha = 0;
        GRRLIB_DrawImg(xpos, ypos, tex, 0, scaleX, scaleY, 0xFFFFFF00 | alpha);
        GRRLIB_Render();
    }
}

Offline

 

Board footer

Powered by FluxBB