我大三选的游戏方向课。现在开始上游戏的专业课了,所以决定写一个VC++ game开发系列的博客。从头开始记录VC++ game学习过程中的一点一滴。
该系列博客只讲诉怎么实现一些要求,至于为什么,以及知识点的延伸,这里不会涉及。就是傻瓜操作,我称之为,SOS:simple operation style一:创建一个空工程,显示出一个空窗口1:文件->新建项目->VC++ win32项目->设定项目名称和路径->下一步,选择窗口应用程序,空项目:ok,空项目建立成功。2:小细节:防止字符串报错以及MFC类库的使用:项目右键->属性->配置属性下的常规-->字符集设置成:未设置 。MFC的使用:静态和dll动态使用都可以。3:使用MFC类库的CFrameWnd和CWinApp作为基类实现空窗口1)项目右键添加新建项--》选择头文件,头文件名为:stdafx,在文件里面只写一行:#include<afxwin.h> 2)工具栏->项目-》添加类-》MFC类-》这时可能报错,点击确定略过-》填写类名CMyWnd-》基类选择CFrameWnd,->确定。工程自动生成头文件和资源文件.将头文件里面的构造函数钱的protected改成public资源文件中做如下操作:详见代码区:3)同步骤2在创建一个基于CWinApp的类为CMyApp将头文件里面的构造函数钱的protected改成public资源文件中做如下操作:详见代码区:步骤就是上面这些,代码如下:stdafx.h#include<afxwin.h>CMyWnd.h:#pragma once // CMyWnd 框架 class CMyWnd : public CFrameWnd { DECLARE_DYNCREATE(CMyWnd) public: CMyWnd(); // 动态创建所使用的受保护的构造函数 virtual ~CMyWnd(); protected: DECLARE_MESSAGE_MAP() private: RECT *windowRect; public: afx_msg void OnPaint(); private: CDC *cDc; };
CMyWnd.cpp:
// MyWnd.cpp : 实现文件 // #include "stdafx.h" #include "MyWnd.h" // CMyWnd IMPLEMENT_DYNCREATE(CMyWnd, CFrameWnd) CMyWnd::CMyWnd() : cDc(NULL) { Create(NULL,"window"); CClientDC dc(this); //beside CClientDc ,there are another two CDC'child class: CWindowDC(draw everywhere you want ) CPaintDC(only in OnPaint()function) GetClientRect(windowRect);//the show view //GetWindowRect(windowRect); the whole view //dc.TextOutA(100,100,"GOOD",5); have a problem int width=dc.GetDeviceCaps(HORZRES);//get the decice's width int height=dc.GetDeviceCaps(VERTRES); width=(width-800)/2; height=(height-600)/2; MoveWindow(width,height,800,600); // cDc=new CDC; } CMyWnd::~CMyWnd() { } BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd) ON_WM_PAINT() END_MESSAGE_MAP() // CMyWnd 消息处理程序 void CMyWnd::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CFrameWnd::OnPaint() }
CMyApp.h:
#pragma once // CMyApp class CMyApp : public CWinApp { DECLARE_DYNCREATE(CMyApp) public: CMyApp(); // 动态创建所使用的受保护的构造函数 virtual ~CMyApp(); public: virtual BOOL InitInstance(); virtual int ExitInstance(); protected: DECLARE_MESSAGE_MAP() };
CMyApp.cpp:
// MyApp.cpp : 实现文件 // #include "stdafx.h" #include "MyApp.h" #include"MyWnd.h" // CMyApp IMPLEMENT_DYNCREATE(CMyApp, CWinApp) CMyApp app; CMyApp::CMyApp() { } CMyApp::~CMyApp() { } BOOL CMyApp::InitInstance() { //the below three lines can,t be intialized in constructor CMyWnd * Frame=new CMyWnd; Frame->ShowWindow(m_nCmdShow); this->m_pMainWnd=Frame; // TODO: 在此执行任意逐线程初始化 return TRUE; } int CMyApp::ExitInstance() { // TODO: 在此执行任意逐线程清理 return CWinApp::ExitInstance(); } BEGIN_MESSAGE_MAP(CMyApp, CWinApp) END_MESSAGE_MAP() // CMyApp 消息处理程序