Skip to content

Commit

Permalink
WIP- add crop and scale options to libretro core
Browse files Browse the repository at this point in the history
Building but untested
  • Loading branch information
jtothebell committed Sep 2, 2024
1 parent d98376f commit 8399fe8
Show file tree
Hide file tree
Showing 4 changed files with 586 additions and 6 deletions.
136 changes: 130 additions & 6 deletions platform/libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <array>

#include "libretro.h"
#include "libretro_core_options.h"

#include "../../source/vm.h"
#include "../../source/PicoRam.h"
Expand Down Expand Up @@ -40,10 +41,18 @@ const int PicoScreenHeight = 128;

const int BytesPerPixel = 2;

const size_t screenBufferSize = PicoScreenWidth*PicoScreenHeight;
static int scale = 1;
static int crop_h_left = 0;
static int crop_h_right = 0;
static int crop_v_top = 0;
static int crop_v_bottom = 0;

const size_t screenBufferSize = PicoScreenWidth*PicoScreenHeight;
uint16_t screenBuffer[screenBufferSize];

const size_t screenBufferSize2x = PicoScreenWidth*PicoScreenHeight*4;
uint16_t screenBuffer2x[screenBufferSize2x];

uint16_t _rgb565Colors[144];

Vm* _vm;
Expand All @@ -60,6 +69,90 @@ static void frame_time_cb(retro_usec_t usec)
frame_time = usec / 1000000.0;
}

static void check_variables(bool startup)
{
struct retro_variable var = {0};
char key[256];
int video_updated = 0;

var.key = "fake08_video_scale";
if (enviro_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
int oldScale = scale;
int newScale = 0;
if (!strcmp(var.value, "1x")) {
newScale = 1;
}
else if (!strcmp(var.value, "2x")) {
newScale = 2;
}
if (newScale != 0 && newScale != oldScale)
{
scale = newScale;
video_updated = 2;
}
}


var.key = "fake08_crop_h_left";

if (enviro_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
int newval = atoi(var.value);
if (newval != crop_h_left)
{
crop_h_left = newval;
video_updated = 1;
}
}

var.key = "fake08_crop_h_right";

if (enviro_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
int newval = atoi(var.value);
if (newval != crop_h_right)
{
crop_h_right = newval;
video_updated = 1;
}
}

var.key = "fake08_crop_v_top";

if (enviro_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
int newval = atoi(var.value);
if (newval != crop_v_top)
{
crop_v_top = newval;
video_updated = 1;
}
}

var.key = "fake08_crop_v_bottom";

if (enviro_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
int newval = atoi(var.value);
if (newval != crop_v_bottom)
{
crop_v_bottom = newval;
video_updated = 1;
}
}

if (video_updated && !startup)
{
struct retro_system_av_info av_info;
retro_get_system_av_info(&av_info);
if (video_updated == 2)
enviro_cb(RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO, &av_info);
else
enviro_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info);
}
}


EXPORT void retro_set_environment(retro_environment_t cb)
{
Expand All @@ -74,6 +167,8 @@ EXPORT void retro_set_environment(retro_environment_t cb)

struct retro_frame_time_callback frame_cb = { frame_time_cb, 1000000 / 60 };
enviro_cb(RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK, &frame_cb);

libretro_set_core_options(cb);
}

EXPORT void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
Expand Down Expand Up @@ -173,12 +268,14 @@ EXPORT void retro_get_system_info(struct retro_system_info *info)

EXPORT void retro_get_system_av_info(struct retro_system_av_info *info)
{
unsigned width = (PicoScreenWidth - crop_h_left - crop_h_right) * scale;
unsigned height = (PicoScreenHeight - crop_v_top - crop_v_bottom) * scale;
memset(info, 0, sizeof(*info));
info->geometry.base_width = PicoScreenWidth;
info->geometry.base_height = PicoScreenHeight;
info->geometry.max_width = PicoScreenWidth;
info->geometry.max_height = PicoScreenHeight;
info->geometry.aspect_ratio = 1.f;
info->geometry.base_width = width;
info->geometry.max_width = PicoScreenWidth * scale;
info->geometry.base_height = height;
info->geometry.max_height = PicoScreenHeight * scale;
info->geometry.aspect_ratio = 1.0f;
info->timing.fps = 60.f;
info->timing.sample_rate = 22050.f;

Expand Down Expand Up @@ -223,6 +320,11 @@ int flip = 0;

EXPORT void retro_run()
{
bool updated = false;

if (enviro_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables(false);

//TODO: improve this so slower hardware can play 30fps games at full speed
if (_vm->getTargetFps() == 60 || frame % 2 == 0)
{
Expand Down Expand Up @@ -367,6 +469,24 @@ EXPORT void retro_run()
break;
}

#ifdef TWO_X_SCALE

for(int scry = 0; scry < PicoScreenHeight; scry++) {
for (int scrx = 0; scrx < PicoScreenWidth; scrx++) {
int picox = scrx / drawModeScaleX;
int picoy = scry / drawModeScaleY;
uint16_t color = _rgb565Colors[screenPaletteMap[getPixelNibble(picox, picoy, picoFb)]];

for (int y = 0; y < SCALE; y++) {
for (int x = 0; x < SCALE; x++) {
screenBuffer[(scry*SCALE+y)*PicoScreenWidthScaled+scrx*SCALE+x] = color;
}
}
}
}


#else
//TODO: handle rotation/flip/mirroring
for(int scry = 0; scry < PicoScreenHeight; scry++) {
for (int scrx = 0; scrx < PicoScreenWidth; scrx++) {
Expand All @@ -378,6 +498,8 @@ EXPORT void retro_run()

video_cb(&screenBuffer, PicoScreenWidth, PicoScreenHeight, PicoScreenWidth * BytesPerPixel);

#endif

frame++;
}

Expand Down Expand Up @@ -618,6 +740,8 @@ EXPORT void retro_cheat_set(unsigned index, bool enabled, const char *code)

EXPORT bool retro_load_game(struct retro_game_info const *info)
{
check_variables(true);

if (!info) {
_vm->QueueCartChange("__FAKE08-BIOS.p8");
return true;
Expand Down
Loading

0 comments on commit 8399fe8

Please sign in to comment.