Sign in to follow this  

Surfaces wont work for some reason

This topic is 3673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, i've been having this problem for awhile now. Now matter what I do, surfaces wont render. Ok, here's my code: DLL:

.h file:
#ifndef _DIRECT_3D_RENDER9
#define _DIRECT_3D_RENDER9


//Include the DirectX headers.
#include <d3d9.h>
#include <d3dx9.h>
#include <windows.h>


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


#define DIRECT3D9_RENDER __declspec( dllexport )

extern LPDIRECT3D9 Direct_3D;
extern LPDIRECT3DDEVICE9 Direct_3D_Device;
extern LPDIRECT3DSURFACE9 backBuffer;

//Make a function to initialize Direct3D.
DIRECT3D9_RENDER int Initialize_Renderer(HWND, int, int, bool, IDirect3DSurface9* surface);
//Make a functio to render
DIRECT3D9_RENDER int Render();
//Create the device.
DIRECT3D9_RENDER LPDIRECT3DDEVICE9 CreateDevice(HWND);
//Get's the device.
DIRECT3D9_RENDER LPDIRECT3DDEVICE9 Getdevice();
//Get's Direct3D
DIRECT3D9_RENDER LPDIRECT3D9 ReturnDirect3D();
//Returns the back buffer
DIRECT3D9_RENDER IDirect3DSurface9* ReturnBackBuffer();

#endif

.cpp file:
#include "C_Direct3D9.h"


#pragma warning(disable : 4860)

LPDIRECT3D9 Direct_3D;
LPDIRECT3DDEVICE9 Direct_3D_Device;
LPDIRECT3DSURFACE9 backBuffer;

DIRECT3D9_RENDER int Initialize_Renderer(HWND hwnd, int width, int height, bool fullscreen,IDirect3DSurface9* surface)
{
	HRESULT result;
	
	Direct_3D = Direct3DCreate9(D3D_SDK_VERSION);

	if(!Direct_3D)
	{
		MessageBoxA(NULL,"Failed to initialize Render","Error",MB_OK);
		return 0;
	}

	D3DPRESENT_PARAMETERS Direct_3D_Presentation;
	ZeroMemory(&Direct_3D_Presentation,sizeof(Direct_3D_Presentation));
	Direct_3D_Presentation.Windowed					= (!fullscreen);
	Direct_3D_Presentation.SwapEffect				= D3DSWAPEFFECT_COPY;
	Direct_3D_Presentation.BackBufferFormat			= D3DFMT_X8R8G8B8;
	Direct_3D_Presentation.BackBufferCount			= 1;
	Direct_3D_Presentation.BackBufferWidth			= width;
	Direct_3D_Presentation.BackBufferHeight			= height;
	Direct_3D_Presentation.hDeviceWindow			= hwnd;
	Direct_3D_Presentation.EnableAutoDepthStencil	= true;
	Direct_3D_Presentation.AutoDepthStencilFormat	= D3DFMT_D16;
	//Direct_3D_Presentation.PresentationInterval		= D3DPRESENT_INTERVAL_DEFAULT;
	Direct_3D_Presentation.PresentationInterval		= D3DPRESENT_INTERVAL_IMMEDIATE;
	

	Direct_3D->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hwnd,
		D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&Direct_3D_Presentation,
		&Direct_3D_Device);

	if(Direct_3D == NULL)
	{
		MessageBoxA(NULL,"The divice was not create","Tough luck - Jack", MB_OK);
		return 0;
	}
	return 1;
	

	

	
}

DIRECT3D9_RENDER LPDIRECT3DDEVICE9 GetDevice()
{
	return Direct_3D_Device;
}

DIRECT3D9_RENDER LPDIRECT3D9 ReturnDirect3D()
{
	return Direct_3D;
}

DIRECT3D9_RENDER IDirect3DSurface9* ReturnBackBuffer()
{
	return backBuffer;
}


My questio is, do i need to set up Direct3D inside the project file (main one - exe) for the surfaces to work? cause i've tried everything i could think of and nothing worked, I want the DirectX code to be hidden. Hopefully you can help me with this. PS: I tried to do GetBackBuffer() witnin the function to create the device, bur that didn't work either, please, if anyone can help, i'd appreciate it

Share this post


Link to post
Share on other sites
Define "surfaces" and "won't render". You can't render surfaces, only textures (which contain surfaces). Do you get any errors? Do you get any debug output? Are you using the debug runtimes?

What is the surface pointer going into the initialise function? You can't have created any surface before the device, and you can't return a surface pointer in it (It'd have to be a pointer to a pointer), so it seems like a pointless parameter? Or is that the problem? It's a duff pointer when you return from the function?

Share this post


Link to post
Share on other sites
As Steve suggests, some debugging output and results of your runtime analysis will be very useful. There is surprisingly little that we can do from static code analysis alone.

You don't appear to be calling AddRef() on your returned interfaces either - could be a quick trip to Memory Leak City for you if you're not careful.

hth
Jack

Share this post


Link to post
Share on other sites
Ok, a surface that i'm trying to render is basicly a .bmp file. I'm following the code from "Begining Game Programming" second edition by Jonathan S. Harbour.

in the book, he has a function that gets the backbuffer from the pointer to a surface, I just followed his code and put it in a DLL to hide it so you wouldn't have to re-do it and all. I also made a class to load the surface defined as this:



class CSurface
{
public:

LPDIRECT3DSURFACE9 LoadSurface(char* filename,D3DCOLOR color);
void ReleaseSurface();
void DrawSurface(LPDIRECT3DSURFACE9 surface);
//void CreateRendableSurface(ID3DXRenderToSurface render_surface, int width, int height,D3DFORMAT format, bool depthstencil, D3DFORMAT Depstencilformaot, LPDIRECT3DSURFACE9 surface);
LPDIRECT3DSURFACE9 GetBackbuffer();

private:
LPDIRECT3DSURFACE9 surface;
LPDIRECT3DSURFACE9 backbuffer;
LPDIRECT3DDEVICE9 dev;
};

LPDIRECT3DSURFACE9 CSurface::LoadSurface(char *filename, D3DCOLOR color)
{
CSurface::surface = NULL;
D3DXIMAGE_INFO info;
HRESULT result;

//get the width and height from bitmap file
result = D3DXGetImageInfoFromFile(filename,&info);
if(!result)
return 0;

result = dev->CreateOffscreenPlainSurface(
info.Width,
info.Height,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&surface,
NULL);

if(!result)
return 0;

result = D3DXLoadSurfaceFromFile(
surface,
NULL,
NULL,
filename,
NULL,
D3DX_DEFAULT,
color,
NULL);

//make sure the file was loaded okay
if(!result)
return 0;
}

void CSurface::DrawSurface(LPDIRECT3DSURFACE9 surface)
{


GetDevice()->StretchRect(surface,NULL,backbuffer,NULL,D3DTEXF_NONE);

}
LPDIRECT3DSURFACE9 CSurface::GetBackbuffer()
{
return CSurface::backbuffer;
}




Not sure if it's the code from the DLL or something wrong with my functions for loading the surface.

Share this post


Link to post
Share on other sites
Quote:
Original post by Enerjak
in the book, he has a function that gets the backbuffer from the pointer to a surface
The backbuffer IS a surface, and the only way to get it is via IDirect3DDevice9::GetBackBuffer(). Can we see the code where you try do this? And does the code work if it's not in a DLL?

That LoadSurface() function looks extremely dodgey to me. Nowhere in that code do you ever initialise the "backbuffer" variable. It's pointing into random memory somewhere.

Share this post


Link to post
Share on other sites
ok, before I had the GetBackBuffer() inside the Initialize_Direct3D()

that didn't work, then I made a backbuffer in my game.cpp file and did this:



void Game::Game_Run(HWND hwnd)
{

GetDevice()->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&BackBuffer);

GetDevice()->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,1);

//Set the frame rate to a certain var
if(GetTickCount() - start >= 30)
{
}



if(!GetDevice()->BeginScene())
{

Surface->DrawSurface(Back_surface);


GetDevice()->EndScene();
}

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



if(KEY_DOWN(VK_ESCAPE)))
{
PostMessageA(hwnd, WM_DESTROY,0,0);
MessageBox(hwnd,"The window is about to close","Have a nice day",MB_OK);
}
}



That didn't work. So, i don't know if i should try to put the initializing code in files in the main project and if that'll make a difference. I just don't know why i'm doing wrong.

Share this post


Link to post
Share on other sites
would u mind if I gave you my project? Also I put a GetDirectDevice()->getbackbuffer in the function that loads the surface that didn't work either, so I don't know. I'm going to try to re-write it in the way the books shows. I'll let you know how that turns out.

Share this post


Link to post
Share on other sites

This topic is 3673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this