update yaze_py module and py ext sample

This commit is contained in:
scawful
2024-08-10 22:55:13 -04:00
parent 52502dc568
commit cc39225a2a
4 changed files with 107 additions and 37 deletions

View File

@@ -1,31 +1,31 @@
#include "rom.h"
#include <algorithm> // for remove
#include <chrono> // for system_clock
#include <cstddef> // for size_t
#include <cstdint> // for uint32_t, uint8_t
#include <cstring> // for memcpy
#include <ctime> // for ctime
#include <filesystem> // for copy_options, copy_options...
#include <fstream> // for string, fstream, ifstream
#include <stack> // for stack
#include <string> // for hash, operator==, char_traits
#include <unordered_map> // for unordered_map, operator!=
#include <utility> // for tuple_element<>::type
#include <vector> // for vector, vector<>::value_type
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h" // for flat_hash_map, BitMask
#include "absl/status/status.h" // for OkStatus, InternalError
#include "absl/status/statusor.h" // for StatusOr
#include "absl/strings/str_cat.h" // for StrCat
#include "absl/strings/string_view.h" // for string_view, operator==
#include "app/core/constants.h" // for Bytes, ASSIGN_OR_RETURN
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "app/core/constants.h"
#include "app/core/platform/renderer.h"
#include "app/gfx/bitmap.h" // for Bitmap, BitmapTable
#include "app/gfx/compression.h" // for DecompressV2
#include "app/gfx/snes_color.h" // for SNESColor
#include "app/gfx/snes_palette.h" // for PaletteGroup
#include "app/gfx/snes_tile.h" // for SnesTo8bppSheet
#include "app/gfx/bitmap.h"
#include "app/gfx/compression.h"
#include "app/gfx/snes_color.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
namespace yaze {
namespace app {

View File

@@ -1,29 +1,46 @@
import yaze
class MyExtension:
class YazePyExtension:
def __init__(self):
self.name = "My Python Extension"
self.version = "1.0"
def initialize(self):
print(f"{self.name} Initialized")
def initialize(self, context):
print(f"{self.name} Initialized with context: {context}")
self.context = context
self.register_event_hooks()
def cleanup(self):
print(f"{self.name} Cleaned Up")
def render_ui(self, editor_context):
import imgui
imgui.begin("My Python Extension Window")
imgui.text("Hello from My Python Extension!")
imgui.end()
def manipulate_rom(self, rom):
if rom and rom.data:
print(f"First byte of ROM: 0x{rom.data[0]:02X}")
else:
print("ROM data is not loaded.")
def extend_ui(self, context):
import imgui
if imgui.begin("My Python Extension Window"):
imgui.text("Hello from My Python Extension!")
imgui.end()
def register_commands(self):
# Register custom commands here
print(f"{self.name} Commands Registered")
def register_custom_tools(self):
# Register custom tools here
print(f"{self.name} Custom Tools Registered")
def on_rom_loaded(self):
print("ROM has been loaded!")
def register_event_hooks(self):
# Register event hooks, like for ROM loaded
self.context.register_event_hooks(
YAZE_EVENT_ROM_LOADED, self.on_rom_loaded)
def get_yaze_extension():
return MyExtension()
return YazePyExtension()

View File

@@ -6,6 +6,20 @@ find_package(Boost COMPONENTS python3 REQUIRED)
add_library(
yaze_py MODULE
py/yaze_py.cc
yaze.cc
app/rom.cc
app/core/common.cc
app/core/labeling.cc
app/zelda3/overworld/overworld_map.cc
app/zelda3/overworld/overworld.cc
app/zelda3/sprite/sprite.cc
app/editor/utils/gfx_context.cc
${YAZE_APP_GFX_SRC}
${IMGUI_PATH}/imgui.cpp
${IMGUI_PATH}/imgui_demo.cpp
${IMGUI_PATH}/imgui_draw.cpp
${IMGUI_PATH}/imgui_widgets.cpp
${IMGUI_PATH}/misc/cpp/imgui_stdlib.cpp
)
if (APPLE)
@@ -19,6 +33,7 @@ target_include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/
lib/
app/
${SDL_INCLUDE_DIR}
${PYTHON_HEADERS}
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS}
@@ -26,6 +41,11 @@ target_include_directories(
target_link_libraries(
yaze_py PUBLIC
${SDL_TARGETS}
${ABSL_TARGETS}
${PYTHON_LIBRARIES}
${PNG_LIBRARIES}
Boost::python3
ImGui
ImGuiTestEngine
)

View File

@@ -1,10 +1,12 @@
#include <boost/python.hpp>
#include "base/overworld.h"
#include "base/snes_color.h"
#include "base/sprite.h"
#include "ext/extension.h"
#include "yaze.h"
BOOST_PYTHON_MODULE(yaze) {
BOOST_PYTHON_MODULE(yaze_py) {
using namespace boost::python;
class_<z3_rom>("z3_rom")
@@ -31,4 +33,35 @@ BOOST_PYTHON_MODULE(yaze) {
.def_readonly("name", &z3_sprite::name)
.def_readonly("id", &z3_sprite::id)
.def_readonly("actions", &z3_sprite::actions);
}
class_<yaze_flags>("yaze_flags")
.def_readwrite("debug", &yaze_flags::debug)
.def_readwrite("rom_filename", &yaze_flags::rom_filename)
.def_readwrite("rom", &yaze_flags::rom);
class_<yaze_project>("yaze_project")
.def_readonly("filename", &yaze_project::filename)
.def_readonly("rom", &yaze_project::rom)
.def_readonly("overworld", &yaze_project::overworld);
class_<yaze_editor_context>("yaze_editor_context")
.def_readonly("project", &yaze_editor_context::project);
enum_<yaze_event_type>("yaze_event_type")
.value("YAZE_EVENT_ROM_LOADED", YAZE_EVENT_ROM_LOADED)
.value("YAZE_EVENT_ROM_SAVED", YAZE_EVENT_ROM_SAVED)
.value("YAZE_EVENT_SPRITE_MODIFIED", YAZE_EVENT_SPRITE_MODIFIED)
.value("YAZE_EVENT_PALETTE_CHANGED", YAZE_EVENT_PALETTE_CHANGED);
class_<yaze_extension>("yaze_extension")
.def_readonly("name", &yaze_extension::name)
.def_readonly("version", &yaze_extension::version);
// Functions that return raw pointers need to be managed by Python's garbage
// collector
def("yaze_load_rom", &yaze_load_rom,
return_value_policy<manage_new_object>());
def("yaze_unload_rom", &yaze_unload_rom); // No need to manage memory here
def("yaze_get_color_from_paletteset", &yaze_get_color_from_paletteset);
def("yaze_check_version", &yaze_check_version);
}