Add input and music alternation

This commit is contained in:
Lucas S. Vieira 2024-07-19 23:50:47 -03:00
parent 2732d65bed
commit 2d6fc994c6
4 changed files with 68 additions and 7 deletions

12
include/input.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef INPUT_H
#define INPUT_H
#include <psxpad.h>
void pad_init(void);
void pad_update(void);
uint16_t pad_pressing(PadButton b);
uint16_t pad_pressed(PadButton b);
#endif

View file

@ -21,11 +21,11 @@
type="data"
source="${PROJECT_SOURCE_DIR}/assets/sprites/SONIC.CHARA" />
</dir>
<!-- <dir name="AUDIO"> -->
<dir name="AUDIO">
<file name="BGM001.XA"
type="xa"
source="${PROJECT_SOURCE_DIR}/assets/bgm/BGM001.XA" />
<!-- </dir> -->
</dir>
<dummy sectors="1024"/>
</directory_tree>
</track>

40
src/input.c Normal file
View file

@ -0,0 +1,40 @@
#include "input.h"
#include <psxapi.h>
#include <psxpad.h>
static uint8_t _padbuff[2][34];
static uint16_t _cur_state = 0;
static uint16_t _old_state = 0;
void
pad_init(void)
{
InitPAD(_padbuff[0], 34, _padbuff[1], 34);
StartPAD();
ChangeClearPAD(0);
}
void
pad_update(void)
{
PADTYPE *pad = ((PADTYPE*)_padbuff[0]);
if((pad->stat == 0) &&
((pad->type == PAD_ID_DIGITAL) // Digital
|| (pad->type == PAD_ID_ANALOG_STICK) // DualShock in digital mode
|| (pad->type == PAD_ID_ANALOG))) { // DualShock in analog mode
_old_state = _cur_state;
_cur_state = ~pad->btn;
}
}
uint16_t
pad_pressing(PadButton b)
{
return _cur_state & b;
}
uint16_t
pad_pressed(PadButton b)
{
return !(_old_state & b) && (_cur_state & b);
}

View file

@ -11,6 +11,7 @@
#include "util.h"
#include "chara.h"
#include "sound.h"
#include "input.h"
#define SPRTSZ 56
@ -46,6 +47,9 @@ static MATRIX world = { 0 };
static Chara *sonic_chara = NULL;
#define MUSIC_NUM_CHANNELS 2
static uint8_t music_channel = 0;
void
engine_init()
{
@ -53,6 +57,7 @@ engine_init()
set_clear_color(63, 0, 127);
sound_init();
CdInit();
pad_init();
uint32_t filelength;
TIM_IMAGE tim;
@ -66,15 +71,14 @@ engine_init()
// Start playback after we don't need the CD anymore.
// NOTE: This .XA contains songs of different lengths, so there is a
// huge silence at the end of channel 0.
sound_play_xa("\\BGM001.XA;1", 0, 1);
sound_play_xa("\\AUDIO\\BGM001.XA;1", 0, music_channel);
}
void
engine_update()
{
sound_update();
pad_update();
if(x < (SPRTSZ >> 1) || x > (SCREEN_XRES - 32)) dx = -dx;
if(y < (SPRTSZ >> 1) || y > (SCREEN_YRES - 32)) dy = -dy;
@ -85,6 +89,11 @@ engine_update()
rotation.vx += 6;
rotation.vy -= 8;
rotation.vz -= 12;
if(pad_pressed(PAD_CROSS)) {
music_channel = (music_channel + 1) % MUSIC_NUM_CHANNELS;
sound_xa_set_channel(music_channel);
}
}
void
@ -144,9 +153,9 @@ engine_draw()
sound_xa_get_pos(&minute, &second, &sector);
char buffer[255] = { 0 };
snprintf(buffer, 255,
"%02u:%02u:%03u\n"
"%02u:%02u:%03u Chn: %u\n"
"CD status: %02x\n",
minute, second, sector,
minute, second, sector, music_channel,
sound_get_cd_status());
draw_text(8, 216, 0, buffer);
}