Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
joncampbell123 committed Jan 2, 2025
1 parent 5d7c5c2 commit 188c8e1
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/gui/sdl_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ namespace WLGUI {
}
};

struct ReferenceCountTracking {
int refcount = 0;
int AddRef(void);
int Release(void); /* which does NOT delete the object when refcount == 0 */
};

static Handle MakeHandle(const HandleType ht,const HandleIndex idx);
static HandleIndex GetHandleIndex(const HandleType ht,const Handle h);
static unsigned int MaskToWidth(DevicePixel m);
Expand Down Expand Up @@ -352,7 +358,7 @@ namespace WLGUI {
DevicePixel ForegroundColor = 0;
DevicePixelDescription ColorDescription; /* init by constructor if base, otherwise UNINITIALIZED */
ColorspaceType Colorspace = ColorspaceType::RGB;
int refcount = 0;
ReferenceCountTracking ref;
Flags Flags;

void (*SetPixel)(Obj &obj,long x,long y,const DevicePixel c) = &SetPixel_stub;
Expand Down Expand Up @@ -386,6 +392,9 @@ namespace WLGUI {
void ConvertLogicalToDeviceCoordinates(long &x,long &y);
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);
};

Handle CreateSDLSurfaceDC(SDL_Surface *surf);
Expand Down Expand Up @@ -488,6 +497,14 @@ namespace WLGUI {
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;
Expand Down Expand Up @@ -530,6 +547,7 @@ namespace WLGUI {
}

Obj::~Obj() {
if (ref.refcount != 0) LOG_MSG("Object release when refcount != 0 (this=%p)",(void*)this);
}

BkMode Obj::SetBkMode(BkMode x) {
Expand Down Expand Up @@ -644,7 +662,14 @@ namespace WLGUI {
BackgroundColor = ColorDescription.t.RGB.Make8(0xFF,0xFF,0xFF);
ForegroundColor = ColorDescription.t.RGB.Make8(0x00,0x00,0x00);

SetPixel = SetPixel_32bpp;
if (ColorDescription.BytesPerPixel == 4)
SetPixel = SetPixel_32bpp;
else if (ColorDescription.BytesPerPixel == 3)
SetPixel = SetPixel_24bpp;
else if (ColorDescription.BytesPerPixel == 2)
SetPixel = SetPixel_16bpp;
else if (ColorDescription.BytesPerPixel == 1)
SetPixel = SetPixel_8bpp;
}

void ObjSDLSurface::ConvertLogicalToDeviceCoordinates(long &x,long &y) {
Expand Down Expand Up @@ -674,6 +699,30 @@ namespace WLGUI {
if (row != NULL) *row = uint32_t(c);
}

void ObjSDLSurface::SetPixel_24bpp(Obj &bobj,long x,long y,const DevicePixel c) {
ObjSDLSurface &obj = reinterpret_cast<ObjSDLSurface&>(bobj);
obj.ConvertLogicalToDeviceCoordinates(x,y);
uint8_t *row = (uint8_t*)obj.GetSurfaceRowPtr(x,y);
if (row != NULL) { /* this is why 24bpp isn't well supported past the late 1990s, it's awkward to draw sometimes */
host_writew(row,uint16_t(c));
host_writeb(row+2,uint8_t(c >> DevicePixel(16)));
}
}

void ObjSDLSurface::SetPixel_16bpp(Obj &bobj,long x,long y,const DevicePixel c) {
ObjSDLSurface &obj = reinterpret_cast<ObjSDLSurface&>(bobj);
obj.ConvertLogicalToDeviceCoordinates(x,y);
uint16_t *row = (uint16_t*)obj.GetSurfaceRowPtr(x,y);
if (row != NULL) *row = uint16_t(c);
}

void ObjSDLSurface::SetPixel_8bpp(Obj &bobj,long x,long y,const DevicePixel c) {
ObjSDLSurface &obj = reinterpret_cast<ObjSDLSurface&>(bobj);
obj.ConvertLogicalToDeviceCoordinates(x,y);
uint8_t *row = (uint8_t*)obj.GetSurfaceRowPtr(x,y);
if (row != NULL) *row = uint8_t(c);
}

Handle CreateSDLSurfaceDC(SDL_Surface *surf) {
const size_t idx = List.AllocateHandleIndex();
if (idx != InvalidHandleIndex) {
Expand Down

0 comments on commit 188c8e1

Please sign in to comment.