Hello!
I will begin with some quick facts to help you understand my problem. For now I am trying to make a circle, used as a crosshair on the game screen and when I press Space the game will "drop sand"(basically paint the place the crosshair aims with a sandy colour).
I would like to add that I am a beginner with Allegro so it's all Greek to me(well not really as I am Greek myself...). I am suspecting that I'm doing something insanely stupid but after 2 days of trying to figure it out I decided to give up and seek answers because Allegro5 is fairly new and there aren't many tutorials about it.
Since I am a beginner I used an Allegro5 "Pong" code and started editing stuff around. Felt much easier than coding everything from scratch and I actually learned something from it. I did read Allegro5 tutorials about Bitmaps and Display though.
What my problem is:
Basically I have a very simple goal. The cursor needs to be redrawn but the sand that the cursor drops shouldn't. I would like to add that I kept the ball code of "Pong" for experimental reasons so the ball needs to be redrawn too. However while playing around with the code I was juggling around:
A) Nothing showing up(cursor and ball was invisible)but pressing Space actually proved that everything was there as I could paint sand on screen normally.
B) Ball and Cursor not getting redrawn properly, they were drawn but not deleted so there is an endless trail for both when they move. "Sand" works normally here.
From what I understood to redraw something you have to use al_set_target_bitmap to select it, then use al_clear_to_color to "wipe" it and then proceed to redraw it. This is why I select both the ball and the cursor individually, clear them and then select the backbuffer and draw them again. This however is causing the problem of B case. What am I doing wrong?
Thanks in advance
PS. I am aware some of my code is not needed. It is legacy code from Pong I didn't delete either because it wasn't needed yet or because I am planning to use/fix in the future, however any tips are welcome of course!
It looks like you're kind of making a painting app; like MS Paint, etc.
In that case, you need an ALLEGRO_BITMAP that acts as a canvas for the user to draw to. So, let's get started.
Add this towards the top of main, as another variable:
ALLEGRO_BITMAP *canvas = NULL;
Add this during initialization, probably right after creating the cursor, sand, bouncer bitmaps:
canvas = al_create_bitmap(SCREEN_W, SCREEN_H);
This creates a canvas the size of the screen.
Add this after the code for clearing bouncer and cursor:
al_set_target_bitmap(canvas); al_clear_to_color(al_map_rgb(0,0,0));
That clears the canvas to black.
Inside the code where you call drop_sand do this instead:
al_set_target_bitmap(canvas); drop_sand(cursor_x, cursor_y); al_set_target_bitmap(al_get_backbuffer(display)); redraw = true;
This will draw the sand to the canvas.
Now in the code where you redraw the screen (line 271 in the code you posted), modify to this:
al_set_target_bitmap(al_get_backbuffer(display)); al_draw_bitmap(canvas, 0, 0, 0); // Draw the canvas over the entire screen draw_cursor(cursor_x, cursor_y); // Draw the cursor on top (on the screen) draw_ball(bouncer_x, bouncer_y);
That will "refresh" the screen with the current canvas, and draw the cursor and ball.
Right now, you're using the screen as your canvas. These new modifications just create an actual bitmap for the canvas, so you don't overwrite it every time
What are you planning to do with the sand after the user drops it?
If you're using C++, you might want to have a container that keeps track of where the sand has been dropped. Or if you go with a canvas idea like BillyBob suggested, then you could read a pixel location and compare it to your sand color.
Thanks for the help, gonna try it in a while as I just woke up!
As about the use of the sand, I guess I should have been more clear. My current goal is to only have a cursor and space to drop the sand as I said in the first post. However it's aimed to be a game where you have a secondary colour in the background(let's say blue)and you fill the coloured background with sand. I was even thinking of having a more complicated system like like :
Blue background: Drop sand once
Yellow: Drop sand twice
Green: Drop thrice
It should end up a precision game against time.