1
\$\begingroup\$

I want to know if there is any better way to do this. But i want to make a game where its 2d and made of blocks. But a test that draws blocks to the screen and its going so slow that its not even taking keyboard events anymore.

Is there a way to optimize drawing? Or save predawn parts of the map and pan it across the screen when the player moves?

the code is probably unnecessary. But basically im drawing it and p

for (int y = 0; y != Vertical_Block_count; y++)
           {
    for (int x = 0; x != Horizontal_Block_count; x++)
al_draw_bitmap_region(Bitmap, Sprite_Selected * BlockSize, BlockSize * sprites_on_sprite_sheet, BlockSize, BlockSize, x * 16 - 16, y * 16 -16, 0);
            }
    al_flip_display();
\$\endgroup\$
2
\$\begingroup\$

You are creating and loading bitmaps before the Display is created with al_create_display... This is causing the bitmaps to be on system RAM and not Video RAM.

When they are stored in main RAM, you don't get hardware acceleration because everything has to be drawn with the CPU then copied to the GPU already drawn...

Try creating a screen as soon as the program start and then load your bitmaps. Another option is to load them first, if that is what you want, but you then copy them to the Video RAM as soon as you have the display available. Don't forget to delete the System RAM copies after doing that!

\$\endgroup\$
0
\$\begingroup\$

In general, a big help when drawing tiles is al_hold_bitmap_drawing. Assuming your tiles are all part of a single image (a 'spritesheet' or 'tile atlas'), you should use something like:

al_hold_bitmap_drawing(true);
for (int y = 0; y != Vertical_Block_count; y++)
    for (int x = 0; x != Horizontal_Block_count; x++)
        al_draw_bitmap_region(Bitmap, Sprite_Selected * BlockSize, BlockSize * sprites_on_sprite_sheet, BlockSize, BlockSize, x * 16 - 16, y * 16 -16, 0);
al_hold_bitmap_drawing(false);
al_flip_display();

Note that while bitmap drawing is held, you want to draw only from that one bitmap -- don't jump around to different bitmaps.

its going so slow that its not even taking keyboard events anymore.

Ideally, you want input to be processed no matter how slow your game gets. To do this, ensure you drain the event queue before drawing each frame. Take a look at the Allegro5 timer tutorial. Notice that the condition for rendering the next frame is redraw && al_is_event_queue_empty(event_queue). This ensures that all input events get a chance to be heard before the next frame.

\$\endgroup\$
  • \$\begingroup\$ Thank you allot. Everything works better. But i am still unable to achieve more than a frame a second drawing the same type of block across a screen of 500 x 500. Which means I'm drawing 961 blocks. Btw i should add im tring to make a 2d sandbox like King Arthur's Gold or Terraria. \$\endgroup\$ – GameHog Dec 29 '15 at 2:21
  • \$\begingroup\$ its hard to tell what the issue is from the info you've given, but drawing that many tiles should certainly not be that hard on performance. Here's a simple example of filling a 500x500 display with tiles drawn from a single bitmap). I'm easily getting 60FPS on my old laptop. \$\endgroup\$ – rcorre Dec 29 '15 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.