diff --git a/src/gui/sdl_gui.cpp b/src/gui/sdl_gui.cpp index 2d5a48cae..76032bf5b 100644 --- a/src/gui/sdl_gui.cpp +++ b/src/gui/sdl_gui.cpp @@ -238,1058 +238,6 @@ extern uint8_t int10_font_08[256 * 8]; extern uint8_t int10_font_14[256 * 14]; extern uint8_t int10_font_16[256 * 16]; -/* Windows-Like GUI toolkit (to better emulate the look and feel of Windows 3.1) */ -namespace WLGUI { - - typedef size_t Handle; - typedef size_t HandleIndex; - typedef uint32_t DevicePixel;/*pixel value, in target device format i.e. 16bpp rrrrrggggggbbbbb*/ - - static constexpr uint32_t Mask24 = uint32_t(0xFFFFFFu); - static constexpr Handle InvalidHandleValue = ~Handle(0u); - static constexpr HandleIndex InvalidHandleIndex = ~HandleIndex(0u); - - enum class HandleType { - NoType=0, /* FIXME: Why can't I use "None", GCC? Are you reserving identifiers for future Python support or something? */ - DC=1, - FontHandle=2 - }; - - struct DevicePixelDescription { - struct mask { - DevicePixel r,g,b,a; - } mask; - struct shift { - uint8_t r,g,b,a; - } shift; - struct width { - uint8_t r,g,b,a; - } width; - - uint8_t BitsPerPixel; - uint8_t BytesPerPixel; /* 0 if less than 8, use BitsPerPixel */ - - DevicePixel Make8(const unsigned int rv,const unsigned int gv,const unsigned int bv,const unsigned int av=0xFF) const; - }; - - struct Dimensions { - unsigned int w,h; - - Dimensions() { } - Dimensions(const unsigned int _w,const unsigned int _h) : w(_w), h(_h) { } - }; - - struct Point { - long x,y; - - Point() { } - Point(const long _x,const long _y) : x(_x), y(_y) { } - }; - - struct Rect { - long left,top,right,bottom; - - Rect() { } - Rect(const long _l,const long _t,const long _r,const long _b) : left(_l), top(_t), right(_r), bottom(_b) { } - bool isInRect(const long x,const long y) const; - bool isEmpty(void) const; - }; - - struct ReferenceCountTracking { - int refcount = 0; - int AddRef(void); - int Release(void); /* which does NOT delete the object when refcount == 0 */ - }; - - struct Resource { - ReferenceCountTracking ref; /* init by count tracking class */ - HandleType htype; /* init by Resource constructor */ - - Resource(const HandleType init_ht); - virtual ~Resource(); - }; - - class ResourceList { - public: - HandleIndex ListAlloc = 0; - std::vector List; - Resource *Get(const HandleIndex i); - bool Set(const HandleIndex i,Resource *p); - bool Delete(const HandleIndex i); - size_t Size(void) const; - HandleIndex AllocateHandleIndex(void); - }; - - ResourceList Resources; - - static Handle MakeHandle(const HandleType ht,const HandleIndex idx); - static HandleIndex GetHandleIndex(const HandleType ht,const Handle h); - static unsigned int MaskToWidth(DevicePixel m); - static unsigned int Pixel8ToWidth(const unsigned int v,const unsigned int width); - - namespace FontHandle { - - enum class ObjType { - Base=0, /* you shouldn't use this */ - VGAFont=1 - }; - - struct Bitmap { - uint8_t bpp = 1; /* 1bpp (mono) or 8bpp (grayscale) */ - uint16_t pitch = 0,height = 0; - const unsigned char* base = NULL; - uint16_t sx = 0,sy = 0; /* source pixels to draw */ - uint16_t dw = 0,dh = 0; /* dimensions of pixels to draw */ - int16_t dx = 0,dy = 0; /* dest pixels offset from origin to draw */ - int16_t advancex256 = 0; /* advance x in 1/256th of a pixel */ - uint16_t cw = 0; /* calculation width, for centering and such */ - }; - - struct Obj : public Resource { - struct Flags { - static constexpr uint32_t Antialiased = uint32_t(1u) << uint32_t(0u); /* make anti-aliased TrueType where possible */ - static constexpr uint32_t TrueType = uint32_t(1u) << uint32_t(1u); /* font is TrueType */ - static constexpr uint32_t FixedPitch = uint32_t(1u) << uint32_t(2u); /* font is fixed pitch */ - uint32_t v = 0; - }; - - Flags Flags; - ObjType type; /* init by constructor */ - int16_t totalHeight = 0; /* font cell (top to bottom) */ - int16_t ascentY = 0; /* height from baseline upward */ - int16_t internalLeading = 0; - int16_t externalLeading = 0; - - Obj(const ObjType t); - virtual ~Obj(); - - virtual signed int GlyphLookup(int32_t uc); - - static constexpr unsigned int GCF_BITMAP = 1u << 0u; - virtual bool GetChar(unsigned int glyph,Bitmap &bmp,unsigned int flags=0); - }; - - /* VGA font */ - struct ObjVGAFont : public Obj { - const unsigned char* font = (const unsigned char*)NULL; - unsigned char* copy = NULL; /* if you ask for larger sizes */ - uint16_t fontheight = 0; - uint16_t scale = 1; - - ObjVGAFont(const unsigned int height); - virtual ~ObjVGAFont(); - - /* TODO: Callback function to convert unicode -> CP437 */ - virtual signed int GlyphLookup(int32_t uc) override; - virtual bool GetChar(unsigned int glyph,Bitmap &bmp,unsigned int flags) override; - }; - - Handle CreateVGAFont(const unsigned int height); - int GetHeight(const Handle h); - int GetAscent(const Handle h); - bool Destroy(Handle h); - - } - - namespace DC { - - enum class ObjType { - Base=0, /* you shouldn't use this */ - SDLSurface=1 - }; - - /* A bit of polymorphism because like a real Windows DC it can be a display, a printer, etc. - * In this toolkit, it can be a SDL surface, OpenGL texture, etc. Unlike Windows there's - * no need to worry about partial redraw because this toolkit takes the DWM approach where - * every window is a surface, texture, etc. - * - * In most cases you should GetDC and ReleaseDC to draw on your window just like real Windows. - * Don't keep the DC open except to draw. */ - struct Obj : public Resource { - struct Flags { - static constexpr uint32_t DC_CLIPRECT = uint32_t(1u) << uint32_t(0u); - uint32_t v = 0; - }; - - ObjType type; /* init by constructor */ - Rect clip = {-1,-1,-1,-1}; - Dimensions viewport = {0,0}; /* the viewport in device pixels i.e. SDL surface pixels */ - Point originSrc = {0,0}; /* coordinate system origin */ - Point originDst = {0,0}; /* coordinate system origin */ - DevicePixel BackgroundColor = 0; - DevicePixel ForegroundColor = 0; - DevicePixelDescription ColorDescription; /* init by constructor if base, otherwise UNINITIALIZED */ - ReferenceCountTracking ref; - Handle CurrentFont = InvalidHandleValue; - Flags Flags; - - DevicePixel (*GetPixel)(Obj &obj,long x,long y) = &GetPixel_stub; - void (*SetPixel)(Obj &obj,long x,long y,const DevicePixel c) = &SetPixel_stub; - - Obj(const ObjType t); - virtual ~Obj(); - virtual DevicePixel MakeRGB8(const unsigned int r,const unsigned int g,const unsigned int b,const unsigned int a=0xFF); - virtual DevicePixel SetBackgroundColor(const DevicePixel c); - virtual DevicePixel SetForegroundColor(const DevicePixel c); - virtual bool SetLogicalOrigin(const long x=0,const long y=0,Point *po=NULL); - virtual bool SetDeviceOrigin(const long x=0,const long y=0,Point *po=NULL); - virtual void ConvertLogicalToDeviceCoordinates(long &x,long &y); - virtual Handle SelectFont(Handle newValue); - virtual bool TextOut(long x,long y,const char *str/*TODO UTF-8*/); - virtual bool DrawTextChar1bpp(long x,long y,FontHandle::Bitmap &bmp); - virtual bool SetClipRegion(const Rect &r); - bool DrawTextChar(long x,long y,FontHandle::Bitmap &bmp); - virtual bool clipCheck(const long x,const long y); - - static DevicePixel GetPixel_stub(Obj &obj,long x,long y); - static void SetPixel_stub(Obj &obj,long x,long y,const DevicePixel c); - }; - - /* SDL surface DC */ - struct ObjSDLSurface : public Obj { - SDL_Surface* surface = NULL; - Point viewport_origin = {0,0}; /* in case we do subregions of a surface as "window objects" */ - - ObjSDLSurface(SDL_Surface *surf); - virtual ~ObjSDLSurface(); - - virtual void ConvertLogicalToDeviceCoordinates(long &x,long &y) override; - - void initFromSurface(void); - void *GetSurfaceRowPtr(long x,long y); - - static void SetPixel_32bpp(Obj &bobj,long x,long y,const DevicePixel c); - static void SetPixel_24bpp(Obj &bobj,long x,long y,const DevicePixel c); - static void SetPixel_16bpp(Obj &bobj,long x,long y,const DevicePixel c); - static void SetPixel_8bpp(Obj &bobj,long x,long y,const DevicePixel c); - - static DevicePixel GetPixel_32bpp(Obj &bobj,long x,long y); - static DevicePixel GetPixel_24bpp(Obj &bobj,long x,long y); - static DevicePixel GetPixel_16bpp(Obj &bobj,long x,long y); - static DevicePixel GetPixel_8bpp(Obj &bobj,long x,long y); - }; - - Handle CreateSDLSurfaceDC(SDL_Surface *surf); - Obj* GetObject(const Handle h); - DevicePixel MakeRGB8(const Handle h,const unsigned int r,const unsigned int g,const unsigned int b,const unsigned int a=0xFF); - void SetPixel(const Handle h,const long x,const long y,const DevicePixel c); - bool GetDevicePixelFormat(const Handle h,DevicePixelDescription &d); - DevicePixel SetBackgroundColor(const Handle h,const DevicePixel c); - DevicePixel SetForegroundColor(const Handle h,const DevicePixel c); - bool SetLogicalOrigin(const Handle h,const long x=0,const long y=0,Point *po=NULL); - bool SetDeviceOrigin(const Handle h,const long x=0,const long y=0,Point *po=NULL); - bool Delete(const Handle h); - Handle SelectFont(const Handle DC,const Handle newValue); - bool TextOut(const Handle h,long x,long y,const char *str/*TODO UTF-8*/); - bool SetClipRegion(const Handle h,const Rect &r); - - } - -} - -namespace WLGUI { - - static Handle MakeHandle(const HandleType ht,const HandleIndex idx) { - return ((Handle)ht << (Handle)24u) + Handle(idx & 0xFFFFFFu); - } - - static HandleType GetHandleType(const Handle h) { - return HandleType(h >> (Handle)24u); - } - - static HandleIndex GetHandleIndex(const HandleType ht,const Handle h) { - if (ht == HandleType(h >> (Handle)24u)) - return HandleIndex(h & 0xFFFFFFu); - else - return InvalidHandleIndex; - } - - static unsigned int MaskToWidth(DevicePixel m) { - if (m != DevicePixel(0)) { - unsigned int count = 0; - while ((m & DevicePixel(1)) == DevicePixel(0)) m >>= DevicePixel(1); - while (m) { m >>= DevicePixel(1); count++; } - return count; - } - - return 0; - } - - static unsigned int Pixel8ToWidth(const unsigned int v,const unsigned int width) { - if (width != 0) { - const unsigned int mv = (1u << width) - 1u; - return ((v * mv) + 128u) / 255u; - } - - return 0; - } - - bool Rect::isEmpty(void) const { - return left == -1l && left == right && right == top && top == bottom; - } - - bool Rect::isInRect(const long x,const long y) const { - return (x >= left && x < right) && (y >= top && y < bottom); - } - - Resource::Resource(const HandleType init_ht) : htype(init_ht) { - } - - Resource::~Resource() { - if (ref.refcount > 0) LOG_MSG("Object release when refcount > 0 (this=%p type=%u ref=%d)",(void*)this,(unsigned int)htype,ref.refcount); - else if (ref.refcount < 0) LOG_MSG("Object released too much, refcount < 0 (this=%p type=%u ref=%d)",(void*)this,(unsigned int)htype,ref.refcount); - } - - DevicePixel DevicePixelDescription::Make8(const unsigned int rv,const unsigned int gv,const unsigned int bv,const unsigned int av) const { - return (DevicePixel(Pixel8ToWidth(rv,width.r)) << DevicePixel(shift.r)) + - (DevicePixel(Pixel8ToWidth(gv,width.g)) << DevicePixel(shift.g)) + - (DevicePixel(Pixel8ToWidth(bv,width.b)) << DevicePixel(shift.b)) + - (DevicePixel(Pixel8ToWidth(av,width.a)) << DevicePixel(shift.a)); - } - - Resource *ResourceList::Get(const HandleIndex i) { - if (i < HandleIndex(List.size())) return List[i]; - return NULL; - } - - bool ResourceList::Set(const HandleIndex i,Resource *p) { - if (i < HandleIndex(List.size())) { - List[i] = p; - return true; - } - - return false; - } - - bool ResourceList::Delete(const HandleIndex i) { - if (i < HandleIndex(List.size())) { - if (List[i] != NULL) { - delete List[i]; - List[i] = NULL; - ListAlloc = i; - return true; - } - } - - return false; - } - - size_t ResourceList::Size(void) const { - return List.size(); - } - - int ReferenceCountTracking::AddRef(void) { - return ++refcount; - } - - int ReferenceCountTracking::Release(void) { - return --refcount; - } - - HandleIndex ResourceList::AllocateHandleIndex(void) { - /* scan forward from ListAlloc */ - const HandleIndex pListAlloc = ListAlloc; - while (ListAlloc < List.size()) { - const HandleIndex index = ListAlloc++; - if (List[index] == NULL) return index; - } - - ListAlloc = 0; /* No opening, scan again but only up to where the first loop started scanning */ - while (ListAlloc < List.size() && ListAlloc <= pListAlloc) { - const HandleIndex index = ListAlloc++; - if (List[index] == NULL) return index; - } - - /* Well then, enlarge the list */ - { - const size_t osz = List.size(); - if (osz >= 1024u) return InvalidHandleIndex; /* but not too much! */ - ListAlloc = HandleIndex(osz); /* neither of the first two found anything, scan from where we extend the list */ - List.resize(osz + (osz / 4u) + 64u); - for (size_t i=osz;i < List.size();i++) List[i] = NULL; - } - - /* one more time */ - while (ListAlloc < List.size()) { - const HandleIndex index = ListAlloc++; - if (List[index] == NULL) return index; - } - - return InvalidHandleIndex; - } - - namespace FontHandle { - - Obj::Obj(const ObjType t) : Resource(HandleType::FontHandle), type(t) { - } - - Obj::~Obj() { - } - - signed int Obj::GlyphLookup(int32_t uc) { - (void)uc; - return -1; - } - - bool Obj::GetChar(unsigned int glyph,Bitmap &bmp,unsigned int flags) { - (void)glyph; - (void)bmp; - (void)flags; - return false; - } - - ///////////////// - - ObjVGAFont::ObjVGAFont(const unsigned int height) : Obj(ObjType::VGAFont) { - scale = 1; while ((height/scale) >= 24) scale++; - - Flags.v |= Flags::FixedPitch; - if (height >= 16) { - font = int10_font_16; - fontheight = 16; - } - else if (height >= 14) { - font = int10_font_14; - fontheight = 14; - } - else { - font = int10_font_08; - fontheight = 8; - } - - if (scale > 1) { - copy = new unsigned char[fontheight*256*scale*scale]; /* expand in both dimensions */ - memset(copy,0,fontheight*256*scale*scale); - for (unsigned int y=0;y < (fontheight*256);y++) { - unsigned char *d = copy + (y*scale*scale),*ld = d; - const unsigned char *s = font + y; - - memset(d,0,scale); - d += scale; - - unsigned char smsk = 0x80,dmsk = 0x80; - for (unsigned int x=0;x < 8;x++) { - for (unsigned int c=0;c < scale;c++) { - if (*s & smsk) *ld |= dmsk; - - if ((dmsk >>= 1u) == 0) { - dmsk = 0x80; - ld++; - } - } - - smsk >>= 1u; - } - - for (unsigned int c=1;c < scale;c++) { - memcpy(d,d-scale,scale); - d += scale; - } - } - - fontheight *= scale; - font = copy; - } - - totalHeight = fontheight; - ascentY = fontheight - (2 * scale); - } - - ObjVGAFont::~ObjVGAFont() { - if (copy) delete[] copy; - } - - signed int ObjVGAFont::GlyphLookup(int32_t uc) { - /* TODO: Map unicode to CP437 since that is what the stock VGA font uses */ - if (uc >= 0 && uc <= 255) - return (int)uc; - - return -1; - } - - bool ObjVGAFont::GetChar(unsigned int glyph,Bitmap &bmp,unsigned int flags) { - if (glyph < 256) { - bmp = Bitmap(); - bmp.bpp = 1; - bmp.pitch = 1 * scale; - bmp.height = fontheight; - bmp.base = font + (glyph * fontheight * bmp.pitch); - bmp.sx = 0; - bmp.sy = 0; - bmp.dw = 8 * scale; - bmp.dh = fontheight; - bmp.dx = 0; - bmp.dy = 0; - bmp.advancex256 = (8u * scale) << 8u; - bmp.cw = 8; - (void)flags; - return true; - } - - return false; - } - - ////////////////// - - /* for internal use only */ - Obj* GetObject(const Handle h) { - const HandleIndex idx = GetHandleIndex(HandleType::FontHandle,h); - return (Obj*)Resources.Get(idx); - } - - Handle CreateVGAFont(const unsigned int height) { - const size_t idx = Resources.AllocateHandleIndex(); - if (idx != InvalidHandleIndex) { - Resources.Set(idx,(Obj*)(new ObjVGAFont(height))); - return MakeHandle(HandleType::FontHandle,HandleIndex(idx)); - } - - return InvalidHandleValue; - } - - int GetHeight(const Handle h) { - Obj* obj = GetObject(h); - if (obj) { return obj->totalHeight; } - return 0; - } - - int GetAscent(const Handle h) { - Obj* obj = GetObject(h); - if (obj) { return obj->ascentY; } - return 0; - } - - bool Destroy(Handle h) { - const HandleIndex idx = GetHandleIndex(HandleType::FontHandle,h); - if (idx != InvalidHandleIndex) return Resources.Delete(idx); - return false; - } - } - - namespace DC { - - Obj::Obj(const ObjType t) : Resource(HandleType::DC), type(t) { - } - - Obj::~Obj() { - } - - DevicePixel Obj::MakeRGB8(const unsigned int r,const unsigned int g,const unsigned int b,const unsigned int a) { - /* you must override this if your colorspace is not RGB (but this toolkit will be used where RGB is always used) */ - return ColorDescription.Make8(r,g,b,a); - } - - DevicePixel Obj::SetBackgroundColor(const DevicePixel c) { - const DevicePixel prev = BackgroundColor; - BackgroundColor = c; - return prev; - } - - DevicePixel Obj::SetForegroundColor(const DevicePixel c) { - const DevicePixel prev = ForegroundColor; - ForegroundColor = c; - return prev; - } - - bool Obj::SetLogicalOrigin(const long x,const long y,Point *po) { - if (po) *po = originSrc; - originSrc = Point(x,y); - return true; - } - - bool Obj::SetDeviceOrigin(const long x,const long y,Point *po) { - if (po) *po = originDst; - originDst = Point(x,y); - return true; - } - - void Obj::ConvertLogicalToDeviceCoordinates(long &x,long &y) { - x += originSrc.x; - y += originSrc.y; - x += originDst.x; - y += originDst.y; - } - - Handle Obj::SelectFont(Handle newValue) { - if (GetHandleType(newValue) == HandleType::FontHandle) { - Handle pv = CurrentFont; - CurrentFont = newValue; - return pv; - } - else if (newValue == InvalidHandleValue) { - CurrentFont = newValue; - } - - return InvalidHandleValue; - } - - bool Obj::SetClipRegion(const Rect &r) { - if (!r.isEmpty()) { - Flags.v |= Flags::DC_CLIPRECT; - clip = r; - } - else { - Flags.v &= ~Flags::DC_CLIPRECT; - } - - return true; - } - - /* NTS: Override this method if you have a faster more optimized routine for 1bpp bitmap font rendering */ - bool Obj::DrawTextChar1bpp(long x,long y,FontHandle::Bitmap &bmp) { - long dx = x + bmp.dx; - for (unsigned int subx=0;subx < bmp.dw;subx++) { - const unsigned int bsx = bmp.sx + subx; - unsigned char msk = 0x80 >> (bsx & 7u); - long dy = y + bmp.dy; - const unsigned char *s = - bmp.base + - (bmp.sy * bmp.pitch) + - (bsx >> 3u); - - for (unsigned int suby=0;suby < bmp.dh;suby++) { - if ((*s & msk) && clipCheck(dx,dy)) SetPixel(*this,dx,dy,ForegroundColor); - s += bmp.pitch; - dy++; - } - - dx++; - } - - return true; - } - - bool Obj::clipCheck(const long x,const long y) { - if (Flags.v & Flags::DC_CLIPRECT) - return clip.isInRect(x,y); - - return true; - } - - bool Obj::DrawTextChar(long x,long y,FontHandle::Bitmap &bmp) { - if (bmp.base != NULL) { - if (bmp.bpp == 1) - return DrawTextChar1bpp(x,y,bmp); - } - - return false; - } - - bool Obj::TextOut(long x,long y,const char *str) { - FontHandle::Obj *fh = (FontHandle::Obj*)Resources.Get(GetHandleIndex(HandleType::FontHandle,CurrentFont)); - - if (fh) { - long fx = x << 8l; - int32_t c; - - while ((c=(unsigned char)(*str++)/*TODO: Read UTF-8 char*/) != 0) { - int glyph = fh->GlyphLookup(c); - if (glyph >= 0) { - FontHandle::Bitmap bmp; - if (fh->GetChar((unsigned int)glyph,bmp)) { - DrawTextChar(fx >> 8l,y,bmp); - fx += bmp.advancex256; - } - } - } - - return true; - } - - return false; - } - - DevicePixel Obj::GetPixel_stub(Obj &obj,long x,long y) { - (void)obj; - (void)x; - (void)y; - return DevicePixel(0); - } - - void Obj::SetPixel_stub(Obj &obj,long x,long y,const DevicePixel c) { - (void)obj; - (void)x; - (void)y; - (void)c; - } - - /////////////////////////// - - ObjSDLSurface::ObjSDLSurface(SDL_Surface *surf) : Obj(ObjType::SDLSurface), surface(surf) { - initFromSurface(); - } - - ObjSDLSurface::~ObjSDLSurface() { - } - - void ObjSDLSurface::initFromSurface(void) { - viewport.w = abs(surface->w); - viewport.h = abs(surface->h); - ColorDescription.BitsPerPixel = surface->format->BitsPerPixel; - ColorDescription.BytesPerPixel = surface->format->BytesPerPixel; - - ColorDescription.mask.r = surface->format->Rmask; - ColorDescription.shift.r = surface->format->Rshift; - ColorDescription.width.r = MaskToWidth(surface->format->Rmask); - - ColorDescription.mask.g = surface->format->Gmask; - ColorDescription.shift.g = surface->format->Gshift; - ColorDescription.width.g = MaskToWidth(surface->format->Gmask); - - ColorDescription.mask.b = surface->format->Bmask; - ColorDescription.shift.b = surface->format->Bshift; - ColorDescription.width.b = MaskToWidth(surface->format->Bmask); - - ColorDescription.mask.a = surface->format->Amask; - ColorDescription.shift.a = surface->format->Ashift; - ColorDescription.width.a = MaskToWidth(surface->format->Amask); - - BackgroundColor = ColorDescription.Make8(0xFF,0xFF,0xFF); - ForegroundColor = ColorDescription.Make8(0x00,0x00,0x00); - - if (ColorDescription.BytesPerPixel == 4) { - GetPixel = GetPixel_32bpp; - SetPixel = SetPixel_32bpp; - } - else if (ColorDescription.BytesPerPixel == 3) { - GetPixel = GetPixel_24bpp; - SetPixel = SetPixel_24bpp; - } - else if (ColorDescription.BytesPerPixel == 2) { - GetPixel = GetPixel_16bpp; - SetPixel = SetPixel_16bpp; - } - else if (ColorDescription.BytesPerPixel == 1) { - GetPixel = GetPixel_8bpp; - SetPixel = SetPixel_8bpp; - } - } - - void ObjSDLSurface::ConvertLogicalToDeviceCoordinates(long &x,long &y) { - Obj::ConvertLogicalToDeviceCoordinates(x,y); - x += viewport_origin.x; - y += viewport_origin.y; - } - - /* WARNING: This is not suitable for surfaces less than 8bpp if x != 0 however SDL doesn't support those either */ - void *ObjSDLSurface::GetSurfaceRowPtr(long x,long y) { - /* We trust the viewport has not been corrupted to extend outside the SDL surface! */ - if (x >= 0l && x < (long)viewport.w && y >= 0l && y < (long)viewport.h) { - unsigned char *p = (unsigned char*)(surface->pixels); - if (p == NULL) return NULL; - return - p + - ((unsigned int)surface->pitch * (unsigned int)y) + - ((unsigned int)ColorDescription.BytesPerPixel * (unsigned int)x); - } - return NULL; - } - - void ObjSDLSurface::SetPixel_32bpp(Obj &bobj,long x,long y,const DevicePixel c) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint32_t *row = (uint32_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) *row = uint32_t(c); - } - - DevicePixel ObjSDLSurface::GetPixel_32bpp(Obj &bobj,long x,long y) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint32_t *row = (uint32_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) return DevicePixel(*row); - return DevicePixel(0); - } - - void ObjSDLSurface::SetPixel_24bpp(Obj &bobj,long x,long y,const DevicePixel c) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint8_t *row = (uint8_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) *row = (uint32_t(c) & Mask24) + ((*row) & (~Mask24)); - } - - DevicePixel ObjSDLSurface::GetPixel_24bpp(Obj &bobj,long x,long y) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint32_t *row = (uint32_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) return DevicePixel(*row & Mask24); - return DevicePixel(0); - } - - void ObjSDLSurface::SetPixel_16bpp(Obj &bobj,long x,long y,const DevicePixel c) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint16_t *row = (uint16_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) *row = uint16_t(c); - } - - DevicePixel ObjSDLSurface::GetPixel_16bpp(Obj &bobj,long x,long y) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint16_t *row = (uint16_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) return DevicePixel(*row); - return DevicePixel(0); - } - - void ObjSDLSurface::SetPixel_8bpp(Obj &bobj,long x,long y,const DevicePixel c) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint8_t *row = (uint8_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) *row = uint8_t(c); - } - - DevicePixel ObjSDLSurface::GetPixel_8bpp(Obj &bobj,long x,long y) { - ObjSDLSurface &obj = reinterpret_cast(bobj); - uint8_t *row = (uint8_t*)obj.GetSurfaceRowPtr(x,y); - if (row != NULL) return DevicePixel(*row); - return DevicePixel(0); - } - - Handle CreateSDLSurfaceDC(SDL_Surface *surf) { - const size_t idx = Resources.AllocateHandleIndex(); - if (idx != InvalidHandleIndex) { - Resources.Set(idx,(Obj*)(new ObjSDLSurface(surf))); - return MakeHandle(HandleType::DC,HandleIndex(idx)); - } - - return InvalidHandleValue; - } - - /* for internal use only */ - Obj* GetObject(const Handle h) { - const HandleIndex idx = GetHandleIndex(HandleType::DC,h); - return (Obj*)Resources.Get(idx); - } - - DevicePixel MakeRGB8(const Handle h,const unsigned int r,const unsigned int g,const unsigned int b,const unsigned int a) { - Obj* obj = GetObject(h); - if (obj) return obj->ColorDescription.Make8(r,g,b,a); - return DevicePixel(0); - } - - DevicePixel GetPixel(const Handle h,long x,long y) { - Obj* obj = GetObject(h); - if (obj) { - obj->ConvertLogicalToDeviceCoordinates(x,y); - if (obj->clipCheck(x,y)) return obj->GetPixel(*obj,x,y); /* NTS: call through function pointer */ - } - return DevicePixel(0); - } - - void SetPixel(const Handle h,long x,long y,const DevicePixel c) { - Obj* obj = GetObject(h); - if (obj) { - obj->ConvertLogicalToDeviceCoordinates(x,y); - if (obj->clipCheck(x,y)) obj->SetPixel(*obj,x,y,c); /* NTS: call through function pointer */ - } - } - - bool GetDevicePixelFormat(const Handle h,DevicePixelDescription &d) { - Obj* obj = GetObject(h); - if (obj) { d = obj->ColorDescription; return true; } - return false; - } - - DevicePixel SetBackgroundColor(const Handle h,const DevicePixel c) { - Obj* obj = GetObject(h); - if (obj) return obj->SetBackgroundColor(c); - return DevicePixel(0); - } - - DevicePixel SetForegroundColor(const Handle h,const DevicePixel c) { - Obj* obj = GetObject(h); - if (obj) return obj->SetForegroundColor(c); - return DevicePixel(0); - } - - bool SetLogicalOrigin(const Handle h,const long x,const long y,Point *po) { - Obj* obj = GetObject(h); - if (obj) return obj->SetLogicalOrigin(x,y,po); - return false; - } - - bool SetDeviceOrigin(const Handle h,const long x,const long y,Point *po) { - Obj* obj = GetObject(h); - if (obj) return obj->SetDeviceOrigin(x,y,po); - return false; - } - - bool Delete(const Handle h) { - const HandleIndex idx = GetHandleIndex(HandleType::DC,h); - if (idx != InvalidHandleIndex) return Resources.Delete(idx); - return false; - } - - Handle SelectFont(const Handle h,const Handle newValue) { - Obj* obj = GetObject(h); - if (obj) return obj->SelectFont(newValue); - return InvalidHandleValue; - } - - bool TextOut(const Handle h,long x,long y,const char *str/*TODO UTF-8*/) { - Obj* obj = GetObject(h); - if (obj) { - obj->ConvertLogicalToDeviceCoordinates(x,y); - return obj->TextOut(x,y,str); - } - return false; - } - - bool SetClipRegion(const Handle h,const Rect &r) { - Obj* obj = GetObject(h); - if (obj) { - if (!r.isEmpty()) { - Rect cr = r; - obj->ConvertLogicalToDeviceCoordinates(cr.left,cr.top); - obj->ConvertLogicalToDeviceCoordinates(cr.right,cr.bottom); - return obj->SetClipRegion(cr); - } - else { - return obj->SetClipRegion(r); - } - } - return false; - } - - } - -} - -void NewUIExperiment(bool pressed) { - if (!pressed) return; - - GFX_EndUpdate(nullptr); - GFX_SetTitle(-1,-1,-1,true); - KEYBOARD_ClrBuffer();//Clear buffer - GFX_LosingFocus();//Release any keys pressed (buffer gets filled again). (could be in above if, but clearing the mapper input when exiting the mapper is sensible as well - SDL_Delay(20); - -#if defined(C_SDL2) -#elif defined(C_HX_DOS) -#else - int dw,dh; - void UpdateWindowDimensions(void); - UpdateWindowDimensions(); - dw = (int)currentWindowWidth; - dh = (int)currentWindowHeight; - - if (dw < 32) dw = 32; - if (dh < 32) dh = 32; - - SDL_Event event; - - LOG_MSG("NewGUI %d x %d",dw,dh); - SDL_Surface *gui_surface = SDL_SetVideoMode(dw,dh,32,0); - if (gui_surface == NULL) E_Exit("Could not initialize video mode for GUI: %s",SDL_GetError()); - SDL_FillRect(gui_surface, nullptr, 0); - - WLGUI::Handle gui_surface_dc = WLGUI::DC::CreateSDLSurfaceDC(gui_surface); - if (gui_surface_dc == WLGUI::InvalidHandleValue) E_Exit("Cannot create SDL DC"); - - WLGUI::DC::SetClipRegion(gui_surface_dc,WLGUI::Rect(dw/4,dh/4,(dw*3)/4,(dh*3)/4)); - - WLGUI::Handle VGAFont = WLGUI::FontHandle::CreateVGAFont(16*2); - if (VGAFont == WLGUI::InvalidHandleValue) E_Exit("Cannot create VGA font"); - - WLGUI::DC::SelectFont(gui_surface_dc,VGAFont); - WLGUI::DC::SetForegroundColor(gui_surface_dc,WLGUI::DC::MakeRGB8(gui_surface_dc,0xFF,0xFF,0xFF)); - WLGUI::DC::SetBackgroundColor(gui_surface_dc,WLGUI::DC::MakeRGB8(gui_surface_dc,0x00,0x00,0xFF)); - - WLGUI::DC::TextOut(gui_surface_dc,0,0,"Hello,"); - WLGUI::DC::TextOut(gui_surface_dc,0,WLGUI::FontHandle::GetHeight(VGAFont),"World!"); - - for (long x=-100;x < 100;x++) { - WLGUI::DC::SetPixel(gui_surface_dc,x, x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,x+128,x+128)); - WLGUI::DC::SetPixel(gui_surface_dc,x+10,x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,0, 0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+20,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, x+128,0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+30,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, 0, x+128)); - } - - SDL_UpdateRect(gui_surface, 0, 0, 0, 0); - while (SDL_PollEvent(&event)); - SDL_Delay(1000); - SDL_FillRect(gui_surface, nullptr, 0); - - WLGUI::DC::SetLogicalOrigin(gui_surface_dc,100,0); - for (long x=-100;x < 100;x++) { - WLGUI::DC::SetPixel(gui_surface_dc,x, x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,x+128,x+128)); - WLGUI::DC::SetPixel(gui_surface_dc,x+10,x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,0, 0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+20,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, x+128,0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+30,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, 0, x+128)); - } - WLGUI::DC::SetLogicalOrigin(gui_surface_dc,0,100); - for (long x=-100;x < 100;x++) { - WLGUI::DC::SetPixel(gui_surface_dc,x, x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,x+128,x+128)); - WLGUI::DC::SetPixel(gui_surface_dc,x+10,x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,0, 0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+20,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, x+128,0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+30,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, 0, x+128)); - } - WLGUI::DC::SetLogicalOrigin(gui_surface_dc); - for (long x=-100;x < 100;x++) { - WLGUI::DC::SetPixel(gui_surface_dc,x, x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,x+128,x+128)); - WLGUI::DC::SetPixel(gui_surface_dc,x+10,x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,0, 0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+20,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, x+128,0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+30,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, 0, x+128)); - } - - WLGUI::DC::SetDeviceOrigin(gui_surface_dc,200,0); - for (long x=-100;x < 100;x++) { - WLGUI::DC::SetPixel(gui_surface_dc,x, x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,x+128,x+128)); - WLGUI::DC::SetPixel(gui_surface_dc,x+10,x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,0, 0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+20,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, x+128,0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+30,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, 0, x+128)); - } - WLGUI::DC::SetDeviceOrigin(gui_surface_dc,0,200); - for (long x=-100;x < 100;x++) { - WLGUI::DC::SetPixel(gui_surface_dc,x, x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,x+128,x+128)); - WLGUI::DC::SetPixel(gui_surface_dc,x+10,x,WLGUI::DC::MakeRGB8(gui_surface_dc,x+128,0, 0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+20,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, x+128,0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+30,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, 0, x+128)); - } - WLGUI::DC::SetDeviceOrigin(gui_surface_dc); - - SDL_UpdateRect(gui_surface, 0, 0, 0, 0); - while (SDL_PollEvent(&event)); - SDL_Delay(1000); - SDL_FillRect(gui_surface, nullptr, 0); - - WLGUI::DC::SetDeviceOrigin(gui_surface_dc,dw/2,dh/2); - - for (long x=-4000;x < 4000;x++) { - long cx = ((x+4000l)*255l)/8000l; - WLGUI::DC::SetPixel(gui_surface_dc,x, x,WLGUI::DC::MakeRGB8(gui_surface_dc,cx,cx,cx)); - WLGUI::DC::SetPixel(gui_surface_dc,x+100,x,WLGUI::DC::MakeRGB8(gui_surface_dc,cx,0, 0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+200,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, cx,0 )); - WLGUI::DC::SetPixel(gui_surface_dc,x+300,x,WLGUI::DC::MakeRGB8(gui_surface_dc,0, 0, cx)); - } - - WLGUI::DC::TextOut(gui_surface_dc,0,0,"Hello!"); - WLGUI::DC::TextOut(gui_surface_dc,1000,1000,"Hello!"); - - WLGUI::DC::SetLogicalOrigin(gui_surface_dc,0,0); - WLGUI::DC::SetDeviceOrigin(gui_surface_dc,0,0); - - SDL_UpdateRect(gui_surface, 0, 0, 0, 0); - while (SDL_PollEvent(&event)); - SDL_Delay(1000); - - { - const unsigned int sw = dw/4,sh = dh/4; - const unsigned int sx = dw - sw,sy = dh - sh; - for (unsigned int y=0;y < sx;y++) { - for (unsigned int x=0;x < sy;x++) { - WLGUI::DC::SetPixel(gui_surface_dc,x,y, - WLGUI::DC::GetPixel(gui_surface_dc,sx+(x/4),sy+(y/4))); - } - } - } - - SDL_UpdateRect(gui_surface, 0, 0, 0, 0); - while (SDL_PollEvent(&event)); - SDL_Delay(1000); - SDL_FillRect(gui_surface, nullptr, 0); - - if (!WLGUI::DC::Delete(gui_surface_dc)) E_Exit("Cannot delete SDL DC"); - if (!WLGUI::FontHandle::Destroy(VGAFont)) E_Exit("Cannot destroy font"); -#endif - - GFX_Stop(); - if (sdl.draw.callback) (sdl.draw.callback)( GFX_CallBackReset ); - GFX_Start(); -} - extern bool toscale; extern const char* RunningProgram; static GUI::ScreenSDL *UI_Startup(GUI::ScreenSDL *screen) { diff --git a/src/gui/sdlmain.cpp b/src/gui/sdlmain.cpp index 34081f8c0..c78ff3208 100644 --- a/src/gui/sdlmain.cpp +++ b/src/gui/sdlmain.cpp @@ -3346,8 +3346,6 @@ void Sendkeymapper(bool pressed) { bool has_GUI_StartUp = false; -void NewUIExperiment(bool pressed); - static void GUI_StartUp() { DOSBoxMenu::item *item; @@ -3964,9 +3962,6 @@ static void GUI_StartUp() { SetWindowTransparency(section->Get_int("transparency")); UpdateWindowDimensions(); ApplyPreventCap(); - - /* Experiment -- You're not supposed to play with this yet hence why no binding is assigned by default */ - MAPPER_AddHandler(NewUIExperiment, MK_nothing, 0, "newuitest", "New UI test", &item); } void Mouse_AutoLock(bool enable) {