• 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.
  • entries
    743
  • comments
    1924
  • views
    578275

Varadic templates and signals/slots

Sign in to follow this  
Followers 0
Aardvajk

681 views

Been playing around with a typesafe signal/slot system (yes another one) using varadic templates tonight. Its so much cleaner than the existing approaches pre-C++0x. It's a real demonstration of the usefulness of this awesome new feature:#include #include #include template class Signal;template class AbstractSlot{public: virtual ~AbstractSlot(); virtual void add(Signal *s){ v.push_back(s); } virtual void remove(Signal *s){ auto i = v.begin(); while(i != v.end()){ if(*i == s) i = v.erase(i); else ++i; } } virtual void call(Args... args) = 0;private: std::vector*> v;};template class Slot : public AbstractSlot{public: Slot(T *t, void(T::*f)(Args...)) : t(t), f(f) { } virtual void call(Args... args){ (t->*f)(args...); }private: T *t; void(T::*f)(Args...);};template class Signal{public: ~Signal(){ for(auto i = v.begin(); i != v.end(); ++i) (*i)->remove(this); } void connect(AbstractSlot &s){ v.push_back(&s); s.add(this); } void disconnect(AbstractSlot *s){ auto i = v.begin(); while(i != v.end()){ if(*i == s) i = v.erase(i); else ++i; } } void operator()(Args... args){ for(auto i = v.begin(); i != v.end(); ++i) (*i)->call(args...); }private: std::vector*> v;};template AbstractSlot::~AbstractSlot(){ for(auto i = v.begin(); i != v.end(); ++i) (*i)->disconnect(this);}
Some usageclass Thing{public: Thing(const std::string &name) : name(name) { } void f1(){ std::cout << name << " f1\n"; } void f2(int a){ std::cout << name << " f2 " << a << "\n"; } void f3(float f, char c){ std::cout << name << " f3 " << f << " " << c << "\n"; } std::string name;};int main(){ Thing f("One"); Thing g("Two"); Slot s1(&f, &Thing::f2); Slot s2(&g, &Thing::f2); Signal x1; x1.connect(s1); x1.connect(s2); Signal x2; Slot s4(&f, &Thing::f3); x2.connect(s4); x2(123.4f, 'x'); if(true) { Thing h("Three"); Slot s3(&h, &Thing::f2); x1.connect(s3); x1(100); std::cout << "END OF SCOPE\n"; } x1(230375);}
You can even do this: Signal<> x3; Slot s5(&f, &Thing::f1); x3.connect(s5); x3();
So you have a completely uniform way of defining and calling things, without any of the hackery we used to have to rely on for this stuff.

Excellent. Jolly good.

EDIT:

Also, it seems if you make a non-type-dependant base class for Slot, you can do this:class Slots{public: Slots(){ } ~Slots(){ for(auto i = v.begin(); i != v.end(); ++i) delete *i; } template void connect(T *t, void(T::*f)(Args...), Signal &s){ v.push_back(new Slot(t, f, s)); }private: std::vector v;};
Then rather than have a separate object for each slot in the owning class, you can just do:class SomeProvider{public: Signal signal; Signal other;};class Thing{public: Thing(SomeProvider &provider);private: void f(int i, const std::string &s){ } void g(char c){ } Slots slots;};Thing::Thing(SomeProvider &provider){ slots.connect(this, &Thing::f, provider.signal); slots.connect(this, &Thing::g, provider.other);}
So you only have one object, and the compiler (GCC in my case) appears to be able to deduce all of the types in the statement so no need for any template specification at the point of use, which is nice.

(I'm aware I need to implement the rule of three for these classes by the way, just an example).

4
Sign in to follow this  
Followers 0


0 Comments


There are no comments to display.

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