Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » DAllegro and D confusing output...

This thread is locked; no one can reply to it. rss feed Print
DAllegro and D confusing output...
Chris Katko
Member #1,881
January 2002
avatar

[edit 1] DOOOOH. I accidentally placed my color code above allegro_init. UHGGGHHH.

[edit 2] WAIT. Now THIS is still really confusing. If I printf with floats, they display correctly:

(RGBA)
0.501961 0.250980 0.501961 1.000000
0.501961 0.250980 0.501961 1.000000
0.501961 0.250980 0.501961 1.000000

Now watch what happens when I use printf unsigned int:

1056997505 1056997505 255        2011460928
0          0          2013416856 0
1056997505 1056997505 2013416888 2011460928

What the heck is going on? Are there multiple "float" values for the same thing? Why would they change going from one variable to another, and why ONLY when using printf with integer output?

Here's both output, at the same time, so you can be sure there's no other data changes:

printf("%12u %12u %12u %12u - %12f %12f %12f %12f \n", c.r, c.g, c.b, c.a, c.r, c.g, c.b, c.a);

  1056997505   1056997505          255    991089984 -     0.501961     0.250980     0.501961     1.000000 
           0            0    993045912            0 -     0.501961     0.250980     0.501961     1.000000 
  1056997505   1056997505    993045944    991089984 -     0.501961     0.250980     0.501961     1.000000

Using ul instead of u (unsigned long) doesn't help either.

[orignal post:]

I keep getting strange problems. One is, printf doesn't seem to always output.

But the DAllegro specific issue...

#SelectExpand
1 2struct player_t 3{ 4ALLEGRO_COLOR c; 5 6void setup(ALLEGRO_COLOR c) 7 { 8 color = c; 9 printf("%i %i %i\n", c.r, c.g, c.b); 10 } 11 12void draw(float offset_x, float offset_y) 13 { 14 al_draw_circle(x - offset_x, y - offset_y, 10, al_map_rgb(255,0,255), 2); 15 } 16 17} 18 19player_t [2] players; 20 21 22void initialize() 23 { 24 printf("---------------\n"); 25 ALLEGRO_COLOR x = al_map_rgb(128, 64, 128); 26 printf("%i %i %i\n", x.r, x.g, x.b); //first line output 27 28 g.players[0].setup( x ); //second line output 29 g.players[1].setup( al_map_rgb(128, 64, 128) ); //third line output 30 31 printf("---------------\n"); 32 33 //... 34 }

Output:

---------------
0 0 255
0 0 -111424104
0 0 -111424072
---------------

First, why is R=0, G=0 in the first line?

Secondly, why does the color data explode when I store it in a struct?

I'm looking into this problem because I'm writing something super-simple and trying to draw my "players" with a set color. And whenever I pass the data, they go invisible.

I thought structs were value-types, so they should copy in just fine. And if they're going out of scope, shouldn't that be a segfault?

Hmm...

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

SiegeLord
Member #7,827
October 2006
avatar

Why would they change going from one variable to another, and why ONLY when using printf with integer output?

You're running into the gnarly details of the calling convention (probably optlink, if you're on Windows). Floating point values and integer values are probably placed in different memory locations, and by asking it to decode integers when you're passing floats, you're likely reading garbage.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Chris Katko
Member #1,881
January 2002
avatar

I'm running Linux, but the same may apply. It's certainly a head scratcher for me.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

DanielH
Member #934
January 2001
avatar

I've never used D, but

Float: 0.501961

Actual float value stored in memory: 0.5019609928131103515625

Breakdown:
float = sign : exponent : mantissa

sign: 0
exp: 01111110
mantissa: 00000001000000010000100

Binary: 00111111000000001000000010000100
Hex: 0x3f008084
Integer: 1056997508

Chris Katko
Member #1,881
January 2002
avatar

Yes! But perhaps I didn't make it clearer. Why does the value change... once it's stored into a class method?

The only possibility (which I don't think exists on further exploration) was that there were multiple bitfield values that equal a float value and somehow it was "converted" to the second one in some kind of casting or something. But I think that's false.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Go to: