Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro.h>
- #include <memory>
- #include <vector>
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- BITMAP * mainBuffer;
- BITMAP * workBuffer;
- BITMAP * hudBuffer;
- BITMAP * sprite;
- int SCRW = 320;
- int SCRH = 180;
- int scale = 6;
- class Frame
- {
- public:
- Frame(int minX, int minY, int maxX, int maxY) : minX(minX), minY(minY), maxX(maxX), maxY(maxY)
- {
- }
- ~Frame(){}
- void draw(BITMAP* bmp, int color)
- {
- rect(bmp,minX,minY,maxX,maxY, color);
- }
- protected:
- private:
- int minX;
- int minY;
- int maxX;
- int maxY;
- };
- int random(int beginRange, int endRange)
- {
- return (rand() % endRange) + beginRange;
- }
- 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'
- {
- if (x>0 && x<SCRW && y>0 && y<SCRH)
- {
- int current;
- current=getpixel(bmp,x,y);
- if(current==old)
- {
- putpixel(bmp,x,y,color);
- floodFill(bmp,x+1,y,old,color);
- floodFill(bmp,x-1,y,old,color);
- floodFill(bmp,x,y+1,old,color);
- floodFill(bmp,x,y-1,old,color);
- }
- }
- }
- 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'
- {
- if (x>0 && x<SCRW && y>0 && y<SCRH)
- {
- int current;
- current=getpixel(bmp,x,y);
- if(current!=transcolor)
- {
- if (current != color)
- {
- putpixel(bmp,x,y,color);
- floodFillInside(bmp,x+1,y,transcolor,color);
- floodFillInside(bmp,x-1,y,transcolor,color);
- floodFillInside(bmp,x,y+1,transcolor,color);
- floodFillInside(bmp,x,y-1,transcolor,color);
- floodFillInside(bmp,x-1,y-1,transcolor,color);
- floodFillInside(bmp,x+1,y-1,transcolor,color);
- floodFillInside(bmp,x-1,y+1,transcolor,color);
- floodFillInside(bmp,x+1,y+1,transcolor,color);
- }
- }
- }
- }
- 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'
- {
- if (x>0 && x<SCRW && y>0 && y<SCRH)
- {
- int current;
- current=getpixel(bmp,x,y);
- if(current!=transcolor)
- {
- if (x<minX) minX = x;
- if (x>maxX) maxX = x;
- if (y<minY) minY = y;
- if (y>maxY) maxY = y;
- putpixel(bmp,x,y,transcolor);
- findMaxSize(bmp,x+1,y,transcolor, minX, minY, maxX, maxY);
- findMaxSize(bmp,x-1,y,transcolor, minX, minY, maxX, maxY);
- findMaxSize(bmp,x,y+1,transcolor, minX, minY, maxX, maxY);
- findMaxSize(bmp,x,y-1,transcolor, minX, minY, maxX, maxY);
- findMaxSize(bmp,x-1,y-1,transcolor, minX, minY, maxX, maxY);
- findMaxSize(bmp,x+1,y-1,transcolor, minX, minY, maxX, maxY);
- findMaxSize(bmp,x-1,y+1,transcolor, minX, minY, maxX, maxY);
- findMaxSize(bmp,x+1,y+1,transcolor, minX, minY, maxX, maxY);
- }
- }
- }
- void randomFillRect(int left, int top, int right, int bottom)
- {
- for (int i(0);i<100; i++)
- {
- int x(random(left,right-left));
- int y(random(top,bottom-top));
- putpixel(workBuffer,x,y,makecol(0,255,100));
- }
- }
- int main(void)
- {
- if (allegro_init() != 0)
- return 1; /* you should always do this at the start of Allegro programs */
- install_keyboard();
- install_mouse();
- set_color_depth(24);
- set_display_switch_mode(SWITCH_AMNESIA);
- if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCRW*scale, SCRH*scale, 0, 0) != 0)
- {
- allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
- return 1;
- }
- set_palette(desktop_palette); /* set the color palette */
- PALETTE palette;
- mainBuffer = create_bitmap(SCRW*scale,SCRH*scale);
- workBuffer = create_bitmap(SCRW,SCRH);
- hudBuffer = create_bitmap(SCRW,SCRH);
- sprite = load_bitmap("tarcus.tga",palette);
- //sprite = load_bitmap("yoshi.tga",palette);
- srand (time(NULL));
- show_mouse(screen);
- int clearColor = makecol(25,55,55);
- clear_to_color(workBuffer, clearColor);
- int xmouse(0);
- int ymouse(0);
- int prevLineX(0);
- int prevLineY(0);
- bool mouseButton2 = false;
- bool drawCursor(true);
- int vfade = 2;
- int fade = 100;
- int fadingColor = makecol(100,0,100);
- std::vector<Frame*> vecFrame;
- while (!key[KEY_ESC])
- {
- fade += vfade;
- if (fade>250) vfade=-2;
- if (fade<10) vfade=2;
- fadingColor = makecol(100,250-fade,fade);
- xmouse = mouse_x/scale;
- ymouse = mouse_y/scale;
- // ---- MAIN BUFFER
- acquire_bitmap(mainBuffer);
- clear_bitmap(mainBuffer);
- release_bitmap(mainBuffer);
- //---- WORK BUFFER
- acquire_bitmap(workBuffer);
- if (mouse_b & 1)
- {
- line(workBuffer, prevLineX,prevLineY, xmouse, ymouse, makecol(255,0,0));
- prevLineX = xmouse;
- prevLineY = ymouse;
- drawCursor = false;
- }
- else
- {
- prevLineX = xmouse;
- prevLineY = ymouse;
- drawCursor = true;
- }
- if (!(mouse_b & 2)) mouseButton2 = false;
- if(mouse_b & 2 && !mouseButton2)
- {
- mouseButton2 = true;
- //floodFill(workBuffer,xmouse,ymouse, old,makecol(255,0,0));
- //floodFillInside(workBuffer,xmouse,ymouse,clearColor,makecol(255,255,0));
- //floodfill(workBuffer,xmouse,ymouse, makecol(255,0,0));
- int minX = xmouse;
- int maxX = xmouse;
- int minY = ymouse;
- int maxY = ymouse;
- BITMAP * cache;
- cache = create_bitmap(SCRW,SCRH);
- blit(workBuffer,cache,0,0,0,0,SCRW,SCRH);
- findMaxSize(workBuffer,xmouse,ymouse,clearColor,minX,minY,maxX,maxY);
- blit (cache, workBuffer, minX, minY, minX,minY, maxX-minX+1, maxY-minY+1);
- //rect(workBuffer,minX-1,minY-1,maxX+1,maxY+1, makecol(55,255,55));
- vecFrame.emplace_back(new Frame(minX-1,minY-1,maxX+1,maxY+1)); // ajoute un rectangle dans le hud !
- destroy_bitmap(cache);
- }
- if (key[KEY_BACKSPACE]) // On efface tout l'ecran !
- {
- for (auto it = vecFrame.begin(); it != vecFrame.end(); ++it){
- delete *it;
- }
- vecFrame.clear();
- clear_to_color(workBuffer, clearColor);
- draw_sprite(workBuffer,sprite,12,12);
- for (int i(0);i<4; i++)
- {
- int x(random(0,SCRW-24));
- int y(random(0,SCRH-24));
- draw_sprite(workBuffer,sprite,x,y);
- }
- }
- //randomFillRect(10,10,60,60);
- //rect(workBuffer, 10,10,60,60, makecol(255,255,255));
- release_bitmap(workBuffer);
- //---- HUD BUFFER
- //drawCursor ^= 1; // on fait clignoter le curseur !
- //drawCursor = false; // cache le curseur !
- acquire_bitmap(hudBuffer);
- clear_to_color(hudBuffer, makecol(255, 0, 255));
- textout_centre_ex(hudBuffer, font, "MUGEN", SCRW/2, SCRH-10, fadingColor, -1);
- textprintf_ex(hudBuffer,font,2,2,makecol(50,255,200),-1,"color = %i", getpixel(workBuffer,xmouse,ymouse));
- textprintf_ex(hudBuffer,font,140,2,makecol(55,255,200),-1,"%i,%i", xmouse, ymouse );
- for (auto & frame: vecFrame)
- {
- frame->draw(hudBuffer, fadingColor);
- }
- if (drawCursor)
- {
- hline(hudBuffer,0,ymouse, SCRW, makecol(100,100,100));
- vline(hudBuffer,xmouse,0, SCRH, makecol(100,100,100));
- putpixel(hudBuffer, xmouse, ymouse, makecol(150,150,150));
- }
- release_bitmap(hudBuffer);
- stretch_blit(workBuffer,mainBuffer, 0,0,SCRW,SCRH,0,0,SCRW*scale,SCRH*scale);
- masked_stretch_blit(hudBuffer,mainBuffer, 0,0,SCRW,SCRH,0,0,SCRW*scale,SCRH*scale);
- // FLIP ALL in SCREEN
- blit(mainBuffer,screen,0,0,0,0,SCRW*scale,SCRH*scale);
- rest(10);
- }
- for (auto it = vecFrame.begin(); it != vecFrame.end(); ++it){
- delete *it;
- }
- vecFrame.clear();
- destroy_bitmap(sprite);
- destroy_bitmap(hudBuffer);
- destroy_bitmap(workBuffer);
- destroy_bitmap(mainBuffer);
- return 0;
- }
- END_OF_MAIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement