#include #include #include #include struct D3DVERTEX { float fX,fY,fZ; DWORD dwColor; }; int main( int argc, char* argv[] ) { SDL_Event event; //used for processing events and handling input LPDIRECT3D9 direct3D_object; //used to create a direct3D device LPDIRECT3DDEVICE9 direct3D_device; // your most trusty rendering tool D3DPRESENT_PARAMETERS present_parameters; //contains the parameters for important properties of the direct 3D device D3DXMATRIX projection_matrix; //your projection matrix D3DVERTEX aTriangle[ ] = {{-2.0f,1.0f,10.0f,0xffff0000},{-3.0f,-1.0f,10.0f,0xff00ff00},{-1.0f,-1.0f,10.0f,0xff0000ff}}; //the actual data to be fed to the verte buffer LPDIRECT3DVERTEXBUFFER9 tri_buffer = NULL; //used to store and draw triangle data VOID* pData; //pointer to the data og the vertex buffer //creating window if( SDL_Init( SDL_INIT_VIDEO ) < 0 || !SDL_GetVideoInfo() ) return 0; SDL_SetVideoMode( 800, 600, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_RESIZABLE ); //creating direct3D object direct3D_object = Direct3DCreate9(D3D_SDK_VERSION); if( direct3D_object == NULL ) { MessageBox(GetActiveWindow(),"Could not create Direct3D Object","D3D_OBJ ERR",MB_OK); return 0; } //setting parameters that will be needed to created direct3D device ZeroMemory(&present_parameters, sizeof(present_parameters)); present_parameters.Windowed = false; present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD; present_parameters.EnableAutoDepthStencil = true; present_parameters.AutoDepthStencilFormat = D3DFMT_D16; present_parameters.hDeviceWindow = GetActiveWindow(); present_parameters.BackBufferWidth = 800; present_parameters.BackBufferHeight = 600; present_parameters.BackBufferFormat = D3DFMT_R5G6B5; present_parameters.MultiSampleType = D3DMULTISAMPLE_NONE; //creating direct3D device if( FAILED(direct3D_object->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,GetActiveWindow(), D3DCREATE_SOFTWARE_VERTEXPROCESSING,&present_parameters,&direct3D_device))) { MessageBox(GetActiveWindow(),"Could not create Direct3D Device","D3D_DEV ERR",MB_OK); return 0; } //establishing projection matrix, lighting, culling, depth buffer, and vertex format D3DXMatrixPerspectiveFovLH(&projection_matrix, D3DX_PI / 4.0f,(float) 800/600,1, 1000); direct3D_device->SetTransform(D3DTS_PROJECTION,&projection_matrix); direct3D_device->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255)); direct3D_device->SetRenderState(D3DRS_LIGHTING,false); direct3D_device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); direct3D_device->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE); direct3D_device->SetFVF((D3DFVF_XYZ | D3DFVF_DIFFUSE)); // creating vertex buffer direct3D_device->CreateVertexBuffer(sizeof(aTriangle),D3DUSAGE_WRITEONLY,(D3DFVF_XYZ | D3DFVF_DIFFUSE),D3DPOOL_MANAGED,&tri_buffer,NULL); tri_buffer->Lock(0,sizeof(pData),(void**)&pData,0); memcpy(pData,aTriangle,sizeof(aTriangle)); tri_buffer->Unlock(); // MAIN LOOP while( 1 ) { direct3D_device->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0); direct3D_device->BeginScene(); direct3D_device->SetStreamSource(0,tri_buffer,0,sizeof(D3DVERTEX)); direct3D_device->DrawPrimitive(D3DPT_TRIANGLELIST,0,1); direct3D_device->EndScene(); direct3D_device->Present(NULL,NULL,NULL,NULL); while( SDL_PollEvent( &event ) ) switch( event.type ) { case SDL_KEYDOWN: if ( event.key.keysym.sym == SDLK_ESCAPE ) { direct3D_device->Release(); direct3D_device = NULL; direct3D_object->Release(); direct3D_object = NULL; return 0; } break; case SDL_QUIT: return 0; } } return 0; }