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

Author Topic: New to SFML, creating my own game and guidelines needed!  (Read 14900 times)

0 Members and 2 Guests are viewing this topic.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #15 on: December 13, 2015, 11:52:31 am »
That would create/destroy an std::mt19937 every time that function is called though. Which isn't a very good idea, it's not small by any means. And recreating it generally loses the whole point of randomness.

An explanation: http://www.elbeno.com/blog/?p=1325

thanks for the link.
the correct implementation for random shuffle is like this:

void utilities::shuffleVector(std::vector<int> &shuffle) {
        int i = 0;
        std::transform(shuffle.begin(), shuffle.end(), shuffle.begin(), [&i](int j) { return j = i++; });
        thread_local static std::random_device rd{};
        thread_local static std::mt19937 random{ rd() };
        std::shuffle(shuffle.begin(), shuffle.end(), random);
}

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: New to SFML, creating my own game and guidelines needed!
« Reply #16 on: December 13, 2015, 12:53:18 pm »
You are not seeding all of the internal state.
Something like his would be better for seeding it:
std::array<int, std::mt19937::state_size> seed_data;
std::random_device r;
std::generate_n(seed_data.data(), seed_data.size(), std::ref(r));
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));

std::mt19937 engine(seq);

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #17 on: December 13, 2015, 02:08:21 pm »
You are not seeding all of the internal state.
Something like his would be better for seeding it:
std::array<int, std::mt19937::state_size> seed_data;
std::random_device r;
std::generate_n(seed_data.data(), seed_data.size(), std::ref(r));
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));

std::mt19937 engine(seq);
thanks for mention it, i have done that for a my game but i have experienced performance issue when i try to seed for every call. in this game it okay it is not noticeable.

for generic random engine i have modified Jesper Juhl suggested to allow user to use either std::mt19937 or std::mt19937_64. by default it is for 32 bit like this:
template<class T = std::mt19937, std::size_t N = T::state_size>
auto ProperlySeededRandomEngine() -> typename std::enable_if_t<!!N, T> {
        std::array<typename T::result_type, N> seed_data;
        thread_local static std::random_device source;
        std::generate(std::begin(seed_data), std::end(seed_data), std::ref(source));
        std::seed_seq seeds(std::begin(seed_data), std::end(seed_data));
        T seeded_engine(seeds);
        return seeded_engine;
}

void utilities::shuffleVector(std::vector<int> &shuffle) {
        int i = 0;
        std::transform(shuffle.begin(), shuffle.end(), shuffle.begin(), [&i](int j) { return j = i++; });
        std::shuffle(shuffle.begin(), shuffle.end(), ProperlySeededRandomEngine());
}
« Last Edit: December 13, 2015, 02:48:51 pm by MORTAL »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: New to SFML, creating my own game and guidelines needed!
« Reply #18 on: December 13, 2015, 02:12:00 pm »
thanks for mention it, i have done that for a my game but i have experience efficiency issue when i try to seed for every call. in this game it okay it is not noticeable.
Why would you seed on every call? That's just silly.
You create and seed your engine once and then you keep reusing it (and never copy it - this is important; always pass it by reference or pointer).
« Last Edit: December 13, 2015, 02:14:40 pm by Jesper Juhl »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #19 on: December 13, 2015, 03:37:45 pm »
thanks for mention it, i have done that for a my game but i have experience efficiency issue when i try to seed for every call. in this game it okay it is not noticeable.
Why would you seed on every call? That's just silly.
You create and seed your engine once and then you keep reusing it (and never copy it - this is important; always pass it by reference or pointer).
exactly that i what i did to solve performance issue, i have devoted a class for it as showing here https://github.com/MORTAL2000/Space-Invaders-Clone/blob/master/Random.hpp

samm3

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #20 on: December 15, 2015, 09:44:02 pm »
After a bit of coding, I'm finally done! The game is working perfectly and exactly as it should.
I'd like to thank everyone who helped me, I learned a lot and the new randomizer noticeably improved the performance .

Here's the final product, https://www.dropbox.com/s/kmrf5b90b9wwjjf/Nostalgitripp.rar?dl=0

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #21 on: December 15, 2015, 09:51:30 pm »
sorry for late, i was thinking about what Jesper Juhl suggest to correct my previous implementation for shuffle. here latest update. i have comments the change to distinguished the differences between both codes

#include "Utilities.h"
#include <random>
#include <algorithm>
#include <functional> // std::ref
#include <array>

template<class T = std::mt19937, std::size_t N = T::state_size>
static auto ProperlySeededRandomEngine() -> std::enable_if_t<!!N, T&> //using typename, it's unnecessary. in c++14 std::enable_if_t will take care of that for us.
{
        std::array<typename T::result_type, N> seed_data;
        static std::random_device source; // thread_local implies static so writing both is redundant
        std::generate(std::begin(seed_data), std::end(seed_data), std::ref(source));
        std::seed_seq seeds(std::begin(seed_data), std::end(seed_data));
        static T seeded_engine(seeds);
        return seeded_engine;
}

void utilities::shuffleVector(std::vector<int> &shuffle) {
        static auto& RandomEngine = ProperlySeededRandomEngine(); // only once, take it by reference no copy
        int i = 0;
        std::transform(shuffle.begin(), shuffle.end(), shuffle.begin(), [&i](int j) { return j = i++; });
        std::shuffle(shuffle.begin(), shuffle.end(), RandomEngine); // reuse it
}
« Last Edit: June 09, 2016, 11:34:38 pm by MORTAL »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #22 on: December 15, 2015, 09:59:22 pm »
After a bit of coding, I'm finally done! The game is working perfectly and exactly as it should.
I'd like to thank everyone who helped me, I learned a lot and the new randomizer noticeably improved the performance .

Here's the final product, https://www.dropbox.com/s/kmrf5b90b9wwjjf/Nostalgitripp.rar?dl=0
congart,  :)
it looks really awesome

samm3

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #23 on: December 15, 2015, 10:08:18 pm »
congart,  :)
it looks really awesome

Thanks! Regarding the seed generator, is it really necessary to implement that in my small game for a perfect "random"? Because I doubt it'll matter when I turn it in to my teacher. I probably won't be able to explain it in detail if I'm questioned, as I lack "general" knowledge in C++, and the game itself is enough for an A :)

Also, mind explaining what std::transform does? I read that it has something to do with lambas but can't really find the real mening behind its purpose when I use it ;/

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #24 on: December 15, 2015, 10:24:23 pm »
Regarding the seed generator, is it really necessary to implement that in my small game for a perfect "random"? Because I doubt it'll matter when I turn it in to my teacher. I probably won't be able to explain it in detail if I'm questioned, as I lack "general" knowledge in C++, and the game itself is enough for an A :)

in your game it is not necessary but for your knowledge it is.
just little improvement to exist code, make sure when you use thread_local no need for explicit static, thread_local implies static for us.

Also, mind explaining what std::transform does? I read that it has something to do with lambas but can't really find the real mening behind its purpose when I use it ;/
best source for understanding std::transform is in this link
http://en.cppreference.com/w/cpp/algorithm/transform
« Last Edit: December 18, 2015, 07:25:44 pm by MORTAL »

samm3

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #25 on: December 16, 2015, 12:49:38 am »
I have to ask you... does the cards show up on different locations for you every time you start it or is it only the order that they're shown in that's randomized?

EDIT: My fault, removed and changed some old code. Solved it =)
« Last Edit: December 16, 2015, 01:38:37 am by samm3 »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #26 on: December 16, 2015, 04:07:22 am »
I have to ask you... does the cards show up on different locations for you every time you start it or is it only the order that they're shown in that's randomized?
i have modified your original code, so the code in my side is bit different than yours . and for your question, yeah the code here generates a new locations for sprites for every run.

here the code after modification: https://gist.github.com/MORTAL2000/9b8ac45d28abb97f6281

EDIT: My fault, removed and changed some old code. Solved it =)
glad you solved it :)
« Last Edit: December 18, 2015, 07:27:31 pm by MORTAL »

christinegrant98

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: New to SFML, creating my own game and guidelines needed!
« Reply #27 on: January 20, 2017, 12:16:19 pm »
Try this guide to fix Error Code 0xc000007b worked for me as i have faced the same problem