Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API refactor #851

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 32blit-pico/input/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ void update_input() {
new_buttons |= 1 << i;
}

blit::api.buttons = new_buttons;
blit::api_data.buttons = new_buttons;
}
32 changes: 16 additions & 16 deletions 32blit-pico/input/usb_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void update_input() {
}
}

api.buttons = new_buttons;
api_data.buttons = new_buttons;
}

if(!hid_gamepad_id)
Expand All @@ -121,19 +121,19 @@ void update_input() {
while(mapping->id && mapping->id != hid_gamepad_id)
mapping++;

api.buttons = dpad_map[hid_hat > 8 ? 8 : hid_hat]
| (hid_buttons & (1 << mapping->left) ? uint32_t(Button::DPAD_LEFT) : 0)
| (hid_buttons & (1 << mapping->right) ? uint32_t(Button::DPAD_RIGHT) : 0)
| (hid_buttons & (1 << mapping->up) ? uint32_t(Button::DPAD_UP) : 0)
| (hid_buttons & (1 << mapping->down) ? uint32_t(Button::DPAD_DOWN) : 0)
| (hid_buttons & (1 << mapping->a) ? uint32_t(Button::A) : 0)
| (hid_buttons & (1 << mapping->b) ? uint32_t(Button::B) : 0)
| (hid_buttons & (1 << mapping->x) ? uint32_t(Button::X) : 0)
| (hid_buttons & (1 << mapping->y) ? uint32_t(Button::Y) : 0)
| (hid_buttons & (1 << mapping->menu) ? uint32_t(Button::MENU) : 0)
| (hid_buttons & (1 << mapping->home) ? uint32_t(Button::HOME) : 0)
| (hid_buttons & (1 << mapping->joystick) ? uint32_t(Button::JOYSTICK) : 0);

api.joystick.x = (float(hid_joystick[0]) - 0x80) / 0x80;
api.joystick.y = (float(hid_joystick[1]) - 0x80) / 0x80;
api_data.buttons = dpad_map[hid_hat > 8 ? 8 : hid_hat]
| (hid_buttons & (1 << mapping->left) ? uint32_t(Button::DPAD_LEFT) : 0)
| (hid_buttons & (1 << mapping->right) ? uint32_t(Button::DPAD_RIGHT) : 0)
| (hid_buttons & (1 << mapping->up) ? uint32_t(Button::DPAD_UP) : 0)
| (hid_buttons & (1 << mapping->down) ? uint32_t(Button::DPAD_DOWN) : 0)
| (hid_buttons & (1 << mapping->a) ? uint32_t(Button::A) : 0)
| (hid_buttons & (1 << mapping->b) ? uint32_t(Button::B) : 0)
| (hid_buttons & (1 << mapping->x) ? uint32_t(Button::X) : 0)
| (hid_buttons & (1 << mapping->y) ? uint32_t(Button::Y) : 0)
| (hid_buttons & (1 << mapping->menu) ? uint32_t(Button::MENU) : 0)
| (hid_buttons & (1 << mapping->home) ? uint32_t(Button::HOME) : 0)
| (hid_buttons & (1 << mapping->joystick) ? uint32_t(Button::JOYSTICK) : 0);

api_data.joystick.x = (float(hid_joystick[0]) - 0x80) / 0x80;
api_data.joystick.y = (float(hid_joystick[1]) - 0x80) / 0x80;
}
6 changes: 3 additions & 3 deletions 32blit-pico/led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ void update_led() {

#ifdef HAVE_LED
const float gamma = 2.8;
uint16_t value = (uint16_t)(std::pow((float)(api.LED.r) / 255.0f, gamma) * 65535.0f + 0.5f);
uint16_t value = (uint16_t)(std::pow((float)(api_data.LED.r) / 255.0f, gamma) * 65535.0f + 0.5f);
pwm_set_gpio_level(led_pins[0], value);
value = (uint16_t)(std::pow((float)(api.LED.g) / 255.0f, gamma) * 65535.0f + 0.5f);
value = (uint16_t)(std::pow((float)(api_data.LED.g) / 255.0f, gamma) * 65535.0f + 0.5f);
pwm_set_gpio_level(led_pins[1], value);
value = (uint16_t)(std::pow((float)(api.LED.b) / 255.0f, gamma) * 65535.0f + 0.5f);
value = (uint16_t)(std::pow((float)(api_data.LED.b) / 255.0f, gamma) * 65535.0f + 0.5f);
pwm_set_gpio_level(led_pins[2], value);
#endif
}
123 changes: 70 additions & 53 deletions 32blit-pico/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,76 @@ static GameMetadata get_metadata() {
return ret;
}

