• MFC中显示cocos2dx


    存在的问题:没实现立即动态更改分辨率,而是通过缩放改变窗口的大小:
    void CAnimateMakerView::OnSize(UINT nType, int cx, int cy)
    {
    CFormView::OnSize(nType, cx, cy);
    #if 1
    if (::IsWindow(m_picture.m_hWnd))//&&m_bInitCoco)
    {
    CRect rect;
    m_picture.GetWindowRect(rect);
    ScreenToClient(rect);
    rect.right = cx - rect.left;
    rect.bottom = cy - rect.left;
    m_picture.MoveWindow(rect);
    if (m_bInitCoco)
    {
    m_picture.resize(rect.Width(),rect.Height());
    printf("cocos2d size=%dX%d",rect.Width(),rect.Height());
    //CCDirector::sharedDirector()->reshapeProjection(CCSizeMake(rect.Width(),rect.Height()));
    }
    }
    #endif
    }


    Cocos2dXWin.cpp
    // Cocos2dXWin.cpp : 实现文件
    //

    #include "stdafx.h"
    #include "AnimateMaker.h"
    #include "Cocos2dXWin.h"
    #include "CCEGLView.h"
    #include "CCApplication.h"
    #include "AppDelegate.h"


    // CCocos2dXWin

    IMPLEMENT_DYNAMIC(CCocos2dXWin, CWnd)

    CCocos2dXWin::CCocos2dXWin()
    {
        m_iWidth=0;
        m_iHeight=0;
    }

    CCocos2dXWin::~CCocos2dXWin()
    {
    }


    BEGIN_MESSAGE_MAP(CCocos2dXWin, CWnd)
    END_MESSAGE_MAP()

     

    // CCocos2dXWin 消息处理程序

    BOOL    CCocos2dXWin::CreateCocos2dXWindow() 

        //新建一个CRect变量获取窗口的客户区大小 
        CRect   tClientRect; 
        GetClientRect(&tClientRect); 
        //调用cocos2d中的CCApplication::sharedApplication()获取Cocos2d-x程序类单件实例对象。调用其run函数。在这里我们传入四个参数,与第一章“HelloWorld”不同的是这里增加了新参数“当前窗口的句柄”。这是使用MFC来嵌入Cocos2d-x窗口的关健所在。我们暂时埋个伏笔,后面再详解。 

        //resize(tClientRect.Width(),tClientRect.Height());
        CCEGLView* eglView = CCEGLView::sharedOpenGLView();
        m_iWidth=tClientRect.Width();
        m_iHeight=tClientRect.Height();

        eglView->setFrameSize(m_iWidth,m_iHeight, GetSafeHwnd());

        cocos2d::CCApplication::sharedApplication()->run(1);//GetSafeHwnd(),TEXT("第一个Cocos2d-x程序"),tClientRect.Width(),tClientRect.Height()); 
        //这里将变量设置为TRUE 

        m_bInitCocos2dX = TRUE; 
        return TRUE; 

    void CCocos2dXWin::resize(float width, float height)
    {
        if (m_bInitCocos2dX)
        {
            CCEGLView* eglView = CCEGLView::sharedOpenGLView();
            //eglView::CCEGLViewProtocol.setFrameSize(width, height);

            //eglView->setFrameSize(width, height, GetSafeHwnd());
            eglView->resize(width, height);
        //    float newScale = (float)(enabled ? 2 : 1);
            //eglView->setContentScaleFactor(5);
            float scaleX=width/m_iWidth;
            float scaleY=height/m_iHeight;
           
            //float scale=scaleY<scaleX?scaleY:scaleX;
            float scale=MIN(scaleY,scaleX);
            CCDirector::sharedDirector()->setContentScaleFactor(scale);

        }
    }
    Cocos2dXWin.h
    #pragma once


    // CCocos2dXWin
    // #include "CCEGLView_win32.h"
    // #include "Classes/AppDelegate.h"
    #include "cocos2d.h"
    #include "AppDelegate.h"
    class CCocos2dXWin : public CWnd
    {
    DECLARE_DYNAMIC(CCocos2dXWin)

    public:
    CCocos2dXWin();
    virtual ~CCocos2dXWin();
    //创建Cocos2dX窗口
    BOOL CreateCocos2dXWindow();
    AppDelegate app;
    void resize(float width, float height);
    protected:
    DECLARE_MESSAGE_MAP()

    //是否已经初始化
    BOOL m_bInitCocos2dX;
    float m_iWidth;
    float m_iHeight;

    };

     

    void CCEGLView::resize(int width, int height)
    {
    if (! m_hWnd)
    {
    return;
    }


    POINT ptDiff;

    // calculate new window width and height

    RECT rcClient;
    if (!m_bUseMfc)
    {
    RECT rcWindow;
    GetWindowRect(m_hWnd, &rcWindow);
    GetClientRect(m_hWnd, &rcClient);
    ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
    ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
    rcClient.right = rcClient.left + width;
    rcClient.bottom = rcClient.top + height;
    }

    m_windowWidth = width;
    m_windowHeight = height;
    const CCSize& frameSize = getFrameSize();
    if (frameSize.width > 0)
    {
    m_windowTouchScaleX = frameSize.width / width;
    m_windowTouchScaleY = frameSize.height / height;

    TCHAR buff[MAX_PATH + 1];
    memset(buff, 0, sizeof(buff));
    swprintf_s(buff, MAX_PATH, L"%s - %0.0fx%0.0f - %0.2f",
    kWindowClassName, frameSize.width, frameSize.height, 1.0f / m_windowTouchScaleX);
    SetWindowText(m_hWnd, buff);
    if (m_bUseMfc)
    {
    OutputDebugString(buff);
    OutputDebugString(_T("\n"));
    }
    }

    if (!m_bUseMfc)
    {
    AdjustWindowRectEx(&rcClient, GetWindowLong(m_hWnd, GWL_STYLE), false, GetWindowLong(m_hWnd, GWL_EXSTYLE));

    // change width and height
    SetWindowPos(m_hWnd, 0, 0, 0, width + ptDiff.x, height + ptDiff.y,
    SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
    }

    }


  • 相关阅读:
    谷歌浏览器network请求时间(stalled,DNS Lookup,Waiting)分析以及解决方案
    Maven项目下启动后Eclipse报错:org.springframework.web.context.ContextLoaderListener
    探讨ES6的import export default 和CommonJS的require module.exports
    Node.js+websocket+mongodb实现即时聊天室
    slider轮播插件的多种写法
    原生canvas写的飞机游戏
    vue父组件如何向子组件中传递数据?
    vue计算属性VS侦听属性
    vue等单页面应用优缺点
    vue自定义过滤器的创建与使用
  • 原文地址:https://www.cnblogs.com/iapp/p/3631748.html
Copyright © 2020-2023  润新知