# HG changeset patch # User Markus Bröker # Date 1239878952 -7200 # Node ID 4b9e1ac4024666bc901ef782ed46a896ca4f5d7c # Parent 7197576fedcf878b6a6fe38788ccdb9c44ba4fbe pmc: Screen Depth Fix: Paint on 8, 16 and 32 bpp * a crocked rectangle and cube * new color settings and better oow protection committer: Markus Bröker diff --git a/pmc/cube.cc b/pmc/cube.cc --- a/pmc/cube.cc +++ b/pmc/cube.cc @@ -78,11 +78,6 @@ void Cube::show () { - /* - * Implement some Sanity Checks - */ - int height = surface->getHeight (); - surface->drawLine (P[0]->X (), P[0]->Y (), P[1]->X (), P[1]->Y ()); surface->drawLine (P[3]->X (), P[3]->Y (), P[2]->X (), P[2]->Y ()); surface->drawLine (P[2]->X (), P[2]->Y (), P[1]->X (), P[1]->Y ()); diff --git a/pmc/include/surface.h b/pmc/include/surface.h --- a/pmc/include/surface.h +++ b/pmc/include/surface.h @@ -14,12 +14,13 @@ int width; int height; int depth; + int bpp; - Uint32 color, red, black; + Uint32 color, red, green, blue, black; SDL_Surface *screen; public: - enum foregroundColor { BLACK, RED }; + enum foregroundColor { BLACK=1, RED, GREEN, BLUE }; Surface (int w, int h, int d); ~Surface (); diff --git a/pmc/main.cc b/pmc/main.cc --- a/pmc/main.cc +++ b/pmc/main.cc @@ -37,10 +37,11 @@ Vector p1 (0, 0); Vector p2 (height, 0); - Vector p3 (height, height); - Vector p4 (0, height); + Vector p3 (height + 100, height + 100); + Vector p4 (100, height + 100); - surface = new Surface (1024, 768, 16); + surface = new Surface (1024, 768, 32); + SDL_WM_SetCaption ("Pimp my Cube", NULL); d[0] = new Rectangle (surface, p1, p2, p3, p4); d[1] = new Cube (surface); @@ -51,7 +52,7 @@ x = y = 0; - d[1]->move (Vector (25, 25)); + d[1]->move (Vector (3 * height, 25)); d[2]->move (Vector (290, 0)); while (running) { @@ -64,7 +65,7 @@ surface->setColor (Surface::BLACK); for (i = 0; i < MAX; i++) d[i]->show (); - surface->setColor (Surface::RED); + surface->setColor (Surface::GREEN); switch (event.key.keysym.sym) { case SDLK_ESCAPE: diff --git a/pmc/rectangle.cc b/pmc/rectangle.cc --- a/pmc/rectangle.cc +++ b/pmc/rectangle.cc @@ -63,9 +63,9 @@ if ((anker.Y () + location.Y ()) < 0) return; - if ((anker.X () + location.X ()) >= surface->getWidth ()) + if ((anker.X () + location.X ()) > surface->getWidth ()) return; - if ((anker.Y () + location.Y ()) >= surface->getHeight ()) + if ((anker.Y () + location.Y ()) > surface->getHeight ()) return; for (int i = 0; i < 4; i++) { diff --git a/pmc/surface.cc b/pmc/surface.cc --- 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; }; }