March 28, 2024, 06:36:31 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Help in making sprites flip horizontaly

Started by pistol350, July 02, 2007, 02:20:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pistol350

Hi guys!
Could anyone of you help me get this sprite flipped horizontaly please.
I attached my file here.
cheers!
Peter
Regards,

Peter B.

Brice Manuel

July 02, 2007, 02:40:19 PM #1 Last Edit: July 02, 2007, 03:09:51 PM by Brice Manuel
Fix coming, just a sec...

The problem is simple and is in your main loop:

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);
//draw a sprite and an alpha shadow
sprite1.SetRenderMode(RENDER_TRANSSHADOW);
sprite1.RenderXY(back,x,y);
sprite1.SetRenderMode(RENDER_TRANS);
sprite1.RenderXY(back,x,y);
s.WriteText( x,y,""+NumToStr(frame));
fps = s.Flip(0);

//adjust sprite1 direction
x += xdir;
sprite1.SetRenderMode(RENDER_TRANSSCALED);
if((x + sprite1.GetWidth()) > 800 | x < 0 )
xdir = -xdir;
sprite1.RenderXY(back,x,y);
sprite1.SetRenderMode(RENDER_TRANSHFLIP);

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


} until GetKeyState(0x01) or run=0;



You are on the right track  ;D  However, the issue is the way you are constructing your loop.  You are calling flip in the middle of the loop.  Then after flip you are handling the check for direction and setting the sprite to be hflipped.  (You are also rendering the sprite before you hflip it.) 

The problem is, right after that when the loop starts again, you are erasing the back buffer and the sprite you have just drawn.

What I like to do, is keep my main loop small and break everything down into functions or subroutines.  An example in psuedo code for my wip in game that is using a custom 2D GUI:


do
Handle_GUI()         // Handles the GUI System
Update_Game()     // Updates all elements of the game, checks collision, points, everything (each of these are in their own sub)
Render_Game()     // Renders all elements of the game (again each element (like GUI, HUD, Players) are in their own sub)
Flip Buffers
until inkey = <esc>


This helps to keep problems like yours from happening as things are more organized and it is easier to keep track of things.  Think of your main loop like the way a "how to" book is broken down into indivudual chapters that deals with each step of the project, in this case the "project" is your main loop.

Some tips for your example...

1.  You should use a flag for determine whether your sprite is hflipped or not.
2.  You should check the collision with the edge of the screen once per loop.
3.  If the sprite is detected at being at the edge of the screen, then you should set your flag so that the sprite is hflipped.
4.  The last thing you should do in your loop is flip the buffers.


FWIW, I love Aurora and find it much easier to use than EBASIC.  I literally have not bathed or slept since I bought Aurora Sunday morning.  I am VERY stinky and tired  ;D

LarryMc

Thanks for putting that image in my head! ;)
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Brice Manuel

July 02, 2007, 05:05:32 PM #3 Last Edit: July 02, 2007, 05:09:41 PM by Brice Manuel
Hehe, well if it helps, I took a bath and 2-Litre bottles of RC are keeping the sleeplessness in check  ;D

pistol350

Thank you very much Brice!
All you said only have rich instructions  :o !
About the structure of the code,well (i won't say it aloud but Paul is to blame since it is more or less the replication of the "dxtest.src" code   ;D)
In fact i really like the way of organising the code by breaking everything down and that's what i tried to do.
But it seems that i am still far from a descent result.
now that i reorganised the code i found a bunch of errors that i can't understand.
for instance, my sprite was declared but the compiler says NO  ???
See by yourself :

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

/* SART*/

global sub main()
{
   C2DScreen s;
   //s.CreateFullScreen(800,600,32);
   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;
   if(!sprite1.Load(s,getstartpath() + "robot16.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);
   do
   {
      updateGame();
      renderGame();   
      fps = s.Flip(0);
   
   } until GetKeyState(0x01) or run=0;
   s.CloseScreen();
   return 0;
}


sub updateGame
{
   //adjust sprite1 direction
   x += xdir;
   sprite1.SetRenderMode(RENDER_TRANSSCALED);
   if((x + sprite1.GetWidth()) > 800 | x < 0 )
      xdir = -xdir;
      sprite1.RenderXY(back,x,y);
      sprite1.SetRenderMode(RENDER_TRANSHFLIP);
      
   frame += ((x2%10) = 0);
   IF frame > 16
         frame = 0;
return 0;
}


sub renderGame
{
   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);
   //draw a sprite and an alpha shadow
   sprite1.SetRenderMode(RENDER_TRANSSHADOW);
   sprite1.RenderXY(back,x,y);
   sprite1.SetRenderMode(RENDER_TRANS);
   sprite1.RenderXY(back,x,y);
   s.WriteText( x,y,""+NumToStr(frame));   
return 0;
}
Regards,

Peter B.

pistol350

I still don't get how to structure my code to have it work properly.
Any suggestions ?

Regards!
Peter
Regards,

Peter B.

Ionic Wind Support Team

There is a difference between global and local variables.   'back' for instance is local to the main subroutine, and cannot be seen by the RenderGame subroutine.  Pass them as parameters, or create them dynamically and use global pointers.
Ionic Wind Support Team

pistol350

QuotePass them as parameters, or create them dynamically and use global pointers.


It took me ages to finally understand what you meant and then to know what to do,  :D
but after a long meditation period, i managed to have the code compiled although i am not convinced that i did it the right way.
Anyway,I chose to pass them as parameters since the 2 other ways are still far from my understanding  :D ;D ;D


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

/* SART*/

global sub main()
{
C2DScreen s;
//s.CreateFullScreen(800,600,32);
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;
if(!sprite1.Load(s,getstartpath() + "robot16.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);
do
{
updateGame(sprite1,back);
renderGame(s,sprite1,back);
fps = s.Flip(0);

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


sub updateGame(C2DSprite sprite1,C2DSurface *back)
{
//adjust sprite1 direction
x += xdir;
sprite1.SetRenderMode(RENDER_TRANSSCALED);
if((x + sprite1.GetWidth()) > 800 | x < 0 )
xdir = -xdir;
sprite1.RenderXY(back,x,y);
sprite1.SetRenderMode(RENDER_TRANSHFLIP);

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


sub renderGame(C2DScreen s,C2DSprite sprite1,C2DSurface *back)
{
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);
//draw a sprite and an alpha shadow
sprite1.SetRenderMode(RENDER_TRANSSHADOW);
sprite1.RenderXY(back,x,y);
sprite1.SetRenderMode(RENDER_TRANS);
sprite1.RenderXY(back,x,y);
s.WriteText( x,y,""+NumToStr(frame));
return 0;
}

Regards,

Peter B.