ERR TestControllerLoop()
{
    while(flags & CONTINUE)
    {
        //AddForce(ship->physics_object, 0.0, 10.0, ship->physics_object->cog_x, ship->physics_object->cog_y);        

        SDL_Event event;
        while(SDL_PollEvent(&event) != 0)
        {
            if(event.type == SDL_QUIT)
                 flags ^= CONTINUE;
            else if(event.type == SDL_KEYDOWN && event.key.repeat == 0)
                switch(event.key.keysym.sym)
                {
                case SDLK_UP: 
                    flames->sprite->data[CURRENT_LOOP] ^= 1;
                    flags ^= ENGINE;
                    break;
                case SDLK_DOWN:
                    ship->y_speed = 0.0;
                    ship->x_speed = 0.0;
                    break;
                case SDLK_LEFT: ship->a_speed   = -5.0; break;
                case SDLK_RIGHT: ship->a_speed  = 5.0; break;
                case SDLK_r :
                    yoshi->center_x = 220;
                    yoshi->center_y = 120;
                    yoshi->x_speed = 0;
                    yoshi->y_speed = 0;
                    ship->center_x = 520;
                    ship->center_y = 120;
                    ship->x_speed = 0;
                    ship->y_speed = 0;
                case SDLK_SPACE:
                    tmp = CopyEntity(bolt);
                    tmp->center_x = ship->center_x;
                    tmp->center_y = ship->center_y;
                    tmp->angle = ship->angle;
                    tmp->x_speed = RotateOffsetX(0.0, -10.0, ship->angle);
                    tmp->y_speed = RotateOffsetY(0.0, -10.0, ship->angle);
                    InsertValue(tmp, bolts, 0);
                    break;
                }
            else if(event.type == SDL_KEYUP)
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    flames->sprite->data[CURRENT_LOOP] ^= 1;
                    flags ^= ENGINE;
                    break;
                case SDLK_DOWN: break;
                case SDLK_LEFT: ship->a_speed   = 0.0; break;
                case SDLK_RIGHT: ship->a_speed  = 0.0; break;
                case SDLK_SPACE: 
                    break;
                }
        }

        if(flags & UPDATE)
        {
            if(flags & ENGINE)
                AddForce(ship->physics_object, RotateOffsetX(0.0, -20.0, ship->angle), RotateOffsetY(0.0, -20.0, ship->angle),
                        ship->physics_object->cog_x, ship->physics_object->cog_y);

            if(CheckCollision(ship->collision_object, yoshi->collision_object) != 0)
            {
                printf("BOOM\n");
                //flags ^= CONTINUE;
                AddForce(ship->physics_object, yoshi->x_speed * yoshi->physics_object->mass, yoshi->y_speed * yoshi->physics_object->mass,
                        ship->physics_object->cog_x, ship->physics_object->cog_y);
                AddForce(yoshi->physics_object, ship->x_speed * ship->physics_object->mass, ship->y_speed * ship->physics_object->mass,
                        yoshi->physics_object->cog_x, yoshi->physics_object->cog_y);
            }
            Element* el = bolts->start;
            Entity* tmp = NULL;
            int i = 0;
            while(el != NULL)
            {
                tmp = (Entity*)el->value;
                //if(CheckCollision(ship->collision_object, tmp->collision_object) != 0)
                //{
                //    printf("BOOM\n");
                //    flags ^= CONTINUE;
                //}
                if(CheckCollision(yoshi->collision_object, tmp->collision_object) != 0)
                {
                    AddForce(yoshi->physics_object, RotateOffsetX(0.0, -10.0, tmp->angle), RotateOffsetY(0.0, -10.0, tmp->angle),
                            yoshi->physics_object->cog_x, yoshi->physics_object->cog_y);
                    yoshi->sprite->zoom -= 0.5;
                    if(yoshi->sprite->zoom < 0.5)
                        yoshi->sprite->zoom = 0.5;
                    FreeEntity(tmp);
                    FreeElement(bolts, i);
                }
                i++;
                el = el->next;
            }

            UpdateEntity(ship);

            flames->angle = ship->angle;
            flames->center_x = ship->center_x + RotateOffsetX(0.0, ship->sprite->h * ship->sprite->zoom, ship->angle);
            flames->center_y = ship->center_y + RotateOffsetY(0.0, ship->sprite->h * ship->sprite->zoom, ship->angle);
            UpdateEntity(flames);

            UpdateEntity(yoshi);
            yoshi->sprite->zoom      += 0.01;
            yoshi->physics_object->mass = yoshi->sprite->zoom * 100;
            if(yoshi->physics_object->mass < 1)
                yoshi->physics_object->mass = 1;
            yoshi->collision_object->radius = 32*yoshi->sprite->zoom;
            if(yoshi->collision_object->radius < 1)
                yoshi->collision_object->radius = 1;

            for(i = 0; i < bolts->size; i++)
            {
                Entity* tmp = (Entity*)GetValue(bolts, i);
                if(tmp->center_x < -15 || tmp->center_y < -15 || tmp->center_x > 1000 || tmp->center_y > 500)
                {
                    FreeEntity(tmp);
                    FreeElement(bolts, i);
                }
                else
                    UpdateEntity(tmp);
            }
        }

        if(flags & DRAW)
        {
            ClearPMap(visual_debug);
            DrawEntity(ship);
            DrawEntityDebugInfo(visual_debug, ship);
            DrawEntity(flames);
            DrawEntity(yoshi);
            DrawEntityDebugInfo(visual_debug, yoshi);
            int i;
            for(i = 0; i < bolts->size; i++)
            {
                Entity* tmp = (Entity*)GetValue(bolts, i);
                DrawEntity(tmp);
                DrawEntityDebugInfo(visual_debug, tmp);
            }
            DrawPixelMap(visual_debug);
            Render();
        }
    }
    return ExitTestController();
}
Example #2
0
void Group::Generate(IdList<Entity,hEntity> *entity,
                     IdList<Param,hParam> *param)
{
    Vector gn = (SS.GW.projRight).Cross(SS.GW.projUp);
    Vector gp = SS.GW.projRight.Plus(SS.GW.projUp);
    Vector gc = (SS.GW.offset).ScaledBy(-1);
    gn = gn.WithMagnitude(200/SS.GW.scale);
    gp = gp.WithMagnitude(200/SS.GW.scale);
    int a, i;
    switch(type) {
        case DRAWING_3D:
            break;

        case DRAWING_WORKPLANE: {
            Quaternion q;
            if(subtype == WORKPLANE_BY_LINE_SEGMENTS) {
                Vector u = SK.GetEntity(predef.entityB)->VectorGetNum();
                Vector v = SK.GetEntity(predef.entityC)->VectorGetNum();
                u = u.WithMagnitude(1);
                Vector n = u.Cross(v);
                v = (n.Cross(u)).WithMagnitude(1);

                if(predef.swapUV) SWAP(Vector, u, v);
                if(predef.negateU) u = u.ScaledBy(-1);
                if(predef.negateV) v = v.ScaledBy(-1);
                q = Quaternion::From(u, v);
            } else if(subtype == WORKPLANE_BY_POINT_ORTHO) {
                // Already given, numerically.
                q = predef.q;
            } else oops();

            Entity normal;
            memset(&normal, 0, sizeof(normal));
            normal.type = Entity::NORMAL_N_COPY;
            normal.numNormal = q;
            normal.point[0] = h.entity(2);
            normal.group = h;
            normal.h = h.entity(1);
            entity->Add(&normal);

            Entity point;
            memset(&point, 0, sizeof(point));
            point.type = Entity::POINT_N_COPY;
            point.numPoint = SK.GetEntity(predef.origin)->PointGetNum();
            point.group = h;
            point.h = h.entity(2);
            entity->Add(&point);

            Entity wp;
            memset(&wp, 0, sizeof(wp));
            wp.type = Entity::WORKPLANE;
            wp.normal = normal.h;
            wp.point[0] = point.h;
            wp.group = h;
            wp.h = h.entity(0);
            entity->Add(&wp);
            break;
        }

        case EXTRUDE: {
            AddParam(param, h.param(0), gn.x);
            AddParam(param, h.param(1), gn.y);
            AddParam(param, h.param(2), gn.z);
            int ai, af;
            if(subtype == ONE_SIDED) {
                ai = 0; af = 2;
            } else if(subtype == TWO_SIDED) {
                ai = -1; af = 1;
            } else oops();

            // Get some arbitrary point in the sketch, that will be used
            // as a reference when defining top and bottom faces.
            hEntity pt = { 0 };
            for(i = 0; i < entity->n; i++) {
                Entity *e = &(entity->elem[i]);
                if(e->group.v != opA.v) continue;

                if(e->IsPoint()) pt = e->h;

                e->CalculateNumerical(false);
                hEntity he = e->h; e = NULL;
                // As soon as I call CopyEntity, e may become invalid! That
                // adds entities, which may cause a realloc.
                CopyEntity(entity, SK.GetEntity(he), ai, REMAP_BOTTOM,
                    h.param(0), h.param(1), h.param(2),
                    NO_PARAM, NO_PARAM, NO_PARAM, NO_PARAM,
                    true, false);
                CopyEntity(entity, SK.GetEntity(he), af, REMAP_TOP,
                    h.param(0), h.param(1), h.param(2),
                    NO_PARAM, NO_PARAM, NO_PARAM, NO_PARAM,
                    true, false);
                MakeExtrusionLines(entity, he);
            }
            // Remapped versions of that arbitrary point will be used to
            // provide points on the plane faces.
            MakeExtrusionTopBottomFaces(entity, pt);
            break;
        }

        case LATHE: {
            break;
        }

        case TRANSLATE: {
            // The translation vector
            AddParam(param, h.param(0), gp.x);
            AddParam(param, h.param(1), gp.y);
            AddParam(param, h.param(2), gp.z);

            int n = (int)valA, a0 = 0;
            if(subtype == ONE_SIDED && skipFirst) {
                a0++; n++;
            }

            for(a = a0; a < n; a++) {
                for(i = 0; i < entity->n; i++) {
                    Entity *e = &(entity->elem[i]);
                    if(e->group.v != opA.v) continue;

                    e->CalculateNumerical(false);
                    CopyEntity(entity, e,
                        a*2 - (subtype == ONE_SIDED ? 0 : (n-1)),
                        (a == (n - 1)) ? REMAP_LAST : a,
                        h.param(0), h.param(1), h.param(2),
                        NO_PARAM, NO_PARAM, NO_PARAM, NO_PARAM,
                        true, false);
                }
            }
            break;
        }
        case ROTATE: {
            // The center of rotation
            AddParam(param, h.param(0), gc.x);
            AddParam(param, h.param(1), gc.y);
            AddParam(param, h.param(2), gc.z);
            // The rotation quaternion
            AddParam(param, h.param(3), 30*PI/180);
            AddParam(param, h.param(4), gn.x);
            AddParam(param, h.param(5), gn.y);
            AddParam(param, h.param(6), gn.z);

            int n = (int)valA, a0 = 0;
            if(subtype == ONE_SIDED && skipFirst) {
                a0++; n++;
            }

            for(a = a0; a < n; a++) {
                for(i = 0; i < entity->n; i++) {
                    Entity *e = &(entity->elem[i]);
                    if(e->group.v != opA.v) continue;

                    e->CalculateNumerical(false);
                    CopyEntity(entity, e,
                        a*2 - (subtype == ONE_SIDED ? 0 : (n-1)),
                        (a == (n - 1)) ? REMAP_LAST : a,
                        h.param(0), h.param(1), h.param(2),
                        h.param(3), h.param(4), h.param(5), h.param(6),
                        false, true);
                }
            }
            break;
        }
        case IMPORTED:
            // The translation vector
            AddParam(param, h.param(0), gp.x);
            AddParam(param, h.param(1), gp.y);
            AddParam(param, h.param(2), gp.z);
            // The rotation quaternion
            AddParam(param, h.param(3), 1);
            AddParam(param, h.param(4), 0);
            AddParam(param, h.param(5), 0);
            AddParam(param, h.param(6), 0);

            for(i = 0; i < impEntity.n; i++) {
                Entity *ie = &(impEntity.elem[i]);
                CopyEntity(entity, ie, 0, 0,
                    h.param(0), h.param(1), h.param(2),
                    h.param(3), h.param(4), h.param(5), h.param(6),
                    false, false);
            }
            break;

        default: oops();
    }
}