Advertisement
SilverTES

FloodFill Study : Allegro 4.2

Nov 8th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.73 KB | None | 0 0
  1. #include <allegro.h>
  2. #include <memory>
  3. #include <vector>
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. BITMAP * mainBuffer;
  9. BITMAP * workBuffer;
  10. BITMAP * hudBuffer;
  11.  
  12. BITMAP * sprite;
  13.  
  14. int SCRW = 320;
  15. int SCRH = 180;
  16. int scale = 6;
  17.  
  18. class Frame
  19. {
  20. public:
  21.     Frame(int minX, int minY, int maxX, int maxY) : minX(minX), minY(minY), maxX(maxX), maxY(maxY)
  22.     {
  23.  
  24.     }
  25.  
  26.     ~Frame(){}
  27.     void draw(BITMAP* bmp, int color)
  28.     {
  29.         rect(bmp,minX,minY,maxX,maxY, color);
  30.     }
  31. protected:
  32. private:
  33.     int minX;
  34.     int minY;
  35.     int maxX;
  36.     int maxY;
  37. };
  38.  
  39.  
  40.  
  41. int random(int beginRange, int endRange)
  42. {
  43.     return (rand() % endRange) + beginRange;
  44. }
  45.  
  46.  
  47. void floodFill(BITMAP * bmp,int x, int y, int old, int color) // Remplit les pixels qui sont de même couleur que 'old' avec la couleur 'color'
  48. {
  49.     if (x>0 && x<SCRW && y>0 && y<SCRH)
  50.     {
  51.         int current;
  52.         current=getpixel(bmp,x,y);
  53.  
  54.         if(current==old)
  55.         {
  56.             putpixel(bmp,x,y,color);
  57.             floodFill(bmp,x+1,y,old,color);
  58.             floodFill(bmp,x-1,y,old,color);
  59.             floodFill(bmp,x,y+1,old,color);
  60.             floodFill(bmp,x,y-1,old,color);
  61.         }
  62.  
  63.     }
  64. }
  65.  
  66. void floodFillInside(BITMAP * bmp,int x, int y,int transcolor, int color) // Remplit les pixels qui sont de couleur différent de 'transcolor' avec la couleur 'color'
  67. {
  68.     if (x>0 && x<SCRW && y>0 && y<SCRH)
  69.     {
  70.         int current;
  71.         current=getpixel(bmp,x,y);
  72.         if(current!=transcolor)
  73.         {
  74.             if (current != color)
  75.             {
  76.                 putpixel(bmp,x,y,color);
  77.                 floodFillInside(bmp,x+1,y,transcolor,color);
  78.                 floodFillInside(bmp,x-1,y,transcolor,color);
  79.                 floodFillInside(bmp,x,y+1,transcolor,color);
  80.                 floodFillInside(bmp,x,y-1,transcolor,color);
  81.  
  82.                 floodFillInside(bmp,x-1,y-1,transcolor,color);
  83.                 floodFillInside(bmp,x+1,y-1,transcolor,color);
  84.                 floodFillInside(bmp,x-1,y+1,transcolor,color);
  85.                 floodFillInside(bmp,x+1,y+1,transcolor,color);
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. void findMaxSize(BITMAP * bmp,int x, int y,int transcolor, int &minX, int &minY, int &maxX, int &maxY) // Cherche les coordonnées min et max de la forme dont les couleurs sont différent de 'transcolor'
  92. {
  93.     if (x>0 && x<SCRW && y>0 && y<SCRH)
  94.     {
  95.         int current;
  96.         current=getpixel(bmp,x,y);
  97.         if(current!=transcolor)
  98.         {
  99.             if (x<minX) minX = x;
  100.             if (x>maxX) maxX = x;
  101.             if (y<minY) minY = y;
  102.             if (y>maxY) maxY = y;
  103.  
  104.             putpixel(bmp,x,y,transcolor);
  105.             findMaxSize(bmp,x+1,y,transcolor, minX, minY, maxX, maxY);
  106.             findMaxSize(bmp,x-1,y,transcolor, minX, minY, maxX, maxY);
  107.             findMaxSize(bmp,x,y+1,transcolor, minX, minY, maxX, maxY);
  108.             findMaxSize(bmp,x,y-1,transcolor, minX, minY, maxX, maxY);
  109.  
  110.             findMaxSize(bmp,x-1,y-1,transcolor, minX, minY, maxX, maxY);
  111.             findMaxSize(bmp,x+1,y-1,transcolor, minX, minY, maxX, maxY);
  112.             findMaxSize(bmp,x-1,y+1,transcolor, minX, minY, maxX, maxY);
  113.             findMaxSize(bmp,x+1,y+1,transcolor, minX, minY, maxX, maxY);
  114.         }
  115.     }
  116. }
  117.  
  118.  
  119. void randomFillRect(int left, int top, int right, int bottom)
  120. {
  121.     for (int i(0);i<100; i++)
  122.     {
  123.         int x(random(left,right-left));
  124.         int y(random(top,bottom-top));
  125.         putpixel(workBuffer,x,y,makecol(0,255,100));
  126.     }
  127. }
  128.  
  129.  
  130. int main(void)
  131. {
  132.     if (allegro_init() != 0)
  133.         return 1; /* you should always do this at the start of Allegro programs */
  134.  
  135.     install_keyboard();
  136.     install_mouse();
  137.  
  138.     set_color_depth(24);
  139.     set_display_switch_mode(SWITCH_AMNESIA);
  140.     if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCRW*scale, SCRH*scale, 0, 0) != 0)
  141.     {
  142.         allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
  143.         return 1;
  144.     }
  145.  
  146.     set_palette(desktop_palette); /* set the color palette */
  147.  
  148.     PALETTE palette;
  149.  
  150.     mainBuffer = create_bitmap(SCRW*scale,SCRH*scale);
  151.     workBuffer = create_bitmap(SCRW,SCRH);
  152.     hudBuffer = create_bitmap(SCRW,SCRH);
  153.  
  154.     sprite = load_bitmap("tarcus.tga",palette);
  155.     //sprite = load_bitmap("yoshi.tga",palette);
  156.  
  157.  
  158.  
  159.     srand (time(NULL));
  160.  
  161.     show_mouse(screen);
  162.  
  163.     int clearColor = makecol(25,55,55);
  164.     clear_to_color(workBuffer, clearColor);
  165.  
  166.     int xmouse(0);
  167.     int ymouse(0);
  168.  
  169.     int prevLineX(0);
  170.     int prevLineY(0);
  171.  
  172.     bool mouseButton2 = false;
  173.  
  174.     bool drawCursor(true);
  175.  
  176.     int vfade = 2;
  177.     int fade = 100;
  178.     int fadingColor = makecol(100,0,100);
  179.  
  180.     std::vector<Frame*> vecFrame;
  181.  
  182.     while (!key[KEY_ESC])
  183.     {
  184.         fade += vfade;
  185.         if (fade>250) vfade=-2;
  186.         if (fade<10) vfade=2;
  187.  
  188.         fadingColor = makecol(100,250-fade,fade);
  189.  
  190.         xmouse = mouse_x/scale;
  191.         ymouse = mouse_y/scale;
  192.  
  193.         // ---- MAIN BUFFER
  194.         acquire_bitmap(mainBuffer);
  195.         clear_bitmap(mainBuffer);
  196.         release_bitmap(mainBuffer);
  197.  
  198.         //---- WORK BUFFER
  199.         acquire_bitmap(workBuffer);
  200.         if (mouse_b & 1)
  201.         {
  202.             line(workBuffer, prevLineX,prevLineY, xmouse, ymouse, makecol(255,0,0));
  203.             prevLineX = xmouse;
  204.             prevLineY = ymouse;
  205.             drawCursor = false;
  206.         }
  207.         else
  208.         {
  209.             prevLineX = xmouse;
  210.             prevLineY = ymouse;
  211.             drawCursor = true;
  212.         }
  213.  
  214.         if (!(mouse_b & 2)) mouseButton2 = false;
  215.  
  216.         if(mouse_b & 2 && !mouseButton2)
  217.         {
  218.             mouseButton2 = true;
  219.             //floodFill(workBuffer,xmouse,ymouse, old,makecol(255,0,0));
  220.  
  221.             //floodFillInside(workBuffer,xmouse,ymouse,clearColor,makecol(255,255,0));
  222.  
  223.             //floodfill(workBuffer,xmouse,ymouse, makecol(255,0,0));
  224.  
  225.             int minX = xmouse;
  226.             int maxX = xmouse;
  227.             int minY = ymouse;
  228.             int maxY = ymouse;
  229.  
  230.             BITMAP * cache;
  231.             cache = create_bitmap(SCRW,SCRH);
  232.             blit(workBuffer,cache,0,0,0,0,SCRW,SCRH);
  233.             findMaxSize(workBuffer,xmouse,ymouse,clearColor,minX,minY,maxX,maxY);
  234.  
  235.             blit (cache, workBuffer, minX, minY, minX,minY, maxX-minX+1, maxY-minY+1);
  236.  
  237.             //rect(workBuffer,minX-1,minY-1,maxX+1,maxY+1, makecol(55,255,55));
  238.  
  239.             vecFrame.emplace_back(new Frame(minX-1,minY-1,maxX+1,maxY+1)); // ajoute un rectangle dans le hud !
  240.  
  241.             destroy_bitmap(cache);
  242.  
  243.         }
  244.  
  245.         if (key[KEY_BACKSPACE]) // On efface tout l'ecran !
  246.         {
  247.             for (auto it = vecFrame.begin(); it != vecFrame.end(); ++it){
  248.                 delete *it;
  249.             }
  250.             vecFrame.clear();
  251.  
  252.             clear_to_color(workBuffer, clearColor);
  253.             draw_sprite(workBuffer,sprite,12,12);
  254.  
  255.             for (int i(0);i<4; i++)
  256.             {
  257.                 int x(random(0,SCRW-24));
  258.                 int y(random(0,SCRH-24));
  259.                 draw_sprite(workBuffer,sprite,x,y);
  260.             }
  261.  
  262.         }
  263.  
  264.         //randomFillRect(10,10,60,60);
  265.  
  266.         //rect(workBuffer, 10,10,60,60, makecol(255,255,255));
  267.  
  268.         release_bitmap(workBuffer);
  269.  
  270.         //---- HUD BUFFER
  271.  
  272.         //drawCursor ^= 1; // on fait clignoter le curseur !
  273.         //drawCursor = false; // cache le curseur !
  274.  
  275.  
  276.         acquire_bitmap(hudBuffer);
  277.         clear_to_color(hudBuffer, makecol(255, 0, 255));
  278.         textout_centre_ex(hudBuffer, font, "MUGEN", SCRW/2, SCRH-10, fadingColor, -1);
  279.         textprintf_ex(hudBuffer,font,2,2,makecol(50,255,200),-1,"color = %i", getpixel(workBuffer,xmouse,ymouse));
  280.         textprintf_ex(hudBuffer,font,140,2,makecol(55,255,200),-1,"%i,%i", xmouse, ymouse );
  281.  
  282.         for (auto & frame: vecFrame)
  283.         {
  284.             frame->draw(hudBuffer, fadingColor);
  285.         }
  286.  
  287.         if (drawCursor)
  288.         {
  289.             hline(hudBuffer,0,ymouse, SCRW, makecol(100,100,100));
  290.             vline(hudBuffer,xmouse,0, SCRH, makecol(100,100,100));
  291.             putpixel(hudBuffer, xmouse, ymouse, makecol(150,150,150));
  292.         }
  293.  
  294.         release_bitmap(hudBuffer);
  295.  
  296.         stretch_blit(workBuffer,mainBuffer, 0,0,SCRW,SCRH,0,0,SCRW*scale,SCRH*scale);
  297.         masked_stretch_blit(hudBuffer,mainBuffer, 0,0,SCRW,SCRH,0,0,SCRW*scale,SCRH*scale);
  298.  
  299.         // FLIP ALL in SCREEN
  300.         blit(mainBuffer,screen,0,0,0,0,SCRW*scale,SCRH*scale);
  301.         rest(10);
  302.  
  303.     }
  304.  
  305.     for (auto it = vecFrame.begin(); it != vecFrame.end(); ++it){
  306.         delete *it;
  307.     }
  308.  
  309.     vecFrame.clear();
  310.  
  311.     destroy_bitmap(sprite);
  312.     destroy_bitmap(hudBuffer);
  313.     destroy_bitmap(workBuffer);
  314.     destroy_bitmap(mainBuffer);
  315.  
  316.     return 0;
  317. }
  318. END_OF_MAIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement