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

Static as substitute of private members.

19 posts in this topic

The [b]C[/b] way to deal with variables that are not supposed to be used by client programmers is by using the static keyword in global space. In this way the variables are not available to external files, so that, even if an implementation file imports the header that contains such private variables, they are unavailable (name-definition-unaware, or more technically: out of scope).

 

If I can recall correctly, [i]extern[/i] variables are declared in the header and defined in the implementation file.

 

What disadvantages are there from e.g. C++'s private class members?

0

Share this post


Link to post
Share on other sites

Static members are global, global is bad.

But the main problem is that static members are not instance members.  You decide to use static when there will only be one member for all instances of that class globally, not based on visible scope.

 

 

Static is not a replacement for private in C.  Declaring and defining them only in the .C file is.

 

 

L. Spiro

Why static is bad ? In an engine class it's good to have all init using Init() function.

You use this way on your engine : http://lspiroengine.com/docs/classlse_1_1_c_engine.html

On a renderer it's nice to have RenderState class with static variables + Init() to avoid Renderer.cpp of 4000 lines.

Do you think struct + extern is better to use than class/struct with static ?

I really want your opinion about all that L. Spiro smile.png.

Edited by Alundra
0

Share this post


Link to post
Share on other sites

Why static is bad ? In an engine class it's good to have all init using Init() function.


It's non-ideal, not bad. There are a lot of problems with it, however. It makes threading a pain in the butt. It makes writing tests against mock objects a pain in the butt. It makes some kinds of debugging a pain in the butt.

On a renderer it's nice to have RenderState class with static variables + Init() to avoid Renderer.cpp of 4000 lines.


That doesn't follow. Nothing about static variables makes the rest of your code smaller. You've got a false equivalency going on here.

Do you think struct + extern is better to use than class/struct with static ?


I don't understand what these have to do with each other.

Make functions or classes that take as parameters any dependencies they have and don't rely on statics. This is called Dependency Injection, and is something to strive for. That way, for instance, you can pass in a NullaryRenderDevice or something so you can test your renderer in a unit test on your build machine without needing an actual GPU, or a MockFileSystem that serves up only some cooked-in test files for tests. It lets you switch out things at runtime easily.

There are many places for static values. I use them extensively in reflection code, error dialog and assert code, memory space support, and so on. They're all hidden behind very special-case features and not part of the main game code or even the main game systems.

Everything else are systems/manager encapsulated in classes that require references to interfaces for other systems. e.g the GraphicsSystem requires a reference to IResourceSystem so that it can load textures and such on demand. The AudioSpace attached to any Space on the GameObjectSystem has a reference to an IAudioSystem so that sound sources in game can play sound. etc. No statics, no globals, no singletons, no service locator, none of that cruft. I can initialize the engine, systems, or spaces in any configuration I need, including initializing them completely differently in the release mode game, debug mode game, editor, asset baker, multiplayer server, etc.
1

Share this post


Link to post
Share on other sites


On a renderer it's nice to have RenderState class with static variables + Init() to avoid Renderer.cpp of 4000 lines.

 

Huh? How does a static Init avoid 4000 lines?

 

There's really no good reason to have your renderer or engine be static/global.

1

Share this post


Link to post
Share on other sites

A graphics module doesn’t need to be global, but I also strongly dislike having to pass an instance down to every graphics resource such as a vertex buffer.


I solve this by making my graphics resources opaque objects. Only things that manipulate resources need the graphics system, which turns out to be relatively rare (it's mostly the code that generates the draw commands). It's fairly D3D11-ish. As a further bonus, all my graphics resource handles are C++ wrappers around ints, making it much easier to sort by material/buffer for rendering (especially as I guarantee that the unique bits of those integers are the lower X bits, meaning you can radix sort on only X bits instead of on most of the bits of a pointer).
1

Share this post


Link to post
Share on other sites

As a side note, the actual use of C-style static in global space has really been replaced by anonymous namespaces now and static in global space in C++ is really only for backwards compatibility with legacy C code as far as I'm aware. I prefer anonymous namespaces because its too easy to forget to add static to the front of a new definition but easy to remember to include it in the namespace.

 

I don't use much, if any, static global state but I tend to put implementation functions in anonymous namespaces to avoid accidental linking and name clashes.

Edited by Aardvajk
1

Share this post


Link to post
Share on other sites


Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

 

Don't they need to be declared static inside the .C file so that they only visible from within that compilation unit.

2

Share this post


Link to post
Share on other sites

 


Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

 

Don't they need to be declared static inside the .C file so that they only visible from within that compilation unit.

 

 

No, each compilation unit is compiled individually, a variable has to be declared as extern to become accessible across compilation units.

 

Edit: Sorry, obviously incorrect.

Edited by SimonForsman
-2

Share this post


Link to post
Share on other sites
Alundra, on 11 Apr 2014 - 03:04 AM, said:
On a renderer it's nice to have RenderState class with static variables + Init() to avoid Renderer.cpp of 4000 lines.

That doesn't follow. Nothing about static variables makes the rest of your code smaller. You've got a false equivalency going on here.

I was not clean on this view, but L. Spiro said the reason after.

It's to have a class who store all render state object needed for the render static to avoid to have monster function call with 10 parameters.

And one Init() in RenderState class to avoid Renderer Init() function to be large code.

But I have to say that I don't allow external render state, for example I have that in Material :

enum TBlendMode
{
  BM_OPAQUE   = 0, //!< Opaque blend mode.
  BM_ADDITIVE = 1, //!< Additive blend mode.
  BM_ALPHA    = 2  //!< Alpha blend mode.
};
RenderState has (and init) :
ID3D11BlendState* m_OpaqueBlendState; //!< Opaque blend state.
ID3D11BlendState* m_AlphaBlendState; //!< Alpha blend state.
ID3D11BlendState* m_AdditiveBlendState; //!< Additive blend state.
ID3D11BlendState* m_AdditiveSrcAlphaBlendState; //!< AdditiveSrcAlpha blend state.

But it's not just that, it has too :

ID3D11DepthStencilState* m_DisabledDepthStencilState; //!< Disabled depth stencil state.
ID3D11DepthStencilState* m_LessEqualDepthStencilState; //!< Less-Equal depth stencil state.
ID3D11RasterizerState* m_NoneCullRasterizerState; //!< NoneCull rasterizer state.
ID3D11RasterizerState* m_ClockWiseCullRasterizerState; //!< ClockWiseCull rasterizer state.
ID3D11RasterizerState* m_CounterClockWiseCullRasterizerState; //!< CounterClockWiseCull rasterizer state.
ID3D11SamplerState* m_ClampLinearSamplerState; //!< ClampLinear sampler state.
ID3D11SamplerState* m_RepeatLinearSamplerState; //!< RepeatLinear sampler state.
ID3D11SamplerState* m_ClampPointSamplerState; //!< ClampPoint sampler state.
ID3D11SamplerState* m_RepeatPointSamplerState; //!< RepeatPoint sampler state.

I have a list of Vertex/Pixel/Hull/Geometry/Compute shader as well but inside renderer actually, but I would change to avoid monster params function call.

All that combined to give that : http://uppix.com/f-GameD3D11_135347f06e001605d7.png

The probleme is to have renderer cleaned about clustered renderer/shadow mapping.

Edited by Alundra
0

Share this post


Link to post
Share on other sites

 

 


Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

 

Don't they need to be declared static inside the .C file so that they only visible from within that compilation unit.

 

 

No, each compilation unit is compiled individually, a variable has to be declared as extern to become accessible across compilation units.

 

 

To expand on what people are saying with a few examples:

 

 

 

CASE 1

 

test.h

extern int x;
void foo(void);

test.c

int x;
void foo(void) { x = 1; }

main.c

#include "test.h"
int main(void)
{
    foo(); /* ok */
    x = 6; /* ok */
    return 0;
}

x and foo() are both visible in external modules.

 

 

CASE 2

 

test.h

/* extern int x removed */
void foo(void);

test.c

int x;
void foo(void) { x = 1; }

main.c

#include "test.h"
int main(void)
{
    foo(); /* ok */
    x = 6; /* ERROR, x undeclared */
    return 0;
}

x wasn't declared as extern in the header file and is therefore invisible to external modules.

 

 

CASE 3

 

test.h

extern int x;
static void foo(void);

test.c

int x;
static void foo(void) { x = 1; }

main.c

#include "test.h"
int main(void)
{
    foo(); /* ERROR: foo() undeclared */
    x = 6; /* ok */
    return 0;
}

foo() was declared static and therefore is invisible to external modules.

Edited by TheComet
0

Share this post


Link to post
Share on other sites




CASE 1



test.h

extern int x;
void foo(void);

test.c

int x;
void foo(void) { x = 1; }

main.c

#include "test.h"
int main(void)
{
foo(); /* ok */
x = 6; /* ok */
return 0;
}

x and foo() are both visible in external modules.

 

But in this case if you were to declare x as static within test.c you could could redclare it in main and it would be a different variable.

0

Share this post


Link to post
Share on other sites

Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

 
Don't they need to be declared static inside the .C file so that they only visible from within that compilation unit.

 
No, each compilation unit is compiled individually, a variable has to be declared as extern to become accessible across compilation units.

If you declare and define the variable in global scope in the .C file, it's implicitly static anyway.


What do you mean "implicitly static"? If you have two compilation units that define variables with the same name in the global scope and none of them are static, you will get a "multiple definition" error when linking. There is no such thing as "implicitly static".
2

Share this post


Link to post
Share on other sites

If you DON'T declare it static AND you DON'T declare it extern either BUT you have two variables in two C files both called x AND both files are in the same linker unit you WILL get a linker error because the linker WILL see them as the same object in global scope even though you haven't declared either of them extern.  If you declare both variables as static then you will not get a linker error.

 

 

Alvaro got there before me.

Edited by Buster2000
1

Share this post


Link to post
Share on other sites


What do you mean "implicitly static"? If you have two compilation units that define variables with the same name in the global scope and none of them are static, you will get a "multiple definition" error when linking. There is no such thing as "implicitly static".

I was mistaken, sorry.

1

Share this post


Link to post
Share on other sites

Static is not a replacement for private in C.  Declaring and defining them only in the .C file is.

 

 

L. Spiro

 

Actually, technically one could import ([i]include[/i]) the .c file and use it in another .c file through the [i]extern[/i] keyword(which declares, does not define. Hence why header files contain many [i]extern[/i] keywords for variables but not for functions, since for global functions it is optional.)

0

Share this post


Link to post
Share on other sites

As a side note, the actual use of C-style static in global space has really been replaced by anonymous namespaces now and static in global space in C++ is really only for backwards compatibility with legacy C code as far as I'm aware. I prefer anonymous namespaces because its too easy to forget to add static to the front of a new definition but easy to remember to include it in the namespace.
 
I don't use much, if any, static global state but I tend to put implementation functions in anonymous namespaces to avoid accidental linking and name clashes.


Using static this way used to be deprecated, but is not deprecated in C++ 11 anymore.

Guess they realized it's kind of a silly thing to deprecate.
0

Share this post


Link to post
Share on other sites
Static and anonymous namespaces can still lead to problems if you're definining globals or helper functions with identical names. For instance, if you ever try a "unity build" (nothing to do with the game engine).

Name things uniquely. If you need some local helper functions, consider giving them a namespae wrapper, e.g.

// foo.cpp
namespace /*anonymous*/ {
  namespace FooHelpers {
    void do_thing() { /*...*/ {
  }
}

void PublicClass::Method() {
  using namespace FooHelpers;
  // ...
  do_thing();
}

void PublicClass::OtherMethod() {
  using namespace FooHelpers;
  do_thing();
  // ...
}
using statements in global contexts is also bad in a unity build scenario (even more so than they are in general) so only use them inside function bodies.
1

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