# HG changeset patch # User Matti Hamalainen # Date 1526173520 -10800 # Node ID 31010318ea8cf657d8d980cdc3a10e3f08619c26 # Parent ba09aa661c72350393bd26b5c413d604e561308b Do some vetting on the tests, which are rather crappy anyway. diff -r ba09aa661c72 -r 31010318ea8c Makefile.gen --- a/Makefile.gen Sun May 13 01:19:21 2018 +0300 +++ b/Makefile.gen Sun May 13 04:05:20 2018 +0300 @@ -145,7 +145,7 @@ ifeq ($(DM_BUILD_TESTS),yes) ifeq ($(DM_GFX_BLITS),yes) ifeq ($(DM_USE_STDIO),yes) -TESTS_BINARIES += blittest efu +TESTS_BINARIES += blittest endif endif endif @@ -272,7 +272,7 @@ ### What tests to build? ifeq ($(DM_BUILD_TESTS),yes) -TESTS_BINARIES += vecmattest fptest dzlib +TESTS_BINARIES += vecmattest fptest dzlibtest endif FONTCONV_BIN=fontconv @@ -430,7 +430,6 @@ @$(RANLIB) $@ - ### ### Tests ### @@ -438,25 +437,17 @@ @echo " LINK $+" @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(SDL_TTF_LDFLAGS) -lm -$(TESTS_BINPATH)efu$(EXEEXT): $(OBJPATH)efu.o $(DMLIB_A) - @echo " LINK $+" - @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(SDL_TTF_LDFLAGS) $(ZLIB_LDFLAGS) -lm - -$(TESTS_BINPATH)vptest$(EXEEXT): $(OBJPATH)vptest.o $(DMLIB_A) +$(TESTS_BINPATH)dzlibtest$(EXEEXT): $(OBJPATH)dzlibtest.o $(DMLIB_A) @echo " LINK $+" - @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(SDL_TTF_LDFLAGS) -lm - -$(TESTS_BINPATH)%test$(EXEEXT): $(OBJPATH)%test.o $(DMLIB_A) - @echo " LINK $+" - @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(TOOL_LDFLAGS) -lm + @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(ZLIB_LDFLAGS) $(TESTS_BINPATH)plrtest$(EXEEXT): $(OBJPATH)plrtest.o $(DMLIB_A) @echo " LINK $+" @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(ZLIB_LDFLAGS) -lm -$(TESTS_BINPATH)dzlib$(EXEEXT): $(OBJPATH)dzlib.o $(DMLIB_A) +$(TESTS_BINPATH)%test$(EXEEXT): $(OBJPATH)%test.o $(DMLIB_A) @echo " LINK $+" - @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(SDL_LDFLAGS) $(ZLIB_LDFLAGS) + @$(CC) -o $@ $(filter %.o %.a,$+) $(DM_LDFLAGS) $(TOOL_LDFLAGS) -lm ### diff -r ba09aa661c72 -r 31010318ea8c tests/dzlib.c --- a/tests/dzlib.c Sun May 13 01:19:21 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,332 +0,0 @@ -#include "dmlib.h" -#include "dmzlib.h" -#include "dmfile.h" -#include "dmargs.h" -#include - - -#define SET_TMPBUF_SIZE (16 * 1024) - -char *optInFilename = NULL, - *optOutFilename = NULL; - -unsigned int optSkip = 0; -unsigned int optCompressLevel = Z_BEST_COMPRESSION; -BOOL optCompress = FALSE, - optUseZLIB = FALSE; - - -static const DMOptArg optList[] = -{ - { 0, '?', "help", "Show this help", OPT_NONE }, - { 1, 'q', "quiet", "Decrease verbosity", OPT_NONE }, - { 2, 'Z', "zlib", "Use ZLIB instead of dmzlib", OPT_NONE }, - { 3, 'c', "compress", "Compress instead of decompressing (ZLIB only)", OPT_NONE }, - { 4, 'l', "level", "Set zlib compression level 1-9", OPT_ARGREQ }, - { 5, 's', "skip", "Skip bytes from input start", OPT_ARGREQ }, -}; - -static const int optListN = sizeof(optList) / sizeof(optList[0]); - - -void argShowHelp() -{ - dmPrintBanner(stdout, dmProgName, "[options] [input filename] [output filename]"); - dmArgsPrintHelp(stdout, optList, optListN, 0); -} - - -BOOL argHandleOpt(const int optN, char *optArg, char *currArg) -{ - (void) currArg; - - switch (optN) - { - case 0: - argShowHelp(); - exit(0); - break; - - case 1: - dmVerbosity = 0; - break; - - case 2: - optUseZLIB = TRUE; - break; - - case 3: - optCompress = TRUE; - break; - - case 4: - if (!dmGetIntVal(optArg, &optCompressLevel) || - optCompressLevel < 1 || optCompressLevel > 9) - { - dmErrorMsg("Invalid compression level argument '%s', must be 1 .. 9.\n", optArg); - return FALSE; - } - break; - - case 5: - if (!dmGetIntVal(optArg, &optSkip)) - { - dmErrorMsg("Invalid skip value.\n", optArg); - return FALSE; - } - break; - - default: - return FALSE; - } - - return TRUE; -} - - -BOOL argHandleFile(char *currArg) -{ - if (optInFilename == NULL) - optInFilename = currArg; - else - if (optOutFilename == NULL) - optOutFilename = currArg; - else - { - dmErrorMsg("Excess filenames specified.\n"); - return FALSE; - } - return TRUE; -} - - -int dmTestDMZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level) -{ - Uint8 *inBuffer = NULL, *outBuffer = NULL; - DMZLibContext ctx; - int ret; - - (void) level; - - if (compress) - { - ret = dmError(DMERR_NOT_SUPPORTED, - "Compression is not supported with dmzlib.\n"); - goto out; - } - - if ((ret = dmReadDataFile(inFile, "-", &inBuffer, inSize)) != DMERR_OK) - { - dmErrorMsg("Failed to read file.\n"); - goto out; - } - - if ((outBuffer = dmMalloc(SET_TMPBUF_SIZE)) == NULL) - { - ret = dmError(DMERR_MALLOC, - "Malloc failed.\n"); - goto out; - } - - // Initialize decompression structures - if ((ret = dmZLibInitInflate(&ctx)) != DMERR_OK) - goto out; - - ctx.inBuffer = ctx.inBufferStart = inBuffer; - ctx.inBufferEnd = inBuffer + *inSize; - - ctx.outBuffer = ctx.outBufferStart = outBuffer; - ctx.outBufferEnd = outBuffer + SET_TMPBUF_SIZE; - ctx.expandable = TRUE; - - // Attempt decompression - if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) - { - dmErrorMsg("Error parsing ZLIB header: %d, %s\n", ret, dmErrorStr(ret)); - goto out; - } - - if ((ret = dmZLibInflate(&ctx)) != DMERR_OK) - { - dmErrorMsg("Error in ZLIB decompress: %d, %s\n", ret, dmErrorStr(ret)); - goto out; - } - - outBuffer = ctx.outBufferStart; - *outSize = ctx.outBuffer - ctx.outBufferStart; - - if (fwrite(outBuffer, sizeof(Uint8), *outSize, outFile) != *outSize) - { - ret = dmError(DMERR_FWRITE, "File write error.\n"); - goto out; - } - -out: - dmZLibCloseInflate(&ctx); - dmFree(inBuffer); - dmFree(outBuffer); - return ret; -} - - -int dmTestZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level) -{ - Uint8 *inBuffer = NULL, *outBuffer = NULL; - int zret, zinit = FALSE; - int ret = DMERR_OK; - z_stream zstr; - - dmMsg(0, "Operating mode: %s\n", - compress ? "compress" : "decompress"); - - dmMemset(&zstr, 0, sizeof(zstr)); - - if ((inBuffer = malloc(SET_TMPBUF_SIZE)) == NULL || - (outBuffer = malloc(SET_TMPBUF_SIZE)) == NULL) - { - ret = dmError(DMERR_MALLOC, "Malloc failed.\n"); - goto out; - } - - if (compress) - zret = deflateInit(&zstr, level); - else - zret = inflateInit(&zstr); - - if (zret != Z_OK) - { - ret = dmError(DMERR_INIT_FAIL, "Zlib init fail.\n"); - goto out; - } - zinit = TRUE; - - // Initialize compression streams - zret = Z_OK; - *outSize = 0; - while (!feof(inFile) && zret == Z_OK) - { - zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile); - - zstr.next_in = inBuffer; - zstr.next_out = outBuffer; - zstr.avail_out = SET_TMPBUF_SIZE; - zstr.total_out = 0; - - if (compress) - zret = deflate(&zstr, Z_FULL_FLUSH); - else - zret = inflate(&zstr, Z_FULL_FLUSH); - - if (zstr.total_out > 0) - { - *outSize += zstr.total_out; - if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out) - { - ret = dmError(DMERR_FWRITE, - "File write error.\n"); - goto out; - } - } - } - - - switch (zret) - { - case Z_OK: - break; - - case Z_ERRNO: - dmErrorMsg("Error: errno\n"); - break; - - case Z_STREAM_END: - dmErrorMsg("Stream end.\n"); - break; - - default: - ret = dmError(DMERR_COMPRESSION, - "Error: %d, %s\n", zret, zError(zret)); - } - -out: - *inSize = zstr.total_in; - - if (zinit) - { - if (compress) - deflateEnd(&zstr); - else - inflateEnd(&zstr); - } - - dmFree(inBuffer); - dmFree(outBuffer); - - return ret; -} - - -int main(int argc, char *argv[]) -{ - FILE *inFile = NULL, *outFile = NULL; - size_t inSize = 0, outSize = 0; - int ret; - - dmInitProg("testdmzlib", "ZLIB/dmzlib tester", NULL, NULL, NULL); - dmVerbosity = 1; - - if (!dmArgsProcess(argc, argv, optList, optListN, - argHandleOpt, argHandleFile, OPTH_BAILOUT)) - exit(1); - - dmZLibInit(); - - // Input and output files - if (optInFilename == NULL) - inFile = stdin; - else - if ((inFile = fopen(optInFilename, "rb")) == NULL) - { - int res = dmGetErrno(); - dmErrorMsg("Failed to open input file '%s': %s\n", - optInFilename, dmErrorStr(res)); - goto out; - } - - if (optOutFilename == NULL) - outFile = stdout; - else - if ((outFile = fopen(optOutFilename, "wb")) == NULL) - { - int res = dmGetErrno(); - dmErrorMsg("Failed to open output file '%s': %s\n", - optOutFilename, dmErrorStr(res)); - goto out; - } - - if (optSkip > 0 && fseeko(inFile, optSkip, SEEK_CUR) != 0) - { - int res = dmGetErrno(); - dmErrorMsg("Failed to seek in input stream: %s\n", - dmErrorStr(res)); - goto out; - } - - if (optUseZLIB) - ret = dmTestZlib(inFile, outFile, &inSize, &outSize, optCompress, optCompressLevel); - else - ret = dmTestDMZlib(inFile, outFile, &inSize, &outSize, optCompress, optCompressLevel); - - dmMsg(0, "[%d] In %d, out %d bytes.\n", ret, inSize, outSize); - -out: - // Cleanup - if (outFile != NULL) - fclose(outFile); - - if (inFile != NULL) - fclose(inFile); - - dmZLibClose(); - return 0; -} diff -r ba09aa661c72 -r 31010318ea8c tests/dzlibtest.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/dzlibtest.c Sun May 13 04:05:20 2018 +0300 @@ -0,0 +1,332 @@ +#include "dmlib.h" +#include "dmzlib.h" +#include "dmfile.h" +#include "dmargs.h" +#include + + +#define SET_TMPBUF_SIZE (16 * 1024) + +char *optInFilename = NULL, + *optOutFilename = NULL; + +unsigned int optSkip = 0; +unsigned int optCompressLevel = Z_BEST_COMPRESSION; +BOOL optCompress = FALSE, + optUseZLIB = FALSE; + + +static const DMOptArg optList[] = +{ + { 0, '?', "help", "Show this help", OPT_NONE }, + { 1, 'q', "quiet", "Decrease verbosity", OPT_NONE }, + { 2, 'Z', "zlib", "Use ZLIB instead of dmzlib", OPT_NONE }, + { 3, 'c', "compress", "Compress instead of decompressing (ZLIB only)", OPT_NONE }, + { 4, 'l', "level", "Set zlib compression level 1-9", OPT_ARGREQ }, + { 5, 's', "skip", "Skip bytes from input start", OPT_ARGREQ }, +}; + +static const int optListN = sizeof(optList) / sizeof(optList[0]); + + +void argShowHelp() +{ + dmPrintBanner(stdout, dmProgName, "[options] [input filename] [output filename]"); + dmArgsPrintHelp(stdout, optList, optListN, 0); +} + + +BOOL argHandleOpt(const int optN, char *optArg, char *currArg) +{ + (void) currArg; + + switch (optN) + { + case 0: + argShowHelp(); + exit(0); + break; + + case 1: + dmVerbosity = 0; + break; + + case 2: + optUseZLIB = TRUE; + break; + + case 3: + optCompress = TRUE; + break; + + case 4: + if (!dmGetIntVal(optArg, &optCompressLevel) || + optCompressLevel < 1 || optCompressLevel > 9) + { + dmErrorMsg("Invalid compression level argument '%s', must be 1 .. 9.\n", optArg); + return FALSE; + } + break; + + case 5: + if (!dmGetIntVal(optArg, &optSkip)) + { + dmErrorMsg("Invalid skip value.\n", optArg); + return FALSE; + } + break; + + default: + return FALSE; + } + + return TRUE; +} + + +BOOL argHandleFile(char *currArg) +{ + if (optInFilename == NULL) + optInFilename = currArg; + else + if (optOutFilename == NULL) + optOutFilename = currArg; + else + { + dmErrorMsg("Excess filenames specified.\n"); + return FALSE; + } + return TRUE; +} + + +int dmTestDMZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level) +{ + Uint8 *inBuffer = NULL, *outBuffer = NULL; + DMZLibContext ctx; + int ret; + + (void) level; + + if (compress) + { + ret = dmError(DMERR_NOT_SUPPORTED, + "Compression is not supported with dmzlib.\n"); + goto out; + } + + if ((ret = dmReadDataFile(inFile, "-", &inBuffer, inSize)) != DMERR_OK) + { + dmErrorMsg("Failed to read file.\n"); + goto out; + } + + if ((outBuffer = dmMalloc(SET_TMPBUF_SIZE)) == NULL) + { + ret = dmError(DMERR_MALLOC, + "Malloc failed.\n"); + goto out; + } + + // Initialize decompression structures + if ((ret = dmZLibInitInflate(&ctx)) != DMERR_OK) + goto out; + + ctx.inBuffer = ctx.inBufferStart = inBuffer; + ctx.inBufferEnd = inBuffer + *inSize; + + ctx.outBuffer = ctx.outBufferStart = outBuffer; + ctx.outBufferEnd = outBuffer + SET_TMPBUF_SIZE; + ctx.expandable = TRUE; + + // Attempt decompression + if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) + { + dmErrorMsg("Error parsing ZLIB header: %d, %s\n", ret, dmErrorStr(ret)); + goto out; + } + + if ((ret = dmZLibInflate(&ctx)) != DMERR_OK) + { + dmErrorMsg("Error in ZLIB decompress: %d, %s\n", ret, dmErrorStr(ret)); + goto out; + } + + outBuffer = ctx.outBufferStart; + *outSize = ctx.outBuffer - ctx.outBufferStart; + + if (fwrite(outBuffer, sizeof(Uint8), *outSize, outFile) != *outSize) + { + ret = dmError(DMERR_FWRITE, "File write error.\n"); + goto out; + } + +out: + dmZLibCloseInflate(&ctx); + dmFree(inBuffer); + dmFree(outBuffer); + return ret; +} + + +int dmTestZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level) +{ + Uint8 *inBuffer = NULL, *outBuffer = NULL; + int zret, zinit = FALSE; + int ret = DMERR_OK; + z_stream zstr; + + dmMsg(0, "Operating mode: %s\n", + compress ? "compress" : "decompress"); + + dmMemset(&zstr, 0, sizeof(zstr)); + + if ((inBuffer = malloc(SET_TMPBUF_SIZE)) == NULL || + (outBuffer = malloc(SET_TMPBUF_SIZE)) == NULL) + { + ret = dmError(DMERR_MALLOC, "Malloc failed.\n"); + goto out; + } + + if (compress) + zret = deflateInit(&zstr, level); + else + zret = inflateInit(&zstr); + + if (zret != Z_OK) + { + ret = dmError(DMERR_INIT_FAIL, "Zlib init fail.\n"); + goto out; + } + zinit = TRUE; + + // Initialize compression streams + zret = Z_OK; + *outSize = 0; + while (!feof(inFile) && zret == Z_OK) + { + zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile); + + zstr.next_in = inBuffer; + zstr.next_out = outBuffer; + zstr.avail_out = SET_TMPBUF_SIZE; + zstr.total_out = 0; + + if (compress) + zret = deflate(&zstr, Z_FULL_FLUSH); + else + zret = inflate(&zstr, Z_FULL_FLUSH); + + if (zstr.total_out > 0) + { + *outSize += zstr.total_out; + if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out) + { + ret = dmError(DMERR_FWRITE, + "File write error.\n"); + goto out; + } + } + } + + + switch (zret) + { + case Z_OK: + break; + + case Z_ERRNO: + dmErrorMsg("Error: errno\n"); + break; + + case Z_STREAM_END: + dmErrorMsg("Stream end.\n"); + break; + + default: + ret = dmError(DMERR_COMPRESSION, + "Error: %d, %s\n", zret, zError(zret)); + } + +out: + *inSize = zstr.total_in; + + if (zinit) + { + if (compress) + deflateEnd(&zstr); + else + inflateEnd(&zstr); + } + + dmFree(inBuffer); + dmFree(outBuffer); + + return ret; +} + + +int main(int argc, char *argv[]) +{ + FILE *inFile = NULL, *outFile = NULL; + size_t inSize = 0, outSize = 0; + int ret; + + dmInitProg("testdmzlib", "ZLIB/dmzlib tester", NULL, NULL, NULL); + dmVerbosity = 1; + + if (!dmArgsProcess(argc, argv, optList, optListN, + argHandleOpt, argHandleFile, OPTH_BAILOUT)) + exit(1); + + dmZLibInit(); + + // Input and output files + if (optInFilename == NULL) + inFile = stdin; + else + if ((inFile = fopen(optInFilename, "rb")) == NULL) + { + int res = dmGetErrno(); + dmErrorMsg("Failed to open input file '%s': %s\n", + optInFilename, dmErrorStr(res)); + goto out; + } + + if (optOutFilename == NULL) + outFile = stdout; + else + if ((outFile = fopen(optOutFilename, "wb")) == NULL) + { + int res = dmGetErrno(); + dmErrorMsg("Failed to open output file '%s': %s\n", + optOutFilename, dmErrorStr(res)); + goto out; + } + + if (optSkip > 0 && fseeko(inFile, optSkip, SEEK_CUR) != 0) + { + int res = dmGetErrno(); + dmErrorMsg("Failed to seek in input stream: %s\n", + dmErrorStr(res)); + goto out; + } + + if (optUseZLIB) + ret = dmTestZlib(inFile, outFile, &inSize, &outSize, optCompress, optCompressLevel); + else + ret = dmTestDMZlib(inFile, outFile, &inSize, &outSize, optCompress, optCompressLevel); + + dmMsg(0, "[%d] In %d, out %d bytes.\n", ret, inSize, outSize); + +out: + // Cleanup + if (outFile != NULL) + fclose(outFile); + + if (inFile != NULL) + fclose(inFile); + + dmZLibClose(); + return 0; +} diff -r ba09aa661c72 -r 31010318ea8c tests/efu.c --- a/tests/efu.c Sun May 13 01:19:21 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,511 +0,0 @@ -#include "dmlib.h" -#include "dmargs.h" -#include "dmvecmat.h" -#include "dmimage.h" -#include "dmtext.h" -#include "dmperlin.h" -#include - -#define DM_COLORS (256) - -char *optFontFile = "font.ttf", - *optBitmapFilename = "tnsp.png"; -BOOL optBenchmark = FALSE; -int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; -int optScrWidth = 640, optScrHeight = 480, optFontSize = 20, optScrDepth = 32; -int optBenchmarkLen = 20; -DMPerlinContext perlinCtx; - -static const DMOptArg optList[] = -{ - { 0, '?', "help", "Show this help", OPT_NONE }, - { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, - { 3, 'f', "full", "Fullscreen", OPT_NONE }, - { 4, 'h', "hw", "Use SDL hardware surface", OPT_NONE }, - { 5, 's', "size", "Initial window size/resolution -s 640x480", OPT_ARGREQ }, - { 6, 'd', "depth", "Color depth of mode/window in bits (8/15/16/32)", OPT_ARGREQ }, - { 7, 'b', "bench", "Run in benchmark mode", OPT_NONE }, -}; - -const int optListN = sizeof(optList) / sizeof(optList[0]); - - -void argShowHelp() -{ - dmArgsPrintHelp(stdout, optList, optListN, 0); -} - - -BOOL argHandleOpt(const int optN, char *optArg, char *currArg) -{ - switch (optN) { - case 0: - argShowHelp(); - exit(0); - break; - - case 2: - dmVerbosity++; - break; - - case 3: - optVFlags |= SDL_FULLSCREEN; - break; - - case 6: - if (optArg) - optScrDepth = atoi(optArg); - break; - - case 5: - { - int w, h; - if (sscanf(optArg, "%dx%d", &w, &h) == 2) - { - if (w < 320 || h < 200 || w > 3200 || h > 3200) - { - dmErrorMsg("Invalid width or height: %d x %d\n", w, h); - return FALSE; - } - optScrWidth = w; - optScrHeight = h; - } - else - { - dmErrorMsg("Invalid size argument '%s'.\n", optArg); - return FALSE; - } - } - break; - - case 7: - optBenchmark = TRUE; - break; - - default: - dmErrorMsg("Unknown option '%s'.\n", currArg); - return FALSE; - } - - return TRUE; -} - - -void DM_MakePalette(SDL_Surface *scr) -{ - SDL_Color pal[DM_COLORS]; - int n; - - for (n = 0; n < 256; n++) - { - pal[n].r = n; - pal[n].g = n; - pal[n].b = n; - } - - SDL_SetColors(scr, pal, 0, DM_COLORS); -} - - -void DM_PrintRect(FILE *f, SDL_Rect *r) -{ - fprintf(f, "SDL_Rect <%d, %d : %d, %d>\n", - r->x, r->y, r->w, r->h); -} - -BOOL DM_InitializeVideo(SDL_Surface **screen) -{ - *screen = SDL_SetVideoMode(optScrWidth, optScrHeight, optScrDepth, optVFlags | SDL_RESIZABLE); - if (*screen == NULL) - { - dmErrorMsg("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); - return FALSE; - } - -#if 0 - SDL_Rect r; - r.x = -50; - r.y = 50; - r.w = 700; - r.h = 300; - DM_PrintRect(stderr, &r); - SDL_SetClipRect(*screen, &r); - DM_PrintRect(stderr, &r); - DM_PrintRect(stderr, &((*screen)->clip_rect)); -#endif - - return TRUE; -} - -void DM_Random(SDL_Surface *screen, int q) -{ - Uint8 *pix = screen->pixels; - int xc, yc; - - for (yc = 0; yc < screen->h; yc++) - { - Uint8 *dp = pix; - - for (xc = 0; xc < screen->w; xc++) - *dp++ = yc + (xc ^ q) + (yc & q); - - pix += screen->pitch; - } -} - - -#define QWIDTH 256 -#define QHEIGHT 160 - -typedef Uint8 DMBlockMap[QHEIGHT][QWIDTH]; - - -void dmMakeBumpMap(DMBlockMap map, DMFloat q, DMFloat m) -{ - int x, y; - for (y = 0; y < QHEIGHT; y++) - for (x = 0; x < QWIDTH; x++) - { - DMFloat f = 0.40f + dmPerlinNoise2D(&perlinCtx, x, y, 1.1f, q, 2); - map[y][x] = (int) (dmClamp10(f) * m); - } -} - - -void dmShadowTraceHeightMap(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light) -{ - int i, j; - - light->z = 150; - - for (j = 0; j < QHEIGHT; j++) - for (i = 0; i < QWIDTH; i++) - { - DMVector vr, vl, va; - DMFloat vrayLen, vfactor; - int vlen; - BOOL wasHit; - - // Perform shadow occlusion via simplistic raytracing - vr.x = i; - vr.y = j; - vr.z = 200; //light->z; // - 10.0; - - // Calculate light vector vector - dm_vector_sub_r(&vl, &vr, light); - vrayLen = dm_vector_length(&vl); - -#if 1 - dm_vector_copy(&va, &vl); - dm_vector_normalize(&va); - dm_vector_copy(&vr, light); - - vlen = 0; - wasHit = FALSE; - do - { - float h; - - // If ray is inside the heightmap, get value - if (vr.x >= 0 && vr.y >= 0 && vr.x < QWIDTH && vr.y < QHEIGHT) - h = pheightMap[(int) vr.y][(int) vr.x]; - else - break; - - // Check for hits - if (h > vr.z) - wasHit = TRUE; - else - { - // Move forwards - dm_vector_add(&vr, &va); - vlen++; - } - } - while (!wasHit && vlen <= vrayLen); - - // Check if the ray hit something, e.g. is this point occluded? - if (wasHit && vlen < vrayLen) - { - vfactor = vlen * 0.05; - } - else - vfactor = vlen * 0.001; -#endif - -#if 0 - { - /* Calculate light's intensity based on the angle it "hits" - * - * 1) Calculate the vectors that form the imaginary "plane" - * 2) Cross-product -> normal vector of the plane - * 2) Normalize the normal vector - * 3) Calculate light vector's hit angle by dot product - */ - DMVector v1, v2; - DMFloat c; - - v1.x = 2.0f; - v1.y = 0.0f; - v1.z = (DMFloat) (pheightMap[j][i] - pheightMap[j][i + 1]); - - v2.x = 0.0f; - v2.y = 2.0f; - v2.z = (DMFloat) (pheightMap[j][i] - pheightMap[j + 1][i]); - - dm_vector_cross(&vr, &v1, &v2); - dm_vector_normalize(&vr); - dm_vector_normalize(&vl); - c = dm_vector_dot(&vl, &vr); - - vrayLen = 255 - (vrayLen * 0.1) * vrayLen + (c * 128.0f) + (vfactor * vfactor * 1255); - } -#else - vrayLen = 255 - vrayLen * vrayLen * (vfactor * vfactor); - if (vrayLen < 0) vrayLen = 0; - vrayLen -= pheightMap[j][i]; - -#endif - - // Clip result - if (vrayLen < 0) - vrayLen = 0; - else if (vrayLen > 255.0f) - vrayLen = 255.0f; - - lightMap[j][i] = vrayLen; - } -} - -#define Q_KERNEL(x, y, dist, coeff) ( \ - in[(y) - (dist)][(x)] * (coeff) + \ - in[(y) + (dist)][(x)] * (coeff) + \ - in[(y)][(x) - (dist)] * (coeff) + \ - in[(y)][(x) + (dist)] * (coeff) + \ - in[(y) - (dist)][(x) - (2)] * (coeff) + \ - in[(y) - (dist)][(x) + (2)] * (coeff) + \ - in[(y) + (dist)][(x) - (2)] * (coeff) + \ - in[(y) + (dist)][(x) + (2)] * (coeff)) - -#define Q_VALUES(coeff) (8 * (coeff)) - -void dmSmoothMap(DMBlockMap out, DMBlockMap in) -{ - int x, y; - - for (y = 0; y < QHEIGHT; y++) - for (x = 0; x < QWIDTH; x++) - { - if (y >= 3 && x >= 3 && y < QHEIGHT - 3 && x < QWIDTH - 3) - out[y][x] = ( - Q_KERNEL(x, y, 3, 1) + - Q_KERNEL(x, y, 2, 2) + - Q_KERNEL(x, y, 1, 4) + - in[y][x] * 16) / - (16 + Q_VALUES(1) + Q_VALUES(2) + Q_VALUES(4)); - else - out[y][x] = in[y][x]; - } -} - -int main(int argc, char *argv[]) -{ - SDL_Surface *screen = NULL, *bmap = NULL, *logo = NULL; - TTF_Font *font = NULL; - SDL_Color fontcol={255,155,155,0}; - SDL_Event event; - int mouseX, mouseY, res; - BOOL initSDL = FALSE, initTTF = FALSE, exitFlag, showMap = FALSE; - DMResource *file; - - if (!dmArgsProcess(argc, argv, optList, optListN, - argHandleOpt, NULL, OPTH_BAILOUT)) - exit(1); - - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) - { - dmErrorMsg("Could not initialize SDL: %s\n", SDL_GetError()); - goto error_exit; - } - initSDL = TRUE; - - - if (TTF_Init() < 0) - { - dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); - goto error_exit; - } - initTTF = TRUE; - - font = TTF_OpenFont(optFontFile, optFontSize); - if (font == NULL) - { - dmErrorMsg("Could not load TTF font '%s' (%d): %s\n", - optFontFile, optFontSize, SDL_GetError()); - goto error_exit; - } - TTF_SetFontStyle(font, TTF_STYLE_NORMAL); - - - if ((res = dmf_create_stdio(optBitmapFilename, "rb", &file)) != DMERR_OK) - { - dmErrorMsg("Could not open resource file '%s'.\n", optBitmapFilename); - goto error_exit; - } - logo = dmLoadImage(file); - dmf_close(file); - if (logo == NULL) - { - dmErrorMsg("Could not load image file '%s'.\n", optBitmapFilename); - goto error_exit; - } - - - if (optBenchmark) - { - screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); - if (screen == NULL) - { - dmErrorMsg("Could not create screen surface.\n"); - goto error_exit; - } - - dmMsg(0, "Benchmark mode, not opening window.\n"); - } - else - { - if (!DM_InitializeVideo(&screen)) - goto error_exit; - - SDL_WM_SetCaption("Halleluja", "DMT"); - } - - dmPerlinInit(&perlinCtx, 1234); - - bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); - DM_MakePalette(bmap); - DM_Random(bmap, 15); - - DMVector light; - DMBlockMap heightMap; - light.x = light.y = 128; - light.z = 128; - dmMakeBumpMap(heightMap, 0.06, 254); - - - - int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; - exitFlag = FALSE; - - if (optBenchmark) - dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); - - while (!exitFlag) - { - if (!optBenchmark) - { - while (SDL_PollEvent(&event)) - switch (event.type) - { - case SDL_KEYDOWN: - switch (event.key.keysym.sym) - { - case SDLK_ESCAPE: exitFlag = TRUE; break; - - case SDLK_F1: - DM_Random(bmap, (SDL_GetTicks() / 10) % 1000); - break; - - case SDLK_F5: - showMap = !showMap; - break; - - default: - break; - } - - break; - - case SDL_VIDEORESIZE: - optScrWidth = event.resize.w; - optScrHeight = event.resize.h; - - if (!DM_InitializeVideo(&screen)) - goto error_exit; - - break; - - case SDL_VIDEOEXPOSE: - break; - - case SDL_QUIT: - exit(0); - } - - SDL_GetMouseState(&mouseX, &mouseY); - light.x = ((DMFloat) mouseX * QWIDTH) / (DMFloat) optScrWidth; - light.y = ((DMFloat) mouseY * QHEIGHT) / (DMFloat) optScrHeight; - } - - if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) - { - dmErrorMsg("Can't lock surface.\n"); - goto error_exit; - } - - - if (showMap) - memcpy(bmap->pixels, heightMap, QWIDTH * QHEIGHT); - else - dmShadowTraceHeightMap(bmap->pixels, logo->pixels, &light); - dmScaledBlitSurfaceAny(bmap, 0, 0, screen->w, screen->h, screen, DMD_NONE); - - - - if (!optBenchmark) - { - dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", - (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); - - if (SDL_MUSTLOCK(screen) != 0) - SDL_UnlockSurface(screen); - - SDL_Flip(screen); - SDL_Delay(20); - } - - endTime = SDL_GetTicks(); - numFrames++; - - if (optBenchmark) - { - if (endTime - startTime > optBenchmarkLen * 1000) - exitFlag = TRUE; - } - } - - // Print benchmark results - dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", - numFrames, endTime - startTime, - (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); - - -error_exit: - dmMsg(0, "Shutting down dmlib.\n"); - if (screen) - SDL_FreeSurface(screen); - - if (logo) - SDL_FreeSurface(logo); - - if (font) - TTF_CloseFont(font); - - if (initSDL) - SDL_Quit(); - - if (initTTF) - TTF_Quit(); - - return 0; -} diff -r ba09aa661c72 -r 31010318ea8c tests/testeval.sh --- a/tests/testeval.sh Sun May 13 01:19:21 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -#!/bin/sh -ETEST="./evaltest -f -e" -CTEST=./frm - -echo " -2 << 1 + 9 >> 3 * 5 -(2 << 1 + 9) >> 3 * 5 -2 << 1 + 31 >> 3 * 5 -5 << 1 -8 << 3 * 5 -3200/2 << 5 -3200/(2 << 5) -15+5/3 -(15+5)/3 -5*-3 --64 + 5 << 3 * 3 - 1 --64 + 5 << 3 + 3 - 1 -3 * -1 + 5 << 1 -7 + 3 << 2 * 3 -7 * 3 << 4 - 3 -127&127 + 12&3 -1+3*5&7 -35&3 + 2 -9+5&(3 + 7) -(5*(7&3) + 1) -123045 >> 1 & 60 -~60 -3 + ~5 --~128 -5-~127 -" | while read f; do - if test "x$f" != "x"; then - printf "#include \nint main(int argc, char *argv[])\n{\n (void) argc; (void) argv;\n printf(\"%%1.5f\\\\n\", (double) (%s));\n return 0;\n}\n" "$f" > "$CTEST.c" - gcc -g -o "$CTEST" "$CTEST.c" - if test $? -eq 0; then - RES1=`$CTEST` - RES2=`$ETEST "$f"` - if test "$RES1" != "$RES2"; then - echo "---------------------------------------------" - echo "RESULT MISMATCH: '$f'" - echo " C test: $RES1" - echo " eval: $RES2" - cat "$CTEST.c" - elif test "x$1" != "x"; then - echo "---------------------------------------------" - echo "FORMULA: '$f'" - echo " C test: $RES1" - echo " eval: $RES2" - fi - fi - fi -done -#rm -f "$CTEST" "$CTEST.c" diff -r ba09aa661c72 -r 31010318ea8c tests/tnsp.png Binary file tests/tnsp.png has changed