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 - mashedtatoes

Pages: [1]
1
SFML projects / S2DEC: A entity component game engine in C++
« on: August 14, 2015, 07:56:32 am »
https://github.com/S2DGames/S2DEC

I have been working on this engine on and off for a while now. I mainly use it for game jams but I haven't really made any noteworthy games with it yet. I always seem to end up working on the engine more than the game whenever ludum dare comes around. Anyways back on topic.

I finally feel like the engine is at a point where someone else might be interested in using it if they knew about. So I'm not going to put off posting here any longer.

S2DEC is a 2d game engine that uses an entity component system for game objects and also utilizes box2d for physics.

I will edit this post tomorrow with a small tutorial because it is nearly 2am and I have to get up for work tomorrow. For now you can look in the game folder for am idea of how it works.  https://github.com/S2DGames/S2DEC/tree/master/S2DEC/Games pong is the best example in there. Snake runs like crap once you get a lot of segments and space invaders is not done or really even started.

Edit:
Here is a short overview of my engine. To use it, first create a game object passing the resolution and the window title like this
Game game(1920, 1080, "Game title");
Next you can either initialize  the game which will open the window,
game.init();
or change other settings such as tell the game to open full screen
game.setFullScreen();
Once we have the window set up the way we want, before or after initializing the window, we need to add our game objects to the game.

We do this by creating an entity
Entity& entity = game.createEntity("entity name");
and add a component with
entity.addComponent<SampleComponent>(/*paramaters to pass to the component constructor*/);
Here is the code for sample component which is just a box that moves around the screen depending on what arrow key you press.
#pragma once

#include "Game.h"

using namespace S2D;

class SampleComponent : public Component{
private:
        //stuff for box2d.
        b2Body* body{nullptr};
        b2BodyDef bodyDef;
        b2PolygonShape shape;
        b2Fixture* fixture{nullptr};
        b2Vec2 velocity{0.0f, 0.0f};

        sf::RectangleShape image;

        sf::Vector2f size;
public:
        SampleComponent(sf::Vector2f size) : size(size){
               
        }

        /**
        * Called when this component is added to an Entity.
        */

        void init() override{
                //////////////////////////set up physics stuff////////////////////////////
                bodyDef.type = b2_dynamicBody;
                //start off in the center. note that we need to use this function to convert the window coordinates to physics world coordinates
                bodyDef.position = sfTob2(game->getView().getCenter());
                //Here we are using the same function on a single number as apposed to a vector
                shape.SetAsBox(sfTob2(size.x / 2.0f), sfTob2(size.y / 2.0f));
                //create the body
                body = game->CreateBody(&bodyDef);
                //we dont need this now but if this body were to collide with something, you need to set the user data so it is notified viea a callback function
                body->SetUserData(this);
                body->SetFixedRotation(true);
                //create the fixture.
                //a body does not have a shape unless it has a fixture
                fixture = body->CreateFixture(&shape, 1.0f);
                fixture->SetFriction(0.0f);
                fixture->SetRestitution(1.0f);

                //////////////////////////set up image stuff///////////////////////////////
                image.setSize(size);
                movesfTob2(image, body);
        }

        /**
        * Called once every frame.
        */

        void update(float frameTime) override{
                //check keyboard keys. There are 4 states,
                //KEY_PRESSED   - The key was just pressed
                //KEY_HELD              - The key was pressed and is still being pressed
                //KEY_RELEASED  - The key was just released
                //NOT_PRESSED   - The key is not pressed and has not just been released
                if(game->getKeyState(sf::Keyboard::Up) != NOT_PRESSED){
                        velocity.y -= 1.0f;
                }
                if(game->getKeyState(sf::Keyboard::Down) != NOT_PRESSED){
                        velocity.y += 1.0f;
                }
                if(game->getKeyState(sf::Keyboard::Left) != NOT_PRESSED){
                        velocity.x -= 1.0f;
                }
                if(game->getKeyState(sf::Keyboard::Right) != NOT_PRESSED){
                        velocity.x += 1.0f;
                }

                //set the velocity of the physics body
                body->SetLinearVelocity(velocity);

                //update the image position
                movesfTob2(image, body);
                //reser the velocity back to 0 so it only moves when a button is being pressed
                velocity = {0.0f, 0.0f};
        }

        /**
        * Called once every frame.
        */

        void draw(sf::RenderTarget& target) override{
                //lastly draw the image
                target.draw(image);
        }
};

Calling
game.play();
will begin running the main loop of the game and will update and draw all objects that were added to the game. In this case, there is only one. You can also set the z of an entity which will determine the order it is drawn.

2
CircleShape, RectangleShape, ConvexShape, and Sprite do very similar things. They all apply a texture to some shape. What I am suggesting is to remove the functionality that allows the shapes to be drawable and add the ability to apply a shape to a sprite. Then you create the shape first and pass that shape into the constructor for the sprite. So rather than creating just a CircleShape, you would create a CircleShape then create a sprite using that CircleShape. When you apply the texture to the sprite, it will only give you a circular cutout of the texture.

This does add an extra line of code but, to me, nothing about CircleShape seems like it should be anything more than a shape (just a bunch of points in some space). You shouldn't be able to apply a texture to this shape and draw it because it is just a shape. It shouldn't be able to represent an image. That is where Sprite and Image should be used.

It just seems a lot more intuitive to me if the process of creating a shaped drawable was
  • Create a shape
  • Create a container (sprite) using that shape
  • Apply the texture to the container

Box2D creates shaped objects in a similar way so if I did not explain it very good and you would like some code to look at, you can look at Box2D's b2Fixture.

3
Graphics / sf::Shader::CurrentTexture and texture rects
« on: October 23, 2014, 11:42:36 pm »
For example take a sprite sheet. It is not exactly what I am doing but similar. Lets say you set the texture rect  to the first frame in the animation. When you use sf::Shader::CurrentTexture to set the parameter in the shader code, is this current texture the entire sprite sheet or is it just the part of the texture within the texture rect?

4
Graphics / [Solved] Help with rotating a texture rect around a point
« on: October 13, 2014, 12:36:48 am »
I am trying to make a planet appear to be rotating on an axis parallel to the plane of the screen (2d game). I have a 5000 x 5000 pixel texture of randomly generated continents/oceans (here is a smaller example. imgur wont let me upload the big one. For each planet I choose a random point on this texture and set the texture rect at this position and the width/height to the diameter of the planet. Here is a drawing to help visualize what I am trying to do and here is a gif of what I have achieved so far but it isn't quite right. In the image, I want the texture rect to translate around the larger circle in a small amount each frame. As for the gif, I honestly have no clue what is causing that stretching stuff as there is nothing like that in the texture. It is possible that it is going out of the bounds of the texture because i don't check for that yet. Any help is appreciated.

Here is the code after setting the texture rect and radius of the circle,

    //this is the radius of the larger circle that the texture rect will move along
        bigRadius = (std::sqrt(2.0) + 1) * circle.getRadius() - circle.getRadius();
    //origin of the circle is set to the center of the circle. this is the point we want to translate (circle is the planets circleshape)
        point = circle.getPosition();
        //point we want to translate around
        centerPoint.x = circle.getPosition().x;
        centerPoint.y = circle.getPosition().y + bigRadius;

Here is my update function (angle is initialized to 0)
        angle += .1;
        float sin = std::sin(angle * DEGTORAD);
        float cos = std::cos(angle * DEGTORAD);
        point.x = centerPoint.x * cos - centerPoint.y * sin;
        point.y = centerPoint.x * sin + centerPoint.y * cos;
        circle.setTextureRect(sf::IntRect(point.x - circle.getRadius(), point.y - circle.getRadius(), circle.getTextureRect().width, circle.getTextureRect().height));

If you watch the gif closely, you can see that the translation is kind of working but I don't know what to do to make it work completely. I also know this is not exactly parallel to the plane of the screen but this is the best I could come up with. Anybody have any ideas?

Pages: [1]
anything