Advertisement
lylythechosenone

Memory problems

Feb 23rd, 2021
1,517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. // Game.h
  2. #pragma once
  3. #include <string>
  4.  
  5. #include "GameType.h"
  6. #include "InputType.h"
  7.  
  8. class Game
  9. {
  10. public:
  11.     // end-user functions
  12.     Game(std::wstring name, GameType type);
  13.  
  14.     // internal functions
  15.     Game();
  16.     void ReceiveInput(InputType type, int code, int repeatCount, int scanCode);
  17.  
  18.     // internal variables
  19.     std::wstring name;
  20.     GameType type;
  21. };
  22. // Game.cpp
  23. #include "Game.h"
  24.  
  25. #include <iostream>
  26.  
  27. #include <Windows.h>
  28.  
  29. Game::Game(std::wstring name, GameType type)
  30. {
  31.     this->name = name;
  32.     this->type = type;
  33. }
  34.  
  35. Game::Game() {}
  36.  
  37. void Game::ReceiveInput(InputType type, int code, int repeatCount, int scanCode)
  38. {
  39.     std::cout << (char)MapVirtualKey(code, MAPVK_VK_TO_CHAR) << '\n';
  40. }
  41. // Window.h
  42. #pragma once
  43. #include <map>
  44. #include <string>
  45. #include <windows.h>
  46. #include "FailedToOpenWindowException.h"
  47. #include "Game.h"
  48.  
  49. class Window
  50. {
  51. public:
  52.     // internal functions
  53.     void Constructor(std::wstring text, bool fullscreen);
  54.    
  55.     // end-user functions
  56.     Window(std::wstring text, bool fullscreen);
  57.     HWND GetHandle();
  58.  
  59.     // internal variables
  60.     static std::map<HWND, Window *> windows;
  61.     bool fullscreen;
  62.     HWND handle;
  63.     std::wstring text;
  64.  
  65.     // end-user variables
  66.     Game *game;
  67. };
  68.  
  69. std::map<HWND, Window *> Window::windows = std::map<HWND, Window *>();
  70. // Window.cpp
  71. #include "Window.h"
  72.  
  73. #include <stdexcept>
  74. #include <iostream>
  75. #include <map>
  76. #include <string>
  77.  
  78. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  79. {
  80.     switch (uMsg)
  81.     {
  82.         case WM_PAINT:
  83.             {
  84.                 PAINTSTRUCT ps;
  85.                 HDC hdc = BeginPaint(hWnd, &ps);
  86.  
  87.                 Window *window = Window::windows[hWnd];
  88.  
  89.                 RECT windowRect = ps.rcPaint;
  90.                 if (window->fullscreen)
  91.                 {
  92.                     windowRect = {0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)};
  93.                 }
  94.  
  95.                 FillRect(hdc, &windowRect, (HBRUSH)(COLOR_WINDOW+1));
  96.  
  97.                 EndPaint(hWnd, &ps);
  98.             }
  99.             return 0;
  100.         case WM_KEYDOWN:
  101.             {
  102.                 Window *window = Window::windows[hWnd];
  103.                 try
  104.                 {
  105.                     Game *game = window->game;
  106.                     game->ReceiveInput(InputType::KeyDown, wParam, lParam & 0xFFFF, lParam & 0x0000FF);
  107.                 } catch (std::exception &ignore) {}
  108.             }
  109.     }
  110.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  111. }
  112.  
  113. void Window::Constructor(std::wstring text, bool fullscreen)
  114. {
  115.     this->fullscreen = fullscreen;
  116.    
  117.     WNDCLASS wndClass;
  118.     if (!GetClassInfo(nullptr, L"LEngineWindow", &wndClass))
  119.     {
  120.         wndClass = { };
  121.         wndClass.lpszClassName = L"LEngineWindow";
  122.         wndClass.lpfnWndProc = WindowProc;
  123.  
  124.         RegisterClass(&wndClass);
  125.     }
  126.  
  127.     handle = CreateWindowEx(0,
  128.         L"LEngineWindow",
  129.         text.c_str(),
  130.         fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW | CS_OWNDC,
  131.  
  132.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  133.  
  134.         nullptr,
  135.         nullptr,
  136.         nullptr,
  137.         nullptr);
  138.  
  139.     if (handle == nullptr)
  140.     {
  141.         throw FailedToOpenWindowException();
  142.     }
  143.  
  144.     windows.insert_or_assign(handle, this);
  145.    
  146.     SetCursor(LoadCursor(nullptr, IDC_ARROW));
  147.     ShowWindow(handle, fullscreen ? SW_MAXIMIZE : SW_NORMAL);
  148.  
  149.     MSG msg;
  150.     while (GetMessage(&msg, nullptr, 0, 0))
  151.     {
  152.         TranslateMessage(&msg);
  153.         DispatchMessage(&msg);
  154.     }
  155. }
  156.  
  157. DWORD WINAPI RunConstructor(LPVOID lParam)
  158. {
  159.     Window *window = (Window *)lParam;
  160.     window->Constructor(window->text, window->fullscreen);
  161.     return 0;
  162. }
  163.  
  164. Window::Window(std::wstring text, bool fullscreen)
  165. {
  166.     this->text = text;
  167.     this->fullscreen = fullscreen;
  168.    
  169.     CreateThread(nullptr, 0, RunConstructor, this, 0, nullptr);
  170. }
  171.  
  172. HWND Window::GetHandle()
  173. {
  174.     return handle;
  175. }
  176. // TestGame.cpp
  177. #include <iostream>
  178.  
  179. #include "../Headers/Window.h"
  180.  
  181. int main(int argc, char* argv[])
  182. {
  183.     Window window(L"Some Window", true);
  184.  
  185.     Game game(L"Test Game", GameType::TwoDimensions);
  186.     window.game = &game;
  187.     while (true) {}
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement