Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Josh_M

Pages: [1]
1
General / Re: All sprites in vector are moving in the exact same pattern
« on: February 26, 2020, 09:57:43 am »
It's because you don't understand how the random functions are intended to be used. They're quite over-complicated for simple uses because they're intended to be useful for scientists and statisticians.

The previous poster is also wrong, you shouldn't keep reseeding every call, as if you call more than once in a second you'll keep getting the same result (by design)!

Consider the following code snippet, similar to yours:
// Example program
#include <iostream>
#include <string>
#include <random>

class Test
{
private:
    std::mt19937 mt;
    std::uniform_int_distribution<int> dist;
public:
    Test() : mt(time(0)), dist(0, 4)
    {
    }

    int genRandom()
    {
        return dist(mt);
    }
};

int main()
{  
    Test t1{};
    Test t2{};
   
    for(int i = 0; i < 3; i++)
    {
        std::cout << "iteration #" << i << "\n";
        std::cout << t1.genRandom() << "\n";
        std::cout << t2.genRandom() << "\n";
    }
}

The output is:
iteration #0
3
3
iteration #1
1
1
iteration #2
0
0

This is because we seed two random generators with the same seed at the same time, then call them at the same times! You should only seed a random generator once in your entire application, then call it everywhere, unless you have very exotic needs.

Put it in a static method with a lazy initializer would be my recommendation, like:
class Random
{
public:
    static int getRandomInt(int min, int max)
    {
        // static so it's only created once, then every other call reuses it (forever)
        // if you need multiple types of random functions you'd need to make it a private member
        // and reuse it instead
        static std::mt19937 mt(time(0));
       
        auto dist = std::uniform_int_distribution<int>(min, max);
        return dist(mt);
    }
};

And call it like this (anywhere):
Random::getRandomInt(0, 4)

Good luck.

2
Graphics / Re: Advise on collision detection using std::vector
« on: March 16, 2015, 08:01:07 am »
Linked lists are almost completely redundant with modern processors:

There's a reason c#'s List structure is actually a dynamic array internally  ;)

If your vector is unsorted, then if deletion performance is an issue you can move the last element of the vector into the index that you wish to delete, thus saving shifting every element after it in the array.

3
General / Re: Better way to create particle as Vertex Array?
« on: February 06, 2015, 02:25:59 am »
Try something like this instead for that function Hapax mentioned perhaps.

#include <cmath>

...

a = ceil(a * 100) / 100;

4
Inlining a function just removes the overhead of the function call. Inline functions, like functions, are generally evaluated at run time. The more you can evaluate at compile time the better :).

5
General discussions / Re: Better keyboard handling
« on: November 26, 2013, 11:08:07 pm »
As someone completely uneducated on the topic, what is wrong with using the USB codes on that list from wintertime? I assume it's because some embedded/propriety formats (ie: laptops) or PS/2 connections have different input.

6
General / Re: inconsistent fps
« on: November 26, 2013, 07:29:18 am »
You could try running the code through a profiler for the slow period, then you can see which calls are taking a really long time initially.



7
You can save your current project as a template, which means you just select whatever you saved that template as for future projects. However it will have all your old files in it, so you might want to save a template with no files in it for simplicity. :)

8
General / Re: Error checking a list of values.
« on: October 06, 2013, 03:47:21 am »
Ahh cheers, I'll try implementing something like that and see how it goes.

9
General / Error checking a list of values.
« on: October 05, 2013, 05:22:02 pm »
Hi, I'm just wondering how I'd implement some specific error checking code. If I have for example, some key bindings in a namespace of type sf::KeyboardKey, which I equate to some of SFML's key enumerations, how can I check that I am passing a binding and not SFML's predefined enum to a function? (to avoid redefining SFML's enumerations by accident!)

Some code to demonstrate what I mean clearly...

Say I have a binding namespace like this:
namespace kb
{
        sf::Keyboard::Key moveLeft = sf::Keyboard::A;
        // etc...
}

And call a function like this (yes it's rather trivial, but the point remains):
void KeyBindings::SetKeyBinding(sf::Keyboard::Key &binding, sf::Keyboard::Key newKey)
{
        // check binding is a binding and not a predefined key
        if(binding is a binding)
        {      
                binding = newKey;
        }
}

I considered using an enumerated list for the bindings, but realised they're supposed to be rvalues, when I need lvalues. And the obvious problem with checking if binding == moveLeft (or whatever) is that binding == sf::Keyboard::A too ::).

Cheers.

10
SFML projects / Re: my network game
« on: September 21, 2013, 07:03:45 pm »
Love the sound effects! Starcraft stimpack brought on a wave of nostalgia ^^

11
General / Re: Lethn's Programming Questions Thread
« on: September 12, 2013, 02:28:39 pm »
I don't really see how it can be so boring o_0. I've been through a 1400 page C++ book thoroughly in like 2 weeks before, and that was without access to even a basic compiler, so all my programming practice was on paper with painstaking (and terrible) error checking. It wasn't the most interesting thing in itself, but if you make your own exercises to do that involve what you're currently learning, it helps a bit too. But primarily, I think you just need a real interest in learning the language, nothing else is really going to cut it.

Pages: [1]
anything