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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
/*
* Nestopia UE
*
* Copyright (C) 2012-2021 R. Danbrook
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <cstdio>
#include <sys/stat.h>
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/gl.h>
#include <SDL.h>
#include "nstcommon.h"
#include "cli.h"
#include "config.h"
#include "audio.h"
#include "video.h"
#include "input.h"
#include "cheats.h"
#include "fltkui.h"
#include "fltkui_archive.h"
#include "fltkui_cheats.h"
#include "fltkui_config.h"
#define MBARHEIGHT 24
static NstWindow *nstwin;
static Fl_Menu_Bar *menubar;
static NstGlArea *glarea;
static NstChtWindow *chtwin;
static NstConfWindow *confwin;
static int fps = 60;
extern int loaded;
Fl_Color NstGreen = 0x255f6500;
Fl_Color NstPurple = 0x5f578700;
Fl_Color NstRed = 0xb51e2c00;
Fl_Color NstBlueGrey = 0x383c4a00;
Fl_Color NstLightGrey = 0xd3dae300;
extern Input::Controllers *cNstPads;
extern nstpaths_t nstpaths;
extern bool (*nst_archive_select)(const char*, char*, size_t);
static int fltkui_refreshrate(void) {
// Get the screen refresh rate using an SDL window
int refresh = 60;
SDL_Window *sdlwin;
sdlwin = SDL_CreateWindow(
"refreshrate",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
1, 1, SDL_WINDOW_HIDDEN
);
SDL_DisplayMode dm;
SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(sdlwin), &dm);
refresh = dm.refresh_rate;
SDL_DestroyWindow(sdlwin);
return refresh;
}
static void fltkui_cheats(Fl_Widget* w, void* userdata) {
if (!loaded) { return; }
chtwin->refresh();
chtwin->show();
}
static void fltkui_config(Fl_Widget* w, void* userdata) {
confwin->show();
}
static void fltkui_rom_open(Fl_Widget* w, void* userdata) {
// Create native chooser
Fl_Native_File_Chooser fc;
fc.title("Select a ROM");
fc.type(Fl_Native_File_Chooser::BROWSE_FILE);
fc.filter("NES Games\t*.{nes,unf,fds,zip,7z,gz,bz2,xz}");
// Show file chooser
switch (fc.show()) {
case -1: fprintf(stderr, "Error: %s\n", fc.errmsg()); break;
case 1: break; // Cancel
default:
if (fc.filename()) {
loaded = nst_load(fc.filename());
nstwin->label(nstpaths.gamename);
if (loaded) {
nst_play();
fps = nst_pal() ? 50 : 60;
}
}
break;
}
}
static void fltkui_movie_load(Fl_Widget* w, void* userdata) {
// Create native chooser
if (!loaded) { return; }
Fl_Native_File_Chooser fc;
fc.title("Select a Movie");
fc.type(Fl_Native_File_Chooser::BROWSE_FILE);
fc.directory((const char*)nstpaths.nstdir);
fc.filter("Nestopia Movies\t*.nsv");
// Show file chooser
switch (fc.show()) {
case -1: fprintf(stderr, "Error: %s\n", fc.errmsg()); break;
case 1: break; // Cancel
default:
if (fc.filename()) {
nst_movie_load(fc.filename());
}
break;
}
}
static void fltkui_movie_save(Fl_Widget* w, void* userdata) {
// Create native chooser
if (!loaded) { return; }
Fl_Native_File_Chooser fc;
fc.title("Save Movie");
fc.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
fc.directory((const char*)nstpaths.nstdir);
fc.filter("Nestopia Moviess\t*.nsv");
fc.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM | Fl_Native_File_Chooser::USE_FILTER_EXT);
// Show file chooser
if (fc.show()) { return; }
nst_movie_save(fc.filename());
}
static void fltkui_movie_stop(Fl_Widget* w, void* userdata) {
nst_movie_stop();
}
static void fltkui_state_load(Fl_Widget* w, void* userdata) {
// Create native chooser
if (!loaded) { return; }
Fl_Native_File_Chooser fc;
fc.title("Load State");
fc.type(Fl_Native_File_Chooser::BROWSE_FILE);
fc.directory((const char*)nstpaths.statepath);
fc.filter("Nestopia States\t*.nst");
// Show file chooser
switch (fc.show()) {
case -1: fprintf(stderr, "Error: %s\n", fc.errmsg()); break;
case 1: break; // Cancel
default:
if (fc.filename()) {
nst_state_load(fc.filename());
}
break;
}
}
static void fltkui_state_save(Fl_Widget* w, void* userdata) {
// Create native chooser
if (!loaded) { return; }
Fl_Native_File_Chooser fc;
fc.title("Save State");
fc.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
fc.directory((const char*)nstpaths.statepath);
fc.filter("Nestopia States\t*.nst");
fc.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM | Fl_Native_File_Chooser::USE_FILTER_EXT);
// Show file chooser
if (fc.show()) { return; }
nst_state_save(fc.filename());
}
static void fltkui_screenshot(Fl_Widget* w, void* userdata) {
// Create native chooser
if (!loaded) { return; }
Fl_Native_File_Chooser fc;
fc.title("Save Screenshot");
fc.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
fc.directory((const char*)nstpaths.nstdir);
fc.filter("PNG Screenshots\t*.png");
fc.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM | Fl_Native_File_Chooser::USE_FILTER_EXT);
// Show file chooser
if (fc.show()) { return; }
video_screenshot(fc.filename());
}
static void fltkui_palette_open(Fl_Widget* w, void* userdata) {
// Create native chooser
Fl_Native_File_Chooser fc;
fc.title("Select a Palette");
fc.type(Fl_Native_File_Chooser::BROWSE_FILE);
fc.filter("NES Palettes\t*.pal");
// Show file chooser
switch (fc.show()) {
case -1: fprintf(stderr, "Error: %s\n", fc.errmsg()); break;
case 1: break; // Cancel
default:
if (fc.filename()) {
nst_palette_load(fc.filename());
nst_palette_save();
conf.video_palette_mode = 2;
video_init();
}
break;
}
}
static void fltkui_state_qload(Fl_Widget* w, void* userdata) {
nst_state_quickload(atoi((const char*)userdata));
}
static void fltkui_state_qsave(Fl_Widget* w, void* userdata) {
nst_state_quicksave(atoi((const char*)userdata));
}
static void fltkui_pause(Fl_Widget* w, void* userdata) {
if (nst_playing()) {
nst_pause();
}
else {
nst_play();
}
}
static void fltkui_reset(Fl_Widget* w, void* userdata) {
nst_reset(atoi((const char*)userdata));
}
void fltkui_resize() {
video_set_dimensions();
dimensions_t rendersize = nst_video_get_dimensions_render();
nstwin->size(rendersize.w, rendersize.h + MBARHEIGHT);
menubar->resize(0, 0, nstwin->w(), MBARHEIGHT);
glarea->resize(0, 24, rendersize.w, rendersize.h);
nst_video_set_dimensions_screen(rendersize);
video_init();
}
void fltkui_fullscreen(Fl_Widget* w, void* userdata) {
if (!nst_playing()) { return; }
conf.video_fullscreen ^= 1;
if (conf.video_fullscreen) {
int x, y, w, h;
Fl::screen_xywh(x, y, w, h);
menubar->hide();
nstwin->fullscreen();
dimensions_t scrdim = {w, h};
nstwin->resize(0, 0, scrdim.w, scrdim.h);
glarea->resize(0, 0, scrdim.w, scrdim.h);
nst_video_set_dimensions_screen(scrdim);
video_init();
}
else {
video_set_dimensions();
dimensions_t rendersize = nst_video_get_dimensions_render();
nstwin->fullscreen_off();
nstwin->size(rendersize.w, rendersize.h + MBARHEIGHT);
menubar->show();
menubar->resize(0, 0, nstwin->w(), MBARHEIGHT);
glarea->resize(0, 24, rendersize.w, rendersize.h);
nst_video_set_dimensions_screen(rendersize);
video_init();
}
}
static void fltkui_fds_flip(Fl_Widget* w, void* userdata) {
nst_fds_flip();
}
static void fltkui_fds_switch(Fl_Widget* w, void* userdata) {
nst_fds_switch();
}
static void fltkui_about_close(Fl_Widget* w, void* userdata) {
Fl_Window *about = (Fl_Window*)userdata;
about->hide();
}
static void fltkui_about(Fl_Widget* w, void* userdata) {
Fl_Window about(460, 440);
Fl_Box iconbox(166, 16, 128, 128);
Fl_Box text0(0, 144, 460, 24, "Nestopia UE");
text0.labelfont(FL_BOLD);
Fl_Box text1(0, 166, 460, 24, "1.52.0");
Fl_Box text2(0, 208, 460, 24, "Cycle-Accurate Nintendo Entertainment System Emulator");
Fl_Box text3(0, 256, 460, 24, "FLTK Frontend\n(c) 2012-2022, R. Danbrook\n(c) 2007-2008, R. Belmont");
text3.labelsize(10);
Fl_Box text4(0, 320, 460, 24, "Nestopia Emulator\n(c) 2020-2022, Rupert Carmichael\n(c) 2012-2020, Nestopia UE Contributors\n(c) 2003-2008, Martin Freij");
text4.labelsize(10);
Fl_Box text5(0, 360, 460, 24, "Icon based on drawing by Trollekop");
text5.labelsize(10);
// Set up the icon
char iconpath[512];
snprintf(iconpath, sizeof(iconpath), "%s/icons/hicolor/128x128/apps/nestopia.png", NST_DATAROOTDIR);
// Load the SVG from local source dir if make install hasn't been done
struct stat svgstat;
if (stat(iconpath, &svgstat) == -1) {
snprintf(iconpath, sizeof(iconpath), "icons/128/nestopia.png");
}
Fl_PNG_Image nsticon(iconpath);
iconbox.image(nsticon);
Fl_Button close(360, 400, 80, 24, "&Close");
close.callback(fltkui_about_close, (void*)&about);
about.set_modal();
about.show();
while (about.shown()) { Fl::wait(); }
}
static void quit_cb(Fl_Widget* w, void* userdata) {
nstwin->hide();
}
// this is used to stop Esc from exiting the program:
int handle(int e) {
return (e == FL_SHORTCUT); // eat all keystrokes
}
int NstWindow::handle(int e) {
switch (e) { case FL_KEYDOWN: case FL_KEYUP: fltkui_input_process_key(e); break; }
return Fl_Double_Window::handle(e);
}
int NstGlArea::handle(int e) {
if (nst_input_zapper_present()) {
switch (e) {
case FL_ENTER:
cursor(conf.misc_disable_cursor_special ? FL_CURSOR_NONE : FL_CURSOR_CROSS);
break;
case FL_LEAVE:
cursor(FL_CURSOR_DEFAULT);
break;
case FL_PUSH:
nst_input_inject_mouse(cNstPads, Fl::event_button(), 1, Fl::event_x(), Fl::event_y());
break;
case FL_RELEASE:
nst_input_inject_mouse(cNstPads, Fl::event_button(), 0, Fl::event_x(), Fl::event_y());
break;
}
}
else if (e == FL_ENTER && conf.misc_disable_cursor) { cursor(FL_CURSOR_NONE); }
else if (e == FL_LEAVE) { cursor(FL_CURSOR_DEFAULT); }
return Fl_Gl_Window::handle(e);
}
static Fl_Menu_Item menutable[] = {
{"&File", 0, 0, 0, FL_SUBMENU},
{"&Open", FL_ALT + 'o', fltkui_rom_open, 0, FL_MENU_DIVIDER},
{"Load State...", 0, fltkui_state_load, 0, 0},
{"Save State...", 0, fltkui_state_save, 0, FL_MENU_DIVIDER},
{"Quick Load", 0, 0, 0, FL_SUBMENU},
{"Slot 0", 0, fltkui_state_qload, (void*)"0", 0},
{"Slot 1", 0, fltkui_state_qload, (void*)"1", 0},
{"Slot 2", 0, fltkui_state_qload, (void*)"2", 0},
{"Slot 3", 0, fltkui_state_qload, (void*)"3", 0},
{"Slot 4", 0, fltkui_state_qload, (void*)"4", 0},
{0},
{"Quick Save", 0, 0, 0, FL_SUBMENU|FL_MENU_DIVIDER},
{"Slot 0", 0, fltkui_state_qsave, (void*)"0", 0},
{"Slot 1", 0, fltkui_state_qsave, (void*)"1", 0},
{"Slot 2", 0, fltkui_state_qsave, (void*)"2", 0},
{"Slot 3", 0, fltkui_state_qsave, (void*)"3", 0},
{"Slot 4", 0, fltkui_state_qsave, (void*)"4", 0},
{0},
{"Open Palette...", 0, fltkui_palette_open, 0, FL_MENU_DIVIDER},
{"Screenshot...", 0, fltkui_screenshot, 0, FL_MENU_DIVIDER},
{"Load Movie...", 0, fltkui_movie_load, 0, 0},
{"Record Movie...", 0, fltkui_movie_save, 0, 0},
{"Stop Movie", 0, fltkui_movie_stop, 0, FL_MENU_DIVIDER},
{"&Quit", FL_ALT + 'q', quit_cb},
{0}, // End File
{"&Emulator", 0, 0, 0, FL_SUBMENU},
{"Pause/Play", 0, fltkui_pause, 0, FL_MENU_DIVIDER},
{"Reset (Soft)", 0, fltkui_reset, (void*)"0", 0},
{"Reset (Hard)", 0, fltkui_reset, (void*)"1", FL_MENU_DIVIDER},
{"Fullscreen", 0, fltkui_fullscreen, 0, FL_MENU_DIVIDER},
{"Flip Disk", 0, fltkui_fds_flip, 0, 0},
{"Switch Disk", 0, fltkui_fds_switch, 0, FL_MENU_DIVIDER},
{"Cheats...", 0, fltkui_cheats, 0, FL_MENU_DIVIDER},
{"Configuration...", 0, fltkui_config, 0},
{0}, // End Emulator
{"&Help", 0, 0, 0, FL_SUBMENU},
{"About", 0, fltkui_about, 0, 0},
{0}, // End Help
{0} // End Menu
};
void makenstwin(const char *name) {
video_set_dimensions();
dimensions_t rendersize = nst_video_get_dimensions_render();
Fl::add_handler(handle);
// Cheats Window
chtwin = new NstChtWindow(660, 500, "Cheat Manager");
chtwin->populate();
// Configuration Window
confwin = new NstConfWindow(400, 400, "Configuration");
confwin->populate();
// Main Window
nstwin = new NstWindow(rendersize.w, rendersize.h + MBARHEIGHT, name);
nstwin->color(FL_BLACK);
nstwin->xclass("nestopia");
// Menu Bar
menubar = new Fl_Menu_Bar(0, 0, nstwin->w(), MBARHEIGHT);
menubar->box(FL_FLAT_BOX);
menubar->menu(menutable);
glarea = new NstGlArea(0, MBARHEIGHT, nstwin->w(), nstwin->h() - MBARHEIGHT);
glarea->color(FL_BLACK);
nstwin->end();
}
int main(int argc, char *argv[]) {
// Set up directories
nst_set_dirs();
// Set default config options
config_set_default();
// Read the config file and override defaults
config_file_read(nstpaths.nstconfdir);
// Handle command line arguments
cli_handle_command(argc, argv);
// Set the video dimensions
video_set_dimensions();
// Set up callbacks
nst_set_callbacks();
// Initialize SDL Audio and Joystick
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
int refreshrate = fltkui_refreshrate();
// Set archive handler function pointer
nst_archive_select = &fltkui_archive_select;
// Detect and set up Joysticks
nstsdl_input_joysticks_detect();
nstsdl_input_conf_defaults();
nstsdl_input_conf_read();
// Initialize and load FDS BIOS and NstDatabase.xml
nst_fds_bios_load();
nst_db_load();
makenstwin(argv[0]);
nstwin->label("Nestopia UE");
nstwin->show();
menubar->show();
glarea->make_current();
glarea->show();
Fl::check();
// Load a rom from the command line
if (argc > 1 && argv[argc - 1][0] != '-') {
int loaded = nst_load(argv[argc - 1]);
if (loaded) { nst_play(); }
else { exit(1); }
nstwin->label(nstpaths.gamename);
}
else if (conf.video_fullscreen) {
conf.video_fullscreen = 0;
}
if (conf.video_fullscreen) {
conf.video_fullscreen = 0;
fltkui_fullscreen(NULL, NULL);
}
video_init();
int frames = 0;
int framefrags = 0;
fps = nst_pal() ? 50 : 60;
while (true) {
Fl::check();
if (!nstwin->shown()) {
break;
}
else if (!nstwin->shown() && (confwin->shown() || chtwin->shown())) {
break;
}
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_JOYHATMOTION:
case SDL_JOYAXISMOTION:
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
nstsdl_input_process(cNstPads, event);
break;
default: break;
}
}
frames = (fps / refreshrate);
framefrags += fps % refreshrate;
if (framefrags >= refreshrate) {
frames++;
framefrags -= refreshrate;
}
for (int i = 0; i < frames; i++) { nst_emuloop(); }
glarea->redraw();
}
// Remove the cartridge and shut down the NES
nst_unload();
// Unload the FDS BIOS, NstDatabase.xml, and the custom palette
nst_db_unload();
nst_fds_bios_unload();
nst_palette_unload();
// Deinitialize audio
audio_deinit();
// Deinitialize joysticks
nstsdl_input_joysticks_close();
// Write the input config file
nstsdl_input_conf_write();
// Write the config file
config_file_write(nstpaths.nstconfdir);
return 0;
}
|