2013-11-13 10:27:37 +01:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// File: TemplateMain.cpp
|
|
|
|
//
|
|
|
|
// BTH-D3D-Template
|
|
|
|
//
|
|
|
|
// Copyright (c) Stefan Petersson 2011. All rights reserved.
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
#define NOMINMAX
|
2013-11-29 10:25:27 +01:00
|
|
|
#include <vld.h>
|
2013-11-13 10:27:37 +01:00
|
|
|
#include <Windows.h>
|
2013-11-26 15:33:05 +01:00
|
|
|
#include "DllInterfaces\GFXAPI.h"
|
|
|
|
|
|
|
|
|
2013-11-13 10:27:37 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Global Variables
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
HINSTANCE g_hInst = NULL;
|
|
|
|
HWND g_hWnd = NULL;
|
2013-11-29 10:25:27 +01:00
|
|
|
Oyster::Graphics::Model::Model* m = NULL;
|
2013-12-04 09:36:43 +01:00
|
|
|
Oyster::Graphics::Model::Model* m2 = NULL;
|
2013-12-18 20:28:06 +01:00
|
|
|
Oyster::Graphics::Model::Model* m3 = NULL;
|
2014-02-07 16:51:35 +01:00
|
|
|
Oyster::Graphics::API::Texture t = NULL;
|
2013-11-21 18:31:16 +01:00
|
|
|
Oyster::Math::Float4x4 V;
|
|
|
|
Oyster::Math::Float4x4 P;
|
2014-02-10 11:53:44 +01:00
|
|
|
Oyster::Graphics::Definitions::Pointlight pl;
|
2013-11-13 10:27:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Forward declarations
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
|
|
|
|
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
|
|
|
|
HRESULT Render(float deltaTime);
|
|
|
|
HRESULT Update(float deltaTime);
|
|
|
|
HRESULT InitDirect3D();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Entry point to the program. Initializes everything and goes into a message processing
|
|
|
|
// loop. Idle time is used to render the scene.
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
|
|
|
|
{
|
2013-11-26 15:33:05 +01:00
|
|
|
|
2013-12-04 09:56:03 +01:00
|
|
|
BOOL b = SetDllDirectoryW(L"..\\DLL");
|
2013-11-26 15:33:05 +01:00
|
|
|
|
2013-11-13 10:27:37 +01:00
|
|
|
if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( FAILED( InitDirect3D() ) )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
__int64 cntsPerSec = 0;
|
|
|
|
QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec);
|
|
|
|
float secsPerCnt = 1.0f / (float)cntsPerSec;
|
|
|
|
|
|
|
|
__int64 prevTimeStamp = 0;
|
|
|
|
QueryPerformanceCounter((LARGE_INTEGER*)&prevTimeStamp);
|
|
|
|
|
2014-01-08 07:01:59 +01:00
|
|
|
std::string fps = "FPS:";
|
|
|
|
char count[100];
|
2013-11-13 10:27:37 +01:00
|
|
|
// Main message loop
|
|
|
|
MSG msg = {0};
|
2014-01-08 07:01:59 +01:00
|
|
|
float fpsCounter = 0;
|
2013-11-13 10:27:37 +01:00
|
|
|
while(WM_QUIT != msg.message)
|
|
|
|
{
|
|
|
|
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE) )
|
|
|
|
{
|
|
|
|
TranslateMessage( &msg );
|
|
|
|
DispatchMessage( &msg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__int64 currTimeStamp = 0;
|
|
|
|
QueryPerformanceCounter((LARGE_INTEGER*)&currTimeStamp);
|
|
|
|
float dt = (currTimeStamp - prevTimeStamp) * secsPerCnt;
|
|
|
|
|
|
|
|
//render
|
|
|
|
Update(dt);
|
|
|
|
Render(dt);
|
2014-01-08 07:01:59 +01:00
|
|
|
fpsCounter += dt;
|
|
|
|
if(fpsCounter>0.1f)
|
|
|
|
{
|
|
|
|
sprintf_s(count, "%f",1/dt);
|
|
|
|
SetWindowTextA(g_hWnd, (fps + count).c_str());
|
|
|
|
fpsCounter = 0;
|
|
|
|
}
|
2013-11-13 10:27:37 +01:00
|
|
|
prevTimeStamp = currTimeStamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:54:57 +01:00
|
|
|
Oyster::Graphics::API::Option o = Oyster::Graphics::API::GetOption();
|
|
|
|
|
2013-11-29 10:25:27 +01:00
|
|
|
Oyster::Graphics::API::DeleteModel(m);
|
2013-12-05 14:56:34 +01:00
|
|
|
Oyster::Graphics::API::DeleteModel(m2);
|
2014-01-08 07:01:59 +01:00
|
|
|
Oyster::Graphics::API::DeleteModel(m3);
|
2013-11-29 10:25:27 +01:00
|
|
|
Oyster::Graphics::API::Clean();
|
2013-11-13 10:27:37 +01:00
|
|
|
return (int) msg.wParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Register class and create window
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
|
|
|
|
{
|
|
|
|
// Register class
|
|
|
|
WNDCLASSEX wcex;
|
|
|
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wcex.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
wcex.lpfnWndProc = WndProc;
|
|
|
|
wcex.cbClsExtra = 0;
|
|
|
|
wcex.cbWndExtra = 0;
|
|
|
|
wcex.hInstance = hInstance;
|
|
|
|
wcex.hIcon = 0;
|
|
|
|
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
|
|
|
|
wcex.lpszMenuName = NULL;
|
|
|
|
wcex.lpszClassName = L"BTH_D3D_Template";
|
|
|
|
wcex.hIconSm = 0;
|
|
|
|
if( !RegisterClassEx(&wcex) )
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Adjust and create window
|
|
|
|
g_hInst = hInstance;
|
2014-01-22 16:31:33 +01:00
|
|
|
RECT rc = { 0, 0, 1280, 720 };
|
2013-11-13 10:27:37 +01:00
|
|
|
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
|
|
|
|
|
|
|
|
if(!(g_hWnd = CreateWindow(
|
|
|
|
L"BTH_D3D_Template",
|
|
|
|
L"BTH - Direct3D 11.0 Template",
|
|
|
|
WS_OVERLAPPEDWINDOW,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
rc.right - rc.left,
|
|
|
|
rc.bottom - rc.top,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
hInstance,
|
|
|
|
NULL)))
|
|
|
|
{
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowWindow( g_hWnd, nCmdShow );
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Create Direct3D device and swap chain
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
HRESULT InitDirect3D()
|
|
|
|
{
|
|
|
|
HRESULT hr = S_OK;;
|
|
|
|
|
2013-11-26 15:33:05 +01:00
|
|
|
if(Oyster::Graphics::API::Init(g_hWnd,false,false, Oyster::Math::Float2( 1024, 768 )) == Oyster::Graphics::API::Fail)
|
|
|
|
{
|
2013-11-20 10:22:01 +01:00
|
|
|
return E_FAIL;
|
2013-11-26 15:33:05 +01:00
|
|
|
}
|
2014-02-07 15:52:07 +01:00
|
|
|
Oyster::Graphics::API::Option o = Oyster::Graphics::API::GetOption();
|
|
|
|
o.modelPath = L"..\\Content\\Models\\";
|
|
|
|
o.texturePath = L"..\\Content\\Textures\\";
|
|
|
|
Oyster::Graphics::API::SetOptions(o);
|
|
|
|
|
2014-02-07 13:46:55 +01:00
|
|
|
m = Oyster::Graphics::API::CreateModel(L"crate_colonists.dan");
|
2014-02-07 15:52:07 +01:00
|
|
|
m2 = Oyster::Graphics::API::CreateModel(L"T_reskinned.dan");
|
2014-02-03 12:09:11 +01:00
|
|
|
m2->WorldMatrix = Oyster::Math3D::OrientationMatrix(Oyster::Math::Float3::null,Oyster::Math::Float3(0,0,0),Oyster::Math::Float3::null);
|
2014-02-10 11:53:44 +01:00
|
|
|
Oyster::Graphics::API::PlayAnimation(m2, L"Bend",true);
|
2014-01-27 14:24:13 +01:00
|
|
|
//m3 = Oyster::Graphics::API::CreateModel(L"box_2.dan");
|
|
|
|
//m3->WorldMatrix = Oyster::Math3D::OrientationMatrix(Oyster::Math::Float3::null,Oyster::Math::Float3(0,5,0),Oyster::Math::Float3::null);
|
2013-11-21 23:54:12 +01:00
|
|
|
|
2014-02-07 16:51:35 +01:00
|
|
|
t = Oyster::Graphics::API::CreateTexture(L"white.png");
|
2013-11-21 18:31:16 +01:00
|
|
|
|
2014-01-22 16:31:33 +01:00
|
|
|
P = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1280.0f/720.0f,.1f,10000);
|
2013-12-05 14:56:34 +01:00
|
|
|
Oyster::Graphics::API::SetProjection(P);
|
2013-11-22 10:55:21 +01:00
|
|
|
|
2014-02-03 12:09:11 +01:00
|
|
|
V = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),Oyster::Math::Float3(0,0,5.4f));
|
2013-12-18 20:28:06 +01:00
|
|
|
V = V.GetInverse();
|
|
|
|
|
|
|
|
|
2014-02-10 11:53:44 +01:00
|
|
|
|
2014-02-07 15:52:07 +01:00
|
|
|
pl.Color = Oyster::Math::Float3(1,0,1);
|
2013-12-18 20:28:06 +01:00
|
|
|
pl.Bright = 1;
|
2014-02-05 16:00:28 +01:00
|
|
|
pl.Pos = Oyster::Math::Float3(0,-20.0f,0.4f);
|
2014-01-22 16:31:33 +01:00
|
|
|
pl.Radius = 90;
|
2013-12-18 20:28:06 +01:00
|
|
|
|
2014-02-10 11:53:44 +01:00
|
|
|
Oyster::Graphics::API::AddLight(&pl);
|
2013-11-22 10:55:21 +01:00
|
|
|
|
2013-11-15 08:41:11 +01:00
|
|
|
|
2013-11-13 10:27:37 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
2013-11-28 14:34:52 +01:00
|
|
|
float angle = 0;
|
2013-11-13 10:27:37 +01:00
|
|
|
HRESULT Update(float deltaTime)
|
|
|
|
{
|
2014-01-22 16:31:33 +01:00
|
|
|
angle += Oyster::Math::pi/16 * deltaTime;
|
2014-02-07 15:52:07 +01:00
|
|
|
//m->WorldMatrix = Oyster::Math3D::RotationMatrix_AxisY(angle);
|
|
|
|
//m->WorldMatrix = m->WorldMatrix * Oyster::Math3D::RotationMatrix_AxisX(-Oyster::Math::pi/2);
|
|
|
|
m2->WorldMatrix = Oyster::Math3D::OrientationMatrix(Oyster::Math::Float3(0,1,0)*-Oyster::Math::pi/2,Oyster::Math::Float3(0,0,0),Oyster::Math::Float3::null);
|
|
|
|
Oyster::Math::Matrix ma = Oyster::Math::Matrix::identity;
|
2014-02-03 12:09:11 +01:00
|
|
|
//ma *= 50;
|
|
|
|
//ma.m44 = 1;
|
|
|
|
//m2->WorldMatrix = m2->WorldMatrix * ma;
|
2014-02-10 11:53:44 +01:00
|
|
|
m2->Animation.data.AnimationTime += deltaTime;// * 0.5f;
|
2014-01-27 14:24:13 +01:00
|
|
|
//m3->WorldMatrix = Oyster::Math3D::OrientationMatrix(Oyster::Math::Float3(1,0,0)*-0,Oyster::Math::Float3(3,4,-1*angle),Oyster::Math::Float3::null);
|
2013-11-13 10:27:37 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT Render(float deltaTime)
|
|
|
|
{
|
2013-12-05 14:56:34 +01:00
|
|
|
Oyster::Graphics::API::SetView(V);
|
|
|
|
Oyster::Graphics::API::NewFrame();
|
2013-11-21 18:31:16 +01:00
|
|
|
|
2014-02-07 13:46:55 +01:00
|
|
|
Oyster::Graphics::API::RenderModel(*m);
|
2014-02-07 15:52:07 +01:00
|
|
|
Oyster::Graphics::API::RenderModel(*m2);
|
2014-01-27 14:24:13 +01:00
|
|
|
//Oyster::Graphics::API::RenderModel(*m3);
|
2014-02-10 11:53:44 +01:00
|
|
|
//Oyster::Graphics::API::StartGuiRender();
|
2014-02-07 16:51:35 +01:00
|
|
|
Oyster::Graphics::API::RenderGuiElement(t,Oyster::Math::Float2(0.5f,0.5f),Oyster::Math::Float2(0.2f,0.2f));
|
2013-11-26 15:33:05 +01:00
|
|
|
Oyster::Graphics::API::EndFrame();
|
2013-11-13 10:27:37 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Called every time the application receives a message
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
|
|
|
|
{
|
|
|
|
PAINTSTRUCT ps;
|
|
|
|
HDC hdc;
|
|
|
|
|
|
|
|
switch (message)
|
|
|
|
{
|
|
|
|
case WM_PAINT:
|
|
|
|
hdc = BeginPaint(hWnd, &ps);
|
|
|
|
EndPaint(hWnd, &ps);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_DESTROY:
|
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_KEYDOWN:
|
|
|
|
|
|
|
|
switch(wParam)
|
|
|
|
{
|
|
|
|
case VK_ESCAPE:
|
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
2014-01-08 07:01:59 +01:00
|
|
|
//R
|
|
|
|
case 0x52:
|
|
|
|
#ifdef _DEBUG
|
|
|
|
Oyster::Graphics::API::ReloadShaders();
|
|
|
|
#endif
|
|
|
|
break;
|
2014-02-05 16:00:28 +01:00
|
|
|
//Z -
|
|
|
|
case 0x5A:
|
2014-02-10 11:53:44 +01:00
|
|
|
//m2->AnimationTime -= 0.1f;
|
|
|
|
//if(m2->AnimationTime < 0)
|
|
|
|
//m2->AnimationTime = 0;
|
2014-02-05 16:00:28 +01:00
|
|
|
break;
|
|
|
|
//X +
|
|
|
|
case 0x58:
|
2014-02-10 11:53:44 +01:00
|
|
|
//m2->AnimationTime += 0.1f;
|
2014-02-05 16:00:28 +01:00
|
|
|
break;
|
|
|
|
|
2013-11-13 10:27:37 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|