Texture fail to render - DirectX 9 - c++

I have this code in DirectX 9, in which I need to get the DC from a COM interface and draw it. I do get the DC and it contains the image, but I get a black screen. Any ideas why?
LPDIRECT3DSURFACE9 pRenderSurface = NULL, pRenderSurfaceTMP = NULL;
m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface);
if (pRenderSurface == NULL)
return FALSE;
m_pD3DDevice->CreateOffscreenPlainSurface(m_nWidth, m_nHeight,
D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &pRenderSurfaceTMP, 0);
m_pD3DDevice->GetRenderTargetData(pRenderSurface,pRenderSurfaceTMP);
HDC hDC = NULL;
hr = pRenderSurfaceTMP->GetDC(&hDC);
if (FAILED(hr))
return FALSE;
if (m_pViewObject != NULL)
{
// RECT is relative to the windowless container rect
RECTL rcRect = {0, 0, m_nWidth, m_nHeight};
// Draw onto the DC!
hr = m_pViewObject->Draw(DVASPECT_CONTENT, 1,
NULL, NULL, NULL, hDC, &rcRect, NULL, NULL,
0);
}
pRenderSurface->ReleaseDC(hDC);
pRenderSurface->Release();
// Draw the surface
m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );
hr = m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
m_hbrBackground = NULL;
pRenderSurfaceTMP->Release();
Thanks in advance

Make sure that m_pRenderTexture is created with D3DUSAGE_RENDERTARGET. The size and format of both pRenderSurface and pRenderSurfaceTMP should match. For more info, see when GetRenderTargetData fails: http://msdn.microsoft.com/en-us/library/windows/desktop/bb174405(v=vs.85).aspx

Try this:
m_pD3DDevice->CreateTexture(m_nWidth, m_nHeight, 1, 0, D3DFMT_X8R8G8B8,
D3DPOOL_MANAGED, &m_pRenderTexture, NULL);
// ...
LPDIRECT3DSURFACE9 pRenderSurface = NULL;
if (FAILED(m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface))) return FALSE;
HDC hDC = NULL;
if (FAILED(pRenderSurface->GetDC(&hDC))) return FALSE;
if (m_pViewObject != NULL)
{
RECTL rcRect = {0, 0, m_nWidth, m_nHeight};
m_pViewObject->Draw(DVASPECT_CONTENT, 1,
NULL, NULL, NULL, hDC, &rcRect, NULL, NULL, 0);
}
pRenderSurface->ReleaseDC(hDC);
pRenderSurface->Release();
m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
or this:
m_pD3DDevice->CreateTexture(m_nWidth, m_nHeight, 1, 0, D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT, &m_pRenderTexture, NULL);
// ...
LPDIRECT3DSURFACE9 pRenderSurfaceTMP = NULL;
m_pD3DDevice->CreateOffscreenPlainSurface(m_nWidth, m_nHeight,
D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &pRenderSurfaceTMP, 0);
HDC hDC = NULL;
if (FAILED(pRenderSurfaceTMP->GetDC(&hDC))) return FALSE;
if (m_pViewObject != NULL)
{
RECTL rcRect = {0, 0, m_nWidth, m_nHeight};
m_pViewObject->Draw(DVASPECT_CONTENT, 1,
NULL, NULL, NULL, hDC, &rcRect, NULL, NULL, 0);
}
pRenderSurfaceTMP->ReleaseDC(hDC);
LPDIRECT3DSURFACE9 pRenderSurface = NULL;
if (FAILED(m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface)))
return FALSE;
if (FAILED(m_pD3DDevice->UpdateSurface(pRenderSurfaceTMP, NULL,
pRenderSurface, NULL))) return FALSE;
pRenderSurface->Release();
m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

Related

Failed to create render target view DirectX 11

