Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #pragma comment(lib, "gdiplus.lib") // Dolaczamy biblioteke gdiplus
- #include <windows.h> //Plik naglowkowy zawieraj¹cy definicje funkcji, typy danych oraz makra WinAPI
- #include <Windows.h>
- #include <GdiPlus.h>
- using namespace Gdiplus;
- struct MyPixel{
- int mBlackness;
- MyPixel(){
- mBlackness=0;
- }
- };
- MyPixel mFrontBuffer[40][30];
- int lClickCount=0,
- xLClick=0,
- yLClick=0;
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
- {
- MSG msg ;
- HWND hWnd;
- WNDCLASS wndClass;
- wndClass.style = CS_HREDRAW | CS_VREDRAW; //Styl okna - bedzie sie odmalowywalo przy kazdym przesunieciu lub zmianie rozdzielczosci
- wndClass.lpfnWndProc = WndProc; //Wskazujemy procedure przetwarzajaca komunikaty okna
- wndClass.cbClsExtra = 0;
- wndClass.cbWndExtra = 0;
- wndClass.hInstance = hInstance; //Ustawiamy w oknie instancje naszego programu
- wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Ladujemy z zasobow systemowych
- wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Domyslny kursor i ikone
- wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // Kolor tla okna
- wndClass.lpszMenuName = NULL; // Nie tworzymy menu
- wndClass.lpszClassName = TEXT("GettingStarted"); // Nazwa klasy okna, wyswietlana w naglowku okna
- RegisterClass(&wndClass); //Rejestrujemy klase okna w systemie
- hWnd = CreateWindow(
- TEXT("GettingStarted"), // window class name
- TEXT("Getting Started"), // window caption
- WS_OVERLAPPEDWINDOW, // window style
- CW_USEDEFAULT, // initial x position
- CW_USEDEFAULT, // initial y position
- 800, // initial x size
- 600, // initial y size
- NULL, // parent window handle
- NULL, // window menu handle
- hInstance, // program instance handle
- NULL); // creation parameters
- RECT rect = { 0, 0, 800, 600 }; //Tworzymy prostokat o wymiarach 800x600
- AdjustWindowRect( &rect, GetWindowLong( hWnd, GWL_STYLE ), FALSE ); //Skalujemy okno tak aby obszar roboczy faktycznie mial 800x600px
- SetWindowPos( hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE );
- GdiplusStartupInput gdiplusStartupInput; //Struktura zawierajaca parametry startowe
- ULONG_PTR gdiplusToken; // Wskaznik pod ktory zostanie przypisany token
- // Initializacja GDI
- GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
- ShowWindow(hWnd, nCmdShow); //Wyswietlamy okno
- UpdateWindow(hWnd);
- /*
- Ponizej znajduje sie petla wiadomosci. Odbieramy
- wiadomosci od systemu za pomoca funkcji GetMessage
- i przekazujemy je do procedury okna.
- */
- while( GetMessage(&msg, NULL, 0, 0)) {
- DispatchMessage(&msg);
- }
- GdiplusShutdown(gdiplusToken);
- return (int) msg.wParam;
- }
- void resetmFrontBuffer(){
- for(int i=0; i<40; i++){
- for(int j=0; j<30; j++){
- mFrontBuffer[i][j].mBlackness=0;
- }
- }
- }
- int abs(int x){
- int result=x;
- if(x<0){
- result=x*-1;;
- }
- return result;
- }
- void FloodFill(int x, int y, int borderColor, int fillColor){
- if(x<=40 && y<=30 && x>=0 && y>=0){
- if(mFrontBuffer[x][y].mBlackness!=borderColor && mFrontBuffer[x][y].mBlackness==false){
- mFrontBuffer[x][y].mBlackness=fillColor;
- FloodFill(x+1, y, borderColor, fillColor);
- FloodFill(x-1, y, borderColor, fillColor);
- FloodFill(x, y+1, borderColor, fillColor);
- FloodFill(x, y-1, borderColor, fillColor);
- }
- }
- }
- void Bressenham(int x0, int y0, int x1, int y1, int blackness){
- int dx, dy, a, b, p, x, y, xEnd;
- if(x0<x1){
- x=x0;
- y=y0;
- dx=x1-x0;
- dy=y1-y0;
- xEnd=x1;
- }else{
- x=x1;
- y=y1;
- dx=x0+x1;
- dy=y0+y1;
- xEnd=x0;
- }
- a=2*dy;
- b=2*dy-2*dx;
- p=2*dy-dx;
- while(x!=xEnd){
- if(p>=0){
- x++;
- y++;
- p+=b;
- }else{
- x++;
- p+=a;
- }
- mFrontBuffer[x][y].mBlackness=blackness;
- }
- }
- void SimpleLine(int x0, int y0, int x1, int y1, int blackness){
- int dx, dy, b;
- float a;
- dx=x1-x0;
- dy=y1-y0;
- a=(float) dy/(float) dx;
- b=y0-a*x0;
- mFrontBuffer[x0][y0].mBlackness=blackness;
- if(abs(dx)>abs(dy)){
- dx = (x1 > x0) ? 1 : -1;
- while(x0!=x1){
- x0+=dx;
- y0=a*x0+b;
- mFrontBuffer[x0][y0].mBlackness=blackness;
- }
- }
- else{
- if(dy!=0){
- a = (float) dx / (float) dy;
- b = x0 - a*y0;
- dy = (dy < 0) ? -1 : 1;
- while (y0 != y1) {
- y0 += dy;
- x0=a*y0+b;
- mFrontBuffer[x0][y0].mBlackness=blackness;
- }
- }
- }
- }
- void DrawPixel(HDC hdc, int x, int y, int blackness){
- Graphics graphics(hdc);
- SolidBrush solidBrush(Color(blackness, 0, 0, 0));
- graphics.FillRectangle(&solidBrush, x, y, 20, 20);
- }
- void Present(HDC hdc){
- for(int i=0; i<40; i++){
- for(int j=0; j<30; j++){
- DrawPixel(hdc, i*20, j*20, mFrontBuffer[i][j].mBlackness);
- }
- }
- }
- void DrawGrid(HDC hdc){
- Graphics graphics(hdc);
- Pen pen(Color(255, 0, 0, 0));
- for(int i=0; i<600; i=i+20){
- graphics.DrawLine(&pen, 800, i, 0, i);
- }
- for(int i=0; i<800; i=i+20){
- graphics.DrawLine(&pen, i, 0, i, 600);
- }
- //DrawPixel(hdc, 400, 400, 70);
- }
- void OnPaint(HDC hdc){
- // Graphics graphics(hdc);
- // Pen pen(Color(255, 0, 0, 0)); //Konstruktor przyjmuje kolor w formacie ARGB
- // graphics.DrawLine(&pen, 0 , 0, 800, 600); //argumenty to obiekt Pen, x1, y1, x2, y2
- // Graphics graphics(hdc);
- // SolidBrush solidBrush(Color(255, 0, 0, 0));
- // graphics.FillRectangle(&solidBrush, 100, 100, 30, 10);
- DrawGrid(hdc);
- Present(hdc);
- //for(int i =0; i< 10; i = i + 1)
- //{
- // graphics.DrawLine(&pen, i , 0, i, 600);
- //}
- }
- LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
- {
- HDC hdc;
- PAINTSTRUCT ps;
- int x, y;
- switch(msg)
- {
- case WM_LBUTTONDOWN:
- x=LOWORD(lParam);
- y=HIWORD(lParam);
- lClickCount++;
- if(lClickCount%2==0){
- //SimpleLine(xLClick/20, yLClick/20, x/20, y/20, 40);
- Bressenham(xLClick/20, yLClick/20, x/20, y/20, 40);
- }else{
- xLClick=x;
- yLClick=y;
- }
- RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
- break;
- case WM_RBUTTONDOWN:
- FloodFill(LOWORD(lParam)/20, HIWORD(lParam)/20, 40, 128);
- //Bressenham(xClick, yClick, LOWORD(lParam), HIWORD(lParam), 80);
- //SimpleLine(xClick, yClick, LOWORD(lParam), HIWORD(lParam), 80);
- RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
- break;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- OnPaint(hdc);
- EndPaint(hwnd, &ps);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0); // Ta funkcja dodaje do kolejki wiadomosci WM_QUIT
- return 0;
- }
- // wszystkie inne wiadomosci sa obslugiwane w sposob domyslny
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- //////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement