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.


Topics - Mortal

Pages: [1] 2
1
SFML projects / Tower Defence
« on: May 11, 2017, 08:59:27 pm »
i was planning to begin with 3D game as my first 3D project, but i have found an old game called Starports when i was looking for assets to make simple tower defence. but unfortunately it doesn't support SFML.

i wrote SFML extension to that engine. since it does exactly what i want to achieve for now.

here game-play video:
http://www.youtube.com/watch?v=hEpNgAeqLuQ

EXE file

2
General / comparing floating points
« on: January 24, 2017, 04:55:45 am »
hello.

first of all, apologies for the question wasn't directly related to SFML, but i was so frustrated about what's the proper way to compare floating points.  if someone had similar problem how can we do it.

i read alot of online papers about this subject. the latest one was about ULP which is used the integer comparison instead of comparing the float/double data. i've implemented this already but it seems not working either.
i made minimal example about ULPs method. which is, by the way taken from google units test. and i have changed it a little bit to suite my engine that i'm working on.
the example shows the failure of comparing two matrices for assign operator operator== when i tested the sf::Transform against A A-1 = A-1 A = I.

(click to show/hide)

3
SFML projects / experiment on Terrains
« on: October 30, 2016, 11:50:33 pm »
hello

i was working on terrain lately, i'm trying to cover as much as i can on this topic which it seems, it is massive topic wasn't a best choice to lean basic opengl on this path. but i have made some progresses so far.

here i have made a demo to test opengl 4.4 features by drawing terrain with Tessellation based on nvida demonstration on Tessellation and Program Pipelines. but unfortunately, this technique has huge drawback regarding performance especially for terrain atleast on my Labtop.

the demo shows the terrain colored based on its height and dynamic cloud & lighting effects.i made it with c++/SFML.  i would like to carry on this topic to get a decent knowledge for both opengl in general and terrain in specific

demo:
http://www.youtube.com/watch?v=2iLtqPRua7s

source code: github
 

4
Graphics / sf::Font with GLSL-program
« on: August 03, 2016, 11:02:57 pm »
hello

i'm trying to draw some letters on screen by using sf::Font and opengl-glsl shader, for some reasons the character appear as solid block. after searching for a solution, it turns that i have to enable" blend test" and use blend function but when i used it, the block disappeared as being blend totally with background color.

how to solve this?

here the code i used to draw single letter on screen:
(click to show/hide)

5
General / [solved] drawing in-place
« on: July 05, 2016, 10:17:16 pm »
hello

i'm working on demo for tile-based path-finder's algorithms. fortunately i found awesome tutorial about this subject with live demo, but it was written in javascript as shown here .

author provides C++ source code but it is ASCII console app. i tried to convert it to SFML-app but i had problem with draw the algorithm's processing. after many attempts i have come up with some thing like this .i know, it is awful, and there are many better ways to do it. but i can't wrap my head around it. in javascript, it seems an easy task. like this demo

i would like to know what is the best way to draw these kind of demos with SFML?
is there already open source for it?

thanks in advance

Edit:
SOLVED- i used vertex array. this is alone,  is enough to solve the render issue in my code.


6
SFML projects / Simple Super-Mario game
« on: June 25, 2016, 08:01:52 pm »
hello everyone.

here my first attempt to create simple platform game-alike, it is super mario game. game not yet finished, i just implemented basic components like physic, animation and collision detection. but the levels still need more.

i used Tiled software editor for tile-map  and pugixml lib for map parser.

whole project is meant for educational purpose,  it is suitable for beginner in both sfml/c++.

here main references:
For C++, SFML and animation: "SFML Game Development" book.
For Physics, Graphic Effects and Collision Detection: http://trederia.blogspot.com/

here video demo:(apologises for bad quality of video)

http://www.youtube.com/watch?v=KHOsrcTPdWQ

the project is available in here

i would like to know if someone had a chance to compile it, to give me feed back about this game.

thanks in advance

7
General / best way to calculate length of vector
« on: May 24, 2016, 03:30:30 pm »
usually when i wanted to compute the length of vector, i used traditional way as i have learnt from many tutorials by squared root the dot product of given vector. and these were usually defined in free function for each of them. like so,

