Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 137,227 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,014 people online right now. Registration is fast and FREE... Join Now!





Allegro-Some extra color functions

A simple implementation of a few useful functions I have made. For beginner purposes I have built off of the basic tutorial at http://cppgameprogramming.com/ though it should be cgameprogramming, since allegro is a C based lib (I'm pretty sure).

Submitted By: rbmj
Actions:
Rating:
Views: 178

Language: C

Last Modified: June 19, 2008
Instructions: You need the Allegro Library to compile. Can be done in C++, too (obviously). The most important function in here is BITMAP * FIND_AND_REPLACE_COLOR(BITMAP * buffer, int FIND_COLOR, int REPLACE_COLOR){}, which easily allows you to change the background color of a screen/buffer. This function requires few arguments thanks to the extensive allegro documentation. It extracts data from the bitmap* struct so that there is no need to input width/height. Now i'll stop talking and get to code.

Snippet


  1. #include <allegro.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SCREENWIDTH 640
  6. #define SCREENHEIGHT 480
  7.  
  8. BITMAP * FIND_AND_REPLACE_COLOR(BITMAP * buffer, int FIND_COLOR, int REPLACE_COLOR) {
  9.        int test_color;
  10.        int control, controlx, controly;
  11.        int SCREEN_HEIGHT = buffer->h;
  12.        int SCREEN_WIDTH = buffer->w;
  13.        for (controlx=0, controly=0;controly<=SCREEN_HEIGHT;controlx++){
  14.            if (controlx>SCREEN_WIDTH) {
  15.               controlx=0;
  16.               controly++;
  17.            }   
  18.            test_color = getpixel(buffer, controlx, controly);
  19.            if (test_color==FIND_COLOR) {
  20.               putpixel(buffer, controlx, controly, REPLACE_COLOR);
  21.            }
  22.            else {
  23.                 putpixel(buffer, controlx, controly, test_color);
  24.            }             
  25.        }
  26.        return (buffer);
  27. }
  28.  
  29. int CREATE_RAND_COLOR () {
  30.     int RAND_COLOR;
  31.     RAND_COLOR = makecol (rand() % 256, rand() % 256, rand() % 256);
  32.     return (RAND_COLOR);
  33. }
  34.    
  35. int INVERSE_COLOR (int COLOR) {
  36.     int COLOR_R, COLOR_G, COLOR_B;
  37.     int INVERSE_OF_COLOR;
  38.     COLOR_R = getr(COLOR);
  39.     COLOR_G = getg(COLOR);
  40.     COLOR_B = getb(COLOR);
  41.     COLOR_R = 255-COLOR_R;
  42.     COLOR_G = 255-COLOR_G;
  43.     COLOR_B = 255-COLOR_B;
  44.     INVERSE_OF_COLOR = makecol (COLOR_R, COLOR_G, COLOR_B);
  45.     return (INVERSE_OF_COLOR);
  46. }
  47.  
  48. int main() {
  49.     allegro_init();
  50.     srand(time(NULL));
  51.     BITMAP * buffer;
  52.     BITMAP * buffer2;
  53.     buffer = create_bitmap(SCREENWIDTH, SCREENHEIGHT);
  54.     int x=30;
  55.     int y=30;
  56.     int pixel;
  57.     int background_color, text_color, old_background_color, old_text_color;
  58.     install_keyboard();
  59.     set_gfx_mode( GFX_AUTODETECT , SCREENWIDTH, SCREENHEIGHT, 0, 0);
  60.     acquire_screen();
  61.     background_color=CREATE_RAND_COLOR();
  62.     text_color=INVERSE_COLOR(background_color);
  63.     rectfill(buffer, 0, 0, SCREENWIDTH, SCREENHEIGHT, background_color);
  64.     textout_ex(buffer, font, "To move around the @, press the arrow keys. Press <ESC> to exit.", 10, 10, text_color, background_color);
  65.     textout_ex(buffer, font, "To change the background color, press C.", 20, 20, text_color, background_color);
  66.     draw_sprite(screen, buffer, 0, 0);
  67.     release_screen();
  68.     while ( !key[KEY_ESC] ) {
  69.           clear_keybuf();
  70.           acquire_screen();
  71.           textout_ex(buffer, font, " ", x, y, background_color, background_color);
  72.           textout_ex(buffer, font, "To move around the @, press the arrow keys. Press <ESC> to exit.", 10, 10, text_color, background_color);
  73.           textout_ex(buffer, font, "To change the background color, press C.", 20, 20, text_color, background_color);
  74.           if (key[KEY_C]) {
  75.              do {
  76.                 old_background_color=background_color;
  77.                 old_text_color=text_color;
  78.                 background_color=CREATE_RAND_COLOR();
  79.                 text_color=INVERSE_COLOR(background_color);
  80.              }  while (background_color==old_text_color); //just in case, so that the the two find and replaces don't conflict
  81.              buffer = FIND_AND_REPLACE_COLOR(buffer, old_background_color, background_color);
  82.              buffer = FIND_AND_REPLACE_COLOR(buffer, old_text_color, text_color);
  83.           }
  84.           if (key[KEY_UP]) --y;
  85.           if (key[KEY_DOWN]) ++y;
  86.           if (key[KEY_RIGHT]) ++x;
  87.           if (key[KEY_LEFT]) --x;
  88.           textout_ex(buffer, font, "@", x, y, text_color, background_color);
  89.           draw_sprite(screen, buffer, 0, 0);
  90.           release_screen();
  91.           rest(50);
  92.     }
  93.     return 0;
  94. }
  95. END_OF_MAIN();
  96.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month