pmc/main.cc
changeset 42 83b8151b966d
parent 9 c3fecc82ade6
child 45 7197576fedcf
equal deleted inserted replaced
41:574503cf7bb0 42:83b8151b966d
     1 /**
     1 /**
     2  * test/demos/pmc/main.cc
     2  * test/demos/pmc/main.cc
     3  * Copyright (C) 2008 Markus Broeker
     3  * Copyright (C) 2008 Markus Broeker
     4  */
     4  */
     5 
     5 
       
     6 #include <surface.h>
       
     7 #include <rectangle.h>
     6 #include <cube.h>
     8 #include <cube.h>
     7 #include <SDL/SDL.h>
       
     8 
     9 
     9 unsigned long color;
    10 #include <cstdio>
    10 SDL_Surface *screen;
    11 #include <cstdlib>
    11 
    12 
    12 #ifndef STEP
    13 #ifndef STEP
    13 #define STEP 1
    14 #define STEP 1
    14 #endif
    15 #endif
    15 
    16 
       
    17 #define MAX 3
       
    18 
    16 int main (int argc, char **argv)
    19 int main (int argc, char **argv)
    17 {
    20 {
       
    21     Surface *surface;
       
    22     SDL_Event event;
       
    23     Drawable *d[MAX];
    18 
    24 
    19     unsigned long red, black;
    25     int height;
    20     SDL_Event event;
       
    21 
    26 
    22     if (argc != 2) {
    27     if (argc != 2) {
    23         std::cout << "USAGE: " << argv[0]
    28         fprintf (stderr, "Usage: %s <HEIGHT>\n", argv[0]);
    24             << " height" << std::endl;
    29         height = 250;
    25         exit (0);
    30     } else
    26     }
    31         height = atoi (argv[1]);
    27     int h = atoi (argv[1]);
       
    28 
    32 
    29     if ((h < 10) || (h >= 360))
    33     if ((height < 10) || (height >= 360))
    30         h = 200;
    34         height = 200;
    31 
    35 
    32     Vector p1 (0, 0, 0);
    36     Vector p1 (0, 0);
    33     Vector p2 (0, h, 0);
    37     Vector p2 (height, 0);
    34     Vector p3 (h, 0, 0);
    38     Vector p3 (height, height);
    35     Vector p4 (h, h, 0);
    39     Vector p4 (0, height);
    36 
    40 
    37     SDL_Init (SDL_INIT_VIDEO);
    41     surface = new Surface (1024, 768, 16);
    38     screen = SDL_SetVideoMode (1010, 700, 16, SDL_HWSURFACE);
       
    39     red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
       
    40     black = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
       
    41 
    42 
    42     Cube *c = new Cube (p1, p2, p3, p4, h);
    43     d[0] = new Rectangle (surface, p1, p2, p3, p4);
    43     Cube *d = new Cube (p1, p2, p3, p4, h);
    44     d[1] = new Cube (surface);
    44 
    45     d[2] = new Cube (surface, p1, p2, p3, p4, height);
    45     c->move (Vector (100, 250, 0));
       
    46     d->move (Vector (400, 250, 0));
       
    47 
    46 
    48     bool running = true;
    47     bool running = true;
    49     int x, y, z;
    48     int x, y;
    50 
    49 
    51     x = y = z = 0;
    50     x = y = 0;
       
    51 
       
    52     d[1]->move (Vector (25, 25));
       
    53     d[2]->move (Vector (290, 0));
    52 
    54 
    53     while (running) {
    55     while (running) {
    54         SDL_PollEvent (&event);
    56         SDL_PollEvent (&event);
    55 
    57 
    56         if (event.type == SDL_QUIT)
    58         if (event.type == SDL_QUIT)
    57             running = false;
    59             running = false;
    58 
    60 
    59         if (event.type == SDL_KEYDOWN) {
    61         if (event.type == SDL_KEYDOWN) {
    60             color = black;
    62             surface->setColor (Surface::BLACK);
    61             c->show ();
    63             for (int i = 0; i < MAX; i++)
    62             d->show ();
    64                 d[i]->show ();
    63             color = red;
    65             surface->setColor (Surface::RED);
    64 
    66 
    65             switch (event.key.keysym.sym) {
    67             switch (event.key.keysym.sym) {
    66             case SDLK_ESCAPE:
    68             case SDLK_ESCAPE:
    67                 running = false;
    69                 running = false;
    68                 break;
    70                 break;
    69 
    71 
    70             case SDLK_UP:
    72             case SDLK_UP:
    71                 c->move ((Vector (x, y - STEP, z)));
    73                 for (int i = 0; i < MAX; i++)
    72                 d->move ((Vector (x, y - STEP, z)));
    74                     d[i]->move ((Vector (x, y - STEP)));
    73                 break;
    75                 break;
    74 
    76 
    75             case SDLK_DOWN:
    77             case SDLK_DOWN:
    76                 c->move ((Vector (x, y + STEP, z)));
    78                 for (int i = 0; i < MAX; i++)
    77                 d->move ((Vector (x, y + STEP, z)));
    79                     d[i]->move ((Vector (x, y + STEP)));
    78                 break;
    80                 break;
    79 
    81 
    80             case SDLK_LEFT:
    82             case SDLK_LEFT:
    81                 c->move ((Vector (x - STEP, y, z)));
    83                 for (int i = 0; i < MAX; i++)
    82                 d->move ((Vector (x - STEP, y, z)));
    84                     d[i]->move ((Vector (x - STEP, y)));
    83                 break;
    85                 break;
    84 
    86 
    85             case SDLK_RIGHT:
    87             case SDLK_RIGHT:
    86                 c->move ((Vector (x + STEP, y, z)));
    88                 for (int i = 0; i < MAX; i++)
    87                 d->move ((Vector (x + STEP, y, z)));
    89                     d[i]->move ((Vector (x + STEP, y)));
    88                 break;
    90                 break;
    89             default:
    91             default:
    90                 break;
    92                 break;
    91             }
    93             }
    92 
    94 
    93             c->show ();
    95             for (int i = 0; i < MAX; i++) {
    94             d->show ();
    96                 d[i]->show ();
    95             SDL_Flip (screen);
    97             }
       
    98 
       
    99             surface->flip ();
    96         }
   100         }
    97     }
   101     }
    98 
   102 
    99     delete c;
   103     for (int i = 0; i < MAX; i++)
   100     delete d;
   104         delete d[i];
   101 
   105 
   102     SDL_Quit ();
   106     delete surface;
   103 
   107 
   104     return 0;
   108     return EXIT_SUCCESS;
   105 }
   109 }