//calculates dot product of 2 vectors
inline float dot(const sf::Vector2f& lv, const sf::Vector2f& rv)
{
    return lv.x * rv.x + lv.y * rv.y;
}

//Returns length of a given vector
inline float length(const sf::Vector2f& source)
{
    return std::sqrt(dot(source, source));
}

//Returns a given vector with its length normalized to 1
inline sf::Vector2f normalise(sf::Vector2f source)
{
    float length = std::sqrt(dot(source, source));
    if (length != 0) source /= length;
    return source;
}
 

and usage:
auto speed = length(velocity) * dt.asSeconds();
auto amount = normalise(velocity) * speed;
sprite.move(amount );

however, i have found in math.h header file a builtin function hypotf(x,y) that do exactly same thing. later in c++11 defined in cmath by std::hypot(x, y) where x and y is float, which is equivalent to std::fabs(std::complex<float>(x,y)).
typical usage would be some thing like this:

//Returns a given vector with its length normalized to 1
inline sf::Vector2f normalise(sf::Vector2f source)
{
    float length = std::hypot(source.x, source.y);
    if (length != 0) source /= length;
    return source;
}

auto speed = std::hypot(velocity.x, velocity.y) * dt.asSeconds();
auto amount = normalise(velocity) * speed;
sprite.move(amount );
 

as you can see both did same job while the first is most popular and other rarely used in game dev if not used ever.

my question why most of game dev don't use std::hypot and save them from rewriting those free functions of length and the dot every times, is it because of some performance drawbacks due to std::hypot. or they simply don't know about it.

8
General / problem when sf::RenderWindow is noncopyable
« on: May 01, 2016, 09:55:25 pm »
i'm recently working on new game design that reply on boost::any. the idea is simple, storing all update and draw methods for each game entities in appropriate container and call those methods when it is needed. sound simple and it can be done. also i went further and implemented my own any class similar to boost::any. my attempting seems work fine when i tested against primitives type (int, float and so on) even i tested with user-types. all previous tests went smoothly but when time come to test it with sfml i got some issues. for example, sf::Drawable::draw takes two parameters and it can be call through (sfml magic) by instance of any sf::RenderTarget. this doesn't work on my design. i solved it by make game entity non-drawable.

the issue that i can't find any solution for it is when sf::RenderWindow is noncopyable so, whenever, i tried to cast it down from boost::any or from my own any class, i got always bad cast assertion says "sf::RenderWindow& is deleted object". the only cheep solution that i come up with it, is to turn off assert checking, since i don't have access to boost::any, i changed my own any class and comment assert. the test code run fine but i not quite conformable with it.

here my code:
(click to show/hide)

my Questions:
how to solve noncopyable issue?
is my design reliable in game dev?

9
General discussions / Dispatcher with new meta-Function idiom
« on: April 19, 2016, 09:13:31 pm »
hello

i have an opportunity to get a proposal paper that might be included in C++ standard and i would like to share it with you. here a link to pdf file:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4436.pdf.
it is actually an idiom for meta-programming language. since we are mostly interesting in game development, this idiom might helpful for beginners like me to understand and implement it in any way that suits our need in game dev. an easy usage of this idiom would be dispatcher-like structures.

the dispatcher structure has a feature of registration facilities to attach member functions as listeners in easiest way possible. also, it has an ability to call right member function for any handler.

my interesting case to use this idiom in dispatcher-like structure is when i want the score to be updated for any  game's event that may happen.

here is a simple demo.
(click to show/hide)

10
General / scale animation problem
« on: April 05, 2016, 01:37:19 am »
hello

i'm having trouble time with scale animation which i thought it will be most easiest part in my game that i'm working on. but it turned i was wrong about it.

my goal is to achieve smooth and constant scaling animation. i have implemented the "easing" technique to calculate ratio of scale amount that need to step each frame to sprite object. it seems run but i didn't get what i was looking for.  the scaling effect is varying from time to time which is not suppose to happen. some time it goes faster while other goes slower.

