Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 15 16 [17] 18 19 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100591 times)

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Programming Help Thread (For Dummies)
« Reply #240 on: August 12, 2011, 04:15:35 pm »

So I'm trying to figure out how to code a singleton class in C++. should I have the constructor, copy constructor, and assignment be private, and have a static variable of the single class, or should I just have all the relevant bits of the class be static?
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #241 on: August 12, 2011, 04:21:23 pm »

http://sourcemaking.com/design_patterns/singleton should have most of the answers you need.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #242 on: August 15, 2011, 07:04:46 am »

So I'm trying to figure out how to code a singleton class in C++. should I have the constructor, copy constructor, and assignment be private, and have a static variable of the single class, or should I just have all the relevant bits of the class be static?

Can I ask why you want to code a singleton? They are generally considered anti-patterns and should really be avoided. There are very few situations where you'd actually use one.
If it's just for learning then Virex's link is good.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #243 on: August 15, 2011, 07:14:59 am »

Singletons are often used in graphics programming, or other instances when interfacing with lower-level (hardware)libraries, in my limited experience.

Otherwise Shades is right, it's just a nicer way of using globals. Which you should avoid where possible.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Programming Help Thread (For Dummies)
« Reply #244 on: August 15, 2011, 11:12:22 am »

Well, the singleton class I'm writing is called DisplayManager, so I think it'll be okay.
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Programming Help Thread (For Dummies)
« Reply #245 on: August 15, 2011, 11:25:55 am »

I believe that standard way to produce a singleton is to have no public constructors and the instance  should be a private static member of the class, and have a public static method that returns it. This makes it harder to loose your primary reference to the only instance.

I also recommend against making everything static because it makes it harder to substitute two singletons that both implement the same abstract/super class for each other.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #246 on: August 16, 2011, 04:44:35 am »

Well, the singleton class I'm writing is called DisplayManager, so I think it'll be okay.

You'll probably be okay, but it sounds like what you want to do is create an instance and pass it around to things that needed it rather than using the global scope.

However the classic singleton structure is something like

Code: [Select]
// header file
class one_of_these
{
public:
    static one_of_these& get_instance()
    {
        if (nullptr == instance)
        {
            instance = new one_of_these();
        }
 
        return *instance;
    }

private:
    static one_of_these* instance;

    // singleton
    one_of_these();
    ~one_of_these();

    // nocopy
    one_of_these(one_of_these const&);
    void operator=(one_of_these const&);
}

// somewhere in the source file
one_of_these::instance = nullptr;

I should note that nullptr is c++0x (or I guess c++11 now) and not supported by some older compilers yet.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

USEC_OFFICER

  • Bay Watcher
  • Pulls the strings and makes them ring.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #247 on: August 18, 2011, 04:59:03 pm »

Question: Has anybody managed to get keyboard input working in libtcod for C++? I can only seem to read the input from the computer, but I can't seem to differentiate the different key-presses from each other. If someone could post a snippet of code that works with keyboard input, that would be nice. (I would post my code, but it's barely 22 lines long and not really worth showing).
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #248 on: August 18, 2011, 05:08:40 pm »

From my world-renowned 7drl Pachter:

Code: [Select]
  TCOD_key_t k=TCODConsole::checkForKeypress(TCOD_KEY_PRESSED|TCOD_KEY_RELEASED);
  update(k)


// here it comes: If a menu returns false, pop it from the stack. Gives me a kind of layered menus like DF:

bool GameScreen::update(float elapsed, TCOD_key_t k, TCOD_mouse_t mouse){
  //menustuff here
  if(!menus.top()->keypressed(k)){
    delete menus.top();
    menus.pop();
  }
  return true;
}


//And a menu example:

bool Menu_Main::keypressed(TCOD_key_t k){
  if(!k.pressed){   // this makes sure it fires "on release"
    if(k.vk == TCODK_SPACE){
      Stats::engine->togglePause();
    } else if(k.c == 'b'){
      Stats::engine->screen->addMenu(new Menu_Build);
    } else if(k.c == 'g'){
      Stats::engine->screen->addMenu(new Menu_Gather);
    } else if(k.c == 'w'){
      Stats::engine->screen->addMenu(new Menu_Work);
    } else if(k.c == 'j'){
      Stats::engine->screen->addMenu(new Menu_Jobs);
    } else if(k.c == 'p'){
      Stats::engine->screen->addMenu(new Menu_Gather);
    } else if(k.c == 's'){
      Stats::engine->screen->addMenu(new Menu_Gather);
    }
  }

  return true;
}

Oh and it's not a switch because I check for more than just the keypress in the same "if" in some other menus.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

USEC_OFFICER

  • Bay Watcher
  • Pulls the strings and makes them ring.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #249 on: August 18, 2011, 05:21:38 pm »

Thanks, but what does update (k) do?
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #250 on: August 18, 2011, 07:11:28 pm »

Siquo's update(k) is basically calling keypressed(k). Don't worry about anything else inbetween, it's not related.

See the docs http://doryen.eptalys.net/data/libtcod/doc/1.5.1/html2/console_input.html?c=false&cpp=true&cs=false&py=false&lua=false. You use TCOD_key_t.vk (which is a keycode, i.e. for arrow buttons) or TCOD_key_t.c (which is a character, i.e. for letters and numbers) to check what key was pressed.

By the way, libtcod's keyboard input is subtly broken since it doesn't poll for input multiple times per frame. So if you press (or release, or release and press) two keys at the same time in a single frame, only the first key event will go through. I would personally suggest just using the underlying SDL for keyboard input. It's basically the same format, except it's not broken. And a lot better documented.
« Last Edit: August 18, 2011, 07:13:06 pm by Normandy »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #251 on: August 19, 2011, 03:06:30 am »

Thanks, but what does update (k) do?
Call my own function called "update" that comes immediately after it in the example code :)

By the way, libtcod's keyboard input is subtly broken since it doesn't poll for input multiple times per frame.
Ahaa, so that is where it went wrong. I already found it sluggish. That's a good tip, thanks!
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

USEC_OFFICER

  • Bay Watcher
  • Pulls the strings and makes them ring.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #252 on: August 19, 2011, 01:46:53 pm »

By the way, libtcod's keyboard input is subtly broken since it doesn't poll for input multiple times per frame. So if you press (or release, or release and press) two keys at the same time in a single frame, only the first key event will go through. I would personally suggest just using the underlying SDL for keyboard input. It's basically the same format, except it's not broken. And a lot better documented.

Seeing as I can't get libtcod's keyboard input to work anyways, I'll be switching to SDL then.

Thanks, but what does update (k) do?
Call my own function called "update" that comes immediately after it in the example code :)

*Facepalm*

Anyways, thanks for the help everybody and expect me to ask some more help in the future (perhaps).
Logged

quinnr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #253 on: August 23, 2011, 11:57:23 am »

It's been a long time since I programmed anything, and I'll I've done before was PHP..
I recently started learning Ruby. (on Rails, eventually. But for now I need to learn the Ruby basics..)

How would I go about storing data in a grid? I want to have an x,y co-ordinate plane, and each location on the x,y should have the ability to store several different variables.

I have no clue how to even start going around to doing that, though.
Logged
To exist or not exist, that is the query. For whether it is more optimal of the CPU to endure the viruses and spam of outragous fortune, or to something something something.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #254 on: August 23, 2011, 12:08:01 pm »

I don't know too much about ruby, but typically you'd use a 2-dimensional array for that, which stores either structures, classes (if you know what data each cell can hold beforehand.) or hash maps (if you don't).


If you're using a really large, but sparsely populated grid (for example, the entire milky way. Which would incidentally require a few terrabytes in the best case scenario if you're just storing the xy coordinates of the stars), it may however be easier to use a quad tree, some sort of R-tree or a Kd-tree as it will consume significantly less space and things like pathfinding and finding the nearest neighbor are O(log(n)) for tree-based storage methods as opposed to O(n) for a grid (if that doesn't mean anything to you, it tells you that for absurdly large numbers, the tree based methods are guaranteed to be faster then the grid based method)
« Last Edit: August 23, 2011, 12:21:54 pm by Virex »
Logged
Pages: 1 ... 15 16 [17] 18 19 ... 91