Handmade Hero»Forums»Code
3 posts
[Day 33] signed/unsigned addition
Edited by samrat on
Hi,
In day 33, at the end of the stream Casey does the following to compute `row` and `column` in the nested loop to draw the tiles:

1
2
uint32_t column = rel_column + gameState->player_p.abs_tile_x;
uint32_t row = rel_row + gameState->player_p.abs_tile_y;


Here, `rel_column` and `rel_row` are signed and can be negative while the player's tile coords are unsigned integers.

The problem I am facing is that column and row get computed as garbage values because their type is unsigned but the expression on the right hand side evaluates to a signed value. As a result the assert in `get_tile_value_unchecked` breaks the program.

This is a problem that I am having with code that I typed and Casey's code on the stream seems to working fine. So, it would be great if someone could help me understand what I might have missed.
Simon Anciaux
1337 posts
[Day 33] signed/unsigned addition
I may be wrong:
we want the game world to wraps around, meaning if you go to -1 you are in fact at the maximum x position.
E.g. if we work with 8 bits, a value of -1 equals 255 in as unsigned value.
1
2
3
int8 a = 0;
int8 b = -1;
uint8 c = a + b; // c = 255;

What you see as garbage value is probably the "other side" of the world (the highest int unsigned value).
3 posts
[Day 33] signed/unsigned addition
I re-watched the video and found out what my mistake was. I'd just forgotten to replace the call to `get_tile_value_unchecked` with `get_tile_value`. That got rid of the assert firing up.