// blit API
static const blit::APIConst blit_api_const {
blit::api_version_major, blit::api_version_minor,

::channels,

::set_screen_mode,
::set_screen_palette,

::now,
::random,
nullptr, // exit
::debug,

::open_file,
::read_file,
::write_file,
::close_file,
::get_file_length,
::list_files,
::file_exists,
::directory_exists,
::create_directory,
::rename_file,
::remove_file,
::get_save_path,
::is_storage_available,

nullptr, // enable_us_timer
::get_us_timer,
::get_max_us_timer,

nullptr, // decode_jpeg_buffer
nullptr, // decode_jpeg_file

nullptr, // launch
nullptr, // erase_game
nullptr, // get_type_handler_metadata

::get_launch_path,

::is_multiplayer_connected,
::set_multiplayer_enabled,
::send_multiplayer_message,

nullptr, // flash_to_tmp
nullptr, // tmp_file_closed

::get_metadata,

::set_screen_mode_format,

nullptr, // i2c_send
nullptr, // i2c_recieve

nullptr, // set_raw_cdc_enabled
nullptr, // cdc_write
nullptr, // cdc_read

nullptr, // list_installed_games
nullptr, // can_launch
};

static blit::APIData blit_api_data;

namespace blit {
const APIConst &api = blit_api_const;
APIData &api_data = blit_api_data;
}

