#include "system.h" #include #include #include "SDL.h" namespace es { typedef struct _ColorRGB { Uint8 color[4]; } ColorRGB; static const char *ctrl_array[] = {"Input", "DeviceNumber", "A", "B", "C", "X", "Y", "Z", "R", "EndInput","Up", "Left", "Down", "Right", 0 }; enum { CTRL_INPUT=0, CTRL_DEV, CTRL_A, CTRL_B, CTRL_C, CTRL_X, CTRL_Y, CTRL_Z, CTRL_R, CTRL_END, C_UP, C_LEFT, C_DOWN, C_RIGHT }; int compareCtrlString(char *string) { unsigned int i; for(i = 0; ctrl_array[i] != 0; i++) if(strcmp(ctrl_array[i], string) == 0) return i; return -1; } void mainSystem::unload() { if(quitPlug) quitPlug(); if(obj) { SDL_UnloadObject(obj); obj = 0; } } mainSystem::~mainSystem() { unload(); SDL_ShowCursor(SDL_TRUE); if(player1.stick) SDL_JoystickClose(player1.stick); if(player2.stick) SDL_JoystickClose(player2.stick); if(pfont) SDL_FreeFont(pfont); } mainSystem::mainSystem(std::string exec, int w, int h , Uint32 flags) : mxWnd(w, h, flags) { j_one = 0; setTitle("freeES"); SDL_ShowCursor(SDL_FALSE); surf_rez.createSurface(front.getSurface()->w,front.getSurface()->h); pfont = SDL_InitFont("data/font/system.mxf"); if(!pfont) { throw mx::mxException(" Error could not Load System Font! "); } setupControls(); initJoysticks(); if(exec == "") loadDash(); else loadObject(exec.c_str()); } void mainSystem::initJoysticks() { std::cout << "number of connected joysticks: " << SDL_NumJoysticks() << "\n"; if(SDL_NumJoysticks() == 0) return; std::cout << "device number: " << player1.which << "\n"; player1.stick = SDL_JoystickOpen(player1.which); if(player1.stick) { std::cout << SDL_JoystickName(player1.which) << " detected\n"; if(strstr(SDL_JoystickName(player1.which), "Sony") || SDL_JoystickNumAxes(player1.stick) == 28) { std::cout << "setting control mode to Playstation 3\n"; ps3 = true; } std::cout << "Number of Axes: " << SDL_JoystickNumAxes(player1.stick) << "\n"; } player2.stick = SDL_JoystickOpen(player2.which); if(player2.stick) { std::cout << SDL_JoystickName(player2.which) << " detected\n"; std::cout << "Number of Axes: " << SDL_JoystickNumAxes(player2.stick) << "\n"; } SDL_JoystickEventState(SDL_ENABLE); } // setup system controls from configuration file void mainSystem::setupControls() { player1.which = 0; player2.which = 1; std::fstream ifile; ifile.open("data/control.cfg", std::ios::in | std::ios::binary); if(!ifile.is_open()) { throw mx::mxException(" Error could not access control configuration file, check to see if it exisits, file path should be data/control.cfg "); } ifile.seekg(0, std::ios::end); size_t len; len = ifile.tellg(); ifile.seekg(0, std::ios::beg); char *buf = new char [ len + 1 ]; ifile.read(buf, len); buf[len] = 0; ifile.close(); std::string str = buf; delete [] buf; mascToken token; mascInit(&token); token.source_length = str.length(); int rt; while((rt = mascLex(&token, str.c_str())) != MID_NULL) { if(rt != MID_SKIP) { switch(rt) { case MID_LETTER: { int cX = compareCtrlString(token.token); if(cX != -1) { if(cX == CTRL_INPUT) { mascTokenReset(&token); while ((rt = mascLex(&token, str.c_str()) == MID_SKIP)) {} if(strcmp(token.token, "Keyboard") == 0) { syscontrol.name = token.token; syscontrol.key = true; getKeys(&token, str, syscontrol); } else if(strcmp(token.token, "Controller") == 0) { static int id = 0; if(SDL_NumJoysticks() == 0) { continue; } if(id == 0) { player1.name = "Controller 1"; player1.key = false; getKeys(&token, str, player1); id++; } if(id == 1) { player2.name = "Controller 2"; player2.key = false; getKeys(&token, str, player2); id++; } } else if(strcmp(token.token, "Xarcade") == 0) { arcade_control.name = token.token; arcade_control.arcade = true; getKeys(&token, str, arcade_control); } } } } break; } } mascTokenReset(&token); } } void mainSystem::getKeys(mascToken *token, std::string vstr, systemController &syscontrol) { int rt; while( (rt = mascLex(token, vstr.c_str())) != MID_NULL ) { if(rt != MID_SKIP) { if(rt == MID_LETTER) { int cmp = compareCtrlString(token->token); if(cmp == CTRL_END) break; mascTokenReset(token); while ((rt = mascLex(token, vstr.c_str()) == MID_SKIP)) {} switch(cmp) { case CTRL_A: syscontrol.button[0] = atoi(token->token); break; case CTRL_B: syscontrol.button[1] = atoi(token->token); break; case CTRL_C: syscontrol.button[2] = atoi(token->token); break; case CTRL_X: syscontrol.button[3] = atoi(token->token); break; case CTRL_Y: syscontrol.button[4] = atoi(token->token); break; case CTRL_Z: syscontrol.button[5] = atoi(token->token); break; case CTRL_R: syscontrol.button[6] = atoi(token->token); break; case C_UP: syscontrol.c_up = atoi(token->token); break; case C_LEFT: syscontrol.c_left = atoi(token->token); break; case C_DOWN: syscontrol.c_down = atoi(token->token); break; case C_RIGHT: syscontrol.c_right = atoi(token->token); break; case CTRL_DEV: syscontrol.which = atoi(token->token); break; } } mascTokenReset(token); } } std::cout << "Control Configuration for " << syscontrol.name << " initalized\n"; } void mainSystem::getControl(mascToken *token, std::string vstr) { } void mainSystem::loadDash() { loadObject("plugins/dash/dash.lsd"); // since we loaded the dash run_call = (GetName) SDL_LoadFunction(obj, "dash_run"); } void mainSystem::loadObject(const char *src) { run_call = 0; obj = SDL_LoadObject(src); if(obj != 0) { GetName g,v; g = (GetName)SDL_LoadFunction(obj, "ES_plug_name"); v = (GetName)SDL_LoadFunction(obj, "ES_plug_version"); std::cout << "Loaded plugin: " << g() << " " << v() << "\n"; initPlug = (syscall) SDL_LoadFunction(obj, "ES_init"); quitPlug = (syscall) SDL_LoadFunction(obj, "ES_quit"); initPlug(); rscr = (renderScr) SDL_LoadFunction(obj, "ES_render"); joy_bd = (button_func) SDL_LoadFunction(obj,"ES_button_down"); joy_bu = (button_func) SDL_LoadFunction(obj,"ES_button_up"); m_axis = (axisMove) SDL_LoadFunction(obj, "ES_axis_movement"); } else { throw mx::mxException(" fatal error could not load plugin: " + std::string(SDL_GetError())); } } void mainSystem::eventPassed(SDL_Event &e) { // debug keypress event if((e.type == SDL_KEYDOWN && e.key.keysym.sym == syscontrol.button[BUTTON_R]) || (e.type == SDL_JOYBUTTONDOWN && e.jbutton.button == 9) || (ps3==true && e.type == SDL_JOYAXISMOTION && e.jaxis.axis == 3 && e.jaxis.value == 0)) { if(run_call) quit(); else { p.resetSurface(&front); p.setFont(pfont); SDL_FillRect(front, 0, 0); p.printText(100, 100, mx::Color(255, 255, 255), "freeES Loading..."); #ifdef _WIN32 SDL_UpdateRect(front, 0, 0, front.getSurface()->w, front.getSurface()->h ); #else front.Flip(); #endif unload(); run_call = 0; SDL_Delay(200); loadDash(); return; } } switch(e.type) { case SDL_QUIT: quit(); break; case SDL_JOYHATMOTION: if(e.jhat.value & SDL_HAT_UP) m_axis(e.jhat.which, 5, -1); if(e.jhat.value & SDL_HAT_DOWN) m_axis(e.jhat.which, 5, 1); if(e.jhat.value & SDL_HAT_LEFT) m_axis(e.jhat.which, 4, -1); if(e.jhat.value & SDL_HAT_RIGHT) m_axis(e.jhat.which, 4, 1); break; case SDL_KEYDOWN: if(arcade_control.arcade == true) { if(e.key.keysym.sym == arcade_control.c_left) m_axis(0,4,-1); if(e.key.keysym.sym == arcade_control.c_right) m_axis(0, 4, 1); if(e.key.keysym.sym == arcade_control.c_up) m_axis(0, 5,-1); if(e.key.keysym.sym == arcade_control.c_down) m_axis(0,5,1); int qr; for(qr =0; qr<7; qr++) { if(e.key.keysym.sym == arcade_control.button[qr]) { joy_bd(0, qr); return; } } } switch(e.key.keysym.sym) { case SDLK_LEFT: m_axis(0, 4, -1); break; case SDLK_RIGHT: m_axis(0, 4, 1); break; case SDLK_UP: m_axis(0,5, -1); break; case SDLK_DOWN: m_axis(0,5, 1); break; default: break; } int qr; for(qr =0; qr<7; qr++) { if(e.key.keysym.sym == syscontrol.button[qr]) { joy_bd(0, qr); break; } } joy_bd(3, e.key.keysym.sym); break; case SDL_JOYAXISMOTION: if(ps3 == true) { static int p_axe = 0; int axe = e.jaxis.axis; int value = e.jaxis.value; static int wait = 0; if(p_axe != axe) { wait = 0; p_axe = axe; } if( ( (++wait) % 10 ) != 0) return; //printf("axis = %d value = %d\n",axe, value); switch(axe) { case 8: // up if(value > 0) m_axis(e.jaxis.which, 5, -1); break; case 10: if(value > 0) m_axis(e.jaxis.which, 5, 1); break; case 11: if(value > 0) { m_axis(e.jaxis.which, 4, -1); } break; case 9: if(value > 0) { m_axis(e.jaxis.which,4, 1); } break; case 19: if(value > 0) joy_bd(e.jaxis.which, BUTTON_A); break; case 16: if(value > 0) joy_bd(e.jaxis.which, BUTTON_B); case 14: if(value > 0) joy_bd(e.jaxis.which, BUTTON_C); break; case 18: if(value > 0) joy_bd(e.jaxis.which, BUTTON_X); break; case 17: if(value > 0) joy_bd(e.jaxis.which, BUTTON_Y); break; case 15: if(value > 0) joy_bd(e.jaxis.which, BUTTON_Z); break; case 3: break; } } else { m_axis(e.jaxis.which, e.jaxis.axis, e.jaxis.value); } break; case SDL_JOYBUTTONDOWN: joy_bd(e.jbutton.which, e.jbutton.button); break; case SDL_JOYBUTTONUP: joy_bu(e.jbutton.which, e.jbutton.button); break; case SDL_KEYUP: joy_bu(3, e.key.keysym.sym); break; } } void mainSystem::renderScreen() { /* check if module-callback will switch the window */ if(run_call) { const char *src = run_call(); if(src != 0) { p.resetSurface(&front); p.setFont(pfont); SDL_FillRect(front, 0, 0); p.printText(100, 100, mx::Color(255, 255, 255), "freeES Loading..."); #ifdef _WIN32 SDL_UpdateRect(front, 0, 0, front.getSurface()->w , front.getSurface()->h); #else front.Flip(); #endif char run_dat[256]; strncpy(run_dat, src, 255); unload(); std::cout << "Loading Module <" << run_dat << ">\n"; SDL_Delay(200); loadObject(run_dat); run_call = 0; return; } } int value = 0; render_surface = rscr(&value); if(value == -24) { p.resetSurface(&front); p.setFont(pfont); SDL_FillRect(front, 0, 0); p.printText(100, 100, mx::Color(255, 255, 255), "freeES Updating..."); #ifdef _WIN32 SDL_UpdateRect(front, 0, 0, front.getSurface()->w , front.getSurface()->h); #else front.Flip(); #endif unload(); #ifdef _WIN32 system("mxp-i.exe --install=data/downloads/newdash.mxp"); #else system("mxp-i --install=data/downloads/newdash.mxp"); #endif SDL_Delay(200); loadDash(); return; } if(value == -23) { p.resetSurface(&front); p.setFont(pfont); SDL_FillRect(front, 0, 0); p.printText(100, 100, mx::Color(255, 255, 255), "freeES Loading..."); #ifdef _WIN32 SDL_UpdateRect(front, 0, 0, front.getSurface()->w, front.getSurface()->h ); #else front.Flip(); #endif unload(); run_call = 0; SDL_Delay(200); loadDash(); return; } mx::mxSurface surf = render_surface; surf.noZero(true); if(resize) { surf_rez.copyResizeSurface(surf); front.copySurface(surf_rez, 0, 0); front.Flip(); } else { front.copySurface(surf, 0, 0); front.Flip(); } } systemController::systemController(SDL_Joystick *stick) { this->stick = stick; if(stick == 0) key = true; else key = false; memset(button, 0, sizeof(button)); c_left = c_right = c_up = c_down = 0; arcade = false; } void systemController::initXarcade() { } }