Advertisement
hoosier18

main.cpp

Feb 22nd, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1.  
  2. #include "ScanContents.h"
  3.  
  4.  
  5. bool TakeScreenshot(std::string WindowToFind, BITMAP &bm, HBITMAP &hbmap, BITMAPINFO &bmi, HDC &hdcShot,
  6. HBITMAP &hbitmapOld, HWND &hwnd);
  7.  
  8.  
  9.  
  10.  
  11. void SetupBitmapInfo(BITMAPINFO &bmi, int bWidth, int bHeight, int bitsPerPixel);
  12. bool CompareColour(RGBQUAD * pPixels, int height, int width, int x, int y);
  13. void ScanBMP(ScanContents * scan);
  14. bool Aim_Bot(HWND appWnd, std::string GameWindow);
  15. MouseCoord CurrentMouseXY(0, 0);
  16.  
  17.  
  18. int main()
  19. {
  20. std::string GameWindow = "rgb - Windows Photo Viewer";  //Counter-Strike Source      
  21. HWND appWnd = FindWindow(0, GameWindow.c_str());
  22.  
  23.  
  24. while(!appWnd)
  25. {
  26. system("CLS");
  27. HWND appWnd = FindWindow(0, GameWindow.c_str());
  28. std::cout << "Unable to find " << GameWindow.c_str() <<std::endl;
  29. Sleep(500);
  30. }
  31.  
  32.  
  33. POINT currentPos;
  34. GetCursorPos(& currentPos);
  35. CurrentMouseXY.X = currentPos.x;
  36. CurrentMouseXY.Y = currentPos.y;
  37.  
  38.  
  39. Aim_Bot(appWnd, GameWindow);
  40. system("Pause");
  41. return 0;
  42. }
  43.  
  44.  
  45. bool TakeScreenshot(std::string WindowToFind, BITMAP &bm, HBITMAP &hbmap, BITMAPINFO &bmi, HDC &hdcShot,
  46. HBITMAP &hbitmapOld, HWND &hwnd)
  47.  
  48.  
  49. {
  50. RECT rc;
  51. GetWindowRect(hwnd, &rc);
  52.  
  53.  
  54. hdcShot = CreateCompatibleDC(0);
  55. hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left, rc.bottom - rc.top);
  56. SelectObject(hdcShot, hbmap);
  57.  
  58.  
  59. BitBlt(hdcShot, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
  60. GetDC(0),rc.left,rc.top, SRCCOPY);
  61.  
  62.  
  63.  
  64. if(!GetObject(hbmap, sizeof(BITMAP), (LPSTR)&bm))
  65. return false;
  66.  
  67.  
  68. int bitsPerPixel = bm.bmBitsPixel;
  69.  
  70.  
  71. if(bitsPerPixel != 32 || bm.bmPlanes != 1)
  72. return false;
  73.  
  74.  
  75. SetupBitmapInfo(bmi, bm.bmWidth, bm.bmHeight, bitsPerPixel);
  76. return true;
  77. }
  78.  
  79.  
  80. bool Aim_Bot(HWND appWnd, std::string GameWindow)
  81. {
  82. RECT rcWindow;
  83. GetWindowRect(appWnd, &rcWindow);
  84. BITMAP bm;
  85. HBITMAP hbmap;
  86. HBITMAP hbmapOld;
  87. BITMAPINFO bmi;
  88. HDC hdcShot;
  89. HDC hdfcScreen;
  90.  
  91.  
  92. RGBQUAD * pPixels;
  93.  
  94.  
  95. int TimeTakenScreenAndScan;
  96. while(true)
  97. {
  98. if(!GetAsyncKeyState('X'))
  99. {
  100.   TimeTakenScreenAndScan = clock();
  101.  
  102.  
  103. if(TakeScreenshot(GameWindow, bm, hbmap, bmi, hdcShot, hbmapOld, appWnd))
  104. break;
  105.  
  106.  
  107. HBITMAP hbmapNew = CreateCompatibleBitmap(hdcShot, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top);
  108.  
  109.  
  110. HDC hdcShotNew = CreateCompatibleDC(hdcShot);
  111.  
  112.  
  113. HBITMAP OldBmp = (HBITMAP) SelectObject(hdcShotNew, hbmapNew);
  114.  
  115.  
  116. BitBlt(hdcShotNew, 0, 0, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top,
  117. hdcShot, 0,0, SRCCOPY);
  118.  
  119.  
  120.  
  121.  
  122. pPixels = new RGBQUAD[bm.bmWidth * bm.bmHeight];
  123. if(!pPixels)return false;
  124.  
  125.  
  126. SelectObject(hdcShotNew, OldBmp);
  127.  
  128.  
  129. if(!GetDIBits(hdcShotNew, hbmapNew, 0, bm.bmHeight, pPixels, &bmi, DIB_RGB_COLORS))
  130. {
  131. ReleaseDC(appWnd, hdcShot);
  132. delete[] pPixels;
  133. return false;
  134. }
  135. ReleaseDC(appWnd, hdcShot);
  136.  
  137.  
  138. ScanContents scanContentsMain(bm,rcWindow, pPixels);
  139.  
  140.  
  141. ScanBMP(&scanContentsMain);
  142.  
  143.  
  144.  
  145.  
  146. if(pPixels)
  147. free(pPixels);
  148. SelectObject(hdcShot, hbmapOld);
  149. DeleteObject(hbmap);
  150. DeleteDC(hdcShot);
  151. DeleteObject(hbmapNew);
  152. DeleteObject(OldBmp);
  153. DeleteDC(hdcShotNew);
  154. //std::cout << "out of scan, took " << clock() - TimeTakenScreenAndScan << " milliseconds" << std::endl;
  155. }
  156. }
  157. }
  158. void SetupBitmapInfo(BITMAPINFO &bmi, int bWidth, int bHeight, int bitsPerPixel)
  159. {
  160. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  161. bmi.bmiHeader.biWidth = bWidth;
  162. bmi.bmiHeader.biHeight = bHeight;
  163. bmi.bmiHeader.biPlanes = 1;
  164. bmi.bmiHeader.biBitCount = bitsPerPixel;
  165. bmi.bmiHeader.biCompression = BI_RGB;
  166. bmi.bmiHeader.biSizeImage = 0;
  167. }
  168. void ShootBot(int x, int y)
  169. {
  170. mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
  171. mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
  172. }
  173.  
  174.  
  175. void ScanBMP(ScanContents * scan)
  176. {
  177. for(int y = (scan ->RcWindow.bottom - scan->RcWindow.top)/4;
  178. y < ((scan->RcWindow.bottom - scan->RcWindow.top) - (scan->RcWindow.bottom - scan->RcWindow.top)/3.5);
  179. y++)
  180.  
  181.  
  182. {
  183. for(int x = (scan ->RcWindow.right - scan->RcWindow.left)/4;
  184. x < ((scan->RcWindow.right - scan->RcWindow.left) - (scan->RcWindow.right - scan->RcWindow.left)/4);
  185. x++)
  186. {
  187.     SetCursorPos(x+scan->RcWindow.left, (y+4)+scan->RcWindow.top);
  188.  
  189.  
  190.  
  191.  
  192. if(CompareColour(scan-> PPixels, scan->Bm.bmHeight, scan ->Bm.bmWidth, x, y))
  193. {
  194. //SetCursorPos(x+scan->RcWindow.left, (y+4)+scan->RcWindow.top);
  195.  
  196.  
  197. POINT currentPos;
  198. GetCursorPos(&currentPos);
  199.  
  200.  
  201.  //ShootBot(x+scan->RcWindow.left, y+scan->RcWindow.top);
  202.  
  203.  
  204. CurrentMouseXY.X = currentPos.x;
  205. CurrentMouseXY.Y = currentPos.y;
  206. return;
  207. }
  208. }
  209. }
  210. }
  211. bool CompareColour(RGBQUAD * pPixels, int height, int width, int x, int y)
  212. {
  213.  
  214.  
  215. int p = (height-y-1)*width+x;
  216.  
  217.  
  218. if((int)pPixels[p].rgbRed > 215 && (int)pPixels[p].rgbGreen < 30 && (int)pPixels[p].rgbBlue < 30)
  219. {
  220. return true;
  221.  
  222.  
  223.  
  224.  
  225. }
  226.  
  227.  
  228. if(GetAsyncKeyState(VK_DELETE))
  229. {
  230. exit(0);
  231. }
  232. return false;
  233.  
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement