1. // INCLUDES ///////////////////////////////////////////////
  2. #define WIN32_LEAN_AND_MEAN // just say no to MFC
  3.  
  4. #include <windows.h> // include important windows stuff
  5. #include <windowsx.h>
  6. #include <mmsystem.h>
  7. #include <iostream.h> // include important C/C++ stuff
  8. #include <conio.h>
  9. #include <stdlib.h>
  10. #include <malloc.h>
  11. #include <memory.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include <io.h>
  17. #include <fcntl.h>
  18.  
  19. // DEFINES ////////////////////////////////////////////////
  20.  
  21. // defines for windows
  22. #define WINDOW_CLASS_NAME "WINCLASS1"
  23. #define WINDOW_WIDTH 400
  24. #define WINDOW_HEIGHT 300
  25.  
  26. // starfield defines
  27. #define NUM_STARS 256
  28.  
  29. // MACROS /////////////////////////////////////////////////
  30.  
  31. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  32. #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  33.  
  34. // TYPES //////////////////////////////////////////////////
  35.  
  36. typedef struct STAR_TYP
  37. {
  38. int x,y; // position of star
  39. int vel; // horizontal velocity of star
  40. COLORREF col; // color of star
  41. } STAR, *STAR_PTR;
  42.  
  43. // PROTOTYPES /////////////////////////////////////////////
  44.  
  45. void Erase_Stars(void);
  46. void Draw_Stars(void);
  47. void Move_Stars(void);
  48. void Init_Stars(void);
  49.  
  50. // GLOBALS ////////////////////////////////////////////////
  51.  
  52. HWND main_window_handle = NULL; // globally track main window
  53. HINSTANCE hinstance_app = NULL; // globally track hinstance
  54.  
  55. HDC global_dc = NULL; // tracks a global dc
  56.  
  57. char buffer[80]; // general printing buffer
  58.  
  59. STAR stars[256]; // holds the starfield
  60.  
  61. // FUNCTIONS //////////////////////////////////////////////
  62. LRESULT CALLBACK WindowProc(HWND hwnd,
  63. UINT msg,
  64. WPARAM wparam,
  65. LPARAM lparam)
  66. {
  67. // this is the main message handler of the system
  68. PAINTSTRUCT ps; // used in WM_PAINT
  69. HDC hdc; // handle to a device context
  70. char buffer[80]; // used to print strings
  71.  
  72. // what is the message
  73. switch(msg)
  74. {
  75. case WM_CREATE:
  76. {
  77. // do initialization stuff here
  78. // return success
  79. return(0);
  80. } break;
  81.  
  82. case WM_PAINT:
  83. {
  84. // simply validate the window
  85. hdc = BeginPaint(hwnd,&ps);
  86.  
  87. // end painting
  88. EndPaint(hwnd,&ps);
  89.  
  90. // return success
  91. return(0);
  92. } break;
  93.  
  94. case WM_DESTROY:
  95. {
  96.  
  97. // kill the application, this sends a WM_QUIT message
  98. PostQuitMessage(0);
  99.  
  100. // return success
  101. return(0);
  102. } break;
  103.  
  104. default:break;
  105.  
  106. } // end switch
  107.  
  108. // process any messages that we didn't take care of
  109. return (DefWindowProc(hwnd, msg, wparam, lparam));
  110.  
  111. } // end WinProc
  112.  
  113. ///////////////////////////////////////////////////////////
  114.  
  115. void Init_Stars(void)
  116. {
  117. // this function initializes all the stars
  118.  
  119. for (int index=0; index < NUM_STARS; index++)
  120. {
  121. // select random position
  122. stars[index].x = rand()%WINDOW_WIDTH;
  123. stars[index].y = rand()%WINDOW_HEIGHT;
  124.  
  125. // set random velocity
  126. stars[index].vel = 1 + rand()%16;
  127.  
  128. // set intensity which is inversely prop to velocity for 3D effect
  129. // note, I am mixing equal amounts of RGB to make black -> bright white
  130. int intensity = 15*(17 - stars[index].vel);
  131. stars[index].col = RGB(intensity, intensity, intensity);
  132.  
  133. } // end for index
  134.  
  135. } // end Init_Stars
  136.  
  137. ////////////////////////////////////////////////////////////
  138.  
  139. void Erase_Stars(void)
  140. {
  141. // this function erases all the stars
  142. for (int index=0; index < NUM_STARS; index++)
  143. SetPixel(global_dc, stars[index].x, stars[index].y, RGB(0,0,0));
  144.  
  145. } // end Erase_Stars
  146.  
  147. ////////////////////////////////////////////////////////////
  148.  
  149. void Draw_Stars()
  150. {
  151. // this function draws all the stars
  152. for (int index=0; index < NUM_STARS; index++)
  153. SetPixel(global_dc, stars[index].x, stars[index].y, stars[index].col);
  154.  
  155. } // end Draw_Stars
  156.  
  157. ////////////////////////////////////////////////////////////
  158.  
  159. void Move_Stars(void)
  160. {
  161. // this function moves all the stars and wraps them around the
  162. // screen boundaries
  163. for (int index=0; index < NUM_STARS; index++)
  164. {
  165. // move the star and test for edge
  166. stars[index].x+=stars[index].vel;
  167.  
  168. if (stars[index].x >= WINDOW_WIDTH)
  169. stars[index].x -= WINDOW_WIDTH;
  170.  
  171. } // end for index
  172.  
  173. } // end Move_Stars
  174.  
  175. ////////////////////////////////////////////////////////////
  176.  
  177. int Game_Main(void *parms = NULL, int num_parms = 0)
  178. {
  179. // this is the main loop of the game, do all your processing
  180. // here
  181.  
  182. // get the time
  183. DWORD start_time = GetTickCount();
  184.  
  185. // erase the stars
  186. Erase_Stars();
  187.  
  188. // move the stars
  189. Move_Stars();
  190.  
  191. // draw the stars
  192. Draw_Stars();
  193.  
  194. // lock to 30 fps
  195. while((start_time - GetTickCount() < 33));
  196.  
  197. // for now test if user is hitting ESC and send WM_CLOSE
  198. if (KEYDOWN(VK_ESCAPE))
  199. SendMessage(main_window_handle,WM_CLOSE,0,0);
  200.  
  201. // return success or failure or your own return code here
  202. return(1);
  203.  
  204. } // end Game_Main
  205.  
  206. ////////////////////////////////////////////////////////////
  207.  
  208. int Game_Init(void *parms = NULL, int num_parms = 0)
  209. {
  210. // this is called once after the initial window is created and
  211. // before the main event loop is entered, do all your initialization
  212. // here
  213.  
  214. // first get the dc to the window
  215. global_dc = GetDC(main_window_handle);
  216.  
  217. // initialize the star field here
  218. Init_Stars();
  219.  
  220. // return success or failure or your own return code here
  221. return(1);
  222.  
  223. } // end Game_Init
  224.  
  225. /////////////////////////////////////////////////////////////
  226.  
  227. int Game_Shutdown(void *parms = NULL, int num_parms = 0)
  228. {
  229. // this is called after the game is exited and the main event
  230. // loop while is exited, do all you cleanup and shutdown here
  231.  
  232. // release the global dc
  233. ReleaseDC(main_window_handle, global_dc);
  234.  
  235. // return success or failure or your own return code here
  236. return(1);
  237.  
  238. } // end Game_Shutdown
  239.  
  240. // WINMAIN ////////////////////////////////////////////////
  241. int WINAPI WinMain( HINSTANCE hinstance,
  242. HINSTANCE hprevinstance,
  243. LPSTR lpcmdline,
  244. int ncmdshow)
  245. {
  246.  
  247. WNDCLASSEX winclass; // this will hold the class we create
  248. HWND hwnd; // generic window handle
  249. MSG msg; // generic message
  250. HDC hdc; // graphics device context
  251.  
  252. // first fill in the window class stucture
  253. winclass.cbSize = sizeof(WNDCLASSEX);
  254. winclass.style = CS_DBLCLKS | CS_OWNDC |
  255. CS_HREDRAW | CS_VREDRAW;
  256. winclass.lpfnWndProc = WindowProc;
  257. winclass.cbClsExtra = 0;
  258. winclass.cbWndExtra = 0;
  259. winclass.hInstance = hinstance;
  260. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  261. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  262. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  263. winclass.lpszMenuName = NULL;
  264. winclass.lpszClassName = WINDOW_CLASS_NAME;
  265. winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  266.  
  267. // save hinstance in global
  268. hinstance_app = hinstance;
  269.  
  270. // register the window class
  271. if (!RegisterClassEx(&winclass))
  272. return(0);
  273.  
  274. // create the window
  275. if (!(hwnd = CreateWindowEx(NULL, // extended style
  276. WINDOW_CLASS_NAME, // class
  277. "T3D Game Console Star Demo", // title
  278. WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  279. 0,0, // initial x,y
  280. 400,300, // initial width, height
  281. NULL, // handle to parent
  282. NULL, // handle to menu
  283. hinstance,// instance of this application
  284. NULL))) // extra creation parms
  285. return(0);
  286.  
  287. // save main window handle
  288. main_window_handle = hwnd;
  289.  
  290. // initialize game here
  291. Game_Init();
  292.  
  293. // enter main event loop
  294. while(TRUE)
  295. {
  296. // test if there is a message in queue, if so get it
  297. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  298. {
  299. // test if this is a quit
  300. if (msg.message == WM_QUIT)
  301. break;
  302.  
  303. // translate any accelerator keys
  304. TranslateMessage(&msg);
  305.  
  306. // send the message to the window proc
  307. DispatchMessage(&msg);
  308. } // end if
  309.  
  310. // main game processing goes here
  311. Game_Main();
  312.  
  313. } // end while
  314.  
  315. // closedown game here
  316. Game_Shutdown();
  317.  
  318. // return to Windows like this
  319. return(msg.wParam);
  320.  
  321. } // end WinMain
  322.  
  323. ///////////////////////////////////////////////////////////