This is my code. The final if is true so i get the messageBox that tells my the createRenderTargetView is failed. I'm pretty new to this and i searched online a lot, but found nothing, do you have any tips? Thanks.
This is the probelmatic part
//feature levels
D3D_FEATURE_LEVEL featureLevels[6]{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1,
};
//swap chain
ZeroMemory(&m_swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
m_swapChainDesc.BufferDesc.Height = 600;
m_swapChainDesc.BufferDesc.Width = 800;
m_swapChainDesc.Windowed = true;
m_swapChainDesc.BufferCount = 1;
m_swapChainDesc.OutputWindow = hwnd;
m_swapChainDesc.Flags = 0;
m_swapChainDesc.SampleDesc.Count = 1;
m_swapChainDesc.SampleDesc.Quality = 0;
m_swapChainDesc.BufferUsage = DXGI_USAGE_DISCARD_ON_PRESENT;
m_swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
m_swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
m_swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
UINT createDeviceFlags = 0;
m_swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
m_swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
m_swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
#if defined(DEBUG) || defined
(_DEBUG)createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif*/
//creazione device e swapchain
/*hr=D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, 0, 0, 0, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION,
&m_swapChainDesc, &m_swapChain, &m_device, featureLevels, &m_deviceContext );
*/
D3D_FEATURE_LEVEL featureLevel;
hr = D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, createDeviceFlags, featureLevels, 6, D3D11_SDK_VERSION, &m_device, &featureLevel, &m_deviceContext);
if (FAILED(hr))
MessageBox(0, L"D3D11CreateDevice Failed.", 0, 0);
IDXGIDevice *dxgiDevice = 0;
hr=m_device->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
if (FAILED(hr))
MessageBox(0, L"Fallita queryInterface", 0, 0);
IDXGIAdapter *dxgiAdapter = 0;
hr=dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter);
if (FAILED(hr))
MessageBox(0, L"Fallito getParent", 0, 0);
IDXGIFactory *dxgiFactory = 0;
hr=dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory);
if (FAILED(hr))
MessageBox(0, L"Fallito getParent", 0, 0);
IDXGISwapChain *m_swapChain;
hr=dxgiFactory->CreateSwapChain(m_device, &m_swapChainDesc, &m_swapChain);
if (FAILED(hr))
MessageBox(0, L"Failed crating the swap chain", 0, 0);
dxgiAdapter->Release();
dxgiDevice->Release();
dxgiFactory->Release();
ID3D11RenderTargetView *m_renderTargetView;
ID3D11Texture2D *m_backBuffer;
hr=m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&m_backBuffer));
if (FAILED(hr))
MessageBox(0, L"Failed get buffer", 0, 0);
hr=m_device->CreateRenderTargetView(m_backBuffer, NULL, &m_renderTargetView);
if (FAILED(hr))
MessageBox(0, L"Failed to create rendertargetview", 0, 0);

Capturing Entire ScreenShot Of PC in WIndows 8.1

I'm making a C++ program which takes screenshot of the entire screen. I am facing quite a problem. When I run the program, it takes screenshot of the console screen only, not the entire desktop.
HDC Screen = CreateDC(L"DISPLAY", NULL, NULL, NULL);
HDC Capture = CreateCompatibleDC(Screen);
int width = GetDeviceCaps(Screen, HORZRES);
int height = GetDeviceCaps(Screen, VERTRES);
LPBYTE lpcapture;
BITMAPINFO bmiCapture =
{ { sizeof(BITMAPINFOHEADER),width,height,1,24,BI_RGB,0,0,0,0,0 } };
HBITMAP hbmCapture = CreateDIBSection(Screen, &bmiCapture, DIB_RGB_COLORS, (LPVOID *)&lpcapture, NULL, 0);
if (hbmCapture)
{
HBITMAP hbmOld = (HBITMAP) SelectObject(Capture, Capture);
BitBlt(Capture, 0, 0, width, height, Screen, 0, 0, SRCCOPY);
SelectObject(Capture, hbmOld);
}
DeleteDC(Capture);
DeleteDC(Screen);
return hbmCapture;
This should work in a console program, but it would show black console screen in the middle
int ScreenCapture(const char* fname)
{
int result = 0;
HWND hWnd = GetDesktopWindow();
HBITMAP hbmScreen = NULL;
HDC hdcScreen = GetDC(NULL);
HDC hdcWindow = GetDC(hWnd);
int w = GetSystemMetrics(SM_CXSCREEN);
int h = GetSystemMetrics(SM_CYSCREEN);
HDC hdcMemDC = CreateCompatibleDC(hdcWindow);
if (!hdcMemDC) goto cleanup;
hbmScreen = CreateCompatibleBitmap(hdcWindow, w, h);
if (!hbmScreen) goto cleanup;
SelectObject(hdcMemDC, hbmScreen);
if (!BitBlt(hdcMemDC, 0, 0, w, h, hdcWindow, 0, 0, SRCCOPY)) goto cleanup;
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = w;
bi.biHeight = h;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((w * bi.biBitCount + 31) / 32) * 4 * h;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char* lpbitmap = (char*)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0, h, lpbitmap, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfType = 0x4D42; //'BM' for Bitmaps
DWORD temp = 0;
HANDLE hFile = CreateFileA(fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &temp, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &temp, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &temp, NULL);
CloseHandle(hFile);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
result = 1; //success
cleanup:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL, hdcScreen);
ReleaseDC(hWnd, hdcWindow);
return result;
}

Derefering a pointer to a handle is giving me "illegal, right operand.."?

