First and foremost, hello to all of you. I've been visiting here for quite some time trying to figure out various compiler errors in my journey to program in directX and I've seen no less than kind people and amazing coders.
After buying the book "Beginning Game Programming", and reading threw the first few chapters, I became rather excited with the idea of creating a simple 3d world and there fore decided to Make that my senior project goal. I was able to get threw the book without a hitch up until chapter 6, when the directx header file d3dx9.h come into play. After trying to include that file I have encountered nothing but compiler errors and linker errors.
Here is where I would greatly appreciate some help.
// Beginning Game Programming
// Chapter 6
// Load_Bitmap program
//header files to include
#pragma comment(lib, "libd3d9.a")
#include <d3d9.h>
#include <d3dx9.h>
#include <time.h>
//application title
#define APPTITLE "Create_Surface"
//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)
//screen resolution
#define SCREEN_WIDTH 1440
#define SCREEN_HEIGHT 900
//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)
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 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 into newly created surface
result = D3DXLoadSurfaceFromFile(
surface, //destination surface
NULL, //destination palette
NULL, //destination rectangle
"legotron.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;
//return okay
return 1;
}
void Game_Run(HWND hwnd)
{
//make sure the Direct3D device is valid
if (d3ddev == NULL)
return;
//start rendering
if (d3ddev->BeginScene())
{
//create pointer to the back buffer
d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
//draw surface to the backbuffer
d3ddev->StretchRect(surface, NULL, backbuffer, NULL, 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();
}
After fixing the linker errors and missing header files, as well as downloading the latest direct x SDK I have gotten down to these few errors.
In file included from C:/Dev-Cpp/include/d3dx9math.h:1787,
from C:/Dev-Cpp/include/d3dx9.h:47,
from ../Documents and Settings/Wil/Desktop/game/winmain.cpp:8:
C:/Dev-Cpp/include/d3dx9math.inl: In static member function `static void* _D3DXMATRIXA16::operator new(size_t)':
C:/Dev-Cpp/include/d3dx9math.inl:993: warning: `operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)
C:/Dev-Cpp/include/d3dx9math.inl: In static member function `static void* _D3DXMATRIXA16::operator new [](size_t)':
C:/Dev-Cpp/include/d3dx9math.inl:1008: warning: `operator new' must not return NULL unless it is declared `throw()' (or -fcheck-new is in effect)
To put it simply, I have no idea how to fix this error. I have not messed with d3dx9math.h or have the slightest clue of where it might be. If you have any ideas or solutions to this problem I would greatly appreciate any help.
A few little extra details that may help with identifying my errors:
IDE: Bloodshed Dev-C++ 4.9.9.2
Direct x SDK: Updated Dec 2006
OS: Windows XP Pro
previous errors I had were fixed by installing all of the direct x header files and linker files into the dev-c++ base directories and by linking gdi32, d3d9, and d3dx9. I also threw in #pragma comment(lib, "libd3d9.a") in order to fix several errors as you can see in the code i pasted above.
Thanks again,
~Poent
[Edited by - Poent on February 1, 2007 11:31:34 PM]