forked from nzeemin/neonbtl
/
Main.cpp
194 lines (154 loc) · 5.28 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* This file is part of NEONBTL.
NEONBTL is free software: you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
NEONBTL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with
NEONBTL. If not, see <http://www.gnu.org/licenses/>. */
// NEONBTL.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <commdlg.h>
#include <crtdbg.h>
#include <mmintrin.h>
#include <vfw.h>
#include <commctrl.h>
#include "Main.h"
#include "Emulator.h"
#include "Dialogs.h"
#include "Views.h"
//////////////////////////////////////////////////////////////////////
// Global Variables
HINSTANCE g_hInst = NULL; // current instance
HWND g_hwnd = NULL;
long m_nMainLastFrameTicks = 0;
//////////////////////////////////////////////////////////////////////
// Forward declarations
BOOL InitInstance(HINSTANCE, int);
void DoneInstance();
//////////////////////////////////////////////////////////////////////
int APIENTRY _tWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF);
int n = 0;
_CrtSetBreakAlloc(n);
#endif
g_hInst = hInstance; // Store instance handle in our global variable
LARGE_INTEGER nFrameStartTime;
nFrameStartTime.QuadPart = 0;
// Initialize global strings
LoadString(g_hInst, IDS_APP_TITLE, g_szTitle, MAX_LOADSTRING);
LoadString(g_hInst, IDC_NEONBTL, g_szWindowClass, MAX_LOADSTRING);
MainWindow_RegisterClass();
// Perform application initialization
if (! InitInstance(hInstance, nCmdShow))
return FALSE;
HACCEL hAccelTable = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_NEONBTL));
LARGE_INTEGER nPerformanceFrequency;
::QueryPerformanceFrequency(&nPerformanceFrequency);
// Main message loop
MSG msg;
while (true)
{
::QueryPerformanceCounter(&nFrameStartTime);
if (!g_okEmulatorRunning)
::Sleep(20);
else
{
if (Emulator_IsBreakpoint())
Emulator_Stop();
else
{
if (Emulator_SystemFrame())
{
ScreenView_RedrawScreen();
//MemoryMapView_RedrawMap();
}
}
}
// Process all queue
while (::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
{
if (msg.message == WM_QUIT)
goto endprog;
if (::TranslateAccelerator(g_hwnd, hAccelTable, &msg))
continue;
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
#if 1
if (g_okEmulatorRunning /*&& Settings_GetRealSpeed()*/)
{
// Slow down to 25 frames per second
LARGE_INTEGER nFrameFinishTime; // Frame start time
::QueryPerformanceCounter(&nFrameFinishTime);
LONGLONG nTimeElapsed = (nFrameFinishTime.QuadPart - nFrameStartTime.QuadPart)
* 1000 / nPerformanceFrequency.QuadPart;
if (nTimeElapsed > 0 && nTimeElapsed < 20) // 1000 millisec / 25 = 40 millisec
{
LONG nTimeToSleep = (LONG)(20 - nTimeElapsed);
::Sleep((DWORD) nTimeToSleep / 2);
ScreenView_ScanKeyboard();
::Sleep((DWORD) nTimeToSleep / 2);
}
}
#endif
}
endprog:
DoneInstance();
#ifdef _DEBUG
if (_CrtDumpMemoryLeaks())
::MessageBeep(MB_ICONEXCLAMATION);
#endif
return (int) msg.wParam;
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
INITCOMMONCONTROLSEX ics; ics.dwSize = sizeof(ics);
ics.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&ics);
#if !defined(PRODUCT)
DebugLogClear();
#endif
Settings_Init();
//Joystick_Init();
//Joystick_SelectJoystick(Settings_GetJoystick());
if (!Emulator_Init())
return FALSE;
int conf = Settings_GetConfiguration();
//if (conf == 0) //TODO
if (!Emulator_InitConfiguration((NeonConfiguration)conf))
return FALSE;
Emulator_SetSound(Settings_GetSound());
if (!CreateMainWindow())
return FALSE;
return TRUE;
}
// Instance finalization
void DoneInstance()
{
ScreenView_Done();
Emulator_Done();
//Joystick_Done();
Settings_Done();
}
//////////////////////////////////////////////////////////////////////