I don't understand what the compiler is trying to enforce. I have a function that sets up an OpenGL context, and returns the HGLRC via a pointer:
BOOL SetupWin32Context(HDC hDC, HGLRC *phRC) {
/* do bunch of work*/
HGLRC hRC = wglCreateContext(hDC);
*phRC = hRC;
return TRUE;}
On *phRC = hRC; I get:
error C2297: '*' : illegal, right operand has type 'HGLRC *'
And that's not making any sense to me.
Full code copied from pastebin
#include "Bindings.h"
#include <Windows.h>
#include <gl\GL.h>
BOOL SetupWin32Context(HDC hDC, HGLRC *phRC)
{
HGLRC hRC = NULL;
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, /* Colordepth of the framebuffer. */
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
0, /* Bits of the depthbuffer. */
0, /* Bits of the stencilbuffer. */
0, /* Number of Aux buffers. */
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int format = ChoosePixelFormat(hDC, &pfd);
if(format == 0) return FALSE;
if(!SetPixelFormat(hDC,format, &pfd)) return FALSE;
hRC = wglCreateContext(hDC);
if(hRC == 0)
hRC = wglCreateContext(hDC);
if(hRC == 0)
return FALSE;
wglMakeCurrent(hDC, hRC)
*phRC = hRC;
return TRUE;
}
In the real code, you need a semicolon after
wglMakeCurrent(hDC, hRC)
like this:
wglMakeCurrent(hDC, hRC);

Text is rotated when use DrawText() on Bitmap?

I want to draw the text on Bitmap and I did it with the summary code below
BITMAPINFO bitmapInfo;
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = _imgWidth;
bitmapInfo.bmiHeader.biHeight = _imgHeight;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 24;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = 0;
HDC hdc = GetDC(NULL);
if (hdc == NULL)
return false;
HFONT hFont = CreateFont( 50, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial" );
if(hFont == NULL)
return false;
HBITMAP hBitmap = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER) &bitmapInfo.bmiHeader, CBM_INIT, _BRG24arrayIn, (LPBITMAPINFO) &bitmapInfo, DIB_RGB_COLORS);
if(hBitmap == NULL)
return false;
HDC hMemDC = CreateCompatibleDC(hdc);
if (hMemDC == NULL)
return false;
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemDC, hBitmap);
if( hBitmapOld == NULL )
return false;
HFONT hFontOld = (HFONT)SelectObject(hMemDC, hFont);
if ( hFontOld == NULL )
return false;
SetBkMode(hMemDC, TRANSPARENT);
SetTextColor(hMemDC, 0x0000FF00);
RECT rect;
SetRect(&rect, 0, 0, _imgWidth, _imgHeight);
if (DrawText(hMemDC, "11:41:33", -1, &rect, DT_TOP|DT_LEFT) == 0)
return false;
GetDIBits(hdc, hBitmap, 0, _imgHeight, _BRG24arrayOut, (LPBITMAPINFO)&bitmapInfo, DIB_RGB_COLORS);
return true;
The text that I want to draw is "11:41:33" and text alignment is DT_TOP|DT_LEFT
But the result is the text is rotated and occurred on the LEFT-BOTTOM as result image below
The input array _BRG24arrayIn is in BRG24 format, someone can tell me what happen?
Many thanks,
T&TGroup!
You need to negate the height in the BITMAPINFOHEADER structure to get a top-down bitmap (i.e. one where row 0 is at the top rather than the bottom). For example:
bitmapInfo.bmiHeader.biHeight = -_imgHeight;

Loading bitmap in particular way

I have a function that essentially takes a screen shot and saves a pointer to it as a structure. I would like to use the same structure for bitmaps that I load from files.
typedef struct _BITMAPCAPTURE {
HBITMAP hbm;
LPDWORD pixels;
INT width;
INT height;
} BITMAPCAPTURE;
BOOL CaptureScreen(BITMAPCAPTURE* bmpCapture)
{
BOOL bResult = FALSE;
if(!bmpCapture)
return bResult;
ZeroMemory(bmpCapture, sizeof(BITMAPCAPTURE));
HDC hdcScreen = GetDC(NULL);
HDC hdcCapture = CreateCompatibleDC(NULL);
int nWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN),
nHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
// Bitmap is stored top down as BGRA,BGRA,BGRA when used as
// DWORDs endianess would change it to ARGB.. windows COLORREF is ABGR
LPBYTE lpCapture;
BITMAPINFO bmiCapture = { {
sizeof(BITMAPINFOHEADER), nWidth, -nHeight, 1, 32, BI_RGB, 0, 0, 0, 0, 0,
} };
bmpCapture->hbm = CreateDIBSection(hdcScreen, &bmiCapture,
DIB_RGB_COLORS, (LPVOID *)&lpCapture, NULL, 0);
if(bmpCapture->hbm){
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcCapture, bmpCapture->hbm);
BitBlt(hdcCapture, 0, 0, nWidth, nHeight, hdcScreen, 0, 0, SRCCOPY);
SelectObject(hdcCapture, hbmOld);
bmpCapture->pixels = (LPDWORD)lpCapture;
bmpCapture->width = nWidth;
bmpCapture->height = nHeight;
bResult = TRUE;
}
DeleteDC(hdcCapture);
DeleteDC(hdcScreen);
return bResult;
}
The handle to the bitmap, as well as the width and height are all easy enough to get but I'm unsure of how to get the pixels.