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

游戏

开发平台:

Visual C++

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