/** ** Supermodel ** A Sega Model 3 Arcade Emulator. ** Copyright 2011-2016 Bart Trzynadlowski, Nik Henson ** ** This file is part of Supermodel. ** ** Supermodel 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. ** ** Supermodel 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 Supermodel. If not, see . **/ /* * Main.cpp * * Main program driver for the SDL port. * * To Do Before Next Release * ------------------------- * - Thoroughly test config system (do overrides work as expected? XInput * force settings?) * - Make sure fragment and vertex shaders are configurable for 3D (and 2D?) * - Remove all occurrences of "using namespace std" from Nik's code. * - Standardize variable naming (recently introduced vars_like_this should be * converted back to varsLikeThis). * - Update save state file revision (strings > 1024 chars are now supported). * - Fix BlockFile.cpp to use fstream! * - Check to make sure save states use explicitly-sized types for 32/64-bit * compatibility (i.e., size_t, int, etc. not allowed). * - Make sure quitting while paused works. * - Add UI keys for balance setting? * - 5.1 audio support? * - Stretch video option * * Compile-Time Options * -------------------- * - SUPERMODEL_WIN32: Define this if compiling on Windows. * - SUPERMODEL_OSX: Define this if compiling on Mac OS X. * - SUPERMODEL_DEBUGGER: Enable the debugger. * - DEBUG: Debug mode (use with caution, produces large logs of game behavior) */ #include #include #include #include #include #include #include #include #include "Pkgs/glew.h" #ifdef SUPERMODEL_OSX #include #else #include #endif #include "Supermodel.h" #include "Util/Format.h" #include "Util/NewConfig.h" #include "Util/ConfigBuilders.h" #include "GameLoader.h" #include "SDLInputSystem.h" #ifdef SUPERMODEL_WIN32 #include "DirectInputSystem.h" #include "WinOutputs.h" #endif #include // Log file names #define DEBUG_LOG_FILE "debug.log" #define ERROR_LOG_FILE "error.log" /****************************************************************************** Global Run-time Config ******************************************************************************/ static Util::Config::Node s_runtime_config("Global"); /****************************************************************************** Display Management ******************************************************************************/ /* * Position and size of rectangular region within OpenGL display to render to. * Unlike the config tree, these end up containing the actual resolution (and * computed offsets within the viewport) that will be rendered based on what * was obtained from SDL. */ static unsigned xOffset, yOffset; // offset of renderer output within OpenGL viewport static unsigned xRes, yRes; // renderer output resolution (can be smaller than GL viewport) static unsigned totalXRes, totalYRes; // total resolution (the whole GL viewport) static bool SetGLGeometry(unsigned *xOffsetPtr, unsigned *yOffsetPtr, unsigned *xResPtr, unsigned *yResPtr, unsigned *totalXResPtr, unsigned *totalYResPtr, bool keepAspectRatio) { // What resolution did we actually get? const SDL_VideoInfo *VideoInfo = SDL_GetVideoInfo(); *totalXResPtr = VideoInfo->current_w; *totalYResPtr = VideoInfo->current_h; // If required, fix the aspect ratio of the resolution that the user passed to match Model 3 ratio float xRes = float(*xResPtr); float yRes = float(*yResPtr); if (keepAspectRatio) { float model3Ratio = 496.0f/384.0f; if (yRes < (xRes/model3Ratio)) xRes = yRes*model3Ratio; if (xRes < (yRes*model3Ratio)) yRes = xRes/model3Ratio; } // Center the visible area *xOffsetPtr = (*xResPtr - (unsigned) xRes)/2; *yOffsetPtr = (*yResPtr - (unsigned) yRes)/2; // If the desired resolution is smaller than what we got, re-center again if (int(*xResPtr) < VideoInfo->current_w) *xOffsetPtr += (VideoInfo->current_w - *xResPtr)/2; if (int(*yResPtr) < VideoInfo->current_h) *yOffsetPtr += (VideoInfo->current_h - *yResPtr)/2; // OpenGL initialization glViewport(0,0,*xResPtr,*yResPtr); glClearColor(0.0,0.0,0.0,0.0); glClearDepth(1.0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glDisable(GL_CULL_FACE); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.0,(GLfloat)xRes/(GLfloat)yRes,0.1,1e5); glMatrixMode(GL_MODELVIEW); // Clear both buffers to ensure a black border for (int i = 0; i < 2; i++) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); SDL_GL_SwapBuffers(); } // Write back resolution parameters *xResPtr = (unsigned) xRes; *yResPtr = (unsigned) yRes; UINT32 correction = (UINT32)(((yRes / 384.f) * 2) + 0.5f); glEnable(GL_SCISSOR_TEST); // Scissor box (to clip visible area) if (s_runtime_config["WideScreen"].ValueAsDefault(false)) { glScissor(0, correction, *totalXResPtr, *totalYResPtr - (correction * 2)); } else { glScissor(*xOffsetPtr + correction, *yOffsetPtr + correction, *xResPtr - (correction * 2), *yResPtr - (correction * 2)); } return OKAY; } /* * CreateGLScreen(): * * Creates an OpenGL display surface of the requested size. xOffset and yOffset * are used to return a display surface offset (for OpenGL viewport commands) * because the actual drawing area may need to be adjusted to preserve the * Model 3 aspect ratio. The new resolution will be passed back as well -- both * the adjusted viewable area resolution and the total resolution. * * NOTE: keepAspectRatio should always be true. It has not yet been tested with * the wide screen hack. */ static bool CreateGLScreen(const char *caption, unsigned *xOffsetPtr, unsigned *yOffsetPtr, unsigned *xResPtr, unsigned *yResPtr, unsigned *totalXResPtr, unsigned *totalYResPtr, bool keepAspectRatio, bool fullScreen) { GLenum err; // Initialize video subsystem if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) return ErrorLog("Unable to initialize SDL video subsystem: %s\n", SDL_GetError()); // Important GL attributes SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); // Set vsync SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, s_runtime_config["VSync"].ValueAsDefault(false) ? 1 : 0); // Set video mode if (SDL_SetVideoMode(*xResPtr,*yResPtr,0,SDL_OPENGL|(fullScreen?SDL_FULLSCREEN|SDL_HWSURFACE:0)) == NULL) { ErrorLog("Unable to create an OpenGL display: %s\n", SDL_GetError()); return FAIL; } // Create window caption SDL_WM_SetCaption(caption,NULL); // Initialize GLEW, allowing us to use features beyond OpenGL 1.2 err = glewInit(); if (GLEW_OK != err) { ErrorLog("OpenGL initialization failed: %s\n", glewGetErrorString(err)); return FAIL; } return SetGLGeometry(xOffsetPtr, yOffsetPtr, xResPtr, yResPtr, totalXResPtr, totalYResPtr, keepAspectRatio); } static bool ResizeGLScreen(unsigned *xOffsetPtr, unsigned *yOffsetPtr, unsigned *xResPtr, unsigned *yResPtr, unsigned *totalXResPtr, unsigned *totalYResPtr, bool keepAspectRatio, bool fullScreen) { // Set video mode if (SDL_SetVideoMode(*xResPtr,*yResPtr,0,SDL_OPENGL|(fullScreen?SDL_FULLSCREEN|SDL_HWSURFACE:0)) == NULL) { ErrorLog("Unable to create an OpenGL display: %s\n", SDL_GetError()); return FAIL; } return SetGLGeometry(xOffsetPtr, yOffsetPtr, xResPtr, yResPtr, totalXResPtr, totalYResPtr, keepAspectRatio); } /* * PrintGLInfo(): * * Queries and prints OpenGL information. A full list of extensions can * optionally be printed. */ static void PrintGLInfo(bool createScreen, bool infoLog, bool printExtensions) { unsigned xOffset, yOffset, xRes=496, yRes=384, totalXRes, totalYRes; if (createScreen) { if (OKAY != CreateGLScreen("Supermodel - Querying OpenGL Information...", &xOffset, &yOffset, &xRes, &yRes, &totalXRes, &totalYRes, false, false)) { ErrorLog("Unable to query OpenGL.\n"); return; } } GLint value; if (infoLog) InfoLog("OpenGL information:"); else puts("OpenGL information:\n"); const GLubyte *str = glGetString(GL_VENDOR); if (infoLog) InfoLog(" Vendor : %s", str); else printf(" Vendor : %s\n", str); str = glGetString(GL_RENDERER); if (infoLog) InfoLog(" Renderer : %s", str); else printf(" Renderer : %s\n", str); str = glGetString(GL_VERSION); if (infoLog) InfoLog(" Version : %s", str); else printf(" Version : %s\n", str); str = glGetString(GL_SHADING_LANGUAGE_VERSION); if (infoLog) InfoLog(" Shading Language Version : %s", str); else printf(" Shading Language Version : %s\n", str); glGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &value); if (infoLog) InfoLog(" Maximum Vertex Array Size: %d vertices", value); else printf(" Maximum Vertex Array Size: %d vertices\n", value); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); if (infoLog) InfoLog(" Maximum Texture Size : %d texels", value); else printf(" Maximum Texture Size : %d texels\n", value); glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &value); if (infoLog) InfoLog(" Maximum Vertex Attributes: %d", value); else printf(" Maximum Vertex Attributes: %d\n", value); glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &value); if (infoLog) InfoLog(" Maximum Vertex Uniforms : %d", value); else printf(" Maximum Vertex Uniforms : %d\n", value); glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &value); if (infoLog) InfoLog(" Maximum Texture Img Units: %d", value); else printf(" Maximum Texture Img Units: %d\n", value); if (printExtensions) { str = glGetString(GL_EXTENSIONS); char *strLocal = (char *) malloc((strlen((char *) str)+1)*sizeof(char)); if (NULL == strLocal) { if (infoLog) InfoLog(" Supported Extensions : %s", str); else printf(" Supported Extensions : %s\n", str); } else { strcpy(strLocal, (char *) str); if (infoLog) InfoLog(" Supported Extensions : %s", (strLocal = strtok(strLocal, " \t\n"))); else printf(" Supported Extensions : %s\n", (strLocal = strtok(strLocal, " \t\n"))); while ((strLocal = strtok(NULL, " \t\n")) != NULL) { if (infoLog) InfoLog(" %s", strLocal); else printf(" %s\n", strLocal); } } } if (infoLog) InfoLog(""); else printf("\n"); } #ifdef DEBUG static void PrintBAT(unsigned regu, unsigned regl) { uint32_t batu = ppc_read_spr(regu); uint32_t batl = ppc_read_spr(regl); uint32_t bepi = batu >> (31 - 14); uint32_t bl = (batu >> (31 - 29)) & 0x7ff; bool vs = batu & 2; bool vp = batu & 1; uint32_t brpn = batl >> (31 - 14); uint32_t wimg = (batl >> (31 - 28)) & 0xf; uint32_t pp = batl & 3; uint32_t size = (bl + 1) * 128 * 1024; uint32_t ea_base = bepi << (31 - 14); uint32_t ea_limit = ea_base + size - 1; uint32_t pa_base = brpn << (31 - 14); uint32_t pa_limit = pa_base + size - 1; printf("%08X-%08X -> %08X-%08X ", ea_base, ea_limit, pa_base, pa_limit); printf("%c%c%c%c ", (wimg&8)?'W':'-', (wimg&4)?'I':'-', (wimg&2)?'M':'-', (wimg&1)?'G':'-'); printf("PP="); if (pp == 0) printf("NA"); else if (pp == 2) printf("RW"); else printf("RO"); printf(" Vs=%d Vp=%d", vs, vp); } #endif #ifdef DEBUG static void DumpPPCRegisters(IBus *bus) { for (int i = 0; i < 32; i += 4) { printf("R%d=%08X\tR%d=%08X\tR%d=%08X\tR%d=%08X\n", i + 0, ppc_get_gpr(i + 0), i + 1, ppc_get_gpr(i + 1), i + 2, ppc_get_gpr(i + 2), i + 3, ppc_get_gpr(i + 3)); } printf("PC =%08X\n", ppc_get_pc()); printf("LR =%08X\n", ppc_get_lr()); printf("DBAT0U=%08X\tIBAT0U=%08X\n", ppc_read_spr(SPR603E_DBAT0U), ppc_read_spr(SPR603E_IBAT0U)); printf("DBAT0L=%08X\tIBAT0L=%08X\n", ppc_read_spr(SPR603E_DBAT0L), ppc_read_spr(SPR603E_IBAT0L)); printf("DBAT1U=%08X\tIBAT1U=%08X\n", ppc_read_spr(SPR603E_DBAT1U), ppc_read_spr(SPR603E_IBAT1U)); printf("DBAT1L=%08X\tIBAT1L=%08X\n", ppc_read_spr(SPR603E_DBAT1L), ppc_read_spr(SPR603E_IBAT1L)); printf("DBAT2U=%08X\tIBAT2U=%08X\n", ppc_read_spr(SPR603E_DBAT2U), ppc_read_spr(SPR603E_IBAT2U)); printf("DBAT2L=%08X\tIBAT2L=%08X\n", ppc_read_spr(SPR603E_DBAT2L), ppc_read_spr(SPR603E_IBAT2L)); printf("DBAT3U=%08X\tIBAT3U=%08X\n", ppc_read_spr(SPR603E_DBAT3U), ppc_read_spr(SPR603E_IBAT3U)); printf("DBAT3L=%08X\tIBAT3L=%08X\n", ppc_read_spr(SPR603E_DBAT3L), ppc_read_spr(SPR603E_IBAT3L)); for (int i = 0; i < 10; i++) printf("SR%d =%08X VSID=%06X\n", i, ppc_read_sr(i), ppc_read_sr(i) & 0x00ffffff); for (int i = 10; i < 16; i++) printf("SR%d=%08X VSID=%06X\n", i, ppc_read_sr(i), ppc_read_sr(i) & 0x00ffffff); printf("SDR1=%08X\n", ppc_read_spr(SPR603E_SDR1)); printf("\n"); printf("DBAT0: "); PrintBAT(SPR603E_DBAT0U, SPR603E_DBAT0L); printf("\n"); printf("DBAT1: "); PrintBAT(SPR603E_DBAT1U, SPR603E_DBAT1L); printf("\n"); printf("DBAT2: "); PrintBAT(SPR603E_DBAT2U, SPR603E_DBAT2L); printf("\n"); printf("DBAT3: "); PrintBAT(SPR603E_DBAT3U, SPR603E_DBAT3L); printf("\n"); printf("IBAT0: "); PrintBAT(SPR603E_IBAT0U, SPR603E_IBAT0L); printf("\n"); printf("IBAT1: "); PrintBAT(SPR603E_IBAT1U, SPR603E_IBAT1L); printf("\n"); printf("IBAT2: "); PrintBAT(SPR603E_IBAT2U, SPR603E_IBAT2L); printf("\n"); printf("IBAT3: "); PrintBAT(SPR603E_IBAT3U, SPR603E_IBAT3L); printf("\n"); printf("\n"); /* printf("First PTEG:\n"); uint32_t ptab = ppc_read_spr(SPR603E_SDR1) & 0xffff0000; for (int i = 0; i < 65536/8; i++) { uint64_t pte = bus->Read64(ptab + i*8); uint32_t vsid = (pte >> (32 + (31 - 24))) & 0x00ffffff; uint32_t rpn = pte & 0xfffff000; int wimg = (pte >> 3) & 0xf; bool v = pte & 0x8000000000000000ULL; printf(" %d: %016llX V=%d VSID=%06X RPN=%08X WIMG=%c%c%c%c\n", i, pte, v, vsid, rpn, (wimg&8)?'W':'-', (wimg&4)?'I':'-', (wimg&2)?'M':'-', (wimg&1)?'G':'-'); } */ } #endif /****************************************************************************** Render State Analysis ******************************************************************************/ #ifdef DEBUG #include "Model3/Model3GraphicsState.h" #include "Util/BMPFile.h" #include "OSD/SDL/PolyAnalysis.h" static void SaveFrameBuffer(const std::string &file) { std::shared_ptr pixels(new uint8_t[totalXRes*totalYRes*4], std::default_delete()); glReadPixels(0, 0, totalXRes, totalYRes, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); Util::WriteSurfaceToBMP(file, pixels.get(), totalXRes, totalYRes, true); } bool g_forceFlushModels = false; int g_testPolyHeaderIdx = -1; uint32_t g_testPolyHeaderMask = 0; static std::string s_gfxStatePath; static std::string GetFileBaseName(const std::string &file) { std::string base = file; size_t pos = file.find_last_of('/'); if (pos != std::string::npos) base = file.substr(pos + 1); pos = file.find_last_of('\\'); if (pos != std::string::npos) base = file.substr(pos + 1); return base; } static void TestPolygonHeaderBits(IEmulator *Emu) { const static std::vector unknownPolyBits { 0x000003b0, 0x000000ab, // actual color 0x000000fc, 0x000000c0, 0x000000a0, 0xffffff60, 0xff0300ff // contour, luminous, etc. }; GLint readBuffer; glGetIntegerv(GL_READ_BUFFER, &readBuffer); glReadBuffer(GL_FRONT); // Render separate image for each unknown bit g_forceFlushModels = true; for (int idx = 0; idx < 7; idx++) { for (int bit = 0; bit < 32; bit++) { uint32_t mask = 1 << bit; g_testPolyHeaderIdx = idx; g_testPolyHeaderMask = mask; if ((unknownPolyBits[idx] & mask)) { Emu->RenderFrame(); std::string file = Util::Format() << "Analysis/" << GetFileBaseName(s_gfxStatePath) << "." << idx << "_" << Util::Hex(mask) << ".bmp"; SaveFrameBuffer(file); } } } glReadBuffer(readBuffer); // Generate the HTML GUI std::string file = Util::Format() << "Analysis/_" << GetFileBaseName(s_gfxStatePath) << ".html"; std::ofstream fs(file); if (!fs.good()) ErrorLog("Unable to open '%s' for writing.", file.c_str()); else { std::string contents = s_polyAnalysisHTMLPrologue; contents += " var g_file_base_name = '" + GetFileBaseName(s_gfxStatePath) + "';\n"; contents += " var g_unknown_bits = [" + std::string(Util::Format(",").Join(unknownPolyBits)) + "];\n"; contents += s_polyAnalysisHTMLEpilogue; fs << contents; printf("Produced: %s\n", file.c_str()); } } #endif /****************************************************************************** Save States and NVRAM Save states and NVRAM use the same basic format. When anything changes that breaks compatibility with previous versions of Supermodel, the save state and NVRAM version numbers must be incremented as needed. Header block name: "Supermodel Save State" or "Supermodel NVRAM State" Data: Save state file version (4-byte integer), ROM set ID (up to 9 bytes, including terminating \0). Different subsystems output their own blocks. ******************************************************************************/ static const int STATE_FILE_VERSION = 2; // save state file version static const int NVRAM_FILE_VERSION = 0; // NVRAM file version static unsigned s_saveSlot = 0; // save state slot # static void SaveState(IEmulator *Model3) { CBlockFile SaveState; std::string file_path = Util::Format() << "Saves/" << Model3->GetGame().name << ".st" << s_saveSlot; if (OKAY != SaveState.Create(file_path, "Supermodel Save State", "Supermodel Version " SUPERMODEL_VERSION)) { ErrorLog("Unable to save state to '%s'.", file_path.c_str()); return; } // Write file format version and ROM set ID to header block int32_t fileVersion = STATE_FILE_VERSION; SaveState.Write(&fileVersion, sizeof(fileVersion)); SaveState.Write(Model3->GetGame().name); // Save state Model3->SaveState(&SaveState); SaveState.Close(); printf("Saved state to '%s'.\n", file_path.c_str()); DebugLog("Saved state to '%s'.\n", file_path.c_str()); } static void LoadState(IEmulator *Model3) { CBlockFile SaveState; // Generate file path std::string file_path = Util::Format() << "Saves/" << Model3->GetGame().name << ".st" << s_saveSlot; // Open and check to make sure format is correct if (OKAY != SaveState.Load(file_path)) { ErrorLog("Unable to load state from '%s'.", file_path.c_str()); return; } if (OKAY != SaveState.FindBlock("Supermodel Save State")) { ErrorLog("'%s' does not appear to be a valid save state file.", file_path.c_str()); return; } int32_t fileVersion; SaveState.Read(&fileVersion, sizeof(fileVersion)); if (fileVersion != STATE_FILE_VERSION) { ErrorLog("'%s' is incompatible with this version of Supermodel.", file_path.c_str()); return; } // Load Model3->LoadState(&SaveState); SaveState.Close(); printf("Loaded state from '%s'.\n", file_path.c_str()); DebugLog("Loaded state from '%s'.\n", file_path.c_str()); } static void SaveNVRAM(IEmulator *Model3) { CBlockFile NVRAM; std::string file_path = Util::Format() << "NVRAM/" << Model3->GetGame().name << ".nv"; if (OKAY != NVRAM.Create(file_path, "Supermodel NVRAM State", "Supermodel Version " SUPERMODEL_VERSION)) { ErrorLog("Unable to save NVRAM to '%s'. Make sure directory exists!", file_path.c_str()); return; } // Write file format version and ROM set ID to header block int32_t fileVersion = NVRAM_FILE_VERSION; NVRAM.Write(&fileVersion, sizeof(fileVersion)); NVRAM.Write(Model3->GetGame().name); // Save NVRAM Model3->SaveNVRAM(&NVRAM); NVRAM.Close(); DebugLog("Saved NVRAM to '%s'.\n", file_path.c_str()); } static void LoadNVRAM(IEmulator *Model3) { CBlockFile NVRAM; // Generate file path std::string file_path = Util::Format() << "NVRAM/" << Model3->GetGame().name << ".nv"; // Open and check to make sure format is correct if (OKAY != NVRAM.Load(file_path)) { //ErrorLog("Unable to restore NVRAM from '%s'.", filePath); return; } if (OKAY != NVRAM.FindBlock("Supermodel NVRAM State")) { ErrorLog("'%s' does not appear to be a valid NVRAM file.", file_path.c_str()); return; } int32_t fileVersion; NVRAM.Read(&fileVersion, sizeof(fileVersion)); if (fileVersion != NVRAM_FILE_VERSION) { ErrorLog("'%s' is incompatible with this version of Supermodel.", file_path.c_str()); return; } // Load Model3->LoadNVRAM(&NVRAM); NVRAM.Close(); DebugLog("Loaded NVRAM from '%s'.\n", file_path.c_str()); } /****************************************************************************** UI Rendering Currently, only does crosshairs for light gun games. ******************************************************************************/ static void GunToViewCoords(float *x, float *y) { *x = (*x-150.0f)/(651.0f-150.0f); // Scale [150,651] -> [0.0,1.0] *y = (*y-80.0f)/(465.0f-80.0f); // Scale [80,465] -> [0.0,1.0] } static void DrawCrosshair(float x, float y, float r, float g, float b) { float base = 0.01f, height = 0.02f; // geometric parameters of each triangle float dist = 0.004f; // distance of triangle tip from center float a = (float)xRes/(float)yRes; // aspect ratio (to square the crosshair) glColor3f(r, g, b); glVertex2f(x, y+dist); // bottom triangle glVertex2f(x+base/2.0f, y+(dist+height)*a); glVertex2f(x-base/2.0f, y+(dist+height)*a); glVertex2f(x, y-dist); // top triangle glVertex2f(x-base/2.0f, y-(dist+height)*a); glVertex2f(x+base/2.0f, y-(dist+height)*a); glVertex2f(x-dist, y); // left triangle glVertex2f(x-dist-height, y+(base/2.0f)*a); glVertex2f(x-dist-height, y-(base/2.0f)*a); glVertex2f(x+dist, y); // right triangle glVertex2f(x+dist+height, y-(base/2.0f)*a); glVertex2f(x+dist+height, y+(base/2.0f)*a); } /* static void PrintGLError(GLenum error) { switch (error) { case GL_INVALID_ENUM: printf("invalid enum\n"); break; case GL_INVALID_VALUE: printf("invalid value\n"); break; case GL_INVALID_OPERATION: printf("invalid operation\n"); break; case GL_STACK_OVERFLOW: printf("stack overflow\n"); break; case GL_STACK_UNDERFLOW: printf("stack underflow\n"); break; case GL_OUT_OF_MEMORY: printf("out of memory\n"); break; case GL_TABLE_TOO_LARGE: printf("table too large\n"); break; case GL_NO_ERROR: break; default: printf("unknown error\n"); break; } } */ static void UpdateCrosshairs(CInputs *Inputs, unsigned crosshairs) { float x[2], y[2]; crosshairs &= 3; if (!crosshairs) return; // Set up the viewport and orthogonal projection glUseProgram(0); // no shaders glViewport(xOffset, yOffset, xRes, yRes); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 1.0, 1.0, 0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_TEXTURE_2D); // no texture mapping glDisable(GL_BLEND); // no blending glDisable(GL_DEPTH_TEST); // no Z-buffering needed glDisable(GL_LIGHTING); // Convert gun coordinates to viewspace coordinates x[0] = (float) Inputs->gunX[0]->value; y[0] = (float) Inputs->gunY[0]->value; x[1] = (float) Inputs->gunX[1]->value; y[1] = (float) Inputs->gunY[1]->value; GunToViewCoords(&x[0], &y[0]); GunToViewCoords(&x[1], &y[1]); // Draw visible crosshairs glBegin(GL_TRIANGLES); if ((crosshairs & 1) && !Inputs->trigger[0]->offscreenValue) // Player 1 DrawCrosshair(x[0], y[0], 1.0f, 0.0f, 0.0f); if ((crosshairs & 2) && !Inputs->trigger[1]->offscreenValue) // Player 2 DrawCrosshair(x[1], y[1], 0.0f, 1.0f, 0.0f); glEnd(); //PrintGLError(glGetError()); } /****************************************************************************** Video Callbacks ******************************************************************************/ static CInputs *videoInputs = NULL; bool BeginFrameVideo() { return true; } void EndFrameVideo() { // Show crosshairs for light gun games if (videoInputs) UpdateCrosshairs(videoInputs, s_runtime_config["Crosshairs"].ValueAs()); // Swap the buffers SDL_GL_SwapBuffers(); } static void SuperSleep(UINT32 time) { UINT32 start = SDL_GetTicks(); UINT32 tics = start; while (start + time > tics) { tics = SDL_GetTicks(); } } /****************************************************************************** Main Program Loop ******************************************************************************/ #ifdef SUPERMODEL_DEBUGGER int Supermodel(const Game &game, ROMSet *rom_set, IEmulator *Model3, CInputs *Inputs, COutputs *Outputs, Debugger::CDebugger *Debugger) { CLogger *oldLogger = 0; #else int Supermodel(const Game &game, ROMSet *rom_set, IEmulator *Model3, CInputs *Inputs, COutputs *Outputs) { #endif // SUPERMODEL_DEBUGGER unsigned prevFPSTicks; unsigned fpsFramesElapsed; bool gameHasLightguns = false; bool quit = false; bool paused = false; bool dumpTimings = false; // Initialize and load ROMs if (OKAY != Model3->Init()) return 1; if (Model3->LoadGame(game, *rom_set)) return 1; *rom_set = ROMSet(); // free up this memory we won't need anymore // Load NVRAM LoadNVRAM(Model3); // Start up SDL and open a GL window char baseTitleStr[128]; char titleStr[128]; totalXRes = xRes = s_runtime_config["XResolution"].ValueAs(); totalYRes = yRes = s_runtime_config["YResolution"].ValueAs(); sprintf(baseTitleStr, "Supermodel - %s", game.title.c_str()); if (OKAY != CreateGLScreen(baseTitleStr, &xOffset, &yOffset ,&xRes, &yRes, &totalXRes, &totalYRes, true, s_runtime_config["FullScreen"].ValueAs())) return 1; // Info log GL information PrintGLInfo(false, true, false); // Initialize audio system if (OKAY != OpenAudio()) return 1; // Hide mouse if fullscreen, enable crosshairs for gun games Inputs->GetInputSystem()->SetMouseVisibility(!s_runtime_config["FullScreen"].ValueAs()); gameHasLightguns = !!(game.inputs & (Game::INPUT_GUN1|Game::INPUT_GUN2)); if (gameHasLightguns) videoInputs = Inputs; else videoInputs = NULL; // Attach the inputs to the emulator Model3->AttachInputs(Inputs); // Attach the outputs to the emulator if (Outputs != NULL) Model3->AttachOutputs(Outputs); // Initialize the renderers CRender2D *Render2D = new CRender2D(); IRender3D *Render3D = s_runtime_config["New3DEngine"].ValueAs() ? ((IRender3D *) new New3D::CNew3D()) : ((IRender3D *) new Legacy3D::CLegacy3D(s_runtime_config)); if (OKAY != Render2D->Init(xOffset, yOffset, xRes, yRes, totalXRes, totalYRes)) goto QuitError; if (OKAY != Render3D->Init(xOffset, yOffset, xRes, yRes, totalXRes, totalYRes)) goto QuitError; Model3->AttachRenderers(Render2D,Render3D); // Reset emulator Model3->Reset(); #ifdef SUPERMODEL_DEBUGGER // If debugger was supplied, set it as logger and attach it to system oldLogger = GetLogger(); if (Debugger != NULL) { SetLogger(Debugger); Debugger->Attach(); } #endif // SUPERMODEL_DEBUGGER // Emulate! fpsFramesElapsed = 0; prevFPSTicks = SDL_GetTicks(); quit = false; paused = false; dumpTimings = false; #ifdef DEBUG if (dynamic_cast(Model3)) { TestPolygonHeaderBits(Model3); quit = true; } #endif while (!quit) { auto startTime = SDL_GetTicks(); // Render if paused, otherwise run a frame if (paused) Model3->RenderFrame(); else Model3->RunFrame(); // Poll the inputs if (!Inputs->Poll(&game, xOffset, yOffset, xRes, yRes)) quit = true; #ifdef SUPERMODEL_DEBUGGER bool processUI = true; if (Debugger != NULL) { Debugger->Poll(); // Check if debugger requests exit or pause if (Debugger->CheckExit()) { quit = true; processUI = false; } else if (Debugger->CheckPause()) { paused = true; processUI = false; } } if (processUI) { #endif // SUPERMODEL_DEBUGGER // Check UI controls if (Inputs->uiExit->Pressed()) { // Quit emulator quit = true; } else if (Inputs->uiReset->Pressed()) { if (!paused) { Model3->PauseThreads(); SetAudioEnabled(false); } // Reset emulator Model3->Reset(); #ifdef SUPERMODEL_DEBUGGER // If debugger was supplied, reset it too if (Debugger != NULL) Debugger->Reset(); #endif // SUPERMODEL_DEBUGGER if (!paused) { Model3->ResumeThreads(); SetAudioEnabled(true); } puts("Model 3 reset."); } else if (Inputs->uiPause->Pressed()) { // Toggle emulator paused flag paused = !paused; if (paused) { Model3->PauseThreads(); SetAudioEnabled(false); sprintf(titleStr, "%s (Paused)", baseTitleStr); SDL_WM_SetCaption(titleStr,NULL); } else { Model3->ResumeThreads(); SetAudioEnabled(true); SDL_WM_SetCaption(baseTitleStr,NULL); } // Send paused value as output if (Outputs != NULL) Outputs->SetValue(OutputPause, paused); } else if (Inputs->uiFullScreen->Pressed()) { // Toggle emulator fullscreen s_runtime_config.Get("FullScreen").SetValue(!s_runtime_config["FullScreen"].ValueAs()); // Delete renderers and recreate them afterwards since GL context will most likely be lost when switching from/to fullscreen delete Render2D; delete Render3D; Render2D = NULL; Render3D = NULL; // Resize screen totalXRes = xRes = s_runtime_config["XResolution"].ValueAs(); totalYRes = yRes = s_runtime_config["YResolution"].ValueAs(); if (OKAY != ResizeGLScreen(&xOffset,&yOffset,&xRes,&yRes,&totalXRes,&totalYRes,true,s_runtime_config["FullScreen"].ValueAs())) goto QuitError; // Recreate renderers and attach to the emulator Render2D = new CRender2D(); Render3D = s_runtime_config["New3DEngine"].ValueAs() ? ((IRender3D *) new New3D::CNew3D()) : ((IRender3D *) new Legacy3D::CLegacy3D(s_runtime_config)); if (OKAY != Render2D->Init(xOffset, yOffset, xRes, yRes, totalXRes, totalYRes)) goto QuitError; if (OKAY != Render3D->Init(xOffset, yOffset, xRes, yRes, totalXRes, totalYRes)) goto QuitError; Model3->AttachRenderers(Render2D,Render3D); Inputs->GetInputSystem()->SetMouseVisibility(!s_runtime_config["FullScreen"].ValueAs()); } else if (Inputs->uiSaveState->Pressed()) { if (!paused) { Model3->PauseThreads(); SetAudioEnabled(false); } // Save game state SaveState(Model3); if (!paused) { Model3->ResumeThreads(); SetAudioEnabled(true); } } else if (Inputs->uiChangeSlot->Pressed()) { // Change save slot ++s_saveSlot; s_saveSlot %= 10; // clamp to [0,9] printf("Save slot: %d\n", s_saveSlot); } else if (Inputs->uiLoadState->Pressed()) { if (!paused) { Model3->PauseThreads(); SetAudioEnabled(false); } // Load game state LoadState(Model3); #ifdef SUPERMODEL_DEBUGGER // If debugger was supplied, reset it after loading state if (Debugger != NULL) Debugger->Reset(); #endif // SUPERMODEL_DEBUGGER if (!paused) { Model3->ResumeThreads(); SetAudioEnabled(true); } } else if (Inputs->uiMusicVolUp->Pressed()) { // Increase music volume by 10% if (!Model3->GetGame().mpeg_board.empty()) { int vol = (std::min)(200, s_runtime_config["MusicVolume"].ValueAs() + 10); s_runtime_config.Get("MusicVolume").SetValue(vol); printf("Music volume: %d%%", vol); if (200 == vol) puts(" (maximum)"); else printf("\n"); } else puts("This game does not have an MPEG music board."); } else if (Inputs->uiMusicVolDown->Pressed()) { // Decrease music volume by 10% if (!Model3->GetGame().mpeg_board.empty()) { int vol = (std::max)(0, s_runtime_config["MusicVolume"].ValueAs() - 10); s_runtime_config.Get("MusicVolume").SetValue(vol); printf("Music volume: %d%%", vol); if (0 == vol) puts(" (muted)"); else printf("\n"); } else puts("This game does not have an MPEG music board."); } else if (Inputs->uiSoundVolUp->Pressed()) { // Increase sound volume by 10% int vol = (std::min)(200, s_runtime_config["SoundVolume"].ValueAs() + 10); s_runtime_config.Get("SoundVolume").SetValue(vol); printf("Sound volume: %d%%", vol); if (200 == vol) puts(" (maximum)"); else printf("\n"); } else if (Inputs->uiSoundVolDown->Pressed()) { // Decrease sound volume by 10% int vol = (std::max)(0, s_runtime_config["SoundVolume"].ValueAs() - 10); s_runtime_config.Get("SoundVolume").SetValue(vol); printf("Sound volume: %d%%", vol); if (0 == vol) puts(" (muted)"); else printf("\n"); } else if (Inputs->uiDumpInpState->Pressed()) { // Dump input states Inputs->DumpState(&game); } else if (Inputs->uiDumpTimings->Pressed()) { dumpTimings = !dumpTimings; } else if (Inputs->uiSelectCrosshairs->Pressed() && gameHasLightguns) { int crosshairs = (s_runtime_config["Crosshairs"].ValueAs() + 1) & 3; s_runtime_config.Get("Crosshairs").SetValue(crosshairs); switch (crosshairs) { case 0: puts("Crosshairs disabled."); break; case 3: puts("Crosshairs enabled."); break; case 1: puts("Showing Player 1 crosshair only."); break; case 2: puts("Showing Player 2 crosshair only."); break; } } else if (Inputs->uiClearNVRAM->Pressed()) { // Clear NVRAM Model3->ClearNVRAM(); puts("NVRAM cleared."); } else if (Inputs->uiToggleFrLimit->Pressed()) { // Toggle frame limiting s_runtime_config.Get("Throttle").SetValue(!s_runtime_config["Throttle"].ValueAs()); printf("Frame limiting: %s\n", s_runtime_config["Throttle"].ValueAs() ? "On" : "Off"); } #ifdef SUPERMODEL_DEBUGGER else if (Debugger != NULL && Inputs->uiEnterDebugger->Pressed()) { // Break execution and enter debugger Debugger->ForceBreak(true); } } #endif // SUPERMODEL_DEBUGGER // Frame rate and limiting unsigned currentFPSTicks = SDL_GetTicks(); unsigned currentTicks = currentFPSTicks; if (s_runtime_config["ShowFrameRate"].ValueAs()) { ++fpsFramesElapsed; if((currentFPSTicks-prevFPSTicks) >= 1000) // update FPS every 1 second (each tick is 1 ms) { sprintf(titleStr, "%s - %1.1f FPS%s", baseTitleStr, (float)fpsFramesElapsed/((float)(currentFPSTicks-prevFPSTicks)/1000.0f), paused ? " (Paused)" : ""); SDL_WM_SetCaption(titleStr,NULL); prevFPSTicks = currentFPSTicks; // reset tick count fpsFramesElapsed = 0; // reset frame count } } if (paused || s_runtime_config["Throttle"].ValueAs()) { UINT32 endTime = SDL_GetTicks(); UINT32 diff = endTime - startTime; UINT32 frameTime = (UINT32)(1000 / 60.f); // 60 fps, we could roll with 57.5? that would be a jerk fest on 60hz screens though if (diff < frameTime) { SuperSleep(frameTime - diff); } } if (dumpTimings && !paused) { CModel3 *M = dynamic_cast(Model3); if (M) M->DumpTimings(); } } // Make sure all threads are paused before shutting down Model3->PauseThreads(); #ifdef SUPERMODEL_DEBUGGER // If debugger was supplied, detach it from system and restore old logger if (Debugger != NULL) { Debugger->Detach(); SetLogger(oldLogger); } #endif // SUPERMODEL_DEBUGGER // Save NVRAM SaveNVRAM(Model3); // Close audio CloseAudio(); // Shut down renderers delete Render2D; delete Render3D; return 0; // Quit with an error QuitError: delete Render2D; delete Render3D; return 1; } /****************************************************************************** Entry Point and Command Line Procesing ******************************************************************************/ static const char s_configFilePath[] = { "Config/Supermodel.ini" }; static const char s_gameXMLFilePath[] = { "Config/Games.xml" }; // Create and configure inputs static bool ConfigureInputs(CInputs *Inputs, Util::Config::Node *config, bool configure) { static const char configFileComment[] = { ";\n" "; Supermodel Configuration File\n" ";\n" }; Inputs->LoadFromConfig(*config); // If the user wants to configure the inputs, do that now if (configure) { // Open an SDL window unsigned xOffset, yOffset, xRes=496, yRes=384; if (OKAY != CreateGLScreen("Supermodel - Configuring Inputs...", &xOffset, &yOffset, &xRes, &yRes, &totalXRes, &totalYRes, false, false)) return (bool) ErrorLog("Unable to start SDL to configure inputs.\n"); // Configure the inputs if (Inputs->ConfigureInputs(NULL, xOffset, yOffset, xRes, yRes)) { // Write input configuration and input system settings to config file Inputs->StoreToConfig(config); Util::Config::WriteINIFile(s_configFilePath, *config, configFileComment); } else puts("Configuration aborted..."); puts(""); } return OKAY; } // Print game list static void PrintGameList(const std::string &xml_file, const std::map &games) { if (games.empty()) { puts("No games defined."); return; } printf("Games defined in %s:\n", xml_file.c_str()); puts(""); puts(" ROM Set Title"); puts(" ------- -----"); for (auto &v: games) { const Game &game = v.second; printf(" %s", game.name.c_str()); for (int i = game.name.length(); i < 9; i++) // pad for alignment (no game ID should be more than 9 letters) printf(" "); if (!game.version.empty()) printf(" %s (%s)\n", game.title.c_str(), game.version.c_str()); else printf(" %s\n", game.title.c_str()); } } static void LogConfig(const Util::Config::Node &config) { InfoLog("Runtime configuration:"); for (auto &child: config) { if (child.Empty()) InfoLog(" %s=", child.Key().c_str()); else InfoLog(" %s=%s", child.Key().c_str(), child.ValueAs().c_str()); } InfoLog(""); } static Util::Config::Node DefaultConfig() { Util::Config::Node config("Global"); config.Set("GameXMLFile", s_gameXMLFilePath); // CModel3 config.Set("MultiThreaded", true); config.Set("GPUMultiThreaded", true); config.Set("PowerPCFrequency", "50"); // CLegacy3D config.Set("MultiTexture", false); config.Set("VertexShader", ""); config.Set("FragmentShader", ""); // CSoundBoard config.Set("EmulateSound", true); config.Set("Balance", false); // CDSB config.Set("EmulateDSB", true); config.Set("SoundVolume", "100"); config.Set("MusicVolume", "100"); // CDriveBoard #ifdef SUPERMODEL_WIN32 config.Set("ForceFeedback", false); #endif // Platform-specific/UI config.Set("New3DEngine", false); config.Set("XResolution", "496"); config.Set("YResolution", "384"); config.Set("FullScreen", false); config.Set("WideScreen", false); config.Set("VSync", true); config.Set("Throttle", true); config.Set("ShowFrameRate", false); config.Set("Crosshairs", int(0)); config.Set("FlipStereo", false); #ifdef SUPERMODEL_WIN32 config.Set("InputSystem", "dinput"); // DirectInput ForceFeedback config.Set("DirectInputConstForceLeftMax", "100"); config.Set("DirectInputConstForceRightMax", "100"); config.Set("DirectInputSelfCenterMax", "100"); config.Set("DirectInputFrictionMax", "100"); config.Set("DirectInputVibrateMax", "100"); // XInput ForceFeedback config.Set("XInputConstForceThreshold", "30"); config.Set("XInputConstForceMax", "100"); config.Set("XInputVibrateMax", "100"); #else config.Set("InputSystem", "sdl"); #endif config.Set("Outputs", "none"); return config; } static void Title(void) { puts("Supermodel: A Sega Model 3 Arcade Emulator (Version " SUPERMODEL_VERSION ")"); puts("Copyright 2011-2016 by Bart Trzynadlowski and Nik Henson\n"); } static void Help(void) { Util::Config::Node defaultConfig = DefaultConfig(); puts("Usage: Supermodel [options]"); puts("ROM set must be a valid ZIP file containing a single game."); puts(""); puts("General Options:"); puts(" -?, -h, -help, --help Print this help text"); puts(" -print-games List supported games and quit"); puts(""); puts("Core Options:"); printf(" -ppc-frequency= PowerPC frequency in MHz [Default: %d]\n", defaultConfig["PowerPCFrequency"].ValueAs()); puts(" -no-threads Disable multi-threading entirely"); puts(" -gpu-multi-threaded Run graphics rendering in separate thread [Default]"); puts(" -no-gpu-thread Run graphics rendering in main thread"); puts(""); puts("Video Options:"); puts(" -res=, Resolution [Default: 496,384]"); puts(" -window Windowed mode [Default]"); puts(" -fullscreen Full screen mode"); puts(" -wide-screen Expand 3D field of view to screen width"); puts(" -multi-texture Use 8 texture maps for decoding"); puts(" -no-multi-texture Decode to a single texture map [Default]"); puts(" -no-throttle Disable 60 Hz frame rate lock"); puts(" -vsync Lock to vertical refresh rate [Default]"); puts(" -no-vsync Do not lock to vertical refresh rate"); puts(" -show-fps Display frame rate in window title bar"); puts(" -crosshairs= Crosshairs configuration for gun games:"); puts(" 0=none [Default], 1=P1 only, 2=P2 only, 3=P1 & P2"); puts(" -legacy3d Legacy 3D engine [Default]"); puts(" -new3d New 3D engine by Ian Curtis"); puts(" -vert-shader= Load vertex shader from file (legacy 3D engine)"); puts(" -frag-shader= Load fragment shader from file (legacy 3D engine)"); puts(" -print-gl-info Print OpenGL driver information and quit"); puts(""); puts("Audio Options:"); puts(" -sound-volume= Volume of SCSP-generated sound in %, applies only"); puts(" when Digital Sound Board is present [Default: 100]"); puts(" -music-volume= Digital Sound Board volume in % [Default: 100]"); puts(" -balance= Relative front/rear balance in % [Default: 0]"); puts(" -flip-stereo Swap left and right audio channels"); puts(" -no-sound Disable sound board emulation (sound effects)"); puts(" -no-dsb Disable Digital Sound Board (MPEG music)"); puts(""); puts("Input Options:"); #ifdef SUPERMODEL_WIN32 puts(" -force-feedback Enable force feedback (DirectInput, XInput)"); #endif puts(" -config-inputs Configure keyboards, mice, and game controllers"); #ifdef SUPERMODEL_WIN32 printf(" -input-system= Input system [Default: %s]\n", defaultConfig["InputSystem"].ValueAs().c_str()); printf(" -outputs= Outputs [Default: %s]\n", defaultConfig["Outputs"].ValueAs().c_str()); #endif puts(" -print-inputs Prints current input configuration"); puts(""); #ifdef SUPERMODEL_DEBUGGER puts("Debug Options:"); puts(" -disable-debugger Completely disable debugger functionality"); puts(" -enter-debugger Enter debugger at start of emulation"); puts(""); #endif // SUPERMODEL_DEBUGGER } struct ParsedCommandLine { Util::Config::Node config = Util::Config::Node("CommandLine"); std::vector rom_files; bool show_help = false; bool print_games = false; bool print_gl_info = false; bool config_inputs = false; bool print_inputs = false; bool disable_debugger = false; bool enter_debugger = false; #ifdef DEBUG std::string gfx_state; #endif }; static ParsedCommandLine ParseCommandLine(int argc, char **argv) { ParsedCommandLine cmd_line; const std::map valued_options { // -option=value { "-game-xml-file", "GameXMLFile" }, { "-ppc-frequency", "PowerPCFrequency" }, { "-crosshairs", "Crosshairs" }, { "-vert-shader", "VertexShader" }, { "-frag-shader", "FragmentShader" }, { "-sound-volume", "SoundVolume" }, { "-music-volume", "MusicVolume" }, { "-balance", "Balance" }, { "-input-system", "InputSystem" }, { "-outputs", "Outputs" } }; const std::map> bool_options { // -option { "-threads", { "MultiThreaded", true } }, { "-no-threads", { "MultiThreaded", false } }, { "-gpu-multi-threaded", { "GPUMultiThreaded", true } }, { "-no-gpu-thread", { "GPUMultiThreaded", false } }, { "-window", { "FullScreen", false } }, { "-fullscreen", { "FullScreen", true } }, { "-no-wide-screen", { "WideScreen", false } }, { "-wide-screen", { "WideScreen", true } }, { "-no-multi-texture", { "MultiTexture", false } }, { "-multi-texture", { "MultiTexture", true } }, { "-throttle", { "Throttle", true } }, { "-no-throttle", { "Throttle", false } }, { "-vsync", { "VSync", true } }, { "-no-vsync", { "VSync", false } }, { "-show-fps", { "ShowFrameRate", true } }, { "-no-fps", { "ShowFrameRate", false } }, { "-new3d", { "New3DEngine", true } }, { "-legacy3d", { "New3DEngine", false } }, { "-no-flip-stereo", { "FlipStereo", false } }, { "-flip-stereo", { "FlipStereo", true } }, { "-sound", { "EmulateSound", true } }, { "-no-sound", { "EmulateSound", false } }, { "-dsb", { "EmulateDSB", true } }, { "-no-dsb", { "EmulateDSB", false } }, #ifdef SUPERMODEL_WIN32 { "-no-force-feedback", { "ForceFeedback", false } }, { "-force-feedback", { "ForceFeedback", true } }, #endif }; for (int i = 1; i < argc; i++) { std::string arg(argv[i]); if (arg[0] == '-') { // First, check maps size_t idx_equals = arg.find_first_of('='); if (idx_equals != std::string::npos) { std::string option(arg.begin(), arg.begin() + idx_equals); std::string value(arg.begin() + idx_equals + 1, arg.end()); if (value.length() == 0) { ErrorLog("Argument to '%s' cannot be blank.", option.c_str()); continue; } auto it = valued_options.find(option); if (it != valued_options.end()) { const std::string &config_key = it->second; cmd_line.config.Set(config_key, value); continue; } } else { auto it = bool_options.find(arg); if (it != bool_options.end()) { const std::string &config_key = it->second.first; bool value = it->second.second; cmd_line.config.Set(config_key, value); continue; } else if (valued_options.find(arg) != valued_options.end()) { ErrorLog("'%s' requires an argument.", argv[i]); continue; } } // Fell through -- handle special cases if (arg == "-?" || arg == "-h" || arg == "-help" || arg == "--help") cmd_line.show_help = true; else if (arg == "-print-games") cmd_line.print_games = true; else if (arg == "-res" || arg.find("-res=") == 0) { std::vector parts = Util::Format(arg).Split('='); if (parts.size() != 2) ErrorLog("'-res' requires both a width and height (e.g., '-res=496,384')."); else { unsigned x, y; if (2 == sscanf(&argv[i][4],"=%d,%d", &x, &y)) { std::string xres = Util::Format() << x; std::string yres = Util::Format() << y; cmd_line.config.Set("XResolution", xres); cmd_line.config.Set("YResolution", yres); } else ErrorLog("'-res' requires both a width and height (e.g., '-res=496,384')."); } } else if (arg == "-print-gl-info") cmd_line.print_gl_info = true; else if (arg == "-config-inputs") cmd_line.config_inputs = true; else if (arg == "-print-inputs") cmd_line.print_inputs = true; #ifdef SUPERMODEL_DEBUGGER else if (arg == "-disable-debugger") cmd_line.disable_debugger = true; else if (arg == "-enter-debugger") cmd_line.enter_debugger = true; #endif #ifdef DEBUG else if (arg == "-gfx-state" || arg.find("-gfx-state=") == 0) { std::vector parts = Util::Format(arg).Split('='); if (parts.size() != 2) ErrorLog("'-gfx-state' requires a file name."); else cmd_line.gfx_state = parts[1]; } #endif else ErrorLog("Ignoring unrecognized option: %s", argv[i]); } else cmd_line.rom_files.emplace_back(arg); } return cmd_line; } /* * main(argc, argv): * * Program entry point. */ int main(int argc, char **argv) { #ifdef SUPERMODEL_DEBUGGER bool cmdEnterDebugger = false; #endif // SUPERMODEL_DEBUGGER Title(); if (argc <= 1) { Help(); return 0; } // Create default logger CFileLogger Logger(DEBUG_LOG_FILE, ERROR_LOG_FILE); Logger.ClearLogs(); SetLogger(&Logger); InfoLog("Started as:"); for (int i = 0; i < argc; i++) InfoLog(" argv[%d] = %s", i, argv[i]); InfoLog(""); // Load config and parse command line auto cmd_line = ParseCommandLine(argc, argv); if (cmd_line.print_gl_info) { PrintGLInfo(true, false, false); return 0; } #ifdef DEBUG s_gfxStatePath.assign(cmd_line.gfx_state); #endif bool print_games = cmd_line.print_games; bool rom_specified = !cmd_line.rom_files.empty(); if (!rom_specified && !print_games && !cmd_line.config_inputs) { ErrorLog("No ROM file specified."); return 0; } // Load game and resolve run-time config Game game; ROMSet rom_set; Util::Config::Node fileConfig("Global"); Util::Config::Node fileConfigWithDefaults("Global"); { Util::Config::Node config3("Global"); Util::Config::Node config4("Global"); Util::Config::FromINIFile(&fileConfig, s_configFilePath); Util::Config::MergeINISections(&fileConfigWithDefaults, DefaultConfig(), fileConfig); // apply .ini file's global section over defaults Util::Config::MergeINISections(&config3, fileConfigWithDefaults, cmd_line.config); // apply command line overrides if (rom_specified || print_games) { std::string xml_file = config3["GameXMLFile"].ValueAs(); GameLoader loader(xml_file); if (print_games) { PrintGameList(xml_file, loader.GetGames()); return 0; } if (loader.Load(&game, &rom_set, *cmd_line.rom_files.begin())) return 1; Util::Config::MergeINISections(&config4, config3, fileConfig[game.name]); // apply game-specific config } else config4 = config3; Util::Config::MergeINISections(&s_runtime_config, config4, cmd_line.config); // apply command line overrides once more } LogConfig(s_runtime_config); // Initialize SDL (individual subsystems get initialized later) if (SDL_Init(0) != 0) { ErrorLog("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } // Create Model 3 emulator #ifdef DEBUG IEmulator *Model3 = s_gfxStatePath.empty() ? static_cast(new CModel3(s_runtime_config)) : static_cast(new CModel3GraphicsState(s_runtime_config, s_gfxStatePath)); #else IEmulator *Model3 = new CModel3(s_runtime_config); #endif // Create input system (default is SDL) and debugger CInputSystem *InputSystem = NULL; CInputs *Inputs = NULL; COutputs *Outputs = NULL; int exitCode = 0; #ifdef SUPERMODEL_DEBUGGER Debugger::CSupermodelDebugger *Debugger = NULL; #endif // SUPERMODEL_DEBUGGER // Create input system // NOTE: fileConfigWithDefaults is passed so that the global section is used // for input settings with default values populated std::string selectedInputSystem = s_runtime_config["InputSystem"].ValueAs(); if (selectedInputSystem == "sdl") InputSystem = new CSDLInputSystem(); #ifdef SUPERMODEL_WIN32 else if (selectedInputSystem == "dinput") InputSystem = new CDirectInputSystem(fileConfigWithDefaults, false, false); else if (selectedInputSystem == "xinput") InputSystem = new CDirectInputSystem(fileConfigWithDefaults, false, true); else if (selectedInputSystem == "rawinput") InputSystem = new CDirectInputSystem(fileConfigWithDefaults, true, false); #endif // SUPERMODEL_WIN32 else { ErrorLog("Unknown input system: %s\n", selectedInputSystem.c_str()); exitCode = 1; goto Exit; } // Create inputs from input system (configuring them if required) Inputs = new CInputs(InputSystem); if (!Inputs->Initialize()) { ErrorLog("Unable to initalize inputs.\n"); exitCode = 1; goto Exit; } // NOTE: fileConfig is passed so that the global section is used for input settings // and because this function may write out a new config file, which must preserve // all sections. We don't want to pollute the output with built-in defaults. if (ConfigureInputs(Inputs, &fileConfig, cmd_line.config_inputs)) { exitCode = 1; goto Exit; } if (cmd_line.print_inputs) { Inputs->PrintInputs(NULL); InputSystem->PrintSettings(); } if (!rom_specified) goto Exit; // Create outputs #ifdef SUPERMODEL_WIN32 { std::string outputs = s_runtime_config["Outputs"].ValueAs(); if (outputs == "none") Outputs = NULL; else if (outputs == "win") Outputs = new CWinOutputs(); else { ErrorLog("Unknown outputs: %s\n", outputs.c_str()); exitCode = 1; goto Exit; } } #endif // SUPERMODEL_WIN32 // Initialize outputs if (Outputs != NULL && !Outputs->Initialize()) { ErrorLog("Unable to initialize outputs.\n"); exitCode = 1; goto Exit; } #ifdef SUPERMODEL_DEBUGGER // Create Supermodel debugger unless debugging is disabled if (!cmd_line.disable_debugger) { Debugger = new Debugger::CSupermodelDebugger(dynamic_cast(Model3), Inputs, &Logger); // If -enter-debugger option was set force debugger to break straightaway if (cmdEnterDebugger) Debugger->ForceBreak(true); } // Fire up Supermodel with debugger exitCode = Supermodel(game, &rom_set, Model3, Inputs, Outputs, Debugger); if (Debugger != NULL) delete Debugger; #else // Fire up Supermodel exitCode = Supermodel(game, &rom_set, Model3, Inputs, Outputs); #endif // SUPERMODEL_DEBUGGER delete Model3; Exit: if (Inputs != NULL) delete Inputs; if (InputSystem != NULL) delete InputSystem; if (Outputs != NULL) delete Outputs; SDL_Quit(); if (exitCode) InfoLog("Program terminated due to an error."); else InfoLog("Program terminated normally."); return exitCode; }