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

游戏

开发平台:

Visual C++

  1. // DEMOII8_2b.CPP - texture color modulation with base texture demo 
  2. // to compile make sure to include DDRAW.LIB, DSOUND.LIB,
  3. // DINPUT.LIB, WINMM.LIB, and of course 
  4. // T3DLIB1.CPP,T3DLIB2.CPP,T3DLIB3.CPP..T3DLIB6.CPP
  5. // and only run app if desktop is in 16-bit color mode
  6. // INCLUDES ///////////////////////////////////////////////
  7. #define INITGUID
  8. #define WIN32_LEAN_AND_MEAN  
  9. #include <windows.h>   // include important windows stuff
  10. #include <windowsx.h> 
  11. #include <mmsystem.h>
  12. #include <iostream.h> // include important C/C++ stuff
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <malloc.h>
  16. #include <memory.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include <stdio.h> 
  20. #include <math.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <ddraw.h>  // directX includes
  24. #include <dsound.h>
  25. #include <dmksctrl.h>
  26. #include <dmusici.h>
  27. #include <dmusicc.h>
  28. #include <dmusicf.h>
  29. #include <dinput.h>
  30. #include "T3DLIB1.h" // game library includes
  31. #include "T3DLIB2.h"
  32. #include "T3DLIB3.h"
  33. #include "T3DLIB4.h"
  34. #include "T3DLIB5.h"
  35. #include "T3DLIB6.h"
  36. // DEFINES ////////////////////////////////////////////////
  37. // defines for windows 
  38. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  39. // setup a 640x480 16-bit windowed mode example
  40. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  41. #define WINDOW_WIDTH      640   // size of window
  42. #define WINDOW_HEIGHT     480
  43. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  44.                                 // note: if windowed and not
  45.                                 // fullscreen then bitdepth must
  46.                                 // be same as system bitdepth
  47.                                 // also if 8-bit the a pallete
  48.                                 // is created and attached
  49. #define WINDOWED_APP      1     // 0 not windowed, 1 windowed
  50. #define TEXTSIZE           128 // size of texture mxm
  51. #define NUM_TEXT           12  // number of textures
  52. #define NUM_LMAP           4   // number of light map textures
  53. // PROTOTYPES /////////////////////////////////////////////
  54. // game console
  55. int Game_Init(void *parms=NULL);
  56. int Game_Shutdown(void *parms=NULL);
  57. int Game_Main(void *parms=NULL);
  58. // GLOBALS ////////////////////////////////////////////////
  59. HWND main_window_handle           = NULL; // save the window handle
  60. HINSTANCE main_instance           = NULL; // save the instance
  61. char buffer[80];                          // used to print text
  62. BITMAP_IMAGE textures[NUM_TEXT],   // holds our texture library 
  63.              lightmaps[NUM_LMAP],  // holds our light map textures
  64.              temp_text;            // temporary working texture
  65. // FUNCTIONS //////////////////////////////////////////////
  66. LRESULT CALLBACK WindowProc(HWND hwnd, 
  67.     UINT msg, 
  68.                             WPARAM wparam, 
  69.                             LPARAM lparam)
  70. {
  71. // this is the main message handler of the system
  72. PAINTSTRUCT ps;    // used in WM_PAINT
  73. HDC hdc;    // handle to a device context
  74. // what is the message 
  75. switch(msg)
  76. {
  77. case WM_CREATE: 
  78.         {
  79. // do initialization stuff here
  80. return(0);
  81. } break;
  82.     case WM_PAINT:
  83.          {
  84.          // start painting
  85.          hdc = BeginPaint(hwnd,&ps);
  86.          // end painting
  87.          EndPaint(hwnd,&ps);
  88.          return(0);
  89.         } break;
  90. case WM_DESTROY: 
  91. {
  92. // kill the application
  93. PostQuitMessage(0);
  94. return(0);
  95. } break;
  96. default:break;
  97.     } // end switch
  98. // process any messages that we didn't take care of 
  99. return (DefWindowProc(hwnd, msg, wparam, lparam));
  100. } // end WinProc
  101. // WINMAIN ////////////////////////////////////////////////
  102. int WINAPI WinMain( HINSTANCE hinstance,
  103. HINSTANCE hprevinstance,
  104. LPSTR lpcmdline,
  105. int ncmdshow)
  106. {
  107. // this is the winmain function
  108. WNDCLASSEX winclass; // this will hold the class we create
  109. HWND    hwnd;  // generic window handle
  110. MSG    msg;  // generic message
  111. HDC        hdc;      // graphics device context
  112. // first fill in the window class stucture
  113. winclass.cbSize         = sizeof(WNDCLASSEX);
  114. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  115.                           CS_HREDRAW | CS_VREDRAW;
  116. winclass.lpfnWndProc = WindowProc;
  117. winclass.cbClsExtra = 0;
  118. winclass.cbWndExtra = 0;
  119. winclass.hInstance = hinstance;
  120. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  121. winclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
  122. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  123. winclass.lpszMenuName = NULL;
  124. winclass.lpszClassName = WINDOW_CLASS_NAME;
  125. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  126. // save hinstance in global
  127. main_instance = hinstance;
  128. // register the window class
  129. if (!RegisterClassEx(&winclass))
  130. return(0);
  131. // create the window
  132. if (!(hwnd = CreateWindowEx(NULL,                  // extended style
  133.                             WINDOW_CLASS_NAME,     // class
  134.     WINDOW_TITLE, // title
  135.     (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)), 
  136.       0,0,   // initial x,y
  137.     WINDOW_WIDTH,WINDOW_HEIGHT,  // initial width, height
  138.     NULL,   // handle to parent 
  139.     NULL,   // handle to menu
  140.     hinstance,// instance of this application
  141.     NULL))) // extra creation parms
  142. return(0);
  143. // save main window handle
  144. main_window_handle = hwnd;
  145. if (WINDOWED_APP)
  146. {
  147. // now resize the window, so the client area is the actual size requested
  148. // since there may be borders and controls if this is going to be a windowed app
  149. // if the app is not windowed then it won't matter
  150. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  151. // make the call to adjust window_rect
  152. AdjustWindowRectEx(&window_rect,
  153.      GetWindowStyle(main_window_handle),
  154.      GetMenu(main_window_handle) != NULL,
  155.      GetWindowExStyle(main_window_handle));
  156. // save the global client offsets, they are needed in DDraw_Flip()
  157. window_client_x0 = -window_rect.left;
  158. window_client_y0 = -window_rect.top;
  159. // now resize the window with a call to MoveWindow()
  160. MoveWindow(main_window_handle,
  161.            0, // x position
  162.            0, // y position
  163.            window_rect.right - window_rect.left, // width
  164.            window_rect.bottom - window_rect.top, // height
  165.            FALSE);
  166. // show the window, so there's no garbage on first render
  167. ShowWindow(main_window_handle, SW_SHOW);
  168. } // end if windowed
  169. // perform all game console specific initialization
  170. Game_Init();
  171. // enter main event loop
  172. while(1)
  173. {
  174. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  175. // test if this is a quit
  176.         if (msg.message == WM_QUIT)
  177.            break;
  178. // translate any accelerator keys
  179. TranslateMessage(&msg);
  180. // send the message to the window proc
  181. DispatchMessage(&msg);
  182. } // end if
  183.     
  184.     // main game processing goes here
  185.     Game_Main();
  186. } // end while
  187. // shutdown game and release all resources
  188. Game_Shutdown();
  189. // return to Windows like this
  190. return(msg.wParam);
  191. } // end WinMain
  192. // T3D GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  193. int Game_Init(void *parms)
  194. {
  195. // this function is where you do all the initialization 
  196. // for your game
  197. int index; // looping variable
  198. // initialize directdraw, very important that in the call
  199. // to setcooperativelevel that the flag DDSCL_MULTITHREADED is used
  200. // which increases the response of directX graphics to
  201. // take the global critical section more frequently
  202. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  203. // load in the textures
  204. Load_Bitmap_File(&bitmap16bit, "OMPTXT128_24.BMP");
  205. // now extract each 128x128x16 texture from the image
  206. for (int itext = 0; itext < NUM_TEXT; itext++)
  207.     {     
  208.     // create the bitmap
  209.     Create_Bitmap(&textures[itext],(WINDOW_WIDTH/2)-4*(TEXTSIZE/2),(WINDOW_HEIGHT/2)-2*(TEXTSIZE/2),TEXTSIZE,TEXTSIZE,16);
  210.     Load_Image_Bitmap16(&textures[itext], &bitmap16bit,itext % 4,itext / 4,BITMAP_EXTRACT_MODE_CELL);
  211.     } // end for
  212. // create temporary working texture (load with first texture to set loaded flags)
  213. Create_Bitmap(&temp_text,(WINDOW_WIDTH/2)-(TEXTSIZE/2),(WINDOW_HEIGHT/2)+(TEXTSIZE/2),TEXTSIZE,TEXTSIZE,16);
  214. Load_Image_Bitmap16(&temp_text, &bitmap16bit,0,0,BITMAP_EXTRACT_MODE_CELL);
  215. // done, so unload the bitmap
  216. Unload_Bitmap_File(&bitmap16bit);
  217. // now load the lightmaps
  218. // load in the textures
  219. Load_Bitmap_File(&bitmap16bit, "LIGHTMAPS128_24.BMP");
  220. // now extract each 128x128x16 texture from the image
  221. for (itext = 0; itext < NUM_LMAP; itext++)
  222.     {     
  223.     // create the bitmap
  224.     Create_Bitmap(&lightmaps[itext],(WINDOW_WIDTH/2)+2*(TEXTSIZE/2),(WINDOW_HEIGHT/2)-2*(TEXTSIZE/2),TEXTSIZE,TEXTSIZE,16);
  225.     Load_Image_Bitmap16(&lightmaps[itext], &bitmap16bit,itext % 4,itext / 4,BITMAP_EXTRACT_MODE_CELL);
  226.     } // end for
  227. // done, so unload the bitmap
  228. Unload_Bitmap_File(&bitmap16bit);
  229. // initialize directinput
  230. DInput_Init();
  231. // acquire the keyboard only
  232. DInput_Init_Keyboard();
  233. // hide the mouse
  234. if (!WINDOWED_APP)
  235.    ShowCursor(FALSE);
  236. // seed random number generate
  237. srand(Start_Clock());
  238. // return success
  239. return(1);
  240. } // end Game_Init
  241. ///////////////////////////////////////////////////////////
  242. int Game_Shutdown(void *parms)
  243. {
  244. // this function is where you shutdown your game and
  245. // release all resources that you allocated
  246. // shut everything down
  247. // shutdown directdraw last
  248. DDraw_Shutdown();
  249. // now directsound
  250. DSound_Stop_All_Sounds();
  251. DSound_Shutdown();
  252. // shut down directinput
  253. DInput_Shutdown();
  254. // return success
  255. return(1);
  256. } // end Game_Shutdown
  257. //////////////////////////////////////////////////////////
  258. int Game_Main(void *parms)
  259. {
  260. // this is the workhorse of your game it will be called
  261. // continuously in real-time this is like main() in C
  262. // all the calls for you game go here!
  263. int          index;               // looping var
  264. static int   curr_texture  = 7;   // currently active texture
  265. static int   curr_lightmap = 1;   // current light map
  266. static float scalef        = .25; // texture ambient scale factor
  267. // start the timing clock
  268. Start_Clock();
  269. // clear the drawing surface
  270. DDraw_Fill_Surface(lpddsback, 0);
  271. // lock back buffer and copy background into it
  272. DDraw_Lock_Back_Surface();
  273. ///////////////////////////////////////////
  274. // our little image processing algorithm :)
  275. // Pixel_dest[x,y]rgb = pixel_source[x,y]rgb * ambient + 
  276. //                      pixel_source[x,y]rgb * light_map[x,y]rgb
  277. USHORT *sbuffer = (USHORT *)textures[curr_texture].buffer;
  278. USHORT *lbuffer = (USHORT *)lightmaps[curr_lightmap].buffer;
  279. USHORT *tbuffer = (USHORT *)temp_text.buffer;
  280. // perform RGB transformation on bitmap
  281. for (int iy = 0; iy < temp_text.height; iy++)
  282.     for (int ix = 0; ix < temp_text.width; ix++)
  283.          {
  284.          int rs,gs,bs;   // used to extract the source rgb values
  285.          int rl, gl, bl; // light map rgb values
  286.          int rf,gf,bf;   // the final rgb terms
  287.          // extract pixel from source bitmap
  288.          USHORT spixel = sbuffer[iy*temp_text.width + ix];
  289.          // extract RGB values
  290.          _RGB565FROM16BIT(spixel, &rs,&gs,&bs);
  291.          // extract pixel from lightmap bitmap
  292.          USHORT lpixel = lbuffer[iy*temp_text.width + ix];
  293.          // extract RGB values
  294.          _RGB565FROM16BIT(lpixel, &rl,&gl,&bl);
  295.          // simple formula base + scale * lightmap
  296.          rf = ( scalef * (float)rl ) + ( (float)rs );
  297.          gf = ( scalef * (float)gl ) + ( (float)gs );
  298.          bf = ( scalef * (float)bl ) + ( (float)bs );
  299.          // test for overflow
  300.          if (rf > 255) rf=255;
  301.          if (gf > 255) gf=255;
  302.          if (bf > 255) bf=255;
  303.          // rebuild RGB and test for overflow
  304.          // and write back to buffer
  305.          tbuffer[iy*temp_text.width + ix] = _RGB16BIT565(rf,gf,bf);
  306.          
  307.          } // end for ix     
  308. ////////////////////////////////////////
  309. // draw textures
  310. Draw_Bitmap16(&temp_text, back_buffer, back_lpitch,0);
  311. Draw_Bitmap16(&textures[curr_texture], back_buffer, back_lpitch,0);
  312. Draw_Bitmap16(&lightmaps[curr_lightmap], back_buffer, back_lpitch,0);
  313. // unlock back surface
  314. DDraw_Unlock_Back_Surface();
  315. // read keyboard
  316. DInput_Read_Keyboard();
  317. // test if user wants to change texture
  318. if (keyboard_state[DIK_RIGHT])
  319.    {
  320.    if (++curr_texture > (NUM_TEXT-1))
  321.       curr_texture = (NUM_TEXT-1);
  322.    Wait_Clock(100);
  323.    } // end if
  324. if (keyboard_state[DIK_LEFT])
  325.    {
  326.    if (--curr_texture < 0)
  327.       curr_texture = 0;
  328.    Wait_Clock(100);
  329.    } // end if
  330. // test if user wants to change ligthmap texture
  331. if (keyboard_state[DIK_UP])
  332.    {
  333.    if (++curr_lightmap > (NUM_LMAP-1))
  334.       curr_lightmap = (NUM_LMAP-1);
  335.    Wait_Clock(100);
  336.    } // end if
  337. if (keyboard_state[DIK_DOWN])
  338.    {
  339.    if (--curr_lightmap < 0)
  340.       curr_lightmap = 0;
  341.    Wait_Clock(100);
  342.    } // end if
  343. // is user changing scaling factor
  344. if (keyboard_state[DIK_PGUP])
  345.    {
  346.    scalef+=.01;
  347.    if (scalef > 10)
  348.       scalef = 10;
  349.    Wait_Clock(10);
  350.    } // end if
  351. if (keyboard_state[DIK_PGDN])
  352.    {
  353.    scalef-=.01;
  354.    if (scalef < 0)
  355.       scalef = 0;
  356.    Wait_Clock(10);
  357.    } // end if
  358. // draw title
  359. Draw_Text_GDI("Use <RIGHT>/<LEFT> arrows to change texture.",10,4,RGB(255,255,255), lpddsback);
  360. Draw_Text_GDI("Use <UP>/<DOWN> arrows to change the light map texture.",10,20,RGB(255,255,255), lpddsback);
  361. Draw_Text_GDI("Use <PAGE UP>/<PAGE DOWN> arrows to change ambient scale factor.",10, 36,RGB(255,255,255), lpddsback);
  362. Draw_Text_GDI("Press <ESC> to Exit. ",10, 56,RGB(255,255,255), lpddsback);
  363. // print stats
  364. sprintf(buffer,"Texture #%d, Ligtmap #%d, Ambient Scaling Factor: %f", curr_texture, curr_lightmap, scalef);
  365. Draw_Text_GDI(buffer, 10, WINDOW_HEIGHT - 20,RGB(255,255,255), lpddsback);
  366. // flip the surfaces
  367. DDraw_Flip();
  368. // sync to 30ish fps
  369. Wait_Clock(30);
  370. // check of user is trying to exit
  371. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  372.     {
  373.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  374.     // stop all sounds
  375.     DSound_Stop_All_Sounds();
  376.     } // end if
  377. // return success
  378. return(1);
  379. } // end Game_Main
  380. //////////////////////////////////////////////////////////