diff --git a/chess/chess.cpp b/chess/chess.cpp index 18e0ff3..9ddc86b 100644 --- a/chess/chess.cpp +++ b/chess/chess.cpp @@ -2,6 +2,7 @@ #include "chess.hpp" #include "sh7091/serial.hpp" + namespace chess { xy position_to_xy(int8_t position) @@ -281,7 +282,7 @@ static void board_init(game_state& game_state) void game_init(game_state& game_state) { - game_state.turn = -1; // white + game_state.turn = 1; // white game_state.en_passant_target = -1; game_state.halfmove_number = 0; game_state.fullmove_number = 0; @@ -452,6 +453,14 @@ void clear_annotations(game_state& game_state) game_state.interaction.annotation_list.length = 0; } +void delete_annotation(annotation_list& annotation_list, int i) +{ + annotation_list.length -= 1; + for (int j = i; j < annotation_list.length; j++) { + annotation_list.annotation[j] = annotation_list.annotation[j + 1]; + } +} + void annotate_position(game_state& game_state, int8_t x, int8_t y) { int8_t position = xy_to_position(x, y); @@ -459,6 +468,7 @@ void annotate_position(game_state& game_state, int8_t x, int8_t y) for (int i = 0; i < annotation_list.length; i++) { if (annotation_list.annotation[i].from_position == position) { + delete_annotation(annotation_list, i); return; } } diff --git a/chess/main.cpp b/chess/main.cpp index 9046576..69a419e 100644 --- a/chess/main.cpp +++ b/chess/main.cpp @@ -34,16 +34,23 @@ void cursor_update(struct render::cursor_state& cursor_state, uint32_t frame_ix) continue; auto& cursor = cursor_state.cur[cursor_ix]; + float invert = port_ix % 2 == 0 ? 1.f : -1.f; + auto& port = input::state.port[port_ix]; if (port.function_type & function_type::controller) { auto& bus_data = port.host_response_data_transfer_ft0->bus_data; auto& data_fields = bus_data.data_fields; if (std::byteswap(data_fields.function_type) & function_type::controller) { auto& data = data_fields.data; - cursor.x += static_cast(data.analog_coordinate_axis[2] - 0x80) * 0.0035; - cursor.y += static_cast(data.analog_coordinate_axis[3] - 0x80) * -0.0035; + cursor.x += static_cast(data.analog_coordinate_axis[2] - 0x80) * 0.0015 * invert; + cursor.y += static_cast(data.analog_coordinate_axis[3] - 0x80) * -0.0015 * invert; cursor.button[frame_ix].a = ft0::data_transfer::digital_button::a(data.digital_button) == 0; cursor.button[frame_ix].b = ft0::data_transfer::digital_button::b(data.digital_button) == 0; + bool start = ft0::data_transfer::digital_button::start(data.digital_button) == 0; + if (start) { + cursor.x = 3.5f; + cursor.y = 3.5f; + } } } if (port.function_type & function_type::pointing) { @@ -51,12 +58,22 @@ void cursor_update(struct render::cursor_state& cursor_state, uint32_t frame_ix) auto& data_fields = bus_data.data_fields; if (std::byteswap(data_fields.function_type) & function_type::pointing) { auto& data = data_fields.data; - cursor.x += static_cast(data.analog_coordinate_axis[0] - 0x200) * 0.0035; - cursor.y += static_cast(data.analog_coordinate_axis[1] - 0x200) * -0.0035; + cursor.x += static_cast(data.analog_coordinate_axis[0] - 0x200) * 0.0065 * invert; + cursor.y += static_cast(data.analog_coordinate_axis[1] - 0x200) * -0.0065 * invert; cursor.button[frame_ix].a = ft9::data_transfer::digital_button::a(data.digital_button) == 0; cursor.button[frame_ix].b = ft9::data_transfer::digital_button::b(data.digital_button) == 0; + bool w = ft9::data_transfer::digital_button::w(data.digital_button) == 0; + if (w) { + cursor.x = 3.5f; + cursor.y = 3.5f; + } } } + + if (cursor.x < -1.0f) cursor.x = -1.0f; + if (cursor.x > 8.0f) cursor.x = 8.0f; + if (cursor.y < -0.5f) cursor.y = -0.5f; + if (cursor.y > 7.5f) cursor.y = 7.5f; } } @@ -100,7 +117,7 @@ void main() core_init(); region_array2(640 / 32, 480 / 32, opb_size); - background_parameter(0xff0000ff); + background_parameter(0xff223311); chess::game_state game_state; chess::game_init(game_state); @@ -116,13 +133,22 @@ void main() vt.piece_rotation = 0.f; vt.board_rotation = 0.f; + auto piece_rotation_animator = render::animator(0.f); + struct render::cursor_state cursor_state = render::cursor_state(); + constexpr float pi = 3.141592653589793f; + while (true) { input::state_update(send_buf, recv_buf); cursor_update(cursor_state, frame_ix); cursor_events(game_state, cursor_state, frame_ix); + float target = game_state.turn == 1 ? 0.f : pi; + piece_rotation_animator.set_target(target, 60); + //vt.board_rotation = piece_rotation_animator.interpolate(); + //vt.piece_rotation = piece_rotation_animator.interpolate(); + ta_polygon_converter_init(opb_size.total(), ta_alloc, 640 / 32, diff --git a/chess/render.cpp b/chess/render.cpp index d063304..1efbbba 100644 --- a/chess/render.cpp +++ b/chess/render.cpp @@ -282,8 +282,8 @@ void draw_pieces(const view_transform vt, uint32_t cursor_colors[4] = { 0xffff00ff, + 0xff0000ff, 0xff00ff00, - 0xffffff00, 0xffff0000, }; diff --git a/chess/render.hpp b/chess/render.hpp index 184fc26..ec58b64 100644 --- a/chess/render.hpp +++ b/chess/render.hpp @@ -11,6 +11,38 @@ struct view_transform { float board_rotation; }; +template +struct animator { + T start; + T end; + int steps; + int step; + + constexpr animator(T initial) + : start(initial), end(initial), steps(0), step(0) + { } + + void set_target(T target_value, int target_steps) + { + if (target_value != end) { + start = interpolate(); + end = target_value; + steps = target_steps; + step = 0; + } + } + + T interpolate() + { + if (step == steps) + return end; + + T value = start + ((end - start) * step) / steps; + step += 1; + return value; + } +}; + struct button { bool a; bool b; @@ -22,7 +54,7 @@ struct cursor { struct button button[2]; constexpr cursor() - : x(4.f), y(4.f) + : x(3.5f), y(3.5f) { button[0] = {false, false}; button[1] = {false, false}; diff --git a/maple/maple_bus_ft9.hpp b/maple/maple_bus_ft9.hpp index d586610..3fcfb6d 100644 --- a/maple/maple_bus_ft9.hpp +++ b/maple/maple_bus_ft9.hpp @@ -15,8 +15,8 @@ namespace ft9 { constexpr uint32_t u() { return 0b1 << 4; } constexpr uint32_t u(uint32_t reg) { return (reg >> 4) & 0b1; } - constexpr uint32_t s() { return 0b1 << 3; } - constexpr uint32_t s(uint32_t reg) { return (reg >> 3) & 0b1; } + constexpr uint32_t w() { return 0b1 << 3; } + constexpr uint32_t w(uint32_t reg) { return (reg >> 3) & 0b1; } constexpr uint32_t a() { return 0b1 << 2; } constexpr uint32_t a(uint32_t reg) { return (reg >> 2) & 0b1; } diff --git a/regs/maple_bus_ft9.csv b/regs/maple_bus_ft9.csv index 78ad170..8dbee27 100644 --- a/regs/maple_bus_ft9.csv +++ b/regs/maple_bus_ft9.csv @@ -1,5 +1,5 @@ "data_transfer",7,6,5,4,3,2,1,0 -"digital_button","r","l","d","u","s","a","b","c" +"digital_button","r","l","d","u","w","a","b","c" "option",,,,,,,"batt","wire" "analog_coordinate_overflow",,,,,,,, "reserved",,,,,,,, diff --git a/regs/maple_bus_ft9.ods b/regs/maple_bus_ft9.ods index 78d4038..909004b 100644 Binary files a/regs/maple_bus_ft9.ods and b/regs/maple_bus_ft9.ods differ