pmc/surface.cc
changeset 46 4b9e1ac40246
parent 42 83b8151b966d
child 54 c064ce9f40f5
equal deleted inserted replaced
45:7197576fedcf 46:4b9e1ac40246
    17     depth = d;
    17     depth = d;
    18 
    18 
    19     SDL_Init (SDL_INIT_VIDEO);
    19     SDL_Init (SDL_INIT_VIDEO);
    20     screen = SDL_SetVideoMode (width, height, depth, SDL_HWSURFACE);
    20     screen = SDL_SetVideoMode (width, height, depth, SDL_HWSURFACE);
    21     red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
    21     red = SDL_MapRGB (screen->format, 0xff, 0x00, 0x00);
       
    22     green = SDL_MapRGB (screen->format, 0x00, 0xff, 0x00);
       
    23     blue = SDL_MapRGB (screen->format, 0x00, 0x00, 0xff);
    22     black = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
    24     black = SDL_MapRGB (screen->format, 0x00, 0x00, 0x00);
    23 
    25 
    24     color = red;
    26     color = red;
       
    27     bpp = screen->format->BytesPerPixel;
    25 }
    28 }
    26 
    29 
    27 Surface::~Surface ()
    30 Surface::~Surface ()
    28 {
    31 {
    29     SDL_Quit ();
    32     SDL_Quit ();
    30 }
    33 }
    31 
    34 
    32 void Surface::drawPixel (int x, int y)
    35 void Surface::drawPixel (int x, int y)
    33 {
    36 {
       
    37     if (x < 0 || y < 0)
       
    38         return;
       
    39 
       
    40     if (x > width || y > height)
       
    41         return;
       
    42 
    34     if (SDL_MUSTLOCK (screen)) {
    43     if (SDL_MUSTLOCK (screen)) {
    35         if (SDL_LockSurface (screen) < 0) {
    44         if (SDL_LockSurface (screen) < 0) {
    36             return;
    45             return;
    37         }
    46         }
    38     }
    47     }
    39 
    48 
    40     switch (screen->format->BytesPerPixel) {
    49     switch (bpp) {
    41     case 1:{                   /* vermutlich 8 Bit */
    50     case 1:{                   /* vermutlich 8 Bit */
    42             Uint8 *bufp;
    51             Uint8 *bufp;
    43 
    52 
    44             bufp = (Uint8 *) screen->pixels + y * screen->pitch + x;
    53             bufp = (Uint8 *) screen->pixels + y * screen->pitch + x;
    45             *bufp = color;
    54             *bufp = color;
    94     int sx;
   103     int sx;
    95     int sy;
   104     int sy;
    96     int dx;
   105     int dx;
    97     int dy;
   106     int dy;
    98 
   107 
    99     Uint8 *lineAddr;
       
   100     Sint32 yOffset;
       
   101 
       
   102     /*
       
   103      * SANITY CHECK: fix segfault in *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
       
   104      */
       
   105     if (y1 < 0 || y2 < 0)
       
   106         return;
       
   107 
       
   108     if (x1 < 0 || x2 < 0)
       
   109         return;
       
   110 
       
   111     dx = x2 - x1;
   108     dx = x2 - x1;
   112     ax = abs (dx) << 1;
   109     ax = abs (dx) << 1;
   113     sx = sign (dx);
   110     sx = sign (dx);
   114 
   111 
   115     dy = y2 - y1;
   112     dy = y2 - y1;
   116     ay = abs (dy) << 1;
   113     ay = abs (dy) << 1;
   117     sy = sign (dy);
   114     sy = sign (dy);
   118     yOffset = sy * screen->pitch;
       
   119 
   115 
   120     x = x1;
   116     x = x1;
   121     y = y1;
   117     y = y1;
   122 
   118 
   123     lineAddr = ((Uint8 *) screen->pixels) + (y * screen->pitch);
       
   124     if (ax > ay) {              /* x dominant */
   119     if (ax > ay) {              /* x dominant */
   125         d = ay - (ax >> 1);
   120         d = ay - (ax >> 1);
   126         for (;;) {
   121         for (;;) {
   127             *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
   122             drawPixel (x, y);
   128 
   123 
   129             if (x == x2) {
   124             if (x == x2) {
   130                 return;
   125                 return;
   131             }
   126             }
   132             if (d >= 0) {
   127             if (d >= 0) {
   133                 y += sy;
   128                 y += sy;
   134                 lineAddr += yOffset;
       
   135                 d -= ax;
   129                 d -= ax;
   136             }
   130             }
   137             x += sx;
   131             x += sx;
   138             d += ay;
   132             d += ay;
   139         }
   133         }
   140     } else {                    /* y dominant */
   134     } else {                    /* y dominant */
   141         d = ax - (ay >> 1);
   135         d = ax - (ay >> 1);
   142         for (;;) {
   136         for (;;) {
   143             *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
   137             drawPixel (x, y);
   144 
   138 
   145             if (y == y2) {
   139             if (y == y2) {
   146                 return;
   140                 return;
   147             }
   141             }
   148             if (d >= 0) {
   142             if (d >= 0) {
   149                 x += sx;
   143                 x += sx;
   150                 d -= ay;
   144                 d -= ay;
   151             }
   145             }
   152             y += sy;
   146             y += sy;
   153             lineAddr += yOffset;
       
   154             d += ax;
   147             d += ax;
   155         }
   148         }
   156     }
   149     }
   157 }
   150 }
   158 
   151 
   168         color = black;
   161         color = black;
   169         break;
   162         break;
   170     case RED:
   163     case RED:
   171         color = red;
   164         color = red;
   172         break;
   165         break;
       
   166     case GREEN:
       
   167         color = green;
       
   168         break;
       
   169     case BLUE:
       
   170         color = blue;
       
   171         break;
       
   172     default:
       
   173         color = red;
   173     };
   174     };
   174 }
   175 }