| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
Reworld,下班在家制作游戏,1500万奖金等你拿以码会友 以友辅仁
共有 2482 人关注过本帖, 1 人收藏
标题:游戏的底层构架!!!!!!!!!
只看楼主 加入收藏
kingode
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2004-5-6
  问题点数:0  回复次数:6   
游戏的底层构架!!!!!!!!!

// INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN // just say no to MFC

#include <windows.h> // include important windows stuff #include <windowsx.h> #include <mmsystem.h> #include <iostream.h> // include important C/C++ stuff #include <conio.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <string.h> #include <stdarg.h> #include <stdio.h> #include <math.h> #include <io.h> #include <fcntl.h>

// DEFINES ////////////////////////////////////////////////

// defines for windows #define WINDOW_CLASS_NAME "WINCLASS1"

// MACROS /////////////////////////////////////////////////

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// GLOBALS //////////////////////////////////////////////// HWND main_window_handle = NULL; // globally track main window HINSTANCE hinstance_app = NULL; // globally track hinstance

char buffer[80]; // general printing buffer

// FUNCTIONS ////////////////////////////////////////////// LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { // this is the main message handler of the system PAINTSTRUCT ps; // used in WM_PAINT HDC hdc; // handle to a device context char buffer[80]; // used to print strings

// what is the message switch(msg) { case WM_CREATE: { // do initialization stuff here // return success return(0); } break; case WM_PAINT: { // simply validate the window hdc = BeginPaint(hwnd,&ps); // end painting EndPaint(hwnd,&ps);

// return success return(0); } break;

case WM_DESTROY: {

// kill the application, this sends a WM_QUIT message PostQuitMessage(0);

// return success return(0); } break;

default:break;

} // end switch

// process any messages that we didn't take care of return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

///////////////////////////////////////////////////////////

int Game_Main(void *parms = NULL, int num_parms = 0) { // this is the main loop of the game, do all your processing // here

// for now test if user is hitting ESC and send WM_CLOSE if (KEYDOWN(VK_ESCAPE)) SendMessage(main_window_handle,WM_CLOSE,0,0);

// return success or failure or your own return code here return(1);

} // end Game_Main

////////////////////////////////////////////////////////////

int Game_Init(void *parms = NULL, int num_parms = 0) { // this is called once after the initial window is created and // before the main event loop is entered, do all your initialization // here

// return success or failure or your own return code here return(1);

} // end Game_Init

/////////////////////////////////////////////////////////////

int Game_Shutdown(void *parms = NULL, int num_parms = 0) { // this is called after the game is exited and the main event // loop while is exited, do all you cleanup and shutdown here

// return success or failure or your own return code here return(1);

} // end Game_Shutdown

// WINMAIN //////////////////////////////////////////////// int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) {

WNDCLASSEX winclass; // this will hold the class we create HWND hwnd; // generic window handle MSG msg; // generic message HDC hdc; // graphics device context

// first fill in the window class stucture winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global hinstance_app = hinstance;

// register the window class if (!RegisterClassEx(&winclass)) return(0);

// create the window if (!(hwnd = CreateWindowEx(NULL, // extended style WINDOW_CLASS_NAME, // class " Game Console Version 1.0", // title WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, // initial x,y 400,300, // initial width, height NULL, // handle to parent NULL, // handle to menu hinstance,// instance of this application NULL))) // extra creation parms return(0);

// save main window handle main_window_handle = hwnd;

// initialize game here Game_Init();

// enter main event loop while(TRUE) { // test if there is a message in queue, if so get it if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { // test if this is a quit if (msg.message == WM_QUIT) break; // translate any accelerator keys TranslateMessage(&msg);

// send the message to the window proc DispatchMessage(&msg); } // end if // main game processing goes here Game_Main(); } // end while

// closedown game here Game_Shutdown();

// return to Windows like this return(msg.wParam);

} // end WinMain

///////////////////////////////////////////////////////////

搜索更多相关主题的帖子: include 构架 windows 底层 
2004-05-09 11:39
asdliu
Rank: 1
等 级:新手上路
帖 子:175
专家分:0
注 册:2004-4-21
  得分:0 
你能把每一条的语句解释一下吗?也就是说有一些函数的作用等等,拜托了

阳光版主欢迎您
2004-05-09 18:18
firechun
Rank: 1
等 级:新手上路
帖 子:135
专家分:0
注 册:2004-4-13
  得分:0 

从程序的执行顺序看吧

首先是WinMain

// WINMAIN //////////////////////////////////////////////// int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) {

