Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 27 Jun 2001
Online Last Active Today, 10:20 PM

Posts I've Made

In Topic: Code review of a pong clone created using SDL

Yesterday, 12:29 PM

You don't need a "RightPaddle" and "LeftPaddle" class.  Make a "Paddle" class, and pass in the parameters to the constructor that defines where it lives, and what keys move it.


Also, you shouldn't blindly have every surface defined in "globals.h" and have every class have access to them.  Define those surfaces in the classes that use them, or in the main file where they're used.  Then, you can pass in what information each class needs.


The huge if else if block in main.ccp is not needed.  Simply do something like this:


char score[4];
// get player 1 score
sprintf(score, "%d", ball.Player1Score);
score1 = TTF_RenderText_Solid(font, score, textColor);
// get player 2 score
sprintf(score, "%d", ball.Player2Score);
score2 = TTF_RenderText_Solid(font, score, textColor);


And, why are score1 and score2 globals?  Shouldn't they should be defined in main() since that's the only place they are used?


I didn't look at the rest, just a few things I noticed.


Good luck and have fun.

In Topic: Component-Entity game design issue

Yesterday, 12:08 PM

Hi MuTeD,


I wanted to share with you an example, from my entity component system, of how you can build entities with components, and how they can be controlled with systems.  Much of the implementation is squirreled away in the engine, but it hopefully gives you an idea .


This just shows how I create systems, and then create 2 entities, one is a player controlled entity, the other has an "enemy" component, and is AI controlled

    pWorld = Esc::TWorld::GetInstance();
    mpSpace = cpSpaceNew();
    // create all the systems we'll need
    PhysicsSystem = new TPhysicsSystem(mpSpace);
    RenderingSystem = new TRenderingSystem(app);
    DamageSystem = new TDamageSystem();
    DeathSystem = new TDeathSystem(mpSpace);
    InputSystem = new TInputSystem(app);
    EnemySystem = new TEnemySystem();
    // add the systems to the Entity-Component world
    pWorld->AddSystem(EnemySystem, 1000, true);
    pWorld->AddSystem(new TTestSystem());
    Esc::TEntityPtr Entity1;
    Esc::TEntityPtr Entity2;
    // Create the 1st entity, to be the player, and has the "Input" component
    Entity1 = pWorld->CreateEntity();
    // Generate all the components this entity will use (Graphics, Physical, Health, and Input)
    TGraphicsObject* graphicComp(new TGraphicsObject(Utilities::ImageGet("gfx/player.bmp")));
    TPhysicalObject* physicalComp;
    THealth* healthComp(new THealth(100, 100));
    TInput* inputComp(new TInput());
    // Create a Physics object
    uint32_t width, height;
    graphicComp->GetSize(width, height);
    cpBody *pBody = cpBodyNew(1,
                           cpMomentForCircle(1, (width+height)/4,
                                             0.0f, cpvzero));
    pBody->p = cpv(pApp->GetWidth()/2,pApp->GetHeight()/2);
    //pBody->v_limit = 200.0f;
    cpShape *pShape = cpCircleShapeNew(pBody, (width+height)/4, cpvzero);
    pShape->collision_type = 1;
    pShape->layers = 1;
    pShape->e = 0.0f;  // not elatics, no bouncing
    pShape->u = 0.0f; // 0.0 is frictionless
    cpSpaceAddBody(mpSpace, pBody);
    cpSpaceAddShape(mpSpace, pShape);
    physicalComp = new TPhysicalObject(mpSpace, pBody, pShape, 200.0f);
    // Now add all the components to the entity
    pWorld->AddComponent(Entity1, graphicComp);
    pWorld->AddComponent(Entity1, physicalComp);
    pWorld->AddComponent(Entity1, healthComp);
    pWorld->AddComponent(Entity1, inputComp);
    pWorld->SetGroup(Entity1, "Player");
    // Add Enemy entity, no Input component, instead an "Enemy" component
    Entity2 = pWorld->CreateEntity();
    TGraphicsObject* graphicComp2(new TGraphicsObject(Utilities::ImageGet("gfx/blueenemy.bmp")));
    TPhysicalObject* physicalComp2;
    TDamage* damageComp2(new TDamage(10));
    TEnemy* enemyComp2(new TEnemy());
    // Create a Physics object
    graphicComp->GetSize(width, height);
    cpBody *pBody2 = cpBodyNew(1,
                           cpMomentForCircle(1, (width+height)/4,
                                             0.0f, cpvzero));
    pBody2->p = cpv(400,400);
    //pBody2->v_limit = 150.0f;
    cpShape *pShape2 = cpCircleShapeNew(pBody2, (width+height)/4, cpvzero);
    pShape2->collision_type = 2;
    pShape2->layers = 1;
    pShape2->e = 0.0f;  // not elatics, no bouncing
    pShape2->u = 0.0f; // 0.0 is frictionless
    cpSpaceAddBody(mpSpace, pBody2);
    cpSpaceAddShape(mpSpace, pShape2);
    physicalComp2 = new TPhysicalObject(mpSpace, pBody2, pShape2, 150.0f);
    pWorld->AddComponent(Entity2, graphicComp2);
    pWorld->AddComponent(Entity2, physicalComp2);
    pWorld->AddComponent(Entity2, damageComp2);
    pWorld->AddComponent(Entity2, enemyComp2);

Later, I have an "InputSystem" which only runs on entities with the "Input" component

void TInputSystem::Update(Esc::TEntityPtr entity, uint32_t tickDelta)
    TPhysicalObject *physicalObject =
    const sf::Input& Input = mpApp->GetInput();
    cpFloat speed = physicalObject->GetMaxSpeed();
    // find location of Mouse and point player towards it
    cpFloat MouseX = (cpFloat)Input.GetMouseX();
    cpFloat MouseY = (cpFloat)Input.GetMouseY();
    cpFloat Angle = atan2(MouseY - physicalObject->GetBody()->p.y,
                          MouseX - physicalObject->GetBody()->p.x);
    physicalObject->GetBody()->a = Angle;
    if (Utilities::KeyIsDown(Input, GAME_KEY_UP)) {
        physicalObject->GetBody()->f.x = cos(Angle)*speed;
        physicalObject->GetBody()->f.y = sin(Angle)*speed;
    else if (Utilities::KeyIsDown(Input, GAME_KEY_FIRE)) {
        physicalObject->GetBody()->f.x = cos(Angle)*2*speed;
        physicalObject->GetBody()->f.y = sin(Angle)*2*speed;
    else {
        physicalObject->GetBody()->f = cpvzero;
    if (Utilities::KeyIsDown(Input, GAME_KEY_DOWN)) {
        // Slow it down
        physicalObject->GetBody()->v.x = physicalObject->GetBody()->v.x + -physicalObject->GetBody()->v.x/16;
        physicalObject->GetBody()->v.y = physicalObject->GetBody()->v.y + -physicalObject->GetBody()->v.y/16;
And, in my DeathSystem, if the entity with the Input dies, I switch the player and the enemy (just to exercise the component/entities)
void TDeathSystem::Update(Esc::TComponentPtr component, uint32_t tickDelta)
    THealth *health = static_cast<THealth*>(component);
    if (health->IsDead()) {
        printf("Entity Dead! Swap\n");
        // Let's try and swap some components, healthEntity is the old player entity, to become the new enemy
        Esc::TEntityPtr healthEntity = component->GetOwnerEntity();
        // Find the enemy connected to us via the Collision component
        TCollision *collisionComp = static_cast<TCollision*>(healthEntity->GetComponent("Collision"));
        Esc::TEntityPtrs entities;

        // damageEntity is the old enemy entity, will become the new player entity
        Esc::TEntityPtr damageEntity = entities[0];
        World->SetGroup(healthEntity, "Player", false);
        Esc::TComponentPtr healthComp = component;
        Esc::TComponentPtr inputComp = static_cast<TInput*>(healthEntity->GetComponent("Input"));
        Esc::TComponentPtr damageComp = static_cast<TDamage*>(damageEntity->GetComponent("Damage"));
        Esc::TComponentPtr enemyComp = static_cast<TEnemy*>(damageEntity->GetComponent("Enemy"));
        World->RemoveComponent(healthEntity, healthComp, false);
        World->RemoveComponent(healthEntity, inputComp, false);
        World->RemoveComponent(damageEntity, damageComp, false);
        World->RemoveComponent(damageEntity, enemyComp, false);
        World->AddComponent(damageEntity, healthComp);
        World->AddComponent(damageEntity, inputComp);
        World->AddComponent(healthEntity, damageComp);
        World->AddComponent(healthEntity, enemyComp);
        World->SetGroup(damageEntity, "Player", true);


This is just an example, and not really the best, but I'm just trying to show how you can create systems to act on components/entities, and add and remove components from entities.  This example, and more can be seen in my Escape System on github

In Topic: Simple 2D lighting effects

16 September 2014 - 12:51 PM

Net-Gnome had a nice post in his journal about his lighting system using shadow casting.  I'd advise you to check it out.


He open sourced his code a while back too, so you can check out how he does it as well.

In Topic: need suggestion about games

15 September 2014 - 09:45 AM

No screenshots?  No gameplay video?  Those hand-drawn images do nothing to really show what the game is like.


Because of that, I wouldn't even install the game. 

In Topic: Limit Theory Scripting and Procedural generation

08 September 2014 - 02:32 PM

The LT devlogs are a great read for game developers.  He often gets into details, and it's pretty neat to follow along.  He's much closer to the end than the beginning, but, it wouldn't be a terrible idea to go back through his logs and read some of the things he thinks about while making the game.


It's good stuff.