Skip to content

Commit

Permalink
WIP - just saving progress for a branch switch
Browse files Browse the repository at this point in the history
  • Loading branch information
jtothebell committed Sep 8, 2023
1 parent dc4deed commit 0be1ae4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 78 deletions.
141 changes: 69 additions & 72 deletions platform/libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,94 +223,91 @@ int flip = 0;

EXPORT void retro_run()
{
//TODO: improve this so slower hardware can play 30fps games at full speed
if (_vm->getTargetFps() == 60 || frame % 2 == 0)
{
input_poll_cb();

uint8_t currKDown = 0;
uint8_t currKHeld = 0;
for (int i = 0; i < 7; i++) {
bool down = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, buttons[i]);
if (down) {
currKHeld |= BITMASK(i);
//if the key is a new push this frame, mark it as down as well
if (!(kHeld & BITMASK(i))) {
currKDown |= BITMASK(i);
}
input_poll_cb();

uint8_t currKDown = 0;
uint8_t currKHeld = 0;
for (int i = 0; i < 7; i++) {
bool down = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, buttons[i]);
if (down) {
currKHeld |= BITMASK(i);
//if the key is a new push this frame, mark it as down as well
if (!(kHeld & BITMASK(i))) {
currKDown |= BITMASK(i);
}
}
}

mouseBtnState = 0;
mouseBtnState = 0;

if (_memory->drawState.devkitMode) {
bool havePointer = false;
bool haveAnalog = false;
bool haveMouse = false;
bool gotTouch = false;
uint64_t flags = 0;
enviro_cb(RETRO_ENVIRONMENT_GET_INPUT_DEVICE_CAPABILITIES, &flags);
if (_memory->drawState.devkitMode) {
bool havePointer = false;
bool haveAnalog = false;
bool haveMouse = false;
bool gotTouch = false;

uint64_t flags = 0;
enviro_cb(RETRO_ENVIRONMENT_GET_INPUT_DEVICE_CAPABILITIES, &flags);


haveMouse = flags & (1 << RETRO_DEVICE_MOUSE);
havePointer = flags & (1 << RETRO_DEVICE_POINTER);
haveAnalog = flags & (1 << RETRO_DEVICE_ANALOG);
haveMouse = flags & (1 << RETRO_DEVICE_MOUSE);
havePointer = flags & (1 << RETRO_DEVICE_POINTER);
haveAnalog = flags & (1 << RETRO_DEVICE_ANALOG);

if (haveMouse) {
int16_t pointX = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
int16_t pointY = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
mouseBtnState = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);
if (haveMouse) {
int16_t pointX = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
int16_t pointY = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
mouseBtnState = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);

//this needs to be adjusted to screen scale I think?
picoMouseX += pointX;
picoMouseY += pointY;
}
else if (havePointer) {
int16_t pointX = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
int16_t pointY = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
int16_t pressed = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);

if (pressed) {
picoMouseX = pointX * 64 / 32768 + 64;
picoMouseY = pointY * 64 / 32768 + 64;
mouseBtnState = 1;
gotTouch = true;
}
//this needs to be adjusted to screen scale I think?
picoMouseX += pointX;
picoMouseY += pointY;
}
else if (havePointer) {
int16_t pointX = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
int16_t pointY = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
int16_t pressed = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);

if (pressed) {
picoMouseX = pointX * 64 / 32768 + 64;
picoMouseY = pointY * 64 / 32768 + 64;
mouseBtnState = 1;
gotTouch = true;
}
}

if (haveAnalog && !gotTouch) {
// Read the analog X/Y
int16_t analogX = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
int16_t analogY = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
// Pre-calculate where the cursor will be
int16_t tempX = picoMouseX + (((PicoScreenWidth / 32767.0f ) * analogX)/32);
int16_t tempY = picoMouseY + (((PicoScreenHeight / 32767.0f ) * analogY)/32);
// Make sure the cursor stays within the screen
if ( ((tempX - 0) | (PicoScreenWidth - tempX)) >= 0) {
picoMouseX = tempX;
}
if ( ((tempY - 0) | (PicoScreenHeight - tempY)) >= 0) {
picoMouseY = tempY;
}
// Grab the state of the X button
mouseBtnState = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X);
if (haveAnalog && !gotTouch) {
// Read the analog X/Y
int16_t analogX = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
int16_t analogY = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
// Pre-calculate where the cursor will be
int16_t tempX = picoMouseX + (((PicoScreenWidth / 32767.0f ) * analogX)/32);
int16_t tempY = picoMouseY + (((PicoScreenHeight / 32767.0f ) * analogY)/32);
// Make sure the cursor stays within the screen
if ( ((tempX - 0) | (PicoScreenWidth - tempX)) >= 0) {
picoMouseX = tempX;
}
if ( ((tempY - 0) | (PicoScreenHeight - tempY)) >= 0) {
picoMouseY = tempY;
}
// Grab the state of the X button
mouseBtnState = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X);
}

setInputState(currKDown, currKHeld, picoMouseX, picoMouseY, mouseBtnState);

_vm->UpdateAndDraw();
kHeld = currKHeld;
kDown = currKDown;
}

setInputState(currKDown, currKHeld, picoMouseX, picoMouseY, mouseBtnState);

if (frame % 2 == 0) {
_audio->FillAudioBuffer(&audioBuffer, 0, SAMPLESPERFRAME);
audio_batch_cb(audioBuffer, SAMPLESPERFRAME);
}
_vm->Step();
//_vm->UpdateAndDraw();
kHeld = currKHeld;
kDown = currKDown;

if (frame % 2 == 0) {
_audio->FillAudioBuffer(&audioBuffer, 0, SAMPLESPERFRAME);
audio_batch_cb(audioBuffer, SAMPLESPERFRAME);
}


uint8_t* picoFb = _vm->GetPicoInteralFb();
uint8_t* screenPaletteMap = _vm->GetScreenPaletteMap();

Expand Down
5 changes: 3 additions & 2 deletions source/picoluaapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,8 @@ int stat(lua_State *L) {
break;
//target framerate
case 8:
lua_pushnumber(L, _vmForLuaApi->getTargetFps());
//lua_pushnumber(L, _vmForLuaApi->getTargetFps());
lua_pushnumber(L, _vmForLuaApi->getFps());
return 1;
break;
//16-19 audio sfx currently playing
Expand Down Expand Up @@ -1287,7 +1288,7 @@ int reset(lua_State *L) {
}

int setFps(lua_State *L){
_vmForLuaApi->setTargetFps(lua_tointeger(L, 1));
//_vmForLuaApi->setTargetFps(lua_tointeger(L, 1));

return 0;
}
Expand Down
26 changes: 23 additions & 3 deletions source/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,25 @@ void Vm::deserializeCartDataToMemory(std::string cartDataStr) {
}
}

bool Vm::Step(){
bool ret = false;
lua_getglobal(_luaState, "__z8_tick");
int status = lua_pcall(_luaState, 0, 1, 0);
if (status != LUA_OK)
{
char const *message = lua_tostring(_luaState, -1);
_cartLoadError = "Error in main loop: " + std::string(message);
}
else
{
ret = (int)lua_tonumber(_luaState, -1) >= 0;
}
lua_pop(_luaState, 1);


return ret;
}

// void Vm::UpdateAndDraw() {
// update_buttons();

Expand Down Expand Up @@ -718,10 +737,10 @@ void Vm::GameLoop() {
while (_host->shouldRunMainLoop())
{
//shouldn't need to set this every frame
_host->setTargetFps(_targetFps);
//_host->setTargetFps(_targetFps);

//is this better at the end of the loop?
_host->waitForTargetFps();
//_host->waitForTargetFps();

if (_host->shouldQuit()) break; // break in order to return to hbmenu
//this should probably be handled just in the host class
Expand All @@ -730,7 +749,8 @@ void Vm::GameLoop() {
//update buttons needs to be callable from the cart, and also flip
//it should update call the pico part of scanInput and set the values in memory
//then we don't need to pass them in here
UpdateAndDraw();
//UpdateAndDraw();
Step();

uint8_t* picoFb = GetPicoInteralFb();
uint8_t* screenPaletteMap = GetScreenPaletteMap();
Expand Down
3 changes: 2 additions & 1 deletion source/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Vm {

bool _cleanupDeps;

//int _targetFps;
int _targetFps;

int _picoFrameCount;
//bool _hasUpdate;
Expand Down Expand Up @@ -75,6 +75,7 @@ class Vm {
void LoadCart(const unsigned char* cartData, size_t size, bool loadBiosOnFail = true);

// void UpdateAndDraw();
bool Step();

uint8_t* GetPicoInteralFb();
uint8_t* GetScreenPaletteMap();
Expand Down

0 comments on commit 0be1ae4

Please sign in to comment.