| 1 |
namespace LearningMain |
| 2 |
{ |
| 3 |
using namespace System; |
| 4 |
|
| 5 |
public ref class Game : System::Windows::Forms::Form |
| 6 |
{ |
| 7 |
public: |
| 8 |
|
| 9 |
Game(void) { |
| 10 |
} |
| 11 |
|
| 12 |
void Init(void) { |
| 13 |
this->InitializeGraphics(); |
| 14 |
while(this->Created) { |
| 15 |
this->Render(); |
| 16 |
System::Windows::Forms::Application::DoEvents(); |
| 17 |
} |
| 18 |
this->DisposeGraphics(); |
| 19 |
} |
| 20 |
|
| 21 |
private: |
| 22 |
|
| 23 |
void InitializeGraphics(void) { |
| 24 |
Microsoft::DirectX::Direct3D::PresentParameters ^para = gcnew Microsoft::DirectX::Direct3D::PresentParameters(); |
| 25 |
para->Windowed = true; |
| 26 |
para->SwapEffect = Microsoft::DirectX::Direct3D::SwapEffect::Discard; |
| 27 |
|
| 28 |
this->device = gcnew Microsoft::DirectX::Direct3D::Device(0, |
| 29 |
Microsoft::DirectX::Direct3D::DeviceType::Hardware, this->Handle, |
| 30 |
Microsoft::DirectX::Direct3D::CreateFlags::SoftwareVertexProcessing, para); |
| 31 |
|
| 32 |
this->tcolor = |
| 33 |
gcnew Microsoft::DirectX::Direct3D::CustomVertex::PositionColored(); |
| 34 |
|
| 35 |
this->mycolor = System::Drawing::Color::BlueViolet; |
| 36 |
|
| 37 |
this->vertices = CreateVertexBuffer(); |
| 38 |
this->device->Lights[0]->Enabled = false; |
| 39 |
|
| 40 |
delete para; |
| 41 |
} |
| 42 |
|
| 43 |
void Render(void) { |
| 44 |
this->device->Clear(Microsoft::DirectX::Direct3D::ClearFlags::Target, |
| 45 |
System::Drawing::Color::DarkRed, 1.0, 0); |
| 46 |
this->device->BeginScene(); |
| 47 |
|
| 48 |
this->device->VertexFormat = |
| 49 |
this->tcolor->Format; |
| 50 |
|
| 51 |
this->device->SetStreamSource(0, this->vertices, 0); |
| 52 |
this->device->DrawPrimitives( |
| 53 |
Microsoft::DirectX::Direct3D::PrimitiveType::TriangleList,0,1); |
| 54 |
|
| 55 |
this->device->EndScene(); |
| 56 |
this->device->Present(); |
| 57 |
} |
| 58 |
|
| 59 |
void DisposeGraphics(void) { |
| 60 |
delete this->device; |
| 61 |
delete this->vertices; |
| 62 |
} |
| 63 |
|
| 64 |
Microsoft::DirectX::Direct3D::VertexBuffer ^ CreateVertexBuffer(void) { |
| 65 |
array<Microsoft::DirectX::Direct3D::CustomVertex::PositionColored^,1> ^vert = |
| 66 |
gcnew array<Microsoft::DirectX::Direct3D::CustomVertex::PositionColored^,1>(3); |
| 67 |
|
| 68 |
vert->SetValue( |
| 69 |
gcnew Microsoft::DirectX::Direct3D::CustomVertex::PositionColored(600.0 / 2.0, 300.0 / 4.0, 1, this->mycolor->ToArgb()),0); |
| 70 |
vert->SetValue( |
| 71 |
gcnew Microsoft::DirectX::Direct3D::CustomVertex::PositionColored(600.0 * 3.0 / 4.0, 300.0 * 3.0 / 4.0, 1, this->mycolor->ToArgb()),1); |
| 72 |
vert->SetValue( |
| 73 |
gcnew Microsoft::DirectX::Direct3D::CustomVertex::PositionColored(600.0 / 4.0, 300.0 * 3.0 / 4.0, 1, this->mycolor->ToArgb()),2); |
| 74 |
|
| 75 |
Microsoft::DirectX::Direct3D::VertexBuffer ^buf = gcnew Microsoft::DirectX::Direct3D::VertexBuffer( |
| 76 |
this->tcolor->GetType(), |
| 77 |
vert->Length, this->device, Microsoft::DirectX::Direct3D::Usage::None, |
| 78 |
this->tcolor->Format, |
| 79 |
Microsoft::DirectX::Direct3D::Pool::Default); |
| 80 |
|
| 81 |
buf->SetData(vert[0],0,Microsoft::DirectX::Direct3D::LockFlags::None); |
| 82 |
buf->SetData(vert[1],1,Microsoft::DirectX::Direct3D::LockFlags::None); |
| 83 |
buf->SetData(vert[2],2,Microsoft::DirectX::Direct3D::LockFlags::None); |
| 84 |
|
| 85 |
return buf; |
| 86 |
} |
| 87 |
|
| 88 |
Microsoft::DirectX::Direct3D::Device ^device; |
| 89 |
Microsoft::DirectX::Direct3D::VertexBuffer ^vertices; |
| 90 |
Microsoft::DirectX::Direct3D::CustomVertex::PositionColored ^tcolor; |
| 91 |
System::Drawing::Color ^mycolor; |
| 92 |
}; |
| 93 |
} |