1

hello what I'm currently dealing with is the ability to get input from a text file and then convert it into a bitmap and save it to a file.

the input looks like this:

########
#      #
########

and I want to draw it using allegro and instead of # there would be pixels of specified size. Each # should represent a tile (10x10 pixel). So the final result would look like this

link to an image

I've actually drawn it using this code:

 for (int i = 0; i < 80; i++){
        for (int j = 0; j < 10; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }
    for (int i = 0; i < 10; i++){
        for (int j = 10; j < 20; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }
    for (int i = 70; i < 80; i++){
        for (int j = 10; j < 20; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }
    for (int i = 0; i < 80; i++){
        for (int j = 20; j < 30; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }

yeah that's pretty bad, so how do I achieve something like that but with a common procedure which would be independent on the text file? thanks for any advice.

note: the only allowed headers are allegro5/allegro.h and allegro5/allegro_image.h

Markus
  • 686
  • 1
  • 11
  • 18

1 Answers1

0

To draw to an image with Allegro 5, you need to do something like:

ALLEGRO_BITMAP *bmp = al_create_bitmap(640, 480);
al_set_target_bitmap(bmp);

Now all of your drawing operations will happen on the image. To later save it:

al_save_bitmap("somefile.bmp", bmp);

You can also use png and jpg as extensions if your image library has support for it enabled.

Use these functions to read the text file:

  • al_fopen
  • al_fgetc
  • al_feof
  • al_fclose

Set int x and y to zero. You'll be looping over until the end of the file. On every iteration increment x by one. If you reach a new line character (\n) increment y by one and set x to zero. (You should ignore \r characters.)

Now, depending on the character read, draw a tile:

ALLEGRO_BITAMP *tile_to_draw = NULL;
if (c == '#')
   tile_to_draw = bmp1;
else if (c == ' ')
   tile_to_draw = bmp2;

if (tile_to_draw)
   al_draw_bitmap(tile_to_draw, x * 10, y * 10, 0);

Of course there's better ways to map characters to tiles than a series of ifs, but the above works and should be enough to help you finish your homework.

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • where do I store those values? or I don't have to store them at all? like int c; or an array of char to store all those characters? also what are those bmp1 and bmp2? – Markus Apr 28 '12 at 18:56
  • also there is an error with the x counter.. since the al_feof return EOF so x takes EOF and has value of 2. How to fix that? – Markus Apr 28 '12 at 19:16
  • `c` represents the value of `al_fgetc` for the current iteration. You really should be able to get a working solution based on my code. If not, you probably need more help than this site is really designed to provide, and you should consider getting help at some forums, such as http://www.allegro.cc/forums. – Matthew Apr 29 '12 at 01:25