MasterX.cpp
Upload User: hnktjx
Upload Date: 2009-12-18
Package Size: 39k
Code Size: 30k
Category:

Game Program

Development Platform:

C/C++

  1. /* MasterX System Devlopment Kit (SDK)
  2.    DirectX API Encapsulation For easy game programming.
  3.    Written by Jared Bruni . Age 17
  4.    Email Me: MasterOnLSD@lostsidedead.com 
  5. www.lostsidedead.com
  6.   for MasterX Documentation check out
  7.   www.lostsidedead.com/gameprog/
  8.   I LOVE VIDEO GAMES!!!!!!!!!!!!!!
  9.   - :)
  10. */
  11. #include "MasterX.h"
  12. /********** MasterScreen Object ******************/
  13. MasterScreen::~MasterScreen()
  14. {
  15. if(lpBack)
  16. {
  17. lpBack->Release();
  18. lpBack = NULL;
  19. }
  20. if(lpFront)
  21. {
  22. lpFront->Release();
  23. lpFront = NULL;
  24. }
  25. if(lpDD)
  26. {
  27. lpDD->Release();
  28. lpDD = NULL;
  29. }
  30. }
  31. // ****************** MasterXHWND Object 
  32. // Create and initilze MasterX
  33. bool MasterXHWND::CreateMasterX(char* mytitle,int width, int height,DisplayColor color,WNDPROC proc,HINSTANCE hInst,HICON icon,HCURSOR cursor)
  34. {
  35. WNDCLASS wc;
  36. wc.cbClsExtra = 0;
  37. wc.cbWndExtra = 0;
  38. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  39. wc.hInstance = hInst;
  40. wc.hIcon = icon;
  41. wc.hCursor = cursor;
  42. wc.lpfnWndProc = (WNDPROC) proc;
  43. wc.lpszClassName = mytitle;
  44. wc.lpszMenuName = NULL;
  45. wc.style = CS_HREDRAW | CS_VREDRAW;
  46. RegisterClass(&wc);
  47. hwnd = CreateWindow(mytitle,mytitle,WS_POPUPWINDOW,0,0,width,height,0,0,hInst,0);
  48. if(hwnd == 0)
  49. {
  50. return false;
  51. }
  52. ShowWindow(hwnd,SW_SHOW);
  53. UpdateWindow(hwnd);
  54. int bpp;
  55. switch(color)
  56. {
  57. case COLOR_DEFAULT:
  58.     HDC hdc;
  59. // retrieves device-specific information about 
  60.     // the specified device. 
  61.     // Get the windows device context
  62. hdc = GetDC(NULL);
  63.     // Get the number of adjacent color bits for each pixel.
  64.     bpp = GetDeviceCaps(hdc, BITSPIXEL);
  65.     // Release the Device Context
  66. ReleaseDC(NULL, hdc);
  67. break;
  68. case COLOR_8:
  69. bpp = 8;
  70. break;
  71. case COLOR_16:
  72. bpp = 16;
  73. break;
  74. case COLOR_24:
  75. bpp = 24;
  76. break;
  77. case COLOR_32:
  78. bpp = 32;
  79. break;
  80. }
  81. HRESULT ddrval;
  82. // create main dd object
  83. ddrval = DirectDrawCreate(NULL, &scr.lpDD,NULL);
  84. if(ddrval != DD_OK)
  85. {
  86. MessageBox(NULL,"Couldnt Create DirectX Object! Error: Terminating!","Error!!!",MB_ICONERROR);
  87. SendMessage(hwnd,WM_CLOSE,0,0);// Get outta here
  88. }
  89. ddrval = scr.lpDD->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT );
  90. if(ddrval != DD_OK)
  91. {
  92. scr.lpDD->Release();
  93. MessageBox(0,"Couldnt Set Cooperative Level, Fatal Error Aborting","Error",MB_ICONERROR);
  94. }
  95. ddrval = scr.lpDD->SetDisplayMode(width,height,bpp);
  96. if(ddrval != DD_OK)
  97. {
  98. MessageBox(0,"Error Setting Display Mode",0,MB_ICONERROR);
  99. scr.lpDD->Release();
  100. return false;
  101. }
  102. if(setprimarysurface())
  103. {
  104. initSound();
  105. }
  106. else
  107. {
  108. MessageBox(0,"Couldnt Set Surfaces! Fatal Error! DirectX is jacked Man","Broken Software :(",MB_ICONERROR);
  109. return false;
  110. }
  111. initInput();
  112. text.init((MasterScreen*)&scr);
  113. paint.init(&scr);
  114. alive = true;
  115. return true;
  116. }
  117. // initilize Message Processing Loop
  118. WPARAM MasterXHWND::InitLoop(void(* update)(MASTERSCREEN xscr))
  119. {
  120. MSG msg;
  121. while(1)
  122.     {
  123.         int t = PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
  124.         if (t)
  125.         {
  126.             if (!GetMessage (& msg, NULL, 0, 0))
  127.             return msg.wParam;
  128.             TranslateMessage (&msg);
  129.             DispatchMessage (&msg);
  130.         }
  131.         
  132.         else if (activeapp)// this is important here
  133. {
  134. clear();
  135. update(mscr);
  136. mx_update();
  137. }
  138.         else if (!activeapp)
  139. {
  140.          WaitMessage();
  141. }
  142.     }
  143. Destroy();
  144. return msg.wParam;
  145. }
  146. // release and destroy Objects 
  147. void MasterXHWND::Destroy()
  148. {
  149. // Kill Sound Handler
  150. // Kill Graphic Handler
  151. pKeyboard->Unacquire();
  152. pKeyboard->Release();
  153. pDI->Release();
  154. pDI = NULL;
  155. if(pDS)
  156. {
  157. pDS->Release();
  158. }
  159. }
  160. // Break the Loop and kill this Program
  161. void MasterXHWND::Kill()
  162. {
  163. alive = false;
  164. SendMessage(hwnd,WM_CLOSE,0,0);
  165. }
  166. // Get Current Screen
  167. MASTERSCREEN MasterXHWND::GetScreen()
  168. {
  169. return mscr;
  170. }
  171. // Set the Screen
  172. void MasterXHWND::SetScreen(MASTERSCREEN xmscr)
  173. {
  174. mscr = xmscr;
  175. }
  176. // Send a Message to the HWND current iniilized
  177. LRESULT MasterXHWND::SendMasterMessage(UINT msg,WPARAM wParam,LPARAM lParam)
  178. {
  179. return SendMessage(hwnd,msg,wParam,lParam);
  180. }
  181. // Master Clear Screen
  182. void MasterXHWND::clear()
  183. {
  184. ZeroMemory(&ddbltfx,sizeof(ddbltfx));
  185. ddbltfx.dwSize = sizeof(ddbltfx);
  186. ddbltfx.dwFillColor = 0;
  187. scr.lpBack->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT,&ddbltfx);
  188. }
  189. // Manual MasterX Update (Buffer Flip)
  190. void MasterXHWND::mx_update()
  191. {
  192. HRESULT ddrval;
  193. ddrval = scr.lpFront->Flip(NULL, DDFLIP_WAIT);
  194. if(ddrval == DD_OK)
  195. {
  196. }
  197. else
  198. {
  199.     scr.lpFront->Restore();
  200. }
  201. }
  202. // initilize Input
  203. void MasterXHWND::initInput()
  204. {
  205. DirectInputCreate(GetModuleHandle(NULL),DIRECTINPUT_VERSION,(struct IDirectInputA**)&pDI,NULL);
  206. pDI->CreateDevice(GUID_SysKeyboard,&pKeyboard,NULL);
  207. pKeyboard->SetDataFormat(&c_dfDIKeyboard);
  208. // Set the cooperative level 
  209. pKeyboard->SetCooperativeLevel(hwnd, 
  210. DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
  211. pKeyboard->Acquire();
  212. }
  213. // set primary surfaces
  214. bool MasterXHWND::setprimarysurface()
  215. {
  216. DDSURFACEDESC ddsd;
  217. DDSCAPS ddscaps;
  218. HRESULT ddrval;
  219. memset(&ddsd,0,sizeof(ddsd)); // ok were filling out a surface description
  220. ddsd.dwSize = sizeof(ddsd); // then using createsurface
  221. // to attach it to the global surface
  222. ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  223.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
  224. ddsd.dwBackBufferCount = 1;
  225. ddrval = scr.lpDD->CreateSurface(&ddsd,&scr.lpFront,NULL);
  226. if(ddrval != DD_OK)
  227. {
  228.         scr.lpDD->Release();
  229. return false;
  230. }
  231. // Set the description for backbuffer
  232. ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  233. ddrval = scr.lpFront->GetAttachedSurface(&ddscaps, &scr.lpBack);
  234. if(ddrval != DD_OK)
  235. {
  236. scr.lpFront->Release();
  237. scr.lpDD->Release();
  238. return false;
  239. }
  240. return true;
  241. }
  242. // init sound
  243. bool MasterXHWND::initSound()
  244. {
  245. HRESULT h;
  246. h = DirectSoundCreate(NULL, &pDS, NULL);
  247. if (h != DS_OK)
  248. return FALSE;
  249. HWND hWnd = GetForegroundWindow();
  250. if (hWnd == NULL)
  251. hWnd = GetDesktopWindow();
  252. h = pDS->SetCooperativeLevel(hWnd, DSSCL_PRIORITY);
  253. if (h != DS_OK)
  254. { pDS->Release();
  255. pDS = NULL;
  256. return FALSE;
  257. }
  258. IDirectSoundBuffer *lpDsb;
  259. DSBUFFERDESC dsbdesc;
  260. memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
  261. dsbdesc.dwSize = sizeof(DSBUFFERDESC);
  262. dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
  263. dsbdesc.dwBufferBytes = 0;
  264. dsbdesc.lpwfxFormat = NULL;
  265. if (pDS->CreateSoundBuffer(&dsbdesc, &lpDsb, NULL) != DS_OK)
  266. { pDS->Release();
  267. pDS = NULL;
  268. return FALSE;
  269. }
  270. lpDsb->Play(0, 0, DSBPLAY_LOOPING);
  271. return TRUE;
  272. }
  273. // Load a Graphic 
  274. bool MasterXHWND::LoadGraphic(MasterGraphic* g,char* filename)
  275. {
  276. return g->LoadGraphic(filename,&scr);
  277. }
  278. // Load a Sound
  279. bool MasterXHWND::LoadSound(MasterSound* s,LPCSTR resourceID)
  280. {
  281. return s->LoadSound(pDS,resourceID);
  282. }
  283. // Check a keystate via DirectInput
  284. bool MasterXHWND::KeyCheck(int key)
  285. {
  286. BYTE state[256];
  287. pKeyboard->GetDeviceState(sizeof(state), &state);
  288. if(state[key] & 0x80)
  289. {
  290. return true;
  291. }
  292. else
  293. {
  294. return false;
  295. }
  296. }
  297. // Check a keystate via Windows
  298. bool MasterXHWND::AsyncKeyCheck(int key)
  299. {
  300.   if(GetAsyncKeyState(key))
  301.   {
  302.   return true;
  303.   }
  304.   else
  305.   {
  306.   return false;
  307.   }
  308. }
  309. /***** Master Text Object *****************************************/
  310. // set the default font back agian & shit
  311. // set the default font
  312. void MasterText::setdefaultfont()
  313. {
  314. font = CreateFont(13,
  315.         0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  316.         ANSI_CHARSET,
  317.         OUT_DEFAULT_PRECIS,
  318.         CLIP_DEFAULT_PRECIS,
  319.         NONANTIALIASED_QUALITY,
  320.         VARIABLE_PITCH,
  321.         "Arial");
  322. }
  323. //init master text object
  324. void MasterText::init(MasterScreen* x)
  325. {
  326. scr = (MasterScreen*)x;
  327. font = CreateFont(13,
  328.         0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  329.         ANSI_CHARSET,
  330.         OUT_DEFAULT_PRECIS,
  331.         CLIP_DEFAULT_PRECIS,
  332.         NONANTIALIASED_QUALITY,
  333.         VARIABLE_PITCH,
  334.         "Arial"); //  Create a Stupid Font
  335. underfont = CreateFont(13,
  336.         0, 0, 0, FW_NORMAL, FALSE, TRUE, FALSE,
  337.         ANSI_CHARSET,
  338.         OUT_DEFAULT_PRECIS,
  339.         CLIP_DEFAULT_PRECIS,
  340.         NONANTIALIASED_QUALITY,
  341.         VARIABLE_PITCH,
  342.         "Arial"); //  Create a Stupid Font
  343. bk = TRANSPARENT;
  344. text = RGB(255,0,0);
  345. }
  346. // set the font
  347. void MasterText::setfont(HFONT f)
  348. {
  349.     font = f;
  350. }
  351. // set ud
  352. void MasterText::setunderfont(HFONT f)
  353. {
  354. underfont = f;
  355. }
  356. // set the bg color (RGB)
  357. void MasterText::setbkcolor(COLORREF f)
  358. {
  359. bk = f;
  360. }
  361. // set the textcolor
  362. void MasterText::settextcolor(COLORREF f)
  363. {
  364. text = f;
  365. }
  366. // print text
  367. void FAST MasterText::printtext(char* strtext, int x, int y)
  368. {
  369. HDC dc;
  370.     scr->lpBack->GetDC(&dc);
  371. SetTextColor(dc,text);
  372. SetBkColor(dc,bk);
  373. if(font != NULL)
  374. {
  375. SelectObject(dc, font);
  376. }
  377. TextOut(dc,x,y,strtext,strlen(strtext));
  378. scr->lpBack->ReleaseDC(dc);
  379. }
  380. // print formated text within a rectangle
  381. void FAST MasterText::printtextrect(char* strtext, int y, int x, int w, int h)
  382. {
  383. RECT rt;
  384. SetRect(&rt,x,y,w,h);
  385. HDC dc;
  386.     scr->lpBack->GetDC(&dc);
  387. SetTextColor(dc,text);
  388. SetBkColor(dc,bk);
  389. if(font != NULL)
  390. {
  391. SelectObject(dc, font);
  392. }
  393. DrawText(dc,strtext,strlen(strtext),&rt,DT_EDITCONTROL);
  394. scr->lpBack->ReleaseDC(dc);
  395. }
  396. // print text with a underline
  397. void FAST MasterText::printtextunderline(char* strtext, int x, int y)
  398. {
  399. HDC dc;
  400.     scr->lpBack->GetDC(&dc);
  401. SetTextColor(dc,text);
  402. SetBkColor(dc,bk);
  403. if(underfont != NULL)
  404. {
  405. SelectObject(dc, underfont);
  406. }
  407. TextOut(dc,x,y,strtext,strlen(strtext));
  408. scr->lpBack->ReleaseDC(dc);
  409. }
  410. //************************************* GRAPHIC OBJECT ***************************************/
  411. MasterGraphic::~MasterGraphic()
  412. {
  413. if(surf)
  414. {
  415. surf->Release();
  416. surf = NULL;
  417. }
  418. }
  419. MasterGraphic::MasterGraphic()
  420. {
  421. ckey = false;
  422. surf = NULL;
  423. scr  = NULL;
  424. }
  425. // Release a exisiting MasterX Graphic
  426. void MasterGraphic::Release()
  427. {
  428. if(surf)
  429. {
  430. surf->Release();
  431. }
  432. }
  433. // Display a exisiting MasterX Graphic
  434. void MasterGraphic::DisplayGraphic(int x, int y)
  435. {
  436. HRESULT ddrval;
  437. RECT     rcRect;
  438. SetRect(&rcRect,0,0,w,h);
  439. if(surf)
  440. {
  441. if(ckey)
  442. {
  443. ddrval= scr->lpBack->BltFast(x, y, surf,&rcRect, DDBLTFAST_SRCCOLORKEY);
  444. if(ddrval != DD_OK)
  445. {
  446. ReLoadGraphic();
  447. }
  448. }
  449. else
  450. {
  451. ddrval = scr->lpBack->BltFast(x,y,surf,&rcRect,DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
  452. if(ddrval != DD_OK)
  453. {
  454. ReLoadGraphic();
  455. }
  456. }
  457. }
  458. }
  459. // initilize the Screen Data structure
  460. void MasterGraphic::init(MasterScreen* xscr)
  461. {
  462. scr = xscr;
  463. }
  464. // Load MasterX Graphic
  465. bool MasterGraphic::LoadGraphic(char* filenamex, MasterScreen* scrx)
  466. {
  467. scr = scrx;
  468. strcpy(filename,filenamex);
  469. return LoadGraphic(filename);
  470. }
  471. // Reload the Graphic when Destroyed
  472. void MasterGraphic::ReLoadGraphic()
  473. {
  474. DDReLoadBitmap(surf,filename);
  475. }
  476. // Load the Graphic, if you already initilized a screen
  477. bool MasterGraphic::LoadGraphic(char* filenamex)
  478. {
  479. strcpy(filename,filenamex);
  480. if(scr != NULL)
  481. {
  482. surf = DDLoadBitmap(scr->lpDD,filename);
  483. if(surf) 
  484. {
  485. w = MXWIDTH; // from static global
  486.      h = MXHEIGHT; // from static global
  487. return true;
  488. }
  489. else
  490. {
  491. return false;
  492. }
  493. } else
  494. {
  495. return false;// no screen initilized
  496. }
  497. }
  498. // Set a Color key to this graphic
  499. void MasterGraphic::SetColorKey(COLORREF r)
  500. {
  501. if(surf)
  502. {
  503. DDSetColorKey(surf,r);
  504. ckey = true;
  505. }
  506. }
  507. /***************************** THE PAITNING OBJECT ******************************************/
  508. // initlize the MasterPaint
  509. void MasterPaint::init(MasterScreen* xscr)
  510. {
  511. scr = xscr;
  512. }
  513. // Draw a Rectangle
  514. void FAST MasterPaint::mxdrawrect(int x, int y, int xx, int yy, COLORREF fill, COLORREF outline)
  515. {
  516. HDC dc;
  517. scr->lpBack->GetDC(&dc);
  518.    HBRUSH hNewBrush,hOldBrush;// new Brush to paint with and one thats current (so we can restore)
  519.    HPEN hNewPen,hOldPen; // New pen to paint with and the current (so we can restore)
  520.    
  521.    hNewBrush = (HBRUSH)CreateSolidBrush(fill); // create this brush
  522.    hOldBrush = (HBRUSH)SelectObject(dc,(HBRUSH)hNewBrush); // set this brush and get the old brush
  523.    
  524.    hNewPen = CreatePen(PS_SOLID,2,outline); // create this pen
  525.    hOldPen= (HPEN__*)SelectObject(dc,(HPEN__*)hNewPen); // set this pen and  get the old one
  526.    
  527.    Rectangle(dc,x,y,xx,yy); // Draw a rectangle
  528.    // when using shape functions, it outlines the shape with the Pen, and fills it in
  529.    // with the brush
  530.    
  531.    // how shapes work is x,y,xx,yy
  532.    // x is the top left x cord, y, is the bottom left y cord
  533.    // xx is the top right x cord, and yy is the bottom right x cord
  534.    
  535.    // so you basicly set 4 cords and it fills in that space :)
  536.    // restore shit
  537.    
  538.    SelectObject(dc,hOldPen);
  539.    SelectObject(dc,hOldBrush);
  540.    
  541.    DeleteObject(hNewBrush); // DIE DIE MY PEN ILL BE SEEING YOU AGIAN
  542.    DeleteObject(hNewPen); // i dont need this sso lets KILL It
  543.    scr->lpBack->ReleaseDC(dc);
  544. }
  545. // Draw a rectangle
  546. void FAST MasterPaint::mxdrawroundrect(int x, int y, int x2, int y2, int cw, int ch, COLORREF fill,COLORREF outline)
  547. {
  548. HDC dc;
  549. scr->lpBack->GetDC(&dc);
  550. HBRUSH hNewBrush,hOldBrush;// new Brush to paint with and one thats current (so we can restore)
  551.    HPEN hNewPen,hOldPen; // New pen to paint with and the current (so we can restore)
  552.    
  553.    hNewBrush = (HBRUSH)CreateSolidBrush(fill); // create this brush
  554.    hOldBrush = (HBRUSH)SelectObject(dc,(HBRUSH)hNewBrush); // set this brush and get the old brush
  555.    
  556.    hNewPen = CreatePen(PS_SOLID,2,outline); // create this pen
  557.    hOldPen= (HPEN__*)SelectObject(dc,(HPEN__*)hNewPen); // set this pen and  get the old one
  558.    
  559.    
  560.    RoundRect(dc,x,y,x2,y2,cw,ch);
  561.    
  562.    // when using shape functions, it outlines the shape with the Pen, and fills it in
  563.    // with the brush
  564.    
  565.    // how shapes work is x,y,xx,yy
  566.    // x is the top left x cord, y, is the bottom left y cord
  567.    // xx is the top right x cord, and yy is the bottom right x cord
  568.    
  569.    // so you basicly set 4 cords and it fills in that space :)
  570.    // restore shit
  571.    
  572.    SelectObject(dc,hOldPen);
  573.    SelectObject(dc,hOldBrush);
  574.    
  575.    DeleteObject(hNewBrush); // DIE DIE MY PEN ILL BE SEEING YOU AGIAN
  576.    DeleteObject(hNewPen); // i dont need this sso lets KILL It
  577.    scr->lpBack->ReleaseDC(dc);
  578. }
  579. // draw a circle (pie)
  580. void FAST MasterPaint::mxdrawpie(int x, int y, int x2, int y2, int nx, int ny, int nx2, int ny2, COLORREF fill,COLORREF outline)
  581. {
  582. HDC dc;
  583. scr->lpBack->GetDC(&dc);
  584. HBRUSH hNewBrush,hOldBrush;// new Brush to paint with and one thats current (so we can restore)
  585.    HPEN hNewPen,hOldPen; // New pen to paint with and the current (so we can restore)
  586.    
  587.    hNewBrush = (HBRUSH)CreateSolidBrush(fill); // create this brush
  588.    hOldBrush = (HBRUSH)SelectObject(dc,(HBRUSH)hNewBrush); // set this brush and get the old brush
  589.    
  590.    hNewPen = CreatePen(PS_SOLID,2,outline); // create this pen
  591.    hOldPen= (HPEN__*)SelectObject(dc,(HPEN__*)hNewPen); // set this pen and  get the old one
  592.    
  593.    
  594.    Pie(dc,x,y,x2,y2,nx,ny,nx2,ny2);
  595.    // when using shape functions, it outlines the shape with the Pen, and fills it in
  596.    // with the brush
  597.    
  598.    // how shapes work is x,y,xx,yy
  599.    // x is the top left x cord, y, is the bottom left y cord
  600.    // xx is the top right x cord, and yy is the bottom right x cord
  601.    
  602.    // so you basicly set 4 cords and it fills in that space :)
  603.    // restore shit
  604.    
  605.    SelectObject(dc,hOldPen);
  606.    SelectObject(dc,hOldBrush);
  607.    
  608.    DeleteObject(hNewBrush); // DIE DIE MY PEN ILL BE SEEING YOU AGIAN
  609.    DeleteObject(hNewPen); // i dont need this sso lets KILL It
  610.    scr->lpBack->ReleaseDC(dc);
  611. }
  612. // draw chord
  613. void FAST MasterPaint::mxdrawchord(int x, int y, int x2, int y2, int nx, int ny, int nx2, int ny2,COLORREF fill,COLORREF outline)
  614. {
  615.    HDC dc;
  616.    scr->lpBack->GetDC(&dc);
  617.    
  618.        HBRUSH hNewBrush,hOldBrush;// new Brush to paint with and one thats current (so we can restore)
  619.    HPEN hNewPen,hOldPen; // New pen to paint with and the current (so we can restore)
  620.    
  621.    hNewBrush = (HBRUSH)CreateSolidBrush(fill); // create this brush
  622.    hOldBrush = (HBRUSH)SelectObject(dc,(HBRUSH)hNewBrush); // set this brush and get the old brush
  623.    
  624.    hNewPen = CreatePen(PS_SOLID,2,outline); // create this pen
  625.    hOldPen= (HPEN__*)SelectObject(dc,(HPEN__*)hNewPen); // set this pen and  get the old one
  626.    
  627.    Chord(dc,x,y,x2,y2,nx,ny,nx2,ny2);
  628.    
  629.    
  630.    // when using shape functions, it outlines the shape with the Pen, and fills it in
  631.    // with the brush
  632.    
  633.    // how shapes work is x,y,xx,yy
  634.    // x is the top left x cord, y, is the bottom left y cord
  635.    // xx is the top right x cord, and yy is the bottom right x cord
  636.    
  637.    // so you basicly set 4 cords and it fills in that space :)
  638.    // restore shit
  639.    
  640.    SelectObject(dc,hOldPen);
  641.    SelectObject(dc,hOldBrush);
  642.    
  643.    DeleteObject(hNewBrush); // DIE DIE MY PEN ILL BE SEEING YOU AGIAN
  644.    DeleteObject(hNewPen); // i dont need this sso lets KILL It
  645.    scr->lpBack->ReleaseDC(dc);
  646. }
  647. // draw ellipse
  648. void FAST MasterPaint::mxdrawellipse(int x, int y, int x2, int y2,COLORREF fill,COLORREF outline)
  649. {
  650.    HDC dc;
  651.    scr->lpBack->GetDC(&dc);
  652.    
  653.        HBRUSH hNewBrush,hOldBrush;// new Brush to paint with and one thats current (so we can restore)
  654.    HPEN hNewPen,hOldPen; // New pen to paint with and the current (so we can restore)
  655.    
  656.    hNewBrush = (HBRUSH)CreateSolidBrush(fill); // create this brush
  657.    hOldBrush = (HBRUSH)SelectObject(dc,(HBRUSH)hNewBrush); // set this brush and get the old brush
  658.    
  659.    hNewPen = CreatePen(PS_SOLID,2,outline); // create this pen
  660.    hOldPen= (HPEN__*)SelectObject(dc,(HPEN__*)hNewPen); // set this pen and  get the old one
  661.    
  662.    Ellipse(dc,x,y,x2,y2);
  663.    
  664.    
  665.    // when using shape functions, it outlines the shape with the Pen, and fills it in
  666.    // with the brush
  667.    
  668.    // how shapes work is x,y,xx,yy
  669.    // x is the top left x cord, y, is the bottom left y cord
  670.    // xx is the top right x cord, and yy is the bottom right x cord
  671.    
  672.    // so you basicly set 4 cords and it fills in that space :)
  673.    // restore shit
  674.    
  675.    SelectObject(dc,hOldPen);
  676.    SelectObject(dc,hOldBrush);
  677.    
  678.    DeleteObject(hNewBrush); // DIE DIE MY PEN ILL BE SEEING YOU AGIAN
  679.    DeleteObject(hNewPen); // i dont need this sso lets KILL It
  680.    scr->lpBack->ReleaseDC(dc);
  681. }
  682. //*********************** MASTER SOUND OBJECT ***********************************************/
  683. MasterSound::MasterSound()
  684. {
  685. pSound = NULL;
  686. }
  687. MasterSound::~MasterSound()
  688. {
  689. if(pSound)
  690. {
  691. pSound->Release();
  692. }
  693. }
  694. // Load a Sound
  695. bool MasterSound::LoadSound(IDirectSound *pds,LPCSTR name)
  696. {
  697. pSound = DSLoadSoundBuffer(pds,name);
  698. if(pSound)
  699. {
  700. return true;
  701. }
  702. else
  703. {
  704. return false;
  705. }
  706. return false;
  707. }
  708. // Play the Sound
  709. void MasterSound::Play()
  710. {
  711. if(pSound)
  712. {
  713. pSound->Play(0,0,0);
  714. }
  715. }
  716. // Play status of Sound (is it Playing?)
  717. bool MasterSound::PlayStatus()
  718. {
  719.    DWORD status;
  720.    pSound->GetStatus(&status);
  721.   if(status & DSBSTATUS_PLAYING)
  722.   { 
  723.   return true;
  724.   } 
  725.   else 
  726.   {
  727.   return false;
  728.   }
  729.   return false;
  730. }
  731. void MasterSound::Release()
  732. {
  733. if(pSound)
  734. {
  735. pSound->Release();
  736. }
  737. }
  738. //************** NON CLASS RELATED FUNCTION **************************************************/
  739. // DirectDraw Load Bitmap (to surface)
  740. IDirectDrawSurface* DDLoadBitmap(IDirectDraw* pdd, LPCSTR szBitmap)
  741. {
  742. HBITMAP hbm;
  743. BITMAP bm;
  744. IDirectDrawSurface *pdds;
  745. // Load Image for loading from Disk
  746. hbm = (HBITMAP)LoadImage(NULL, szBitmap, IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
  747. if(hbm == NULL) { return NULL; }
  748. GetObject(hbm, sizeof(bm), &bm); // size
  749. // Create Surface for this Bitmap
  750. int cw = bm.bmWidth;
  751. int ch = bm.bmHeight;
  752. MXWIDTH = cw;
  753. MXHEIGHT = ch;
  754. pdds = CreateOffScreenSurface(pdd, bm.bmWidth,bm.bmHeight);
  755. if(pdds)
  756. {
  757. DDCopyBitmap(pdds, hbm, bm.bmWidth,bm.bmHeight);
  758. }
  759. DeleteObject(hbm);
  760. return pdds;
  761. }
  762. mxbitmap DDLoadBitmapX(IDirectDraw* pdd, LPCSTR szBitmap)
  763. {
  764. HBITMAP hbm;
  765. BITMAP bm;
  766. IDirectDrawSurface *pdds;
  767. // Load Image for loading from Disk
  768. hbm = (HBITMAP)LoadImage(NULL, szBitmap, IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
  769. mxbitmap xbm;
  770. if(hbm == NULL) { xbm.surf = NULL; return xbm; }
  771. GetObject(hbm, sizeof(bm), &bm); // size
  772. // Create Surface for this Bitmap
  773. int cw = bm.bmWidth;
  774. int ch = bm.bmHeight;
  775. pdds = CreateOffScreenSurface(pdd, bm.bmWidth,bm.bmHeight);
  776. if(pdds)
  777. {
  778. DDCopyBitmap(pdds, hbm, bm.bmWidth,bm.bmHeight);
  779. }
  780. DeleteObject(hbm);
  781. xbm.surf = pdds;
  782. xbm.w = cw;
  783. xbm.h = ch;
  784. return xbm;
  785. }
  786. // this is a surface when can be cliped 
  787. IDirectDrawSurface* CreateOffScreenSurface(IDirectDraw* pdd, int dx, int dy)
  788. {
  789. DDSURFACEDESC ddsd;
  790. IDirectDrawSurface* pdds;
  791. // Create Surface for this bitmap
  792. ZeroMemory(&ddsd,sizeof(ddsd));
  793. ddsd.dwSize = sizeof(ddsd);
  794. ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
  795. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  796. ddsd.dwWidth = dx;
  797. ddsd.dwHeight = dy;
  798. if(pdd->CreateSurface(&ddsd, &pdds,NULL) != DD_OK)
  799. {
  800. return NULL;
  801. } else {
  802. return pdds;
  803. }
  804. }
  805. // Copy Bitmap
  806. HRESULT DDCopyBitmap(IDirectDrawSurface* pdds, HBITMAP hbm, int dx, int dy)
  807. {
  808. HDC hdcImage;
  809. HDC hdc;
  810. HRESULT hr;
  811. HBITMAP hbmOld;
  812. // Select Bitmap into a MemoryDC 
  813. hdcImage = CreateCompatibleDC(NULL);
  814. hbmOld = (HBITMAP)SelectObject(hdcImage, hbm);
  815. if((hr = pdds->GetDC(&hdc)) == DD_OK)
  816. {
  817. BitBlt(hdc,0,0,dx,dy,hdcImage,0,0,SRCCOPY);
  818. pdds->ReleaseDC(hdc);
  819. }
  820. SelectObject(hdcImage, hbmOld);
  821. DeleteDC(hdcImage);
  822. return hr;
  823. }
  824. // Wav Data
  825. static const TCHAR c_szWAV[] = {"WAVE"};
  826. ///////////////////////////////////////////////////////////////////////////////
  827. //
  828. // DSLoadSoundBuffer
  829. //
  830. ///////////////////////////////////////////////////////////////////////////////
  831. IDirectSoundBuffer *DSLoadSoundBuffer(IDirectSound *pDS, LPCTSTR lpName)
  832. {
  833. IDirectSoundBuffer *pDSB = NULL;
  834. DSBUFFERDESC dsBD = {0};
  835. BYTE *pbWaveData;
  836. if (DSGetWaveResource(NULL, lpName, &dsBD.lpwfxFormat, &pbWaveData, &dsBD.dwBufferBytes))
  837. {
  838. dsBD.dwSize = sizeof(dsBD);
  839. dsBD.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2;
  840. if (SUCCEEDED(pDS->CreateSoundBuffer(&dsBD, &pDSB, NULL)))
  841. {
  842. if (!DSFillSoundBuffer(pDSB, pbWaveData, dsBD.dwBufferBytes))
  843. {
  844. pDSB->Release();
  845. pDSB = NULL;
  846. }
  847. }
  848. else
  849. {
  850. pDSB = NULL;
  851. }
  852. }
  853. return pDSB;
  854. }
  855. ///////////////////////////////////////////////////////////////////////////////
  856. //
  857. // DSReloadSoundBuffer
  858. //
  859. ///////////////////////////////////////////////////////////////////////////////
  860. BOOL DSReloadSoundBuffer(IDirectSoundBuffer *pDSB, LPCTSTR lpName)
  861. {
  862. BOOL result=FALSE;
  863. BYTE *pbWaveData;
  864. DWORD cbWaveSize;
  865. if (DSGetWaveResource(NULL, lpName, NULL, &pbWaveData, &cbWaveSize))
  866. {
  867. if (SUCCEEDED(pDSB->Restore()) &&
  868. DSFillSoundBuffer(pDSB, pbWaveData, cbWaveSize))
  869. {
  870. result = TRUE;
  871. }
  872. }
  873. return result;
  874. }
  875. ///////////////////////////////////////////////////////////////////////////////
  876. //
  877. // DSGetWaveResource
  878. //
  879. ///////////////////////////////////////////////////////////////////////////////
  880. BOOL DSGetWaveResource(HMODULE hModule, LPCTSTR lpName,
  881.    WAVEFORMATEX **ppWaveHeader, BYTE **ppbWaveData, DWORD *pcbWaveSize)
  882. {
  883. HRSRC hResInfo;
  884. HGLOBAL hResData;
  885. void *pvRes;
  886. if (((hResInfo = FindResource(hModule, lpName, c_szWAV)) != NULL) &&
  887. ((hResData = LoadResource(hModule, hResInfo)) != NULL) &&
  888. ((pvRes = LockResource(hResData)) != NULL) &&
  889. DSParseWaveResource(pvRes, ppWaveHeader, ppbWaveData, pcbWaveSize))
  890. {
  891. return TRUE;
  892. }
  893. return FALSE;
  894. }
  895. ///////////////////////////////////////////////////////////////////////////////
  896. ///////////////////////////////////////////////////////////////////////////////
  897. BOOL DSFillSoundBuffer(IDirectSoundBuffer *pDSB, BYTE *pbWaveData, DWORD cbWaveSize)
  898. {
  899. if (pDSB && pbWaveData && cbWaveSize)
  900. {
  901. LPVOID pMem1, pMem2;
  902. DWORD dwSize1, dwSize2;
  903. if (SUCCEEDED(pDSB->Lock(0, cbWaveSize, &pMem1, &dwSize1, &pMem2, &dwSize2, 0)))
  904. {
  905. CopyMemory(pMem1, pbWaveData, dwSize1);
  906. if ( 0 != dwSize2 )
  907. CopyMemory(pMem2, pbWaveData+dwSize1, dwSize2);
  908. pDSB->Unlock(pMem1, dwSize1, pMem2, dwSize2);
  909. return TRUE;
  910. }
  911. }
  912. return FALSE;
  913. }
  914. ///////////////////////////////////////////////////////////////////////////////
  915. ///////////////////////////////////////////////////////////////////////////////
  916. BOOL DSParseWaveResource(void *pvRes, WAVEFORMATEX **ppWaveHeader, BYTE **ppbWaveData,DWORD *pcbWaveSize)
  917. {
  918. DWORD *pdw;
  919. DWORD *pdwEnd;
  920. DWORD dwRiff;
  921. DWORD dwType;
  922. DWORD dwLength;
  923. if (ppWaveHeader)
  924. *ppWaveHeader = NULL;
  925. if (ppbWaveData)
  926. *ppbWaveData = NULL;
  927. if (pcbWaveSize)
  928. *pcbWaveSize = 0;
  929. pdw = (DWORD *)pvRes;
  930. dwRiff = *pdw++;
  931. dwLength = *pdw++;
  932. dwType = *pdw++;
  933. if (dwRiff != mmioFOURCC('R', 'I', 'F', 'F'))
  934. goto exit; // not even RIFF
  935. if (dwType != mmioFOURCC('W', 'A', 'V', 'E'))
  936. goto exit; // not a WAV
  937. pdwEnd = (DWORD *)((BYTE *)pdw + dwLength-4);
  938. while (pdw < pdwEnd)
  939. {
  940. dwType = *pdw++;
  941. dwLength = *pdw++;
  942. switch (dwType)
  943. {
  944. case mmioFOURCC('f', 'm', 't', ' '):
  945. if (ppWaveHeader && !*ppWaveHeader)
  946. {
  947. if (dwLength < sizeof(WAVEFORMAT))
  948. goto exit; // not a WAV
  949. *ppWaveHeader = (WAVEFORMATEX *)pdw;
  950. if ((!ppbWaveData || *ppbWaveData) &&
  951. (!pcbWaveSize || *pcbWaveSize))
  952. {
  953. return TRUE;
  954. }
  955. }
  956. break;
  957. case mmioFOURCC('d', 'a', 't', 'a'):
  958. if ((ppbWaveData && !*ppbWaveData) ||
  959. (pcbWaveSize && !*pcbWaveSize))
  960. {
  961. if (ppbWaveData)
  962. *ppbWaveData = (LPBYTE)pdw;
  963. if (pcbWaveSize)
  964. *pcbWaveSize = dwLength;
  965. if (!ppWaveHeader || *ppWaveHeader)
  966. return TRUE;
  967. }
  968. break;
  969. }
  970. pdw = (DWORD *)((BYTE *)pdw + ((dwLength+1)&~1));
  971. }
  972. exit:
  973. return FALSE;
  974. }
  975. HRESULT
  976. DDSetColorKey(IDirectDrawSurface * pdds, COLORREF rgb)
  977. {
  978.     DDCOLORKEY              ddck;
  979.     ddck.dwColorSpaceLowValue = DDColorMatch(pdds, rgb);
  980.     ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
  981.     return pdds->SetColorKey(DDCKEY_SRCBLT, &ddck);
  982. }
  983. DWORD
  984. DDColorMatch(IDirectDrawSurface * pdds, COLORREF rgb)
  985. {
  986.     COLORREF                rgbT;
  987.     HDC                     hdc;
  988.     DWORD                   dw = CLR_INVALID;
  989.     DDSURFACEDESC          ddsd;
  990.     HRESULT                 hres;
  991.     //
  992.     //  Use GDI SetPixel to color match for us
  993.     //
  994.     if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
  995.     {
  996.         rgbT = GetPixel(hdc, 0, 0);     // Save current pixel value
  997.         SetPixel(hdc, 0, 0, rgb);       // Set our value
  998.         pdds->ReleaseDC(hdc);
  999.     }
  1000.     //
  1001.     // Now lock the surface so we can read back the converted color
  1002.     //
  1003.     ddsd.dwSize = sizeof(ddsd);
  1004.     while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
  1005.         ;
  1006.     if (hres == DD_OK)
  1007.     {
  1008.         dw = *(DWORD *) ddsd.lpSurface;                 // Get DWORD
  1009.         if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
  1010.             dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;  // Mask it to bpp
  1011.         pdds->Unlock(NULL);
  1012.     }
  1013.     //
  1014.     //  Now put the color that was there back.
  1015.     //
  1016.     if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
  1017.     {
  1018.         SetPixel(hdc, 0, 0, rgbT);
  1019.         pdds->ReleaseDC(hdc);
  1020.     }
  1021.     return dw;
  1022. }
  1023. HRESULT
  1024. DDReLoadBitmap(IDirectDrawSurface * pdds, LPCSTR szBitmap)
  1025. {
  1026.     HBITMAP                 hbm;
  1027.     HRESULT                 hr;
  1028.     //
  1029.     //  Try to load the bitmap as a resource, if that fails, try it as a file
  1030.     //
  1031.     hbm = (HBITMAP) LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, 0,
  1032.                               0, LR_CREATEDIBSECTION);
  1033.     if (hbm == NULL)
  1034.         hbm = (HBITMAP) LoadImage(NULL, szBitmap, IMAGE_BITMAP, 0, 0,
  1035.                                   LR_LOADFROMFILE | LR_CREATEDIBSECTION);
  1036.     if (hbm == NULL)
  1037.     {
  1038.         OutputDebugString("handle is nulln");
  1039.         return E_FAIL;
  1040.     }
  1041. hr = DDCopyBitmap2(pdds, hbm, 0, 0, 0, 0);
  1042.    
  1043. if (hr != DD_OK)
  1044.     {
  1045.         OutputDebugString("ddcopybitmap failedn");
  1046.     }
  1047.     DeleteObject(hbm);
  1048.     return hr;
  1049. }
  1050. HRESULT
  1051. DDCopyBitmap2(IDirectDrawSurface * pdds, HBITMAP hbm, int x, int y,
  1052.              int dx, int dy)
  1053. {
  1054.     HDC                     hdcImage;
  1055.     HDC                     hdc;
  1056.     BITMAP                  bm;
  1057.     DDSURFACEDESC          ddsd;
  1058.     HRESULT                 hr;
  1059.     if (hbm == NULL || pdds == NULL)
  1060.         return E_FAIL;
  1061.     //
  1062.     // Make sure this surface is restored.
  1063.     //
  1064.     pdds->Restore();
  1065.     //
  1066.     // Select bitmap into a memoryDC so we can use it.
  1067.     //
  1068.     hdcImage = CreateCompatibleDC(NULL);
  1069.     if (!hdcImage)
  1070.         OutputDebugString("createcompatible dc failedn");
  1071.     SelectObject(hdcImage, hbm);
  1072.     //
  1073.     // Get size of the bitmap
  1074.     //
  1075.     GetObject(hbm, sizeof(bm), &bm);
  1076.     dx = dx == 0 ? bm.bmWidth : dx;     // Use the passed size, unless zero
  1077.     dy = dy == 0 ? bm.bmHeight : dy;
  1078.     //
  1079.     // Get size of surface.
  1080.     //
  1081.     ddsd.dwSize = sizeof(ddsd);
  1082.     ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
  1083.     pdds->GetSurfaceDesc(&ddsd);
  1084.     if ((hr = pdds->GetDC(&hdc)) == DD_OK)
  1085.     {
  1086.         StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, x, y,
  1087.                    dx, dy, SRCCOPY);
  1088.         pdds->ReleaseDC(hdc);
  1089.     }
  1090.     DeleteDC(hdcImage);
  1091.     return hr;
  1092. }
  1093. // ENTRY
  1094. int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR l,int CmdShow)
  1095. {
  1096. return MasterMain(hInst,l);
  1097. }