main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "main.hpp"
  2. #include "resource/resource.rcc"
  3. //nall::string <> QString interface: allows string streaming; automatically converts to UTF-16
  4. class utf8 : public nall::string {
  5. public:
  6. template<typename T> utf8& operator<<(T t) { string::operator<<(t); return *this; }
  7. operator const QString() const { return QString::fromUtf8(*this); }
  8. };
  9. #include "platform.cpp"
  10. #include "config.cpp"
  11. #include "interface.cpp"
  12. #include "ui.cpp"
  13. #include "input/input.cpp"
  14. #include "utility/utility.cpp"
  15. const char defaultStylesheet[] =
  16. "QLabel.title {"
  17. " background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 rgba(255, 0, 0, 48), stop: 1 rgba(128, 0, 0, 96));"
  18. " font-weight: bold;"
  19. " margin-bottom: 5px;"
  20. " padding: 3px;"
  21. "}"
  22. "\n"
  23. "#backdrop {"
  24. " background: #000000;"
  25. "}"
  26. "\n"
  27. "#mouse-capture-box {"
  28. " background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, "
  29. " stop: 0.00 rgba(128, 0, 0, 96), stop: 0.25 rgba(255, 0, 0, 48), "
  30. " stop: 0.75 rgba(255, 0, 0, 48), stop: 1.00 rgba(128, 0, 0, 96));"
  31. " color: #000000;"
  32. " font-weight: bold;"
  33. "}"
  34. "\n";
  35. void Application::initPaths(const char *basename) {
  36. char temp[PATH_MAX];
  37. if(realpath(basename, temp)) {
  38. //remove program name
  39. strtr(temp, "\\", "/");
  40. for(signed i = strlen(temp) - 1; i >= 0; i--) {
  41. if(temp[i] == '/') {
  42. temp[i] = 0;
  43. break;
  44. }
  45. }
  46. if(strend(temp, "/") == false) strcat(temp, "/");
  47. snes.config.path.base = temp;
  48. } else {
  49. snes.config.path.base = "";
  50. }
  51. if(userpath(temp)) {
  52. strtr(temp, "\\", "/");
  53. if(strend(temp, "/") == false) strcat(temp, "/");
  54. snes.config.path.user = temp;
  55. } else {
  56. snes.config.path.user = "";
  57. }
  58. char cwd[PATH_MAX];
  59. snes.config.path.current = getcwd(cwd);
  60. }
  61. void Application::locateFile(string &filename, bool createDataDirectory) {
  62. //first, check if file exists in executable directory (single-user mode)
  63. string temp = string() << snes.config.path.base << filename;
  64. if(file::exists(temp) == false) {
  65. //if not, use user data path (multi-user mode)
  66. temp = snes.config.path.user;
  67. temp << ".bsnes";
  68. if(createDataDirectory) mkdir(temp); //ensure directory exists
  69. temp << "/" << filename;
  70. }
  71. filename = temp;
  72. }
  73. int Application::main(int argc, char **argv) {
  74. app = new App(argc, argv);
  75. #if !defined(_WIN32)
  76. //Windows port uses 256x256 icon from resource file
  77. app->setWindowIcon(QIcon(":/bsnes.png"));
  78. #endif
  79. initargs(argc, argv); //ensure argv[]s are in UTF-8 format
  80. initPaths(argv[0]);
  81. locateFile(configFilename = "bsnes.cfg", true);
  82. locateFile(styleSheetFilename = "style.qss", false);
  83. string customStylesheet;
  84. if(customStylesheet.readfile(styleSheetFilename) == true) {
  85. app->setStyleSheet((const char*)customStylesheet);
  86. } else {
  87. app->setStyleSheet(defaultStylesheet);
  88. }
  89. config.load(configFilename);
  90. init();
  91. snes.init();
  92. if(argc == 2) {
  93. //if valid file was specified on the command-line, attempt to load it now
  94. utility.loadCartridge(argv[1]);
  95. }
  96. while(terminate == false) {
  97. processEvents();
  98. utility.updateSystemState();
  99. inputManager.refresh();
  100. if(config.input.focusPolicy == Configuration::Input::FocusPolicyPauseEmulation) {
  101. bool inactive = (winMain->window->isActiveWindow() == false);
  102. if(!autopause && inactive) {
  103. autopause = true;
  104. audio.clear();
  105. } else if(autopause && !inactive) {
  106. autopause = false;
  107. }
  108. } else {
  109. autopause = false;
  110. }
  111. if(cartridge.loaded() && !pause && !autopause) {
  112. snes.runtoframe();
  113. } else {
  114. usleep(20 * 1000);
  115. }
  116. supressScreenSaver();
  117. }
  118. config.save(configFilename);
  119. return 0;
  120. }
  121. void Application::processEvents() {
  122. app->processEvents();
  123. }
  124. Application::Application() {
  125. terminate = false;
  126. power = false;
  127. pause = false;
  128. autopause = false;
  129. }
  130. Application::~Application() {
  131. //deleting (QApplication)app will segfault the application upon exit
  132. //delete app;
  133. }
  134. int main(int argc, char **argv) {
  135. return application.main(argc, argv);
  136. }