|
| 1 | +/* |
| 2 | +
|
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2025 PCSX-Redux authors |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +#include "psyqo/application.hh" |
| 28 | +#include "psyqo/font.hh" |
| 29 | +#include "psyqo/gpu.hh" |
| 30 | +#include "psyqo/scene.hh" |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +// This is a demo application that shows how to use Lua to inspect |
| 35 | +// or modify some state inside an application. In this case, we're |
| 36 | +// going to modify the background color of the scene in real time. |
| 37 | +// The script called `pcsxlua.lua` will be responsible for that, |
| 38 | +// and needs to be loaded by the emulator before running this. |
| 39 | +// This can be achieved by using the `-dofile pcsxlua.lua` command |
| 40 | +// line argument when starting the emulator. |
| 41 | +class PCSXLua final : public psyqo::Application { |
| 42 | + void prepare() override; |
| 43 | + void createScene() override; |
| 44 | + |
| 45 | + public: |
| 46 | + psyqo::Font<> m_systemFont; |
| 47 | +}; |
| 48 | + |
| 49 | +class PCSXLuaScene final : public psyqo::Scene { |
| 50 | + void start(StartReason reason) override; |
| 51 | + void frame() override; |
| 52 | + // This variable will be registered to PCSX in order |
| 53 | + // to be modified in real time from the Lua script. |
| 54 | + psyqo::Color m_bg = {{.r = 0, .g = 64, .b = 91}}; |
| 55 | +}; |
| 56 | + |
| 57 | +PCSXLua pcsxLua; |
| 58 | +PCSXLuaScene pcsxLuaScene; |
| 59 | + |
| 60 | +// This will be calling into the Lua script from execSlot 255, |
| 61 | +// where it will be able to modify the background color of the scene. |
| 62 | +void pcsxRegisterVariable(void* address, const char* name) { |
| 63 | + register void* a0 asm("a0") = address; |
| 64 | + register const char* a1 asm("a1") = name; |
| 65 | + __asm__ volatile("" : : "r"(a0), "r"(a1)); |
| 66 | + *((volatile uint8_t* const)0x1f802081) = 255; |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace |
| 70 | + |
| 71 | +void PCSXLua::prepare() { |
| 72 | + psyqo::GPU::Configuration config; |
| 73 | + config.set(psyqo::GPU::Resolution::W320) |
| 74 | + .set(psyqo::GPU::VideoMode::AUTO) |
| 75 | + .set(psyqo::GPU::ColorMode::C15BITS) |
| 76 | + .set(psyqo::GPU::Interlace::PROGRESSIVE); |
| 77 | + gpu().initialize(config); |
| 78 | +} |
| 79 | + |
| 80 | +void PCSXLua::createScene() { |
| 81 | + m_systemFont.uploadSystemFont(gpu()); |
| 82 | + pushScene(&pcsxLuaScene); |
| 83 | +} |
| 84 | + |
| 85 | +void PCSXLuaScene::start(StartReason reason) { |
| 86 | + if (reason == StartReason::Create) { |
| 87 | + pcsxRegisterVariable(&m_bg, "pcsxLuaScene.m_bg"); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +void PCSXLuaScene::frame() { |
| 92 | + pcsxLua.gpu().clear(m_bg); |
| 93 | + |
| 94 | + psyqo::Color c = {{.r = 255, .g = 255, .b = uint8_t(255)}}; |
| 95 | + pcsxLua.m_systemFont.print(pcsxLua.gpu(), "Hello World!", {{.x = 16, .y = 32}}, c); |
| 96 | +} |
| 97 | + |
| 98 | +int main() { return pcsxLua.run(); } |
0 commit comments