// user funcs
void init();
void render(uint32_t);
Expand Down Expand Up @@ -141,59 +211,6 @@ int main() {

stdio_init_all();

api.channels = ::channels;

api.set_screen_mode = ::set_screen_mode;
api.set_screen_palette = ::set_screen_palette;
api.set_screen_mode_format = ::set_screen_mode_format;
api.now = ::now;
api.random = ::random;
// api.exit = ::exit;

// serial debug
api.debug = ::debug;

// files
api.open_file = ::open_file;
api.read_file = ::read_file;
api.write_file = ::write_file;
api.close_file = ::close_file;
api.get_file_length = ::get_file_length;
api.list_files = ::list_files;
api.file_exists = ::file_exists;
api.directory_exists = ::directory_exists;
api.create_directory = ::create_directory;
api.rename_file = ::rename_file;
api.remove_file = ::remove_file;
api.get_save_path = ::get_save_path;
api.is_storage_available = ::is_storage_available;

// profiler
// api.enable_us_timer = ::enable_us_timer;
api.get_us_timer = ::get_us_timer;
api.get_max_us_timer = ::get_max_us_timer;

// jpeg
// api.decode_jpeg_buffer = ::decode_jpeg_buffer;
// api.decode_jpeg_file = ::decode_jpeg_file;

// launcher
// api.launch = ::launch;
// api.erase_game = ::erase_game;
// api.get_type_handler_metadata = ::get_type_handler_metadata;

api.get_launch_path = ::get_launch_path;

// multiplayer
api.is_multiplayer_connected = ::is_multiplayer_connected;
api.set_multiplayer_enabled = ::set_multiplayer_enabled;
api.send_message = ::send_multiplayer_message;

// api.flash_to_tmp = ::flash_to_tmp;
// api.tmp_file_closed = ::tmp_file_closed;

api.get_metadata = ::get_metadata;

init_led();
init_display();
init_input();
Expand Down
4 changes: 2 additions & 2 deletions 32blit-pico/multiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void update_multiplayer() {
mp_buffer_off += usb_cdc_read(mp_buffer + mp_buffer_off, mp_buffer_len - mp_buffer_off);

if(mp_buffer_off == mp_buffer_len) {
if(blit::api.message_received)
blit::api.message_received(mp_buffer, mp_buffer_len);
if(blit::api_data.message_received)
blit::api_data.message_received(mp_buffer, mp_buffer_len);

delete[] mp_buffer;
mp_buffer = nullptr;
Expand Down
4 changes: 0 additions & 4 deletions 32blit-sdl/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
#include "audio/audio.hpp"
#include "engine/api_private.hpp"

blit::AudioChannel channels[CHANNEL_COUNT];

static void _audio_callback(void *userdata, uint8_t *stream, int len);

Audio::Audio() {
blit::api.channels = channels;

SDL_AudioSpec desired = {}, audio_spec = {};

desired.freq = _sample_rate;
Expand Down
6 changes: 3 additions & 3 deletions 32blit-sdl/Multiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Multiplayer::~Multiplayer() {
void Multiplayer::update() {
if(!enabled)
return;

if(!socket && !listen_socket) {
// attempt to reconnect
auto now = SDL_GetTicks();
Expand Down Expand Up @@ -131,8 +131,8 @@ void Multiplayer::update() {

// got message, pass to user
if(recv_off == recv_len) {
if(api.message_received)
api.message_received(recv_buf, recv_len);
if(api_data.message_received)
api_data.message_received(recv_buf, recv_len);

delete[] recv_buf;
recv_buf = nullptr;
Expand Down
107 changes: 72 additions & 35 deletions 32blit-sdl/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

extern Input *blit_input;

// blit audio channels
static blit::AudioChannel channels[CHANNEL_COUNT];

int System::width = System::max_width;
int System::height = System::max_height;

Expand Down Expand Up @@ -168,6 +171,75 @@ void blit_send_message(const uint8_t *data, uint16_t length) {
blit_multiplayer->send_message(data, length);
}

// blit API
static const blit::APIConst blit_api_const {
blit::api_version_major, blit::api_version_minor,
channels,

::set_screen_mode,
::set_screen_palette,

::now,
::blit_random,
nullptr, // exit
::blit_debug,

::open_file,
::read_file,
::write_file,
::close_file,
::get_file_length,
::list_files,
::file_exists,
::directory_exists,
::create_directory,
::rename_file,
::remove_file,
::get_save_path,
::is_storage_available,

::enable_us_timer,
::get_us_timer,
::get_max_us_timer,

blit_decode_jpeg_buffer,
blit_decode_jpeg_file,

nullptr, // launch
nullptr, // erase_game
nullptr, // get_type_handler_metadata

::get_launch_path,

blit_is_multiplayer_connected,
blit_set_multiplayer_enabled,
blit_send_message,

nullptr, // flash_to_tmp
nullptr, // tmp_file_closed

::get_metadata,

::set_screen_mode_format,

nullptr, // i2c_send
nullptr, // i2c_recieve

nullptr, // set_raw_cdc_enabled
nullptr, // cdc_write
nullptr, // cdc_read

nullptr, // list_installed_games
nullptr, // can_launch
};

static blit::APIData blit_api_data;

namespace blit {
const APIConst &api = blit_api_const;
APIData &api_data = blit_api_data;
}

// SDL events
const Uint32 System::timer_event = SDL_RegisterEvents(2);
const Uint32 System::loop_event = System::timer_event + 1;
Expand Down Expand Up @@ -208,46 +280,11 @@ void System::run() {

start = std::chrono::steady_clock::now();

blit::api.now = ::now;
blit::api.random = ::blit_random;
blit::api.debug = ::blit_debug;
blit::api.set_screen_mode = ::set_screen_mode;
blit::api.set_screen_palette = ::set_screen_palette;
blit::api.set_screen_mode_format = ::set_screen_mode_format;
blit::update = ::update;
blit::render = ::render;

setup_base_path();

blit::api.open_file = ::open_file;
blit::api.read_file = ::read_file;
blit::api.write_file = ::write_file;
blit::api.close_file = ::close_file;
blit::api.get_file_length = ::get_file_length;
blit::api.list_files = ::list_files;
blit::api.file_exists = ::file_exists;
blit::api.directory_exists = ::directory_exists;
blit::api.create_directory = ::create_directory;
blit::api.rename_file = ::rename_file;
blit::api.remove_file = ::remove_file;
blit::api.get_save_path = ::get_save_path;
blit::api.is_storage_available = ::is_storage_available;

blit::api.enable_us_timer = ::enable_us_timer;
blit::api.get_us_timer = ::get_us_timer;
blit::api.get_max_us_timer = ::get_max_us_timer;

blit::api.decode_jpeg_buffer = blit_decode_jpeg_buffer;
blit::api.decode_jpeg_file = blit_decode_jpeg_file;

blit::api.get_launch_path = ::get_launch_path;

blit::api.is_multiplayer_connected = blit_is_multiplayer_connected;
blit::api.set_multiplayer_enabled = blit_set_multiplayer_enabled;
blit::api.send_message = blit_send_message;

blit::api.get_metadata = ::get_metadata;

blit::set_screen_mode(blit::lores);

#ifdef __EMSCRIPTEN__
Expand Down
1 change: 1 addition & 0 deletions 32blit-stm32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ add_library(BlitHalSTM32 OBJECT
Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Src/usbh_cdc.c
Src/usbd_msc_scsi.c
Src/system_stm32h7xx.c
Src/api.cpp
Src/fatfs.cpp
Src/fatfs_sd.c
Src/usb_otg.c
Expand Down
Loading
Loading