pmc/main.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Mon, 07 Jun 2010 23:20:47 +0200
changeset 132 54a04f5f141d
parent 129 d82830a449a9
permissions -rw-r--r--
Adjusted to show side effects and their handling When dealing with static buffers, successive calls point to the same memory location and overwrite formers ones... committer: Markus Bröker <mbroeker@largo.homelinux.org>

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

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

#include <cstdio>
#include <cstdlib>

#ifndef STEP
#define STEP 1
#endif

#define MAX 1

using namespace algebra;

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

    int height;

    surface = new Surface (0, 0, 32);

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

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

    SDL_WM_SetCaption ("Pimp my Cube", NULL);

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

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

    x = y = 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;

            case SDLK_RETURN:
                fprintf (stderr, "Objects remaining: %d\n", surface->getInstances ());
                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;

    /*
     * the remaining objects will be removed on exit
     */
    fprintf (stderr, "Objects remaining on exit: %d\n", refCounter);

    return EXIT_SUCCESS;
}