Working on proper content rendering.

This commit is contained in:
Scott Duensing 2022-06-17 19:58:36 -05:00
parent 3c51c33430
commit 003a0c0ba8
5 changed files with 228 additions and 193 deletions

View file

@ -19,38 +19,39 @@ static void surfacePixelSet8(uint16_t x, uint16_t y, ColorT color);
static void surfacePixelSet16(uint16_t x, uint16_t y, ColorT color); static void surfacePixelSet16(uint16_t x, uint16_t y, ColorT color);
static void surfacePixelSet32(uint16_t x, uint16_t y, ColorT color); static void surfacePixelSet32(uint16_t x, uint16_t y, ColorT color);
#include "gui.h"
void surfaceBlit(int16_t targetX, int16_t targetY, SurfaceT *source) { void surfaceBlit(int16_t targetX1, int16_t targetY1, int16_t width, int16_t height, SurfaceT *source) {
int16_t y; int16_t y;
int16_t x1 = 0; int16_t x1 = 0;
int16_t y1 = 0; int16_t y1 = 0;
int16_t x2 = source->width;
int16_t y2 = source->height;
size_t bytes; size_t bytes;
size_t offsetTarget; size_t offsetTarget;
size_t offsetSource; size_t offsetSource;
// Did they provide a valid width/height?
if (width <= 0 || width > source->width) width = source->width;
if (height <= 0 || height > source->height) height = source->height;
// Clip on top and left. x1 & y1 are pixel locations inside the source bitmap. // Clip on top and left. x1 & y1 are pixel locations inside the source bitmap.
if (targetX < 0) x1 = -targetX; if (targetX1 < 0) x1 = -targetX1;
if (targetY < 0) y1 = -targetY; if (targetY1 < 0) y1 = -targetY1;
// Clip on right and bottom. // Clip on right and bottom.
if (targetX + x2 > __surfaceActive->width) x2 -= targetX + x2 - __surfaceActive->width; if (targetX1 + width > __surfaceActive->width) width -= targetX1 + width - __surfaceActive->width;
if (targetY + y2 > __surfaceActive->height) y2 -= targetY + y2 - __surfaceActive->height; if (targetY1 + height > __surfaceActive->height) height -= targetY1 + height - __surfaceActive->height;
// Are we still on the screen? // Are we still on the screen?
if (x1 < 0 || y1 < 0 || x2 < x1 || y2 < y1) return; if (x1 < 0 || y1 < 0 || width < x1 || height < y1) return;
if (targetX == 0 && targetY == 0 && __surfaceActive->width == source->width && __surfaceActive->height == source->height) { if (targetX1 == 0 && targetY1 == 0 && __surfaceActive->width == source->width && __surfaceActive->height == source->height) {
// Direct blit of entire surface. // Direct blit of entire surface.
memcpy(__surfaceActive->buffer.bits8, source->buffer.bits8, source->bytes); memcpy(__surfaceActive->buffer.bits8, source->buffer.bits8, source->bytes);
} else { } else {
// Blit into larger surface. // Blit into larger surface.
offsetTarget = (targetY + y1) * __surfaceActive->scanline + (targetX + x1) * __surfaceBytesPerPixel; offsetTarget = (targetY1 + y1) * __surfaceActive->scanline + (targetX1 + x1) * __surfaceBytesPerPixel;
offsetSource = y1 * source->scanline + x1 * __surfaceBytesPerPixel; offsetSource = y1 * source->scanline + x1 * __surfaceBytesPerPixel;
bytes = (x2 - x1) * __surfaceBytesPerPixel; bytes = (width - x1) * __surfaceBytesPerPixel;
for (y=y1; y<y2; y++) { for (y=y1; y<height; y++) {
memcpy(&__surfaceActive->buffer.bits8[offsetTarget], &source->buffer.bits8[offsetSource], bytes); memcpy(&__surfaceActive->buffer.bits8[offsetTarget], &source->buffer.bits8[offsetSource], bytes);
offsetTarget += __surfaceActive->scanline; offsetTarget += __surfaceActive->scanline;
offsetSource += source->scanline; offsetSource += source->scanline;

View file

@ -45,7 +45,7 @@ extern ColorT (*surfacePixelGet)(SurfaceT *surface, int16_t x, int16_t y);
extern void (*surfacePixelSet)(uint16_t x, uint16_t y, ColorT color); extern void (*surfacePixelSet)(uint16_t x, uint16_t y, ColorT color);
void surfaceBlit(int16_t targetX, int16_t targetY, SurfaceT *source); void surfaceBlit(int16_t targetX1, int16_t targetY1, int16_t width, int16_t height, SurfaceT *source);
void surfaceBlitWithTransparency(int16_t targetX, int16_t targetY, SurfaceT *source, ColorT transparent); void surfaceBlitWithTransparency(int16_t targetX, int16_t targetY, SurfaceT *source, ColorT transparent);
void surfaceClear(ColorT color); void surfaceClear(ColorT color);
ColorT surfaceColorMake(uint8_t r, uint8_t g, uint8_t b); ColorT surfaceColorMake(uint8_t r, uint8_t g, uint8_t b);

View file

@ -5,152 +5,15 @@
uint8_t __MAGIC_WINDOW = 0; uint8_t __MAGIC_WINDOW = 0;
static WindowT **_windowList = NULL; static WindowT **_windowList = NULL;
static WindowT *_windowTop = NULL; static WindowT *_windowTop = NULL;
WindowT *windowCreate(uint16_t x, uint16_t y, uint16_t w, uint16_t h, char *title, uint8_t flags, ...) { static void windowCache(WindowT *w);
WindowT *win = NULL;
NEW(WindowT, win);
guiWidgetBaseSet((WidgetT *)win, __MAGIC_WINDOW, x, y, w, h);
win->title = strdup(title);
win->flags = flags;
win->close.x = win->close.y = win->close.x2 = win->close.y2 = 0;
win->titlebar.x = win->titlebar.y = win->titlebar.x2 = win->titlebar.y2 = 0;
win->minimize.x = win->minimize.y = win->minimize.x2 = win->minimize.y2 = 0;
win->maximize.x = win->maximize.y = win->maximize.x2 = win->maximize.y2 = 0;
win->bounds.x = win->bounds.y = win->bounds.x2 = win->bounds.y2 = 0;
win->cached = NULL;
arrput(_windowList, win);
windowFocusSet(win);
return win;
}
void windowDestroy(struct WidgetS *widget, ...) { static void windowCache(WindowT *w) {
uint16_t i;
WindowT *window = (WindowT *)widget;
// Find the window to delete.
for (i=0; i<arrlen(_windowList); i++) {
if (window == _windowList[i]) {
// Was it the focused window?
if (_windowList[i] == _windowTop) {
// Find new topmost window on next call to focus.
_windowTop = NULL;
}
// Free the title.
if (_windowList[i]->title) DEL(_windowList[i]->title);
// Free cached surface.
if (_windowList[i]->cached) surfaceDestroy(&_windowList[i]->cached);
// Delete the window.
DEL(_windowList[i]);
// Remove it from window list.
arrdel(_windowList, i);
// Fixup focus.
windowFocusSet(NULL);
break;
}
}
}
void windowFocusSet(WindowT *win) {
int16_t i;
// Do we have a focused window at the moment?
if (!_windowTop || !win) {
// If there's a list of windows, use the topmost.
if (arrlen(_windowList) > 0) {
_windowTop = _windowList[arrlen(_windowList) - 1];
guiWidgetDirtySet(W(_windowTop), 1);
} else {
// If no list, set it to NULL.
_windowTop = NULL;
}
return;
}
// Did they even pass in a window? If not, we're just intended to fixup _windowTop.
if (!win) return;
// Are we already the topmost window?
if (win == _windowTop) return;
// Mark old focus and new focus dirty to repaint title bar.
guiWidgetDirtySet(W(win), 1);
guiWidgetDirtySet(W(_windowTop), 1);
// Change who has focus.
_windowTop = win;
// Reorder window list.
i = arrlen(_windowList) - 2;
if (i >= 0) {
for (; i>=0; i--) {
if (_windowList[i] == win) {
arrdel(_windowList, i);
arrput(_windowList, win);
break;
}
}
}
}
void windowMoveTo(WindowT *w, uint16_t x, uint16_t y) {
int16_t dx = x - w->base.r.x;
int16_t dy = y - w->base.r.y;
// This is all because we draw the window at 0,0 in order to cache it.
// To keep the coordinates correct, we have to adjust them all when
// the window is moved.
if (w->title || w->flags & WIN_CLOSE || w->flags & WIN_MAXIMIZE || w->flags & WIN_MINIMIZE) {
if (w->flags & WIN_CLOSE) {
w->close.x += dx;
w->close.y += dy;
w->close.x2 += dx;
w->close.y2 += dy;
}
if (w->flags & WIN_MAXIMIZE) {
w->maximize.x += dx;
w->maximize.y += dy;
w->maximize.x2 += dx;
w->maximize.y2 += dy;
}
if (w->flags & WIN_MINIMIZE) {
w->minimize.x += dx;
w->minimize.y += dy;
w->minimize.x2 += dx;
w->minimize.y2 += dy;
}
w->titlebar.x += dx;
w->titlebar.y += dy;
w->titlebar.x2 += dx;
w->titlebar.y2 += dy;
}
w->bounds.x += dx;
w->bounds.y += dy;
w->bounds.x2 += dx;
w->bounds.y2 += dy;
w->base.r.x = x;
w->base.r.y = y;
}
void windowPaint(struct WidgetS *widget, ...) {
int16_t i; int16_t i;
int16_t x1; int16_t x1;
int16_t y1; int16_t y1;
@ -161,17 +24,9 @@ void windowPaint(struct WidgetS *widget, ...) {
int16_t ty2; int16_t ty2;
int16_t originalX; int16_t originalX;
int16_t originalY; int16_t originalY;
WindowT *w;
SurfaceT *target;
ColorT titleBackgroundColor; ColorT titleBackgroundColor;
ColorT widgetColor; ColorT widgetColor;
SurfaceT *target = surfaceGet();
target = surfaceGet();
w = (WindowT *)widget;
// Do we need redrawn?
if (guiWidgetDirtyGet(widget)) {
guiWidgetDirtySet(widget, 0);
// Move the window to 0,0 to cache to it's own surface. // Move the window to 0,0 to cache to it's own surface.
originalX = w->base.r.x; originalX = w->base.r.x;
@ -307,16 +162,193 @@ void windowPaint(struct WidgetS *widget, ...) {
surfaceLineV(x1 + 1, y1, y2, GUI_WHITE); surfaceLineV(x1 + 1, y1, y2, GUI_WHITE);
} }
// Fake Window contents. // Blit contents.
surfaceBoxFilled(w->bounds.x, w->bounds.y, w->bounds.x2, w->bounds.y2, GUI_BLACK); if (w->content) surfaceBlit(w->bounds.x, w->bounds.y, w->bounds.x2 - w->bounds.x, w->bounds.y2 - w->bounds.y, w->content);
// Fixup all the widget coordinates. // Fixup all the widget coordinates.
windowMoveTo(w, originalX, originalY); windowMoveTo(w, originalX, originalY);
surfaceSet(target);
}
// Passing "flags" as a default int provides proper alignment for the following va_args list.
WindowT *windowCreate(uint16_t x, uint16_t y, uint16_t w, uint16_t h, char *title, int flags, ...) {
int16_t width;
int16_t height;
va_list args;
WindowT *win = NULL;
SurfaceT *t = surfaceGet();
NEW(WindowT, win);
guiWidgetBaseSet((WidgetT *)win, __MAGIC_WINDOW, x, y, w, h);
win->title = strdup(title);
win->flags = (uint8_t)flags;
win->close.x = win->close.y = win->close.x2 = win->close.y2 = 0;
win->titlebar.x = win->titlebar.y = win->titlebar.x2 = win->titlebar.y2 = 0;
win->minimize.x = win->minimize.y = win->minimize.x2 = win->minimize.y2 = 0;
win->maximize.x = win->maximize.y = win->maximize.x2 = win->maximize.y2 = 0;
win->bounds.x = win->bounds.y = win->bounds.x2 = win->bounds.y2 = 0;
win->cached = NULL;
win->content = NULL;
// If the window is resizable, we need to get two more arguments for the content size.
if (win->flags & WIN_RESIZE) {
va_start(args, flags);
width = va_arg(args, int);
height = va_arg(args, int);
va_end(args);
} else {
// Use whatever the default content area is. This causes an extra draw on create. Oh well.
windowCache(win);
width = win->bounds.x2 - win->bounds.x;
height = win->bounds.y2 - win->bounds.y;
guiWidgetDirtySet(W(win), 1);
}
win->content = surfaceCreate(width, height);
surfaceSet(win->content);
surfaceClear(GUI_LIGHTGRAY);
surfaceSet(t);
arrput(_windowList, win);
windowFocusSet(win);
return win;
}
void windowDestroy(struct WidgetS *widget, ...) {
uint16_t i;
WindowT *window = (WindowT *)widget;
// Find the window to delete.
for (i=0; i<arrlen(_windowList); i++) {
if (window == _windowList[i]) {
// Was it the focused window?
if (_windowList[i] == _windowTop) {
// Find new topmost window on next call to focus.
_windowTop = NULL;
}
// Free the title.
if (_windowList[i]->title) DEL(_windowList[i]->title);
// Free cached surface.
if (_windowList[i]->cached) surfaceDestroy(&_windowList[i]->cached);
// Free content surface.
if (_windowList[i]->content) surfaceDestroy(&_windowList[i]->content);
// Delete the window.
DEL(_windowList[i]);
// Remove it from window list.
arrdel(_windowList, i);
// Fixup focus.
windowFocusSet(NULL);
break;
}
}
}
void windowFocusSet(WindowT *win) {
int16_t i;
// Do we have a focused window at the moment?
if (!_windowTop || !win) {
// If there's a list of windows, use the topmost.
if (arrlen(_windowList) > 0) {
_windowTop = _windowList[arrlen(_windowList) - 1];
guiWidgetDirtySet(W(_windowTop), 1);
} else {
// If no list, set it to NULL.
_windowTop = NULL;
}
return;
}
// Did they even pass in a window? If not, we're just intended to fixup _windowTop.
if (!win) return;
// Are we already the topmost window?
if (win == _windowTop) return;
// Mark old focus and new focus dirty to repaint title bar.
guiWidgetDirtySet(W(win), 1);
guiWidgetDirtySet(W(_windowTop), 1);
// Change who has focus.
_windowTop = win;
// Reorder window list.
i = arrlen(_windowList) - 2;
if (i >= 0) {
for (; i>=0; i--) {
if (_windowList[i] == win) {
arrdel(_windowList, i);
arrput(_windowList, win);
break;
}
}
}
}
void windowMoveTo(WindowT *w, uint16_t x, uint16_t y) {
int16_t dx = x - w->base.r.x;
int16_t dy = y - w->base.r.y;
// This is all because we draw the window at 0,0 in order to cache it.
// To keep the coordinates correct, we have to adjust them all when
// the window is moved.
if (w->title || w->flags & WIN_CLOSE || w->flags & WIN_MAXIMIZE || w->flags & WIN_MINIMIZE) {
if (w->flags & WIN_CLOSE) {
w->close.x += dx;
w->close.y += dy;
w->close.x2 += dx;
w->close.y2 += dy;
}
if (w->flags & WIN_MAXIMIZE) {
w->maximize.x += dx;
w->maximize.y += dy;
w->maximize.x2 += dx;
w->maximize.y2 += dy;
}
if (w->flags & WIN_MINIMIZE) {
w->minimize.x += dx;
w->minimize.y += dy;
w->minimize.x2 += dx;
w->minimize.y2 += dy;
}
w->titlebar.x += dx;
w->titlebar.y += dy;
w->titlebar.x2 += dx;
w->titlebar.y2 += dy;
}
w->bounds.x += dx;
w->bounds.y += dy;
w->bounds.x2 += dx;
w->bounds.y2 += dy;
w->base.r.x = x;
w->base.r.y = y;
}
void windowPaint(struct WidgetS *widget, ...) {
WindowT *w = (WindowT *)widget;
// Do we need redrawn?
if (guiWidgetDirtyGet(widget)) {
guiWidgetDirtySet(widget, 0);
windowCache(w);
} }
// By now we have a valid cached window. Blit it. // By now we have a valid cached window. Blit it.
surfaceSet(target); surfaceBlit(w->base.r.x, w->base.r.y, 0, 0, w->cached);
surfaceBlit(w->base.r.x, w->base.r.y, w->cached);
} }
@ -335,6 +367,19 @@ RegisterT *windowRegister(uint8_t magic) {
} }
void wmShutdown(void) {
while (arrlen(_windowList) > 0) {
windowDestroy((WidgetT *)_windowList[0]);
}
arrfree(_windowList);
}
void wmStartup(void) {
// Nada
}
void wmUpdate(EventT *event) { void wmUpdate(EventT *event) {
int16_t i; int16_t i;
int16_t x2; int16_t x2;
@ -455,15 +500,3 @@ void wmUpdate(EventT *event) {
} // Do we have windows? } // Do we have windows?
} }
void wmShutdown(void) {
while (arrlen(_windowList) > 0) {
windowDestroy((WidgetT *)_windowList[0]);
}
arrfree(_windowList);
}
void wmStartup(void) {
}

View file

@ -21,6 +21,7 @@ typedef struct WindowS {
RectT minimize; // Coordinates of minimize box, if any. RectT minimize; // Coordinates of minimize box, if any.
RectT maximize; // Coordinates of maximize box, if any. RectT maximize; // Coordinates of maximize box, if any.
RectT bounds; // Inside edge of window frame. RectT bounds; // Inside edge of window frame.
SurfaceT *content; // Actual window contents - widgets and such.
SurfaceT *cached; // Once rendered, keep a cached copy for faster redrawing. SurfaceT *cached; // Once rendered, keep a cached copy for faster redrawing.
} WindowT; } WindowT;
@ -28,7 +29,7 @@ typedef struct WindowS {
extern uint8_t __MAGIC_WINDOW; // Magic ID assigned to us from the GUI. extern uint8_t __MAGIC_WINDOW; // Magic ID assigned to us from the GUI.
WindowT *windowCreate(uint16_t x, uint16_t y, uint16_t w, uint16_t h, char *title, uint8_t flags, ...); WindowT *windowCreate(uint16_t x, uint16_t y, uint16_t w, uint16_t h, char *title, int flags, ...);
void windowDestroy(struct WidgetS *widget, ...); void windowDestroy(struct WidgetS *widget, ...);
void windowFocusSet(WindowT *win); void windowFocusSet(WindowT *win);
void windowMoveTo(WindowT *win, uint16_t x, uint16_t y); void windowMoveTo(WindowT *win, uint16_t x, uint16_t y);
@ -36,9 +37,9 @@ void windowPaint(struct WidgetS *widget, ...);
RegisterT *windowRegister(uint8_t magic); RegisterT *windowRegister(uint8_t magic);
void wmUpdate(EventT *event);
void wmShutdown(void); void wmShutdown(void);
void wmStartup(void); void wmStartup(void);
void wmUpdate(EventT *event);
#endif // WMWINDOW_H #endif // WMWINDOW_H

View file

@ -14,7 +14,7 @@ int main(int argc, char *argv[]) {
if (guiStartup(800, 600, 32) == SUCCESS) { if (guiStartup(800, 600, 32) == SUCCESS) {
for (i=1; i<4; i++) { for (i=1; i<4; i++) {
sprintf(title, "Testing %d", i); sprintf(title, "Testing %d", i);
windowCreate(i * 50, i * 50, 300, 200, title, WIN_CLOSE | WIN_MAXIMIZE | WIN_MINIMIZE | WIN_RESIZE); windowCreate(i * 50, i * 50, 300, 200, title, WIN_CLOSE | WIN_MAXIMIZE | WIN_MINIMIZE);
} }
guiRun(); guiRun();
guiShutdown(); guiShutdown();