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 (10×10 pixel). So the final result would look like this
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
To draw to an image with Allegro 5, you need to do something like:
Now all of your drawing operations will happen on the image. To later save it:
You can also use
pngandjpgas extensions if your image library has support for it enabled.Use these functions to read the text file:
al_fopenal_fgetcal_feofal_fcloseSet
int xandyto zero. You’ll be looping over until the end of the file. On every iteration incrementxby one. If you reach a new line character (\n) incrementyby one and setxto zero. (You should ignore\rcharacters.)Now, depending on the character read, draw a tile:
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.