#define WIN32_LEAN_AND_MEAN #define IDC_OUTPUT_WINDOW 1000 #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE) #include #include #include #include #pragma comment(lib, "d3d9.lib") #ifdef _DEBUG #pragma comment(lib, "d3dx9d.lib") #else #pragma comment(lib, "d3d9x9.lib") #endif LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL; LPDIRECT3D9 g_pD3D; LPDIRECT3DDEVICE9 g_pD3Ddevice; void init_matrices(LPDIRECT3DDEVICE9); struct V_STRUCT { float x, y, z; DWORD color; }; V_STRUCT t_vert[] = { {0.0f/4, 0.0f/4, 0.0f, D3DCOLOR_XRGB(0, 0, 255),}, {1.0f/4, -5.0f/4, 0.0f, D3DCOLOR_XRGB(0, 255, 0),}, {1.0f/4, 5.0f/4, 0.0f, D3DCOLOR_XRGB(255, 0, 0),}, }; LRESULT CALLBACK WndProc(HWND h, UINT message, WPARAM wParam, LPARAM lParam); int total = 0, value1 = 0, operation = 0; bool first = 1; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCommand) { WNDCLASSEX winc, dxclass; HWND hwnd, dxwnd; MSG msg; winc.cbSize = sizeof WNDCLASSEX; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = 0; winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL, IDI_APPLICATION); winc.hCursor = LoadCursor(NULL, IDC_ARROW); winc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = "MyClass"; winc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); dxclass.cbSize = sizeof WNDCLASSEX; dxclass.style = CS_HREDRAW | CS_VREDRAW; dxclass.lpfnWndProc = WndProc; dxclass.cbClsExtra = 0; dxclass.cbWndExtra = 0; dxclass.hInstance = hInstance; dxclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); dxclass.hCursor = LoadCursor(NULL, IDC_ARROW); dxclass.hbrBackground = NULL; dxclass.lpszMenuName = NULL; dxclass.lpszClassName = "DXClass"; dxclass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); if (!RegisterClassEx(&winc)) return 0; if (!RegisterClassEx(&dxclass)) return 0; hwnd = CreateWindowEx(NULL, "MyClass", "WindowTitle", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); dxwnd = CreateWindowEx(NULL, "DXClass", "", WS_VISIBLE | WS_CHILD, 100, 100, 200, 200, hwnd, NULL, hInstance, NULL); if (!hwnd) return 0; if (!dxwnd) return 0; g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if (!g_pD3D) PostQuitMessage(WM_QUIT); D3DPRESENT_PARAMETERS g_D3Dpp; ZeroMemory(&g_D3Dpp, sizeof D3DPRESENT_PARAMETERS); g_D3Dpp.Windowed = true; g_D3Dpp.hDeviceWindow = dxwnd; g_D3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; g_pD3D -> CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, dxwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &g_D3Dpp, &g_pD3Ddevice); init_matrices(g_pD3Ddevice); g_pD3Ddevice->SetRenderState(D3DRS_LIGHTING, false); g_pD3Ddevice->CreateVertexBuffer(3* sizeof V_STRUCT, 0, CUSTOMFVF, D3DPOOL_MANAGED, &t_buffer, NULL); void* vb_vertices; t_buffer->Lock(0, 0, &vb_vertices, 0); memcpy(vb_vertices, t_vert, sizeof t_vert); t_buffer->Unlock(); while(GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); g_pD3Ddevice ->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); g_pD3Ddevice ->BeginScene(); g_pD3Ddevice->SetFVF(CUSTOMFVF); g_pD3Ddevice->SetStreamSource(0, t_buffer, 0, sizeof V_STRUCT); g_pD3Ddevice ->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); g_pD3Ddevice ->EndScene(); g_pD3Ddevice ->Present(NULL, NULL, NULL, NULL); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_DESTROY: PostQuitMessage(WM_QUIT); break; case WM_CREATE: break; default: break; } return DefWindowProc(hwnd, message, wParam, lParam); } void init_matrices(LPDIRECT3DDEVICE9 D3Dd) { D3DXVECTOR3 eye_vector, lookat_vector, up_vector; eye_vector = D3DXVECTOR3(0.0f, 0.0f, -8.0f); lookat_vector = D3DXVECTOR3(0.0f, 0.0f, 0.0f); up_vector = D3DXVECTOR3(0.0f, 1.0f, 0.0f); D3DXMATRIX view_matrix; D3DXMatrixLookAtLH(&view_matrix, & eye_vector, &lookat_vector, &up_vector); D3Dd->SetTransform(D3DTS_VIEW, &view_matrix); D3DXMATRIX projection_matrix; float aspect = 4/3; D3DXMatrixPerspectiveFovLH(&projection_matrix, D3DX_PI / 4, aspect, 1.0f, 100.0f); D3Dd->SetTransform(D3DTS_PROJECTION, &projection_matrix); D3DXMATRIX world_matrix; D3DXMatrixIdentity(&world_matrix); D3Dd->SetTransform(D3DTS_WORLD, &world_matrix); }