Skip to content

Commit

Permalink
Added keyboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
EPICGameGuy committed Dec 29, 2023
1 parent ed20898 commit edce8e8
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 7 deletions.
Binary file added assets/sounds/kettle.wav
Binary file not shown.
11 changes: 11 additions & 0 deletions include/input/keyboard.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "libpad.h"
#include "tamtypes.h"

namespace Input::Keyboard
{
void init();
void read_inputs();
u8 get_key_status(char key);
} // namespace Input::Keyboard
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ EE_OBJS := $(patsubst %.cpp, build/%.o, $(EE_OBJS))
EE_INCS := -I$(PS2SDK)/ports/include -Iinclude -Ivu1 $(EE_INCS)
EE_LDFLAGS += -L$(PS2SDK)/ports/lib
#EE_LIBS = -ldebug -lpatches -lxcdvd
EE_LIBS = -ldebug -lpatches -lxcdvd -lnetman -lps2gl -lps2stuff -legg -lps2ip -ldraw -lgraph -lmath3d -ldma -lpad -lc -lstdc++ -lgs -ldebug -leedebug -laudsrv -lcdvd -lxcdvd
EE_LIBS = -ldebug -lpatches -lxcdvd -lnetman -lps2gl -lps2stuff -legg -lps2ip -ldraw -lgraph -lmath3d -ldma -lpad -lc -lstdc++ -lgs -ldebug -leedebug -laudsrv -lcdvd -lxcdvd -lkbd
EE_OPTFLAGS = -O3 -g -std=gnu++20
EE_CFLAGS += -DNO_VU0_VECTORS -DNO_ASM
EE_CXXFLAGS += -DNO_VU0_VECTORS -DNO_ASM -frtti
Expand Down
8 changes: 7 additions & 1 deletion src/Makefile.iso
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ $(ISO_FOLDER_DIR)/DEV9.IRX: $(ISO_FOLDER_DIR)
$(ISO_FOLDER_DIR)/NETMAN.IRX: $(ISO_FOLDER_DIR)
cp $(PS2SDK)/iop/irx/netman.irx $@

$(ISO_FOLDER_DIR)/USBD.IRX: $(ISO_FOLDER_DIR)
cp $(PS2SDK)/iop/irx/usbd.irx $@

$(ISO_FOLDER_DIR)/PS2KBD.IRX: $(ISO_FOLDER_DIR)
cp $(PS2SDK)/iop/irx/ps2kbd.irx $@

$(ISO_FOLDER_DIR)/SMAP.IRX: $(ISO_FOLDER_DIR)
cp $(PS2SDK)/iop/irx/smap.irx $@

$(ISO_FOLDER_DIR)/ps2client.exe: $(ISO_FOLDER_DIR)
cp assets/ps2client.exe $@

.PHONY: copy_libs
copy_libs: $(ISO_FOLDER_DIR)/AUDSRV.IRX $(ISO_FOLDER_DIR)/DEV9.IRX $(ISO_FOLDER_DIR)/NETMAN.IRX $(ISO_FOLDER_DIR)/SMAP.IRX $(ISO_FOLDER_DIR)/PS2SND.IRX $(ISO_FOLDER_DIR)/ps2client.exe
copy_libs: $(ISO_FOLDER_DIR)/AUDSRV.IRX $(ISO_FOLDER_DIR)/DEV9.IRX $(ISO_FOLDER_DIR)/NETMAN.IRX $(ISO_FOLDER_DIR)/SMAP.IRX $(ISO_FOLDER_DIR)/PS2SND.IRX $(ISO_FOLDER_DIR)/USBD.IRX $(ISO_FOLDER_DIR)/PS2KBD.IRX $(ISO_FOLDER_DIR)/ps2client.exe

# Sound files
AUDIO_FILE_TYPE = wav
Expand Down
16 changes: 14 additions & 2 deletions src/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,22 @@ void init(int argc, char* argv[])

load_asset_manifest();

{
int ret = SifLoadModule("USBD.IRX"_p.to_full_filepath(), 0, nullptr);
printf("ret: %d\n", ret);
checkf(ret >= 0, "USBD.IRX"_p.to_full_filepath());
}

// {
// int ret = SifLoadModule("BDMMFS_F.IRX"_p.to_full_filepath(), 0, nullptr);
// printf("ret: %d\n", ret);
// checkf(ret >= 0, "BDMMFS_F.IRX"_p.to_full_filepath());
// }

Stats::init();
Input::init();
//Filesystem::run_tests();
//Sound::init();
Sound::init();
GS::init();

printf("Graph mode (region): ");
Expand Down Expand Up @@ -209,7 +221,7 @@ void run()

if (Input::Gamepad::get_paddata() & PAD_START)
{
Sound::set_music_volume(50);
Sound::set_music_volume(100);
Stats::print_timer_stats();
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/input/input.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#include "input/input.hpp"
#include "input/gamepad.hpp"
#include "input/keyboard.hpp"

namespace Input
{
void init()
{
Gamepad::init();
Keyboard::init();
}

void read_inputs()
{
Gamepad::read_inputs();
Keyboard::read_inputs();
}
} // namespace Input
49 changes: 49 additions & 0 deletions src/input/keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "input/keyboard.hpp"

#include <loadfile.h>
#include "egg/assert.hpp"
#include "egg/filesystem.hpp"

#include "libkbd.h"
#include "ps2kbd.h"

static u8 keyboard_status[256];

namespace Input
{
void Keyboard::init()
{

memset(keyboard_status, sizeof(keyboard_status), 0);

{
int ret = SifLoadModule("PS2KBD.IRX"_p.to_full_filepath(), 0, nullptr);
printf("ret: %d\n", ret);
checkf(ret >= 0, "PS2KBD.IRX"_p.to_full_filepath());
}

// Note: this depends on the USB modules being load (BSD.IRX)
if (PS2KbdInit() == 0)
{
checkf(false, "Failed to initialize");
}

PS2KbdSetReadmode(PS2KBD_READMODE_RAW);
}

void Keyboard::read_inputs()
{
PS2KbdRawKey key;
while (PS2KbdReadRaw(&key) != 0)
{
printf("New key: %u, %u\n", key.key, key.state);
keyboard_status[key.key] = key.state & 0xF;
}
}

u8 Keyboard::get_key_status(char key)
{
return keyboard_status[(key - 'a') + 4];
}

} // namespace Input
7 changes: 7 additions & 0 deletions src/objects/movement.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "objects/movement.hpp"
#include "input/gamepad.hpp"
#include "input/keyboard.hpp"
#include "egg/math_types.hpp"
#include "objects/components/transform_component.hpp"
#include "objects/components/collision_component.hpp"
Expand Down Expand Up @@ -184,6 +185,12 @@ void ThirdPersonMovement::calculate_movement_input(float delta_time)
input_vector += ((buttons.ljoy_h - 128.f) / 128.f) * right_movement_vector;
input_vector += ((buttons.ljoy_v - 128.f) / 128.f) * forward_movement_vector;

input_vector += Input::Keyboard::get_key_status('a') * right_movement_vector * -1;
input_vector += Input::Keyboard::get_key_status('d') * right_movement_vector;

input_vector += Input::Keyboard::get_key_status('s') * forward_movement_vector;
input_vector += Input::Keyboard::get_key_status('w') * forward_movement_vector * -1;

if (paddata & PAD_CROSS)
{
movement_vector.y += 1.f;
Expand Down
11 changes: 8 additions & 3 deletions src/sound/sound.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void init()
}
}

song_file = std::ifstream("/assets/sounds/white_waking.wav"_p.to_full_filepath());
song_file = std::ifstream("/assets/sounds/kettle.wav"_p.to_full_filepath());
check(song_file.is_open());
song_file.seekg(0x30, std::ios_base::beg);
chunkReadStatus = -1;
Expand Down Expand Up @@ -160,8 +160,13 @@ bool work_song()
}
}

if (chunkReadStatus < (s32)chunkSize)
song_playing = false;
if (chunkReadStatus == 0)
{
printf("looping song!\n");
song_file.seekg(0x30, std::ios_base::beg);
chunkReadStatus = -1;
song_playing = true;
}

return did_work;
}
Expand Down

0 comments on commit edce8e8

Please sign in to comment.