ayangd

kubus.cpp

Nov 9th, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.22 KB | None | 0 0
  1. // Proudly made by ayangd :p
  2.  
  3. #include <windows.h>
  4. #include <windowsx.h>
  5. #include <math.h>
  6. #include <gdiplus.h>
  7. #include <ctime>
  8.  
  9. using namespace Gdiplus;
  10. using namespace std;
  11.  
  12. typedef struct {
  13.     double x;
  14.     double y;
  15. } Vector;
  16.  
  17. #define SCREEN_WIDTH 640
  18. #define SCREEN_HEIGHT 480
  19.  
  20. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  21. void updateWindow(HWND hWnd);
  22.  
  23. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevHInst, LPSTR cmdLine, int nShowCmd) {
  24.     HWND hWnd;
  25.    
  26.     GdiplusStartupInput gdiplusStartupInput;
  27.     ULONG_PTR gdiplusToken;
  28.    
  29.     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  30.    
  31.     WNDCLASSEX wc;
  32.     ZeroMemory(&wc, sizeof(WNDCLASSEX));
  33.     wc.cbSize = sizeof(WNDCLASSEX);
  34.     wc.style = CS_HREDRAW | CS_VREDRAW;
  35.     wc.lpfnWndProc = WindowProc;
  36.     wc.hInstance = hInst;
  37.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  38.     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  39.     wc.lpszClassName = "WindowBalok";
  40.     RegisterClassEx(&wc);
  41.    
  42.     RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
  43.     AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
  44.    
  45.     hWnd = CreateWindowEx(0, // Previously NULL, but compiler complains
  46.                           "WindowBalok",
  47.                           "Kubus",
  48.                           WS_OVERLAPPEDWINDOW,
  49.                           100,
  50.                           100,
  51.                           wr.right - wr.left,
  52.                           wr.bottom - wr.top,
  53.                           NULL,
  54.                           NULL,
  55.                           hInst,
  56.                           NULL);
  57.     ShowWindow(hWnd, nShowCmd);
  58.    
  59.     MSG msg;
  60.     clock_t clocking = clock();
  61.     clock_t newClocking;
  62.     while (TRUE) {
  63.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  64.             TranslateMessage(&msg);
  65.             DispatchMessage(&msg);
  66.             if (msg.message == WM_QUIT)
  67.                 break;
  68.         }
  69.         newClocking = clock();
  70.         if (newClocking - clocking > (double) CLOCKS_PER_SEC / 60) {
  71.             RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
  72.             clocking += (double) CLOCKS_PER_SEC / 60;
  73.         }
  74. //      Sleep(1000 / 60);
  75.     }
  76.    
  77.     GdiplusShutdown(gdiplusToken);
  78.    
  79.     return msg.wParam;
  80. }
  81.  
  82. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  83.     switch (message) {
  84.         case WM_PAINT:
  85.             updateWindow(hWnd);
  86.             return 0;
  87.         case WM_ERASEBKGND:
  88.             return TRUE;
  89.         case WM_DESTROY:
  90.             PostQuitMessage(0);
  91.             return 0;
  92.     }
  93.    
  94.     return DefWindowProc(hWnd, message, wParam, lParam);
  95. }
  96.  
  97. void drawCube(Graphics *graph, Pen *pen, int centerX, int centerY, int scale, double rotation) {
  98.     static Vector corner[4];
  99.     static Vector cornerOff[5][4];
  100.     int i;
  101.     for (i = 0; i < 4; i++) {
  102.         corner[i].x = cos(rotation + (3.14 / 2) * i);
  103.         corner[i].y = sin(rotation + (3.14 / 2) * i) / 4;
  104.         corner[i].x *= (double) scale / 120;
  105.         corner[i].y *= (double) scale / 120;
  106.     }
  107.     graph->DrawLine(pen, (int)(centerX + 100 * corner[0].x), centerY + 100 * corner[0].y - scale / 2, centerX + 100 * corner[3].x, centerY + 100 * corner[3].y - scale / 2);
  108.     graph->DrawLine(pen, (int)(centerX + 100 * corner[0].x), centerY + 100 * corner[0].y + scale / 2, centerX + 100 * corner[3].x, centerY + 100 * corner[3].y + scale / 2);
  109.     for (i = 0; i < 4; i++) {
  110.         if (i != 0)
  111.             graph->DrawLine(pen, (int)(centerX + 100 * corner[i-1].x), centerY + 100 * corner[i-1].y - scale / 2, centerX + 100 * corner[i].x, centerY + 100 * corner[i].y - scale / 2);
  112.         if (i != 0)
  113.             graph->DrawLine(pen, (int)(centerX + 100 * corner[i-1].x), centerY + 100 * corner[i-1].y + scale / 2, centerX + 100 * corner[i].x, centerY + 100 * corner[i].y + scale / 2);
  114.         graph->DrawLine(pen,
  115.                         (int)(centerX + 100 * corner[i].x),
  116.                         (int)(centerY + 100 * corner[i].y) - scale / 2,
  117.                         (int)(centerX + 100 * corner[i].x),
  118.                         (int)(centerY + 100 * corner[i].y) + scale / 2);
  119.     }
  120.    
  121. }
  122.  
  123. void updateWindow(HWND hWnd) {
  124.     static HDC hdc;
  125.     static PAINTSTRUCT ps;
  126.     hdc = BeginPaint(hWnd, &ps);
  127.    
  128.     static Bitmap bmp(SCREEN_WIDTH, SCREEN_HEIGHT);
  129.     static Graphics* graphics = Graphics::FromImage(&bmp);
  130.     graphics->Clear(Color(255, 0, 0, 0));
  131.     static Pen pen(Color(255, 63, 255, 63));
  132.     static double spin = 0; spin += 0.01;
  133.    
  134.     int i;
  135.     for (i = 0; i < 20; i++) {
  136.         drawCube(graphics, &pen, (SCREEN_WIDTH / 2) + (cos(spin + (3.14 * 2) * i / 20) * 200), (SCREEN_HEIGHT / 2) + (sin(spin + (3.14 * 2) * i / 20) * 200), 30, -3.14 / 2 + (spin + (3.14 / 64) * i));
  137.     }
  138.    
  139.     drawCube(graphics, &pen, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 120, -3.14 / 2 + spin);
  140.     drawCube(graphics, &pen, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + (int)(sin(spin * 1) * 30), 60, -3.14 / 2 + spin * 2);
  141.     drawCube(graphics, &pen, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + (int)(sin(spin * 2) * 45), 30, -3.14 / 2 + spin * 3);
  142.     drawCube(graphics, &pen, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + (int)(sin(spin * 3) * 52), 15, -3.14 / 2 + spin * 4);
  143.     drawCube(graphics, &pen, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + (int)(sin(spin * 4) * 55), 7, -3.14 / 2 + spin * 5);
  144.    
  145.     FontFamily fontFamily(L"Consolas");
  146.     Font font(&fontFamily, 12);
  147.     SolidBrush brush(Color(255, 63, 255, 63));
  148.     static int dots = 0; dots = (dots + 1) % 60;
  149.     graphics->DrawString((dots < 20) ? L"Loading." : (dots < 40) ? L"Loading.." : L"Loading...", -1, &font, PointF(SCREEN_WIDTH / 2 - 45, SCREEN_HEIGHT * 29 / 40), &brush);
  150.    
  151.     Graphics realGraph(hdc);
  152.     realGraph.DrawImage(&bmp, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  153.    
  154.     DeleteObject(&bmp);
  155.    
  156.     EndPaint(hWnd, &ps);
  157.     DeleteObject(&hdc);
  158.     DeleteObject(&ps);
  159.     DeleteObject(&pen);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment