From b3931a6446a1f40ea230212e31e8bc750c245c2d Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Thu, 8 Nov 2018 21:29:21 -0600 Subject: [PATCH] Rearranged folder, added image conversion tools. --- .gitattributes | 1 + .gitignore | 12 +- imgconvert/imgconvert.pro | 48 ++++ imgconvert/main.c | 130 ++++++++++ build-IIgs.sh => joeylib/build-IIgs.sh | 14 +- build-PC.sh => joeylib/build-PC.sh | 10 +- joeylib.pro => joeylib/joeylib.pro | 0 {src => joeylib/src}/font.sta | Bin {src => joeylib/src}/jIIgs.asm | 0 {src => joeylib/src}/jIIgs.c | 0 {src => joeylib/src}/jIIgs.macro | 0 {src => joeylib/src}/jPC.c | 0 {src => joeylib/src}/joey.c | 0 {src => joeylib/src}/joey.h | 0 {src => joeylib/src}/kanga.sta | Bin {src => joeylib/src}/music.mod | Bin {src => joeylib/src}/stddclmr.h | 0 {src => joeylib/src}/test.c | 6 - vecdraw/main.c | 338 +++++++++++++++++++++++++ vecdraw/vecdraw.pro | 44 ++++ 20 files changed, 580 insertions(+), 23 deletions(-) create mode 100644 imgconvert/imgconvert.pro create mode 100644 imgconvert/main.c rename build-IIgs.sh => joeylib/build-IIgs.sh (80%) rename build-PC.sh => joeylib/build-PC.sh (91%) rename joeylib.pro => joeylib/joeylib.pro (100%) rename {src => joeylib/src}/font.sta (100%) rename {src => joeylib/src}/jIIgs.asm (100%) rename {src => joeylib/src}/jIIgs.c (100%) rename {src => joeylib/src}/jIIgs.macro (100%) rename {src => joeylib/src}/jPC.c (100%) rename {src => joeylib/src}/joey.c (100%) rename {src => joeylib/src}/joey.h (100%) rename {src => joeylib/src}/kanga.sta (100%) rename {src => joeylib/src}/music.mod (100%) rename {src => joeylib/src}/stddclmr.h (100%) rename {src => joeylib/src}/test.c (94%) create mode 100644 vecdraw/main.c create mode 100644 vecdraw/vecdraw.pro diff --git a/.gitattributes b/.gitattributes index 4f5bb27..87402d0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ *.sta filter=lfs diff=lfs merge=lfs -text *.mod filter=lfs diff=lfs merge=lfs -text +*.vec filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore index 03634be..d195269 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ *~ *.user -lib/ -src/SDL2/ -src/music -src/music.w -src/*.dis -src/*.map +joeylib/lib/ +joeylib/src/SDL2/ +joeylib/src/music +joeylib/src/music.w +joeylib/src/*.dis +joeylib/src/*.map diff --git a/imgconvert/imgconvert.pro b/imgconvert/imgconvert.pro new file mode 100644 index 0000000..beafe44 --- /dev/null +++ b/imgconvert/imgconvert.pro @@ -0,0 +1,48 @@ +TEMPLATE = app +CONFIG += console +CONFIG -= \ + app_bundle \ + qt + +JOEY = /home/scott/joey + +JOEY_INCLUDES = \ + $$JOEY/dist + +JOEY_HEADERS = \ + $$JOEY/dist/joey.h + +JOEY_LIBS = \ + -L$$JOEY/dist/linux/x64 \ + -Wl,-rpath,$$JOEY/dist/linux/x64 \ + -Wl,--enable-new-dtags \ + -l:joeylib.a \ + -Wl,--no-undefined \ + -ldl \ + -lsndio \ + -lpthread \ + -lrt \ + -lm + +JOEY_FLAGS = \ + -pthread \ + -D_REENTRANT + +SDL_IMAGE_LIBS = \ + -lSDL2_image + +QMAKE_CFLAGS += \ + $$JOEY_FLAGS + +INCLUDEPATH += \ + $$JOEY_INCLUDES + +HEADERS += \ + $$JOEY_HEADERS + +SOURCES += \ + main.c + +LIBS += \ + $$JOEY_LIBS \ + $$SDL_IMAGE_LIBS diff --git a/imgconvert/main.c b/imgconvert/main.c new file mode 100644 index 0000000..7695ea7 --- /dev/null +++ b/imgconvert/main.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include + + +#include "joey.h" + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" + +Uint32 getPixel(SDL_Surface *surface, int x, int y) { + + int bpp = surface->format->BytesPerPixel; + Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; + + switch(bpp) { + case 1: + return *p; + break; + + case 2: + // Generates cast increases required alignment of target type [-Wcast-align] warning. Harmless on x86. + return *(Uint16 *)p; + break; + + case 3: + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + return (Uint32)(p[0] << 16 | p[1] << 8 | p[2]); + else + return (Uint32)(p[0] | p[1] << 8 | p[2] << 16); + break; + + case 4: + // Generates cast increases required alignment of target type [-Wcast-align] warning. Harmless on x86. + return *(Uint32 *)p; + break; + + default: + return 0; + } +} + +#pragma GCC diagnostic pop + + +int main(int argc, char *argv[]) { + + if (argc < 3) { + printf("Usage: %s [infile] [outfile] {anyting}\n", argv[0]); + printf("(If {anything} is provided, image will be displayed as converted until keypress.)\n"); + return 1; + } + + jlUtilStartup("STA Converter"); + + int imageFlags = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF; + int imageResult = IMG_Init(imageFlags); + if ((imageResult & imageFlags) != imageFlags) { + printf("Failed to initialize SDL2_Image: %s\n", IMG_GetError()); + jlUtilShutdown(); + } + + SDL_Surface *image = IMG_Load(argv[1]); + int colors = INT_MAX; + SDL_Color *c = NULL; + + if (image->format->palette != NULL) { + colors = image->format->palette->ncolors; + if (colors > 16) { + colors = 16; + printf("Source palette has more than 16 colors. Only the first 16 will be used.\n"); + } + c = image->format->palette->colors; + } + + printf("%s = %dx%d, %d bits per pixel, %d colors\n", argv[1], image->w, image->h, image->format->BitsPerPixel, colors); + + if (image->w != 320 || image->h != 200 || image->format->BitsPerPixel != 8 || colors > 16) { + printf("Image must be 320x200 pixels, 8 bits per pixel, with 16 or fewer colors.\n"); + free(image); + IMG_Quit(); + jlUtilShutdown(); + } + + jlStaT *sta = NULL; + jlStaCreate(sta); + + for (int x=0; xpalette[x].r = c[x].r / 16; + sta->palette[x].g = c[x].g / 16; + sta->palette[x].b = c[x].b / 16; + printf("Palette %03d = R%03d G%03d B%03d\n", x, sta->palette[x].r, sta->palette[x].g, sta->palette[x].b); + } + + if (colors < 16) { + for (int x=colors; x<16; x++) { + sta->palette[x].r = 0; + sta->palette[x].g = 0; + sta->palette[x].b = 0; + printf("Palette %03d = R000 G000 B000 (added)\n", x); + } + } + + int p = 0; + for (int y=0; yh; y++) { + for (int x=0; xw; x+=2) { + //printf("%02X %02X ", getPixel(image, x, y), getPixel(image, x + 1, y)); + // These are backwards - no idea why yet. + sta->pixels[p].r = (unsigned char)getPixel(image, x, y); + sta->pixels[p++].l = (unsigned char)getPixel(image, x + 1, y); + } + } + + jlStaSave(sta, argv[2]); + + SDL_FreeSurface(image); + + if (argc > 3) { + jlStaDisplay(sta); + jlDisplayPresent(); + jlKeyWaitForAny(); + } + + IMG_Quit(); + jlUtilShutdown(); +} diff --git a/build-IIgs.sh b/joeylib/build-IIgs.sh similarity index 80% rename from build-IIgs.sh rename to joeylib/build-IIgs.sh index cccc645..339e532 100755 --- a/build-IIgs.sh +++ b/joeylib/build-IIgs.sh @@ -7,7 +7,7 @@ if [ -d ${OUT} ]; then fi mkdir -p ${OUT} -pushd ${JOEY}/joeylib/src +pushd ${JOEY}/joeylib/joeylib/src iix assemble jIIgs.asm keep=31:/out/joey/jIIgsasm iix compile jIIgs.c keep=31:/out/joey/jIIgsc iix compile joey.c keep=31:/out/joey/joey @@ -25,8 +25,8 @@ iix dumpobj +D 31:/out/joey/test &> test.dis || true php ${JOEY}/sdks/iix/ntconverter.php *.mod popd -cp -f ${JOEY}/joeylib/lib/IIgs/Tool221#ba0000 ${JOEY}/dist/IIgs/. -cp -f ${JOEY}/joeylib/src/joey.h ${JOEY}/dist/. +cp -f ${JOEY}/joeylib/joeylib/lib/IIgs/Tool221#ba0000 ${JOEY}/dist/IIgs/. +cp -f ${JOEY}/joeylib/joeylib/src/joey.h ${JOEY}/dist/. cp -f ${OUT}/joeylib ${JOEY}/dist/IIgs/joeylib#b20000 if [ ! -z $1 ]; then @@ -38,10 +38,10 @@ if [ ! -z $1 ]; then rm ${IMPORT} 2> /dev/null || true cp ${OUT}/test ${OUT}/Test#B30000 - cp ${JOEY}/joeylib/src/kanga.sta ${OUT}/kanga.sta#060000 - cp ${JOEY}/joeylib/src/font.sta ${OUT}/font.sta#060000 - cp ${JOEY}/joeylib/src/music ${OUT}/music#D50000 - cp ${JOEY}/joeylib/src/music.w ${OUT}/music.w#060000 + cp ${JOEY}/joeylib/joeylib/src/kanga.sta ${OUT}/kanga.sta#060000 + cp ${JOEY}/joeylib/joeylib/src/font.sta ${OUT}/font.sta#060000 + cp ${JOEY}/joeylib/joeylib/src/music ${OUT}/music#D50000 + cp ${JOEY}/joeylib/joeylib/src/music.w ${OUT}/music.w#060000 ${CADIUS} createvolume ${IMPORT} ${VOL} 32MB > /dev/null ${CADIUS} createfolder ${IMPORT} ${VOL}/data > /dev/null diff --git a/build-PC.sh b/joeylib/build-PC.sh similarity index 91% rename from build-PC.sh rename to joeylib/build-PC.sh index ed60de9..bec2637 100755 --- a/build-PC.sh +++ b/joeylib/build-PC.sh @@ -1,8 +1,8 @@ #!/bin/bash -e -BUILD="${JOEY}/joeylib/build" -SRC="${JOEY}/joeylib/src" +BUILD="${JOEY}/joeylib/joeylib/build" +SRC="${JOEY}/joeylib/joeylib/src" function doBuild() { @@ -28,6 +28,10 @@ function doBuild() { ar rcs joeylib.a *.o rm *.o popd + + if [ -d "${BUILD}" ]; then + rm -rf "${BUILD}" + fi } CC="gcc" @@ -65,5 +69,3 @@ CFLAGS="-Wall -D_REENTRANT -I${SRC}" DIST="${JOEY}/dist/macos/x64" INSTALLED="${JOEY}/SDL2/installed/macos/x64/lib" doBuild - -rm -rf build diff --git a/joeylib.pro b/joeylib/joeylib.pro similarity index 100% rename from joeylib.pro rename to joeylib/joeylib.pro diff --git a/src/font.sta b/joeylib/src/font.sta similarity index 100% rename from src/font.sta rename to joeylib/src/font.sta diff --git a/src/jIIgs.asm b/joeylib/src/jIIgs.asm similarity index 100% rename from src/jIIgs.asm rename to joeylib/src/jIIgs.asm diff --git a/src/jIIgs.c b/joeylib/src/jIIgs.c similarity index 100% rename from src/jIIgs.c rename to joeylib/src/jIIgs.c diff --git a/src/jIIgs.macro b/joeylib/src/jIIgs.macro similarity index 100% rename from src/jIIgs.macro rename to joeylib/src/jIIgs.macro diff --git a/src/jPC.c b/joeylib/src/jPC.c similarity index 100% rename from src/jPC.c rename to joeylib/src/jPC.c diff --git a/src/joey.c b/joeylib/src/joey.c similarity index 100% rename from src/joey.c rename to joeylib/src/joey.c diff --git a/src/joey.h b/joeylib/src/joey.h similarity index 100% rename from src/joey.h rename to joeylib/src/joey.h diff --git a/src/kanga.sta b/joeylib/src/kanga.sta similarity index 100% rename from src/kanga.sta rename to joeylib/src/kanga.sta diff --git a/src/music.mod b/joeylib/src/music.mod similarity index 100% rename from src/music.mod rename to joeylib/src/music.mod diff --git a/src/stddclmr.h b/joeylib/src/stddclmr.h similarity index 100% rename from src/stddclmr.h rename to joeylib/src/stddclmr.h diff --git a/src/test.c b/joeylib/src/test.c similarity index 94% rename from src/test.c rename to joeylib/src/test.c index eaa840c..180922f 100644 --- a/src/test.c +++ b/joeylib/src/test.c @@ -58,16 +58,10 @@ int main(void) { jlStaT *kanga = NULL; jlStaT *font = NULL; - jlVecT *vec = NULL; jint16 y; jlUtilStartup("JoeyLib Test"); - jlVecLoad(vec, "nowhere"); - jlVecDisplay(vec, 0, 0); - jlDisplayPresent(); - jlKeyWaitForAny(); - jlStaLoad(kanga, "kanga"); jlStaLoad(font, "font"); diff --git a/vecdraw/main.c b/vecdraw/main.c new file mode 100644 index 0000000..da8d942 --- /dev/null +++ b/vecdraw/main.c @@ -0,0 +1,338 @@ +#include +#include +#include + + +#include "joey.h" + + +#define MAX_LINE_SIZE 8192 + +#define STATE_COMMAND 1 +#define STATE_NUMBER 2 +#define STATE_EXECUTE 3 + +#define COMMAND_COLOR 1 // C +#define COMMAND_CLEAR 2 // E +#define COMMAND_PLOT 3 // D (Draw) +#define COMMAND_LINE 4 // L +#define COMMAND_FILL 5 // F +#define COMMAND_PALETTE 6 // P +#define COMMAND_RESET 7 // R +#define COMMAND_HBOX 8 // B (Box) +#define COMMAND_FBOX 9 // S (Solid Box) +#define COMMAND_FILLTO 10 // T (FillTo) + + +typedef struct { + char *keyword; + byte command; + byte args; +} CommandT; + + +static CommandT commands[] = { + { "COLOR", COMMAND_COLOR, 1 }, + { "CLEAR", COMMAND_CLEAR, 0 }, + { "PLOT", COMMAND_PLOT, 2 }, + { "LINE", COMMAND_LINE, 2 }, + { "FILL", COMMAND_FILL, 2 }, + { "PALETTE", COMMAND_PALETTE, 4 }, + { "PAL", COMMAND_PALETTE, 4 }, + { "RESET", COMMAND_RESET, 0 }, + { "BOX", COMMAND_HBOX, 4 }, + { "FILLEDBOX", COMMAND_FBOX, 4 }, + { "FBOX", COMMAND_FBOX, 4 }, + { "FILLTO", COMMAND_FILLTO, 3 } +}; + + +// Ugly Macros +#define EMIT_COMMAND fputc((byte)data[0], out) +#define EMIT_COUNT fputc((byte)count, out) +#define EMIT_BYTE fputc((byte)data[i], out) +#define EMIT_JINT16 _emitJint16(data[i], out); + + +void _emitJint16(jint16 data, FILE *out) { +#ifdef JOEY_LITLE_ENDIAN + fputc((byte)(data & 0xFF), out); + fputc((byte)(data >> 8), out); +#else + fputc((byte)(data >> 8), out); + fputc((byte)(data & 0xFF), out); +#endif +} + + +int _stricmp(const char *a, const char *b) { + int ca; + int cb; + do { + ca = (unsigned char)*a++; + cb = (unsigned char)*b++; + if (islower(ca)) ca = toupper(ca); + if (islower(cb)) cb = toupper(cb); + } while (ca == cb && ca != '\0'); + return ca - cb; +} + + +int main(int argc, char *argv[]) { + + FILE *in; + FILE *out = NULL; + char line[MAX_LINE_SIZE]; + char *token; + int tokenLength; + jint16 data[1024]; + int dIndex; + int state; + int args; + int count; + int i; + int x; + jint16 x1; + jint16 y1; + jint16 x2; + jint16 y2; + + if (argc < 3) { + printf("Usage: %s [infile] [outfile]\n", argv[0]); + return 1; + } + + jlUtilStartup("JoeyLib VecDraw"); + jlDisplayBorder(BORDER_BLACK); + + while (!jlUtilMustExit()) { + + in = fopen(argv[1], "rt"); + if (in == NULL) { + jlUtilShutdown(); + } + + out = fopen(argv[2], "wb"); + if (out == NULL) { + fclose(in); + jlUtilShutdown(); + } + + fputs("VEC", out); // File "Magic" + fputc(0, out); // Version + + while (fgets(line, MAX_LINE_SIZE, in) != NULL) { + state = STATE_COMMAND; + dIndex = 0; + token = strtok(line, " "); + while (token != NULL) { + tokenLength = (int)strlen(token); + if (tokenLength > 0) { + // Handle Comments + if (token[0] == '#') { + break; + } + // Handle EOLs + if ((token[tokenLength - 1] == 10) || (token[tokenLength - 1] == 13)) { + tokenLength--; + token[tokenLength] = 0; + } + if (tokenLength > 0) { + //printf("[%s] %d\n", token, dIndex); + // What is this? + if (state == STATE_COMMAND) { + for (i=0; i<(int)(sizeof(commands)/sizeof(CommandT)); i++) { + //printf("Checking [%s] == [%s]\n", token, commands[i].keyword); + if (_stricmp(token, commands[i].keyword) == 0) { + data[dIndex++] = commands[i].command; + state = (commands[i].args == 0 ? STATE_EXECUTE : STATE_NUMBER); + //printf("Found [%s] expecting %d args\n", token, commands[i].args); + break; + } + } + if (state == STATE_COMMAND) { + printf("Unknown command [%s]\n", token); + break; + } + } else { + if (state == STATE_NUMBER) { + data[dIndex++] = (jint16)atoi(token); + } + } + } + } + token = strtok(NULL, " "); + } + // Can we execute this? + if (dIndex > 0) { + args = dIndex - 1; + i = 1; + //printf("Command %d with %d args\n", data[0], args); + switch (data[0]) { + case COMMAND_COLOR: + if (args == 1) { + EMIT_COMMAND; + EMIT_BYTE; + jlDrawColor((byte)data[i]); + } + break; + + case COMMAND_CLEAR: + if (args == 0) { + EMIT_COMMAND; + jlDrawClear(); + } + break; + + case COMMAND_PLOT: + count = args % 2; + if (count == 0) { + count = args / 2; + EMIT_COMMAND; + EMIT_COUNT; + for (x=0; x= 4)) { + count = args / 2; + EMIT_COMMAND; + EMIT_COUNT; + EMIT_JINT16; + x1 = data[i++]; + EMIT_BYTE; + y1 = data[i++]; + for (x=1; x