add input

add time
add vertex color
This commit is contained in:
2025-09-24 11:44:51 +02:00
parent faa9b71510
commit c9e9dcf572
5 changed files with 51 additions and 15 deletions
+24 -12
View File
@@ -28,15 +28,27 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
void Mesh::update(FLOAT dt) void Mesh::update(FLOAT dt)
{ {
static FLOAT x = 0.0f;
static FLOAT y = 0.0f;
static FLOAT z = 0.0f;
static FLOAT pitch = XM_PIDIV4; // x axis static FLOAT pitch = XM_PIDIV4; // x axis
static FLOAT yaw = XM_PIDIV4; // y axis static FLOAT yaw = XM_PIDIV4; // y axis
static FLOAT roll = XM_PIDIV4; // z axis static FLOAT roll = XM_PIDIV4; // z axis
static FLOAT scale = 1.0f; static FLOAT scale = 1.0f;
yaw += 0.01f; yaw += 1.0f * dt;
//scale += 0.001f; //scale += 0.001f * dt;
XMMATRIX translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 0.0f); FLOAT move = 1.0f * dt;
if (GetAsyncKeyState(VK_LEFT) & 0x8000) x -= move;
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) x += move;
if (GetAsyncKeyState(VK_DOWN) & 0x8000) y -= move;
if (GetAsyncKeyState(VK_UP) & 0x8000) y += move;
if (GetAsyncKeyState('E') & 0x8000) z -= move;
if (GetAsyncKeyState('Q') & 0x8000) z += move;
XMMATRIX translationMatrix = XMMatrixTranslation(x, y, z);
XMMATRIX scaleMatrix = XMMatrixScaling(scale, scale, scale); XMMATRIX scaleMatrix = XMMatrixScaling(scale, scale, scale);
XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll); XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
@@ -121,16 +133,16 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
// cube // cube
// front // front
pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f); pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f, 255, 0, 0);
pVertices[1] = Vertex(0.5f, 0.5f, -0.5f); pVertices[1] = Vertex(0.5f, 0.5f, -0.5f, 0, 255, 0);
pVertices[2] = Vertex(0.5f, -0.5f, -0.5f); pVertices[2] = Vertex(0.5f, -0.5f, -0.5f, 0, 0, 255);
pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f); pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f, 255, 0, 255);
// front // back
pVertices[4] = Vertex(0.5f, 0.5f, 0.5f); pVertices[4] = Vertex(0.5f, 0.5f, 0.5f, 255, 255, 0);
pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f); pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f, 0, 255, 255);
pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f); pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f, 0, 0, 0);
pVertices[7] = Vertex(0.5f, -0.5f, 0.5f); pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 200, 200, 200);
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
CheckFailed(hr, 320); CheckFailed(hr, 320);
+8
View File
@@ -2,11 +2,19 @@
UINT Time::init() UINT Time::init()
{ {
_lastFrame = CLOCK::now();
return 0; return 0;
} }
void Time::update() void Time::update()
{ {
TIMEPOINT now = CLOCK::now();
SECONDS diff = now - _lastFrame;
_lastFrame = now;
_deltaTime = diff.count();
} }
void Time::deInit() void Time::deInit()
+5
View File
@@ -4,6 +4,10 @@
using namespace std; using namespace std;
typedef chrono::steady_clock CLOCK;
typedef CLOCK::time_point TIMEPOINT;
typedef chrono::duration<FLOAT> SECONDS;
class Time class Time
{ {
public: public:
@@ -15,5 +19,6 @@ public:
private: private:
FLOAT _deltaTime = 0.0f; FLOAT _deltaTime = 0.0f;
TIMEPOINT _lastFrame = {};
}; };
+6 -3
View File
@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <d3d9.h> #include <d3d9.h>
#define FVF D3DFVF_XYZ #define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE
struct Vertex struct Vertex
{ {
@@ -9,6 +9,9 @@ struct Vertex
FLOAT y; FLOAT y;
FLOAT z; FLOAT z;
Vertex() : x(0.0f), y(0.0f), z(0.0f) {} D3DCOLOR color;
Vertex(FLOAT _x, FLOAT _y, FLOAT _z) : x(_x), y(_y), z(_z) {}
Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xFFFFFFFF) {}
Vertex(FLOAT _x, FLOAT _y, FLOAT _z) : x(_x), y(_y), z(_z), color(0xFFFFFFFF) {}
Vertex(FLOAT _x, FLOAT _y, FLOAT _z, UINT r, UINT g, UINT b) : x(_x), y(_y), z(_z), color(D3DCOLOR_XRGB(r, g, b)) {}
}; };
+8
View File
@@ -38,6 +38,7 @@ int WINAPI WinMain(
// set render states // set render states
d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, FALSE);
// 3. create a mesh // 3. create a mesh
Mesh mesh = {}; Mesh mesh = {};
@@ -67,6 +68,13 @@ int WINAPI WinMain(
// 8. run application // 8. run application
while (window.run()) while (window.run())
{ {
if (GetAsyncKeyState(VK_F1) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
if (GetAsyncKeyState(VK_F2) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
if (GetAsyncKeyState(VK_F3) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_POINT);
if (GetAsyncKeyState(VK_F4) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
if (GetAsyncKeyState(VK_F5) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
if (GetAsyncKeyState(VK_F6) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
// 8.1. update objects // 8.1. update objects
time.update(); time.update();
mesh.update(time.getDeltaTime()); mesh.update(time.getDeltaTime());