Sign in to follow this  

not a member

This topic is 4110 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

Any one knows any clues, why does the second code fails? In the first one i recognises the methods and in the second no. What the !!
//CODE1

// Beginning Game Programming
// Chapter 5
// d3d_windowed program

//header files to include
#include <d3d9.h>
#include <d3dx9.h>
#include <time.h>

//application title
#define APPTITLE "Load_Bitmap"

//screen resolution
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

//macros to read the keyboard asynchronously
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

//forward declarations
LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
ATOM MyRegisterClass(HINSTANCE);
int Game_Init(HWND);
void Game_Run(HWND);
void Game_End(HWND);

//Direct3D objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECT3DSURFACE9 backbuffer = NULL;
LPDIRECT3DSURFACE9 surface = NULL;

//window event callback function
LRESULT WINAPI
WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
		case WM_DESTROY:
		Game_End(hWnd);
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

//helper function to set up the window properties
ATOM
MyRegisterClass(HINSTANCE hInstance)
{
	//create the window class structure
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX);

	//fill the struct with info
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WinProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = APPTITLE;
	wc.hIconSm = NULL;

	//set up the window with the class info
	return RegisterClassEx(&wc);
}

//entry point for a Windows program
int WINAPI
WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
	// declare variables
	MSG msg;

	// register the class
	MyRegisterClass(hInstance);

	// initialize application
	//note--got rid of initinstance
	HWND hWnd;

	//create a new window
	hWnd = CreateWindow(
		APPTITLE, //window class
		APPTITLE, //title bar
		WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP, //window style
		CW_USEDEFAULT, //x position of window
		CW_USEDEFAULT, //y position of window
		SCREEN_WIDTH, //width of the window
		SCREEN_HEIGHT, //height of the window
		NULL, //parent window
		NULL, //menu
		hInstance, //application instance
		NULL); //window parameters

	//was there an error creating the window?
	if (!hWnd)
		return FALSE;

	//display the window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	//initialize the game
	if (!Game_Init(hWnd))
		return 0;

	// main message loop
	int done = 0;
	while (!done)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			//look for quit message
			if (msg.message == WM_QUIT)
			{
				MessageBox(hWnd, "Received WM_QUIT message", "WinMain", MB_OK);
				done = 1;
			}

			//decode and pass messages on to WndProc
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
			//process game loop (else prevents running after window is closed)
			Game_Run(hWnd);
	}
	return msg.wParam;
}



int
Game_Init(HWND hwnd)
{
	HRESULT result;

	//initialize Direct3D
	d3d = Direct3DCreate9(D3D_SDK_VERSION);

	if (d3d == NULL)
	{
		MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
		return 0;
	}

	//set Direct3D presentation parameters
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;
	d3dpp.hDeviceWindow = hwnd;

	//create Direct3D device
	d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hwnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&d3ddev);

	if (d3ddev == NULL)
	{
		MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
		return 0;
	}
	//set random number seed
	srand(time(NULL));
	
	//clear the backbuffer to black
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
	
	//create pointer to the back buffer
	d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
	
	//create surface
	result = d3ddev->CreateOffscreenPlainSurface(
		640, //width of the surface
		480, //height of the surface
		D3DFMT_X8R8G8B8, //surface format
		D3DPOOL_DEFAULT, //memory pool to use
		&surface, //pointer to the surface
		NULL); //reserved (always NULL)
	
	if (result != D3D_OK)
		return 1;

	//load surface from file
	result = D3DXLoadSurfaceFromFile(
		surface, //destination surface
		NULL, //destination palette
		NULL, //destination rectangle
		"C:\\Documents and Settings\\German\\My Documents\\Visual Studio 2005\\Projects\\Create_Surface_from_File\\c.bmp", //source filename
		NULL, //source rectangle
		D3DX_DEFAULT, //controls how image is filtered
		0, //for transparency (0 for none)
		NULL); //source image info (usually NULL)

	//make sure file was loaded okay
	if (result != D3D_OK)
		return 1;

	//draw surface to the backbuffer
	d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE);

	//return okay
	return 1;
}

void Game_Run(HWND hwnd)
{
	RECT rect;
	//make sure the Direct3D device is valid
	if (d3ddev == NULL)
		return;

	//clear the backbuffer with a white color
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0);

	//start rendering
	if (d3ddev->BeginScene())
	{
		//create pointer to the back buffer
		d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

		//draw surface to the backbuffer
		rect.top = 0;
		rect.left = 0;
		rect.right = 100;
		rect.bottom = 100;
		d3ddev->StretchRect(surface, NULL, backbuffer, &rect, D3DTEXF_NONE);

		//stop rendering
		d3ddev->EndScene();
	}

	//display the back buffer on the screen
	d3ddev->Present(NULL, NULL, NULL, NULL);

	//check for escape key (to exit program)
	if (KEY_DOWN(VK_ESCAPE))
		PostMessage(hwnd, WM_DESTROY, 0, 0);
}

void Game_End(HWND hwnd)
{
	//free the surface
	surface->Release();

	//release the Direct3D device
	if (d3ddev != NULL)
		d3ddev->Release();

	//release the Direct3D object
	if (d3d != NULL)
		d3d->Release();
}


//CODE2

#include "mapADT.h"
#include <string.h>
#include "d3dx9tex.h"
#include "d3dx9.h"
#include "d3d9.h"

#define TRUE 1
#define FALSE 0



struct tStruct{
	LPDIRECT3DSURFACE9 surface;
	int x_position;
	int y_position;
	long graphic;
	int blocked;
	int navigable;
	int mov_dif;
	int extra;
};
typedef struct tStruct tileStruct;

struct mapCDT{
	char* tiles_path;
	long map_width;
	long map_hieght;
	int num_tiles_x;
	int num_tiles_y;
	long tile_width;
	long tile_hieght;
	LPDIRECT3DDEVICE9 d3ddev;
	LPDIRECT3DSURFACE9 tilesSurf;
	tileStruct** tileMatrix;
};

tileStruct** setTileMatrix(mapADT map);
LPDIRECT3DSURFACE9 getTiles(char* tile_images, LPDIRECT3DDEVICE9 d3ddev);
char* getPath(const char* p);


mapADT
NewMap(const char* tile_images, int num_tiles_x, int num_tiles_y,
	   long tile_width, long tile_hieght, LPDIRECT3DDEVICE9 d3ddev)
{
	mapADT map;

	if ((map = malloc(sizeof(mapADT))) == NULL)
		return NULL;
	
	if (((char*)map->tiles_path = (char*)getPath(tile_images)) == NULL)
	{
		free(map);
		return NULL;
	}

	map->tile_hieght = tile_hieght;
	map->tile_width = tile_width;
	map->num_tiles_x = num_tiles_x;
	map->num_tiles_y = num_tiles_y;
	map->map_hieght = (long)num_tiles_y * tile_hieght;
	map->map_width = num_tiles_x * tile_width;

	map->d3ddev = d3ddev;

	map->tilesSurf = (LPDIRECT3DSURFACE9)getTiles(tile_images, d3ddev);
	if((map->tileMatrix = (tileStruct**)setTileMatrix(map)) == NULL)
	{
		map->tilesSurf->Release();      //ERROR1
		free(map->tiles_path);
		free(map);
		return NULL;
	}
	
	return map;
}

// Auxiliary functions



tileStruct** 
setTileMatrix(mapADT map)
{
	int i, j, ok;
	tileStruct **ret;

	ok = TRUE;

	if((ret = malloc(sizeof(ret)* map->num_tiles_x)) == NULL)
		return NULL;
	for(i=0; i<map->num_tiles_x; i++)
	{
		if((ret[i]= malloc(sizeof(*ret) * map->num_tiles_y)) == NULL)
		{
			ok = FALSE;
			break;
		}
	}
	if(ok == FALSE && i > 0)
	{
		while(--i)
			free(ret[i]);
		free(ret);
		return NULL;
	}

	for(i=0; i<map->num_tiles_x; i++)
	{
		for(j=0; j<map->num_tiles_y; j++)
		{
			ret[i][j].blocked = 0;
			ret[i][j].extra = 0;
			ret[i][j].graphic = -1;
			ret[i][j].mov_dif = 10;
			ret[i][j].navigable = 0;
			ret[i][j].surface = NULL;
			ret[i][j].x_position = i;
			ret[i][j].y_position = j;
		}
	}

	return ret;
}

LPDIRECT3DSURFACE9
getTiles(char* tile_images, LPDIRECT3DDEVICE9 d3ddev)
{
	D3DXIMAGE_INFO pSrcInfo;
	LPDIRECT3DSURFACE9 surface;
	int ret;

	if( D3DXGetImageInfoFromFile( tile_images, &pSrcInfo) != D3D_OK)
		return NULL;
	
	ret = d3ddev->CreateOffscreenPlainSurface(
		pSrcInfo.Width,
		pSrcInfo.Hieght
		D3DFMT_X8R8G8B8,
		D3DPOOL_DEFAULT,
		&surface,
		NULL);      //ERROR2

	if(ret != D3D_OK)
		return NULL;

	ret = D3DXLoadSurfaceFromFile(
		surface,
		NULL,
		NULL,
		tile_images,
		NULL,
		D3DX_DEFAULT,
		0,
		NULL);

	if(ret != D3D_OK)
		return NULL;

	return surface;
}

char*
getPath(const char* p)
{
	char* path;

	if((path = malloc(sizeof(char)*(strlen(p)+1))) == NULL )
		return NULL;

	strcpy(path, p);

	return path;
}


//2th CODE errors: /* \mapADT.c(69) : error C2039: 'Release' : is not a member of 'IDirect3DSurface9' 1> C:\Program Files\Microsoft DirectX SDK (June 2006)\Include\d3d9.h(1537) : see declaration of 'IDirect3DSurface9' 1>.\mapADT.c(136) : error C2039: 'CreateOffscreenPlainSurface' : is not a member of 'IDirect3DDevice9' 1> C:\Program Files\Microsoft DirectX SDK (June 2006)\Include\d3d9.h(347) : see declaration of 'IDirect3DDevice9' 1>Build log was saved at "file://c:\Documents and Settings\German\My Documents\Visual Studio 2005\Projects\a_game\a_game\Debug\BuildLog.htm" 1>a_game - 2 error(s), 0 warning(s) */
Edit by Fruny: formatting [Edited by - Fruny on September 14, 2006 5:05:45 PM]

Share this post


Link to post
Share on other sites
Just a tip: use the source tag to preserve the formatting of your code on this forum. And no, I don't see what's wrong, mainly because it's an awfull lot of unformatted code. What exactly did you change before the code broke?

Share this post


Link to post
Share on other sites
Let me guess: Code 1 is a .cpp file, and Code 2 is a .c file.

DirectX uses C++ (though there are ways to do it using C, they're just not as pretty). When you try to use DirectX objects as though they were C++ objects, but you're compiling in C, then the compiler is going to throw a fit. At the minimum, try to change the file extension to .cpp, and see if that's enough.

Share this post


Link to post
Share on other sites

This topic is 4110 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