also, i have faced problem with float point comparison that i can't compare two float values for equality with if statement, i had to implement "close enough" approach.

i have created minimal example to demonstrate the issue:
(click to show/hide)

my questions
how can i fix this tripping in animation?, is my implementation is correct? and are there other ways to achieve smooth and constant scale animation?

Edit: i fixed it, my bad, it was a bug in both source code. i corrected example here and code in my game both work fine at last but it seems i overly complicated it for just scaling , i'm sure there is an easy way to do it, but i don't know it.

is there a better way to do scale animation?


11
General / tilemap glitching[Solved]
« on: March 26, 2016, 12:11:05 pm »
hello,
i have been working on simple exercise on tilemap for 2d platformer but i have experienced some frame glitching. i have no idea what cause this issue.

could someone please help me in this issue. or atleast point me to what is really cause it.

source code:
(click to show/hide)


resources is available in git
https://github.com/MORTAL2000/sfml-examples


12
General / problem with attach and detach child in SceneNode [SOLVED]
« on: February 16, 2016, 01:22:43 am »
i have problem with command pattern in SFML book that works aside with scene-graph data structure when i detached child node and attached it once again. the VS14 doesn't help me much when i debug the problem. it is point me to the vector iterator is not incrementable in onCommend(), this function seems okay, it doesn't invalidate the iterator, it just a loop.

could someone please help me in this issue, thanks you.

i have made minimal example as i could to show the problem as shown below:
#include <stdexcept>
#include <iostream>
#include <utility>
#include <vector>
#include <memory>
#include <cassert>
#include <functional>
#include <queue>
#include <algorithm>

class SceneNode;

namespace Category
{
        enum Type
        {
                None = 0,
                Player1 = 1 << 0,
                Player2 = 1 << 1,
        };
}

struct Command
{
        using Action = std::function<void(SceneNode&)>;

        Command()
                : action()
                , category(Category::None)
        {
        }

        Action          action;
        unsigned int    category;
};

template
<
        typename GameObject,
        typename Function,
        typename = std::enable_if_t<std::is_base_of<SceneNode, GameObject>::value>
>
auto derivedAction(Function fn)
{
        return [=](SceneNode& node)
        {
                fn(static_cast<GameObject&>(node));
        };
}

class CommandQueue
{
public:
        void push(const Command& command)
        {
                mQueue.push(command);
        }

        Command pop()
        {
                auto command(mQueue.front());
                mQueue.pop();
                return command;
        }

        bool isEmpty() const
        {
                return mQueue.empty();
        }


private:
        std::queue<Command>     mQueue;
};

class SceneNode
{
public:
        using Ptr = std::unique_ptr<SceneNode>;


public:
        SceneNode(Category::Type category = Category::None)
                : mChildren()
                , mParent(nullptr)
                , mDefaultCategory(category)
        {
        }

        void attachChild(Ptr child)
        {
                child->mParent = this;
                mChildren.push_back(std::move(child));
        }

        Ptr detachChild(const SceneNode& node)
        {
                auto found = std::find_if(mChildren.begin(), mChildren.end(),
                        [&](Ptr& p)
                {
                        return p.get() == &node;
                });

                assert(found != mChildren.end());

                Ptr result = std::move(*found);
                result->mParent = nullptr;
                mChildren.erase(found);

                return result;
        }

        void onCommand(const Command& command)
        {
                // Command current node, if category matches
                if (command.category & getCategory())
                        command.action(*this);

                // Command children
                for (const auto& child : mChildren) // here VS14 complains
                        child->onCommand(command);
        }

        virtual unsigned int getCategory() const
        {
                return mDefaultCategory;
        }

        SceneNode* getParent() const
        {
                return mParent;
        }

        virtual void setNumber(int i) {};


private:
        virtual void draw(std::ostream& stream) const
        {
                drawCurrent(stream);
                drawChildren(stream);
        }

        friend std::ostream& operator<<(std::ostream& stream, const SceneNode& self)
        {
                self.draw(stream);
                return stream;
        }

