delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/03/20:01:00

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: fix and fixed in Allegro
Date: Tue, 3 Mar 1998 23:59:05 +0000
Organization: None
Message-ID: <2MwOnPAJlJ$0Ewn5@talula.demon.co.uk>
References: <34F8708B DOT 2BEA AT home DOT com>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 60
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dan writes:
>I was wondering if these two types are different at all in allegro.

Yes, they are very different. The fixed type is a standard C typedef and
behaves in exactly the same way as a normal 32 bit integer, while the
fix object is a C++ class.

In C, you should use the fixed type with the manual manipulation
functions like itofix(), fixtoi(), fmul(), fdiv(), etc. I also recommend
that you start out with this approach even if you are working in C++, at
least until you get the hang of how fixed point math works and what kind
of things you can do with it. The fix class can save a bit of hassle by
letting you use the normal arithmetic operators and casts (they are
overloaded so you don't need to call the manipulation routines
yourself), but this overloading has the potential to break down in some
situations and is likely to be very confusing if you don't already
understand exactly what you are trying to achieve...

>Also, I have a question about the rotate_sprite routine in allegro.
>When I first tried to use it, I tried to put in a constant value for
>the angle, (I used 30), but it didn't change.

That is because you are passing an integer to a function which is
expecting a fixed point parameter. When interpreted as a 16.16 fixed
point value, 30 equates to 30/65536 = 0.000458: far too small for you to
notice the rotation!

Pass itofix(30) instead, and all will be well.

>And is there a different in the following code.
>
>//code 1
>
>int angle;
>rotate_sprite( buffer, mysprite, x, y, itofix(angle) );
>
>//code 2
> 
>fix angle;
>rotate_sprite( buffer, mysprite, x, y, itofix(angle) );

That depends on how you initialise these parameters. In most cases they
will probably do the same thing, but the second version isn't a very
sensible bit of code. When you pass the fix object to the itofix()
function (which is expecting an integer parameter), the compiler will
automatically reduce the fixed value to an integer, after which the
itofix() routine will scale it back up to fixed point: rather a waste of
effort! Logically you should just be able to pass the fix angle directly
to rotate_sprite(), but unfortunately that won't work correctly because
the rotate routine is expecting a fixed variable, not a fix class. The
fixed typedef is exactly the same thing as a long, so the compiler will
reduce the fix class to this normal integer format, which is totally the
wrong thing! You need to be very careful when mixing the fix class and
fixed typedef in this way, and prevent any such unwanted conversions by
explicitly passing angle.v to the rotate_sprite() routine.


--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
"Pigs use it for a tambourine" - Frank Zappa

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019