Skip to content

Instantly share code, notes, and snippets.

@rcorre
Created December 29, 2015 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcorre/1d5176c0b231bc66316b to your computer and use it in GitHub Desktop.
Save rcorre/1d5176c0b231bc66316b to your computer and use it in GitHub Desktop.
Demonstrate drawing tiles with hold_bitmap_drawing
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_color.h>
#include <allegro5/allegro_image.h>
const int SCREEN_W = 500;
const int SCREEN_H = 500;
const int TILE_SIZE = 16;
const char *TILESET_NAME = "tileset.png";
int main() {
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_BITMAP *tileset = NULL;
al_init();
al_init_image_addon();
display = al_create_display(SCREEN_W, SCREEN_H);
tileset = al_load_bitmap(TILESET_NAME);
while (1) {
float start_time = al_get_time();
// blank the display and prepare to draw tiles
al_clear_to_color(al_map_rgb(0,0,0));
al_hold_bitmap_drawing(true);
// fill the screen with tiles drawn from a single bitmap
for (int y = 0 ; y <= SCREEN_H - TILE_SIZE ; y += TILE_SIZE)
for (int x = 0 ; x <= SCREEN_W - TILE_SIZE ; x += TILE_SIZE)
al_draw_bitmap_region(
tileset, // source bitmap
0, 0, TILE_SIZE, TILE_SIZE, // source rect
x, y, // destination (top-left)
0); // flags
// done drawing tiles for this frame
al_hold_bitmap_drawing(false);
al_flip_display();
float framerate = 1 / (al_get_time() - start_time);
printf("%f, ", framerate);
}
al_destroy_bitmap(tileset);
al_destroy_display(display);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment