t3dconsole2.cpp
Upload User: husern
Upload Date: 2018-01-20
Package Size: 42486k
Code Size: 10k
Category:

Game Program

Development Platform:

Visual C++

  1. // T3DCONSOLE2.CPP - First Template for Tricks 3D Volume II
  2. // Use this as a template for your applications if you wish
  3. // you may want to change things like the resolution of the
  4. // application, if it's windowed, the directinput devices
  5. // that are acquired and so forth...
  6. // currently the app creates a 640x480x16 windowed display
  7. // hence, you must be in 16 bit color before running the application
  8. // if you want fullscreen mode then simple change the WINDOWED_APP
  9. // value in the #defines below value to FALSE (0). Similarly, if
  10. // you want another bitdepth, maybe 8-bit for 256 colors then 
  11. // change that in the call to DDraw_Init() in the function
  12. // Game_Init() within this file.
  13. // READ THIS!
  14. // To compile make sure to include DDRAW.LIB, DSOUND.LIB,
  15. // DINPUT.LIB, WINMM.LIB in the project link list, and of course 
  16. // the C++ source modules T3DLIB1.CPP,T3DLIB2.CPP, and T3DLIB3.CPP
  17. // and the headers T3DLIB1.H,T3DLIB2.H, and T3DLIB3.H must
  18. // be in the working directory of the compiler
  19. // INCLUDES ///////////////////////////////////////////////
  20. #define INITGUID       // make sure all the COM interfaces are available
  21.                        // instead of this you can include the .LIB file
  22.                        // DXGUID.LIB
  23. #define WIN32_LEAN_AND_MEAN  
  24. #include <windows.h>   // include important windows stuff
  25. #include <windowsx.h> 
  26. #include <mmsystem.h>
  27. #include <iostream.h> // include important C/C++ stuff
  28. #include <conio.h>
  29. #include <stdlib.h>
  30. #include <malloc.h>
  31. #include <memory.h>
  32. #include <string.h>
  33. #include <stdarg.h>
  34. #include <stdio.h> 
  35. #include <math.h>
  36. #include <io.h>
  37. #include <fcntl.h>
  38. #include <ddraw.h>  // directX includes
  39. #include <dsound.h>
  40. #include <dmksctrl.h>
  41. #include <dmusici.h>
  42. #include <dmusicc.h>
  43. #include <dmusicf.h>
  44. #include <dinput.h>
  45. #include "T3DLIB1.h" // game library includes
  46. #include "T3DLIB2.h"
  47. #include "T3DLIB3.h"
  48. // DEFINES ////////////////////////////////////////////////
  49. // defines for windows interface
  50. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  51. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  52. #define WINDOW_WIDTH      640   // size of window
  53. #define WINDOW_HEIGHT     480
  54. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  55.                                 // note: if windowed and not
  56.                                 // fullscreen then bitdepth must
  57.                                 // be same as system bitdepth
  58.                                 // also if 8-bit the a pallete
  59.                                 // is created and attached
  60. #define WINDOWED_APP      1     // 0 not windowed, 1 windowed
  61. // PROTOTYPES /////////////////////////////////////////////
  62. // game console
  63. int Game_Init(void *parms=NULL);
  64. int Game_Shutdown(void *parms=NULL);
  65. int Game_Main(void *parms=NULL);
  66. // GLOBALS ////////////////////////////////////////////////
  67. HWND main_window_handle           = NULL; // save the window handle
  68. HINSTANCE main_instance           = NULL; // save the instance
  69. char buffer[256];                          // used to print text
  70. // FUNCTIONS //////////////////////////////////////////////
  71. LRESULT CALLBACK WindowProc(HWND hwnd, 
  72.     UINT msg, 
  73.                             WPARAM wparam, 
  74.                             LPARAM lparam)
  75. {
  76. // this is the main message handler of the system
  77. PAINTSTRUCT ps;    // used in WM_PAINT
  78. HDC hdc;    // handle to a device context
  79. // what is the message 
  80. switch(msg)
  81. {
  82. case WM_CREATE: 
  83.         {
  84. // do initialization stuff here
  85. return(0);
  86. } break;
  87.     case WM_PAINT:
  88.          {
  89.          // start painting
  90.          hdc = BeginPaint(hwnd,&ps);
  91.          // end painting
  92.          EndPaint(hwnd,&ps);
  93.          return(0);
  94.         } break;
  95. case WM_DESTROY: 
  96. {
  97. // kill the application
  98. PostQuitMessage(0);
  99. return(0);
  100. } break;
  101. default:break;
  102.     } // end switch
  103. // process any messages that we didn't take care of 
  104. return (DefWindowProc(hwnd, msg, wparam, lparam));
  105. } // end WinProc
  106. // WINMAIN ////////////////////////////////////////////////
  107. int WINAPI WinMain( HINSTANCE hinstance,
  108. HINSTANCE hprevinstance,
  109. LPSTR lpcmdline,
  110. int ncmdshow)
  111. {
  112. // this is the winmain function
  113. WNDCLASSEX winclass; // this will hold the class we create
  114. HWND    hwnd;  // generic window handle
  115. MSG    msg;  // generic message
  116. HDC        hdc;      // graphics device context
  117. // first fill in the window class stucture
  118. winclass.cbSize         = sizeof(WNDCLASSEX);
  119. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  120.                           CS_HREDRAW | CS_VREDRAW;
  121. winclass.lpfnWndProc = WindowProc;
  122. winclass.cbClsExtra = 0;
  123. winclass.cbWndExtra = 0;
  124. winclass.hInstance = hinstance;
  125. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  126. winclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
  127. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  128. winclass.lpszMenuName = NULL;
  129. winclass.lpszClassName = WINDOW_CLASS_NAME;
  130. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  131. // register the window class
  132. if (!RegisterClassEx(&winclass))
  133. return(0);
  134. // create the window
  135. if (!(hwnd = CreateWindowEx(NULL,              // extended style
  136.                             WINDOW_CLASS_NAME, // class
  137.     WINDOW_TITLE, // title
  138.     (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)), 
  139.       0,0,   // initial x,y
  140.     WINDOW_WIDTH,WINDOW_HEIGHT,  // initial width, height
  141.     NULL,   // handle to parent 
  142.     NULL,   // handle to menu
  143.     hinstance,// instance of this application
  144.     NULL)))   // extra creation parms
  145. return(0);
  146. // save the window handle and instance in a global
  147. main_window_handle = hwnd;
  148. main_instance      = hinstance;
  149. // resize the window so that client is really width x height
  150. if (WINDOWED_APP)
  151. {
  152. // now resize the window, so the client area is the actual size requested
  153. // since there may be borders and controls if this is going to be a windowed app
  154. // if the app is not windowed then it won't matter
  155. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  156. // make the call to adjust window_rect
  157. AdjustWindowRectEx(&window_rect,
  158.      GetWindowStyle(main_window_handle),
  159.      GetMenu(main_window_handle) != NULL,
  160.      GetWindowExStyle(main_window_handle));
  161. // save the global client offsets, they are needed in DDraw_Flip()
  162. window_client_x0 = -window_rect.left;
  163. window_client_y0 = -window_rect.top;
  164. // now resize the window with a call to MoveWindow()
  165. MoveWindow(main_window_handle,
  166.            0, // x position
  167.            0, // y position
  168.            window_rect.right - window_rect.left, // width
  169.            window_rect.bottom - window_rect.top, // height
  170.            FALSE);
  171. // show the window, so there's no garbage on first render
  172. ShowWindow(main_window_handle, SW_SHOW);
  173. } // end if windowed
  174. // perform all game console specific initialization
  175. Game_Init();
  176. // enter main event loop
  177. while(1)
  178. {
  179. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  180. // test if this is a quit
  181.         if (msg.message == WM_QUIT)
  182.            break;
  183. // translate any accelerator keys
  184. TranslateMessage(&msg);
  185. // send the message to the window proc
  186. DispatchMessage(&msg);
  187. } // end if
  188.     
  189.     // main game processing goes here
  190.     Game_Main();
  191. } // end while
  192. // shutdown game and release all resources
  193. Game_Shutdown();
  194. // return to Windows like this
  195. return(msg.wParam);
  196. } // end WinMain
  197. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  198. int Game_Init(void *parms)
  199. {
  200. // this function is where you do all the initialization 
  201. // for your game
  202. // start up DirectDraw (replace the parms as you desire)
  203. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  204. // initialize directinput
  205. DInput_Init();
  206. // acquire the keyboard 
  207. DInput_Init_Keyboard();
  208. // add calls to acquire other directinput devices here...
  209. // initialize directsound and directmusic
  210. DSound_Init();
  211. DMusic_Init();
  212. // hide the mouse
  213. if (!WINDOWED_APP)
  214.     ShowCursor(FALSE);
  215. // seed random number generator
  216. srand(Start_Clock());
  217. // all your initialization code goes here...
  218. // return success
  219. return(1);
  220. } // end Game_Init
  221. ///////////////////////////////////////////////////////////
  222. int Game_Shutdown(void *parms)
  223. {
  224. // this function is where you shutdown your game and
  225. // release all resources that you allocated
  226. // shut everything down
  227. // release all your resources created for the game here....
  228. // now directsound
  229. DSound_Stop_All_Sounds();
  230. DSound_Delete_All_Sounds();
  231. DSound_Shutdown();
  232. // directmusic
  233. DMusic_Delete_All_MIDI();
  234. DMusic_Shutdown();
  235. // shut down directinput
  236. DInput_Release_Keyboard();
  237. DInput_Shutdown();
  238. // shutdown directdraw last
  239. DDraw_Shutdown();
  240. // return success
  241. return(1);
  242. } // end Game_Shutdown
  243. //////////////////////////////////////////////////////////
  244. int Game_Main(void *parms)
  245. {
  246. // this is the workhorse of your game it will be called
  247. // continuously in real-time this is like main() in C
  248. // all the calls for you game go here!
  249. int index; // looping var
  250. // start the timing clock
  251. Start_Clock();
  252. // clear the drawing surface 
  253. DDraw_Fill_Surface(lpddsback, 0);
  254. // read keyboard and other devices here
  255. DInput_Read_Keyboard();
  256. // game logic here...
  257. // flip the surfaces
  258. DDraw_Flip();
  259. // sync to 30ish fps
  260. Wait_Clock(30);
  261. // check of user is trying to exit
  262. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  263.     {
  264.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  265.     } // end if
  266. // return success
  267. return(1);
  268.  
  269. } // end Game_Main
  270. //////////////////////////////////////////////////////////