2D游戏编程7—星空案例的更多相关文章

  1. 2D游戏编程6—windows程序模板

    // INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN // just say ...

  2. 2D游戏编程5—锁定频率

    核心利用win心跳函数GetTickCount利用差量锁定fps,如下代码锁定30fps,缺点为如果计算机不能以30fps运行,程序将低于30fps #define WIN32_LEAN_AND_ME ...

  3. 2D游戏编程3—GDI

    WM_PAINT消息触发程序重新绘制界面,过程如下: PAINTSTRUCT    ps;    // used in WM_PAINT HDC        hdc;    // handle to ...

  4. 2D游戏编程4—Windows事件

    windows消息传来的参数分解: Message: WM_ACTIVATE Parameterization: fActive      = LOWORD(wParam);       // act ...

  5. 2D游戏编程2--windows高级编程

      windows应用程序布局 编译流程 响应菜单事件消息 菜单消息处理实例: LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wpar ...

  6. 2D游戏编程1--windows编程模型

    一.创建一个windows程序步骤 1.创建一个windows类 2.创建一个事件处理程序 3.注册windows类 4.用之前创建的windows类创建一个窗口 5.创建一个主事件循环   二.存储 ...

  7. Windows游戏编程之从零开始d

    Windows游戏编程之从零开始d I'm back~~恩,几个月不见,大家还好吗? 这段时间真的好多童鞋在博客里留言说或者发邮件说浅墨你回来继续更新博客吧. woxiangnifrr童鞋说每天都在来 ...

  8. 3D游戏编程大师技巧──2D引擎的编译问题

    接上一篇文章,这里将介绍2D引擎的编译,从现在开始才真正进入<3D游戏编程大师技巧>的学习.本书的第一.二章只是简介了游戏编程和windows编程,从第三章开始才是介绍<window ...

  9. 《Unity3D/2D游戏开发从0到1》正式出版发行

    <Unity3D/2D游戏开发从0到1>正式出版发行 去年个人编写的Unity书籍正式在2015年7月正式发行,现在补充介绍一下个人著作.书籍信息:      书籍的名称: <Uni ...

