pmc/main.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 47 63adb261de90
parent 46 4b9e1ac40246
child 54 c064ce9f40f5
permissions -rw-r--r--
tree is a list :P * anyway, some changes here -> a struct T consumes sizeof(struct T) of memory -> actual wasn't really needed here. thrown away. committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/pmc/main.cc
 * Copyright (C) 2008 Markus Broeker
 */

#include <surface.h>
#include <rectangle.h>
#include <cube.h>

#include <cstdio>
#include <cstdlib>

#ifndef STEP
#define STEP 1
#endif

#define MAX 3

using namespace algebra;

int main (int argc, char **argv)
{
    Surface *surface;
    SDL_Event event;
    Drawable *d[MAX];

    int height;

    if (argc != 2) {
        fprintf (stderr, "Usage: %s <HEIGHT>\n", argv[0]);
        height = 250;
    } else
        height = atoi (argv[1]);

    if ((height < 10) || (height >= 360))
        height = 200;

    Vector p1 (0, 0);
    Vector p2 (height, 0);
    Vector p3 (height + 100, height + 100);
    Vector p4 (100, height + 100);

    surface = new Surface (1024, 768, 32);
    SDL_WM_SetCaption ("Pimp my Cube", NULL);

    d[0] = new Rectangle (surface, p1, p2, p3, p4);
    d[1] = new Cube (surface);
    d[2] = new Cube (surface, p1, p2, p3, p4, height);

    bool running = true;
    int i, x, y;

    x = y = 0;

    d[1]->move (Vector (3 * height, 25));
    d[2]->move (Vector (290, 0));

    while (running) {
        SDL_PollEvent (&event);

        if (event.type == SDL_QUIT)
            running = false;

        if (event.type == SDL_KEYDOWN) {
            surface->setColor (Surface::BLACK);
            for (i = 0; i < MAX; i++)
                d[i]->show ();
            surface->setColor (Surface::GREEN);

            switch (event.key.keysym.sym) {
            case SDLK_ESCAPE:
                running = false;
                break;

            case SDLK_UP:
                for (i = 0; i < MAX; i++)
                    d[i]->move ((Vector (x, y - STEP)));
                break;

            case SDLK_DOWN:
                for (i = 0; i < MAX; i++)
                    d[i]->move ((Vector (x, y + STEP)));
                break;

            case SDLK_LEFT:
                for (i = 0; i < MAX; i++)
                    d[i]->move ((Vector (x - STEP, y)));
                break;

            case SDLK_RIGHT:
                for (i = 0; i < MAX; i++)
                    d[i]->move ((Vector (x + STEP, y)));
                break;
            default:
                break;
            }

            for (i = 0; i < MAX; i++) {
                d[i]->show ();
            }

            surface->flip ();
        }
    }

    for (i = 0; i < MAX; i++)
        delete d[i];

    delete surface;

    return EXIT_SUCCESS;
}