milsko/examples/dxdemos/direct3d9.c

92 lines
2.4 KiB
C
Raw Permalink Normal View History

2026-09-17 20:58:41 -07:00
/*
* Adapted from:
* http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-1
* http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-4
*/
#include <Mw/Milsko.h>
2026-09-18 16:43:15 +09:00
#include <Mw/Widget/Direct3D9.h>
2026-09-17 20:58:41 -07:00
2026-09-18 16:43:15 +09:00
MwWidget window, d3d9;
2026-09-17 20:58:41 -07:00
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;
struct CUSTOMVERTEX {
FLOAT x, y, z, rhw; // from the D3DFVF_XYZRHW flag
DWORD color; // from the D3DFVF_DIFFUSE flag
};
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
static void MWAPI draw(MwWidget handle, void* user, void* client) {
// clear the window to a deep blue
d3ddev->lpVtbl->Clear(d3ddev, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
d3ddev->lpVtbl->BeginScene(d3ddev); // begins the 3D scene
// select which vertex format we are using
d3ddev->lpVtbl->SetFVF(d3ddev, CUSTOMFVF);
// select the vertex buffer to display
d3ddev->lpVtbl->SetStreamSource(d3ddev, 0, v_buffer, 0, sizeof(struct CUSTOMVERTEX));
// copy the vertex buffer to the back buffer
d3ddev->lpVtbl->DrawPrimitive(d3ddev, D3DPT_TRIANGLELIST, 0, 1);
d3ddev->lpVtbl->EndScene(d3ddev); // ends the 3D scene
d3ddev->lpVtbl->Present(d3ddev, NULL, NULL, NULL, NULL); // displays the created frame
}
int main() {
2026-09-17 21:44:55 -07:00
VOID* pVoid;
2026-09-17 20:58:41 -07:00
// create three vertices using the CUSTOMVERTEX struct built earlier
struct CUSTOMVERTEX vertices[] =
{
{
400.0f,
0.0f,
0.5f,
1.0f,
D3DCOLOR_XRGB(0, 0, 255),
},
{
800.0f,
600.0f,
0.5f,
1.0f,
D3DCOLOR_XRGB(0, 255, 0),
},
{
0.0f,
600.0f,
0.5f,
1.0f,
D3DCOLOR_XRGB(255, 0, 0),
},
};
2026-09-18 16:43:15 +09:00
MwLibraryInit();
window = MwCreateWidget(MwWindowClass, NULL, NULL, MwDEFAULT, MwDEFAULT, 1024, 768);
d3d9 = MwCreateWidget(MwDirect3D9Class, NULL, window, (1024 - 800) / 2, (768 - 600) / 2, 800, 600);
2026-09-18 16:47:13 +09:00
d3d = MwDirect3D9GetDirect3D9(d3d9);
d3ddev = MwDirect3D9GetDirect3DDevice9(d3d9);
2026-09-18 16:43:15 +09:00
2026-09-17 20:58:41 -07:00
d3ddev->lpVtbl->CreateVertexBuffer(d3ddev,
3 * sizeof(struct CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
v_buffer->lpVtbl->Lock(v_buffer, 0, 0, (void**)&pVoid, 0); // lock the vertex buffer
memcpy(pVoid, vertices, sizeof(vertices)); // copy the vertices to the locked buffer
v_buffer->lpVtbl->Unlock(v_buffer); // unlock the vertex buffer
MwAddUserHandler(window, MwNtickHandler, draw, NULL);
MwLoop(window);
}