        virtual void drawCurrent(std::ostream& stream) const
        {
                // do nothing
        }

        void drawChildren(std::ostream& stream) const
        {
                for (const auto& child : mChildren)
                        child->draw(stream);
        }

private:
        std::vector<Ptr>        mChildren;
        SceneNode*              mParent;
        Category::Type          mDefaultCategory;
};

class Player final : public SceneNode
{
public:
        enum Type
        {
                None,
                Player1,
                Player2,
        };

public:
        Player(Type type, int n, Player* player = nullptr)
                : mType(type)
                , mNumber(n)
                , mPlayer(player)

        {
        }

        void setNumber(int i) override
        {
                mNumber = i;
        }

        void follow()
        {
                if (getCategory() & Category::Player2)
                {
                        if (mPlayer)
                        {
                                auto found = getParent()->detachChild(*this);
                                found->setNumber(20);
                                mPlayer->attachChild(std::move(found));
                        }
                }
        }

        unsigned int getCategory() const override
        {
                if (mType == Type::Player1)
                        return Category::Player1;
                else
                        return Category::Player2;
        }

private:
        void drawCurrent(std::ostream& stream) const override
        {
                stream << "Parent: "<< getParent()->getCategory() << " has child: " << mNumber << '\n';
        }


private:
        Type mType;
        int mNumber;
        Player* mPlayer;
};

int main()
{
        SceneNode sceneGraph;
        CommandQueue commandQueue;

        Player* player1;
        Player* player2;


        auto first(std::make_unique<Player>(Player::Player1, 1));
        player1 = first.get();
        sceneGraph.attachChild(std::move(first));

        // Works
        //auto second(std::make_unique<Player>(Player::Player2, 2, player1));
        //player2 = second.get();
        //player2->setNumber(20);
        //player1->attachChild(std::move(second));

        // Works
        //auto second(std::make_unique<Player>(Player::Player2, 2, player1));
        //player2 = second.get();
        //sceneGraph.attachChild(std::move(second));
        //auto found = player1->getParent()->detachChild(*player2);
        //found->setNumber(20);
        //player1->attachChild(std::move(found));

        // Works
        //auto second(std::make_unique<Player>(Player::Player2, 2, player1));
        //player2 = second.get();
        //sceneGraph.attachChild(std::move(second));
        //player2->follow();

        // Failed ????????
        auto second(std::make_unique<Player>(Player::Player2, 2, player1));
        player2 = second.get();
        sceneGraph.attachChild(std::move(second));

        Command c;
        c.action = derivedAction<Player>(std::bind(&Player::follow, std::placeholders::_1));
        c.category = Category::Player2;
        commandQueue.push(c);

        while (!commandQueue.isEmpty())
                sceneGraph.onCommand(commandQueue.pop());

        std::cout << sceneGraph;
}

13
General / text doesn't draw
« on: February 03, 2016, 02:23:04 am »
i tried to display some text on screen with simple triangle shape by using opengl but for some reasons that doesn't work with my code. how to fixed this

here my code:

#include <gl/glew.h>
#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "OpenGL");

        window.setActive();

        glewExperimental = GL_TRUE;
        if (glewInit() != GLEW_OK)
                return 1;

        sf::Font font;
        if (!font.loadFromFile("Media/Sansation.ttf"))
                return 1;

        sf::Text text("test", font);
        text.setPosition(5.f, 5.f);

        GLuint vboID;
        glGenBuffers(1, &vboID);
        GLfloat Vertices[] =
        {
                -1, -1, 0,
                 0,  1, 0,
                 1, -1, 0,
        };

        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

        glClearColor(0.f, 0.f, 0.f, 1.f);

        while (window.isOpen())
        {
                sf::Event windowEvent;
                while (window.pollEvent(windowEvent))
                {
                        if (windowEvent.type == sf::Event::Closed)
                                window.close();
                }

                // opengl drawing
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glEnableVertexAttribArray(0);
                glBindBuffer(GL_ARRAY_BUFFER, vboID);
                glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

                glDrawArrays(GL_TRIANGLES, 0, 3);

                glDisableVertexAttribArray(0);

                // sfml drawing : failed ???????????
                window.pushGLStates();
                window.draw(text);
                window.popGLStates();

                window.display();
        }
}

