Reputation: 56
I'm writing a 2D object editor so that I can more quickly create UI for basic programs. I'm not using any bitmaps at all. Instead I'm just drawing primitives with function calls in the game loop. Everything so far has been fine, but I'd like to know how to alter the colors of objects during runtime.
The basic concept is that I use a variable for the color, and when the user calls the function that changes the value of that variable, that changes the color. Simple. Something like:
void runColor(){
ALLEGRO_COLOR color = NULL;
if(event.mouse.button&1)
color = al_map_rgb(255, 0, 0);
else
color = al_map_rgb(0, 255, 0);
al_draw_filled_rectangle(firstXval, firstYval, secondXval, secondYval, color);
}
Unfortunately, this doesn't work. When I use similar code, the objects are simply not drawn up. There's no compilation errors, no run-time errors, no nothing. Instead, the objects have no color and are therefore invisible, though I've verified that they do in fact get created. Sooo I guess that means the color variable is somehow the right type, but does not retain the value of al_map_rgb()
when it's set equal to whatever that function returns. I know it has to return something, because otherwise it couldn't be an argument in the al_draw
functions.
While I could just write out if else if statements that lead to different draw functions, I'd rather just have a simple little bundle of code like above than a bunch of redundant stuff that solves something that should be simple. I'd just prefer not to go with the code equivalent of duct tape.
So I'm sure the problem probably has to do with my assumption that al_map_rgb()
returns a value of ALLEGRO_COLOR
, but I don't know. Would you guys be willing to make some suggestions?
Upvotes: 1
Views: 1011
Reputation: 48294
First off, there's no need to make any assumptions. As you can see in the manual, al_map_rgb()
does indeed return an ALLEGRO_COLOR
object.
There's nothing wrong with that bit of code. Either the x,y
values are incorrect, the target bitmap is not what you expect, or you are not calling al_flip_display()
. Be sure to redraw the entire scene on every frame (i.e., each al_flip_display()
) call.
Also, when drawing primitives, make sure you understand how the coordinates work. When drawing a filled rectangle, you'll be specifying the outer points (x1,y1)-(x1+w,y1+h)
.
Upvotes: 0