随机推荐

  1. cgi创建web应用(一)之传递表单数据与返回html

    主旨: 0.环境说明 1.创建一个cgi本地服务 2.创建一个html表单页 3.创建一个对应的cgi 脚本文件 4.运行调试 0.环境说明: 系统:win7 32位家庭版 python:2.7 代码 ...

  2. func_get_args的使用

    func_get_args是获取方法中参数的数组,返回的是一个数组,与func_num_args搭配使用:func_num_args一般写在方法中,用于计数:使用方法如下:function foo($ ...

  3. SQL导入

    然后将新窗口中所有内容放到你需要复制的那个数据库中->新建查询->修改第一行 USE[新数据库名]-> 运行这段代码->刷新数据库 基本就是选择源数据库和目标数据库,特别注意的 ...

  4. Python属性、方法和类管理系列之----元类

    元类的介绍 请看位于下面网址的一篇文章,写的相当好. http://blog.jobbole.com/21351/ 实例补充 class Meta(type): def __new__(meta, c ...

  5. 老鸟的Python入门教程

    转自老鸟的Python入门教程 重要说明 这不是给编程新手准备的教程,如果您入行编程不久,或者还没有使用过1到2门编程语言,请移步!这是有一定编程经验的人准备的.最好是熟知Java或C,懂得命令行,S ...

  6. 类型<T> where T:class的用法

    public void Delete<T>(List<T> EntityList) where T : class, new() 就是说T必须是一个类(class)类型,不能是 ...

  7. java Date和String转换总结

    java.util.Date和String类型的转换是非常常用的,现在总结一下: 1. Date转换为String //Date --->String DateFormat dft = new ...

  8. java代码转换为c# 工具

    Demo Java to C# Converter.exe 已下载到 F:\SoftWare-new\java\Java_to_CSharp_Converter.rar

  9. 【HDOJ】1022 Train Problem I

    栈和队列训练题目. #include <stdio.h> #include <string.h> #define MAXNUM 1005 char in[MAXNUM]; ch ...

  10. INCOIN Importing Multilingual Items (Doc ID 278126.1)

    APPLIES TO: Oracle Inventory Management - Version: 11.5.9 to 11.5.10.CU2 - Release: 11.5 to 11.5 GOA ...