-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1844 from nicolasnoble/pcsxlua
Adding real time Lua property editor example.
- Loading branch information
Showing
5 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
TARGET = pcsxlua | ||
TYPE = ps-exe | ||
|
||
SRCS = \ | ||
pcsxlua.cpp \ | ||
|
||
ifeq ($(TEST),true) | ||
CPPFLAGS = -Werror | ||
endif | ||
CXXFLAGS = -std=c++20 | ||
|
||
include ../../psyqo.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2025 PCSX-Redux authors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
#include "psyqo/application.hh" | ||
#include "psyqo/font.hh" | ||
#include "psyqo/gpu.hh" | ||
#include "psyqo/scene.hh" | ||
|
||
namespace { | ||
|
||
// This is a demo application that shows how to use Lua to inspect | ||
// or modify some state inside an application. In this case, we're | ||
// going to modify the background color of the scene in real time. | ||
// The script called `pcsxlua.lua` will be responsible for that, | ||
// and needs to be loaded by the emulator before running this. | ||
// This can be achieved by using the `-dofile pcsxlua.lua` command | ||
// line argument when starting the emulator. | ||
class PCSXLua final : public psyqo::Application { | ||
void prepare() override; | ||
void createScene() override; | ||
|
||
public: | ||
psyqo::Font<> m_systemFont; | ||
}; | ||
|
||
class PCSXLuaScene final : public psyqo::Scene { | ||
void start(StartReason reason) override; | ||
void frame() override; | ||
// This variable will be registered to PCSX in order | ||
// to be modified in real time from the Lua script. | ||
psyqo::Color m_bg = {{.r = 0, .g = 64, .b = 91}}; | ||
}; | ||
|
||
PCSXLua pcsxLua; | ||
PCSXLuaScene pcsxLuaScene; | ||
|
||
// This will be calling into the Lua script from execSlot 255, | ||
// where it will be able to modify the background color of the scene. | ||
void pcsxRegisterVariable(void* address, const char* name) { | ||
register void* a0 asm("a0") = address; | ||
register const char* a1 asm("a1") = name; | ||
__asm__ volatile("" : : "r"(a0), "r"(a1)); | ||
*((volatile uint8_t* const)0x1f802081) = 255; | ||
} | ||
|
||
} // namespace | ||
|
||
void PCSXLua::prepare() { | ||
psyqo::GPU::Configuration config; | ||
config.set(psyqo::GPU::Resolution::W320) | ||
.set(psyqo::GPU::VideoMode::AUTO) | ||
.set(psyqo::GPU::ColorMode::C15BITS) | ||
.set(psyqo::GPU::Interlace::PROGRESSIVE); | ||
gpu().initialize(config); | ||
} | ||
|
||
void PCSXLua::createScene() { | ||
m_systemFont.uploadSystemFont(gpu()); | ||
pushScene(&pcsxLuaScene); | ||
} | ||
|
||
void PCSXLuaScene::start(StartReason reason) { | ||
if (reason == StartReason::Create) { | ||
pcsxRegisterVariable(&m_bg, "pcsxLuaScene.m_bg"); | ||
} | ||
} | ||
|
||
void PCSXLuaScene::frame() { | ||
pcsxLua.gpu().clear(m_bg); | ||
|
||
psyqo::Color c = {{.r = 255, .g = 255, .b = uint8_t(255)}}; | ||
pcsxLua.m_systemFont.print(pcsxLua.gpu(), "Hello World!", {{.x = 16, .y = 32}}, c); | ||
} | ||
|
||
int main() { return pcsxLua.run(); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--[[ | ||
MIT License | ||
Copyright (c) 2025 PCSX-Redux authors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
--]] | ||
|
||
-- This is the companion Lua script for the PCSX-Redux Lua editor demo. | ||
-- See pcsxlua.cpp for the C++ side of the demo. | ||
|
||
local addresses = {} | ||
PCSX.execSlots[255] = function() | ||
local mem = PCSX.getMemPtr() | ||
local regs = PCSX.getRegisters().GPR.n | ||
local name = ffi.string(mem + bit.band(regs.a1, 0x7fffff)) | ||
addresses[name] = regs.a0 | ||
end | ||
|
||
function DrawImguiFrame() | ||
imgui.safe.Begin('Lua editor demo', true, function() | ||
local mem = PCSX.getMemoryAsFile() | ||
local addr = addresses['pcsxLuaScene.m_bg'] | ||
if type(addr) == 'number' then | ||
local color = { r = mem:readU8At(addr + 0) / 255, g = mem:readU8At(addr + 1) / 255, b = mem:readU8At(addr + 2) / 255 } | ||
local modified, n = imgui.extra.ColorEdit3('bgColor', color) | ||
if modified then | ||
mem:writeU8At(n.r * 255, addr + 0) | ||
mem:writeU8At(n.g * 255, addr + 1) | ||
mem:writeU8At(n.b * 255, addr + 2) | ||
end | ||
end | ||
end) | ||
end |