C++ WIN32(如何使用游戏壳)

PlaneCtrl.h

#pragma once
#include "cgamectrl.h"
class CPlaneCtrl :
    public CGameCtrl
{
public:
    CPlaneCtrl(void);
    ~CPlaneCtrl(void);
public:
    DECLEAR()
public:
    virtual void OnGameDraw();
};

PlaneCtrl.cpp

#include "PlaneCtrl.h"

INPLEMENT(CPlaneCtrl)
CPlaneCtrl::CPlaneCtrl(void)
{
}


CPlaneCtrl::~CPlaneCtrl(void)
{
}
void CPlaneCtrl::OnGameDraw()
{


}

CGameCtrl.h

#pragma once
#include<Windows.h>

#ifndef _GAMECTRL_H_
#define _GAMECTRL_H_    

//=====================两个宏,一个初始化=============================//
#define DECLEAR()\
    static CGameCtrl* CreateObject();

#define INPLEMENT(ThisClass)\
    CGameCtrl* ThisClass::CreateObject()\
{\
    return  new ThisClass ;\
}\
    Init init##ThisClass(&ThisClass::CreateObject);
//=====================两个宏,一个初始化=============================//

class CGameCtrl;
typedef CGameCtrl* (*PFUN)();

class CGameCtrl
{
protected:
    HINSTANCE m_hInstance;
    HWND m_hWnd;
public:
    CGameCtrl()
    {
        m_hInstance=0;
        m_hWnd=0;
    }
    ~CGameCtrl()
    {

    }
public:
    void SetWindow(HINSTANCE instance,HWND wnd)
    {
        m_hInstance=instance;
        m_hWnd=wnd;
    }

public:
        static PFUN pfun;//静态函数指针
public:                             

    virtual void OnCreateGame(){}                       // WM_CREATE    
    virtual void OnGameDraw  (){}                       // WM_PAINT
    virtual void OnGameRun   (WPARAM nTimeId){}         // WM_TIMER 需要判断哪个定时器,传参数wParam [in]The timer identifier.
            //键盘
    virtual void OnKeyDown(WPARAM nKey){}               // WM_KEYDOWN
    virtual void OnKeyUp(WPARAM nKey){}                 // WM_KEYUP
            //鼠标
    virtual void OnLButtonDown(POINT point){}           // WM_LBUTTONDOWN   
    virtual void OnLButtonUp(POINT point){}             // WM_LBUTTONUP
    virtual void OnMouseMove(POINT point){}             // WM_MOUSEMOVE
};
class Init
{
public:
    Init(PFUN pfun)
    {
        CGameCtrl::pfun=pfun;

    }
};
#endif //_GAMECTRL_H_

CGameCtrl.cpp

#include<windows.h>
#include"CGameCtrl.h"
#include"PlaneCtrl.h"
#include<time.h>
PFUN CGameCtrl::pfun=0; //只有静态成员函数才能在类外定义
CGameCtrl *pCtrl=0;
LRESULT CALLBACK MyWinMain(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)//回调函数(消息的)
{
    switch(uMsg)
    {
    case WM_CREATE:
        if(CGameCtrl::pfun==NULL)   //在创建窗口之前需要先判断这个函数指针是否为空
        {
            ::MessageBox(hWnd,"亲注册失败了","提示",MB_OK);
            ::DestroyWindow(hWnd);  //销毁窗口
            ::PostQuitMessage(0);   //退出

        }else
        {
            pCtrl=(*CGameCtrl::pfun)(); //调用函数指针(指向的是子类的对象)通过子类对象去执行子类中的虚构函数
            pCtrl->SetWindow(LPCREATESTRUCT(lParam)->hInstance,hWnd);//设置句柄
            pCtrl->OnCreateGame();  //初始化游戏加载资源
        }
        break;
    case WM_PAINT:
        pCtrl->OnGameDraw();
        break;
    case WM_TIMER:
        pCtrl->OnGameRun(wParam);
        break;
    case WM_KEYDOWN:
        pCtrl->OnKeyDown(wParam);
        break;
    case WM_KEYUP:
        pCtrl->OnKeyUp(wParam);
        break;
    case WM_LBUTTONDOWN:
        {
            POINT point;
            point.x=LOWORD(lParam);
            point.y=HIWORD(lParam);
            pCtrl->OnLButtonDown(point);
        }
        break;
    case WM_LBUTTONUP:
        {
            POINT point;
            point.x=LOWORD(lParam);
            point.y=HIWORD(lParam);
            pCtrl->OnLButtonUp(point);
        }
        break;
    case WM_MOUSEMOVE:
        {
            POINT point;
            point.x=LOWORD(lParam);
            point.y=HIWORD(lParam);
            pCtrl->OnMouseMove(point);
        }
        break;

    case WM_CLOSE:          
        {
        ::PostQuitMessage(0);
        }

    }
    return  DefWindowProc(hWnd,uMsg,wParam,lParam); //返回消息的默认处理
}

int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
    srand((unsigned int)time(0));
    //设置画刷
    HBRUSH hBrush=::CreateSolidBrush(RGB(62,71,98));

    //设计窗口
    WNDCLASSEX wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbSize=sizeof(wndclass);
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=hBrush;
    wndclass.hCursor=0;
    wndclass.hIcon=0;
    wndclass.hIconSm=0;
    wndclass.lpfnWndProc=MyWinMain;
    wndclass.lpszClassName="Leo";   //注册窗口时候要的名字,作为参数   
    wndclass.lpszMenuName=0;        //菜单名字
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.hInstance=hInstance;

    //注册窗口

    if(RegisterClassEx(&wndclass)== false)
    {
        MessageBox(NULL,"注册失败","提示",MB_OK);
        return 0;
    }
    //创建
    HWND hWnd=::CreateWindow("Leo","Small Test",WS_OVERLAPPEDWINDOW,0,200,500,500,NULL,NULL,hInstance,NULL);


    //显示窗口

    ShowWindow(hWnd,nCmdShow);

    //消息循环
    MSG msg;
    while(::GetMessage(&msg,0,0,0))
    {

        ::TranslateMessage(&msg);   //将得到的消息翻译
        ::DispatchMessage(&msg);    //翻译来的消息发送

    }

    return 0;
}
阅读更多
版权声明:18年毕业,在读硕士,自学c/c++ 邮箱:Lovesing@163.com https://blog.csdn.net/Loversing/article/details/54564122
文章标签: win32
个人分类: (C++)WIN32
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