155 lines
3.4 KiB
C++
155 lines
3.4 KiB
C++
|
#include "stdafx.h"
|
||
|
#include "resource.h"
|
||
|
#include "MainWindow.h"
|
||
|
#include "..\Core\CPU.h"
|
||
|
#include "..\Core\Timer.h"
|
||
|
using namespace DirectX;
|
||
|
|
||
|
namespace NES
|
||
|
{
|
||
|
bool MainWindow::Initialize()
|
||
|
{
|
||
|
if(FAILED(InitWindow())) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!_renderer.Initialize(_hInstance, _hWnd)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
int MainWindow::Run()
|
||
|
{
|
||
|
Initialize();
|
||
|
|
||
|
MSG msg = { 0 };
|
||
|
Timer timer;
|
||
|
int frameCount = 0;
|
||
|
while(WM_QUIT != msg.message) {
|
||
|
if(PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
|
||
|
TranslateMessage(&msg);
|
||
|
DispatchMessage(&msg);
|
||
|
} else {
|
||
|
_renderer.Render();
|
||
|
frameCount++;
|
||
|
if(frameCount == 500) {
|
||
|
double fps = (double)frameCount / (timer.GetElapsedMS() / 1000);
|
||
|
OutputDebugString((std::to_wstring((int)fps) + L"\n").c_str());
|
||
|
timer.Reset();
|
||
|
frameCount = 0;
|
||
|
}
|
||
|
}
|
||
|
//std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(50));
|
||
|
}
|
||
|
|
||
|
return (int)msg.wParam;
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------
|
||
|
// Register class and create window
|
||
|
//--------------------------------------------------------------------------------------
|
||
|
HRESULT MainWindow::InitWindow()
|
||
|
{
|
||
|
// Register class
|
||
|
WNDCLASSEX wcex;
|
||
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
||
|
wcex.style = CS_HREDRAW | CS_VREDRAW;
|
||
|
wcex.lpfnWndProc = WndProc;
|
||
|
wcex.cbClsExtra = 0;
|
||
|
wcex.cbWndExtra = 0;
|
||
|
wcex.hInstance = _hInstance;
|
||
|
wcex.hIcon = LoadIcon(_hInstance, (LPCTSTR)IDI_GUI);
|
||
|
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
||
|
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
||
|
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_GUI);
|
||
|
wcex.lpszClassName = L"NESEmu";
|
||
|
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
|
||
|
if(!RegisterClassEx(&wcex))
|
||
|
return E_FAIL;
|
||
|
|
||
|
// Create window
|
||
|
RECT rc = { 0, 0, 320, 240 };
|
||
|
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
|
||
|
_hWnd = CreateWindow(L"NESEmu", L"NESEmu",
|
||
|
WS_OVERLAPPEDWINDOW,
|
||
|
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, _hInstance,
|
||
|
nullptr);
|
||
|
if(!_hWnd) {
|
||
|
return E_FAIL;
|
||
|
}
|
||
|
|
||
|
ShowWindow(_hWnd, _nCmdShow);
|
||
|
|
||
|
return S_OK;
|
||
|
}
|
||
|
|
||
|
INT_PTR CALLBACK MainWindow::About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
||
|
{
|
||
|
UNREFERENCED_PARAMETER(lParam);
|
||
|
switch (message)
|
||
|
{
|
||
|
case WM_INITDIALOG:
|
||
|
return (INT_PTR)TRUE;
|
||
|
|
||
|
case WM_COMMAND:
|
||
|
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
|
||
|
{
|
||
|
EndDialog(hDlg, LOWORD(wParam));
|
||
|
return (INT_PTR)TRUE;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
return (INT_PTR)FALSE;
|
||
|
}
|
||
|
|
||
|
void MainWindow::RunBenchmark()
|
||
|
{
|
||
|
std::thread bmThread(&CPU::RunBenchmark);
|
||
|
bmThread.detach();
|
||
|
}
|
||
|
|
||
|
LRESULT CALLBACK MainWindow::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
||
|
{
|
||
|
PAINTSTRUCT ps;
|
||
|
int wmId, wmEvent;
|
||
|
HDC hdc;
|
||
|
|
||
|
switch(message) {
|
||
|
case WM_COMMAND:
|
||
|
wmId = LOWORD(wParam);
|
||
|
wmEvent = HIWORD(wParam);
|
||
|
// Parse the menu selections:
|
||
|
switch (wmId)
|
||
|
{
|
||
|
case IDM_RunBenchmark:
|
||
|
RunBenchmark();
|
||
|
break;
|
||
|
case IDM_ABOUT:
|
||
|
DialogBox(nullptr, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
|
||
|
break;
|
||
|
case IDM_EXIT:
|
||
|
DestroyWindow(hWnd);
|
||
|
break;
|
||
|
default:
|
||
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
||
|
}
|
||
|
break;
|
||
|
case WM_PAINT:
|
||
|
hdc = BeginPaint(hWnd, &ps);
|
||
|
EndPaint(hWnd, &ps);
|
||
|
break;
|
||
|
|
||
|
case WM_DESTROY:
|
||
|
PostQuitMessage(0);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
}
|