December 03, 2020, 01:34:36 pm

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Problem with Shadow Render

Started by pistol350, October 23, 2007, 04:29:54 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pistol350

Hi all!
I've been playing with Aurora 2D functions for a few days now.
And i stumbled on this problem that i can't solve.
I got my sprite that i make flipping Horizontaly depending on its LEFT or RIGHTdirection and it works great.
However, the shadow of the sprite doesn't update with the flipping,
i mean instead of going the same direction as the sprite, the shadow only keeps the first direction the sprites had when it was first rendered.
I tried to tweak my code in many different ways.
But i still don't get any result.
help please  :)

Code Select
SUB DrawSprites(C2DScreen s,C2DSprite sprite1,C2DSurface *backbuffer)
{
sprite1.SetFrame(sprite1framenumber);
sprite1.SetAlpha(180);
sprite1.SetShadowOffset(-20,-20);

sprite1.SetRenderMode(RENDER_TRANSSHADOW);
       sprite1.RenderXY(backbuffer,sprite1x,sprite1y);

IF(sprite1direction = left)
{
        //Reverse sprite1 so facing left
       sprite1.SetRenderMode(RENDER_TRANSHFLIP);
}
      ELSE
{
       sprite1.SetRenderMode(RENDER_TRANS);
}

sprite1.RenderXY(backbuffer,sprite1x,sprite1y);
}




Regards,

Peter B.

J B Wood (Zumwalt)

That is because you are using the single sprite object and just inverting it, create 2 sprites, one for each facing of the object.
When you go left, only render sprite 2, when you go right render sprite 1.

Try this (note I have not tried it but its logical)

Code Select


SUB DrawSprites(C2DScreen s,C2DSprite sprite1,C2DSprite sprite2,C2DSurface *backbuffer)
{
// for rendering sprite 1
sprite1.SetFrame(sprite1framenumber);
sprite1.SetAlpha(180);
sprite1.SetShadowOffset(-20,-20);

// for rendering sprite 2
sprite2.SetFrame(sprite1framenumber);
sprite2.SetAlpha(180);
sprite2.SetShadowOffset(-20,-20);

IF(spritedirection = left)
{
                     sprite2.SetRenderMode(RENDER_TRANSSHADOW);
                     sprite2.RenderXY(backbuffer,sprite1x,sprite1y);
}
                ELSE
{
                      sprite1.SetRenderMode(RENDER_TRANSSHADOW);
                     sprite1.RenderXY(backbuffer,sprite1x,sprite1y);
}
}


NOTE: you need to invert your sprite graphic strip.
There is probably a better way to do this.

pistol350

Thank you Jonathan.
But unfortunately,what seems "logic" to you is not to the compiler  ;)

I tried something similar before and had the same result (not what i expected ).
When i compile your code only the shadow appears but it still doesn't flip when i press left or right direction.

The concept is valuable and as i said i tried something like that before.I guess i did something wrong, or maybe your current code needs to be modified as you did not try it.
By the way, I will keep on working from what you gave me and try to see where it leads me.

Regards,
Peter.

Regards,

Peter B.

J B Wood (Zumwalt)

I'll make some time this weekend to play with it some, I can export out a graphics animation strip from Hexagon, so I will see what I can come up with and test it with the code.

pistol350

Thank you very much Jonathan.
BTW, you can find one of the sprites that i use plus a previous version of my code here : http://www.ionicwind.com/forums/index.php/topic,1746.0.html

By my side, i will try to clean up my current code so that people can see it without me being ashamed  ::)
Regards,

Peter B.

J B Wood (Zumwalt)

October 25, 2007, 09:22:35 pm #5 Last Edit: October 25, 2007, 09:49:36 pm by Jonathan (zumwalt) Wood
Working on the linked code now, until I get to tired :)

See next post, as I stated, flip the animated strip.
You will have to render your animation in both directions.
Flip will flip the sprite, BUT you can't combine a flip and a shadow to the same sprite and render it.
A shadow is a cookied cut out version of the sprite, so you would need to combine the render mode of both flip and shadow into the same method, which it doesn't take.

Let me know if my code is confusing.
Tired, late, and braindead night for me.

J B Wood (Zumwalt)

Code Select

/* 06/24/2007 */
/*
robot
*/
int fps;
int frame = 0;
int x2 = 100;
int x = 0;
int y = 400;
int xdir = 4;
int run = 1;
int showSprite=1;
/* SART*/

