Skip to main content
GameDev.net gamedev.net
🔒 Locked

directx texture question

Started by hpotter936 Aug 25, 2008 at 7:04 AM 4 replies 600+ views
Original Post
hpotter936
hpotter936
if i have a LPDIRECT3DTEXTURE9 texture can i draw that texture more than once in a frame when ever i try none are drawn at all but it seems a waste to load the same texture more than once
Evil Steve
Evil Steve
Yes, you can. If that doesn't work, we'll need to see your code.
wforl
wforl
Yep,

Thats the way it works, when ever loading in models or anything like that, you should try to make sure the same texture has not already been loaded, and if it has, have your new model's texture container or whatever point to that texture.

The texture data will generally be stored on the card, so optimizations like these are really important.

For example, when you create a bitmap font, such as

http://www.noritake-itron.com/Softview/font5x7int.gif

when you repeat the same letter, you're not loading that portion of the texture image twice, you're repeating that specific U/V co-ordinates twice.

And if you still cant get what your doing to work, its generally best to start of with an existing working app, and modify that. You'll be surprised some of the parameters and configurations etc you forget to setup. I don't think that many people start off from scratch every time, its just easier and less error prone not to in my opinion.

And if you havent already got it, make sure you pick up Frank Lunas - A shader Approach, that book has so many great examples, and are perfect for the beginnings of experiments/modifications and such.

Hope this helps.

hpotter936
hpotter936
ok thanks for the reply's but i still cant get this to work its driving my crazy here's the code im using:

#include
#include
#include

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

LPDIRECT3D9 d3d;
LPD3DXSPRITE sptInterface;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DTEXTURE9 pacMan;
LPDIRECT3DTEXTURE9 pacDot;
D3DXVECTOR3 pacDotPosition[10];
D3DXVECTOR3 pacManPosition(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 defaultCenter(0.0f, 0.0f, 0.0f);
RECT framePosition;
int frameNumber = 0;
int addFrame = 0;


void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
bool collision(void);

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
"WindowClass",
"PacMan",
WS_OVERLAPPEDWINDOW,
300, 300,
640, 480,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);

initD3D(hWnd);

MSG msg;

while(TRUE)
{
DWORD starting_point = GetTickCount();

if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

render_frame();
if(KEY_DOWN(VK_LEFT)) {
pacManPosition.x -= 3;
}
if(KEY_DOWN(VK_RIGHT)) {
pacManPosition.x += 3;
}
if(KEY_DOWN(VK_UP)) {
pacManPosition.y -= 3;
}
if(KEY_DOWN(VK_DOWN)) {
pacManPosition.y += 3;
}

while ((GetTickCount() - starting_point) < 25);
}

cleanD3D();

return msg.wParam;
}


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc (hWnd, message, wParam, lParam);
}


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;

d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
D3DXCreateSprite(d3ddev, &sptInterface);
D3DXCreateTextureFromFileEx(d3ddev, "pacManSprite.bmp", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &pacMan);
D3DXCreateTextureFromFileEx(d3ddev, "pacDot.bmp", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &pacDot);
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3ddev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
/*for(int i = 1; i == 5; ++i) {
for(int j = 1; j == 10; ++j) {
pacDotPosition[j].x = j*10;
}
}
for(int i = 1; i == 5; ++i) {
for(int j = 0; j == 10; ++j) {
pacDotPosition[j].y = j*10;
}
}*/
pacDotPosition[1].x = 10;
pacDotPosition[2].x = 20;
pacDotPosition[3].x = 30;
pacDotPosition[4].x = 40;
pacDotPosition[5].x = 50;
pacDotPosition[6].x = 60;
pacDotPosition[7].x = 70;
pacDotPosition[8].x = 80;
pacDotPosition[9].x = 90;
pacDotPosition[10].x = 100;
pacDotPosition[1].y = 0;
pacDotPosition[2].y = 0;
pacDotPosition[3].y = 0;
pacDotPosition[4].y = 0;
pacDotPosition[5].y = 0;
pacDotPosition[6].y = 0;
pacDotPosition[7].y = 0;
pacDotPosition[8].y = 0;
pacDotPosition[9].y = 0;
pacDotPosition[10].y = 0;
if(d3ddev->SetTexture(NULL, pacDot) != S_OK)
MessageBox(hWnd, "error", "error", MB_OK);


return;
}


void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
SetRect(&framePosition, 64*frameNumber, 0, 64*(frameNumber + 1), 64);
++addFrame;
if(addFrame == 6) {
if(frameNumber < 2)
++frameNumber;
else
frameNumber = 0;
addFrame = 0;
}
d3ddev->BeginScene();
sptInterface->Begin(NULL);
sptInterface->Draw(pacMan, &framePosition, &defaultCenter, &pacManPosition, D3DCOLOR_XRGB(255,255,255));
//why wont this work?????
for(int i = 1; i == 10; ++i) {
sptInterface->Draw(pacDot, NULL, &defaultCenter, &pacDotPosition, D3DCOLOR_XRGB(255,255,255));
}
sptInterface->End();

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);

return;
}


void cleanD3D(void)
{
d3ddev->Release();
d3d->Release();
sptInterface->Release();
pacMan->Release();
return;
}

bool collision(void) {
//TODO: detect collison
return true;
}

can anyone tell me why its not working thanks
Evil Steve
Evil Steve
Oh god, it's another DirectX Tutorial case...

There's lots of things wrong with that code, mainly you don't check any return values, so when a function fails you ignore it and carry on. See This Link on setting up the device correctly, and This link on using the debug runtimes for a start.

I tried that code myself, and it works for me (Apart from the memory leaks at the end).
However, I assume you want the loop to execute? It should be:
for(int i = 1; i<=10; ++i) {
(Assuming you want to use indices 1 through 10) Not
for(int i = 1; i == 10; ++i) {
It's also worth noting that in C++, array indices start at 0, not 1, so you have an array overflow there. You should use indices 0..9, and make the loop:
for(int i=0; i<10; ++i) {

I'd highly recommend getting up to speed on C++ and learning how to use your Debugger, otherwise you'll just keep running into problems like this that you won't know how to solve.
hpotter936
hpotter936
thanks you very much its finaly solved and to think it was something as simple as an incorrect usage of the for loop and you'll be happy to know im reading through those links you gave me maby it time to go back to the basics lol.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.