• 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
Liuqahs15

A question on style regarding writing a small library (C++)

7 posts in this topic

Hi

 

If I am to use your library I'd much rather the classes to be in separate .h/.cpp files.. it makes it much more readable...

 

After that if you want u can always add one single .h file that includes all classes at once but still keeping the actual class code in separate files.

1

Share this post


Link to post
Share on other sites

If I am to use your library I'd much rather the classes to be in separate .h/.cpp files.. it makes it much more readable...
 
After that if you want u can always add one single .h file that includes all classes at once but still keeping the actual class code in separate files.

QFE.

 

One header + cpp file per class (though small utility classes can be grouped together if related), and one <library-name>.h header file that includes everything I need to use the library.

0

Share this post


Link to post
Share on other sites

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

 

My "preference" is definitely not to have all encompassing headers.  I work on a conceptual basis for my code.  An example from my math library is that yes it has a "Math.hpp" file you can include and it takes care of a lot of things.  It defines any primitive types used, includes other concepts if required etc.  So, for instance, using my math library I can do the following:

 

some.hpp

 

#include <Math/Math.hpp>
 
class Blah
{
public:
  Blah( Math::Vector3fv& position );
};

 

That compiles no problem.  What won't compile is:

 

 

#include <Math/Math.hpp>
 
class Blah
{
public:
  Blah( Math::Vector3fv& position );
 
private:
  Math::Vector3fv   mPosition;
};

 

The general header only includes the forward reference, not the class.  As such you have to explicitly add an include, but hopefully I make it very easy:

 

 

#include <Math/Math.hpp>
#include <Math/Vector3fv.hpp>
 
class Blah
{
public:
  Blah( Math::Vector3fv& position );
 
private:
  Math::Vector3fv   mPosition;
};

 

That fixes it for this file.  I'm very explicit in making this easy to figure out since namespace == the directory name, class == file name and as such just replace :: with slash and append hpp and you are done.

 

In the classes themselves, if Vector3fv absolutely must include Matrix or something else to function, it includes it.  I don't leave it up to the user of the code to go figure out implied dependencies...

 

 

This is of course one way to do things, I like it and have seen it in other code bases.  Is it for you, that's your choice. smile.png

 

<Sorry if you saw this cut in half, GD didn't seem to save the whole thing the first time.>

Edited by AllEightUp
0

Share this post


Link to post
Share on other sites

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

To be absolutely and 100% clear, because I've discovered lately that a lot of people missed this class:

 

The single, global header is only to be used by clients of your library.

 

If you globally include <library-name>.h within your library, I will hunt you down, and lecture you on compile times tongue.png

0

Share this post


Link to post
Share on other sites

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

To be absolutely and 100% clear, because I've discovered lately that a lot of people missed this class:

 

The single, global header is only to be used by clients of your library.

 

If you globally include <library-name>.h within your library, I will hunt you down, and lecture you on compile times tongue.png

 

Err, oops.  <Extracts foot from mouth!>  smile.png

 

Though to be honest, I still tend to leave the separated headers in the libraries as personal preference.  I still think it a bit too likely that someone will link another big file to that one and end up with the chain of massive includes.  I guess if you are willing to stand on folks heads and ask: "You will NEVER ever include big includes from other big includes again, will you????", then it works out. smile.png

Edited by AllEightUp
0

Share this post


Link to post
Share on other sites

When I break things into .h/.cpp files, I like to break them up based on their "idea".  if it were java, I would make 1 .java file per class, no matter how big or small.  But with C++ if i have a class that relies on some small struct, I will normally include the struct in the same .h file and inline any methods associated with that struct.  This is simply because in C++, unlike java, an additional include file really DOES matter a lot for compile time, and also, we get the added benefit of the methods being inlined.  However, as soon as I see myself forming a new idea with either new classes, or structs that go beyond that "POD" state, I do branch out into a new .h/.cpp combo.

 

As for the library itself...it depends what you're making.  If you're making a library that is meant to be included as a binary, I think having a "Library.h" file that includes your library itself or many basic types, that makes sense.   The only downside is that if the user upgrades the library, their project will need to recompile all sources that include anything from your library, but let's be frank: if they upgrade a library and don't expect that, what planet are they living on?

Edited by metsfan
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