Jump to content
  • Advertisement
Sign in to follow this  
alop125125

Creating 2 Triangles

This topic is 680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been following http://www.directxtutorial.com to learn d3d and i just finished the tutorial where awe make a triangle and i dont want to move on till i have this mastered so how can i modify this code to work. Thanks, Ignore the poor practices i am still learning

#include <Windows.h>
#include <d3d9.h>
 
//include the d3d lib file
#pragma comment (lib, "d3d9.lib")
 
//global declarations
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;
//fucntion prototypes
void initd3d(HWND hWnd);
void render_frame();
void cleand3d();
void graphics();
 
LRESULT CALLBACK wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 
 
#define SCREEN_HEIGHT 1920
#define SCREEN_WIDTH 1080
 
#define CFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
struct CVERTEX
{
FLOAT x, y, z, rhw;
DWORD color;
};
 
 
 
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR lCmdLine, int nCmdShow)
{
WNDCLASS wc = {};
 
wc.hInstance = hInst;
wc.lpfnWndProc = wndProc;
wc.lpszClassName = "WindowClass";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
 
RegisterClass(&wc);
 
HWND hWnd = CreateWindowEx(NULL, "WindowClass", "program", WS_EX_TOPMOST| WS_POPUP, 0, 0, SCREEN_HEIGHT, SCREEN_WIDTH, NULL, NULL, hInst, NULL);
 
ShowWindow(hWnd, nCmdShow);
//set up d3d
initd3d(hWnd);
 
MSG msg = {};
 
while (true) 
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
 
if (msg.message == WM_QUIT)
{
break;
}
 
//game code
render_frame();
 
}
 
cleand3d();
return msg.wParam;
}
 
LRESULT CALLBACK wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_LBUTTONDOWN:
graphics();
break;
}
 
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
 
 
 
void graphics()
{
 
CVERTEX tri[] = 
{
{SCREEN_WIDTH / 2, 100, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255)},
{SCREEN_WIDTH / 2 + (SCREEN_WIDTH / 3), SCREEN_HEIGHT - 100, 0.5f,1.0f, D3DCOLOR_XRGB(0,255,0)},
{SCREEN_WIDTH / 2 - (SCREEN_WIDTH / 3), SCREEN_HEIGHT - 100, 0.5f,1.0f, D3DCOLOR_XRGB(255,0,0)}
};
CVERTEX tri2[] =
{
{ 100, 100, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255) },
{ 150, 150, 0.5f,1.0f, D3DCOLOR_XRGB(0,255,0) },
{ 50, 150, 0.5f,1.0f, D3DCOLOR_XRGB(255,0,0) }
};
 
 
d3ddev->CreateVertexBuffer(6 * sizeof(CVERTEX), 0, CFVF, D3DPOOL_MANAGED, &v_buffer, NULL);
void *pVoid;
 
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
 
memcpy(pVoid, tri, sizeof(tri));
memcpy(pVoid, tri2, sizeof(tri2));
v_buffer->Unlock();
}
 
void render_frame()
{
 
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
 
d3ddev->BeginScene();
d3ddev->SetFVF(CFVF);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CVERTEX) * 2);
 
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
 
 
 
void initd3d(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
 
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
 
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
 
graphics();
}
 
void cleand3d()
{
v_buffer->Release();
d3d->Release();
d3ddev->Release();
}

 

Share this post


Link to post
Share on other sites
Advertisement

can you be more specific about what issue you are having?  is the code not drawing anything, or is their another issue at play?  
 
to me, it looks like you aren't copying the second triangle to the right address, and instead are overwriting the first:
 
 
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
 
memcpy(pVoid, tri, sizeof(tri));
memcpy(pVoid, tri2, sizeof(tri2));
v_buffer->Unlock();
should be changed to:
 
 
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
 
memcpy(pVoid, tri, sizeof(tri));
memcpy(pVoid+sizeof(tri), tri2, sizeof(tri2));
v_buffer->Unlock();
notice that the pVoid is now offset to the next triangle slot instead of overwritting the first slot(also notice the code tags, which can be used with: [*code] and [*/code](remove the *), to better format your code.)

alternatively you can also do:
 
void graphics()
{
 
CVERTEX tri[] = 
{
{SCREEN_WIDTH / 2, 100, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255)},
{SCREEN_WIDTH / 2 + (SCREEN_WIDTH / 3), SCREEN_HEIGHT - 100, 0.5f,1.0f, D3DCOLOR_XRGB(0,255,0)},
{SCREEN_WIDTH / 2 - (SCREEN_WIDTH / 3), SCREEN_HEIGHT - 100, 0.5f,1.0f, D3DCOLOR_XRGB(255,0,0)},
{ 100, 100, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255) },
{ 150, 150, 0.5f,1.0f, D3DCOLOR_XRGB(0,255,0) },
{ 50, 150, 0.5f,1.0f, D3DCOLOR_XRGB(255,0,0) }
};
 
 
d3ddev->CreateVertexBuffer(6 * sizeof(CVERTEX), 0, CFVF, D3DPOOL_MANAGED, &v_buffer, NULL);
void *pVoid;
 
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
 
memcpy(pVoid, tri, sizeof(tri));
v_buffer->Unlock();
}
this has both triangles inside the tri buffer, and is generally more efficient than copying from multiple buffers, i'd personally recommend setting up things this way instead whenever possible. Edited by slicer4ever

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!