Conversion problem.

Den_d_y tod.naz at ya.ru
Tue Jun 25 12:08:07 UTC 2019


Hello! Here I am again, with my problem ... In my program, I 
cannot manage to convert from "double" to "int". Here is the code:
<< ++
struct Animation - the structure that implements the animation
+ /
struct Animation
{
private List! (Image) img; /// List of pictures in the animation
private double f; /// current picture in animation
private double s; /// speed of animation

/// speed setter (double s)
public void setSpeed ​​(double sp)
{
s = sp;
}

/// Adds a picture to the list
public void addFrame (Image pia)
{
img.add (pia);
}

/// Sets the image to a specific place in the list
public void setFrame (int id, Image pia)
{
img.set (id, pia);
}

/// Returns a picture from the list
public Image getFrame (int id)
{
return img.get (id);
}

/// Returns the list
public List! (Image) getList ()
{
return img;
}

/// The animation event itself. Returns the current animation 
image.
public Image step ()
{
writeln ("Animation :: step start ...");
if (floor (f) == img.length)
{
f = 0.1f;
writeln ("Animation :: f = 0.1f");
} else
{
f + = s;
writeln ("Animation :: f + = s");
}

const int i = roundTo! (int) (f);
writeln ("i set");
writeln ("i:", i);
return img.get (i);
}
} >>
Another code:
<< import std.stdio;

/ ++
The structure that implements the list of objects.
+ /
struct List (TObj)
{
private TObj [] obj;

/// Adds an object to the list
public void add (TObj o)
{
obj ~ = o;
}

/// Returns an object from the list
public TObj get (int id)
{
return obj [id];
}

/// Sets an object in a specific place.
public void set (int id, TObj o)
{
obj [id] = o;
}

/// Returns the length of the list
public int length ()
{
return obj.length;
}

/// Gives the garbage collector a signal to clear the list.
public void free ()
{
obj = null;
}
} >>

"If you don’t understand something, the translator has bricked 
it."

In the moment:
<< public Image step ()
{
writeln ("Animation :: step start ...");
if (floor (f) == img.length)
{
f = 0.1f;
writeln ("Animation :: f = 0.1f");
} else
{
f + = s;
writeln ("Animation :: f + = s");
}

const int i = roundTo! (int) (f); // Error here
writeln ("i set");
writeln ("i:", i);
return img.get (i);
} >> The compiler is silent, but when you start the program, 
reaching this place, it hangs.
  Is there any way to convert a "double" to an "int" without 
errors?



More information about the Digitalmars-d-learn mailing list