Moved to VS2013 and latest SpriteBatch

This commit is contained in:
xawotihs
2014-12-14 22:04:31 +01:00
parent f5702dbdc5
commit 0934b0a27a
67 changed files with 5541 additions and 3287 deletions
+211 -135
View File
@@ -7,6 +7,10 @@
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
//
//-------------------------------------------------------------------------------------
#include "PrecompiledHeader.h"
#undef DebugTrace
#if (!defined IOS) && (!defined QT_CONFIG)
#if (defined WIN32) && (!defined WP8)
#pragma warning(disable : 4786)
@@ -21,14 +25,17 @@
#include <ppl.h>
#include <ppltasks.h>
#include <agile.h>
#include "BasicSprites.h"
#include <d2d1_1.h>
using namespace BasicSprites;
//using namespace BasicSprites;
using namespace Microsoft::WRL;
using namespace DirectX;
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
using namespace Windows::Graphics::Display;
#include "SpriteBatch.h"
#include "CommonStates.h"
#include "PlatformHelpers.h"
using namespace DirectX;
#endif
#include <png.h>
@@ -57,42 +64,45 @@ extern "C" {
#endif
#endif
typedef float4x4 ESMatrix;
//typedef float4x4 ESMatrix;
typedef float GLfloat;
typedef struct {
GLfloat m[4][4];
} ESMatrix;
void esMatrixLoadIdentity(ESMatrix *result)
{
memset(result, 0x0, sizeof(ESMatrix));
(*result)[0][0] = 1.0f;
(*result)[1][1] = 1.0f;
(*result)[2][2] = 1.0f;
(*result)[3][3] = 1.0f;
result->m[0][0] = 1.0f;
result->m[1][1] = 1.0f;
result->m[2][2] = 1.0f;
result->m[3][3] = 1.0f;
}
void esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz)
{
(*result)[0][0] *= sx;
(*result)[0][1] *= sx;
(*result)[0][2] *= sx;
(*result)[0][3] *= sx;
result->m[0][0] *= sx;
result->m[0][1] *= sx;
result->m[0][2] *= sx;
result->m[0][3] *= sx;
(*result)[1][0] *= sy;
(*result)[1][1] *= sy;
(*result)[1][2] *= sy;
(*result)[1][3] *= sy;
result->m[1][0] *= sy;
result->m[1][1] *= sy;
result->m[1][2] *= sy;
result->m[1][3] *= sy;
(*result)[2][0] *= sz;
(*result)[2][1] *= sz;
(*result)[2][2] *= sz;
(*result)[2][3] *= sz;
result->m[2][0] *= sz;
result->m[2][1] *= sz;
result->m[2][2] *= sz;
result->m[2][3] *= sz;
}
void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz)
{
(*result)[3][0] += ((*result)[0][0] * tx + (*result)[1][0] * ty + (*result)[2][0] * tz);
(*result)[3][1] += ((*result)[0][1] * tx + (*result)[1][1] * ty + (*result)[2][1] * tz);
(*result)[3][2] += ((*result)[0][2] * tx + (*result)[1][2] * ty + (*result)[2][2] * tz);
(*result)[3][3] += ((*result)[0][3] * tx + (*result)[1][3] * ty + (*result)[2][3] * tz);
result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[2][0] * tz);
result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[2][1] * tz);
result->m[3][2] += (result->m[0][2] * tx + result->m[1][2] * ty + result->m[2][2] * tz);
result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[2][3] * tz);
}
void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB)
@@ -102,25 +112,25 @@ void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB)
for (i=0; i<4; i++)
{
tmp[i][0] = ((*srcA)[i][0] * (*srcB)[0][0]) +
((*srcA)[i][1] * (*srcB)[1][0]) +
((*srcA)[i][2] * (*srcB)[2][0]) +
((*srcA)[i][3] * (*srcB)[3][0]) ;
tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) +
(srcA->m[i][1] * srcB->m[1][0]) +
(srcA->m[i][2] * srcB->m[2][0]) +
(srcA->m[i][3] * srcB->m[3][0]) ;
tmp[i][1] = ((*srcA)[i][0] * (*srcB)[0][1]) +
((*srcA)[i][1] * (*srcB)[1][1]) +
((*srcA)[i][2] * (*srcB)[2][1]) +
((*srcA)[i][3] * (*srcB)[3][1]) ;
tmp.m[i][1] = (srcA->m[i][0] * srcB->m[0][1]) +
(srcA->m[i][1] * srcB->m[1][1]) +
(srcA->m[i][2] * srcB->m[2][1]) +
(srcA->m[i][3] * srcB->m[3][1]) ;
tmp[i][2] = ((*srcA)[i][0] * (*srcB)[0][2]) +
((*srcA)[i][1] * (*srcB)[1][2]) +
((*srcA)[i][2] * (*srcB)[2][2]) +
((*srcA)[i][3] * (*srcB)[3][2]) ;
tmp.m[i][2] = (srcA->m[i][0] * srcB->m[0][2]) +
(srcA->m[i][1] * srcB->m[1][2]) +
(srcA->m[i][2] * srcB->m[2][2]) +
(srcA->m[i][3] * srcB->m[3][2]) ;
tmp[i][3] = ((*srcA)[i][0] * (*srcB)[0][3]) +
((*srcA)[i][1] * (*srcB)[1][3]) +
((*srcA)[i][2] * (*srcB)[2][3]) +
((*srcA)[i][3] * (*srcB)[3][3]) ;
tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) +
(srcA->m[i][1] * srcB->m[1][3]) +
(srcA->m[i][2] * srcB->m[2][3]) +
(srcA->m[i][3] * srcB->m[3][3]) ;
}
/*
* Actually, srcA and srcB are column-major order matrixes, while they
@@ -183,25 +193,25 @@ void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
// Note: matrixes in OpenGL ES are stored in column-major order!
rotMat[0][0] = (oneMinusCos * xx) + cosAngle;
rotMat[1][0] = (oneMinusCos * xy) - zs;
rotMat[2][0] = (oneMinusCos * zx) + ys;
rotMat[3][0] = 0.0F;
rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle;
rotMat.m[1][0] = (oneMinusCos * xy) - zs;
rotMat.m[2][0] = (oneMinusCos * zx) + ys;
rotMat.m[3][0] = 0.0F;
rotMat[0][1] = (oneMinusCos * xy) + zs;
rotMat[1][1] = (oneMinusCos * yy) + cosAngle;
rotMat[2][1] = (oneMinusCos * yz) - xs;
rotMat[3][1] = 0.0F;
rotMat.m[0][1] = (oneMinusCos * xy) + zs;
rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle;
rotMat.m[2][1] = (oneMinusCos * yz) - xs;
rotMat.m[3][1] = 0.0F;
rotMat[0][2] = (oneMinusCos * zx) - ys;
rotMat[1][2] = (oneMinusCos * yz) + xs;
rotMat[2][2] = (oneMinusCos * zz) + cosAngle;
rotMat[3][2] = 0.0F;
rotMat.m[0][2] = (oneMinusCos * zx) - ys;
rotMat.m[1][2] = (oneMinusCos * yz) + xs;
rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle;
rotMat.m[3][2] = 0.0F;
rotMat[0][3] = 0.0F;
rotMat[1][3] = 0.0F;
rotMat[2][3] = 0.0F;
rotMat[3][3] = 1.0F;
rotMat.m[0][3] = 0.0F;
rotMat.m[1][3] = 0.0F;
rotMat.m[2][3] = 0.0F;
rotMat.m[3][3] = 1.0F;
esMatrixMultiply( result, &rotMat, result );
}
@@ -218,12 +228,12 @@ void esOrtho(ESMatrix *result, float left, float right, float bottom, float top,
return;
esMatrixLoadIdentity(&ortho);
ortho[0][0] = 2.0f / deltaX;
ortho[3][0] = -(right + left) / deltaX;
ortho[1][1] = 2.0f / deltaY;
ortho[3][1] = -(top + bottom) / deltaY;
ortho[2][2] = -2.0f / deltaZ;
ortho[3][2] = -(nearZ + farZ) / deltaZ;
ortho.m[0][0] = 2.0f / deltaX;
ortho.m[3][0] = -(right + left) / deltaX;
ortho.m[1][1] = 2.0f / deltaY;
ortho.m[3][1] = -(top + bottom) / deltaY;
ortho.m[2][2] = -2.0f / deltaZ;
ortho.m[3][2] = -(nearZ + farZ) / deltaZ;
esMatrixMultiply(result, &ortho, result);
}
@@ -282,25 +292,24 @@ void JQuad::SetHotSpot(float x, float y)
}
JRenderer* JRenderer::mInstance = NULL;
BasicSprites::SpriteBatch^ mspriteBatchInstance;
bool JRenderer::m3DEnabled = false;
//////////////////////////////////////////////////////////////////////////
JTexture::JTexture() : mBuffer(NULL)
{
mTexId = (ID3D11Texture2D*)-1;
mTexId = (ID3D11ShaderResourceView*)0;
}
JTexture::~JTexture()
{
if (mBuffer)
{
if(mTexId)
mTexId->Release();
delete [] mBuffer;
mBuffer = NULL;
}
mspriteBatchInstance->RemoveTexture(mTexId);
}
@@ -318,8 +327,6 @@ JRenderer* JRenderer::GetInstance()
if (mInstance == NULL)
{
mInstance = new JRenderer();
mspriteBatchInstance = ref new SpriteBatch();
JASSERT(mInstance != NULL);
mInstance->InitRenderer();
@@ -336,7 +343,6 @@ void JRenderer::Destroy()
mInstance->DestroyRenderer();
delete mInstance;
mInstance = NULL;
mspriteBatchInstance = nullptr;
}
}
@@ -387,7 +393,7 @@ void JRenderer::InitRenderer()
// Create the Direct3D 11 API device object and a corresponding context.
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
D3D11CreateDevice(
nullptr, // Specify nullptr to use the default adapter.
D3D_DRIVER_TYPE_HARDWARE,
@@ -403,17 +409,15 @@ void JRenderer::InitRenderer()
);
// Get the Direct3D 11.1 API device and context interfaces.
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
device.As(&m_d3dDevice)
);
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
context.As(&m_d3dContext)
);
mspriteBatchInstance->Initialize(
m_d3dDevice.Get(), 10024
);
m_spriteBatch = new SpriteBatch(m_d3dContext.Get());
}
void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight)
@@ -434,7 +438,7 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
if(m_swapChain != nullptr)
{
// If the swap chain already exists, resize it.
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
m_swapChain->ResizeBuffers(
2, // Double-buffered swap chain.
static_cast<UINT>(mWindowWidth),
@@ -461,17 +465,17 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
swapChainDesc.Flags = 0;
ComPtr<IDXGIDevice1> dxgiDevice;
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
m_d3dDevice.As(&dxgiDevice)
);
ComPtr<IDXGIAdapter> dxgiAdapter;
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
dxgiDevice->GetAdapter(&dxgiAdapter)
);
ComPtr<IDXGIFactory2> dxgiFactory;
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
dxgiAdapter->GetParent(
__uuidof(IDXGIFactory2),
&dxgiFactory
@@ -479,7 +483,7 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
);
// Windows::UI::Core::CoreWindow* window = (Windows::UI::Core::CoreWindow*)window.Get();
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
dxgiFactory->CreateSwapChainForCoreWindow(
m_d3dDevice.Get(),
m_Window.Get(),
@@ -491,14 +495,14 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
// Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
// ensures that the application will only render after each VSync, minimizing power consumption.
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
dxgiDevice->SetMaximumFrameLatency(1)
);
}
// Create a render target view of the swap chain back buffer.
ComPtr<ID3D11Texture2D> backBuffer;
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
m_swapChain->GetBuffer(
0,
__uuidof(ID3D11Texture2D),
@@ -506,7 +510,7 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
)
);
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
m_d3dDevice->CreateRenderTargetView(
backBuffer.Get(),
nullptr,
@@ -529,7 +533,7 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
);
ComPtr<ID3D11Texture2D> depthStencil;
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
m_d3dDevice->CreateTexture2D(
&depthStencilDesc,
nullptr,
@@ -538,7 +542,7 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
);
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);
DX::ThrowIfFailed(
DirectX::ThrowIfFailed(
m_d3dDevice->CreateDepthStencilView(
depthStencil.Get(),
&depthStencilViewDesc,
@@ -561,6 +565,9 @@ void JRenderer::OnWindowsSizeChanged(void* window, float inWidth, float inHeight
void JRenderer::DestroyRenderer()
{
if(m_spriteBatch)
delete m_spriteBatch;
m_spriteBatch = NULL;
}
void JRenderer::BeginScene()
@@ -576,14 +583,15 @@ void JRenderer::BeginScene()
reinterpret_cast<float*>(&D2D1::ColorF(D2D1::ColorF::MidnightBlue))
);
mspriteBatchInstance->Begin();
CommonStates states(m_d3dDevice.Get());
m_spriteBatch->Begin(SpriteSortMode_Deferred, states.NonPremultiplied());
}
void JRenderer::EndScene()
{
mspriteBatchInstance->End();
m_spriteBatch->End();
// The application may optionally specify "dirty" or "scroll"
// rects to improve efficiency in certain scenarios.
@@ -623,7 +631,7 @@ void JRenderer::EndScene()
}
else
{
DX::ThrowIfFailed(hr);
DirectX::ThrowIfFailed(hr);
}
}
@@ -654,66 +662,127 @@ void JRenderer::RenderQuad(JQuad* quad, float xo, float yo, float angle, float x
float x = -quad->mHotSpotX;
float y = quad->mHotSpotY;
mspriteBatchInstance->Draw(
quad->mTex->mTexId,
float2(
// ((xo+((width*xScale)/2))*GetActualWidth())/SCREEN_WIDTH_F,
// ((yo+((height*yScale)/2))*GetActualHeight())/SCREEN_HEIGHT_F),
((xo)*GetActualWidth())/SCREEN_WIDTH_F,
((yo)*GetActualHeight())/SCREEN_HEIGHT_F),
PositionUnits::Pixels,
float2(
(width*xScale*GetActualWidth())/SCREEN_WIDTH_F,
(height*yScale*GetActualHeight())/SCREEN_HEIGHT_F
),
SizeUnits::Pixels,
float4(
quad->mColor[0].r/255.0f,
quad->mColor[0].g/255.0f,
quad->mColor[0].b/255.0f,
quad->mColor[0].a/255.0f),
-angle,
BlendMode::Alpha,
float4(quad->mTX0, quad->mTX1, quad->mTY0, quad->mTY1),
Vector4<unsigned int>(quad->mColor[0].color, quad->mColor[1].color, quad->mColor[2].color, quad->mColor[3].color)
XMVECTORF32 position = {
((xo)*GetActualWidth())/SCREEN_WIDTH_F, ((yo)*GetActualHeight())/SCREEN_HEIGHT_F, 0, 0
};
XMVECTORF32 origin = {
-x, y, 0, 0
};
XMVECTORF32 color = {
quad->mColor[0].r/255.0f, quad->mColor[0].g/255.0f, quad->mColor[0].b/255.0f, quad->mColor[0].a/255.0f
};
RECT rect;
rect.bottom = quad->mY + height;
rect.top = quad->mY;
rect.left = quad->mX;;
rect.right = quad->mX + width;
XMVECTOR scale = {xScale*GetActualWidth()/SCREEN_WIDTH_F, yScale*GetActualHeight()/SCREEN_HEIGHT_F};
m_spriteBatch->Draw(
quad->mTex->mTexId,
// position
position,
// sourceRectangle
&rect,
// color
color,
// rotation
angle,
// origin
origin,
//scale
scale
);
}
void JRenderer::RenderQuad(JQuad* quad, VertexColor* pt)
{
{/*
float width = quad->mWidth;
float height = quad->mHeight;
float x = -quad->mHotSpotX;
float y = quad->mHotSpotY;
mspriteBatchInstance->Draw(
quad->mTex->mTexId,
float2(
((pt[0].x)*GetActualWidth())/SCREEN_WIDTH_F,
((pt[0].y)*GetActualHeight())/SCREEN_HEIGHT_F),
PositionUnits::Pixels,
float2(
(width*GetActualWidth())/SCREEN_WIDTH_F,
(height*GetActualHeight())/SCREEN_HEIGHT_F
),
SizeUnits::Pixels,
float4(
quad->mColor[0].r/255.0f,
quad->mColor[0].g/255.0f,
quad->mColor[0].b/255.0f,
quad->mColor[0].a/255.0f),
FXMVECTOR position = { 0, 0 };
FXMVECTOR origin = {
-x, y, 0, 0
};
FXMVECTOR color = {
pt[0].color, pt[1].color, pt[2].color, pt[3].color
};
RECT rect;
rect.bottom = quad->mY + height;
rect.top = quad->mY;
rect.left = quad->mX;
rect.right = quad->mX + width;
XMVECTOR scale = {1*GetActualWidth()/SCREEN_WIDTH_F, 1*GetActualHeight()/SCREEN_HEIGHT_F};
m_spriteBatch->Draw(
quad->mTex->mTexId,
// position
position,
// sourceRectangle
&rect,
// color
color,
// rotation
0,
BlendMode::Alpha,
float4(quad->mTX0, quad->mTX1, quad->mTY0, quad->mTY1),
Vector4<unsigned int>(pt[0].color, pt[1].color, pt[2].color, pt[3].color)
);
// origin
origin,
//scale
scale
);*/
}
void JRenderer::FillRect(float x, float y, float width, float height, PIXEL_TYPE color)
{
{/*
FXMVECTOR position = {
((x)*GetActualWidth())/SCREEN_WIDTH_F, ((y)*GetActualHeight())/SCREEN_HEIGHT_F, 0, 0
};
FXMVECTOR origin = {
0, 0, 0, 0
};
JColor col;
col.color = color;
FXMVECTOR colorVector = {
col.r/255.0f, col.g/255.0f, col.b/255.0f, col.a/255.0f
};
RECT rect;
rect.bottom = height;
rect.top = 0;
rect.left = 0;
rect.right = width;
XMVECTOR scale = {GetActualWidth()/SCREEN_WIDTH_F, GetActualHeight()/SCREEN_HEIGHT_F};
m_spriteBatch->Draw(
NULL,//quad->mTex->mTexId,
// position
position,
// sourceRectangle
&rect,
// color
colorVector,
// rotation
0,
// origin
origin,
//scale
scale
);*/
}
@@ -1128,12 +1197,19 @@ void JRenderer::TransferTextureToGLContext(JTexture& inTexture)
initData.SysMemSlicePitch = inTexture.mTexHeight*inTexture.mTexWidth*4;
ID3D11Texture2D* tex2D = nullptr;
hr = m_d3dDevice->CreateTexture2D( &desc, &initData, &inTexture.mTexId);
if( S_OK != hr)
hr = m_d3dDevice->CreateTexture2D( &desc, &initData, &tex2D);
if( FAILED(hr) || tex2D == 0)
return;
mspriteBatchInstance->AddTexture(inTexture.mTexId);
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
hr = m_d3dDevice->CreateShaderResourceView( tex2D, &SRVDesc, &inTexture.mTexId );
tex2D->Release();
delete [] inTexture.mBuffer;
inTexture.mBuffer = NULL;
}