c++ - Drawing a Texture to the Screen -
i attempting learn directx, , feeling quite overwhelmed. can tell me why not working? image not display screen. window black. trying follow along tutorial this, , code had.
#include <windows.h> #include <d3d9.h> #include <d3dx9tex.h> lpdirect3d9 direct3d = null; lpdirect3ddevice9 direct3ddevice = null; idirect3dtexture9* texture; lresult winapi wndproc(hwnd hwnd, uint msg, wparam wparam, lparam lparam); idirect3dtexture9 *loadtexture(wchar_t *filename); void blitd3d (idirect3dtexture9 *texture, rect *rdest, d3dcolor vertexcolour, float rotate); int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshow) { msg msg; wndclassex wc = {sizeof(wndclassex), cs_vredraw|cs_hredraw|cs_owndc, wndproc, 0, 0, hinstance, null, null, (hbrush)(color_window+1), null, l"dx9_tutorial1_class", null}; registerclassex(&wc); hwnd hmainwnd = createwindow(l"dx9_tutorial1_class", l"directx 9 bare bones tutorial 1", ws_overlappedwindow, 100, 100, 300, 300, null, null, hinstance, null); direct3d = direct3dcreate9(d3d_sdk_version); d3dpresent_parameters presentparams; memset(&presentparams, 0, sizeof(d3dpresent_parameters)); presentparams.windowed = true; presentparams.swapeffect = d3dswapeffect_discard; direct3d->createdevice(d3dadapter_default, d3ddevtype_hal, hmainwnd, d3dcreate_software_vertexprocessing, &presentparams, &direct3ddevice); direct3ddevice->setrenderstate(d3drs_lighting, false); direct3ddevice->setrenderstate(d3drs_alphablendenable, true); direct3ddevice->setrenderstate(d3drs_srcblend, d3dblend_srcalpha); direct3ddevice->setrenderstate(d3drs_destblend, d3dblend_invsrcalpha); direct3ddevice->settexturestagestate(0, d3dtss_alphaop, d3dtop_modulate); texture = loadtexture(l"ash.png"); showwindow(hmainwnd, nshow); updatewindow(hmainwnd); while (getmessage(&msg, null, 0, 0)) { translatemessage(&msg); dispatchmessage(&msg); } direct3ddevice->release(); direct3d->release(); return 0; } lresult winapi wndproc(hwnd hwnd, uint msg, wparam wparam, lparam lparam) { switch (msg) { case wm_destroy: postquitmessage(0); return 0; case wm_paint: rect* rect = new rect; rect->left = 10; rect->top = 10; rect->bottom = 60; rect->right = 60; direct3ddevice->clear(0, null, d3dclear_target, d3dcolor_xrgb(0, 0, 0), 1.0f, 0); direct3ddevice->beginscene(); blitd3d(texture, rect, d3dcolor_argb(1, 1, 1,1), 0); direct3ddevice->endscene(); direct3ddevice->present(null, null, null, null); validaterect(hwnd, null); return 0; } return (defwindowproc(hwnd, msg, wparam, lparam)); } //load texture file d3dx //supported formats: bmp, ppm, dds, jpg, png, tga, dib idirect3dtexture9 *loadtexture(wchar_t *filename) { idirect3dtexture9 *d3dtexture; //use magenta colourkey d3dcolor colorkey = 0xffff00ff; // load image file if (failed(d3dxcreatetexturefromfileex (direct3ddevice, filename, 0, 0, 1, 0, d3dfmt_a8r8g8b8, d3dpool_managed, d3dx_filter_none, d3dx_default, colorkey, null, null, &d3dtexture))) { return null; } //return newly made texture return d3dtexture; } //draw textured quad on back-buffer void blitd3d (idirect3dtexture9 *texture, rect *rdest, d3dcolor vertexcolour, float rotate) { const dword d3dfvf_tlvertex = d3dfvf_xyzrhw | d3dfvf_diffuse | d3dfvf_tex1; struct tlvertex { float x, y, z, rhw; d3dcolor color; float u; float v; }; idirect3dvertexbuffer9* vertexbuffer; // set vertex shader. direct3ddevice->setvertexshader(null); direct3ddevice->setfvf(d3dfvf_tlvertex); // create vertex buffer. direct3ddevice->createvertexbuffer(sizeof(tlvertex) * 4, null, d3dfvf_tlvertex, d3dpool_managed, &vertexbuffer, null); direct3ddevice->setstreamsource(0, vertexbuffer, 0, sizeof(tlvertex)); tlvertex* vertices; //lock vertex buffer vertexbuffer->lock(0, 0, (void**)&vertices, null); //setup vertices //a -0.5f modifier applied vertex coordinates match texture //and screen coords. drivers may compensate //automatically, on others texture alignment errors introduced //more information on can found in direct3d 9 documentation vertices[0].color = vertexcolour; vertices[0].x = (float) rdest->left - 0.5f; vertices[0].y = (float) rdest->top - 0.5f; vertices[0].z = 0.0f; vertices[0].rhw = 1.0f; vertices[0].u = 0.0f; vertices[0].v = 0.0f; vertices[1].color = vertexcolour; vertices[1].x = (float) rdest->right - 0.5f; vertices[1].y = (float) rdest->top - 0.5f; vertices[1].z = 0.0f; vertices[1].rhw = 1.0f; vertices[1].u = 1.0f; vertices[1].v = 0.0f; vertices[2].color = vertexcolour; vertices[2].x = (float) rdest->right - 0.5f; vertices[2].y = (float) rdest->bottom - 0.5f; vertices[2].z = 0.0f; vertices[2].rhw = 1.0f; vertices[2].u = 1.0f; vertices[2].v = 1.0f; vertices[3].color = vertexcolour; vertices[3].x = (float) rdest->left - 0.5f; vertices[3].y = (float) rdest->bottom - 0.5f; vertices[3].z = 0.0f; vertices[3].rhw = 1.0f; vertices[3].u = 0.0f; vertices[3].v = 1.0f; //unlock vertex buffer vertexbuffer->unlock(); //set texture direct3ddevice->settexture (0, texture); //draw image direct3ddevice->drawprimitive (d3dpt_trianglefan, 0, 2); }
you have several issues prevent app proper rendering.
your code hairy mess. every function whatever wants. impossible read , find bugs. instead make number of functions, doing concrete tasks, separating responsibilities, can focus (debug, improve) @ 1 task time.
no error checking. naver find bug if (1) dont know happened, (2) happened. after every single function call, must check possible errors. directx api makes easy: can @
hresult
variable, returned function, "decode" it, , in case of fail change program flow handle error or make warning. see code below.use
d3dfmt_unknown
ind3dxcreatetexturefromfileex
if not sure format image file has:d3dxcreatetexturefromfileex (direct3ddevice, filename, 0, 0, 0, 0, d3dfmt_unknown, d3dpool_managed, d3dx_filter_none, d3dx_default, colorkey, 0, 0, &d3dtexture)
never use
wm_paint
trigger rendering. app rendering 1 frame. instead, put rendering in main loop.
full source long , formatting little pain, i've uploaded pastebin: link (you must add linker input options dxerr.lib
dxsdk or here )
further reading:
- msdn
d3dxcreatetexturefromfileex
- error handling:
hresult
, common hresult values - directx 9 tutorials: chad vernon, drunken hyena, directxtutorial.com
Comments
Post a Comment