Advertisement
Guest User

Console.h

a guest
Sep 11th, 2010
1,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. /*************************************************************************************
  2. ********* @ console.h  - Copyright (c) 2010 0RBiT              | _ | [] | X |*********
  3. **************************************************************************************
  4. *********                                                                    *********
  5. ********* console.h by 0rbit. Basic textbox/in-game console for DirectX and  *********
  6. *********    Call of Duty Modern Warfare 2                                   *********
  7. *********                                                                    *********
  8. ********* Greetz to:                                                         *********
  9. *********        * AlexAk92                                                  *********
  10. *********        * SEGnosis                                                  *********
  11. *********        * KiwiDog                                                   *********
  12. *********        * All at uc-forum.com!                                      *********
  13. *********                                                                    *********
  14. ********* This source may only be used for free/public releases. You must    *********
  15. *********    give credit if you use it. NOT FOR USE IN VIP/PAID HACKS.       *********
  16. *********                                                                    *********
  17. ********* Contact:                                                           *********
  18. *********    bit-byte@live.com                                               *********
  19. *********                                                                    *********
  20. *************************************************************************************/
  21.  
  22. #ifndef _CONSOLE_H_
  23. #define _CONSOLE_H_
  24.  
  25. #include "O_Drawing.h"      // <- Replace with your own drawing class.
  26. #include <string>
  27. #include <vector>
  28. using namespace std;
  29.  
  30. #define ADDR_CONSOLE    0x4393E0    // MW2 v1.2.208
  31.                                     // v1.0.182 is 0x446DD0
  32.  
  33. class MW2_Console
  34. {
  35.     public:
  36.         void Render(int x, int y, int width, LPDIRECT3DDEVICE9 pDevice);
  37.         void takeInput(WPARAM wParam);
  38.         void Init();
  39.         void Send(string cmd);
  40.         string command;
  41.         vector<string> prevCommands;
  42.         int caretPos,caretTick,cmdCount,currCmd;
  43.         bool showCaret,handled;
  44. }Console;
  45.  
  46. void MW2_Console::Init()
  47. {
  48.     command="";
  49.     caretPos=0;
  50.     cmdCount=0;
  51.     currCmd=0;
  52. }
  53.  
  54. // SendCommandToConsole(1,1,"Command");
  55. void (__cdecl *SendCommandToConsole)(int a1,int a2,const char *cvar)=(void (__cdecl *)(int,int,const char *))ADDR_CONSOLE;
  56. void MW2_Console::Send(string cmd)
  57. {
  58.     size_t scPos,len;   // scPos = semicolon position. Len = length of cmd.
  59.     char tmp[50];       // temporary char array for splitting multiple commands
  60.     string cmdToSend;   // The command to be sent. Used for splitting multiple commands
  61.     int i=0;   
  62.  
  63.     do
  64.     {
  65.         cmd.begin();
  66.         scPos=cmd.find(';');
  67.         if (scPos!=string::npos)    // There was an ;. More than one command.
  68.         {
  69.             len=cmd.copy(tmp,int(scPos),0);     // Copy the first command to tmp
  70.             tmp[len]='\0';                      // End tmp "string"
  71.             cmdToSend=tmp;                      // Assign tmp as the command to be sent
  72.             cmdToSend+='\n';
  73.             SendCommandToConsole(1,1,cmdToSend.c_str());
  74.             cmd.erase(0,int(scPos)+1);          // Erase the just-sent command, and the ;
  75.         }
  76.         else                        // There was no ;. Just a single command.
  77.         {
  78.             cmd+='\n';
  79.             SendCommandToConsole(1,1,cmd.c_str());
  80.         }
  81.  
  82.     }while(scPos!=string::npos);    // Only loop again if a ; was found in the last iteration.
  83. }
  84.  
  85. void MW2_Console::Render(int x, int y, int width, LPDIRECT3DDEVICE9 pDevice)
  86. {
  87.     // Draw box with border
  88.     Draw.FullRectBordered(x,y,width,20,2,D3DCOLOR_ARGB( 155, 000, 000, 000 ),D3DCOLOR_ARGB( 255, 000, 000, 000 ),pDevice);
  89.     // Draw *command
  90.     Draw.Text(x+2,y+2,D3DCOLOR_ARGB( 255, 255, 165, 0 ),">",pDevice);
  91.     Draw.Text(x+10,y+2,D3DCOLOR_ARGB( 255, 255, 255, 0 ),command.c_str(),pDevice);
  92.    
  93.     caretTick+=1;
  94.     if ( caretTick >= 25)
  95.         {
  96.             caretTick=0;
  97.             showCaret=!showCaret;
  98.         }  
  99.     if(showCaret)
  100.         Draw.Text(x+6+(caretPos*8),y+2,D3DCOLOR_ARGB( 255, 255, 165, 0 ),"|",pDevice);
  101. }
  102.  
  103. void MW2_Console::takeInput(WPARAM wParam)
  104. {
  105.     if(handled)
  106.     {
  107.         handled=false;
  108.         return;
  109.     }
  110.  
  111.     switch(wParam)
  112.     {
  113.     case '\b':  // backspace
  114.         {
  115.         if(caretPos>0)
  116.         {
  117.             command.erase(caretPos-1,1);
  118.             caretPos-=1;
  119.         }
  120.         }
  121.         break;
  122.    
  123.     case '\r':  // return/enter
  124.         {
  125.             prevCommands.push_back(command);
  126.             if(command=="about")
  127.             {
  128.                 // show about
  129.             }
  130.             else
  131.             if(command=="forcehost")
  132.             {
  133.                 // do forcehostan
  134.             }
  135.             else
  136.             {
  137.                 // Just a plain 'ol command!
  138.                 Send(command);
  139.             }
  140.             command="";
  141.             caretPos=0;
  142.             cmdCount=prevCommands.size();
  143.             currCmd=cmdCount;
  144.         }
  145.         break;
  146.  
  147.     case '\t':  // tab
  148.         {
  149.             if(cmdCount>0)
  150.             {
  151.                 if(currCmd>0)
  152.                 {
  153.                     currCmd-=1;
  154.                     command=prevCommands.at(currCmd);
  155.                     caretPos=command.length();
  156.                 }
  157.                 else
  158.                 {
  159.                     currCmd=cmdCount-1;
  160.                     command=prevCommands.at(currCmd);
  161.                     caretPos=command.length();
  162.                 }
  163.             }
  164.         }
  165.         break;
  166.  
  167.     default:
  168.         command.insert(caretPos,1,(char)wParam);
  169.         caretPos+=1;
  170.         break;
  171.     } // switch(wParam)
  172. }
  173.  
  174. #endif
  175.  
  176. /*
  177. Also, my Keypressed function (from main.cpp)
  178.  
  179.  
  180. void KeyPressed(WPARAM wParam)
  181. {
  182.     Console.handled=false;          // in case it's still true
  183.  
  184.  
  185.     if(wParam==VK_SHIFT)            // allows us to use combinations such as Shift+(key)
  186.         shift=true;                
  187.     if(wParam==VK_F1)               // F1 is our show/hide hotkey
  188.     {
  189.         Console.handled=true;      
  190.         if(console)
  191.         {
  192.             Console.Send("con_minicon 0");  // This is my lazy way of getting "feedback" from the console
  193.             console=false;
  194.         }
  195.         else
  196.         {
  197.             Console.Send("con_minicon 1");
  198.             console=true;
  199.         }
  200.     }
  201.  
  202.     if(console)     // If the console is visible, take input.
  203.     {
  204.         switch(wParam)
  205.         {
  206.         case VK_LEFT:
  207.             if(Console.caretPos>0) Console.caretPos-=1; // Left key. Move the caret back 1 if it's not already at 0.
  208.             break;
  209.         case VK_RIGHT:
  210.             if(Console.caretPos<(int)Console.command.length()) Console.caretPos+=1; // Right key. Move the caret forward one if it's not already at the end of the string.
  211.             break;
  212.         case VK_DELETE:
  213.             if(shift)                   // Shift+DEL are pressed.
  214.             {
  215.                 Console.command="";     // Empty the string
  216.                 Console.caretPos=0;     // Reset Caret
  217.             }
  218.             else
  219.             {
  220.                 // Delete the character in front of the caret if it's not at the end of the string
  221.                 // (Note that the caret stays in the same position)
  222.                 if(Console.caretPos<(int)Console.command.length()) Console.command.erase(Console.caretPos,1);
  223.             }
  224.             break;
  225.         case VK_ESCAPE:
  226.             {
  227.                 Console.handled=true;
  228.                 console=false;          // Hide the console.
  229.             }
  230.             break;
  231.         } // switch(wParam)
  232.     } // if(console)
  233. }
  234. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement