Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Performance] Load a bitmap screen vs Draw a screen

This thread is locked; no one can reply to it. rss feed Print
[Performance] Load a bitmap screen vs Draw a screen
Anatta
Member #16,686
June 2017

Hi everyone!

I'm making my game, but now I have to decide whether to load a bitmap for the background/maps or to draw it entirely. Any different in performance of both?
(Screen picture is in the attachment)
So far I redraw everything each frame:

#SelectExpand
1#define W 855 /* 855 == 19 * 45 */ 2#define H 665 /* 665 == 19 * 35 */ 3#define TSIZE 19 4#define WW WTW * TSIZE 5#define WH WTH * TSIZE 6#define WTW 30 /* wall width */ 7#define WTH 30 /* wall height */ 8 9void draw_grid() { 10 float offset = 0.5; 11 // horizon lines 12 for (int i = TSIZE; i < WH; i += TSIZE) { 13 al_draw_line(0 + offset, i + offset, WW - 1 + offset, i + offset, 14 color_grid, 1); 15 } 16 // vertical lines 17 for (int i = TSIZE; i < WW; i += TSIZE) { 18 al_draw_line(i + offset, 0 + offset, i + offset, WH - 1 + offset, 19 color_grid, 1); 20 } 21} 22 23void draw_wall() { 24 float tx = 0; 25 // horizon top wall 26 for (int i = 0; i < WTW; ++i) { 27 al_draw_bitmap(wall, tx, 0, 0); 28 tx += TSIZE; 29 } 30 // vertical walls 31 float ty = TSIZE; 32 tx = WTW * TSIZE - TSIZE; 33 for (int i = 2; i < WTH; ++i) { 34 al_draw_bitmap(wall, 0, ty, 0); 35 al_draw_bitmap(wall, tx, ty, 0); 36 ty += TSIZE; 37 } 38 // horizon bot wall 39 tx = 0; 40 for (int i = 0; i < WTW; ++i) { 41 al_draw_bitmap(wall, tx, ty, 0); 42 tx += TSIZE; 43 } 44} 45 46void draw_GUI() { 47 // lines----- 48 float line_thickness = 4; 49 al_draw_line(WW + TSIZE / 2, TSIZE, W - TSIZE / 2, TSIZE, 50 color_food, line_thickness); 51 al_draw_line(WW + TSIZE / 2, TSIZE, WW + TSIZE / 2, TSIZE * 9, 52 color_food, line_thickness); 53 al_draw_line(W - TSIZE / 2, TSIZE, W - TSIZE / 2, TSIZE * 9, 54 color_food, line_thickness); 55 // mode and score 56 al_draw_text(font, color_red, WW + (W - WW) / 2, TSIZE, 57 ALLEGRO_ALIGN_CENTRE, "MODE"); 58 al_draw_textf(font, color_red, WW + (W - WW) / 2, TSIZE * 3, 59 ALLEGRO_ALIGN_CENTRE, "%s", mode_name[mode]); 60 al_draw_textf(font, color_red, WW + TSIZE, TSIZE * 5, 61 ALLEGRO_ALIGN_LEFT, "%d", score[mode]); 62 al_draw_text(font, color_food, WW + TSIZE * 4, TSIZE * 5, 63 ALLEGRO_ALIGN_LEFT, "- SCORE"); 64 al_draw_textf(font, color_red, WW + TSIZE, TSIZE * 7, 65 ALLEGRO_ALIGN_LEFT, "%d", hscore[mode]); 66 al_draw_text(font, color_food, WW + TSIZE * 4, TSIZE * 7, 67 ALLEGRO_ALIGN_LEFT, "- HIGHEST"); 68 // line----- 69 al_draw_line(WW + 8, TSIZE * 9, W - 8, TSIZE * 9, 70 color_food, line_thickness); 71 // menu 72 al_draw_text(font, color_red, WW + (W - WW) / 2, TSIZE * 9, 73 ALLEGRO_ALIGN_CENTRE, "MENU"); 74} 75 76 77 if (redraw && al_is_event_queue_empty(evque)) { 78 al_clear_to_color(color_background); 79 draw_grid(); 80 al_draw_bitmap(food, foodx, foody, 0); 81 al_draw_bitmap(head, headx, heady, 0); 82 for (int i = 0; i < tail_len; ++i) { 83 al_draw_bitmap(tail, tailx[i], taily[i], 0); 84 } 85 draw_wall(); 86 draw_GUI(); 87 al_flip_display(); 88 redraw = false; 89 }

Niunio
Member #1,975
March 2002
avatar

Have you finished the game? You shouldn't optimize until it is finished, as you can't get an actual profile information.

-----------------
Current projects: Allegro.pas | MinGRo

Anatta
Member #16,686
June 2017

No, I haven't finished it yet. Thank for the advice, I'll continue to redraw everything.

Neil Roy
Member #2,229
April 2002
avatar

I would decide how you want to do it now. I have heard the "don't optimize now" line, but that is not true. Unless you wish to totally rewrite your code later on, you should decide which way to go with now.

For the type of game you have, I don't think you have to worry about optimization. You may wish to go with bitmaps which you load if you want to change the graphics later on and have something different.

You could also draw them ahead of time just once, then just blit that premade bitmap.

If this is how you want your game to look in the end, than it looks like drawing it each time will be fine. If it is turn based especially. If it gets too complicated you may wish to change that.

You could also only update the parts of the screen that change.

Take some time and maybe write up a design document on how you want the game to end up like. Plan it all out. This actually saves you A LOT of headaches later on believe it or not. You will know where you will need speed and what to be concerned with.

For my own games I tend to plan it out and then t he first thing I program is a level editor, this way I know what will be in the levels and can actually have levels created to test the main game with. I have also given out my level editors with my game, which is a bonus. Depends on your game though. But waiting until you are done to optimize it and finding out you done something wrong and have to recode large sections is not fun. And this is not just my opinion, I have seen professionals online who advise against waiting until you're done to optimize it.

---
“I love you too.” - last words of Wanda Roy

Go to: