Creating dynamic? textures.

Started by
10 comments, last by greenpig83 10 years, 1 month ago

Hi Guys,

Just wondering if I could get some guidance on creating dynamic textures (I think that is the term) for something that is in memory.

If I have a pointer to the start of a pure RGBA stream, how could I make this into a texture to display on a sprite?

I have all the framework ready for this and am using a texture from a file, but just wanting to try something different here.

Many thanks in advance smile.png

Advertisement

OK, I am close. I am upto the point where I need to copy the data back. I have a locked rect. But not sure how to do this properly.

I have this, bit it is throwing a bunch of compiler errors;

There is a RAW bitmap starting at bmpTmp that I am trying to send to the texture.


mRenderer->textureVideoGet()->LockRect(0,&lockedRect,&rect,0);
 
for(i=0;i<(videoWidth*videoHeight*3);i+=3)
{
	idx=(x*4)+y*lockedRect.Pitch*4;
 
	lockedRect.pBits[idx]=bmpTmp[i]; //b
	lockedRect.pBits[idx+1]=bmpTmp[i+1]; //g
	lockedRect.pBits[idx+2]=bmpTmp[i+2]; //r
	lockedRect.pBits[idx+3]=255; //a
 
	x++;
	if(x>=videoWidth)
	{
		x=0;
		y--;
		if(y<0)
			y=0;
	}
}

mRenderer->textureVideoGet()->UnlockRect(0);


The errors I am getting are these (one set each for each lockedRect.pBits line);

error C2036: 'void *' : unknown size
error C2120: 'void' illegal with all types

Any help would be great!

Thanks in advance smile.png

You need to cast it to an array of bytes, as a void* (array of nothings) has no type, so you can't assign void values, and you can't use the [] operator.

Just add this right after the LockRect line:

unsigned char* bytes = (unsigned char*)lockedRect.pBits;

and then replace lockedRect.pBits[...] = ...

with bytes[...] = ...

Thanks Hodgman, compiles fine now. But, all I get is a black screen that is flickering my framerate counter (FPS is the last thing drawn in the render process).

I am not getting any errors from (FAILED) though.

This is my draw call. I have simplified it for debugging purposes, so in theory I should get a shot blue line rendered (if I am correct).


RECT rect={0,640,0,480};
D3DLOCKED_RECT lockedRect={0};

hr=mRenderer->textureVideoGet()->LockRect(0,&lockedRect,&rect,0);
if(FAILED(hr))
{
	std::cout<<"Error: "<<hr<<"\r\n";
	system("PAUSE");
	return 0;
}

unsigned char* bytes=(unsigned char*)lockedRect.pBits;

for(i=0;i<20;i=i+4)
{
	bytes[i]=255;
	bytes[i+1]=0;
	bytes[i+2]=0;
	bytes[i+3]=255;
}

mRenderer->textureVideoGet()->UnlockRect(0);

// render the sprite queue
mRenderer->renderSpriteQueue();

// display framerate data
lps=mRenderer->framerateGetReal();
lps=lps-1;
if(lps<0)
	lps=0;

itoa(lps,szBuffer,10);
strcpy(szBuffer2,"Frame Rate: ");
strcat(szBuffer2,szBuffer);
strcat(szBuffer2," FPS");
mRenderer->renderDebugText(14,14,szBuffer2);

Is it something to do with how I am creating the texture?


// Sprite renders ok with this texture
if(FAILED(D3DXCreateTextureFromResourceA(d3dDevice,GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_PNG1),&pTexture)))
	MessageBox(0,"Can not find file","Resource not found",0);

// Sprite won't work with this texture
if(FAILED(D3DXCreateTexture(d3dDevice,1024,1024,0,NULL,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&pVideoTexture))) 
	MessageBox(0,"Can not create blank video texture","Resource not found",0);

Any additional help would be awesome.

BTW, I am a newbie when it comes to dynamic textures and locking (never done it before).

for(i=0;i<20;i=i+4)
{
    bytes[i]=255;
    bytes[i+1]=0;
    bytes[i+2]=0;
    bytes[i+3]=255;
}

Unless I'm missing something, you're only setting 5 pixels. Is that your intent?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.



for(i=0;i<20;i=i+4)
{
    bytes[i]=255;
    bytes[i+1]=0;
    bytes[i+2]=0;
    bytes[i+3]=255;
}
Unless I'm missing something, you're only setting 5 pixels. Is that your intent?


Yes, for the moment (for debugging purposes until I can get something on the screen.

I found that I should probably be using D3DUSAGE_DYNAMIC in my texture creation. So, I have changed my texture creation call to this;

if(FAILED(D3DXCreateTexture(d3dDevice,1024,1024,0,D3DUSAGE_DYNAMIC,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&pVideoTexture))) 
	MessageBox(0,"Can not create blank video texture","Resource not found",0);

But, now my application fails at this call.

The docs say you can't create a texture in D3DPOOL_MANAGED.

What error are you getting? What does the debug output provide?

Before you try D3DPOOL_DEFAULT, check your devcaps to ensure you can support the texture parameters.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

The docs say you can't create a texture in D3DPOOL_MANAGED.

What error are you getting? What does the debug output provide?

Before you try D3DPOOL_DEFAULT, check your devcaps to ensure you can support the texture parameters.


Ooooh, Thanks man. I have changed to D3DPOOL_DEFAULT and it gets past that error. Still not working as I am after yet, but still a step closer ;)

I'll do some more checking now that I am over that hurdle and get back to you smile.png
Now I am failing at the LockRect() call in my draw loop (0x8876086C - seems like some sort of generic error)

The DirectX control panel should have a utility for error codes. At a minimum, you can use DXGetErrorString or DXGetErrorDescription.

At best, compile with debug, use the debug libraries, and set the DirectX control panel to maximum reporting.

Actually, "best" would be to read the docs, look for examples, etc. There've been a lot of posts here on gamedev about similar problems.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement