Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Game.h
- #pragma once
- #include <string>
- #include "GameType.h"
- #include "InputType.h"
- class Game
- {
- public:
- // end-user functions
- Game(std::wstring name, GameType type);
- // internal functions
- Game();
- void ReceiveInput(InputType type, int code, int repeatCount, int scanCode);
- // internal variables
- std::wstring name;
- GameType type;
- };
- // Game.cpp
- #include "Game.h"
- #include <iostream>
- #include <Windows.h>
- Game::Game(std::wstring name, GameType type)
- {
- this->name = name;
- this->type = type;
- }
- Game::Game() {}
- void Game::ReceiveInput(InputType type, int code, int repeatCount, int scanCode)
- {
- std::cout << (char)MapVirtualKey(code, MAPVK_VK_TO_CHAR) << '\n';
- }
- // Window.h
- #pragma once
- #include <map>
- #include <string>
- #include <windows.h>
- #include "FailedToOpenWindowException.h"
- #include "Game.h"
- class Window
- {
- public:
- // internal functions
- void Constructor(std::wstring text, bool fullscreen);
- // end-user functions
- Window(std::wstring text, bool fullscreen);
- HWND GetHandle();
- // internal variables
- static std::map<HWND, Window *> windows;
- bool fullscreen;
- HWND handle;
- std::wstring text;
- // end-user variables
- Game *game;
- };
- std::map<HWND, Window *> Window::windows = std::map<HWND, Window *>();
- // Window.cpp
- #include "Window.h"
- #include <stdexcept>
- #include <iostream>
- #include <map>
- #include <string>
- LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- Window *window = Window::windows[hWnd];
- RECT windowRect = ps.rcPaint;
- if (window->fullscreen)
- {
- windowRect = {0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)};
- }
- FillRect(hdc, &windowRect, (HBRUSH)(COLOR_WINDOW+1));
- EndPaint(hWnd, &ps);
- }
- return 0;
- case WM_KEYDOWN:
- {
- Window *window = Window::windows[hWnd];
- try
- {
- Game *game = window->game;
- game->ReceiveInput(InputType::KeyDown, wParam, lParam & 0xFFFF, lParam & 0x0000FF);
- } catch (std::exception &ignore) {}
- }
- }
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- void Window::Constructor(std::wstring text, bool fullscreen)
- {
- this->fullscreen = fullscreen;
- WNDCLASS wndClass;
- if (!GetClassInfo(nullptr, L"LEngineWindow", &wndClass))
- {
- wndClass = { };
- wndClass.lpszClassName = L"LEngineWindow";
- wndClass.lpfnWndProc = WindowProc;
- RegisterClass(&wndClass);
- }
- handle = CreateWindowEx(0,
- L"LEngineWindow",
- text.c_str(),
- fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW | CS_OWNDC,
- CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
- nullptr,
- nullptr,
- nullptr,
- nullptr);
- if (handle == nullptr)
- {
- throw FailedToOpenWindowException();
- }
- windows.insert_or_assign(handle, this);
- SetCursor(LoadCursor(nullptr, IDC_ARROW));
- ShowWindow(handle, fullscreen ? SW_MAXIMIZE : SW_NORMAL);
- MSG msg;
- while (GetMessage(&msg, nullptr, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- DWORD WINAPI RunConstructor(LPVOID lParam)
- {
- Window *window = (Window *)lParam;
- window->Constructor(window->text, window->fullscreen);
- return 0;
- }
- Window::Window(std::wstring text, bool fullscreen)
- {
- this->text = text;
- this->fullscreen = fullscreen;
- CreateThread(nullptr, 0, RunConstructor, this, 0, nullptr);
- }
- HWND Window::GetHandle()
- {
- return handle;
- }
- // TestGame.cpp
- #include <iostream>
- #include "../Headers/Window.h"
- int main(int argc, char* argv[])
- {
- Window window(L"Some Window", true);
- Game game(L"Test Game", GameType::TwoDimensions);
- window.game = &game;
- while (true) {}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement