Sign in to follow this  
Progames25

Two Source Files work together

Recommended Posts

Hello, I don't know why but I’ve always had trouble with doing this: Having Two Source files work together by sharing their functions and all that. I read this article: http://www.gamedev.net/reference/programming/features/orgfiles/default.asp And was able to get a lot of information but I don’t think it answered my question. So basically I am trying to make my Win32.cpp file (contains all that windows code to make a window) and my Direct3D.cpp file (Contains code/functions to init Direct3D and get everything set up) work together. Basically Win32.cpp and Direct3D both need the HWND hWnd handle, so I placed that in its own header file called Window, Window will contain the HWND handle and any other window information such as size that both D3D and Windows need. So that part is solved, Win32 and Direct3D both include "Window.h", but there is still a problem, and here is where I’m stuck. Direct3D contains multiple functions that Win32.cpp calls to make Direct3D work the way it should, these are: InitD3d(), DestroyD3D(), and Render() These are called once (Destroy is called twice) in Win32.cpp, but Wind32.cpp cant find them: Error C3861: Identifier not found. How do I fix this? Here are the includes for both source files: Win32.cpp Includes: #include "Main.h" #include "Window.h" --------------------- Direct3D Includes: #include "Direct3D.h" #include "Window.h" Main.h just contains your basic function implementations that you need such as WindowProc and a couple other things. Direct3D.h just contains the function implementations for the InitD3D, Render, etc... and the D3D Device and all. Thanks, Progames25

Share this post


Link to post
Share on other sites
Quote:
Original post by Progames25
Main.h just contains your basic function implementations that you need such as WindowProc and a couple other things. Direct3D.h just contains the function implementations for the InitD3D, Render, etc... and the D3D Device and all.


Header files should only contain function interfaces, not function implementations.

Only place function prototypes in headers (these are those lines which describe the function signature, optionally prefixed by extern). Place function definitions/implementations in source files unless they are prefixed with static, inline, or are template functions. .

In regards to data, they can only be in header files if they are marked const, static or extern. Macros can also be placed safely in a header file.

Share this post


Link to post
Share on other sites
Aren’t functions extern by default?
This is what my functions look like the the head file of Direct3D:
IDirect3D9* pD3D9;
or
void DestroyD3D(void);

I thought those were prototypes?

Basically, I need Win32.cpp to be able to use the three functions I have in the Direct3D.h and Direct3D.cpp file, Direct3D contains the information that the functions should do:
Example:
Direct3D.cpp -
void DestroyD3D()
{
//other code
pD3D9->Release()
//other code
}

Thanks,
Progames25

Share this post


Link to post
Share on other sites
Quote:
Original post by Progames25
Aren’t functions extern by default?
This is what my functions look like the the head file of Direct3D:
IDirect3D9* pD3D9;
or
void DestroyD3D(void);

I thought those were prototypes?



Thanks,
Progames25


Correct, as I said earlier the extern keyword is optional before function prototypes.

However earlier you said you placed function "implementations" in your header files which confused me. A function implementation refers to the body of a function.

If you want to be able to use a function defined in a different file, simply put its prototype inside a header, then include that header in the source files where you want to use that function.

Also, it would help if you posted your complete code.

Share this post


Link to post
Share on other sites
Alright,
So, I also edited my reply post.
So here is an example of my Direct3D.h file:
 
#ifndef INC_DIRECT3D_H
#define INC_DIRECT3D_H
#include <d3d9.h>
#include <d3dx9.h>

// globals
IDirect3D9* pD3D9;
IDirect3DDevice9* pD3DDevice9;

// functions
HRESULT InitD3D(HWND hwnd, int width, int height, bool fullscreen);
void Render(void);
void DestroyD3D(void);
void SetPresentParams(void);
#endif




Here is Direct3D.cpp
 
#include "Direct3D.h"
#include "Window.h"

IDirect3D9* pD3D9;
IDirect3DDevice9* pD3DDevice9;

HRESULT InitD3D(HWND hwnd, int width, int height, bool fullscreen)
{
pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D9 == NULL)
{
return E_FAIL;
}
//get the display mode
D3DDISPLAYMODE d3ddm;
pD3D9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);

//set the presentation parameters
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
//Set Present Params
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.Windowed = !fullscreen;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

if (FAILED(pD3D9->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &pD3DDevice9)))
{
return E_FAIL;
}
return S_OK;
}

void Render(void)
{
pD3DDevice9->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
//begin the rendering
pD3DDevice9->BeginScene();
//all rendering code here
//end scene
pD3DDevice9->EndScene();
//present the back buffer to the screen
pD3DDevice9->Present(NULL,NULL,NULL,NULL);
}
void DestroyD3D(void)
{
if (pD3DDevice9)
{
pD3DDevice9->Release();
pD3DDevice9 = NULL;
}
if (pD3D9)
{
pD3D9->Release();
pD3D9 = NULL;
}
}


Here is Main.h:

#ifndef INC_DIRECT3D_H
#define INC_DIRECT3D_H
//Main Header file
//Contains includes for other headers and important global data
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
#pragma region define
#define MAX_LOAD_STRING 50
#define SizeX 640
#define SizeY 480
#pragma endregion
int CreatGWindow(HINSTANCE hInstance);
bool Windowed = true; //false is full screen with no borders
TCHAR szTitle[MAX_LOAD_STRING] = TEXT("Game"); // The title bar text
TCHAR szWindowClass[MAX_LOAD_STRING] = TEXT("WindowClass"); // the main window class name
#endif


I prob. should make some of the things in there extern hu?
WinMain.cpp

// WindowTest.cpp : Defines the entry point for the application.
#include "Main.h"
#include "Window.h"
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc; // window class
//HWND hWND = NULL;

// fill out the window class structure
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szWindowClass;
wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

// register the windows class
if (!RegisterClassEx(&wc))
return 0;


hWnd = CreateWindowEx(NULL, szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 400, 400,
NULL, NULL, hInstance, NULL);

// check if window creation failed (hwnd would equal NULL)
if (!hWnd)
return 0;

if (FAILED(InitD3D(hWnd, 640, 480, false)))
{
DestroyD3D();
return 0;
}

MSG msg;

while (1)
{
PeekMessage(&msg, hWnd, NULL, NULL, PM_REMOVE);

if (msg.message == WM_QUIT)
break;
else
{
Render();

TranslateMessage(&msg); DispatchMessage(&msg);
}
}

DestroyD3D();
UnregisterClass(szWindowClass, hInstance);
return (msg.wParam);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
return 0;
break;

case WM_CLOSE: // windows is closing
PostQuitMessage(0);
return 0;
break;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;

default:
break;
}

return (DefWindowProc(hWnd, message, wParam, lParam));
}


and lastly the Window.h

#ifndef INC_WINDOW_H
#define INC_WINDOW_H
// the handle for the window, filled by a function
HWND hWnd;
//To Add:
//varibales to hold window size information
#endif


Share this post


Link to post
Share on other sites
I see one problem right away (which I don't think is your main problem):

Write the following in your header:


// globals
extern IDirect3D9* pD3D9;
extern IDirect3DDevice9* pD3DDevice9;


Then inside exactly one source file write:


// globals
IDirect3D9* pD3D9;
IDirect3DDevice9* pD3DDevice9;


It would help if you edited your above post with the rest of your source files so we can identify your main problem.

Share this post


Link to post
Share on other sites
I edit my last post with all my source files.

Bregma :
I did that but I still got the same four errors (mentioned in my first post).
And I was not sure if I should do that becuase in the artcle posted in my first post it said to try and avoid including headers that include other headers...

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