XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Issues with sample code

Last post 6/29/2009 3:05 AM by legalize. 2 replies.
  • 6/18/2009 6:44 AM

    Issues with sample code

    Hi. I am completely new to direct 3d. I was following a tutorial on Managed DirectX, and I am not getting the results I am supposed to. Here is the code.

    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

    So obviously I had to change a lot of stuff because Everyone uses C# now so finding a tutorial for Managed D3D in C++ is impossible. I made whatever changes where nessesary to get it to buld in C++. It builds, and runs without errors, but you can not see the triangle that was specified by the verticies. It is mind boggling as to why, as I have gone over the code with a fine tooth comb several times.

    If anyone can point out something I may have missed I would appreciate it.
  • 6/18/2009 7:19 AM In reply to

    Re: Issues with sample code

    Managed Direct3D is no longer supported, so you'll have a hard time finding anyone that uses it anymore.

    If you want to stay with C++, and don't mind native code, you should probably use the native DirectX.
    If you want a managed graphics API, you should probably go with the XNA Framework, or SlimDX.

  • 6/29/2009 3:05 AM In reply to

    Re: Issues with sample code

    Since you are new to Direct3D, you will also benefit from looking at my pipeline poster and my book The Direct3D Graphics Pipeline. You may also find my Direct3D Programming Tips helpful.
Page 1 of 1 (3 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG