Window Manager finished? Moving on!

This commit is contained in:
Scott Duensing 2022-06-24 18:41:27 -05:00
parent 431e4a40eb
commit 451c95a0ca
7 changed files with 229 additions and 86 deletions

View file

@ -75,10 +75,8 @@ void guiRegister(WidgetRegisterT widgetRegister) {
void guiRun(void) { void guiRun(void) {
while (_guiRunning) { while (_guiRunning) {
// Process all GUI events. // Process all GUI events.
guiEventsDo(); guiEventsDo();
} }
} }
@ -111,22 +109,22 @@ void guiShutdown(void) {
uint8_t guiStartup(int16_t width, int16_t height, int16_t depth) { uint8_t guiStartup(int16_t width, int16_t height, int16_t depth) {
uint8_t i; uint8_t i;
uint8_t EGA[16][3] = { uint8_t EGA[16][3] = {
{ 0, 0, 0 }, /* black */ { 0, 0, 0 }, // black
{ 0, 0, 170 }, /* blue */ { 0, 0, 170 }, // blue
{ 0, 170, 0 }, /* green */ { 0, 170, 0 }, // green
{ 0, 170, 170 }, /* cyan */ { 0, 170, 170 }, // cyan
{ 170, 0, 0 }, /* red */ { 170, 0, 0 }, // red
{ 170, 0, 170 }, /* magenta */ { 170, 0, 170 }, // magenta
{ 170, 85, 0 }, /* brown */ { 170, 85, 0 }, // brown
{ 170, 170, 170 }, /* light gray */ { 170, 170, 170 }, // light gray
{ 85, 85, 85 }, /* dark gray */ { 85, 85, 85 }, // dark gray
{ 85, 85, 255 }, /* light blue */ { 85, 85, 255 }, // light blue
{ 85, 255, 85 }, /* light green */ { 85, 255, 85 }, // light green
{ 85, 255, 255 }, /* light cyan */ { 85, 255, 255 }, // light cyan
{ 255, 85, 85 }, /* light red */ { 255, 85, 85 }, // light red
{ 255, 85, 255 }, /* light magenta */ { 255, 85, 255 }, // light magenta
{ 255, 255, 85 }, /* yellow */ { 255, 255, 85 }, // yellow
{ 255, 255, 255 } /* white */ { 255, 255, 255 } // white
}; };
if (platformStartup(width, height, depth) == FAIL) return FAIL; if (platformStartup(width, height, depth) == FAIL) return FAIL;

View file

@ -15,27 +15,37 @@ SurfaceT *imageLoad(char *filename) {
int32_t r; int32_t r;
int32_t x; int32_t x;
int32_t y; int32_t y;
SurfaceT *i = NULL; SurfaceT *i = NULL;
SurfaceT *t = surfaceGet(); SurfaceT *t = surfaceGet();
unsigned char *raw = NULL; unsigned char *raw = NULL;
char *cached = NULL;
r = stbi_info(filename, &w, &h, &n); // Has this image been cached?
if (r) { cached = utilFileExtensionChange(filename, "");
raw = stbi_load(filename, &w, &h, &n, PIXEL_COMPONENTS); i = surfaceLoad(cached);
if (raw) { if (!i) {
i = surfaceCreate(w, h); // Nope. Decode PNG and save raw.
surfaceSet(i); r = stbi_info(filename, &w, &h, &n);
n = 0; if (r) {
for (y=0; y<h; y++) { raw = stbi_load(filename, &w, &h, &n, PIXEL_COMPONENTS);
for (x=0; x<w; x++) { if (raw) {
surfacePixelSet(x, y, surfaceColorMake(raw[n], raw[n + 1], raw[n + 2])); i = surfaceCreate(w, h);
n += PIXEL_COMPONENTS; surfaceSet(i);
n = 0;
for (y=0; y<h; y++) {
for (x=0; x<w; x++) {
surfacePixelSet(x, y, surfaceColorMake(raw[n], raw[n + 1], raw[n + 2]));
n += PIXEL_COMPONENTS;
}
} }
surfaceSet(t);
stbi_image_free(raw);
surfaceSave(i, cached);
} }
surfaceSet(t);
stbi_image_free(raw);
} }
} }
DEL(cached);
return i; return i;
} }

View file

@ -7,10 +7,20 @@ uint8_t __surfaceBytesPerPixel = 0;
SurfaceFormatT __surfaceFormat = { 0 }; SurfaceFormatT __surfaceFormat = { 0 };
void (*surfaceLineH)(int16_t x1, int16_t x2, int16_t y, ColorT c);
void (*surfaceLineV)(int16_t x, int16_t y1, int16_t y2, ColorT c);
ColorT (*surfacePixelGet)(SurfaceT *surface, int16_t x, int16_t y); ColorT (*surfacePixelGet)(SurfaceT *surface, int16_t x, int16_t y);
void (*surfacePixelSet)(uint16_t x, uint16_t y, ColorT color); void (*surfacePixelSet)(uint16_t x, uint16_t y, ColorT color);
static void surfaceLineH8(int16_t x1, int16_t x2, int16_t y, ColorT c);
static void surfaceLineH16(int16_t x1, int16_t x2, int16_t y, ColorT c);
static void surfaceLineH32(int16_t x1, int16_t x2, int16_t y, ColorT c);
static void surfaceLineV8(int16_t x, int16_t y1, int16_t y2, ColorT c);
static void surfaceLineV16(int16_t x, int16_t y1, int16_t y2, ColorT c);
static void surfaceLineV32(int16_t x, int16_t y1, int16_t y2, ColorT c);
static ColorT surfacePixelGet8(SurfaceT *surface, int16_t x, int16_t y); static ColorT surfacePixelGet8(SurfaceT *surface, int16_t x, int16_t y);
static ColorT surfacePixelGet16(SurfaceT *surface, int16_t x, int16_t y); static ColorT surfacePixelGet16(SurfaceT *surface, int16_t x, int16_t y);
static ColorT surfacePixelGet32(SurfaceT *surface, int16_t x, int16_t y); static ColorT surfacePixelGet32(SurfaceT *surface, int16_t x, int16_t y);
@ -97,9 +107,7 @@ void surfaceClear(ColorT color) {
size_t offsetTarget; size_t offsetTarget;
// Draw the top line. // Draw the top line.
for (x=0; x<__surfaceActive->width; x++) { surfaceLineH(0, __surfaceActive->width - 1, 0, color);
surfacePixelSet(x, 0, color);
}
// Copy it to the other lines. // Copy it to the other lines.
offsetTarget = __surfaceActive->scanline; offsetTarget = __surfaceActive->scanline;
@ -120,8 +128,9 @@ ColorT surfaceColorMake(uint8_t r, uint8_t g, uint8_t b) {
SurfaceT *surfaceCreate(int16_t width, int16_t height) { SurfaceT *surfaceCreate(int16_t width, int16_t height) {
SurfaceT *surface = (SurfaceT *)malloc(sizeof(SurfaceT)); SurfaceT *surface = NULL;
NEW(SurfaceT, surface);
if (!surface) return NULL; if (!surface) return NULL;
surface->width = width; surface->width = width;
@ -168,9 +177,7 @@ void surfaceBoxFilled(int16_t x1, int16_t y1, int16_t x2, int16_t y2, ColorT c)
width = (x2 - x1 + 1) * __surfaceBytesPerPixel; width = (x2 - x1 + 1) * __surfaceBytesPerPixel;
// Draw the top line. // Draw the top line.
for (i=x1; i<=x2; i++) { surfaceLineH(x1, x2, y1, c);
surfacePixelSet(i, y1, c);
}
// Copy it to the other lines. // Copy it to the other lines.
offsetTarget = __surfaceActive->scanline * (y1 + 1) + (x1 * __surfaceBytesPerPixel); offsetTarget = __surfaceActive->scanline * (y1 + 1) + (x1 * __surfaceBytesPerPixel);
@ -268,38 +275,135 @@ void surfaceLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, ColorT color) {
} }
void surfaceLineH(int16_t x1, int16_t x2, int16_t y, ColorT c) { static void surfaceLineH8(int16_t x1, int16_t x2, int16_t y, ColorT c) {
int16_t i; int16_t i;
int16_t t; size_t offset;
if (x1 > x2) { if (x1 > x2) {
t = x2; i = x2;
x2 = x1; x2 = x1;
x1 = t; x1 = i;
} }
for (i=x1; i<=x2; i++) { offset = y * __surfaceActive->width + x1;
surfacePixelSet(i, y, c); for (i=x1; i<=x2; i++) __surfaceActive->buffer.bits8[offset++] = (uint8_t)c;
}
static void surfaceLineH16(int16_t x1, int16_t x2, int16_t y, ColorT c) {
int16_t i;
size_t offset;
if (x1 > x2) {
i = x2;
x2 = x1;
x1 = i;
}
offset = y * __surfaceActive->width + x1;
for (i=x1; i<=x2; i++) __surfaceActive->buffer.bits16[offset++] = (uint16_t)c;
}
static void surfaceLineH32(int16_t x1, int16_t x2, int16_t y, ColorT c) {
int16_t i;
size_t offset;
if (x1 > x2) {
i = x2;
x2 = x1;
x1 = i;
}
offset = y * __surfaceActive->width + x1;
for (i=x1; i<=x2; i++) __surfaceActive->buffer.bits32[offset++] = (uint32_t)c;
}
static void surfaceLineV8(int16_t x, int16_t y1, int16_t y2, ColorT c) {
int16_t i;
size_t offset;
if (y1 > y2) {
i = y2;
y2 = y1;
y1 = i;
}
offset = y1 * __surfaceActive->width + x;
for (i=y1; i<=y2; i++) {
__surfaceActive->buffer.bits8[offset] = (uint8_t)c;
offset += __surfaceActive->width;
} }
} }
void surfaceLineV(int16_t x, int16_t y1, int16_t y2, ColorT c) { static void surfaceLineV16(int16_t x, int16_t y1, int16_t y2, ColorT c) {
int16_t i; int16_t i;
int16_t t; size_t offset;
if (y1 > y2) { if (y1 > y2) {
t = y2; i = y2;
y2 = y1; y2 = y1;
y1 = t; y1 = i;
} }
offset = y1 * __surfaceActive->width + x;
for (i=y1; i<=y2; i++) { for (i=y1; i<=y2; i++) {
surfacePixelSet(x, i, c); __surfaceActive->buffer.bits16[offset] = (uint16_t)c;
offset += __surfaceActive->width;
} }
} }
static void surfaceLineV32(int16_t x, int16_t y1, int16_t y2, ColorT c) {
int16_t i;
size_t offset;
if (y1 > y2) {
i = y2;
y2 = y1;
y1 = i;
}
offset = y1 * __surfaceActive->width + x;
for (i=y1; i<=y2; i++) {
__surfaceActive->buffer.bits32[offset] = (uint32_t)c;
offset += __surfaceActive->width;
}
}
SurfaceT *surfaceLoad(char *filename) {
char ext[5] = { 0 };
char *name = NULL;
FILE *in = NULL;
SurfaceT *i = NULL;
sprintf(ext, "S%d", __surfaceBitsPerPixel);
name = utilFileExtensionChange(filename, ext);
if (!utilFileExists(name)) {
in = fopen(name, "rb");
if (in) {
NEW(SurfaceT, i);
if (!i) return NULL;
fread(&i->width, sizeof(uint16_t), 1, in);
fread(&i->height, sizeof(uint16_t), 1, in);
fread(&i->scanline, sizeof(size_t), 1, in);
fread(&i->bytes, sizeof(size_t), 1, in);
i->buffer.bits8 = (uint8_t *)malloc(i->bytes);
fread(i->buffer.bits8, i->bytes, 1, in);
fclose(in);
}
}
DEL(name);
return i;
}
static ColorT surfacePixelGet8(SurfaceT *surface, int16_t x, int16_t y) { static ColorT surfacePixelGet8(SurfaceT *surface, int16_t x, int16_t y) {
return surface->buffer.bits8[y * surface->width + x]; return surface->buffer.bits8[y * surface->width + x];
} }
@ -330,6 +434,28 @@ static void surfacePixelSet32(uint16_t x, uint16_t y, ColorT color) {
} }
void surfaceSave(SurfaceT *surface, char *filename) {
char ext[5] = { 0 };
char *name = NULL;
FILE *out = NULL;
sprintf(ext, "S%d", __surfaceBitsPerPixel);
name = utilFileExtensionChange(filename, ext);
out = fopen(name, "wb");
if (out) {
fwrite(&surface->width, sizeof(uint16_t), 1, out);
fwrite(&surface->height, sizeof(uint16_t), 1, out);
fwrite(&surface->scanline, sizeof(size_t), 1, out);
fwrite(&surface->bytes, sizeof(size_t), 1, out);
fwrite(surface->buffer.bits8, surface->bytes, 1, out);
fclose(out);
}
DEL(name);
}
void surfaceSet(SurfaceT *surface) { void surfaceSet(SurfaceT *surface) {
__surfaceActive = surface; __surfaceActive = surface;
} }
@ -356,6 +482,8 @@ void surfaceStartup(uint8_t bits) {
redMaskSize = 3; redMaskSize = 3;
greenMaskSize = 3; greenMaskSize = 3;
blueMaskSize = 2; blueMaskSize = 2;
surfaceLineH = surfaceLineH8;
surfaceLineV = surfaceLineV8;
surfacePixelSet = surfacePixelSet8; surfacePixelSet = surfacePixelSet8;
surfacePixelGet = surfacePixelGet8; surfacePixelGet = surfacePixelGet8;
break; break;
@ -366,6 +494,8 @@ void surfaceStartup(uint8_t bits) {
redMaskSize = 5; redMaskSize = 5;
greenMaskSize = 6; greenMaskSize = 6;
blueMaskSize = 5; blueMaskSize = 5;
surfaceLineH = surfaceLineH16;
surfaceLineV = surfaceLineV16;
surfacePixelSet = surfacePixelSet16; surfacePixelSet = surfacePixelSet16;
surfacePixelGet = surfacePixelGet16; surfacePixelGet = surfacePixelGet16;
break; break;
@ -376,6 +506,8 @@ void surfaceStartup(uint8_t bits) {
redMaskSize = 8; redMaskSize = 8;
greenMaskSize = 8; greenMaskSize = 8;
blueMaskSize = 8; blueMaskSize = 8;
surfaceLineH = surfaceLineH32;
surfaceLineV = surfaceLineV32;
surfacePixelSet = surfacePixelSet32; surfacePixelSet = surfacePixelSet32;
surfacePixelGet = surfacePixelGet32; surfacePixelGet = surfacePixelGet32;
break; break;

View file

@ -41,6 +41,8 @@ extern uint8_t __surfaceBytesPerPixel;
extern SurfaceFormatT __surfaceFormat; extern SurfaceFormatT __surfaceFormat;
extern void (*surfaceLineH)(int16_t x1, int16_t x2, int16_t y, ColorT c);
extern void (*surfaceLineV)(int16_t x, int16_t y1, int16_t y2, ColorT c);
extern ColorT (*surfacePixelGet)(SurfaceT *surface, int16_t x, int16_t y); 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);
@ -57,8 +59,8 @@ void surfaceDestroy(SurfaceT **surface);
SurfaceT *surfaceGet(void); SurfaceT *surfaceGet(void);
int16_t surfaceHeightGet(SurfaceT *surface); int16_t surfaceHeightGet(SurfaceT *surface);
void surfaceLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, ColorT color); void surfaceLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, ColorT color);
void surfaceLineH(int16_t x1, int16_t x2, int16_t y, ColorT c); SurfaceT *surfaceLoad(char *filename);
void surfaceLineV(int16_t x, int16_t y1, int16_t y2, ColorT c); void surfaceSave(SurfaceT *surface, char *filename);
void surfaceSet(SurfaceT *surface); void surfaceSet(SurfaceT *surface);
void surfaceShutdown(void); void surfaceShutdown(void);
void surfaceStartup(uint8_t bits); void surfaceStartup(uint8_t bits);

View file

@ -37,7 +37,7 @@ void memoryShutdown(void) {
void memoryStartup(char *appName) { void memoryStartup(char *appName) {
char *logName = utilAppNameWithNewExtensionGet(appName, "log"); char *logName = utilFileExtensionChange(appName, "log");
_memoryLog = fopen(logName, "w"); _memoryLog = fopen(logName, "w");

View file

@ -6,36 +6,6 @@
char __scratch[SCRATCH_SIZE]; char __scratch[SCRATCH_SIZE];
char *utilAppNameWithNewExtensionGet(char *appName, char *extension) {
char *c = NULL;
char *newName = NULL;
int16_t x = strlen(appName);
uint16_t len = 2 + strlen(extension); // 2 = dot in extension and 0 terminator.
// Find last portion of filename.
while (x > 0) {
if (appName[x] == '/' || appName[x] == '\\') break;
x--;
len++;
}
// We use this + length of new extension for new string length.
newName = (char *)malloc(len);
if (newName) {
if (strlen(appName) - x < len) {
// Replace extension
strncpy(newName, &appName[x + 1], len - 1);
c = strstr(newName, ".");
if (c) *c = 0;
strncat(newName, ".", len - 1);
strncat(newName, extension, len - 1);
}
}
return newName;
}
void utilBitsPrint(uint8_t byte) { void utilBitsPrint(uint8_t byte) {
int i = 0; int i = 0;
@ -100,6 +70,37 @@ uint8_t utilFileExists(char *filename) {
} }
char *utilFileExtensionChange(char *appName, char *extension) {
char *c = NULL;
char *newName = NULL;
int16_t x = strlen(appName);
uint16_t len = 2 + strlen(extension); // 2 = dot in extension and 0 terminator.
// Find last portion of filename.
while (x > 0) {
if (appName[x] == '/' || appName[x] == '\\') { x++; break; }
x--;
len++;
}
x--;
// We use this + length of new extension for new string length.
newName = (char *)malloc(len);
if (newName) {
if (strlen(appName) - x < len) {
// Replace extension
strncpy(newName, &appName[x + 1], len - 1);
c = strstr(newName, ".");
if (c) *c = 0;
strncat(newName, ".", len - 1);
strncat(newName, extension, len - 1);
}
}
return newName;
}
uint8_t utilFromFileReadByte(FILE *f, uint8_t *result) { uint8_t utilFromFileReadByte(FILE *f, uint8_t *result) {
unsigned char c; unsigned char c;

View file

@ -11,7 +11,7 @@
extern char __scratch[SCRATCH_SIZE]; extern char __scratch[SCRATCH_SIZE];
char *utilAppNameWithNewExtensionGet(char *appName, char *extension); char *utilFileExtensionChange(char *appName, char *extension);
void utilBitsPrint(uint8_t byte); void utilBitsPrint(uint8_t byte);
char *utilCreateString(char *format, ...); char *utilCreateString(char *format, ...);
char *utilCreateStringVArgs(char *format, va_list args); char *utilCreateStringVArgs(char *format, va_list args);