mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-21 06:45:39 -06:00
SDL: Now painting outside is not error + more changes
git-svn-id: svn://ultimatepp.org/upp/trunk@2863 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
3e5b69043e
commit
85420c618a
3 changed files with 71 additions and 29 deletions
|
|
@ -22,7 +22,7 @@ SDL_Cursor *CursorFromImage(const Image &image)
|
||||||
++i;
|
++i;
|
||||||
data[i] = mask[i] = 0;
|
data[i] = mask[i] = 0;
|
||||||
}
|
}
|
||||||
const RGBA *rgba = GetPixel(image, col, row);
|
const RGBA *rgba = &image[row][col];
|
||||||
if (*rgba == Black()) {
|
if (*rgba == Black()) {
|
||||||
data[i] |= 0x01;
|
data[i] |= 0x01;
|
||||||
mask[i] |= 0x01;
|
mask[i] |= 0x01;
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,17 @@
|
||||||
|
|
||||||
#include <SDL/SDLWrapper.h>
|
#include <SDL/SDLWrapper.h>
|
||||||
|
|
||||||
inline bool Odd(int val) {return val%2;}
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
class SDLSurface {
|
class SDLSurface {
|
||||||
public:
|
public:
|
||||||
SDLSurface();
|
SDLSurface();
|
||||||
|
SDLSurface(int width, int height, int bpp = 0);
|
||||||
SDLSurface(SDL_Surface *_surface, bool _del = false);
|
SDLSurface(SDL_Surface *_surface, bool _del = false);
|
||||||
~SDLSurface();
|
~SDLSurface();
|
||||||
void Lock();
|
void Lock();
|
||||||
void Unlock();
|
void Unlock();
|
||||||
|
bool Blit(SDLSurface &surf, Rect source = Null, Rect dest = Null);
|
||||||
void DrawImage(Image img, int x, int y, Color transparent);
|
void DrawImage(Image img, int x, int y, Color transparent);
|
||||||
void DrawLine1(int x1, int y1, int x2, int y2, Color color);
|
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);
|
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);
|
void DrawPixel(int x, int y, Uint32 scolor);
|
||||||
Color GetPixel(int x, int y);
|
Color GetPixel(int x, int y);
|
||||||
byte *GetPixelPos(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);
|
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:
|
protected:
|
||||||
SDL_Surface *surface;
|
SDL_Surface *surface;
|
||||||
|
|
@ -45,9 +45,17 @@ public:
|
||||||
|
|
||||||
SDLCtrl &SetBpp(int _bpp) {bpp = _bpp; return *this;};
|
SDLCtrl &SetBpp(int _bpp) {bpp = _bpp; return *this;};
|
||||||
SDLCtrl &SetVideoflags(int _vf) {videoflags = _vf; 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 SetError(String str);
|
||||||
void ResetError() {strError = "";};
|
void ResetError() {strError = "";};
|
||||||
|
bool IsReady() {return surface;};
|
||||||
|
int GetWidth() {return surface->w;};
|
||||||
|
int GetHeight() {return surface->h;};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void Layout();
|
virtual void Layout();
|
||||||
|
|
@ -69,7 +77,6 @@ private:
|
||||||
virtual void State(int reason);
|
virtual void State(int reason);
|
||||||
#endif
|
#endif
|
||||||
SDL_Cursor *cursor;
|
SDL_Cursor *cursor;
|
||||||
|
|
||||||
String strError;
|
String strError;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,13 @@ SDLSurface::SDLSurface(SDL_Surface *_surface, bool _del) {
|
||||||
del = _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() {
|
SDLSurface::~SDLSurface() {
|
||||||
if (del)
|
if (del)
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
|
|
@ -31,22 +38,60 @@ void SDLSurface::Unlock() {
|
||||||
SDL_UnlockSurface(surface);
|
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) {
|
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;
|
return (byte *)surface->pixels + surface->pitch*y + surface->format->BytesPerPixel*x;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDLSurface::DrawPixel(int x, int y, Color color) {
|
void SDLSurface::DrawPixel(int x, int y, Color color) {
|
||||||
Uint32 scolor = GetColor(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) {
|
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) {
|
Color SDLSurface::GetPixel(int x, int y) {
|
||||||
Uint32 scolor;
|
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;
|
Uint8 r, g, b;
|
||||||
SDL_GetRGB(scolor, surface->format, &r, &g, &b);
|
SDL_GetRGB(scolor, surface->format, &r, &g, &b);
|
||||||
return Color(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) {
|
void SDLSurface::DrawRect(int x, int y, int width, int height, int thick, Color color) {
|
||||||
int t = thick/2;
|
FillRect(x, y, width, thick, color);
|
||||||
FillRect(x+t, y-t, width-thick, thick, color);
|
FillRect(x, y+thick, thick, height-2*thick, color);
|
||||||
FillRect(x+t, y-t+height, width-thick, thick, color);
|
FillRect(x+width-thick, y+thick, thick, height-2*thick, color);
|
||||||
FillRect(x-t, y-t, thick, height+thick, color);
|
FillRect(x, y+height-thick, width, thick, color);
|
||||||
FillRect(x-t+width, y-t, thick, height+thick, color);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDLSurface::FillRect(int x, int y, int width, int height, Color 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) {
|
void SDLSurface::DrawCircle(int x0, int y0, int radius, int thick, Color color) {
|
||||||
if (thick == 1)
|
for (int i = 0; i < thick; ++i)
|
||||||
DrawCircle1(x0, y0, radius, color);
|
DrawCircle1(x0, y0, radius - i, 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDLSurface::FillCircle(int cx, int cy, int radius, Color 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) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue