• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
chris2307

DirectX application will not load when split up in to multiple files

9 posts in this topic

Hi all,

I have made DirectX programs before, using multiple header files to include classes and the such but I am now about to embark on something a little larger so I wanted to split the code up into multiple .cpp files as well (easier to maintain and less likely to get confused when making changes).

The problem is, I have never really done this before and I am having trouble with getting it to work. At the moment, I have a direct3D 9 full screen program which is trying to render a simple sprite in the middle of the screen (nothing complicated). The point of this is to get the direct3D initialization in to a separate .cpp file, supported by a header file. As I make the program, I then intend to put code relating to various functions in its own .cpp file, supported by a header file for classes.

I think the concept I struggle with the most is the structure and hierarchy of files and the scope of structs, objects, variables and functions (etc). Here is my code so far:


DirectXInit.h

[code]
#include <d3d9.h>
#include <d3dx9.h>

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

#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768

extern LPDIRECT3D9 d3d; // Pointer to Direct3D interface
extern LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
extern LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface

extern D3DXVECTOR3 centre, blockPosition;
extern LPDIRECT3DTEXTURE9 blockSprite;

void initDirectX(HWND);
[/code]


DirectXInit.cpp

[code]

#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif

void initDirectX(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // Create DirectX9 interface
D3DPRESENT_PARAMETERS d3dParameters; // Pointer to DirectX9 parameters
ZeroMemory(&d3dParameters, sizeof(d3dParameters)); // Clears the structure so we can use it
d3dParameters.Windowed = FALSE; // Fullscreen
d3dParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Get rid of old frames
d3dParameters.hDeviceWindow = hWnd; // Sets the window to be used by Direct3D
d3dParameters.BackBufferFormat = D3DFMT_X8R8G8B8; // Sets the back buffer format to 32-bit
d3dParameters.BackBufferWidth = SCREEN_WIDTH; // Sets the width of the buffer
d3dParameters.BackBufferHeight = SCREEN_HEIGHT; // Sets the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT, // Creates a device class
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dParameters,
&d3dDevice);
D3DXVECTOR3 centre(0.0f, 0.0f, 0.0f);
D3DXCreateTextureFromFile(d3dDevice, "cube.png", &blockSprite);
D3DXVECTOR3 blockPosition(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0);
}

[/code]

Main.cpp

[code]

#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <string>

#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif

LPDIRECT3D9 d3d; // Pointer to Direct3D interface
LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface

D3DXVECTOR3 centre, blockPosition;
LPDIRECT3DTEXTURE9 blockSprite;

void render(); // Render graphics
void cleanUp(); // Cleans everything up and releases memory

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd; // The handle to the window function
WNDCLASSEX window; // Pointer to window struct
ZeroMemory(&window, sizeof(WNDCLASSEX)); // Clears window class so we can use it
window.cbSize = sizeof(WNDCLASSEX); // Size of window
window.style = CS_HREDRAW | CS_VREDRAW; // Redraws the entire window if a movement or size adjustment changes the height of the client area.
window.lpfnWndProc = WindowProc; // Pointer to the window procedure
window.hInstance = hInstance; // Handle to current instance
window.hCursor = LoadCursor(NULL, IDC_ARROW); // We'll stick with the normal cursor here
window.lpszClassName = "Window"; // Gives the class a name
RegisterClassEx(&window); // Registers the window class
hWnd = CreateWindowEx(NULL,
"Window", // Name of the class
"Space Game", // Title of the window
WS_EX_TOPMOST | WS_POPUP, // Fullscreen
0, 0, // Position of window (0,0 for fullscreen)
SCREEN_WIDTH, SCREEN_HEIGHT, // Screen resolution (Uses global declaration)
NULL, // Parent window (None)
NULL, // Menus (None)
hInstance, // Application handle
NULL); // Set to NULL as we aren't using more than 1 window
ShowWindow(hWnd, nCmdShow); // Display window
initDirectX(hWnd); // Initialises the directX graphics
MSG msg = {0}; // msg holds the Windows events message queue

/************************************** Main game loop *************************************************/

while(TRUE) // Main game loop
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // If messages are waiting
{
TranslateMessage(&msg); // Translates the keystroke messages into the correct format
DispatchMessage(&msg); // Sends message to the windowsProc function
if(msg.message == WM_QUIT) // If the message was a quit message
break; // Breaks out of main game loop
}
else
{
render();
}
}

/*******************************************************************************************************/
// We are out of the main game loop (Due to a WM_QUIT signal)

cleanUp(); // Do some housekeeping before quitting
return msg.wParam; // Return the WM_QUIT message to Windows. End of program
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) // Sorts through messages
{
case WM_DESTROY: // When window has been closed
{
PostQuitMessage(0); // Closes the application
return 0;
}
break;
}
return DefWindowProc (hWnd, // Returns any messages the switch statement didn't pick up
message,
wParam,
lParam);
}

void render()
{
d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10, 0, 32), 1.0f, 0); // Clears the screen to a dark blue
d3dDevice->BeginScene(); // Begins Direct3D scene
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND); // Begin sprite drawing

d3dSprite->Draw(blockSprite, NULL, &centre, &blockPosition, D3DCOLOR_XRGB(255,255,255));
d3dSprite->End(); // End drawing
d3dDevice->EndScene(); // Ends the Direct3D scene
d3dDevice->Present(NULL, NULL, NULL, NULL); // Presents the Direct3D scene (Displays to screen)
}

void cleanUp()
{
d3dDevice->Release(); // Closes the Direct3D device and releases memory
d3d->Release(); // Closes Direct3D and releases memory
}
[/code]

When I run the code, it compiles but I get a directX error at the line:

[code]
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND);
[/code]

The error I get is an access violation and the memory address is 0x0. I was under the impression that I could declare d3dSprite in the normal way in the DirectXInit.h file using the keyword extern to show that it is declared elsewhere and then within the Main file I declared it in the normal way. By the way, if I try any other configuration the program fails to compile (i.e. if I use the extern keyword in the Main file instead of the DirectXInit.h file).

Can anyone please help? I find it quite frustrating that I cannot seem to quite understand this simple concept, yet I can do other things which I would say was more complex. [img]http://public.gamedev.net//public/style_emoticons/default/wacko.png[/img]

Thanks

EDIT: Not really too sure why the post has formatted the code in this weird way (Maybe it is just me). Have tried editing the post - Preview displays it correctly but it is formatted wrong when posted properly. Sorry. Edited by chris2307
0

Share this post


Link to post
Share on other sites
[quote name='chris2307' timestamp='1352155001' post='4997766']
DirectXInit.h
[CODE]
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
extern LPDIRECT3D9 d3d; // Pointer to Direct3D interface
extern LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
extern LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface
extern D3DXVECTOR3 centre, blockPosition;
extern LPDIRECT3DTEXTURE9 blockSprite;
void initDirectX(HWND);
[/CODE]

DirectXInit.cpp
[CODE]
#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif
void initDirectX(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // Create DirectX9 interface
D3DPRESENT_PARAMETERS d3dParameters; // Pointer to DirectX9 parameters
ZeroMemory(&amp;d3dParameters, sizeof(d3dParameters)); // Clears the structure so we can use it
d3dParameters.Windowed = FALSE; // Fullscreen
d3dParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Get rid of old frames
d3dParameters.hDeviceWindow = hWnd; // Sets the window to be used by Direct3D
d3dParameters.BackBufferFormat = D3DFMT_X8R8G8B8; // Sets the back buffer format to 32-bit
d3dParameters.BackBufferWidth = SCREEN_WIDTH; // Sets the width of the buffer
d3dParameters.BackBufferHeight = SCREEN_HEIGHT; // Sets the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT, // Creates a device class
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&amp;d3dParameters,
&amp;d3dDevice);
D3DXVECTOR3 centre(0.0f, 0.0f, 0.0f);
D3DXCreateTextureFromFile(d3dDevice, "cube.png", &amp;blockSprite);
D3DXVECTOR3 blockPosition(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0);
}
[/CODE]
Main.cpp
[CODE]
#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <string>
#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif
LPDIRECT3D9 d3d; // Pointer to Direct3D interface
LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface
D3DXVECTOR3 centre, blockPosition;
LPDIRECT3DTEXTURE9 blockSprite;
void render(); // Render graphics
void cleanUp(); // Cleans everything up and releases memory
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd; // The handle to the window function
WNDCLASSEX window; // Pointer to window struct
ZeroMemory(&amp;window, sizeof(WNDCLASSEX)); // Clears window class so we can use it
window.cbSize = sizeof(WNDCLASSEX); // Size of window
window.style = CS_HREDRAW | CS_VREDRAW; // Redraws the entire window if a movement or size adjustment changes the height of the client area.
window.lpfnWndProc = WindowProc; // Pointer to the window procedure
window.hInstance = hInstance; // Handle to current instance
window.hCursor = LoadCursor(NULL, IDC_ARROW); // We'll stick with the normal cursor here
window.lpszClassName = "Window"; // Gives the class a name
RegisterClassEx(&amp;window); // Registers the window class
hWnd = CreateWindowEx(NULL,
"Window", // Name of the class
"Space Game", // Title of the window
WS_EX_TOPMOST | WS_POPUP, // Fullscreen
0, 0, // Position of window (0,0 for fullscreen)
SCREEN_WIDTH, SCREEN_HEIGHT, // Screen resolution (Uses global declaration)
NULL, // Parent window (None)
NULL, // Menus (None)
hInstance, // Application handle
NULL); // Set to NULL as we aren't using more than 1 window
ShowWindow(hWnd, nCmdShow); // Display window
initDirectX(hWnd); // Initialises the directX graphics
MSG msg = {0}; // msg holds the Windows events message queue
/************************************** Main game loop *************************************************/
while(TRUE) // Main game loop
{
if(PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) // If messages are waiting
{
TranslateMessage(&amp;msg); // Translates the keystroke messages into the correct format
DispatchMessage(&amp;msg); // Sends message to the windowsProc function
if(msg.message == WM_QUIT) // If the message was a quit message
break; // Breaks out of main game loop
}
else
{
render();
}
}
/*******************************************************************************************************/
// We are out of the main game loop (Due to a WM_QUIT signal)
cleanUp(); // Do some housekeeping before quitting
return msg.wParam; // Return the WM_QUIT message to Windows. End of program
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) // Sorts through messages
{
case WM_DESTROY: // When window has been closed
{
PostQuitMessage(0); // Closes the application
return 0;
}
break;
}
return DefWindowProc (hWnd, // Returns any messages the switch statement didn't pick up
message,
wParam,
lParam);
}
void render()
{
d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10, 0, 32), 1.0f, 0); // Clears the screen to a dark blue
d3dDevice->BeginScene(); // Begins Direct3D scene
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND); // Begin sprite drawing
d3dSprite->Draw(blockSprite, NULL, &amp;centre, &amp;blockPosition, D3DCOLOR_XRGB(255,255,255));
d3dSprite->End(); // End drawing
d3dDevice->EndScene(); // Ends the Direct3D scene
d3dDevice->Present(NULL, NULL, NULL, NULL); // Presents the Direct3D scene (Displays to screen)
}
void cleanUp()
{
d3dDevice->Release(); // Closes the Direct3D device and releases memory
d3d->Release(); // Closes Direct3D and releases memory
}
[/CODE]
When I run the CODE, it compiles but I get a directX error at the line:
[CODE]
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND);
[/CODE]
[/quote]
Fixed.


L. Spiro Edited by L. Spiro
0

Share this post


Link to post
Share on other sites
I took a quick glance at your code and may have missed it, but it doesn't look like you're creating a DirectX sprite anywhere. Your "d3dsprite" pointer is declared but is never set to anything. So when you try to call Begin() on that pointer (which is NULL in debug mode, and who knows what in release mode), the program crashes. Edited by gfxgangsta
0

Share this post


Link to post
Share on other sites
Awwwww... I think it is time to bang my head on the desk!

That was the problem. I thought I had initialised it but never checked.

Thanks
0

Share this post


Link to post
Share on other sites
Whilst I have the code posted up here and am on the subject,

Can anyone see anything which I wrong with the way in which I have split the files?

I assumed that I would be able to define this in my DirectXInit.h file

[CODE]

#include <d3d9.h>
#include <d3dx9.h>

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

#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
LPDIRECT3D9 d3d; // Pointer to Direct3D interface
LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface

D3DXVECTOR3 centre, blockPosition;
LPDIRECT3DTEXTURE9 blockSprite;

void initDirectX(HWND);
[/CODE]

I also assumed that I would be able to initialise these in my DirectXInit.cpp file

[CODE]
#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif

D3DXCreateSprite(d3dDevice, &d3dSprite);
D3DXVECTOR3 centre(0.0f, 0.0f, 0.0f);
D3DXCreateTextureFromFile(d3dDevice, "cube.png", &blockSprite);
D3DXVECTOR3 blockPosition(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0);
[/CODE]

And I finally, assumed that it would work if I rendered it in the main.cpp file.

[CODE]

#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <string>
#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif

// ...

void render()
{
d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10, 0, 32), 1.0f, 0); // Clears the screen to a dark blue
d3dDevice->BeginScene(); // Begins Direct3D scene
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND); // Begin sprite drawing

d3dSprite->Draw(blockSprite, NULL, &centre, &blockPosition, D3DCOLOR_XRGB(255,255,255));
d3dSprite->End(); // End drawing
d3dDevice->EndScene(); // Ends the Direct3D scene
d3dDevice->Present(NULL, NULL, NULL, NULL); // Presents the Direct3D scene (Displays to screen)
}
[/CODE]

When I run this, the compiler (Visual Studio) kicks up a fuss, saying:


1>Main.obj : error LNK2005: "struct IDirect3DDevice9 * d3dDevice" (?d3dDevice@@3PAUIDirect3DDevice9@@A) already defined in DirectXInit.obj
1>Main.obj : error LNK2005: "struct ID3DXSprite * d3dSprite" (?d3dSprite@@3PAUID3DXSprite@@A) already defined in DirectXInit.obj
1>Main.obj : error LNK2005: "struct IDirect3D9 * d3d" (?d3d@@3PAUIDirect3D9@@A) already defined in DirectXInit.obj
1>Main.obj : error LNK2005: "struct IDirect3DTexture9 * blockSprite" (?blockSprite@@3PAUIDirect3DTexture9@@A) already defined in DirectXInit.obj
1>Main.obj : error LNK2005: "struct D3DXVECTOR3 centre" (?centre@@3UD3DXVECTOR3@@A) already defined in DirectXInit.obj
1>Main.obj : error LNK2005: "struct D3DXVECTOR3 blockPosition" (?blockPosition@@3UD3DXVECTOR3@@A) already defined in DirectXInit.obj

I'm confused now because I only define it once. Edited by chris2307
0

Share this post


Link to post
Share on other sites
Sure. :-)

Main.cpp

[CODE]
#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <string>
#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif
void render(); // Render graphics
void cleanUp(); // Cleans everything up and releases memory
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd; // The handle to the window function
WNDCLASSEX window; // Pointer to window struct
ZeroMemory(&window, sizeof(WNDCLASSEX)); // Clears window class so we can use it
window.cbSize = sizeof(WNDCLASSEX); // Size of window
window.style = CS_HREDRAW | CS_VREDRAW; // Redraws the entire window if a movement or size adjustment changes the height of the client area.
window.lpfnWndProc = WindowProc; // Pointer to the window procedure
window.hInstance = hInstance; // Handle to current instance
window.hCursor = LoadCursor(NULL, IDC_ARROW); // We'll stick with the normal cursor here
window.lpszClassName = "Window"; // Gives the class a name
RegisterClassEx(&window); // Registers the window class
hWnd = CreateWindowEx(NULL,
"Window", // Name of the class
"Space Game", // Title of the window
WS_EX_TOPMOST | WS_POPUP, // Fullscreen
0, 0, // Position of window (0,0 for fullscreen)
SCREEN_WIDTH, SCREEN_HEIGHT, // Screen resolution (Uses global declaration)
NULL, // Parent window (None)
NULL, // Menus (None)
hInstance, // Application handle
NULL); // Set to NULL as we aren't using more than 1 window
ShowWindow(hWnd, nCmdShow); // Display window
initDirectX(hWnd); // Initialises the directX graphics
MSG msg = {0}; // msg holds the Windows events message queue
/************************************** Main game loop *************************************************/
while(TRUE) // Main game loop
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // If messages are waiting
{
TranslateMessage(&msg); // Translates the keystroke messages into the correct format
DispatchMessage(&msg); // Sends message to the windowsProc function
if(msg.message == WM_QUIT) // If the message was a quit message
break; // Breaks out of main game loop
}
else
{
render();
}
}
/*******************************************************************************************************/
// We are out of the main game loop (Due to a WM_QUIT signal)
cleanUp(); // Do some housekeeping before quitting
return msg.wParam; // Return the WM_QUIT message to Windows. End of program
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) // Sorts through messages
{
case WM_DESTROY: // When window has been closed
{
PostQuitMessage(0); // Closes the application
return 0;
}
break;
}
return DefWindowProc (hWnd, // Returns any messages the switch statement didn't pick up
message,
wParam,
lParam);
}
void render()
{
d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10, 0, 32), 1.0f, 0); // Clears the screen to a dark blue
d3dDevice->BeginScene(); // Begins Direct3D scene
d3dSprite->Begin(D3DXSPRITE_ALPHABLEND); // Begin sprite drawing

d3dSprite->Draw(blockSprite, NULL, &centre, &blockPosition, D3DCOLOR_XRGB(255,255,255));
d3dSprite->End(); // End drawing
d3dDevice->EndScene(); // Ends the Direct3D scene
d3dDevice->Present(NULL, NULL, NULL, NULL); // Presents the Direct3D scene (Displays to screen)
}
void cleanUp()
{
d3dDevice->Release(); // Closes the Direct3D device and releases memory
d3d->Release(); // Closes Direct3D and releases memory
}
[/CODE]

DirectXInit.cpp

[CODE]
#ifndef _DirectXInit_H
#include "DirectXInit.h"
#endif
void initDirectX(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // Create DirectX9 interface
D3DPRESENT_PARAMETERS d3dParameters; // Pointer to DirectX9 parameters
ZeroMemory(&d3dParameters, sizeof(d3dParameters)); // Clears the structure so we can use it
d3dParameters.Windowed = FALSE; // Fullscreen
d3dParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; // Get rid of old frames
d3dParameters.hDeviceWindow = hWnd; // Sets the window to be used by Direct3D
d3dParameters.BackBufferFormat = D3DFMT_X8R8G8B8; // Sets the back buffer format to 32-bit
d3dParameters.BackBufferWidth = SCREEN_WIDTH; // Sets the width of the buffer
d3dParameters.BackBufferHeight = SCREEN_HEIGHT; // Sets the height of the buffer
d3d->CreateDevice(D3DADAPTER_DEFAULT, // Creates a device class
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dParameters,
&d3dDevice);
D3DXCreateSprite(d3dDevice, &d3dSprite);
D3DXVECTOR3 centre(0.0f, 0.0f, 0.0f);
D3DXCreateTextureFromFile(d3dDevice, "cube.png", &blockSprite);
D3DXVECTOR3 blockPosition(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0);
}
[/CODE]

DirectXInit.h

[CODE]
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
LPDIRECT3D9 d3d; // Pointer to Direct3D interface
LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface
D3DXVECTOR3 centre, blockPosition;
LPDIRECT3DTEXTURE9 blockSprite;
void initDirectX(HWND);
[/CODE]
0

Share this post


Link to post
Share on other sites
DirectXInit.h declares some global variables. Because you are including DirectXInit.h in two files, both main.cpp and DirectXInit.cpp, that means your globals are being declared twice. You might say "but I'm using '#ifndef _DirectXInit_H'..." . But where are you defining "_DirectXInit_H" ? Because that preprocessor symbol is not defined, the #ifndef check returns true every time so your header file is being parsed twice. What you tried to do is known as "header guards", and usually they are done like this:


DirectXInit.h

[source lang="cpp"]
#ifndef _DIRECTX_INIT_H
#define _DIRECTX_INIT_H

#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
LPDIRECT3D9 d3d; // Pointer to Direct3D interface
LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface
D3DXVECTOR3 centre, blockPosition;
LPDIRECT3DTEXTURE9 blockSprite;
void initDirectX(HWND);

#endif
[/source]

and then, you just do #include "DirectXInit.h" in your other files, without worrying about adding an #ifndef. Edited by gfxgangsta
0

Share this post


Link to post
Share on other sites
Thanks, you are right, I was trying to use header guards, having not used them before.

I made the change you suggested but am still seeing the same problems. Same error message is reported. Seems strange to me.
0

Share this post


Link to post
Share on other sites
My apologies, it's been a while since I've used C/C++. I think DirectXInit.h is still being parsed twice because main.cpp and DirectXInit.cpp end up as two different translation units ([url="http://en.wikipedia.org/wiki/Translation_unit_(programming)"]http://en.wikipedia....it_(programming[/url])). A quick way to get your program to build is to change the top part of your main.cpp to this:

[source lang="cpp"]#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <string>
// COMMENT THIS OUT
//#include "DirectXInit.h"

// ADD THIS
#include <d3d9.h>
#include <d3dx9.h>
extern LPDIRECT3D9 d3d; // Pointer to Direct3D interface
extern LPDIRECT3DDEVICE9 d3dDevice; // Pointer to the device class
extern LPD3DXSPRITE d3dSprite; // Pointer to Direct3D Sprite interface
extern D3DXVECTOR3 centre, blockPosition;
extern LPDIRECT3DTEXTURE9 blockSprite;
extern void initDirectX(HWND);

#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768

// ... rest of main.cpp:
//void render(); // render graphics
//.
//.
//.[/source]

But ideally, you'd want to stop using or at least reduce your usage of global variables. You could go OOP and put the DX9 vars in a class, or you could make them local to WinMain() and pass them as arguments to initDirectX() and to render(). Edited by gfxgangsta
0

Share this post


Link to post
Share on other sites

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  
Followers 0