diff --git a/bazaar/SDL/SDLCtrl.cpp b/bazaar/SDL/SDLCtrl.cpp index 6c942ea6f..3a294e5a3 100644 --- a/bazaar/SDL/SDLCtrl.cpp +++ b/bazaar/SDL/SDLCtrl.cpp @@ -22,7 +22,7 @@ SDL_Cursor *CursorFromImage(const Image &image) ++i; data[i] = mask[i] = 0; } - const RGBA *rgba = GetPixel(image, col, row); + const RGBA *rgba = &image[row][col]; if (*rgba == Black()) { data[i] |= 0x01; mask[i] |= 0x01; diff --git a/bazaar/SDL/SDLCtrl.h b/bazaar/SDL/SDLCtrl.h index 693360356..6f9872b1a 100644 --- a/bazaar/SDL/SDLCtrl.h +++ b/bazaar/SDL/SDLCtrl.h @@ -3,19 +3,17 @@ #include -inline bool Odd(int val) {return val%2;} - -inline const RGBA *GetPixel(const Image &img, int x, int y) { - return img + x + y*img.GetWidth(); -} +inline bool Odd_(int val) {return val%2;} class SDLSurface { public: SDLSurface(); + SDLSurface(int width, int height, int bpp = 0); SDLSurface(SDL_Surface *_surface, bool _del = false); ~SDLSurface(); void Lock(); void Unlock(); + bool Blit(SDLSurface &surf, Rect source = Null, Rect dest = Null); void DrawImage(Image img, int x, int y, Color transparent); void DrawLine1(int x1, int y1, int x2, int y2, Color color); void DrawLine(int x1, int y1, int x2, int y2, int thick, Color color); @@ -28,9 +26,11 @@ public: void DrawPixel(int x, int y, Uint32 scolor); Color GetPixel(int x, int y); byte *GetPixelPos(int x, int y); - inline int GetWidth() {return surface->w;} - inline int GetHeight(){return surface->h;} Uint32 GetColor(Color color); + int GetBpp() {return surface->format->BitsPerPixel;}; + int GetWidth() {return surface->w;}; + int GetHeight() {return surface->h;}; + const char *GetError() {return SDL_GetError();}; protected: SDL_Surface *surface; @@ -45,9 +45,17 @@ public: SDLCtrl &SetBpp(int _bpp) {bpp = _bpp; return *this;}; SDLCtrl &SetVideoflags(int _vf) {videoflags = _vf; return *this;}; - String GetError() {return strError;}; + String GetError() { + const char *err = SDLSurface::GetError(); + if (*err) + return err; + else + return strError;}; void SetError(String str); void ResetError() {strError = "";}; + bool IsReady() {return surface;}; + int GetWidth() {return surface->w;}; + int GetHeight() {return surface->h;}; protected: virtual void Layout(); @@ -69,7 +77,6 @@ private: virtual void State(int reason); #endif SDL_Cursor *cursor; - String strError; }; diff --git a/bazaar/SDL/SDLSurface.cpp b/bazaar/SDL/SDLSurface.cpp index c788898e1..55ee29379 100644 --- a/bazaar/SDL/SDLSurface.cpp +++ b/bazaar/SDL/SDLSurface.cpp @@ -14,6 +14,13 @@ SDLSurface::SDLSurface(SDL_Surface *_surface, bool _del) { del = _del; } +SDLSurface::SDLSurface(int width, int height, int bpp) { + if (surface = SDL_CreateRGBSurface(0, width, height, bpp, 0x0000FF, 0x00FF00, 0xFF0000, 0)) + del = true; + else + del = false; +} + SDLSurface::~SDLSurface() { if (del) SDL_FreeSurface(surface); @@ -31,22 +38,60 @@ void SDLSurface::Unlock() { SDL_UnlockSurface(surface); } +void ToSDL_Rect(SDL_Rect &sdlrect, Rect &rect) { + sdlrect.x = rect.left; + sdlrect.y = rect.top; + sdlrect.w = rect.GetWidth(); + sdlrect.h = rect.GetHeight(); +} + +bool SDLSurface::Blit(SDLSurface &surf, Rect source, Rect dest) { + SDL_Rect sdlsource, *psdlsource; + SDL_Rect sdldest, *psdldest; + if (!IsNull(source)) { + ToSDL_Rect(sdlsource, source); + psdlsource = &sdlsource; + } else + psdlsource = 0; + if (!IsNull(dest)) { + ToSDL_Rect(sdldest, dest); + psdldest = &sdldest; + } else + psdldest = 0; + + if (0 != SDL_BlitSurface(surf.surface, psdlsource, surface, psdldest)) + return false; + if (0 != SDL_Flip(surface)) + return false; + + return true; +} + inline byte *SDLSurface::GetPixelPos(int x, int y) { + if (x < 0 || y < 0|| x >= surface->w || y >= surface->h) + return 0; return (byte *)surface->pixels + surface->pitch*y + surface->format->BytesPerPixel*x; } void SDLSurface::DrawPixel(int x, int y, Color color) { Uint32 scolor = GetColor(color); - memcpy(GetPixelPos(x, y), &scolor, surface->format->BytesPerPixel); + byte *pixelpos = GetPixelPos(x, y); + if (pixelpos) + memcpy(pixelpos, &scolor, surface->format->BytesPerPixel); } inline void SDLSurface::DrawPixel(int x, int y, Uint32 scolor) { - memcpy(GetPixelPos(x, y), &scolor, surface->format->BytesPerPixel); + byte *pixelpos = GetPixelPos(x, y); + if (pixelpos) + memcpy(pixelpos, &scolor, surface->format->BytesPerPixel); } Color SDLSurface::GetPixel(int x, int y) { Uint32 scolor; - memcpy(&scolor, GetPixelPos(x, y), surface->format->BytesPerPixel); + byte *pixelpos = GetPixelPos(x, y); + if (!pixelpos) + return Null; + memcpy(&scolor, pixelpos, surface->format->BytesPerPixel); Uint8 r, g, b; SDL_GetRGB(scolor, surface->format, &r, &g, &b); return Color(r, g, b); @@ -111,11 +156,10 @@ void SDLSurface::DrawLine(int x0, int y0, int x1, int y1, int thick, Color color } void SDLSurface::DrawRect(int x, int y, int width, int height, int thick, Color color) { - int t = thick/2; - FillRect(x+t, y-t, width-thick, thick, color); - FillRect(x+t, y-t+height, width-thick, thick, color); - FillRect(x-t, y-t, thick, height+thick, color); - FillRect(x-t+width, y-t, thick, height+thick, color); + FillRect(x, y, width, thick, color); + FillRect(x, y+thick, thick, height-2*thick, color); + FillRect(x+width-thick, y+thick, thick, height-2*thick, color); + FillRect(x, y+height-thick, width, thick, color); } void SDLSurface::FillRect(int x, int y, int width, int height, Color color) { @@ -161,16 +205,8 @@ void SDLSurface::DrawCircle1(int x0, int y0, int radius, Color color) { } void SDLSurface::DrawCircle(int x0, int y0, int radius, int thick, Color color) { - if (thick == 1) - DrawCircle1(x0, y0, radius, color); - else { - int max = thick/2; - for (int i = 0; i < max; ++i) { - DrawCircle1(x0, y0, radius + i, color); - if (Odd(i)) - DrawCircle1(x0, y0, radius - i, color); - } - } + for (int i = 0; i < thick; ++i) + DrawCircle1(x0, y0, radius - i, color); } void SDLSurface::FillCircle(int cx, int cy, int radius, Color color) { @@ -191,4 +227,3 @@ void SDLSurface::FillCircle(int cx, int cy, int radius, Color color) { } } } -