Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.10 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////
  2. #include "stdafx.h"
  3. #pragma comment(lib, "gdiplus.lib") // Dolaczamy biblioteke gdiplus
  4. #include <windows.h> //Plik naglowkowy zawieraj¹cy definicje funkcji, typy danych oraz makra WinAPI
  5. #include <Windows.h>
  6. #include <GdiPlus.h>
  7.  
  8. using namespace Gdiplus;
  9.  
  10. struct MyPixel{
  11.     int mBlackness;
  12.  
  13.     MyPixel(){
  14.         mBlackness=0;
  15.     }
  16.  
  17. };
  18.  
  19. MyPixel mFrontBuffer[40][30];
  20. int lClickCount=0,
  21.     xLClick=0,
  22.     yLClick=0;
  23.  
  24.  
  25. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  26.  
  27. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  28. {
  29.     MSG  msg ;    
  30.     HWND hWnd;
  31.     WNDCLASS wndClass;
  32.  
  33.  
  34.     wndClass.style          = CS_HREDRAW | CS_VREDRAW;  //Styl okna - bedzie sie odmalowywalo przy kazdym przesunieciu lub zmianie rozdzielczosci  
  35.     wndClass.lpfnWndProc    = WndProc;                              //Wskazujemy procedure przetwarzajaca komunikaty okna
  36.     wndClass.cbClsExtra     = 0;
  37.     wndClass.cbWndExtra     = 0;
  38.     wndClass.hInstance      = hInstance;                            //Ustawiamy w oknie instancje naszego programu
  39.     wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);      // Ladujemy z zasobow systemowych
  40.     wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);          // Domyslny kursor i ikone
  41.     wndClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);  // Kolor tla okna
  42.     wndClass.lpszMenuName   = NULL;                                 // Nie tworzymy menu
  43.     wndClass.lpszClassName  = TEXT("GettingStarted");               // Nazwa klasy okna, wyswietlana w naglowku okna
  44.  
  45.     RegisterClass(&wndClass);       //Rejestrujemy klase okna w systemie
  46.  
  47.  
  48.     hWnd = CreateWindow(
  49.         TEXT("GettingStarted"),   // window class name
  50.         TEXT("Getting Started"),  // window caption
  51.         WS_OVERLAPPEDWINDOW,      // window style
  52.         CW_USEDEFAULT,            // initial x position
  53.         CW_USEDEFAULT,            // initial y position
  54.         800,                      // initial x size
  55.         600,                      // initial y size
  56.         NULL,                     // parent window handle
  57.         NULL,                     // window menu handle
  58.         hInstance,                // program instance handle
  59.         NULL);                    // creation parameters
  60.  
  61.  
  62.  
  63.     RECT rect = { 0, 0, 800, 600 }; //Tworzymy prostokat o wymiarach 800x600
  64.     AdjustWindowRect( &rect, GetWindowLong( hWnd, GWL_STYLE ), FALSE ); //Skalujemy okno tak aby obszar roboczy faktycznie mial 800x600px
  65.     SetWindowPos( hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE );
  66.  
  67.     GdiplusStartupInput gdiplusStartupInput;    //Struktura zawierajaca parametry startowe
  68.     ULONG_PTR           gdiplusToken;       // Wskaznik pod ktory zostanie przypisany token
  69.  
  70.     // Initializacja GDI
  71.     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  72.  
  73.  
  74.     ShowWindow(hWnd, nCmdShow); //Wyswietlamy okno
  75.     UpdateWindow(hWnd);
  76.  
  77.     /*
  78.     Ponizej znajduje sie petla wiadomosci. Odbieramy
  79.     wiadomosci od systemu za pomoca funkcji GetMessage
  80.     i przekazujemy je do procedury okna.
  81.     */
  82.     while( GetMessage(&msg, NULL, 0, 0)) {
  83.         DispatchMessage(&msg);
  84.     }
  85.  
  86.     GdiplusShutdown(gdiplusToken);
  87.  
  88.     return (int) msg.wParam;
  89. }
  90.  
  91. void resetmFrontBuffer(){
  92.         for(int i=0; i<40; i++){
  93.    
  94.         for(int j=0; j<30; j++){
  95.             mFrontBuffer[i][j].mBlackness=0;
  96.         }
  97.     }
  98. }
  99.  
  100. int abs(int x){
  101.     int result=x;
  102.     if(x<0){
  103.         result=x*-1;;
  104.     }
  105.     return result;
  106. }
  107.  
  108. void FloodFill(int x, int y, int borderColor, int fillColor){
  109.  
  110.     if(x<=40 && y<=30 && x>=0 && y>=0){
  111.  
  112.         if(mFrontBuffer[x][y].mBlackness!=borderColor && mFrontBuffer[x][y].mBlackness==false){
  113.             mFrontBuffer[x][y].mBlackness=fillColor;
  114.             FloodFill(x+1, y, borderColor, fillColor);
  115.             FloodFill(x-1, y, borderColor, fillColor);
  116.             FloodFill(x, y+1, borderColor, fillColor);
  117.             FloodFill(x, y-1, borderColor, fillColor);
  118.         }
  119.    
  120.     }
  121.  
  122. }
  123.  
  124. void Bressenham(int x0, int y0, int x1, int y1, int blackness){
  125.  
  126.     int dx, dy, a, b, p, x, y, xEnd;
  127.  
  128.     if(x0<x1){
  129.         x=x0;
  130.         y=y0;
  131.         dx=x1-x0;
  132.         dy=y1-y0;
  133.         xEnd=x1;
  134.     }else{
  135.         x=x1;
  136.         y=y1;
  137.         dx=x0+x1;
  138.         dy=y0+y1;
  139.         xEnd=x0;
  140.     }
  141.  
  142.     a=2*dy;
  143.     b=2*dy-2*dx;
  144.  
  145.     p=2*dy-dx;
  146.  
  147.     while(x!=xEnd){
  148.         if(p>=0){
  149.             x++;
  150.             y++;
  151.             p+=b;
  152.         }else{
  153.             x++;
  154.             p+=a;
  155.         }
  156.         mFrontBuffer[x][y].mBlackness=blackness;
  157.     }
  158.        
  159.        
  160. }
  161.  
  162. void SimpleLine(int x0, int y0, int x1, int y1, int blackness){
  163.    
  164.     int dx, dy, b;
  165.     float a;
  166.  
  167.     dx=x1-x0;
  168.     dy=y1-y0;
  169.  
  170.     a=(float) dy/(float) dx;
  171.     b=y0-a*x0;
  172.  
  173.     mFrontBuffer[x0][y0].mBlackness=blackness;
  174.    
  175.     if(abs(dx)>abs(dy)){
  176.         dx = (x1 > x0) ? 1 : -1;
  177.         while(x0!=x1){
  178.             x0+=dx;
  179.             y0=a*x0+b;
  180.             mFrontBuffer[x0][y0].mBlackness=blackness;
  181.         }
  182.     }
  183.     else{
  184.         if(dy!=0){
  185.             a = (float) dx / (float) dy;  
  186.             b = x0 - a*y0;
  187.             dy = (dy < 0) ? -1 : 1;
  188.             while (y0 != y1) {
  189.                 y0 += dy;
  190.                 x0=a*y0+b;              
  191.                 mFrontBuffer[x0][y0].mBlackness=blackness;
  192.             }
  193.         }
  194.     }
  195. }
  196.  
  197. void DrawPixel(HDC hdc, int x, int y, int blackness){
  198.     Graphics graphics(hdc);
  199.     SolidBrush solidBrush(Color(blackness, 0, 0, 0));
  200.  
  201.     graphics.FillRectangle(&solidBrush, x, y, 20, 20);
  202. }
  203.  
  204. void Present(HDC hdc){
  205.    
  206.     for(int i=0; i<40; i++){
  207.    
  208.         for(int j=0; j<30; j++){
  209.             DrawPixel(hdc, i*20, j*20, mFrontBuffer[i][j].mBlackness);
  210.         }
  211.  
  212.     }
  213.  
  214. }
  215.  
  216. void DrawGrid(HDC hdc){
  217.  
  218.     Graphics graphics(hdc);
  219.     Pen pen(Color(255, 0, 0, 0));
  220.    
  221.  
  222.     for(int i=0; i<600; i=i+20){
  223.         graphics.DrawLine(&pen, 800, i, 0, i);
  224.     }
  225.  
  226.     for(int i=0; i<800; i=i+20){
  227.         graphics.DrawLine(&pen, i, 0, i, 600);
  228.     }
  229.  
  230.     //DrawPixel(hdc, 400, 400, 70);
  231.    
  232.    
  233. }
  234.  
  235.  
  236.  
  237. void OnPaint(HDC hdc){
  238.    
  239. //  Graphics graphics(hdc);
  240. //  Pen      pen(Color(255, 0, 0, 0)); //Konstruktor przyjmuje kolor w formacie ARGB
  241.  
  242. //  graphics.DrawLine(&pen, 0 , 0, 800, 600); //argumenty to obiekt Pen, x1, y1, x2, y2
  243.  
  244. //  Graphics graphics(hdc);
  245. //  SolidBrush solidBrush(Color(255, 0, 0, 0));
  246. //  graphics.FillRectangle(&solidBrush, 100, 100, 30, 10);
  247.  
  248.     DrawGrid(hdc);
  249.     Present(hdc);
  250.  
  251. //for(int i =0; i< 10; i = i + 1)
  252. //{
  253. //  graphics.DrawLine(&pen, i , 0, i, 600);
  254. //}
  255.  
  256.  
  257. }
  258.  
  259. LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
  260. {
  261.  
  262.     HDC          hdc;
  263.     PAINTSTRUCT  ps;
  264.  
  265.     int x, y;
  266.  
  267.     switch(msg)
  268.     {
  269.     case WM_LBUTTONDOWN:
  270.         x=LOWORD(lParam);
  271.         y=HIWORD(lParam);
  272.        
  273.         lClickCount++;
  274.  
  275.         if(lClickCount%2==0){
  276.             //SimpleLine(xLClick/20, yLClick/20, x/20, y/20, 40);
  277.             Bressenham(xLClick/20, yLClick/20, x/20, y/20, 40);
  278.         }else{
  279.             xLClick=x;
  280.             yLClick=y;
  281.         }
  282.  
  283.         RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
  284.     break;
  285.  
  286.     case WM_RBUTTONDOWN:
  287.         FloodFill(LOWORD(lParam)/20, HIWORD(lParam)/20, 40, 128);
  288.         //Bressenham(xClick, yClick, LOWORD(lParam), HIWORD(lParam), 80);
  289.         //SimpleLine(xClick, yClick, LOWORD(lParam), HIWORD(lParam), 80);
  290.         RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
  291.     break;
  292.  
  293.     case WM_PAINT:
  294.         hdc = BeginPaint(hwnd, &ps);
  295.         OnPaint(hdc);
  296.         EndPaint(hwnd, &ps);
  297.         return 0;
  298.  
  299.     case WM_DESTROY:
  300.         PostQuitMessage(0); // Ta funkcja dodaje do kolejki wiadomosci WM_QUIT
  301.         return 0;
  302.     }
  303.  
  304.     // wszystkie inne wiadomosci sa obslugiwane w sposob domyslny
  305.     return DefWindowProc(hwnd, msg, wParam, lParam);
  306. }
  307.  
  308.  
  309.  
  310. //////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement