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

Author Topic: SFML Game Development -- A book on SFML  (Read 268981 times)

0 Members and 2 Guests are viewing this topic.

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: SFML Game Development -- A book on SFML
« Reply #90 on: July 25, 2013, 11:36:26 pm »
I succesfully added a LUA bind to the "engine" the book make us do.

First of all I get LUA library and Lua SLB, wich is a binder for C++. You can find it here : http://code.google.com/p/slb/
I used the version 2 because I had already used it in the past, but version 3 should be fine.

I dunno if what I did is the best way to do, but here it is :

- I added LUA and Lua SLB headers to my project
- In the ResourcesIdentifiers I added a Scripts namespace with an enum of my scripts
- I created a ScriptPlayer class witch the equivalent of MusicPlayer but for LUA scripts. It has two methods to load scripts : registerFile() and regsterScript() because a LUA script can be in a file or directly in C++ string. The ScriptPlayer is also responsible for storing the lua_State pointer. I pass it to my SceneNode in the Application Context (with other resources managers).
- Then I added a ScriptNode wich keep a pointer onto the ScriptPlayer and plays a script under a certain command or at update or as you want...
- I added functions that bind my essential C++ classes to LUA with SLB. For now I just binded sf::Vector2f, SceneNode and Entity for tests purposes, but you can bind whatever you want.

The result is that I can make a fight between David and Goliath (wich are Entities) in LUA, even move them if I want to :

SLB.using(SLB);

local david = Entity(100);
local goliath = Entity(200);

local position = david:getWorldPosition();
--print("David is at (".. position.x ..":".. position.y ..")");

while ((not david:isDestroyed()) and (not goliath:isDestroyed())) do
        if (david:getHealthpoints() < 80) then
                -- Heal himself
                david:heal(30);
        else
                -- David attack Goliath
                goliath:damage(10);
        end

        -- Goliath attacks David
        david:damage(20);

        -- Recap
        print("\n----------------------------");
        print("David: "..david:getHealthpoints().."HP");
        print("Goliath: "..goliath:getHealthpoints().."HP");
        print("\n");
end

Exposal look like this :

void exposeVector()
{
    SLB::Class<sf::Vector2f>("Vector2f")
            .constructor<float, float>()
            .property("x", &sf::Vector2f::x)
            .property("y", &sf::Vector2f::y);
}

void exposeSceneNode()
{
    SLB::Class<SceneNode, SLB::Instance::NoCopy>("SceneNode")
            .constructor<Category::Type>()
            .set("getWorldPosition", &SceneNode::getWorldPosition);
}

And the ScriptPlayer definition :

class ScriptPlayer : private sf::NonCopyable
{
public:
    ScriptPlayer();
    ~ScriptPlayer();

    bool play(Scripts::ID script);

    void        registerFile(Scripts::ID script, const std::string& filename);
    void        registerScript(Scripts::ID script, const std::string& content);

private:
    lua_State*  mLuaState;
    std::map<Scripts::ID, std::string> mScripts;

};


If you want to have more sources about that, you're welcome to get an eye on my GitHub repo : https://github.com/Lo-X/potato-framework
You may (and must :p ) need to read the book to understand the "framework"

As I already said, I'm not sure this way to do is the best, but as far as my tests go, it works
« Last Edit: July 25, 2013, 11:39:49 pm by Lo-X »

xerwin

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SFML Game Development -- A book on SFML
« Reply #91 on: July 26, 2013, 11:09:57 am »
Hello, hopefully this is in the right place.

I have this book and I'm at the end of chapter 2. However I can't compile it in VS2012. I created the resource holder class almost word-for-word from the book (except for variable name and exception text) and took the code from chapter 1 and changed it so that it should work with the ResourceHolder class.

However, when I try to compile it, I get ton of errors. I have experience with C++ and to me, my code feels like it's OK, I don't see any errors with it. Instead of reposting it here, here is link to stackoverflow where I originally posted this question.

http://stackoverflow.com/questions/17876510/visual-studio-wont-compile-template-class-with-inl-implementation#17876808

If you could tell me what I'm doing wrong, I would be very glad.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Development -- A book on SFML
« Reply #92 on: July 26, 2013, 01:58:47 pm »
The code on the PacktPub website is not the latest version. Try the one from the GitHub repository:
https://github.com/SFML/SFML-Game-Development-Book
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: SFML Game Development -- A book on SFML
« Reply #93 on: July 26, 2013, 08:17:07 pm »
I downloaded the .zip of github code and I'd say that cmake script could be improved, because:

- I go to main dir (the one with cmake lists)
- I use "cmake ."
- I use "make"
- Then I switch to dir 09/Source (the one with binary) and I launch the app "./09_Audio" and I got:
Quote
EXCEPTION: ResourceHolder::load - Failed to load Media/Sound/AlliedGunfire.wav

The solution is to tell cmake to place binaries in "09_Audio/" rather than "09_Audio/Source".

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10936
    • View Profile
    • development blog
    • Email
Re: SFML Game Development -- A book on SFML
« Reply #94 on: July 26, 2013, 09:36:45 pm »
I downloaded the .zip of github code and I'd say that cmake script could be improved, because:

The solution is to tell cmake to place binaries in "09_Audio/" rather than "09_Audio/Source".
Fix it and make a pull request. :P
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Max

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: SFML Game Development -- A book on SFML
« Reply #95 on: July 28, 2013, 02:26:29 pm »
Hi!
I purchased this book and are eager to read it. Normally I would not buy books on Amazon that does not have a preview, so I would recommend that you (the authors) write an email to PacktPub about that. But in this case I support SFML by buying this book. :)
Also it would not hurt if there was more customer reviews.

