demoII3_3.cpp
上传用户:husern
上传日期:2018-01-20
资源大小:42486k
文件大小:11k
源码类别:

游戏

开发平台:

Visual C++

  1. // DEMOII3_3.CPP DirectMusic/DirectSound mixed mode Example
  2. // based on demo from Volume I
  3. // system based on 
  4. // conservation of momentum and kinetic energy
  5. // to compile make sure to include DDRAW.LIB, DSOUND.LIB,
  6. // DINPUT.LIB, DINPUT8.LIB, WINMM.LIB, and of course the T3DLIB files
  7. // INCLUDES ///////////////////////////////////////////////
  8. #define INITGUID
  9. #define WIN32_LEAN_AND_MEAN  // just say no to MFC
  10. #include <windows.h>   // include important windows stuff
  11. #include <windowsx.h> 
  12. #include <mmsystem.h>
  13. #include <iostream.h> // include important C/C++ stuff
  14. #include <conio.h>
  15. #include <stdlib.h>
  16. #include <malloc.h>
  17. #include <memory.h>
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include <stdio.h> 
  21. #include <math.h>
  22. #include <io.h>
  23. #include <direct.h>
  24. #include <wchar.h>
  25. #include <fcntl.h>
  26. #include <ddraw.h>
  27. #include <dsound.h> // include dsound, dmusic
  28. #include <dmksctrl.h>
  29. #include <dmusici.h>
  30. #include <dmusicc.h>
  31. #include <dmusicf.h>
  32. #include <dinput.h>
  33. #include "T3DLIB1.H"
  34. #include "T3DLIB2.H"
  35. #include "T3DLIB3.H"
  36. #include "DEMOII3_3RES.H" // include the resource header
  37. // DEFINES ////////////////////////////////////////////////
  38. // defines for windows interface
  39. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  40. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  41. #define WINDOW_WIDTH      400  // size of window
  42. #define WINDOW_HEIGHT     300
  43. // TYPES //////////////////////////////////////////////////////
  44. // basic unsigned types
  45. typedef unsigned short USHORT;
  46. typedef unsigned short WORD;
  47. typedef unsigned char  UCHAR;
  48. typedef unsigned char  BYTE;
  49. // MACROS /////////////////////////////////////////////////
  50. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  51. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  52. // PROTOTYPES /////////////////////////////////////////////
  53. // GLOBALS ////////////////////////////////////////////////
  54. HWND      main_window_handle = NULL; // globally track main window
  55. HINSTANCE main_instance      = NULL; // globally track hinstance
  56. int midi_ids[10]; // hold storage for 10 songs ids
  57. int sound_ids[4]; // storage for 4 sound fx ids
  58. char buffer[256];                     // general printing buffer
  59. // FUNCTIONS //////////////////////////////////////////////
  60. LRESULT CALLBACK WindowProc(HWND hwnd, 
  61.     UINT msg, 
  62.                             WPARAM wparam, 
  63.                             LPARAM lparam)
  64. {
  65. // this is the main message handler of the system
  66. PAINTSTRUCT ps; // used in WM_PAINT
  67. HDC hdc; // handle to a device context
  68. char buffer[80];        // used to print strings
  69. // what is the message 
  70. switch(msg)
  71. {
  72. case WM_CREATE: 
  73.         {
  74. // do initialization stuff here
  75.         // return success
  76. return(0);
  77. } break;
  78.      case WM_COMMAND:
  79.          {
  80.           switch(LOWORD(wparam))
  81.                 {
  82.                 // handle the FILE menu
  83.                 case MENU_FILE_ID_EXIT:
  84.                      {
  85.                      // terminate window
  86.                      PostQuitMessage(0);
  87.                      } break;
  88.                 // handle the HELP menu
  89.                 case MENU_HELP_ABOUT:                 
  90.                      {
  91.                      //  pop up a message box
  92.                      MessageBox(hwnd, "Menu MIDI Demo", 
  93.                                "About MIDI Menu",
  94.                                 MB_OK | MB_ICONEXCLAMATION);
  95.                      } break;
  96.                 // handle each of midi files
  97.                 case MENU_PLAY_ID_0:
  98.                 case MENU_PLAY_ID_1:
  99.                 case MENU_PLAY_ID_2:
  100.                 case MENU_PLAY_ID_3:
  101.                 case MENU_PLAY_ID_4:
  102.                 case MENU_PLAY_ID_5:
  103.                 case MENU_PLAY_ID_6:
  104.                 case MENU_PLAY_ID_7:
  105.                 case MENU_PLAY_ID_8:
  106.                 case MENU_PLAY_ID_9:
  107.                      {
  108.                      // play the midi file
  109.                      int id =  LOWORD(wparam) - MENU_PLAY_ID_0;
  110.                      
  111.                      DMusic_Play(midi_ids[id]);
  112.         
  113.                      // get the device context
  114.                      HDC hdc = GetDC(hwnd);
  115.                      // set the foreground color to random
  116.                      SetTextColor(hdc, RGB(0,255,0));
  117.                      // set the background color to black
  118.                      SetBkColor(hdc, RGB(0,0,0));
  119.                      // finally set the transparency mode to transparent
  120.                      SetBkMode(hdc, OPAQUE);
  121.                      sprintf(buffer,"Playing MIDI segment [%d]   ",id);
  122.  
  123.                      // draw the midi file
  124.                      TextOut(hdc,8,200,buffer, strlen(buffer));
  125.                      ReleaseDC(hwnd, hdc);
  126.                      } break;
  127.                 // handle each of digtial files
  128.                 case MENU_PLAY_ID_10:
  129.                 case MENU_PLAY_ID_11:
  130.                 case MENU_PLAY_ID_12:
  131.                 case MENU_PLAY_ID_13:
  132.                      {
  133.                      // play the digital file
  134.                      int id =  LOWORD(wparam) - MENU_PLAY_ID_10;
  135.                      
  136.                      DSound_Play(sound_ids[id]);
  137.         
  138.                      // get the device context
  139.                      HDC hdc = GetDC(hwnd);
  140.                      // set the foreground color to random
  141.                      SetTextColor(hdc, RGB(0,255,0));
  142.                      // set the background color to black
  143.                      SetBkColor(hdc, RGB(0,0,0));
  144.                      // finally set the transparency mode to transparent
  145.                      SetBkMode(hdc, OPAQUE);
  146.                      sprintf(buffer,"Playing DIGITAL WAV [%d]   ",id);
  147.  
  148.                      // draw the midi file
  149.                      TextOut(hdc,8,216,buffer, strlen(buffer));
  150.                      ReleaseDC(hwnd, hdc);
  151.                      } break;
  152.                 default: break;
  153.              } // end switch wparam
  154.           } break; // end WM_COMMAND
  155.    
  156. case WM_PAINT: 
  157. {
  158. // simply validate the window 
  159.         hdc = BeginPaint(hwnd,&ps);  
  160.         
  161.         // end painting
  162.         EndPaint(hwnd,&ps);
  163.         // return success
  164. return(0);
  165.     } break;
  166. case WM_DESTROY: 
  167. {
  168. // kill the application, this sends a WM_QUIT message 
  169. PostQuitMessage(0);
  170.         // return success
  171. return(0);
  172. } break;
  173. default:break;
  174.     } // end switch
  175. // process any messages that we didn't take care of 
  176. return (DefWindowProc(hwnd, msg, wparam, lparam));
  177. } // end WinProc
  178. ///////////////////////////////////////////////////////////
  179. int Game_Main(void *parms = NULL)
  180. {
  181. // this is the main loop of the game, do all your processing
  182. // here
  183. int index;
  184. // get the device context
  185. HDC hdc = GetDC(main_window_handle);
  186. // for now test if user is hitting ESC and send WM_CLOSE
  187. if (KEYDOWN(VK_ESCAPE))
  188.    SendMessage(main_window_handle,WM_CLOSE,0,0);
  189. // return success or failure or your own return code here
  190. return(1);
  191. } // end Game_Main
  192. ///////////////////////////////////////////////////////////
  193. int Game_Init(void *parms = NULL)
  194. {
  195. // this is called once after the initial window is created and
  196. // before the main event loop is entered, do all your initialization
  197. // here
  198. int index;
  199. // initialize directsound
  200. DSound_Init();
  201. // initialize directmusic
  202. DMusic_Init();
  203. // load in the digital sound
  204. sound_ids[0] = DSound_Load_WAV("DEACTIVATE1.WAV");
  205. sound_ids[1] = DSound_Load_WAV("EXP0.WAV");
  206. sound_ids[2] = DSound_Load_WAV("INCORRECT1.WAV");
  207. sound_ids[3] = DSound_Load_WAV("UPLINK1.WAV");
  208. // load the midi segments
  209. for (index=0; index<10; index++)
  210.     {
  211.     // build up string name
  212.     sprintf(buffer,"MIDIFILE%d.MID",index);
  213.     // load midi file
  214.     midi_ids[index] = DMusic_Load_MIDI(buffer);
  215.     } // end for index
  216. // return success or failure or your own return code here
  217. return(1);
  218. } // end Game_Init
  219. /////////////////////////////////////////////////////////////
  220. int Game_Shutdown(void *parms = NULL)
  221. {
  222. // this is called after the game is exited and the main event
  223. // loop while is exited, do all you cleanup and shutdown here
  224. // stop all sounds
  225. DSound_Stop_All_Sounds();
  226. // delete all sounds
  227. DSound_Delete_All_Sounds();
  228. // shutdown directsound
  229. DSound_Shutdown();
  230. // delete all the music
  231. DMusic_Delete_All_MIDI();
  232. // shutdown directmusic
  233. DMusic_Shutdown();
  234. // return success or failure or your own return code here
  235. return(1);
  236. } // end Game_Shutdown
  237. // WINMAIN ////////////////////////////////////////////////
  238. int WINAPI WinMain( HINSTANCE hinstance,
  239. HINSTANCE hprevinstance,
  240. LPSTR lpcmdline,
  241. int ncmdshow)
  242. {
  243. WNDCLASSEX winclass; // this will hold the class we create
  244. HWND    hwnd;  // generic window handle
  245. MSG    msg;  // generic message
  246. HDC        hdc;      // graphics device context
  247. // first fill in the window class stucture
  248. winclass.cbSize         = sizeof(WNDCLASSEX);
  249. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  250.                           CS_HREDRAW | CS_VREDRAW;
  251. winclass.lpfnWndProc = WindowProc;
  252. winclass.cbClsExtra = 0;
  253. winclass.cbWndExtra = 0;
  254. winclass.hInstance = hinstance;
  255. winclass.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(ICON_T3DX));
  256. winclass.hCursor = LoadCursor(hinstance, MAKEINTRESOURCE(CURSOR_CROSSHAIR)); 
  257. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  258. winclass.lpszMenuName = "SoundMenu";
  259. winclass.lpszClassName = WINDOW_CLASS_NAME;
  260. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  261. // save hinstance in global
  262. main_instance = hinstance;
  263. // register the window class
  264. if (!RegisterClassEx(&winclass))
  265. return(0);
  266. // create the window
  267. if (!(hwnd = CreateWindowEx(NULL,                  // extended style
  268.                             WINDOW_CLASS_NAME,     // class
  269.     "DirectMusic/DirectSound Mixed Mode Demo", // title
  270.     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  271.       0,0,   // initial x,y
  272.     WINDOW_WIDTH,WINDOW_HEIGHT,  // initial width, height
  273.     NULL,   // handle to parent 
  274.     NULL,   // handle to menu
  275.     hinstance,// instance of this application
  276.     NULL))) // extra creation parms
  277. return(0);
  278. // save main window handle
  279. main_window_handle = hwnd;
  280. // initialize game here
  281. Game_Init();
  282. // enter main event loop
  283. while(TRUE)
  284. {
  285.     // test if there is a message in queue, if so get it
  286. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  287.    { 
  288.    // test if this is a quit
  289.        if (msg.message == WM_QUIT)
  290.            break;
  291.    // translate any accelerator keys
  292.    TranslateMessage(&msg);
  293.    // send the message to the window proc
  294.    DispatchMessage(&msg);
  295.    } // end if
  296.     
  297.        // main game processing goes here
  298.        Game_Main();
  299.        
  300. } // end while
  301. // closedown game here
  302. Game_Shutdown();
  303. // return to Windows like this
  304. return(msg.wParam);
  305. } // end WinMain
  306. ///////////////////////////////////////////////////////////