#Gamedev I feel like this is bad coding practice

13 messages · Page 1 of 1 (latest)

mossy sluice
#
void LoadoutManager::setUp(SDL_Renderer* renderer, const int WINDOW_WIDTH) {
    int loadoutUIWidth = 220;
    int loadoutPaddingY = 50;
    
    int stripSize = (WINDOW_WIDTH - (loadoutSize * loadoutUIWidth) / (loadoutSize + 1));

    for (int i = 0; i < loadoutSize; ++i) {
           //loadouts[i]
           // render stuff
    }
}

I feel like the passing in of the window width and the renderer is a bad practice.

The purpose of this method is to create the canvas/texture of a certain aspect of the GUI (loadouts)

what's the best way to go about this and what do you guys think of the current setup

#

also follow up question: are singleton globals okay if the global is constant?

wispy bone
#

Not sure what you mean by "singleton global".

tacit timber
#

Singletons are an OO thing. In C you just have a normal global variable. Whether global state is "OK" is subjective. The global state being const helps to avoid lots of issues involving race conditions and concurrency. However, global state always makes it harder to tell what a function does at a glance and makes it harder to test functions. You can also run into issues later when you realize you need a second renderer or whatever.

My personal take is to avoid global state where you can. The downside of this is needing to pass in some extra variables here and there (minor annoyance) vs the potential for a massive headache layer when you have to restructure the whole program because of issues with global state (been there)

tacit timber
#

Oh yeah mb. Everything j said still applies though since a singleton is basically just a global variable

mossy sluice
#

okay thanks

#

also what do you guys think about the question prior

wraith finch
#

singletons are considered anti-patterns

#

one way to do this is to have some kind of a manager class that has a static getter that returns a static instance of the class

#

like

class Foo{
public:
  static Foo& get(){
    static Foo instance;
    return instance;
  }
private:
  Foo();
  Foo(const Foo&) = delete; //delete copy ctor so it cant be copied
 }
#

thats how i solved the problem of wrapping a media library into classes that stores bunch of state(events, flags, etc)

#

then you can just do

Foo& fooInstance = Foo::get();