global sub main()
{
C2DScreen s;
s.CreateWindowed(0,0,800,600,AWS_CAPTION|AWS_VISIBLE|AWS_SIZE,0,"Robot 2D Test",NULL);
s.SetStretchedFlip(false);
C2DSurface *back = s.GetBack();
C2DSprite sprite1,sprite2;

if(!sprite1.Load(s,getstartpath() + "robot16.bmp",0,0,16,TRUE))
{
MessageBox(s,"unable to load sprite",getstartpath() + "robot16.bmp");
//run=0;
}

if(!sprite2.Load(s,getstartpath() + "fliprobot16.bmp",0,0,16,TRUE))
{
MessageBox(s,"unable to load sprite",getstartpath() + "robot16.bmp");
//run=0;
}
sprite1.SetShadowOffset(-20,-20);
sprite1.SetMaskColor(RGB(255,255,255));
sprite1.SetAlpha(180);
sprite1.SetRenderMode(RENDER_TRANSSHADOW);

sprite2.SetShadowOffset(-20,-20);
sprite2.SetMaskColor(RGB(255,255,255));
sprite2.SetAlpha(180);
sprite2.SetRenderMode(RENDER_TRANSSHADOW);

do
{
Back->Fill(RGB(25,10,255));
s.WriteText( 180,0,"PRESS LEFTMOUSE TO CLOSE, FPS: " + NumToStr(fps));
//play with the sprites
//sprite1.RenderXY(back,s.Mousex()-10,s.Mousey());
sprite1.SetFrame(frame);
sprite2.SetFrame(frame);
// flip the sprite

//draw a sprite and an alpha shadow
if(showSprite=1)
{
sprite1.SetRenderMode(RENDER_TRANSSHADOW);
sprite1.RenderXY(back,x,y);
sprite1.SetRenderMode(RENDER_TRANS);
sprite1.RenderXY(back,x,y);
sprite1.SetRenderMode(RENDER_TRANSSCALED);
}
else
{
sprite2.SetRenderMode(RENDER_TRANSSHADOW);
sprite2.RenderXY(back,x,y);
sprite2.SetRenderMode(RENDER_TRANS);
sprite2.RenderXY(back,x,y);
sprite2.SetRenderMode(RENDER_TRANSSCALED);
}
s.WriteText( x,y,""+NumToStr(frame));
fps = s.Flip(0);
//adjust sprite direction
x += xdir;

if((x + sprite1.GetWidth()) > 800 | x < 0)
{
if((x+sprite1.GetWidth()) > 800)
{
showSprite=2;
}
else
{
if(x < 0)
{
showSprite=1;
}
}
xdir = -xdir;
}

frame += ((x2%10) = 0);
IF frame > 16
frame = 0;

} until GetKeyState(0x01) or run=0;
s.CloseScreen();
return 0;
}



Only thing I can't do is re-render the walking of the robot so its not doing the moonwalk forward.. lol..

pistol350

October 26, 2007, 03:41:14 am #7 Last Edit: October 26, 2007, 03:59:29 am by pistol350
QuoteOnly thing I can't do is re-render the walking of the robot so its not doing the moonwalk forward.. lol..

LOL!!! ;D
Don't worry about that, i've already fixed that problem.Dealing with that point, my current big problem now is to synchronize the robot steps with a "Robot-like ::)" sound effect.it's not that difficult,and i'm on the right way to get it work.But as i said before, i'm cleaning my code before posting it.it's not descent enough yet :D .

getting back to your code.I'm happy to see that the robot's shadow finally flips well.but i don't like the idea of using 2 different sprites.
I will use it like that for the moment, but i will keep on searching for a better way.

Thanks again. :)

EDIT :
Here is the Subroutine that i use to perform the sprite animation.
I will have to tweak it further to have it animate more than one sprite.but i'm not to that point yet  :P

Quote
SUB animateSprite(C2DScreen s,C2DSprite sprite1,C2DSurface *backbuffer)
{
      //Advance sprite frame number by an amount proportional to the time since the
      //screen was last updated
      sprite1framenumber = sprite1framenumber+((sprite1speed*timesincelastscreenupdate)/90);
      //When reach end of animation restart it
      IF(sprite1framenumber >= NumberofFrames)
     {
        sprite1framenumber = 0;
        //s.WriteText( 300,400,"sprite being animated :"+NumToStr(sprite1framenumber));
     }
          //Set correct frame to show
     sprite1.SetFrame(floor(sprite1framenumber));
}
Regards,

Peter B.

J B Wood (Zumwalt)

You might want to put in a request to Paul to see about adding a new rendermode as

SetRenderMode(RENDER_TRANSSHADOWFLIP);

Probably wouldn't be a priority though since I am not sure how RENDER_TRANSSHADOW works.
The idea is that you can both FLIP the sprite and FLIP the shadow to sync with the sprite.
It doesn't exist at the moment.

Remember, RENDER_TRANSSHADOW is just another copy of your sprite anyway, just offset and shadow colored.
2 seperate sprites won't kill the application and its not much difference in memory use. You only render the one you need when you need it.

I've purchased tons of sound banks, none of which have everything I need unfortunately.

There are many levels to creating games, even 2d ones. You need someone who knows how to make music if you need music, someone to do sound effects, someone to do animation strips, a graphics artist, a storyboard person, and last but most certainly not least, a coder.

Its hard has hell being all of those in one.

pistol350

You're right about the SetRenderMode(RENDER_TRANSSHADOWFLIP);
That seems to be exactly what i need.
However, due to the level of my current project , i don't think it deserves Paul to spend more time on adding this option to the 2D command set.
Well, i'm just toying with the 2D features of Aurora.
I don't really plan on creating a "Game", i'm just messing around to see what i can come up with.

QuoteThere are many levels to creating games, even 2d ones. You need someone who knows how to make music if you need music, someone to do sound effects, someone to do animation strips, a graphics artist, a storyboard person, and last but most certainly not least, a coder.


BTW, I think i have everything if not almost everything that you mentionned here to create a real project.
But for the moment it's not part of my priority ,so i'll keep on playing with codes just to have fun. ;)
Thanks for the info.

Regards,

Peter B.