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

游戏

开发平台:

Visual C++

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