forked from mborik/GPMD85Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPMD85emu.cpp
More file actions
183 lines (149 loc) · 5.21 KB
/
GPMD85emu.cpp
File metadata and controls
183 lines (149 loc) · 5.21 KB
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
/* GPMD85emu.cpp: Initialization and main program loop.
Copyright (c) 2011-2012 Martin Borik <mborik@users.sourceforge.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//-----------------------------------------------------------------------------
#include "CommonUtils.h"
#include "GPMD85main.h"
//-----------------------------------------------------------------------------
int main(int argc, char** argv)
{
printf("\n= %s v%s ~ Copyright (c) %s ~ %s =", PACKAGE_NAME, VERSION, PACKAGE_YEAR, PACKAGE_URL);
printf("\n- This program comes with ABSOLUTELY NO WARRANTY. This is free software,");
printf("\n- and you are welcome to redistribute it under certain conditions.\n\n");
PathUserHome = SDL_getenv("HOME");
PathApplication = getcwd(NULL, PATH_MAX);
PathResources = new char[(strlen(DIR_RESOURCES) + 1)];
PathAppConfig = new char[(strlen(PathUserHome) + 16)];
strcpy(PathResources, DIR_RESOURCES);
sprintf(PathAppConfig, "%s%c.%s", PathUserHome, DIR_DELIMITER, PACKAGE_TARNAME);
debug("", "Resource path: %s", PathResources);
debug(NULL, "Application path: %s", PathApplication);
debug(NULL, "Application config path: %s", PathAppConfig);
struct stat filestat;
if (stat(PathAppConfig, &filestat) != 0)
mkdir(PathAppConfig, 0755);
// initialization of SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
error("", "Couldn't initialize SDL: %s\n", SDL_GetError());
const SDL_VideoInfo *vi = SDL_GetVideoInfo();
SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
gvi.w = gvi.h = 0;
gvi.hw = vi->hw_available;
gvi.wm = vi->wm_available;
gvi.depth = vi->vfmt->BitsPerPixel;
if (modes > (SDL_Rect **) 0) {
debug(NULL, "Actual framebuffer resolution: %d x %d",
vi->current_w, vi->current_h);
for (int i = 0; modes[i]; i++) {
if (modes[i]->w == vi->current_w || modes[i]->h == vi->current_h) {
gvi.w = modes[i]->w;
gvi.h = modes[i]->h;
if (gvi.w == vi->current_w && gvi.h == vi->current_h)
break;
}
}
}
if (!gvi.w || !gvi.h) {
gvi.w = vi->current_w;
gvi.h = vi->current_h;
}
if (SDL_VideoModeOK(gvi.w, gvi.h, gvi.depth, SDL_FULLSCREEN) == 0) {
warning("", "Full-screen disabled, no suitable resolution!");
gvi.w = gvi.h = 0;
}
else
debug(NULL, "Full-screen resolution: %d x %d", gvi.w, gvi.h);
SDL_EnableKeyRepeat(0, 0);
if (gvi.wm) {
SDL_WM_SetCaption(PACKAGE_NAME, NULL);
SDL_Surface *icon = SDL_LoadBMP(LocateResource("icon.bmp", false));
if (icon) {
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, SDL_MapRGB(icon->format, 255, 0, 255));
SDL_WM_SetIcon(icon, NULL);
SDL_FreeSurface(icon);
}
else
warning("", "Can't load icon resource file");
}
else
SDL_ShowCursor(0);
debug(NULL, "Initialization process started...");
Emulator = new TEmulator();
Emulator->ProcessSettings(-1);
debug("", "Starting %d FPS refresh timer", GPU_FRAMES_PER_SEC);
Emulator->BaseTimer = SDL_AddTimer(GPU_TIMER_INTERVAL, FormMain_BaseTimerCallback, Emulator);
Emulator->ActionPlayPause(true);
DWORD nextTick;
int i = 0, j, k = 0;
BYTE *kb = NULL;
SDL_Event event;
bool waitForRelease = false;
debug("", "Starting main CPU %dHz loop", CPU_FRAMES_PER_SEC);
while (Emulator->isActive) {
nextTick = SDL_GetTicks() + CPU_TIMER_INTERVAL;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
case SDL_KEYUP:
case SDL_KEYDOWN:
Emulator->keyBuffer = kb = SDL_GetKeyState(&i);
kb[SDLK_NUMLOCK] = kb[SDLK_CAPSLOCK] = kb[SDLK_SCROLLOCK] = 0;
if (event.type == SDL_QUIT)
kb[SDLK_POWER] = 1;
if (waitForRelease) {
for (j = --i; j > 0; j--) {
if (kb[j]) {
k++;
break;
}
}
}
else if ((waitForRelease = Emulator->TestHotkeys()) == true)
k = 4;
break;
case SDL_ACTIVEEVENT:
if (Settings->pauseOnFocusLost &&
(event.active.state & (SDL_APPINPUTFOCUS | SDL_APPACTIVE)))
Emulator->ActionPlayPause(event.active.gain);
break;
case SDL_VIDEOEXPOSE:
Emulator->RefreshDisplay();
break;
default:
break;
}
}
if (k > 0 && i > 0) {
k--;
memset(kb, 0, sizeof(BYTE) * i);
}
else
waitForRelease = false;
Emulator->CpuTimerCallback();
// have a break, have a tick-tock...
while (SDL_GetTicks() < nextTick)
SDL_Delay(1);
}
SDL_FillRect(GUI->defaultSurface, NULL, 0);
Emulator->RefreshDisplay();
debug("", "Main CPU loop terminated");
SDL_RemoveTimer(Emulator->BaseTimer);
SDL_Delay(WEAK_REFRESH_TIME);
delete Emulator;
SDL_Quit();
delete [] PathResources;
delete [] PathAppConfig;
free(PathApplication);
return EXIT_SUCCESS;
}
//-----------------------------------------------------------------------------