Amorf

Untitled

Dec 14th, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include "Piece.h"
  2. #include "Game.h"
  3. #include <windows.h>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. const int PX_PER_BLOCK = 25;    // Cell size in pixels
  9. const int SCREEN_WIDTH = 10;    // Level width in cells
  10. const int SCREEN_HEIGHT = 20;   // Level height in cells
  11. const int GAME_SPEED = 33;      // Update the game every GAME_SPEED millisecs (= 1/fps)
  12. const int TIMER_ID = 1;
  13.  
  14. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  15.  
  16. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  17.                    PSTR szCmdLine, int iCmdShow)
  18. {
  19.  
  20.     static TCHAR szAppName[] = TEXT("tetris");
  21.     HWND hwnd;
  22.     MSG msg;
  23.     WNDCLASSEX wc;
  24.  
  25.     // We need to repaint a lot, using CS_OWNDC is more efficient
  26.     wc.cbSize = sizeof(WNDCLASSEX);
  27.     wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  28.     wc.lpfnWndProc = WndProc;
  29.     wc.cbClsExtra = 0;
  30.     wc.cbWndExtra = 0;
  31.     wc.hInstance = hInstance;
  32.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  33.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  34.     wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  35.     wc.lpszMenuName = NULL;
  36.     wc.lpszClassName = szAppName;
  37.     wc.hIconSm = NULL;
  38.  
  39.     if (!RegisterClassEx(&wc))
  40.     {
  41.         MessageBox(NULL, TEXT("Program requires Windows!"),
  42.                    szAppName, MB_ICONERROR);
  43.         return 0;
  44.     }
  45.  
  46.     hwnd = CreateWindow(szAppName,
  47.                         TEXT("Курсовая работа Тетрис Кончиц 051007"),
  48.                         WS_MINIMIZEBOX | WS_SYSMENU,  // No window resizing
  49.                         CW_USEDEFAULT,
  50.                         CW_USEDEFAULT,
  51.                         SCREEN_WIDTH * PX_PER_BLOCK + 156,
  52.                         SCREEN_HEIGHT * PX_PER_BLOCK + 25,
  53.                         NULL,
  54.                         NULL,
  55.                         hInstance,
  56.                         NULL);
  57.  
  58.     ShowWindow(hwnd, iCmdShow);
  59.     UpdateWindow(hwnd);
  60.  
  61.     while (GetMessage(&msg, NULL, 0, 0))
  62.     {
  63.         TranslateMessage(&msg);
  64.         DispatchMessage(&msg);
  65.     }
  66.  
  67.     return msg.wParam;
  68. }
  69.  
  70. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  71. {
  72.     HDC hdc;
  73.     PAINTSTRUCT ps;
  74.     static Game *game;
  75.     static DrawEngine *de;
  76.  
  77.     switch (message)
  78.     {
  79.         case WM_CREATE:
  80.             hdc = GetDC(hwnd);
  81.  
  82.             de = new DrawEngine(hdc, hwnd, PX_PER_BLOCK);
  83.             game = new Game(*de);
  84.             SetTimer(hwnd, TIMER_ID, GAME_SPEED, NULL);
  85.  
  86.             ReleaseDC(hwnd, hdc);
  87.             return 0;
  88.  
  89.         case WM_KEYDOWN:
  90.             game->keyPress(wParam);
  91.             return 0;
  92.  
  93.         case WM_TIMER:
  94.             game->timerUpdate();
  95.             return 0;
  96.  
  97.         case WM_KILLFOCUS:
  98.             KillTimer(hwnd, TIMER_ID);
  99.             game->pause(true);
  100.             return 0;
  101.  
  102.         case WM_SETFOCUS:
  103.             SetTimer(hwnd, TIMER_ID, GAME_SPEED, NULL);
  104.             return 0;
  105.  
  106.         case WM_PAINT:
  107.             hdc = BeginPaint(hwnd, &ps);
  108.             game->repaint();
  109.             EndPaint(hwnd, &ps);
  110.             return 0;
  111.  
  112.         case WM_DESTROY:
  113.             delete de;
  114.             delete game;
  115.             KillTimer(hwnd, TIMER_ID);
  116.             PostQuitMessage(0);
  117.             return 0;
  118.  
  119.     }
  120.  
  121.     return DefWindowProc(hwnd, message, wParam, lParam);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment