FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DirectX problem

 
Post new topic   Reply to topic     Forum Index -> Bindings
View previous topic :: View next topic  
Author Message
RuZiggy



Joined: 14 Sep 2008
Posts: 10

PostPosted: Fri Jan 23, 2009 6:32 am    Post subject: DirectX problem Reply with quote

Hello guys. Sorry for my English it's not my native language.
So, i've i wanna to write games with DX9 and D, i've downloaded WinApi project which includes DX bindings.
I've took Wyvex's code and started write, i've added Vertex Buffer and here i've get in trouble. I can draw my Vertices only w\o buffer, i.e. with DrawPrimitiveUP.
Any ideas how to get code work?
Here is code:
Code:
module dx;

pragma( lib, "gdi32.lib" );
pragma( lib, "d3d9.lib" );
pragma( lib, "d3dx9.lib" );

//DirectX/Windows stuff
import std.c.string;
import win32.windows, win32.directx.d3d9, win32.directx.d3dx9;

struct Vertex
{
   FLOAT x, y, z, rhw;
   DWORD color;
}

void main()
{
   auto hInstance = GetModuleHandle(null);
   auto blackBrush = cast(HBRUSH) GetStockObject(BLACK_BRUSH);

   WNDCLASSEX wc = {WNDCLASSEX.sizeof, CS_HREDRAW | CS_VREDRAW, &MsgProc, 0,
         0, hInstance, null, null, blackBrush, null, "WindowClass", null};

   RegisterClassEx( &wc);
   scope(exit)
      UnregisterClass("WindowClass", hInstance);

   auto hWnd = CreateWindowEx(WS_EX_TOPMOST, "WindowClass", "DirectX from D!",
         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, null,
         null, hInstance, null);

   assert(hWnd !is null);

   ShowWindow(hWnd, SW_NORMAL);

   //load DX stuff here
   auto D3D = Direct3DCreate9(D3D_SDK_VERSION);
   assert(D3D !is null);
   scope(exit)
      D3D.Release;

   D3DPRESENT_PARAMETERS dpp;
   dpp.Windowed = true;
   dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
   dpp.hDeviceWindow = hWnd;

   LPDIRECT3DDEVICE9 device;
   D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
         D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dpp, &device);
   assert(device !is null);
   scope(exit)
      device.Release;

   DWORD fvf = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;

   auto vertices = new Vertex[3];
   vertices[0].x = 150.0f;
   vertices[0].y = 50.0f;
   vertices[0].z = 0.5f;
   vertices[0].rhw = 1.0f;
   vertices[0].color = 0xffff0000;

   vertices[1].x = 250.0f;
   vertices[1].y = 250.0f;
   vertices[1].z = 0.5f;
   vertices[1].rhw = 1.0f;
   vertices[1].color = 0xff00ff00;

   vertices[2].x = 50.0f;
   vertices[2].y = 250.0f;
   vertices[2].z = 0.5f;
   vertices[2].rhw = 1.0f;
   vertices[2].color = 0xff00ffff;

   LPDIRECT3DVERTEXBUFFER9 _vb;
   device.CreateVertexBuffer(3 * Vertex.sizeof, 0, fvf, D3DPOOL_DEFAULT, &_vb,
         NULL);

   VOID* pVert;
   _vb.Lock(0, vertices.sizeof, cast(void**)&pVert, 0);

   memcpy(pVert, vertices.ptr, vertices.sizeof);

   _vb.Unlock();

   MSG msg;
   while(true)
   {
      if(PeekMessage( &msg, null, 0, 0, PM_REMOVE))
      {
         if(msg.message == WM_QUIT)
            break;

         TranslateMessage( &msg);
         DispatchMessage( &msg);
      } else
      {
         //game logic Here
         device.Clear(0, null, D3DCLEAR_TARGET, 0x0000FF, 1.0, 0);
         device.BeginScene();

         device.SetStreamSource(0, _vb, 0, Vertex.sizeof);
         device.SetFVF(fvf);

         device.DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
         //device.DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, &vertices[0],
         //      Vertex.sizeof);

         device.EndScene();
         device.Present(null, null, null, null);

         Sleep(17);
      }
   }
}

extern(Windows)
   int MsgProc(HWND hWnd, uint msg, uint wParam, int lParam)
   {
      switch(msg)
      {
         case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
         default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
      }
   }


I am sure that problem in this section:
Code:
   VOID* pVert;
   _vb.Lock(0, vertices.sizeof, cast(void**)&pVert, 0);

   memcpy(pVert, vertices.ptr, vertices.sizeof);

   _vb.Unlock();

But which way will be correct?
Back to top
View user's profile Send private message
RuZiggy



Joined: 14 Sep 2008
Posts: 10

PostPosted: Fri Jan 23, 2009 7:04 am    Post subject: Reply with quote

Thanks to predaeus from IRC channel. Problem is solved.
Solution is to change memcpy to direct copy:
Code:
   
       (cast(Vertex*) pVert)[0] = vertices[0];
   (cast(Vertex*) pVert)[1] = vertices[1];
   (cast(Vertex*) pVert)[2] = vertices[2];

But this solution isn't best. Any ideas what's wrong? Smile
Back to top
View user's profile Send private message
RuZiggy



Joined: 14 Sep 2008
Posts: 10

PostPosted: Fri Jan 23, 2009 7:42 am    Post subject: Reply with quote

Well, thanks to downs at IRC! Now problem is totaly solved.
Solution:
Code:
   memcpy(pVert, vertices.ptr, vertices.length * Vertex.sizeof);
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Bindings All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group