PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

SetRenderTarget and GetBackBuffer problem

I have code that is used to draw on both the screen and surfaces, so I use SetRenderTarget to select the target surface, or bypass it to draw to the screen.

The problem comes in when I have code that uses a surface lock().

I call
Code:

Display * display= GetDisplay();

display->GetBackBuffer()->Lock(buffer, false);

for(int i=0; i<32; i++)
*( buffer.pixels + i ) = Color(255,0,0);

display->GetBackBuffer()->Unlock();

display->Update();


which works perfect for drawing to the screen, however, if I have used
Code:

Display * display= GetDisplay();

display->SetRenderTarget(tempscreen);

display->GetBackBuffer()->Lock(buffer, false);

...

The actual screen is still draw to instead of the surface. Any other operations like SetPixel, DrawLine, etc. work correctly when the target is the tempscreen.

I also looked into using a Rasterizer, but there's no way to code it so that either a Rasterizer or Display object can be passed, not to mention it seems as if ->Lock() is not available to a Rasterizer.

Any ideas?
After inspecting the PF source, I've come across this:

Code:

Surface* Display::GetBackBuffer()
{
if (m_driver->BeginScene())
{
m_backbuffer.SetBuffer( m_driver->GetBackBuffer() );
}

// You shouldn't call GetBackBuffer() before calling Update()
if (!m_backbuffer.m_buffer)
return 0;

SetRenderTarget( &m_backbuffer );

return &m_backbuffer;
}


So, it appears that calling display->GetBackBuffer() calls SetRenderTarget() and sets the target back to the display instead of the surface I want to draw on...

Thierry, would there be any way possible to modify this so that it would just return the pointer to the render target - if one was set? Or am I just out of luck?

The whole reason for needing this is to do transistions between modes in the game, so I grab the current back buffer, run one cycle of the new mode to a temp surface, and then do an effect between the surfaces. The functions that run the modes is called with mode1(Display * display), so they don't need to be aware if they are drawing to the screen or the temp surface.

It all works really well, except for not being able to call the Lock() (which is why GetBackBuffer is needed in the first place). Maybe if Lock() was accessable directly on the display object...
Why not just add a new function?

Display *display = GetDisplay();
Surface *current = display->GetCurrentTarget();

-John
Thanks for the idea Presto...

Adding a new function works - although, I will have to modify a few lines here and there for it to work.

I can call GetTargetSurface instead of GetBackBuffer, which will call GetBackBuffer if no target has been selected... ( I think )

Code:

Surface* Display::GetTargetSurface(void)
{
if(CurrentRenderTarget)
return CurrentRenderTarget;

return GetBackBuffer();
}

Surface* Display::SetRenderTarget( Surface* target )
{
if (target==0)
{
CurrentRenderTarget = NULL;
target = GetBackBuffer();
}
else
{
CurrentRenderTarget = target;
}

return Rasterizer::SetRenderTarget( target );
}


CurrentRenderTarget is a Surface* declared in display.h.