Compare commits
3 Commits
441acbb6c9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cfcba23d2 | ||
|
|
9b2f944cb4 | ||
|
|
5849ece892 |
2
.gitmodules
vendored
2
.gitmodules
vendored
@@ -1,3 +1,3 @@
|
|||||||
[submodule "third_party/nugget"]
|
[submodule "third_party/nugget"]
|
||||||
path = third_party/nugget
|
path = third_party/nugget
|
||||||
url = https://github.com/pcsx-redux/nugget
|
url = https://github.com/pcsx-redux/nugget.git
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -8,11 +8,9 @@ src/splashpack.cpp \
|
|||||||
src/camera.cpp \
|
src/camera.cpp \
|
||||||
src/gtemath.cpp \
|
src/gtemath.cpp \
|
||||||
src/navmesh.cpp \
|
src/navmesh.cpp \
|
||||||
src/lua.cpp \
|
|
||||||
output.o
|
output.o
|
||||||
|
|
||||||
include third_party/nugget/psyqo/psyqo.mk
|
include third_party/nugget/psyqo/psyqo.mk
|
||||||
include third_party/nugget/psyqo-lua/psyqo-lua.mk
|
|
||||||
|
|
||||||
%.o: %.bin
|
%.o: %.bin
|
||||||
$(PREFIX)-objcopy -I binary --set-section-alignment .data=4 --rename-section .data=.rodata,alloc,load,readonly,data,contents -O $(FORMAT) -B mips $< $@
|
$(PREFIX)-objcopy -I binary --set-section-alignment .data=4 --rename-section .data=.rodata,alloc,load,readonly,data,contents -O $(FORMAT) -B mips $< $@
|
||||||
|
|||||||
BIN
output.bin
BIN
output.bin
Binary file not shown.
191
src/lua.cpp
191
src/lua.cpp
@@ -1,191 +0,0 @@
|
|||||||
#include "lua.h"
|
|
||||||
|
|
||||||
#include <psyqo/xprintf.h>
|
|
||||||
|
|
||||||
#include "gameobject.hh"
|
|
||||||
#include "psyqo-lua/lua.hh"
|
|
||||||
|
|
||||||
constexpr const char METATABLE_SCRIPT[] = R"(
|
|
||||||
print("test")
|
|
||||||
metatableForAllGameObjects = {
|
|
||||||
__index = function(self, key)
|
|
||||||
if key == "position" then
|
|
||||||
local pos = rawget(self, key)
|
|
||||||
if pos == nil then
|
|
||||||
pos = get_position(self.__cpp_ptr)
|
|
||||||
rawset(self, key, pos)
|
|
||||||
end
|
|
||||||
return pos
|
|
||||||
end
|
|
||||||
return rawget(self, key)
|
|
||||||
end,
|
|
||||||
|
|
||||||
__newindex = function(self, key, value)
|
|
||||||
if key == "position" then
|
|
||||||
-- Option 1: Directly update C++
|
|
||||||
set_position(self.__cpp_ptr, value)
|
|
||||||
-- Option 2: Also update local cache:
|
|
||||||
rawset(self, key, value)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
rawset(self, key, value)
|
|
||||||
end
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
|
|
||||||
// Lua helpers
|
|
||||||
|
|
||||||
int luaPrint(psyqo::Lua L) {
|
|
||||||
int n = L.getTop(); // Get the number of arguments
|
|
||||||
|
|
||||||
for (int i = 1; i <= n; i++) {
|
|
||||||
if (i > 1) {
|
|
||||||
printf("\t"); // Tab between arguments
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the type of the argument
|
|
||||||
if (L.isString(i)) {
|
|
||||||
printf("%s", L.toString(i)); // If it's a string, print it
|
|
||||||
} else if (L.isNumber(i)) {
|
|
||||||
printf("%g", L.toNumber(i)); // If it's a number, print it
|
|
||||||
} else {
|
|
||||||
// For other types, just print their type (you can expand this if needed)
|
|
||||||
printf("[%s]", L.typeName(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
return 0; // No return value
|
|
||||||
}
|
|
||||||
|
|
||||||
static int gameobjectSetPosition(lua_State* L) {
|
|
||||||
psxsplash::GameObject* go = (psxsplash::GameObject*)lua_touserdata(L, 1);
|
|
||||||
lua_newtable(L);
|
|
||||||
lua_pushnumber(L, go->position.x.raw());
|
|
||||||
lua_setfield(L, -2, "x");
|
|
||||||
lua_pushnumber(L, go->position.y.raw());
|
|
||||||
lua_setfield(L, -2, "y");
|
|
||||||
lua_pushnumber(L, go->position.z.raw());
|
|
||||||
lua_setfield(L, -2, "z");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int gameobjectGetPosition(lua_State* L) {
|
|
||||||
psxsplash::GameObject* go = (psxsplash::GameObject*)lua_touserdata(L, 1);
|
|
||||||
|
|
||||||
lua_getfield(L, 2, "x");
|
|
||||||
psyqo::FixedPoint<> x(lua_tonumber(L, -1), psyqo::FixedPoint<>::RAW);
|
|
||||||
go->position.x = x;
|
|
||||||
lua_pop(L, 1);
|
|
||||||
lua_getfield(L, 2, "y");
|
|
||||||
psyqo::FixedPoint<> y(lua_tonumber(L, -1), psyqo::FixedPoint<>::RAW);
|
|
||||||
go->position.x = x;
|
|
||||||
lua_pop(L, 1);
|
|
||||||
lua_getfield(L, 2, "z");
|
|
||||||
psyqo::FixedPoint<> z(lua_tonumber(L, -1), psyqo::FixedPoint<>::RAW);
|
|
||||||
go->position.x = x;
|
|
||||||
lua_pop(L, 1);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void psxsplash::Lua::Init() {
|
|
||||||
L.push(luaPrint);
|
|
||||||
L.setGlobal("print");
|
|
||||||
|
|
||||||
L.push(gameobjectGetPosition);
|
|
||||||
L.setGlobal("get_position");
|
|
||||||
|
|
||||||
L.push(gameobjectSetPosition);
|
|
||||||
L.setGlobal("set_position");
|
|
||||||
|
|
||||||
// Load and run the metatable script
|
|
||||||
if (L.loadBuffer(METATABLE_SCRIPT, "metatableForAllGameObjects") == 0) {
|
|
||||||
if (L.pcall(0, 0) == 0) {
|
|
||||||
// Script executed successfully
|
|
||||||
printf("Lua script 'metatableForAllGameObjects' loaded successfully\n");
|
|
||||||
} else {
|
|
||||||
// Print Lua error if script execution fails
|
|
||||||
printf("Error executing Lua script: %s\n", L.isString(-1) ? L.toString(-1) : "Unknown error");
|
|
||||||
L.pop();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Print Lua error if script loading fails
|
|
||||||
printf("Error loading Lua script: %s\n", L.isString(-1) ? L.toString(-1) : "Unknown error");
|
|
||||||
L.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the metatable was set as a global
|
|
||||||
L.getGlobal("metatableForAllGameObjects");
|
|
||||||
if (L.isTable(-1)) {
|
|
||||||
printf("metatableForAllGameObjects successfully set as a global\n");
|
|
||||||
} else {
|
|
||||||
printf("Warning: metatableForAllGameObjects not found after init\n");
|
|
||||||
}
|
|
||||||
L.pop(); // Pop the global check
|
|
||||||
}
|
|
||||||
|
|
||||||
void psxsplash::Lua::LoadLuaFile(const char* code, size_t len) {
|
|
||||||
if (L.loadBuffer(code, len) != LUA_OK) {
|
|
||||||
printf("Lua error: %s\n", L.toString(-1));
|
|
||||||
L.pop();
|
|
||||||
}
|
|
||||||
if (L.pcall(0, 0)) {
|
|
||||||
printf("Lua error: %s\n", L.toString(-1));
|
|
||||||
L.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void psxsplash::Lua::RegisterGameObject(GameObject* go) {
|
|
||||||
// Create a new Lua table for the GameObject
|
|
||||||
L.newTable();
|
|
||||||
|
|
||||||
// Set the __cpp_ptr field to store the C++ pointer
|
|
||||||
L.push(go);
|
|
||||||
L.setField(-2, "__cpp_ptr");
|
|
||||||
|
|
||||||
// Set the metatable for the table
|
|
||||||
L.getGlobal("metatableForAllGameObjects");
|
|
||||||
if (L.isTable(-1)) {
|
|
||||||
L.setMetatable(-2); // Set the metatable for the table
|
|
||||||
} else {
|
|
||||||
printf("Warning: metatableForAllGameObjects not found\n");
|
|
||||||
L.pop(); // Pop the invalid metatable
|
|
||||||
}
|
|
||||||
|
|
||||||
L.push(go);
|
|
||||||
L.push(-2);
|
|
||||||
L.rawSet(LUA_REGISTRYINDEX);
|
|
||||||
|
|
||||||
// Debugging: Confirm the GameObject was registered
|
|
||||||
printf("GameObject registered in Lua registry: %p\n", go);
|
|
||||||
|
|
||||||
L.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void psxsplash::Lua::CallOnCollide(GameObject* self, GameObject* other) {
|
|
||||||
L.getGlobal("onCollision");
|
|
||||||
if (!L.isFunction(-1)) {
|
|
||||||
printf("Lua function 'onCollide' not found\n");
|
|
||||||
L.pop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PushGameObject(self);
|
|
||||||
PushGameObject(other);
|
|
||||||
|
|
||||||
if (L.pcall(2, 0)) {
|
|
||||||
printf("Lua error: %s\n", L.toString(-1));
|
|
||||||
L.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void psxsplash::Lua::PushGameObject(GameObject* go) {
|
|
||||||
L.push(go);
|
|
||||||
L.rawGet(LUA_REGISTRYINDEX);
|
|
||||||
|
|
||||||
if (!L.isTable(-1)) {
|
|
||||||
printf("Warning: GameObject not found in Lua registry\n");
|
|
||||||
L.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
20
src/lua.h
20
src/lua.h
@@ -1,20 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <psyqo-lua/lua.hh>
|
|
||||||
|
|
||||||
#include "gameobject.hh"
|
|
||||||
|
|
||||||
namespace psxsplash {
|
|
||||||
class Lua {
|
|
||||||
public:
|
|
||||||
void Init();
|
|
||||||
|
|
||||||
void LoadLuaFile(const char* code, size_t len);
|
|
||||||
void RegisterGameObject(GameObject* go);
|
|
||||||
void CallOnCollide(GameObject* self, GameObject* other);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void PushGameObject(GameObject* go);
|
|
||||||
psyqo::Lua L;
|
|
||||||
};
|
|
||||||
} // namespace psxsplash
|
|
||||||
10
src/main.cpp
10
src/main.cpp
@@ -1,7 +1,5 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
#include <cstdint>
|
|
||||||
#include <psyqo/advancedpad.hh>
|
#include <psyqo/advancedpad.hh>
|
||||||
#include <psyqo/application.hh>
|
#include <psyqo/application.hh>
|
||||||
#include <psyqo/fixed-point.hh>
|
#include <psyqo/fixed-point.hh>
|
||||||
@@ -12,7 +10,6 @@
|
|||||||
|
|
||||||
#include "EASTL/algorithm.h"
|
#include "EASTL/algorithm.h"
|
||||||
#include "camera.hh"
|
#include "camera.hh"
|
||||||
#include "lua.h"
|
|
||||||
#include "navmesh.hh"
|
#include "navmesh.hh"
|
||||||
#include "psyqo/vector.hh"
|
#include "psyqo/vector.hh"
|
||||||
#include "renderer.hh"
|
#include "renderer.hh"
|
||||||
@@ -31,7 +28,6 @@ class PSXSplash final : public psyqo::Application {
|
|||||||
void createScene() override;
|
void createScene() override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
psxsplash::Lua m_lua;
|
|
||||||
psxsplash::SplashPackLoader m_loader;
|
psxsplash::SplashPackLoader m_loader;
|
||||||
|
|
||||||
psyqo::Font<> m_font;
|
psyqo::Font<> m_font;
|
||||||
@@ -77,8 +73,6 @@ void PSXSplash::prepare() {
|
|||||||
|
|
||||||
// Initialize the Renderer singleton
|
// Initialize the Renderer singleton
|
||||||
psxsplash::Renderer::Init(gpu());
|
psxsplash::Renderer::Init(gpu());
|
||||||
|
|
||||||
m_lua.Init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PSXSplash::createScene() {
|
void PSXSplash::createScene() {
|
||||||
@@ -88,8 +82,7 @@ void PSXSplash::createScene() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainScene::start(StartReason reason) {
|
void MainScene::start(StartReason reason) {
|
||||||
app.m_loader.LoadSplashpack(_binary_output_bin_start, app.m_lua);
|
app.m_loader.LoadSplashpack(_binary_output_bin_start);
|
||||||
app.m_lua.CallOnCollide(app.m_loader.gameObjects[0], app.m_loader.gameObjects[1]);
|
|
||||||
psxsplash::Renderer::GetInstance().SetCamera(m_mainCamera);
|
psxsplash::Renderer::GetInstance().SetCamera(m_mainCamera);
|
||||||
|
|
||||||
m_mainCamera.SetPosition(static_cast<psyqo::FixedPoint<12>>(app.m_loader.playerStartPos.x),
|
m_mainCamera.SetPosition(static_cast<psyqo::FixedPoint<12>>(app.m_loader.playerStartPos.x),
|
||||||
@@ -189,7 +182,6 @@ void MainScene::frame() {
|
|||||||
psxsplash::Renderer::GetInstance().RenderNavmeshPreview(*app.m_loader.navmeshes[0], true);
|
psxsplash::Renderer::GetInstance().RenderNavmeshPreview(*app.m_loader.navmeshes[0], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
app.m_font.chainprintf(gpu(), {{.x = 2, .y = 2}}, {{.r = 0xff, .g = 0xff, .b = 0xff}}, "FPS: %i",
|
app.m_font.chainprintf(gpu(), {{.x = 2, .y = 2}}, {{.r = 0xff, .g = 0xff, .b = 0xff}}, "FPS: %i",
|
||||||
gpu().getRefreshRate() / deltaTime);
|
gpu().getRefreshRate() / deltaTime);
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#include <EASTL/array.h>
|
#include <EASTL/array.h>
|
||||||
#include <EASTL/vector.h>
|
#include <EASTL/vector.h>
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <psyqo/fixed-point.hh>
|
#include <psyqo/fixed-point.hh>
|
||||||
#include <psyqo/gte-kernels.hh>
|
#include <psyqo/gte-kernels.hh>
|
||||||
#include <psyqo/gte-registers.hh>
|
#include <psyqo/gte-registers.hh>
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#include <EASTL/array.h>
|
#include <EASTL/array.h>
|
||||||
#include <EASTL/vector.h>
|
#include <EASTL/vector.h>
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <psyqo/bump-allocator.hh>
|
#include <psyqo/bump-allocator.hh>
|
||||||
#include <psyqo/fragments.hh>
|
#include <psyqo/fragments.hh>
|
||||||
#include <psyqo/gpu.hh>
|
#include <psyqo/gpu.hh>
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
#include "splashpack.hh"
|
#include "splashpack.hh"
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <psyqo/primitives/common.hh>
|
#include <psyqo/primitives/common.hh>
|
||||||
|
|
||||||
#include "gameobject.hh"
|
#include "gameobject.hh"
|
||||||
#include "lua.h"
|
|
||||||
#include "mesh.hh"
|
#include "mesh.hh"
|
||||||
#include "psyqo/fixed-point.hh"
|
#include "psyqo/fixed-point.hh"
|
||||||
#include "psyqo/gte-registers.hh"
|
#include "psyqo/gte-registers.hh"
|
||||||
@@ -16,7 +14,6 @@ namespace psxsplash {
|
|||||||
struct SPLASHPACKFileHeader {
|
struct SPLASHPACKFileHeader {
|
||||||
char magic[2];
|
char magic[2];
|
||||||
uint16_t version;
|
uint16_t version;
|
||||||
uint16_t luaFileCount;
|
|
||||||
uint16_t gameObjectCount;
|
uint16_t gameObjectCount;
|
||||||
uint16_t navmeshCount;
|
uint16_t navmeshCount;
|
||||||
uint16_t textureAtlasCount;
|
uint16_t textureAtlasCount;
|
||||||
@@ -24,15 +21,7 @@ struct SPLASHPACKFileHeader {
|
|||||||
psyqo::GTE::PackedVec3 playerStartPos;
|
psyqo::GTE::PackedVec3 playerStartPos;
|
||||||
psyqo::GTE::PackedVec3 playerStartRot;
|
psyqo::GTE::PackedVec3 playerStartRot;
|
||||||
psyqo::FixedPoint<12, uint16_t> playerHeight;
|
psyqo::FixedPoint<12, uint16_t> playerHeight;
|
||||||
uint16_t pad[2];
|
uint16_t pad[1];
|
||||||
};
|
|
||||||
|
|
||||||
struct SPLASHPACKLuaFile {
|
|
||||||
union {
|
|
||||||
uint32_t luaCodeOffset;
|
|
||||||
const char* luaCode;
|
|
||||||
};
|
|
||||||
uint32_t length;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SPLASHPACKTextureAtlas {
|
struct SPLASHPACKTextureAtlas {
|
||||||
@@ -49,7 +38,7 @@ struct SPLASHPACKClut {
|
|||||||
uint16_t pad;
|
uint16_t pad;
|
||||||
};
|
};
|
||||||
|
|
||||||
void SplashPackLoader::LoadSplashpack(uint8_t *data, psxsplash::Lua &lua) {
|
void SplashPackLoader::LoadSplashpack(uint8_t *data) {
|
||||||
psyqo::Kernel::assert(data != nullptr, "Splashpack loading data pointer is null");
|
psyqo::Kernel::assert(data != nullptr, "Splashpack loading data pointer is null");
|
||||||
psxsplash::SPLASHPACKFileHeader *header = reinterpret_cast<psxsplash::SPLASHPACKFileHeader *>(data);
|
psxsplash::SPLASHPACKFileHeader *header = reinterpret_cast<psxsplash::SPLASHPACKFileHeader *>(data);
|
||||||
psyqo::Kernel::assert(memcmp(header->magic, "SP", 2) == 0, "Splashpack has incorrect magic");
|
psyqo::Kernel::assert(memcmp(header->magic, "SP", 2) == 0, "Splashpack has incorrect magic");
|
||||||
@@ -63,17 +52,9 @@ void SplashPackLoader::LoadSplashpack(uint8_t *data, psxsplash::Lua &lua) {
|
|||||||
|
|
||||||
uint8_t *curentPointer = data + sizeof(psxsplash::SPLASHPACKFileHeader);
|
uint8_t *curentPointer = data + sizeof(psxsplash::SPLASHPACKFileHeader);
|
||||||
|
|
||||||
for (uint16_t i = 0; i < header->luaFileCount; i++) {
|
|
||||||
psxsplash::SPLASHPACKLuaFile *luaHeader = reinterpret_cast<psxsplash::SPLASHPACKLuaFile *>(curentPointer);
|
|
||||||
luaHeader->luaCode = reinterpret_cast<const char*>(data + luaHeader->luaCodeOffset);
|
|
||||||
lua.LoadLuaFile(luaHeader->luaCode, luaHeader->length);
|
|
||||||
curentPointer += sizeof(psxsplash::SPLASHPACKLuaFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint16_t i = 0; i < header->gameObjectCount; i++) {
|
for (uint16_t i = 0; i < header->gameObjectCount; i++) {
|
||||||
psxsplash::GameObject *go = reinterpret_cast<psxsplash::GameObject *>(curentPointer);
|
psxsplash::GameObject *go = reinterpret_cast<psxsplash::GameObject *>(curentPointer);
|
||||||
go->polygons = reinterpret_cast<psxsplash::Tri *>(data + go->polygonsOffset);
|
go->polygons = reinterpret_cast<psxsplash::Tri *>(data + go->polygonsOffset);
|
||||||
lua.RegisterGameObject(go);
|
|
||||||
gameObjects.push_back(go);
|
gameObjects.push_back(go);
|
||||||
curentPointer += sizeof(psxsplash::GameObject);
|
curentPointer += sizeof(psxsplash::GameObject);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,12 @@
|
|||||||
|
|
||||||
#include <EASTL/vector.h>
|
#include <EASTL/vector.h>
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
#include "gameobject.hh"
|
#include "gameobject.hh"
|
||||||
#include "lua.h"
|
|
||||||
#include "navmesh.hh"
|
#include "navmesh.hh"
|
||||||
#include "psyqo/fixed-point.hh"
|
#include "psyqo/fixed-point.hh"
|
||||||
|
|
||||||
namespace psxsplash {
|
namespace psxsplash {
|
||||||
|
|
||||||
|
|
||||||
class SplashPackLoader {
|
class SplashPackLoader {
|
||||||
public:
|
public:
|
||||||
eastl::vector<GameObject *> gameObjects;
|
eastl::vector<GameObject *> gameObjects;
|
||||||
@@ -20,7 +16,7 @@ class SplashPackLoader {
|
|||||||
psyqo::GTE::PackedVec3 playerStartPos, playerStartRot;
|
psyqo::GTE::PackedVec3 playerStartPos, playerStartRot;
|
||||||
psyqo::FixedPoint<12, uint16_t> playerHeight;
|
psyqo::FixedPoint<12, uint16_t> playerHeight;
|
||||||
|
|
||||||
void LoadSplashpack(uint8_t *data, Lua &lua);
|
void LoadSplashpack(uint8_t *data);
|
||||||
};
|
};
|
||||||
|
|
||||||
}; // namespace psxsplash
|
}; // namespace psxsplash
|
||||||
2
third_party/nugget
vendored
2
third_party/nugget
vendored
Submodule third_party/nugget updated: c18876727c...e429a934eb
Reference in New Issue
Block a user