Animation and Keyboard
Using Explicit Conversions, Again
There is a hidden type conversion in some programs mentioned in this chapter. As usual, we would like to make obvious where this conversion happens and what the consequences are.
The center of the circle to be drawn by the
circlefill
function is given by a pair of coordinates. Although it does make
sense to place a center of the circle even on non-integer coordinates, the
Allegro library lacks the ability to draw such circles. Allegro is only able to
draw a circle that is centered in the middle of a pixel, and therefore the circlefill
function accepts only integer values for coordinates of its center, as well as
for the radius.
The Allegro library was selected as graphics library for programs in this book largely due to Allegro's enormous simplicity. In the future, you might use a different library to draw graphics, or even a completely different language. Most basic principles that you learn throughout this book remain the same regardless of the library or the language you use. We will, however, continue using Allegro because it makes the essentials of graphics programming quite easy.
To get back to the type conversion issue:
in the program above, the value of variable x
, being of type double
,
gets automatically converted to the type int
because the circlefill
function accepts only integers for coordinates. To make this conversion
explicit, we could use the cast operator like this:
circlefill(surface, int(x), 200, 50, makecol(0,0,255));
Also, note that the same applies to
all the drawing functions from the Allegro library, including the functions line
, rect
and rectfill
.
They only take integer values for coordinates. The putpixel
function has to use integer values in this library and in any other, since it
makes no sense otherwise.
In Visual Studio, Allegro will disable the warning for suspicious conversions. To get all the warnings back again, include the Allegro library like this:
#pragma warning(push) #include <allegro.h> #pragma warning(pop)
It is recommended to keep warnings enabled. It is also recommended that programmers should fix all warnings in their programs, so that programs compile without any warnings.