Display cocos2d-x in MFC

Source: Internet
Author: User

There is a problem: the resolution is not changed dynamically immediately, but the window size is changed by scaling:
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: implementation file
//

# 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 Message Processing Program

BOOL CCocos2dXWin: CreateCocos2dXWindow ()
{
// Create a new CRect variable to obtain the size of the customer zone in the window
CRect tClientRect;
GetClientRect (& tClientRect );
// Call CCApplication: sharedApplication () in cocos2d to get a single-piece Instance Object of the Cocos2d-x program class. Call its run function. Here we pass in four parameters. Different from Chapter 1 "HelloWorld", here we add the new parameter "current window handle ". This is the key to embedding a Cocos2d-x window with MFC. We will provide a foreshadowing for the moment, and I will explain it later.

// Resize (tClientRect. Width (), tClientRect. Height ());
CCEGLView * eglView = CCEGLView: Export dopenglview ();
M_iWidth = tClientRect. Width ();
M_iHeight = tClientRect. Height ();

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

Cocos2d: CCApplication: sharedApplication ()-> run (1); // GetSafeHwnd (), TEXT ("first Cocos2d-x program"), tClientRect. width (), tClientRect. height ());
// Set the variable to TRUE here

M_bInitCocos2dX = TRUE;
Return TRUE;
}

Void CCocos2dXWin: resize (float width, float height)
{
If (m_bInitCocos2dX)
{
CCEGLView * eglView = CCEGLView: Export dopenglview ();
// 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 ();
// Create the Cocos2dX window
BOOL CreateCocos2dXWindow ();
AppDelegate app;
Void resize (float width, float height );
Protected:
DECLARE_MESSAGE_MAP ()

// Whether initialization has been performed
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_1_wwidth = 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 );
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.