View Full Version: First Allegro Program

C++ Learning Community > C++ Creations > First Allegro Program


Title: First Allegro Program


Rmstn1580 - January 24, 2007 02:58 AM (GMT)
I don't know if this is to go in Other Programming Languages or here, but I decided here because it is in C++ :). Basically, I just wanted to see if I could handle Allegro, and I guess I can. This program is pretty useless, but I had fun making it. It generates a random bitmap and allows you to save it to your hard-drive if you wish. Here is my code.

CODE
#include <conio.h>
#include <stdlib.h>
#include "allegro.h"

#define MODE GFX_AUTODETECT_WINDOWED
#define WIDTH 640
#define HEIGHT 480
#define WHITE makecol(255, 255, 255)
#define BLACK makecol(0, 0, 0)

void makeNew();

void makeNew() {
    BITMAP *bmp;
    bmp = create_bitmap(200, 200);
    rectfill(screen, 0, 0, SCREEN_W, SCREEN_H, BLACK);
    int x1, y1, x2, y2;
    int red, green, blue, color;
   
    for(int i = 0; i < 10; i++) {
            x1 = rand() % (bmp->w - 20);
            y1 = rand() % (bmp->h - 20);
            x2 = rand() % (bmp->w - 20);
            y2 = rand() % (bmp->h - 20);
            red = rand() % 255;
            green = rand() % 255;
            blue = rand() % 255;
            color = makecol(red, green, blue);
           
            rectfill(bmp, x1, y1, x2, y2, color);
    }
    blit(bmp, screen, 0, 0, (SCREEN_W - bmp->w) / 2, (SCREEN_H - bmp->h) / 2, bmp->w, bmp->h);
    textout_centre(screen, font, "Are you satisfied with this image? (Y - N)", SCREEN_W/2, SCREEN_H - 40, WHITE);
    while(!key[KEY_Y] || !key[KEY_N]) {
                      if(key[KEY_Y]) {
                                     save_bitmap("C:\\ImageFromSave.bmp", bmp, NULL);
                                     destroy_bitmap(bmp);
                                     rectfill(screen, 0, 0, SCREEN_W, SCREEN_H, BLACK);
                                     textout_centre(screen, font, "Image Saved to Drive C", SCREEN_W/2, SCREEN_H/2, WHITE);
                                     textout_centre(screen, font, "Path: C:\\ImageFromSave.bmp", SCREEN_W/2, (SCREEN_H + 30)/2, WHITE);
                                     rest(3000);
                                     rectfill(screen, 0, 0, SCREEN_W, SCREEN_H, BLACK);
                                     return;
                      }
                      else if(key[KEY_N]) {
                           destroy_bitmap(bmp);
                           rectfill(screen, 0, 0, SCREEN_W, SCREEN_H, BLACK);
                           return;
                      }
    }
   
    destroy_bitmap(bmp);
    return;
}

void main(void)
{
    allegro_init();
    install_keyboard();
    install_timer();
    srand(time(NULL));
    set_color_depth(16);
    if(set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0) != 0)
    {
                          allegro_message(allegro_error);
                          return;
    }
   
    while(!key[KEY_ESC])
    {
                       textout(screen, font, "Random Bitmap Creator - http://www.nox-tech.com/kswp88", 0, 0, WHITE);
                       textout_centre(screen, font, "Rmstn1580's Bitmap Creator", SCREEN_W / 2, SCREEN_H / 2, WHITE);
                       textout_centre(screen, font, "Press A to start", SCREEN_W / 2, (SCREEN_H + 30) / 2, WHITE);
                       textout_centre(screen, font, "Press ESC to quit", SCREEN_W / 2, (SCREEN_H + 60) / 2, WHITE);
                       
                       if(key[KEY_A]) {
                                      makeNew();
                       }
    }
    allegro_exit();
    return;
}
END_OF_MAIN();


Please tell me what you think and I'm open to suggestions!

EDIT: It's available for download here -> http://www.nox-tech.com/kswp88/Downloads/B...p%20Creator.zip

biggoron - January 24, 2007 02:19 PM (GMT)
It was okay but not really very useful.

QUOTE
#define WHITE makecol(255, 255, 255)
#define BLACK makecol(0, 0, 0)

I wouldn't do this because it substitutes functions. This is okay if you use it only once, but it means that your program will make more function calls than is necessary. What I would do is find out what makecol(255, 255, 255) returns and #define that. makecol(0, 0, 0) will always return 0 so calling that function is a waste of time.




Hosted for free by InvisionFree