pmc/main.cc
changeset 42 83b8151b966d
parent 9 c3fecc82ade6
child 45 7197576fedcf
--- a/pmc/main.cc
+++ b/pmc/main.cc
@@ -3,52 +3,54 @@
  * Copyright (C) 2008 Markus Broeker
  */
 
+#include <surface.h>
+#include <rectangle.h>
 #include <cube.h>
-#include <SDL/SDL.h>
 
-unsigned long color;
-SDL_Surface *screen;
+#include <cstdio>
+#include <cstdlib>
 
 #ifndef STEP
 #define STEP 1
 #endif
 
+#define MAX 3
+
 int main (int argc, char **argv)
 {
+    Surface *surface;
+    SDL_Event event;
+    Drawable *d[MAX];
 
-    unsigned long red, black;
-    SDL_Event event;
+    int height;
 
     if (argc != 2) {
-        std::cout << "USAGE: " << argv[0]
-            << " height" << std::endl;
-        exit (0);
-    }
-    int h = atoi (argv[1]);
+        fprintf (stderr, "Usage: %s <HEIGHT>\n", argv[0]);
+        height = 250;
+    } else
+        height = atoi (argv[1]);
 
-    if ((h < 10) || (h >= 360))
-        h = 200;
+    if ((height < 10) || (height >= 360))
+        height = 200;
 
-    Vector p1 (0, 0, 0);
-    Vector p2 (0, h, 0);
-    Vector p3 (h, 0, 0);
-    Vector p4 (h, h, 0);
+    Vector p1 (0, 0);
+    Vector p2 (height, 0);
+    Vector p3 (height, height);
+    Vector p4 (0, height);
 
-    SDL_Init (SDL_INIT_VIDEO);
-    screen = SDL_SetVideoMode (1010, 700, 16, SDL_HWSURFACE);
-    red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
-    black = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
+    surface = new Surface (1024, 768, 16);
 
-    Cube *c = new Cube (p1, p2, p3, p4, h);
-    Cube *d = new Cube (p1, p2, p3, p4, h);
-
-    c->move (Vector (100, 250, 0));
-    d->move (Vector (400, 250, 0));
+    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 x, y, z;
+    int x, y;
 
-    x = y = z = 0;
+    x = y = 0;
+
+    d[1]->move (Vector (25, 25));
+    d[2]->move (Vector (290, 0));
 
     while (running) {
         SDL_PollEvent (&event);
@@ -57,10 +59,10 @@
             running = false;
 
         if (event.type == SDL_KEYDOWN) {
-            color = black;
-            c->show ();
-            d->show ();
-            color = red;
+            surface->setColor (Surface::BLACK);
+            for (int i = 0; i < MAX; i++)
+                d[i]->show ();
+            surface->setColor (Surface::RED);
 
             switch (event.key.keysym.sym) {
             case SDLK_ESCAPE:
@@ -68,38 +70,40 @@
                 break;
 
             case SDLK_UP:
-                c->move ((Vector (x, y - STEP, z)));
-                d->move ((Vector (x, y - STEP, z)));
+                for (int i = 0; i < MAX; i++)
+                    d[i]->move ((Vector (x, y - STEP)));
                 break;
 
             case SDLK_DOWN:
-                c->move ((Vector (x, y + STEP, z)));
-                d->move ((Vector (x, y + STEP, z)));
+                for (int i = 0; i < MAX; i++)
+                    d[i]->move ((Vector (x, y + STEP)));
                 break;
 
             case SDLK_LEFT:
-                c->move ((Vector (x - STEP, y, z)));
-                d->move ((Vector (x - STEP, y, z)));
+                for (int i = 0; i < MAX; i++)
+                    d[i]->move ((Vector (x - STEP, y)));
                 break;
 
             case SDLK_RIGHT:
-                c->move ((Vector (x + STEP, y, z)));
-                d->move ((Vector (x + STEP, y, z)));
+                for (int i = 0; i < MAX; i++)
+                    d[i]->move ((Vector (x + STEP, y)));
                 break;
             default:
                 break;
             }
 
-            c->show ();
-            d->show ();
-            SDL_Flip (screen);
+            for (int i = 0; i < MAX; i++) {
+                d[i]->show ();
+            }
+
+            surface->flip ();
         }
     }
 
-    delete c;
-    delete d;
+    for (int i = 0; i < MAX; i++)
+        delete d[i];
 
-    SDL_Quit ();
+    delete surface;
 
-    return 0;
+    return EXIT_SUCCESS;
 }