WNDCLASSEX winclass; // this will hold the class we create HWND hwnd; // generic window handle MSG msg; // generic message HDC hdc; // graphics device context

// first fill in the window class stucture winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global hinstance_app = hinstance;

// register the window class if (!RegisterClassEx(&winclass)) return(0);

// create the window //创建游戏窗体,如果创建失败则退出 //C++创建windows窗体的典型代码,不用解释了吧? if (!(hwnd = CreateWindowEx(NULL, // extended style WINDOW_CLASS_NAME, // class " Game Console Version 1.0", // title WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, // initial x,y 400,300, // initial width, height NULL, // handle to parent NULL, // handle to menu hinstance,// instance of this application NULL))) // extra creation parms return(0);

// save main window handle main_window_handle = hwnd;

// initialize game here //初始化游戏,你可以在这个过程中放入游戏运行所需要的参数、设定等等 Game_Init();

// enter main event loop //进入主循环,按收用户按键,并把用户的按键交给回调函数WindowProc处理。实际上就是处理用户的游戏行为 while(TRUE) { // test if there is a message in queue, if so get it if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { // test if this is a quit if (msg.message == WM_QUIT) break; // translate any accelerator keys TranslateMessage(&msg);

// send the message to the window proc DispatchMessage(&msg); } // end if // main game processing goes here Game_Main(); } // end while

// closedown game here //当用户按下“ESC”键时(见Game_Main()的代码),退出游戏 Game_Shutdown();

// return to Windows like this return(msg.wParam);

} // end WinMain

//回调函数 // FUNCTIONS ////////////////////////////////////////////// LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { // this is the main message handler of the system PAINTSTRUCT ps; // used in WM_PAINT HDC hdc; // handle to a device context char buffer[80]; // used to print strings

// what is the message //处理相应的消息 switch(msg) { case WM_CREATE: { // do initialization stuff here // return success //看样子这是一个牌类游戏,呵呵 //当window创建成功后,在这里洗牌(stuff),然后返回一个成功标志 return(0); } break; case WM_PAINT: { //重绘屏幕,BegingPaint,EndPaint是绘制屏幕时的具体代码 // simply validate the window hdc = BeginPaint(hwnd,&ps); // end painting EndPaint(hwnd,&ps);

// return success return(0); } break;

case WM_DESTROY: {

//退出游戏,使用PostQuitMessage函数 // kill the application, this sends a WM_QUIT message PostQuitMessage(0);

// return success return(0); } break;

default:break;

} // end switch

// process any messages that we didn't take care of //调用系统的窗口处理程序 return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

//这是游戏的主程序,相关的处理全放在这里,比如用户按下某键,程序如何做出相应的动作等等 int Game_Main(void *parms = NULL, int num_parms = 0) { // this is the main loop of the game, do all your processing // here

// for now test if user is hitting ESC and send WM_CLOSE //按下“ESC”键时,发送WM_CLOSE消息通知系统中断当前程序的运行。在WindowProc中,对应到WM_DESTROY那一段。 if (KEYDOWN(VK_ESCAPE)) SendMessage(main_window_handle,WM_CLOSE,0,0);

// return success or failure or your own return code here return(1);

} // end Game_Main

其它的如Game_Init,Game_ShutDown函数则是游戏初始化和游戏结束的具体代码。

无论是什么游戏,程序的主体结构基本类似于此,只是具体代码不同。

[此贴子已经被作者于2004-05-10 11:32:14编辑过]


本人在编程中国发表的所有文章,除特别说明外均属原创,转载时请注明作者编程中国
2004-05-10 11:29
五月天
Rank: 1
等 级:新手上路
帖 子:181
专家分:0
注 册:2004-4-20
  得分:0 

啦~啦~啦~~
2004-05-26 16:43
guanyou
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2004-6-14
  得分:0 
我编译时,为什么有两个警告??

2004-06-15 19:33
zff_ff
Rank: 1
等 级:新手上路
帖 子:147
专家分:0
注 册:2004-5-12
  得分:0 
以下是引用guanyou在2004-06-15 19:33:05的发言: 我编译时,为什么有两个警告??

这段代码在TC里是无法通过的

在vc里我想你可能是入口出了问题,因为这里的入口是winmain.

把project options里的/subsystem:console改为/subsystem:windows试试


偶是一只想要飞却忘了咋飞的菜鸟
2004-06-15 19:41
chengstone
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:562
专家分:226
注 册:2004-4-3
  得分:0 
thanks

qq:69558139
2004-07-04 13:57
快速回复:游戏的底层构架!!!!!!!!!
数据加载中...
 
   





关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028465 second(s), 7 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved