Snippet
#include <allegro.h>
#include <stdlib.h>
#include <time.h>
#define SCREENWIDTH 640
#define SCREENHEIGHT 480
BITMAP * FIND_AND_REPLACE_COLOR(BITMAP * buffer, int FIND_COLOR, int REPLACE_COLOR) {
int test_color;
int control, controlx, controly;
int SCREEN_HEIGHT = buffer->h;
int SCREEN_WIDTH = buffer->w;
for (controlx=0, controly=0;controly<=SCREEN_HEIGHT;controlx++){
if (controlx>SCREEN_WIDTH) {
controlx=0;
controly++;
}
test_color = getpixel(buffer, controlx, controly);
if (test_color==FIND_COLOR) {
putpixel(buffer, controlx, controly, REPLACE_COLOR);
}
else {
putpixel(buffer, controlx, controly, test_color);
}
}
return (buffer);
}
int CREATE_RAND_COLOR () {
int RAND_COLOR;
RAND_COLOR = makecol (rand() % 256, rand() % 256, rand() % 256);
return (RAND_COLOR);
}
int INVERSE_COLOR (int COLOR) {
int COLOR_R, COLOR_G, COLOR_B;
int INVERSE_OF_COLOR;
COLOR_R = getr(COLOR);
COLOR_G = getg(COLOR);
COLOR_B = getb(COLOR);
COLOR_R = 255-COLOR_R;
COLOR_G = 255-COLOR_G;
COLOR_B = 255-COLOR_B;
INVERSE_OF_COLOR = makecol (COLOR_R, COLOR_G, COLOR_B);
return (INVERSE_OF_COLOR);
}
int main() {
allegro_init();
srand(time(NULL));
BITMAP * buffer;
BITMAP * buffer2;
buffer = create_bitmap(SCREENWIDTH, SCREENHEIGHT);
int x=30;
int y=30;
int pixel;
int background_color, text_color, old_background_color, old_text_color;
install_keyboard();
set_gfx_mode( GFX_AUTODETECT , SCREENWIDTH, SCREENHEIGHT, 0, 0);
acquire_screen();
background_color=CREATE_RAND_COLOR();
text_color=INVERSE_COLOR(background_color);
rectfill(buffer, 0, 0, SCREENWIDTH, SCREENHEIGHT, background_color);
textout_ex(buffer, font, "To move around the @, press the arrow keys. Press <ESC> to exit.", 10, 10, text_color, background_color);
textout_ex(buffer, font, "To change the background color, press C.", 20, 20, text_color, background_color);
draw_sprite(screen, buffer, 0, 0);
release_screen();
while ( !key[KEY_ESC] ) {
clear_keybuf();
acquire_screen();
textout_ex(buffer, font, " ", x, y, background_color, background_color);
textout_ex(buffer, font, "To move around the @, press the arrow keys. Press <ESC> to exit.", 10, 10, text_color, background_color);
textout_ex(buffer, font, "To change the background color, press C.", 20, 20, text_color, background_color);
if (key[KEY_C]) {
do {
old_background_color=background_color;
old_text_color=text_color;
background_color=CREATE_RAND_COLOR();
text_color=INVERSE_COLOR(background_color);
} while (background_color==old_text_color); //just in case, so that the the two find and replaces don't conflict
buffer = FIND_AND_REPLACE_COLOR(buffer, old_background_color, background_color);
buffer = FIND_AND_REPLACE_COLOR(buffer, old_text_color, text_color);
}
if (key[KEY_UP]) --y;
if (key[KEY_DOWN]) ++y;
if (key[KEY_RIGHT]) ++x;
if (key[KEY_LEFT]) --x;
textout_ex(buffer, font, "@", x, y, text_color, background_color);
draw_sprite(screen, buffer, 0, 0);
release_screen();
rest(50);
}
return 0;
}
END_OF_MAIN();
Copy & Paste
|