diff --git a/source/build/include/build.h b/source/build/include/build.h index d96163691..fdd03d0b4 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -1047,7 +1047,6 @@ const uint8_t* tilePtr(int num); // read-only uint8_t* tileData(int num); // writable. bool tileLoad(int16_t tilenume); void tileLoadData(int16_t tilenume, int32_t dasiz, char *buffer); -int32_t tileCRC(int16_t tileNum); void tileUpdatePicSiz(int32_t picnum); int32_t qloadkvx(int32_t voxindex, const char *filename); diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index f2ea81597..afda0ba03 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -204,82 +204,6 @@ static int32_t check_tile(const char *defcmd, int32_t tile, const scriptfile *sc return 0; } -static void tile_from_truecolpic(int32_t tile, const palette_t *picptr, int32_t alphacut) -{ - vec2_16_t const siz = tilesiz[tile]; - int32_t i, j, tsiz = siz.x * siz.y; - - maybe_grow_buffer(&faketilebuffer, &faketilebuffersiz, tsiz); - - getclosestcol_flush(); - - for (j = 0; j < siz.y; ++j) - { - int const ofs = j * siz.x; - for (i = 0; i < siz.x; ++i) - { - palette_t const *const col = &picptr[ofs + i]; - faketilebuffer[(i * siz.y) + j] = - (col->f < alphacut) ? 255 : getclosestcol_lim(col->b, col->g, col->r, 254); - } - } - - tileSetData(tile, tsiz, faketilebuffer); -} - -static int32_t Defs_LoadTileIntoBuffer(int32_t const tile) -{ - vec2_16_t const siz = tilesiz[tile]; - int32_t const tsiz = siz.x * siz.y; - - if (EDUKE32_PREDICT_FALSE(tilesiz[tile].x <= 0 || tilesiz[tile].y <= 0)) - return 0; - - maybe_grow_buffer(&faketilebuffer, &faketilebuffersiz, tsiz); - - tileLoadData(tile, tsiz, faketilebuffer); - - return tsiz; -} - -static void Defs_ApplyPaletteToTileBuffer(int32_t const tsiz, int32_t const pal) -{ - for (bssize_t i = 0; i < tsiz; i++) - faketilebuffer[i] = palookup[pal][faketilebuffer[i]]; -} - -static int32_t Defs_ImportTileFromTexture(char const * const fn, int32_t const tile, int32_t const alphacut, int32_t istexture) -{ - if (check_file_exist(fn)) - return -1; - - - FTexture* tex = FTexture::GetTexture(fn); - int32_t xsiz = tex->GetWidth(), ysiz = tex->GetHeight(); - - if (EDUKE32_PREDICT_FALSE(xsiz <= 0 || ysiz <= 0)) - return -2; - - if (!(paletteloaded & PALETTE_MAIN)) - return -3; - - if (videoGetRenderMode() < REND_POLYMOST) - { - tileSetSize(tile, tex->GetWidth(), tex->GetHeight()); - auto image = tex->GetBgraBitmap(nullptr, nullptr); - tile_from_truecolpic(tile, (const palette_t*)image.GetPixels(), alphacut); - } - -#ifdef USE_OPENGL - if (istexture) - hicsetsubsttex(tile, 0, fn, (float)(255-alphacut) * (1.f/255.f), 1.0f, 1.0f, 1.0, 1.0, HICR_ARTIMMUNITY); -#else - UNREFERENCED_PARAMETER(istexture); -#endif - - return 0; -} - #undef USE_DEF_PROGRESS #if defined _WIN32 || defined HAVE_GTK2 # define USE_DEF_PROGRESS @@ -635,32 +559,8 @@ static int32_t defsparser(scriptfile *script) script->filename, scriptfile_getlinum(script,cmdtokptr)); break; } - - buildvfs_kfd const fil = kopen4load(fn, 0); - if (fil == buildvfs_kfd_invalid) - break; - - artheader_t local; - int32_t headerval = artReadHeader(fil, fn, &local); - if (headerval != 0) - { - kclose(fil); - break; - } - - if (havetile) - { - if (!check_tile("artfile", tile, script, cmdtokptr)) - { - local.tilestart = tile; - local.tileend = tile + local.numtiles - 1; - } - } - - artReadManifest(fil, &local); - artPreloadFile(fil, &local); - - kclose(fil); + if (!check_tile("artfile", tile, script, cmdtokptr)) + TileFiles.LoadArtFile(fn, false, tile); } break; case T_SETUPTILE: @@ -854,7 +754,7 @@ static int32_t defsparser(scriptfile *script) break; } - int32_t const texstatus = Defs_ImportTileFromTexture(fn, tile, alphacut, istexture); + int32_t const texstatus = tileImportFromTexture(fn, tile, alphacut, istexture); if (texstatus == -3) initprintf("Error: No palette loaded, in tilefromtexture definition near line %s:%d\n", script->filename, scriptfile_getlinum(script,texturetokptr)); @@ -885,9 +785,11 @@ static int32_t defsparser(scriptfile *script) char *blockend; int32_t tile = -1, source; int32_t havetile = 0, havexoffset = 0, haveyoffset = 0; - int32_t xoffset = 0, yoffset = 0; + int32_t xoffset = -1024, yoffset = -1024; int32_t flags = 0; int32_t tsiz = 0; + int32_t temppal = -1; + int32_t tempsource = -1; static const tokenlist copytiletokens[] = { @@ -911,13 +813,10 @@ static int32_t defsparser(scriptfile *script) { case T_TILE: { - int32_t tempsource; scriptfile_getsymbol(script,&tempsource); if (check_tile("copytile", tempsource, script, cmdtokptr)) break; - if ((tsiz = Defs_LoadTileIntoBuffer(tempsource)) <= 0) - break; source = tempsource; havetile = 1; @@ -925,7 +824,6 @@ static int32_t defsparser(scriptfile *script) } case T_PAL: { - int32_t temppal; scriptfile_getsymbol(script,&temppal); // palettize self case @@ -933,8 +831,6 @@ static int32_t defsparser(scriptfile *script) { if (check_tile("copytile", source, script, cmdtokptr)) break; - if ((tsiz = Defs_LoadTileIntoBuffer(source)) <= 0) - break; havetile = 1; } @@ -944,8 +840,6 @@ static int32_t defsparser(scriptfile *script) MAXPALOOKUPS-RESERVEDPALS-1); break; } - - Defs_ApplyPaletteToTileBuffer(tsiz, temppal); break; } case T_XOFFSET: @@ -968,27 +862,14 @@ static int32_t defsparser(scriptfile *script) if (check_tile("copytile", tile, script, cmdtokptr)) break; - if (havetile) - { - tileSetData(tile, tsiz, faketilebuffer); - } - else // if !havetile, we have never confirmed a valid source + if (!havetile) { + // if !havetile, we have never confirmed a valid source if (check_tile("copytile", source, script, cmdtokptr)) break; } - if (tsiz <= 0) - { - tileDelete(tile); - break; - } - - tileSetSize(tile, tilesiz[source].x, tilesiz[source].y); - picanm[tile].xofs = havexoffset ? clamp(xoffset, -128, 127) : picanm[source].xofs; - picanm[tile].yofs = haveyoffset ? clamp(yoffset, -128, 127) : picanm[source].yofs; - picanm[tile].sf = (picanm[tile].sf & ~PICANM_MISC_MASK) | (picanm[source].sf & PICANM_MISC_MASK) | flags; - + tileCopy(tile, tempsource, temppal, xoffset, yoffset, flags); } break; case T_IMPORTTILE: @@ -1002,7 +883,7 @@ static int32_t defsparser(scriptfile *script) if (check_tile("importtile", tile, script, cmdtokptr)) break; - int32_t const texstatus = Defs_ImportTileFromTexture(fn, tile, 255, 0); + int32_t const texstatus = tileImportFromTexture(fn, tile, 255, 0); if (texstatus == -3) initprintf("Error: No palette loaded, in importtile definition near line %s:%d\n", script->filename, scriptfile_getlinum(script,cmdtokptr)); diff --git a/source/build/src/tiles.cpp b/source/build/src/tiles.cpp index e1f41d82d..9344b903a 100644 --- a/source/build/src/tiles.cpp +++ b/source/build/src/tiles.cpp @@ -684,31 +684,6 @@ void tileLoadData(int16_t tilenume, int32_t dasiz, char *buffer) } -int32_t tileCRC(int16_t tileNum) -{ - char *data; - - if ((unsigned)tileNum >= (unsigned)MAXTILES) - return 0; - int const dasiz = tilesiz[tileNum].x * tilesiz[tileNum].y; - if (dasiz <= 0) - return 0; - - data = (char *)Xmalloc(dasiz); - tileLoadData(tileNum, dasiz, data); - - int32_t crc = Bcrc32((unsigned char *)data, (unsigned int)dasiz, 0); - - Xfree(data); - - return crc; -} - -// -// allocatepermanenttile -// - - // // copytilepiece // diff --git a/source/common/textures/buildtiles.cpp b/source/common/textures/buildtiles.cpp index c1273c8fa..f240b21a3 100644 --- a/source/common/textures/buildtiles.cpp +++ b/source/common/textures/buildtiles.cpp @@ -403,6 +403,101 @@ void BuildFiles::tileSetExternal(int tilenum, int width, int height, uint8_t* da //========================================================================== // +// Returns checksum for a given tile +// +//========================================================================== + +int32_t tileCRC(int tileNum) +{ + if ((unsigned)tileNum >= (unsigned)MAXTILES) return 0; + auto tile = TileFiles.tiles[tileNum]; + if (!tile ||tile->GetUseType() != FTexture::Art) return 0; // only consider original ART tiles. + auto pixels = tile->Get8BitPixels(); + if (!pixels) return 0; + int size = tile->GetWidth() * tile->GetHeight(); + if (size == 0) return 0; + return crc32(0, (const Bytef*)pixels, size); +} + + +//========================================================================== +// +// Import a tile from an external image. +// This has been signifcantly altered so it may not cover everything yet. +// +//========================================================================== + +int tileImportFromTexture(const char* fn, int tilenum, int alphacut, int istexture) +{ + FTexture* tex = FTexture::GetTexture(fn); + if (tex == nullptr) return -1; + tex->alphaThreshold = 255 - alphacut; + + int32_t xsiz = tex->GetWidth(), ysiz = tex->GetHeight(); + + if (xsiz <= 0 || ysiz <= 0) + return -2; + + TileFiles.AddTile(tilenum, tex); + +#if 0 + // Does this make any difference when the texture gets *properly* inserted into the tile array? + if (istexture) + hicsetsubsttex(tile, 0, fn, (float)(255 - alphacut) * (1.f / 255.f), 1.0f, 1.0f, 1.0, 1.0, 0); +#endif + return 0; + +} + +//========================================================================== +// +// Copies a tile into another and optionally translates its palette. +// +//========================================================================== + +void tileCopy(int tile, int source, int pal, int xoffset, int yoffset, int flags) +{ + // Todo. Since I do not know if some mod needs this it's of low priority now. + // Let's get things working first. + picanm_t* picanm = nullptr; + picanm_t* sourceanm = nullptr; + + if (pal == -1 && tile == source) + { + // Only modify the picanm info. + FTexture* tex = TileFiles.tiles[tile]; + if (!tex) return; + picanm = &tex->PicAnim; + sourceanm = picanm; + } + else + { + if (source == -1) source = tile; + FTexture* tex = TileFiles.tiles[source]; + if (!tex) return; + sourceanm = &tex->PicAnim; + + TArray buffer(tex->GetWidth() * tex->GetHeight(), true); + tex->Create8BitPixels(buffer.Data()); + + if (pal != -1) + { + auto remap = palookup[pal]; + for (auto& pixel : buffer) + { + pixel = palookup[pal][pixel]; + } + } + tex = new FLooseTile(buffer, tex->GetWidth(), tex->GetHeight()); + picanm = &tex->PicAnim; + TileFiles.AddTile(tile, tex); + } + + picanm->xofs = xoffset != -1024 ? clamp(xoffset, -128, 127) : sourceanm->xofs; + picanm->yofs = yoffset != -1024 ? clamp(yoffset, -128, 127) : sourceanm->yofs; + picanm->sf = (picanm->sf & ~PICANM_MISC_MASK) | (sourceanm->sf & PICANM_MISC_MASK) | flags; +} + // // //========================================================================== diff --git a/source/common/textures/textures.h b/source/common/textures/textures.h index 077f44160..ab94f7515 100644 --- a/source/common/textures/textures.h +++ b/source/common/textures/textures.h @@ -517,6 +517,9 @@ struct BuildFiles }; +int tileCRC(int tileNum); +int tileImportFromTexture(const char* fn, int tilenum, int alphacut, int istexture); +void tileCopy(int tile, int tempsource, int temppal, int xoffset, int yoffset, int flags); void tileSetDummy(int tile, int width, int height); void tileDelete(int tile); extern BuildFiles TileFiles;