added DPI support for universal app

This commit is contained in:
Dale Stammen 2014-10-27 11:07:00 -07:00
parent 6fb69870f9
commit 2eccd58d45
7 changed files with 48 additions and 25 deletions

View File

@ -35,14 +35,15 @@ using namespace Windows::Graphics::Display;
USING_NS_CC;
Cocos2dRenderer::Cocos2dRenderer(const int width, const int height, CoreDispatcher^ dispatcher, Panel^ panel)
: mApp(nullptr)
, mWidth(width)
, mHeight(height)
Cocos2dRenderer::Cocos2dRenderer(int width, int height, float dpi, CoreDispatcher^ dispatcher, Panel^ panel)
: m_app(nullptr)
, m_width(width)
, m_height(height)
, m_dpi(dpi)
, m_dispatcher(dispatcher)
, m_panel(panel)
{
mApp = new AppDelegate();
m_app = new AppDelegate();
auto director = cocos2d::Director::getInstance();
GLViewImpl* glview = GLViewImpl::create("Test Cpp");
@ -55,19 +56,25 @@ Cocos2dRenderer::Cocos2dRenderer(const int width, const int height, CoreDispatch
Cocos2dRenderer::~Cocos2dRenderer()
{
delete mApp;
delete m_app;
}
// Draws a basic triangle
void Cocos2dRenderer::Draw(GLsizei width, GLsizei height)
void Cocos2dRenderer::Draw(GLsizei width, GLsizei height, float dpi)
{
if (width != mWidth || height != mHeight)
if (width != m_width || height != m_height)
{
mWidth = width;
mHeight = height;
m_width = width;
m_height = height;
GLViewImpl::sharedOpenGLView()->UpdateForWindowSizeChange(static_cast<float>(width), static_cast<float>(height));
}
if (dpi != m_dpi)
{
m_dpi = dpi;
GLViewImpl::sharedOpenGLView()->UpdateDPI(m_dpi);
}
GLViewImpl::sharedOpenGLView()->ProcessEvents();
GLViewImpl::sharedOpenGLView()->Render();
}

View File

@ -29,18 +29,20 @@ namespace cocos2d
class Cocos2dRenderer
{
public:
Cocos2dRenderer(const int width, const int height, Windows::UI::Core::CoreDispatcher^ dispathcer, Windows::UI::Xaml::Controls::Panel^ panel);
Cocos2dRenderer( int width, int height, float dpi, Windows::UI::Core::CoreDispatcher^ dispathcer, Windows::UI::Xaml::Controls::Panel^ panel);
~Cocos2dRenderer();
void Draw(GLsizei width, GLsizei height);
void Draw(GLsizei width, GLsizei height, float dpi);
void QueuePointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args);
void QueueKeyBoardEvent(Cocos2dKeyEvent type, Windows::UI::Core::KeyEventArgs^ e);
private:
int mWidth;
int mHeight;
int m_width;
int m_height;
float m_dpi;
// The AppDelegate for the Cocos2D app
AppDelegate* mApp;
AppDelegate* m_app;
Platform::Agile<Windows::UI::Core::CoreDispatcher> m_dispatcher;
Platform::Agile<Windows::UI::Xaml::Controls::Panel> m_panel;
};

View File

@ -47,7 +47,8 @@ OpenGLESPage::OpenGLESPage(OpenGLES* openGLES) :
mRenderSurface(EGL_NO_SURFACE),
mCustomRenderSurfaceSize(0,0),
mUseCustomRenderSurfaceSize(false),
m_coreInput(nullptr)
m_coreInput(nullptr),
m_dpi(0.0f)
{
InitializeComponent();
@ -238,16 +239,20 @@ void OpenGLESPage::StartRenderLoop()
GLsizei panelHeight = 0;
GetSwapChainPanelSize(&panelWidth, &panelHeight);
DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
m_dpi = currentDisplayInformation->LogicalDpi;
if (m_renderer.get() == nullptr)
{
m_renderer = std::make_shared<Cocos2dRenderer>(panelWidth, panelHeight, dispatcher, swapChainPanel);
m_renderer = std::make_shared<Cocos2dRenderer>(panelWidth, panelHeight, m_dpi, dispatcher, swapChainPanel);
}
while (action->Status == Windows::Foundation::AsyncStatus::Started)
{
GetSwapChainPanelSize(&panelWidth, &panelHeight);
m_renderer.get()->Draw(panelWidth, panelHeight);
m_renderer.get()->Draw(panelWidth, panelHeight, m_dpi);
// The call to eglSwapBuffers might not be successful (i.e. due to Device Lost)
// If the call fails, then we must reinitialize EGL and the GL resources.

View File

@ -67,5 +67,7 @@ namespace cocos2d
void OnPointerPressed(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);
void OnPointerMoved(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);
void OnPointerReleased(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);
float m_dpi;
};
}

View File

@ -42,8 +42,12 @@ CCFreeTypeFont sFT;
int Device::getDPI()
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
static const float dipsPerInch = 96.0f;
return floor(DisplayProperties::LogicalDpi / dipsPerInch + 0.5f); // Round to nearest integer.
#else
return cocos2d::GLViewImpl::sharedOpenGLView()->GetDPI();
#endif
}
static Accelerometer^ sAccelerometer = nullptr;

View File

@ -108,9 +108,10 @@ bool GLViewImpl::initWithFullScreen(const std::string& viewName)
}
bool GLViewImpl::Create(float width, float height, DisplayOrientations orientation)
bool GLViewImpl::Create(float width, float height, float dpi, DisplayOrientations orientation)
{
m_orientation = orientation;
m_dpi = dpi;
UpdateForWindowSizeChange(width, height);
return true;
}

View File

@ -35,8 +35,8 @@ THE SOFTWARE.
#include <agile.h>
#include <concurrent_queue.h>
#include <string>
#include <memory>
#include <wrl/client.h>
#include <queue>
#include <Keyboard-winrt.h>
NS_CC_BEGIN
@ -61,7 +61,7 @@ public:
virtual void setIMEKeyboardState(bool bOpen);
virtual void setIMEKeyboardState(bool bOpen, std::string str);
virtual bool Create(float width, float height ,Windows::Graphics::Display::DisplayOrientations orientation);
virtual bool Create(float width, float height, float dpi, Windows::Graphics::Display::DisplayOrientations orientation);
void setDispatcher(Windows::UI::Core::CoreDispatcher^ dispatcher);
Windows::UI::Core::CoreDispatcher^ getDispatcher() {return m_dispatcher.Get();}
@ -97,8 +97,11 @@ public:
void centerWindow();
void UpdateOrientation(Windows::Graphics::Display::DisplayOrientations orientation);
void UpdateForWindowSizeChange(float width, float height);
void UpdateForWindowSizeChange(float width, float height);
void SetDPI(float dpi) { m_dpi = dpi; }
float GetDPI() { return m_dpi; }
// static function
/**
@brief get the shared main open gl window
@ -141,6 +144,7 @@ private:
float m_width;
float m_height;
float m_dpi;
Windows::Graphics::Display::DisplayOrientations m_orientation;
Windows::Foundation::Rect m_keyboardRect;
@ -157,8 +161,6 @@ private:
Cocos2dEditBoxDelegate^ m_editBoxDelegate;
Concurrency::concurrent_queue<std::shared_ptr<InputEvent>> mInputEvents;
//std::queue<std::shared_ptr<InputEvent>> mInputEvents;
//Concurrency::critical_section m_criticalSection;
Platform::Agile<Windows::UI::Core::CoreDispatcher> m_dispatcher;
Platform::Agile<Windows::UI::Xaml::Controls::Panel> m_panel;