Advertisement
Redxone

[C++] DosBox Mechwarrior 2 starter

Dec 30th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <fstream>
  4. #include <conio.h>
  5. #include <stdio.h>
  6. #include <direct.h>
  7. #include <string>
  8. #include "computercraft.h"
  9. #define GetCurrentDir _getcwd
  10. #include <sstream>
  11.  
  12. Term *term = new Term;
  13. Color *colors = new Color;
  14. Paintutils *paintutils = new Paintutils();
  15. using namespace std;
  16.  
  17.  
  18. bool writeToConf(string file,string cont)
  19. {
  20.    FILE *oFile;
  21.    oFile = fopen((char*) &file[0],"w");
  22.    if(oFile!=NULL)
  23.    {
  24.        fputs("[sdl] \n",oFile);
  25.        fputs((char*) &cont[0],oFile);
  26.        fclose(oFile);
  27.        return 1;
  28.    }
  29.    return 0;
  30. }
  31.  
  32. string getConf(string file,int line)
  33. {
  34.     ifstream input( (char*) &file[0] );
  35.     string online;
  36.     int next = 0;
  37.  
  38.     while( getline( input, online ) ) {
  39.         if(next == line-1) return online;
  40.         next ++;
  41.     }
  42.    return "fail";
  43. }
  44.  
  45.  
  46. string getWorkingDir(string addto)
  47. {
  48.     char cCurrentPath[FILENAME_MAX];
  49.         if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
  50.         {
  51.             return "No path";
  52.         }
  53.     cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
  54.     return string("\"") + cCurrentPath + "\\" + addto + string("\"");
  55. }
  56.  
  57. string getWorkingDir_NoFormat()
  58. {
  59.        char cCurrentPath[FILENAME_MAX];
  60.         if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
  61.         {
  62.             return "No path";
  63.         }
  64.     cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
  65.     return cCurrentPath;
  66. }
  67.  
  68. string getWorkingDirSkipPer(string addto)
  69. {
  70.     char cCurrentPath[FILENAME_MAX];
  71.         if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
  72.         {
  73.             return "No path";
  74.         }
  75.     cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
  76.     return string(cCurrentPath) + "/" + addto + string("\"");
  77. }
  78.  
  79.  
  80. bool startdos(string args)
  81. {
  82.     string fullpath = "Core/DOSbox.exe " + args;
  83.     WinExec((char*) &fullpath[0],SW_NORMAL);
  84. }
  85.  
  86. int opts(int num)
  87. {
  88.     bool waiting = true;
  89.     int input = 0;
  90.     while (waiting)
  91.     {
  92.             getch();
  93.             if(GetAsyncKeyState('1'))
  94.             {
  95.                 waiting = false;
  96.                 input = 1;
  97.                 break;
  98.             }
  99.             else if(GetAsyncKeyState('2') && num > 1)
  100.             {
  101.                 waiting = false;
  102.                 input = 2;
  103.                 break;
  104.             }
  105.             else if(GetAsyncKeyState('3') &&  num > 2)
  106.             {
  107.                 waiting = false;
  108.                 input = 3;
  109.                 break;
  110.             }
  111.             else if(GetAsyncKeyState('4') && num > 3)
  112.             {
  113.                 waiting = false;
  114.                 input = 4;
  115.                 break;
  116.             }
  117.     }
  118.  
  119.     return input;
  120.  
  121. }
  122.  
  123. bool yesno()
  124. {
  125.     cout << "Y/n ?" << endl;
  126.     bool waiting = true;
  127.     bool yes = false;
  128.     while (waiting)
  129.     {
  130.             getch();
  131.             if(GetAsyncKeyState('Y') || GetAsyncKeyState('y'))
  132.             {
  133.                 waiting = false;
  134.                 yes = true;
  135.                 break;
  136.             }
  137.             else if(GetAsyncKeyState('N') || GetAsyncKeyState('n'))
  138.             {
  139.                 waiting = false;
  140.                 break;
  141.             }
  142.     }
  143.  
  144.     return yes;
  145.  
  146. }
  147.  
  148. /*
  149.     -c MOUNT C getWorkingDir()\Games\MS-DOS
  150.     -c C:
  151.     -c CD SOFTWARE
  152.     -c imgmount E MECHWA~3.CUE -t iso
  153.     -c imgmount F MERCEN~2.CUE -t iso
  154.     -c CD /
  155.     -c CD MECHTR~1/INVERT~1/INVERTED
  156.     -c CTMOUSEY.EXE
  157.     -c CD /
  158.     -c CD MECHTR~1/MW2-31ST
  159.     -c MECH2.exe
  160.  
  161. */
  162.  
  163. string mechsupports[1] = {"MW2-31st"};
  164.  
  165. bool drawScreen()
  166. {
  167.     term->setBackgroundColor(colors->black);
  168.     term->Clear();
  169.     term->setBackgroundColor(colors->blue);
  170.     term->setTextColor(colors->white);
  171.     paintutils->drawBoxFilled(30,5,80,25,colors->gray,colors->gray);
  172.     paintutils->drawBoxFilled(30,5,80,5,colors->blue,colors->blue);
  173.     term->setCursorPos(30,5);
  174.     cout << "Welcome to Mech2Dos!" << endl;
  175.     term->setCursorPos(32,8);
  176.     term->setBackgroundColor(colors->gray);
  177.     term->setTextColor(colors->black);
  178.     cout << "Current supported Mechwarrior version(s)" << endl;
  179.     term->setCursorPos(32,10);
  180.     cout << "1. " << mechsupports[0];
  181.     term->setCursorPos(30,13);
  182.     cout << "  Options [1-3] " << endl;
  183.     term->setCursorPos(35,14);
  184.     cout << "1. Configure mouse sensitivity - [" << getConf(getWorkingDir_NoFormat() + "\\Core\\custom_sens.txt",2).substr(12) << "]" << endl;
  185.     term->setCursorPos(35,15);
  186.     cout << "2. Start game. " << endl;
  187.     term->setCursorPos(35,16);
  188.     cout << "3. Exit. " << endl;
  189.     term->setCursorBlink(false);
  190.     term->setCursorPos(33,25);
  191.     term->setTextColor(colors->lightGray);
  192.     cout << "Programmed by - Lewisk3 (Redxone, AKA Lewrish). ";
  193. }
  194.  
  195.  
  196. bool popupmessage(string title, string message)
  197. {
  198.     term->setBackgroundColor(colors->gray);
  199.     term->setTextColor(colors->white);
  200.     paintutils->drawBoxFilled(42,11,72,17,colors->black,colors->black);
  201.     paintutils->drawBoxFilled(40,11,70,16,colors->lightGray,colors->lightGray);
  202.     paintutils->drawBoxFilled(40,11,70,11,colors->blue,colors->blue);
  203.     term->setCursorPos(40,11);
  204.     cout << "Message: " << title;
  205.     term->setCursorPos(40,13);
  206.     term->setBackgroundColor(colors->lightGray);
  207.     term->setTextColor(colors->black);
  208.     cout << message << endl;
  209.     term->setTextColor(colors->gray);
  210.     term->setCursorPos(40,16);
  211.     cout << "Press any key. ";
  212.     getch();
  213.     return 1;
  214. }
  215.  
  216. double getinput(string title)
  217. {
  218.     double val;
  219.     term->setBackgroundColor(colors->gray);
  220.     term->setTextColor(colors->white);
  221.     paintutils->drawBoxFilled(42,11,72,17,colors->black,colors->black);
  222.     paintutils->drawBoxFilled(40,11,70,16,colors->lightGray,colors->lightGray);
  223.     paintutils->drawBoxFilled(40,11,70,11,colors->blue,colors->blue);
  224.     term->setCursorPos(40,11);
  225.     cout << title;
  226.     term->setCursorPos(40,13);
  227.     term->setBackgroundColor(colors->lightGray);
  228.     term->setTextColor(colors->black);
  229.     cout << "Enter numeric value here below ";
  230.     term->setCursorPos(42,15);
  231.     term->setBackgroundColor(colors->gray);
  232.     cout << "                           ";
  233.     term->setTextColor(colors->white);
  234.     term->setCursorPos(42,15);
  235.     cin >> val;
  236.     return val;
  237. }
  238. bool getopts()
  239. {
  240.     switch(opts(3))
  241.     {
  242.         case 1:
  243.         {
  244.  
  245.  
  246.             double sens = getinput("Mouse sensitivity");
  247.  
  248.             if(sens != NULL && sens > 0)
  249.             {
  250.                 std::stringstream sstm;
  251.                 sstm << "sensitivity=" << sens;
  252.                 string stringsens = sstm.str();
  253.                 writeToConf(getWorkingDir_NoFormat() + "\\Core\\custom_sens.txt", stringsens);
  254.                 popupmessage("Success","Added: " + stringsens);
  255.                 drawScreen();
  256.                 return getopts();
  257.             }
  258.             else
  259.             {
  260.                 drawScreen();
  261.                 return getopts();
  262.             }
  263.         }
  264.         break;
  265.         case 2:
  266.         {
  267.             term->setCursorPos(32,18);
  268.             term->setTextColor(colors->black);
  269.              cout << "Start Mechwarrior, MW2_31ST? : ";
  270.  
  271.             if(yesno())
  272.             {
  273.                 cout << "Getting DosBox ready..." << endl;
  274.                 string fullargs = "-conf " + getWorkingDir("Core/Default_Config.txt") + " -conf " + getWorkingDir("Core/custom_sens.txt") +
  275.                     " -c \"MOUNT C " + getWorkingDirSkipPer("/Games/MS-DOS") +
  276.                     " -c C: " + " -c \"CD SOFTWARE\" -c \"imgmount E MECHWA~3.CUE -t iso\" -c \"imgmount F MERCEN~2.CUE -t iso\" -c \"CD /\" -c \"CD MECHTR~1/MW2-31ST\" -c MECH2.exe";
  277.                 startdos(fullargs);
  278.                 return 1;
  279.             }
  280.             else
  281.             {
  282.                 drawScreen();
  283.                 return getopts();
  284.             }
  285.         }
  286.         break;
  287.         case 3:
  288.             return 0;
  289.         break;
  290.  
  291.         default:
  292.             popupmessage("Invalid","Invalid selection.");
  293.             drawScreen();
  294.             return getopts();
  295.         break;
  296.     }
  297.  
  298. }
  299.  
  300. int main()
  301. {
  302.     paintutils->initialize(term);
  303.     drawScreen();
  304.     if(!getopts()){
  305.         term->setBackgroundColor(colors->black);
  306.         term->setTextColor(colors->white);
  307.         term->Clear();
  308.         popupmessage("Thank you.","Thank you for using Mech2Dos!");
  309.         return 0;
  310.     };
  311.  
  312.     cout << "Program has not exited properly, this could be an issue." << endl << "Please contact program developer Lewisk3 (on youtube)" << endl << "Thank you. ";
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement