• 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
LAURENT*

multiple source files and global varaibles.......................

10 posts in this topic

Hello all

 

Today I've decided it was time to upgrade my skills and use multiple source files instead of coding everything in one big cpp file. I've tried this before but I failed so hard for a week I quit in frustration and went along with my previous  method using only one file. Researching this topic and using 3 main tutorials which includes one on this site I've manage to bring the errors down to a minimum.

 

My problem is with my global variables which I was trained to utilize the most. What I've done is put every global variable in a header file and with the "extern"  key word, then I defined them in a cpp file name global.cpp. I have gotten these specific errors despite including the global header inside the main file.

 

Error C2466

Error C2133

Error C2466

Error C2057

Error C2133

 

I have followed an extremely similar pattern of working with multiple source files. I created a header for every class and a cpp for all the function in the class. I never included a cpp file. I used #ifndef, #define, #endif in every header file. All headers are included within main. I've done just about everything I can think of for the moment.

 

If you haven't noticed I code in c++ programming language, I use visual studious 2010 and that about it. If you have a little bit or a lot of bit of experience dealing with multiple source file I'm preparing a file which you can download and see my source code. I've done everything hy the book so it should be predictable.

 

Download link coming soon.

Download link have come

https://www.dropbox.com/s/7b7yqei55svcyc1/Protype.zip

 

Maybe I'm trying too hard, Maybe I'm not cut out to be a programmer, I always have error and I can never completely figure out stuff on my own. Anyways here the folder if you desire more details on the problem.

Edited by LAURENT*
1

Share this post


Link to post
Share on other sites

Compiler Error C2466: An array is allocated or declared with size zero.

http://msdn.microsoft.com/en-us/library/6zxfydcs.aspx

 

Compiler Error C2133: 'identifier' : unknown size

http://msdn.microsoft.com/en-us/library/c13wk277.aspx

 

Compiler Error C2057: expected constant expression

http://msdn.microsoft.com/en-us/library/eff825eh.aspx

 

Doesn't the compiler tell you which line numbers you have problems with?  Seems like trivial things to fix to me, but I don't have time to look through your source code right now.  The error messages doesn't indicate any problems with include-files being included multiple times or something like that.  Seems like 'syntax errors' to me.

 

Edit: Don't think that you are not fit to be a programmer.  Everyone stumbles on barriers, and you just hit the number one way of learning - by erring!  When you sort this problem, you will come out as a slightly better programmer.  Every time you make an error that you fix you get a little tad better.

 

Edit 2: I will try to have a look tomorrow, if you haven't sorted your problem, or anyone else hasn't helped you. 

Edited by aregee
2

Share this post


Link to post
Share on other sites

I did have a look through your code, and I didn't immediately see anything.  Your code looks good as far as I can see from the quick look I had.  I think line numbers would help a bit more.

 

Just a few nitpicking though.

 

Indentation:

Be consistent.  Consider using 4 spaces rather than 8 for tabs.

 

Don't do this:

 

switch( event.key.keysym.sym ) 
{ 
    case SDLK_UP: yVel -= YS_VEL_WALK; zVel -= YS_VEL_WALK; break; 
    case SDLK_DOWN: yVel += YS_VEL_WALK; zVel += YS_VEL_WALK; break; 
    case SDLK_LEFT: xVel -= YS_VEL_WALK; break; 
    case SDLK_RIGHT: xVel += YS_VEL_WALK; break; 
    case SDLK_SPACE: if(grounded == true){z -= 4; zVel -= YS_VEL_WALK + 20;} break; 
}  

Do this instead:

 

switch( event.key.keysym.sym ) 
{ 
    case SDLK_UP:
    {
        yVel -= YS_VEL_WALK;
        zVel -= YS_VEL_WALK; 
        break; 
    }
    //ETC...
    default:  //Always include the default clause (even if it is not an error not to).
    {
        break;
    }
} 
Don't do this:
 
if( check_collision( camera, floorpad ) == true ) 
apply_surface( wallfront.x - camera.x, wallfront.y - camera.y, tileSheet, screen, &clips[ type ] );
//Hard to immediately tell that this next line is NOT part of the IF, especially when you have not indented the line that do belong.
Do this instead (even if the above is technically correct). (Don't be afraid of curly braces, even if you have only one line inside.)
 
if( check_collision( camera, floorpad ) == true ) 
{
    apply_surface( wallfront.x - camera.x, wallfront.y - camera.y, tileSheet, screen, &clips[ type ] );
}
At least indent the second line so it is clear that it belongs to the IF (and the next line is not).
 
Be neat and consistent about spacing and indentation.  When you see a '{', always indent what is enclosed.  Use the closing '}' on the same line as the opening one.
 
Remember:  The key point is that the code shall be as readable as possible.
 
Other than that, I am quite impressed with what you have achieved, considering the lack of credit you are giving yourself.  You certainly understand quite a few important key points.  I wouldn't give up programming quite yet, if I were you.  At least not if you are thinking it is fun!
 
1

Share this post


Link to post
Share on other sites

Oh found the log in your debug folder.  Seems it is complaining about two lines I thought of as possible candidates, but the consts seemed valid so I rejected that possibility:

 

main.cpp, line 25, 26:

 

Tile *tiles[ TOTAL_TILES ]; 
Tile *walls[ TOTAL_TILES_WALL ]; 
In your global.h file you have:
 
const int TOTAL_TILES = 192; 
const int TOTAL_TILES_WALL = 1; 

Seems ok to me...

 

The question is why the compiler thinks that TOTAL_FILES and TOTAL_FILES_WALL is '0'.

Edited by aregee
0

Share this post


Link to post
Share on other sites

That the same question I asked. Why doesn't the compiler see the value? It should according to the instructions I've read. I really wish I could take the simple route and type the number in but I need to learn somehow.

 

I've been on and off all day figuring this out. The only thing I haven't tried yet is using break point in the debugger. Anyways thanks for the assistance I'm still not giving up I just got a little upset because I didn't get it the second time.

0

Share this post


Link to post
Share on other sites

That the same question I asked. Why doesn't the compiler see the value? It should according to the instructions I've read. I really wish I could take the simple route and type the number in but I need to learn somehow.

 

I've been on and off all day figuring this out. The only thing I haven't tried yet is using break point in the debugger. Anyways thanks for the assistance I'm still not giving up I just got a little upset because I didn't get it the second time.

 

If you really want to do it as per your source code you'll have to include global.cpp in main.cpp.  Personally I think it'd be cleaner for Tile constants to be associated with the Tile class to allow easier access.  It'd be as simple as typing Tile::CONSTANT_NAME which is easier to use when compared to looking through an excessive amount of variables, constants, and whatnot in the global namespace.  This also allows you to break up constants into smaller, bite-sized relevant chunks so you don't have one massive file with tons of constants, which can get messy quick.

0

Share this post


Link to post
Share on other sites

I recognize that you are still learning basic programming, but I still would like to give one design hint: don't use global variables. It will eventually lead to errors that are very difficult to trace and it will also make the code difficult to read. For instance, if I take a look at Function.cpp and its function clip_tiles(). It seems to reference the array clips, but it is not at all clear to the reader where this array is defined. Try to construct your functions so that they only act on the given arguments and have no reference to global things, except for global constants. Global constants are ok.

 

I remember the time when I was a beginner programmer and global variables seemed like an ok way to do things (I had also Basic background). I though it was impossible to write a big real life program without global variables. Boy was I wrong. It's very much possible to construct your game with something like

int main()
{
 CGame game;

 game.initialize();

 while(game.isRunning()){
  game.updateAwsomeness();
 }

 game.close();

 return 0;
}

No globals needed, everything is contained in class CGame. Some might say that this is not the best approach either, but it is certainly an improvement to using a number of global variables.

1

Share this post


Link to post
Share on other sites
[quote]My problem is with my global variables which I was trained to utilize the most.[/quote]I'm curious what you mean by this. I've only heard train and globals used together when saying you should train yourself to not use them.
1

Share this post


Link to post
Share on other sites

 

My problem is with my global variables which I was trained to utilize the most.

I'm curious what you mean by this. I've only heard train and globals used together when saying you should train yourself to not use them.

 

Tutorials I've studied tried to make concepts as simple as possible using global variables to teach. I became used to using them and developed many bad habits.

 

Array sizes need to be known at compile time.

 

The problem here is that the lines here:

Tile *tiles[ TOTAL_TILES ]; 

Tile *walls[ TOTAL_TILES_WALL ]; 

do not know what the values of TOTAL_TILES and TOTAL_TILES_WALL actually are because they are defined in another compilation unit (they are defined in global.cpp but needed in main.cpp)

 

It is good practice to put

extern const int MY_GLOBAL;

in the header and

const int MY_GLOBAL = 10;

in the cpp file. In cases with non-cost objects, this is necessary to prevent linker errors. However, in this case, this leads to the above confusion.

 

You could actually put 

const int TOTAL_TILES = 192;
const int TOTAL_TILES_WALL = 1;

in global.h and remove their definitions entirely from global.cpp and it will compile for you. And it won't produce any linker errors because constants defined like this essentially disappear as their values are copied wherever necessary without them actually taking up a linker symbol.

The code work fine but something doesn't feel right. It feels like I'm breaking rules. I guess I'll just create another header file full of defined constant since I don't need the cpp.

 

 

 

 

That the same question I asked. Why doesn't the compiler see the value? It should according to the instructions I've read. I really wish I could take the simple route and type the number in but I need to learn somehow.

 

I've been on and off all day figuring this out. The only thing I haven't tried yet is using break point in the debugger. Anyways thanks for the assistance I'm still not giving up I just got a little upset because I didn't get it the second time.

 

If you really want to do it as per your source code you'll have to include global.cpp in main.cpp.  Personally I think it'd be cleaner for Tile constants to be associated with the Tile class to allow easier access.  It'd be as simple as typing Tile::CONSTANT_NAME which is easier to use when compared to looking through an excessive amount of variables, constants, and whatnot in the global namespace.  This also allows you to break up constants into smaller, bite-sized relevant chunks so you don't have one massive file with tons of constants, which can get messy quick.

 

I heard including cpp wasn't the best. Anyways you're right I do have a lot of constants. I was planning to shrink my code using something call enum but this option seems efficient too

0

Share this post


Link to post
Share on other sites


Anyways you're right I do have a lot of constants.

 

Yes, and I've tried the whole "keep the constants in one file thing" before a long time ago and I was not a fan of the results.

 


I was planning to shrink my code using something call enum but this option seems efficient too

 

I'd encourage you to try any method that you come up with.  Simply taking someone's word for something is not nearly as effective of a learning experience as trying something yourself and deciding that it isn't efficient.

2

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