Skip to content

Commit

Permalink
implement c module, refract code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSizuku committed Nov 19, 2024
1 parent 39d87e1 commit 1127dc2
Show file tree
Hide file tree
Showing 21 changed files with 827 additions and 655 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
mkdir build
export PATH=${MINGWSDK}/bin:$PATH
export CC=${{ matrix.arch.prefix }}-w64-mingw32-clang
export WINRES=${{ matrix.arch.prefix }}-w64-mingw32-windres
export WINDRES=${{ matrix.arch.prefix }}-w64-mingw32-windres
export BUILD_DIR=build
export BUILD_TYPE=MinSizeRel
bash script/build_mingw.sh
Expand Down
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ add_custom_command(
# define tileviewer
set(CMAKE_CXX_STANDARD 11)
set(TILEVIEWER_CODE
src/core_app.cpp
src/core_solver.cpp
src/plugin_builtin.c
src/plugin_lua.c
src/ui_top.cpp
src/ui_menu.cpp
src/ui_config.cpp
src/ui_tile.cpp
src/core_app.cpp
src/core_decode.c
src/core_plugin.c
)
add_executable(${PROJECT_NAME}
${TILEVIEWER_CODE}
Expand Down
36 changes: 23 additions & 13 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/yurisizuku/TileViewer?color=green&label=TileViewer) ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/YuriSizuku/TileViewer/build_win.yml?label=win(x86|x64)&style=flat-square) ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/YuriSizuku/TileViewer/build_linux.yml?label=linux(x86|x64|arm32|arm64)&style=flat-square)

☘️ A cross platform tool to visulize and analyze texture (usually used for console game font) by tiles. It is inspired by `Crystal Tile 2`, and more flexible with custom lua script (for more complex situation, such as swizzle) and command line support.
☘️ A cross platform tool to visulize and analyze texture (usually used for console game font) by tiles. It is inspired by `Crystal Tile 2`, and more flexible with custom lua script or C plugin (for more complex situation, such as swizzle) and command line support.

Also, it supports for droping file, save decoded image and show cursor moving in tiles. Futhermore, the window is flexible for changing size and support for zooming in and out with converting the client coordinate to logical coordinate.

Expand Down Expand Up @@ -156,22 +156,31 @@ m2 --> c2

### (2) C plugin

Implement these function for C decoder plugin, see `src/core_decode.c` in detail
Implement these function for C decoder plugin, see `src/plugin.h` in detail.

``` C

struct tile_decoder_t
{
REQUIRED CB_decode_open open;
REQUIRED CB_decode_close close;
REQUIRED CB_decode_pixel decode;
OPTIONAL CB_decode_parse pre;
OPTIONAL CB_decode_parse post;
void* context;
const char *msg; // for store decoder failed msg
REQUIRED CB_decode_open open; // open the decoder when loading decoder
REQUIRED CB_decode_close close; // close the decoder when changing decoder
REQUIRED CB_decode_pixel decode; // decode each pixel (fill the (i, x, y) pixel)
OPTIONAL CB_decode_parse pre; // before decoding whole tiles (usually make some tmp values here)
OPTIONAL CB_decode_parse post; // after decoding whole tiles(usually clean some tmp values here)
void* context; // opaque pointer for decoder context, user defined struct
const char *msg; // for passing log informations to log window
};
```

For example, to built plugin `asset/png.c`

``` sh
mkdir -p build_mingw64/plugin
. script/fetch_depend.sh
fetch_stb
gcc -g -Idepend/stb-lastest -Isrc -fPIC -shared asset/plugin/plugin_png.c -o build_mingw64/plugin/png.dll
```

### (3) Lua plugin

Use `log` to print values in logwindow, implement `decode_pre`, `decode_pixel` and `decode_post` to decode tiles;
Expand Down Expand Up @@ -322,17 +331,18 @@ export DOCKER_ARCH=aarch64 BUILD_DIR=build_linuxa64_docker BUILD_TYPE=MinSizeRel
## Roadmap

* Core
* [x] decoder interface with different plugin (builtin, lua)
* [x] built-in decoder, 2|4|8bpp, 16bpp(rgb565), 24bpp(rgb888), 32bpp(rgba8888)
* [x] extern lua decoder api implement ([v0.2](https://github.com/YuriSizuku/TileViewer/releases/tag/v0.2))
* [x] decoder interface with different plugin (builtin, lua, C)
* [x] plugin built-in decoder, 2|4|8bpp, 16bpp(rgb565), 24bpp(rgb888), 32bpp(rgba8888) ([v0.1](https://github.com/YuriSizuku/TileViewer/releases/tag/v0.2))
* [x] plugin lua decoder api implement ([v0.2](https://github.com/YuriSizuku/TileViewer/releases/tag/v0.2))
* [x] plugin C decoder (dll, implement) ([v0.3](https://github.com/YuriSizuku/TileViewer/releases/tag/v0.3))
* [ ] multi thread decoding, rendering

* UI
* [x] start up with hello world, cmake structure for the project
* [x] inital layout, left config view, right tile view, top menu, bottom status
* [x] select and render tiles in real time when format changes
* [x] scale render tile images (zoom in/out) ([v0.1.5](https://github.com/YuriSizuku/TileViewer/releases/tag/v0.1.2))
* [ ] color palette load, save editor
* [ ] color palette load, save editor (partly sovled by plugin)

* Build
* [x] redirect log message to log window
Expand Down
4 changes: 2 additions & 2 deletions asset/icon/icon.rc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ BEGIN
BEGIN
VALUE "CompanyName", "YuriSizuku"
VALUE "FileDescription", "A cross platform tool to view and analyze tiles or textures"
VALUE "FileVersion", "0.1"
VALUE "FileVersion", "0.3"
VALUE "InternalName", "TileViewer"
VALUE "OriginalFilename", "TileViewer.exe"
VALUE "ProductName", "TileViewer"
VALUE "ProductVersion", "0.1"
VALUE "ProductVersion", "0.3"
END
END
BLOCK "VarFileInfo"
Expand Down
132 changes: 132 additions & 0 deletions asset/plugin/plugin_png.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* implement for c module plugin png
* developed by devseed
*/

#include <stdio.h>
#include <string.h>
#include "plugin.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#if defined(_MSC_VER) || defined(__TINYC__)
#ifndef EXPORT
#define EXPORT __declspec(dllexport)
#endif
#else
#ifndef EXPORT
#define EXPORT __attribute__((visibility("default")))
#endif
#endif

static char s_msg[4096] = {0};
static stbi_uc *g_img = NULL;

PLUGIN_STATUS decode_open(const char *name, void **context)
{
s_msg[0] = '\0';
sprintf(s_msg, "[cmodule:png] open %s, version v0.1", name);
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';
return STATUS_OK;
}

PLUGIN_STATUS decode_close(void *context)
{
return STATUS_OK;
}

static bool decode_offset_default(void *context,
const struct tilepos_t *pos, const struct tilefmt_t *fmt, size_t *offset)
{
// no safety check for pointer here
int i = pos->i, x = pos->x, y = pos->y;
size_t w = fmt->w, h = fmt->h;
size_t nbytes = fmt->nbytes;
uint8_t bpp = fmt->bpp;

size_t offset1 = i * nbytes;
size_t offset2 = (x + y * w) * bpp / 8;
*offset = offset1 + offset2;

return true;
}

PLUGIN_STATUS decode_pixel(void *context,
const uint8_t* data, size_t datasize,
const struct tilepos_t *pos, const struct tilefmt_t *fmt,
struct pixel_t *pixel, bool remain_index)
{
// find decode offset
uint8_t bpp = fmt->bpp;
size_t offset = 0;
if(!decode_offset_default(context, pos, fmt, &offset)) return STATUS_RANGERROR;
if(offset + bpp/8 >= fmt->nbytes ) return STATUS_RANGERROR;

// try decode in different bpp
if(bpp==32) // rgba8888
{
memcpy(pixel, g_img + offset, 4);
}
else if(bpp==24) // rgb888
{
memcpy(pixel, g_img + offset, 3);
if(!remain_index) pixel->a = 255;
}
return STATUS_OK;
}

PLUGIN_STATUS decode_pre(void *context,
const uint8_t* rawdata, size_t rawsize, struct tilecfg_t *cfg)
{
s_msg[0] = '\0';

// check size
size_t datasize = cfg->size;
if(!datasize)
{
datasize = rawsize - cfg->start;
}
if(datasize > rawsize - cfg->start)
{
datasize = rawsize - cfg->start;
}

// decode png
int w, h, c;
g_img = stbi_load_from_memory(rawdata + cfg->start, datasize, &w, &h, &c, 0);
if(!g_img)
{
sprintf(s_msg + strlen(s_msg), "[cmoudule:decode_pre] not a png format\n");
return STATUS_FAIL;
}
sprintf(s_msg + strlen(s_msg), "[cmoudule:decode_pre] png %dx%dx%d\n", w, h, c);

// change tilefmt
cfg->bpp = c * 8;
cfg->w = w;
cfg->h = h;
cfg->nrow = 1;
cfg->nbytes = w*h*c;

if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';

return STATUS_OK;
}

PLUGIN_STATUS decode_post(void *context,
const uint8_t* rawdata, size_t rawsize, struct tilecfg_t *cfg)
{
s_msg[0] = '\0';
sprintf(s_msg + strlen(s_msg), "[cmoudule:decode_post] \n");
if(s_msg[strlen(s_msg) - 1] =='\n') s_msg[strlen(s_msg) - 1] = '\0';
if(g_img) stbi_image_free(g_img);

return STATUS_OK;
}

EXPORT struct tile_decoder_t decoder = {
.open = decode_open, .close = decode_close,
.decode = decode_pixel,
.pre = decode_pre, .post=decode_post, .msg = s_msg
};
4 changes: 2 additions & 2 deletions script/build_mingw.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# default for mingw64
if [ -z "$CC" ]; then CC=x86_64-w64-mingw32-clang; fi
if [ -z "$CXX" ]; then CXX=${CC}++; fi
if [ -z "$WINRES" ]; then WINRES=winres; fi
if [ -z "$WINDRES" ]; then WINDRES=windres; fi
if [ -z "$BUILD_DIR" ]; then BUILD_DIR=$(pwd)/build_mingw64; fi
if [ -z "$BUILD_TYPE" ]; then BUILD_TYPE=Debug; fi

Expand All @@ -19,6 +19,6 @@ cmake -G "Unix Makefiles" -B $BUILD_DIR \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_SYSTEM_NAME=Windows \
-DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=${CXX} \
-DCMAKE_RC_COMPILER=$WINRES
-DCMAKE_RC_COMPILER=$WINDRES

make -C $BUILD_DIR -j$CORE_NUM
13 changes: 13 additions & 0 deletions script/fetch_depend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ function fetch_wxwidgets()
mkdir -p $WXWIDGETS_DIR
7z x depend/$WXWIDGETS_NAME.7z -o$WXWIDGETS_DIR
fi
}

function fetch_stb()
{
STB_VERSION=lastest
STB_NAME=stb-${STB_VERSION}
STB_DIR=depend/$STB_NAME
if ! [ -d "$STB_DIR" ]; then
echo "## fetch_stb $STB_NAME"
curl -fsSL https://github.com/nothings/stb/archive/refs/heads/master.zip -o depend/$STB_NAME.7z
7z x depend/$STB_NAME.7z
mv -f stb-master $STB_DIR
fi
}
11 changes: 6 additions & 5 deletions src/core_app.hpp → src/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <wx/wx.h>
#include <wx/bitmap.h>
#include <wx/filename.h>
#include "core_type.h"
#include <wx/dynlib.h>
#include "plugin.h"

#define APP_VERSION "v0.2.1"
#define MAX_PLUGIN 20
#define APP_VERSION "v0.3"

extern struct tilecfg_t g_tilecfg;

Expand All @@ -16,11 +16,10 @@ class ConfigWindow;

class TileSolver
{
public:
static struct tile_decoder_t *LoadDecoder(wxFileName pluginfile);

public:
TileSolver();
bool LoadDecoder(wxFileName pluginfile);
size_t Open(wxFileName infile = wxFileName()); // file -> m_filebuf
int Decode(struct tilecfg_t *cfg, wxFileName pluginfile = wxFileName()); // m_filebuf -> m_tiles
bool Render(); // m_tiles -> m_bitmap
Expand All @@ -31,6 +30,8 @@ class TileSolver
bool RenderOk();

struct tilecfg_t m_tilecfg;
struct tile_decoder_t *m_decoder;
wxDynamicLibrary m_cmodule;
wxFileName m_infile, m_outfile;
wxFileName m_pluginfile;
wxMemoryBuffer m_filebuf;
Expand Down
Loading

0 comments on commit 1127dc2

Please sign in to comment.