Advertisement
Guest User

Untitled

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