pmc/surface.cc
changeset 46 4b9e1ac40246
parent 42 83b8151b966d
child 54 c064ce9f40f5
--- a/pmc/surface.cc
+++ b/pmc/surface.cc
@@ -19,9 +19,12 @@
     SDL_Init (SDL_INIT_VIDEO);
     screen = SDL_SetVideoMode (width, height, depth, SDL_HWSURFACE);
     red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
+    green = SDL_MapRGB (screen->format, 0x00, 0xff, 0x00);
+    blue = SDL_MapRGB (screen->format, 0x00, 0x00, 0xff);
     black = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
 
     color = red;
+    bpp = screen->format->BytesPerPixel;
 }
 
 Surface::~Surface ()
@@ -31,13 +34,19 @@
 
 void Surface::drawPixel (int x, int y)
 {
+    if (x < 0 || y < 0)
+        return;
+
+    if (x > width || y > height)
+        return;
+
     if (SDL_MUSTLOCK (screen)) {
         if (SDL_LockSurface (screen) < 0) {
             return;
         }
     }
 
-    switch (screen->format->BytesPerPixel) {
+    switch (bpp) {
     case 1:{                   /* vermutlich 8 Bit */
             Uint8 *bufp;
 
@@ -96,18 +105,6 @@
     int dx;
     int dy;
 
-    Uint8 *lineAddr;
-    Sint32 yOffset;
-
-    /*
-     * SANITY CHECK: fix segfault in *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
-     */
-    if (y1 < 0 || y2 < 0)
-        return;
-
-    if (x1 < 0 || x2 < 0)
-        return;
-
     dx = x2 - x1;
     ax = abs (dx) << 1;
     sx = sign (dx);
@@ -115,23 +112,20 @@
     dy = y2 - y1;
     ay = abs (dy) << 1;
     sy = sign (dy);
-    yOffset = sy * screen->pitch;
 
     x = x1;
     y = y1;
 
-    lineAddr = ((Uint8 *) screen->pixels) + (y * screen->pitch);
     if (ax > ay) {              /* x dominant */
         d = ay - (ax >> 1);
         for (;;) {
-            *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
+            drawPixel (x, y);
 
             if (x == x2) {
                 return;
             }
             if (d >= 0) {
                 y += sy;
-                lineAddr += yOffset;
                 d -= ax;
             }
             x += sx;
@@ -140,7 +134,7 @@
     } else {                    /* y dominant */
         d = ax - (ay >> 1);
         for (;;) {
-            *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
+            drawPixel (x, y);
 
             if (y == y2) {
                 return;
@@ -150,7 +144,6 @@
                 d -= ay;
             }
             y += sy;
-            lineAddr += yOffset;
             d += ax;
         }
     }
@@ -170,5 +163,13 @@
     case RED:
         color = red;
         break;
+    case GREEN:
+        color = green;
+        break;
+    case BLUE:
+        color = blue;
+        break;
+    default:
+        color = red;
     };
 }