
对这方面感兴趣或者想学习C/C++可以加群:558502932,有问题可以在群内大家一起交流学习。
对于正在学习C/C++的同学或者朋友来说,如何用C/C++写程序都有些模糊的概念,毕竟如果只是学习C/C++的理论知识,很多人甚至都不知道C/C++学了能干嘛。
有很多人都想去往游戏方面发展,毕竟能写出一个游戏供大家学习是一件特别自豪的事情。但是要写出一个游戏,不是那么容易的事情,今天我就来分享一下,我们讲师写过的一个游戏框架,希望能对你们有所帮助。
对这方面感兴趣或者想学习C/C++可以加群:558502932
代码如下:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Windowsframe.cpp: This is a Windows-Game Forms
// Author: MOYG
// Date: Dec-19-2015
// Version 1.0
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// includes ////////////////////////////////////////////////////////////////////////////////////////////////////
#include "windows.h"
#include "time.h“
// DEFINES ////////////////////////////////////////////////////////////////////////////////////////////////////
#define WINDOW_WIDTH 800 //Window width of a macro definition
#define WINDOW_HEIGHT 600 //Window height of a macro definition
#define WINDOW_TITLE L"【潭州教育】程序核心框架" //The window title of marco definition
// GLOBALS /////////////////////////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam ); //The process window function
// WINMAIN /////////////////////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//【1】 窗口创建四部曲之一:开始设计一个完整的窗口类
//【First】 One of the windows to create tetralogy:Start to design a whole window calss
WNDCLASSEX wndClass = { 0 }; //With WNDCALSSEX definition of a window class
wndClass.cbSize = sizeof(WNDCLASSEX); //Number of bytes to set up the structure size
wndClass.style = CS_HREDRAW | CS_VREDRAW; //Set the window style
wndClass.lpfnWndProc = WndProc; //Set the pointer to the window procedure function
wndClass.cbClsExtra = 0; //Additional memory window class
wndClass.cbWndExtra = 0; //Additional memory window
wndClass.hInstance = hInstance; //Specified contains the window handle to an instance of the process of program
//Local load custom ico
wndClass.hIcon = (HICON)::LoadImage(NULL, L"icon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); //Specifies the window handle of the cursor
wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); //The handle of Brush
wndClass.lpszMenuName = NULL; //Specify the menu resource
wndClass.lpszClassName = L"ForTheDreamOfGameDevelop";
//【2】窗口创建四部曲之二:注册窗口类
//【Second】 Second of the window to create tetralogy: RegisterClass
if (!RegisterClassEx(&wndClass))
return -1;
//【3】窗口创建四部曲之三:正式创建窗口
//【Third】 Third of the window to create tetralogy:Formal creation window
HWND hwnd = CreateWindow(L"ForTheDreamOfGameDevelop",
WINDOW_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
//【4】窗口创建四部曲之四:窗口移动、显示与更新
//【Four】 Fourth of the window to create tetralogy:Windows moblie,display and update
MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true); //In the left corner of the window(250,70);
ShowWindow(hwnd,nShowCmd); //Display window
UpdateWindow(hwnd); //Update window
//【5】消息循环过程
//windows information rotation
msg msg = { 0 }; //init msg
while (msg.message != WM_QUIT)
{
//查看应用程序消息队列,有消息时将队列中消息派发出去
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg); //将虚拟消息转换为字符信息
DispatchMessage(&msg); //分发一个消息给窗口程序
}
}
//【6】窗口类的注销
//【Sixth】 logout window class
UnregisterClass(L"ForDearmOfGameClassDevelop", wndClass.hInstance);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT: //重绘消息
ValidateRect(hwnd, NULL); //更新客户的显示
break;
case WM_KEYDOWN: //若是键盘按下任意键消息
if (wParam == VK_ESCAPE) //如果按下的是ESC键
DestroyWindow(hwnd); //销毁窗口,并发送一条WM_DESTORY消息
break;
case WM_DESTROY: //若是窗口销毁消息
PostQuitMessage(0); //像系统表示有个线程有终止请求,用来响应WM_DESTROY消息
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam); //调用默认的窗口过程
}
return 0;
}
以上,就是游戏框架设计的源程序,希望对你们有所帮助,当然,你们如果感兴趣可以运行试试,如果有什么问题,可以加群:558502932,把问题发到群里,或者找群内管理问一下,会帮你解答的。
希望你们学习C/C++都能学有所成。