14
General / How to swap the transform of two objects[Solved]
« on: January 19, 2016, 01:10:58 am »
i'm facing problem with swapping transform(position, scale, rotate) of two object of same type that has transformable member data. i can do it by swapping each of them manually but i think this is noneffective. if i understood it correctly, i think this can be done by std::swap or by "=operator" which is used copy constructor of each given objects and their transform to the new object.

 here example that supposed to work but it didn't.

#include <SFML/Graphics.hpp>

class Tile : public sf::Drawable
{
public:
        Tile(const sf::Texture& texture)
                : mSprite(texture)
                , mTransform()
        {
        }
        ~Tile() = default;

        Tile(const Tile& othher)
                : mSprite(othher.mSprite)
                , mTransform(othher.mTransform)
        {
        }

        Tile& operator=(Tile other) noexcept
        {
                other.swap(*this);
                return *this;
        }

        //Tile(Tile&& other)
        //{
        //      swap(*this, other);
        //}
        //Tile& operator=(Tile&&) = default;

        void swap(Tile& other) noexcept
        {
                using std::swap;

                swap(mTransform, other.mTransform);
                swap(mSprite, other.mSprite);

        }

        friend void swap(Tile& lhs, Tile& rhs) noexcept
        {
                using std::swap;
                swap(lhs.mTransform, rhs.mTransform);
                swap(lhs.mSprite, rhs.mSprite);
        }

        void setPosition(float x, float y)
        {
                mTransform.setPosition(x, y);
        }

        void setPosition(sf::Vector2f pos)
        {
                mTransform.setPosition(pos);
        }

        sf::Vector2f getPosition() const
        {
                return mTransform.getPosition();
        }

        void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                states.transform = mTransform.getTransform();
                target.draw(mSprite, states);
        }


private:
        sf::Sprite mSprite;
        sf::Transformable mTransform;
};

int main()
{
        sf::RenderWindow window(sf::VideoMode(480u, 480u), "Tile tut");
        window.setVerticalSyncEnabled(true);

        sf::Texture texture1, texture2;
        if (!texture1.loadFromFile("0.png"))
                return 1;

        if (!texture2.loadFromFile("1.png"))
                return 1;

        Tile tile1(texture1), tile2(texture2);

        tile1.setPosition(100.f, 120.f);
        tile2.setPosition(100.f + texture1.getSize().x, 120.f);

        bool toggle = false;

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();

                        else if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::Space)
                                {
                                        toggle = !toggle;
                                }
                        }
                }

                if (toggle)
                {
                        toggle = false;

                        // normal position swap works fine:~ do same thing for rotate and scale
                        //auto pos = tile1.getPosition();
                        //tile1.setPosition(tile2.getPosition());
                        //tile2.setPosition(pos);


                        // doesn't works
                        //auto tile = tile1;
                        //tile1 = tile2;
                        //tile2 = tile;
                       
                        //tile1.swap(tile2); // doesn't works
                        //std::swap(tile1, tile2); // doesn't works
                }

                window.clear();
                window.draw(tile1);
                window.draw(tile2);
                window.display();
        }
}

how to fixed this it, did i miss something that i should take care of?

Edit
sorry it seems the problem with sf::sprite's texture not with tansform, i fixed it

15
SFML projects / Simple 15 Puzzle Game
« on: January 14, 2016, 10:27:56 pm »
i have made simple puzzle game, it is quite simple and easy, i have done it before but in win32 console. when i start learning C++,

GamePlay:
the game is simple has only one button "scramble" it does shuffle the numbers and start from beginning, and user may use arrow keys to move in the only space available in the game. i didn't add time limit. it's a bit harder by itself.

Source Code:
https://github.com/MORTAL2000/15-Puzzle-Game

finially i'm able to complete the puzzle. here a demo of game play
http://www.youtube.com/watch?v=qArw1fOMP04

Pages: [1] 2