From what I could tell all those chapters look like they are packed with good stuff, like networking for instance.
Typically that's an area most game dev books just skims over only to show how to send a TCP/UDP-packet. But totally ignores to go deeper on how to make LAN/Internet game (i.e: a 2d tank internet game ). So my hopes is high on this one.

Thanks for make a great community!

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: SFML Game Development -- A book on SFML
« Reply #96 on: July 28, 2013, 02:40:23 pm »
The book covers how to not just send packages but how to actually utilize it in your game as well :)

Hope you will enjoy the book!
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Game Development -- A book on SFML
« Reply #97 on: July 28, 2013, 03:27:39 pm »
Thanks for buying the book! Concerning preview: on the PacktPub site, there is a detailed description, a table of contents and a sample chapter :)
« Last Edit: July 28, 2013, 03:47:13 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: SFML Game Development -- A book on SFML
« Reply #98 on: July 28, 2013, 04:07:01 pm »
I'm perhaps mistaken but I think there's some preview with the Amazon Kindle as well, weird that tere's not preview on Amazon website.

Good reading, you'll enjoy the network part :p

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: SFML Game Development -- A book on SFML
« Reply #99 on: July 28, 2013, 06:48:40 pm »
Thanks to the book, I implemented my first QuadTree today !

Taking advantage of C++11 and the SceneNodes and Command power, it was pretty easy to implement another tree that takes collidable SceneNodes and add them in the right Quad.
Well, this is a pretty simple QuadTree. It does not create children in function of the number of objects in it (yet). Still it can stores objects and return close objects from another one. Handy for collision tests or targeting.

The QuadTree class look like :

class QuadTree
{
public:
    QuadTree(int level, const sf::FloatRect& bounds);
    void                    insert(SceneNode *object);
    void                    clear();
    const sf::FloatRect&    getBounds() const;
    void                    getCloseObjects(SceneNode* from, std::deque<SceneNode*>& returnedObjects);

private:
    bool                    isFinal() const;
    bool                    hasChildren() const;
    void                    split();
    int                     getChildIndex(const sf::FloatRect& rect);

private:
    sf::FloatRect           mBounds;
    int                     mLevel;
    int                     mMaxLevel = 4;
    std::deque<SceneNode*>  mObjects;
    std::array<QuadTree*,4> mChildren;
};

Thanks to authors, filling the QuadTree is as simple as :

Command command;
command.category = Category::All;
command.action = derivedAction<CollideNode>([this] (SceneNode& node, sf::Time) {
        mQuadTree.insert(&node);
});
mScene.onCommand(command, dt);

As for the LUA bind I made, that code is available freely and completely on GitHub. Questions or comments are welcome (perhaps by MP).

Awesome book. I kinda doubled things I can do in video game developpement !

Yemeni Cpluspluser

  • Newbie
  • *
  • Posts: 25
  • C/C++, Soon Java and SQL ^_^
    • View Profile
    • Email
Re: SFML Game Development -- A book on SFML
« Reply #100 on: July 28, 2013, 07:41:06 pm »
This is a great leap forward for the productivity of SFML, having a first book released (even if it costs some coins)  it will motivate a lot of users to start writing there own books for SFML. 
sf::signature mySignature;
mySignature.setPosition(forum.x,forum.y);
window.draw(mySignature);

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: SFML Game Development -- A book on SFML
« Reply #101 on: July 29, 2013, 08:49:56 am »
It is awesome to see you invent new stuff with what we provide. Though wondering, is it not possible to make the scene graph into the actual quad tree? The branch nodes would simply be scene nodes. It might be a bit more work though but would be nice to not have two structures refer to the objects.
« Last Edit: July 29, 2013, 08:51:57 am by Groogy »
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: SFML Game Development -- A book on SFML
« Reply #102 on: July 29, 2013, 10:00:07 am »
It is awesome to see you invent new stuff with what we provide. Though wondering, is it not possible to make the scene graph into the actual quad tree? The branch nodes would simply be scene nodes. It might be a bit more work though but would be nice to not have two structures refer to the objects.

Hi,

I thought about that. The problem I have is that the QuadTree is rebuilt every frame (or each time there's movement). So I need to have SceneNodes stored somewhere. Of course that somewhere can be a simple vector, but I like the advantages of Tree.

Of course, there's perhaps a solution I can not think about to have only a QuadTree and yes, It would be better not to have two stuctures. I'm open to ideas =) I perhaps need to dig into the restructuration of  a SceneGraph given a "parameter" (in our case position, but why not something else like a z-index or something).

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: SFML Game Development -- A book on SFML
« Reply #103 on: July 31, 2013, 10:58:59 am »
Why resource manager uses std::map instead of std::unordered_map?

You don't iterate through resources so unordered_map should be faster (by how much, it depends on the compiler's implementation but still I think adding one word for even small improvment is a good thing).
« Last Edit: July 31, 2013, 11:00:41 am by netrick »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10936
    • View Profile
    • development blog
    • Email
Re: SFML Game Development -- A book on SFML
« Reply #104 on: July 31, 2013, 12:14:55 pm »
Why resource manager uses std::map instead of std::unordered_map?

You don't iterate through resources so unordered_map should be faster (by how much, it depends on the compiler's implementation but still I think adding one word for even small improvment is a good thing).
I've been thinking the same when reading it.
Also since they are already using some C++11 features, std::unordered_map wouldn't be an awkward addition.

Then again, given that it's not something that gets accessed that many times per frame iteration, the performance increase will probably be very small.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/