Skip to main content
GameDev.net gamedev.net
🔒 Locked

Organizing files

Started by ph33r Sep 29, 2002 at 3:06 PM 7 replies 750+ views
Original Post
ph33r
ph33r
Ok I'm trying to organize my project. Because these directx programs tend to get over 1000 lines really quickly, even if your doing something simple. This is how I arranged my project I have the C++ files that actually have all the code in them //This file has both the WinMain function and the WinProc WinStuff.cpp GameInitShut.cpp //direct x initilization and shutdown GameLoop.cpp //has all the render stuff in it misc.cpp //misc functions like debug each of those cpp files has a matching header file that declares the function WinStuff.h GameInitShut.h GameLoop.h misc.h Also I have an init.h header file that I include on the top of each .cpp file init.h looks like this
    //windows includes

#define WIN32_LEAN_AND_MEAN

#include <windows.h>

//all the direct x includes

#include <d3d8.h>
#include <d3dx8.h>

#ifndef _GLBLL
#define _GLBLL

IDirect3D8* g_pd3d = 0;
IDirect3DDevice8* g_pd3ddev = 0;
D3DDEVTYPE g_devtype;
HWND g_hWnd;
#endif

#include "GameInitShut.h"
#include "GameLoop.h"
#include "WinStuff.h"
#include "misc.h"  
  
So every .cpp file at the very top has #include "init.h" but for some reason I'm getting a wierd linking error --------------------Configuration: Pong - Win32 Debug-------------------- Compiling... WinStuff.cpp Linking... GameLoop.obj : error LNK2005: "struct IDirect3DDevice8 * g_pd3ddev" (?g_pd3ddev@@3PAUIDirect3DDevice8@@A) already defined in GameInitShut.obj GameLoop.obj : error LNK2005: "struct IDirect3D8 * g_pd3d" (?g_pd3d@@3PAUIDirect3D8@@A) already defined in GameInitShut.obj misc.obj : error LNK2005: "struct IDirect3DDevice8 * g_pd3ddev" (?g_pd3ddev@@3PAUIDirect3DDevice8@@A) already defined in GameInitShut.obj misc.obj : error LNK2005: "struct IDirect3D8 * g_pd3d" (?g_pd3d@@3PAUIDirect3D8@@A) already defined in GameInitShut.obj WinStuff.obj : error LNK2005: "struct IDirect3DDevice8 * g_pd3ddev" (?g_pd3ddev@@3PAUIDirect3DDevice8@@A) already defined in GameInitShut.obj WinStuff.obj : error LNK2005: "struct IDirect3D8 * g_pd3d" (?g_pd3d@@3PAUIDirect3D8@@A) already defined in GameInitShut.obj GameInitShut.obj : error LNK2001: unresolved external symbol "enum _D3DDEVTYPE g_devtype" (?g_devtype@@3W4_D3DDEVTYPE@@A) GameInitShut.obj : error LNK2001: unresolved external symbol "struct HWND__ * g_hWnd" (?g_hWnd@@3PAUHWND__@@A) WinStuff.obj : error LNK2001: unresolved external symbol "struct HWND__ * g_hWnd" (?g_hWnd@@3PAUHWND__@@A) Debug/Pong.exe : fatal error LNK1120: 2 unresolved externals Error executing link.exe. Pong.exe - 10 error(s), 0 warning(s) [/source] What am I doing wrong? I have never really done too much with includes, and preproccessor directives, because I usually squish everything onto one main.cpp file. Also are there better ways I should be organizing my project? [edited by - ph33r on September 29, 2002 4:08:44 PM]
Arild Fines
Arild Fines
quote:
Original post by ph33r
Because these directx programs tend to get over 1000 lines really quickly,

Ugh. I usually feel bad when I hit the 400 mark.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Matt Calabrese
Matt Calabrese
My current project is a little over 23,000.

I usually organize my code into a similar setup with headers USUALLY matched up with a cpp file (except for the base header and any solely for templated classes)

The breakdown usually goes something like this:

Input
GameLoop
Entities
Audio
Graphics
Collision
TemplatedDataContainers
CommonFunctors
Time
FileIO

And from there I set up a second set of headers and source files that use ONLY those (IE no other headers included and no classes/functions, etc. used except for those defined in the SDK) to make a set of higher level interfaces for things like particular entity types, models (or sprites, depending on what you''re doing), etc.

Basically, with each project, before I do any actual coding of the game I make a complete SDK for what I will be using. Since all of the Game-specific code uses interfaces in the standard SDK i make before I begin, it makes it very simple to port (just changing the implementation of the SDK, without ever having to change any of the game-specific code).

The problem you''re having seems to be that you''re not using any inclussion guards. When you include the same header in multiple files of your project, inclussion guards become a neccissity. Encase your headers in:

#ifndef HeaderName_h
#define HeaderName_h

rest of your header goes in here

#endif

That''s probably the most conventional way of doing it.

Happy coding!
ph33r
ph33r
But I use inclusion guards look at this code again, I use the inclusion guard to make sure my globals don't get included twice
but it gives me these errors I posted at the top.


    
//globals


#ifndef _GLBLL
#define _GLBLL

IDirect3D8* g_pd3d = 0;
IDirect3DDevice8* g_pd3ddev = 0;
D3DDEVTYPE g_devtype;
HWND g_hWnd;

#endif



help is appriciated and thanks in advance

[edited by - ph33r on September 30, 2002 2:04:33 PM]

[edited by - ph33r on September 30, 2002 2:05:07 PM]
Dactylos
Dactylos
Your globals should be declared ''extern'' in the header, and then defined in one single source file. What you''re doing now is defining the globals in each source file that includes "init.h", hence the "already defined"-errors.
Arild Fines
Arild Fines
To clarify:I meant 400 lines in a single file.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
ph33r
ph33r
Ok thanks a bunch, anyway I figured out what I was doing wrong thanks to Dactylos, even though he gave a vague answer it was good enough to help me figure it out.

Here is what I did


  //windows includes

#define WIN32_LEAN_AND_MEAN

#include <windows.h>

//all the direct x includes

#include <d3d8.h>
#include <d3dx8.h>

extern IDirect3D8* g_pd3d;
extern IDirect3DDevice8* g_pd3ddev;
extern D3DDEVTYPE g_devtype;
extern HWND g_hWnd;

#include "GameInitShut.h"
#include "GameLoop.h"
#include "WinStuff.h"
#include "misc.h"


notice I extern ''ed the variables with the variable type before it.

And at the top of my GameInitShut file, I had a header file include "globals.h" that looked like this


  #ifndef GLOBALS_H
#define GLOBALS_H

IDirect3D8* g_pd3d = 0;
IDirect3DDevice8* g_pd3ddev = 0;
D3DDEVTYPE g_devtype;
HWND g_hWnd;

#endif


So thank you for helping me, this has been bugging me for a while now, such simple C++ syntax throws me off sometimes.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.