General cleanups - prob no functional differences:

* Make sure whatever gets initialized/set in retro_load_game is
deinitialized/unset in retro_unload_game, and vice versa for retro_init/retro_deinit
This commit is contained in:
libretroadmin 2024-05-22 14:27:22 +02:00
parent ab05295d81
commit c847150cb2
3 changed files with 423 additions and 459 deletions

View File

@ -272,16 +272,11 @@ void retro_set_rumble_touch(unsigned intensity, float duration)
char* FindFileInDir(const char* dir, const char* wfname, const char* ext);
static void check_system_specs(void)
{
unsigned level = 4;
environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
}
static bool libretro_supports_bitmasks = false;
void retro_init(void)
{
unsigned level = 4;
enum retro_pixel_format rgb565;
struct retro_log_callback log;
@ -299,31 +294,11 @@ void retro_init(void)
if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
libretro_supports_bitmasks = true;
check_system_specs();
environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
}
void retro_deinit(void)
{
D_DoomDeinit();
if (screen_buf)
free(screen_buf);
screen_buf = NULL;
cheats_enabled = false;
cheats_pending = false;
if (cheats_pending_list)
{
unsigned i;
for (i = 0; i < RBUF_LEN(cheats_pending_list); i++)
{
if (cheats_pending_list[i])
free(cheats_pending_list[i]);
cheats_pending_list[i] = NULL;
}
RBUF_FREE(cheats_pending_list);
}
libretro_supports_bitmasks = false;
retro_set_rumble_damage(0, 0.0f);
@ -359,7 +334,7 @@ void retro_get_system_info(struct retro_system_info *info)
info->library_version = "v2.5.0" GIT_VERSION;
info->need_fullpath = true;
info->valid_extensions = "wad|iwad|pwad|lmp";
info->block_extract = false;
info->block_extract = false;
}
void retro_get_system_av_info(struct retro_system_av_info *info)
@ -421,11 +396,11 @@ void retro_get_system_av_info(struct retro_system_av_info *info)
info->timing.fps = TICRATE;
break;
}
info->timing.sample_rate = 44100.0;
info->geometry.base_width = SCREENWIDTH;
info->geometry.base_height = SCREENHEIGHT;
info->geometry.max_width = SCREENWIDTH;
info->geometry.max_height = SCREENHEIGHT;
info->timing.sample_rate = 44100.0;
info->geometry.base_width = SCREENWIDTH;
info->geometry.base_height = SCREENHEIGHT;
info->geometry.max_width = SCREENWIDTH;
info->geometry.max_height = SCREENHEIGHT;
info->geometry.aspect_ratio = 4.0 / 3.0;
}
@ -535,11 +510,9 @@ static void update_variables(bool startup)
char str[100];
strlcpy(str, var.value, sizeof(str));
pch = strtok(str, "x");
if (pch)
if ((pch = strtok(str, "x")))
SCREENWIDTH = strtoul(pch, NULL, 0);
pch = strtok(NULL, "x");
if (pch)
if ((pch = strtok(NULL, "x")))
SCREENHEIGHT = strtoul(pch, NULL, 0);
if (log_cb)
@ -721,18 +694,15 @@ static wadinfo_t get_wadinfo(const char *path)
return header;
}
bool I_PreInitGraphics(void)
{
screen_buf = malloc(SURFACE_PIXEL_DEPTH * SCREENWIDTH * SCREENHEIGHT);
return (screen_buf != NULL);
}
bool retro_load_game(const struct retro_game_info *info)
{
unsigned i;
int argc = 0;
static char *argv[32] = {NULL};
for (i = 0; i < 32; i++)
argv[0] = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
{
if (log_cb)
@ -744,6 +714,7 @@ bool retro_load_game(const struct retro_game_info *info)
update_variables(true);
argv[argc++] = strdup("prboom");
if(info->path)
{
wadinfo_t header;
@ -765,65 +736,63 @@ bool retro_load_game(const struct retro_game_info *info)
}
else
{
header = get_wadinfo(info->path);
// header.identification is static array, always non-NULL, but it might be empty if it couldn't be read
if(header.identification[0] == 0)
{
I_Error("retro_load_game: couldn't read WAD header from '%s'", info->path);
goto failed;
}
if(!strncmp(header.identification, "IWAD", 4))
{
argv[argc++] = strdup("-iwad");
argv[argc++] = strdup(g_basename);
}
else if(!strncmp(header.identification, "PWAD", 4))
{
argv[argc++] = strdup("-file");
argv[argc++] = strdup(info->path);
}
else {
I_Error("retro_load_game: invalid WAD header '%.*s'", 4, header.identification);
goto failed;
}
header = get_wadinfo(info->path);
// header.identification is static array, always non-NULL, but it might be empty if it couldn't be read
if(header.identification[0] == 0)
{
I_Error("retro_load_game: couldn't read WAD header from '%s'", info->path);
goto failed;
}
if(!strncmp(header.identification, "IWAD", 4))
{
argv[argc++] = strdup("-iwad");
argv[argc++] = strdup(g_basename);
}
else if(!strncmp(header.identification, "PWAD", 4))
{
argv[argc++] = strdup("-file");
argv[argc++] = strdup(info->path);
}
else
{
I_Error("retro_load_game: invalid WAD header '%.*s'", 4, header.identification);
goto failed;
}
/* Check for DEH or BEX files */
if((deh = FindFileInDir(g_wad_dir, name_without_ext, ".deh"))
|| (deh = FindFileInDir(g_wad_dir, name_without_ext, ".bex")))
{
argv[argc++] = "-deh";
argv[argc++] = deh;
};
/* Check for DEH or BEX files */
if((deh = FindFileInDir(g_wad_dir, name_without_ext, ".deh"))
|| (deh = FindFileInDir(g_wad_dir, name_without_ext, ".bex")))
{
argv[argc++] = "-deh";
argv[argc++] = deh;
}
if((baseconfig = FindFileInDir(g_wad_dir, name_without_ext, ".prboom.cfg"))
|| (baseconfig = I_FindFile("prboom.cfg", NULL)))
{
argv[argc++] = "-baseconfig";
argv[argc++] = baseconfig;
}
if((baseconfig = FindFileInDir(g_wad_dir, name_without_ext, ".prboom.cfg"))
|| (baseconfig = I_FindFile("prboom.cfg", NULL)))
{
argv[argc++] = "-baseconfig";
argv[argc++] = baseconfig;
}
}
// Get save directory
if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &base_save_dir) && base_save_dir)
{
if (base_save_dir && strlen(base_save_dir) > 0)
{
// > Build save path
snprintf(g_save_dir, sizeof(g_save_dir), "%s%c%s", base_save_dir, DIR_SLASH, name_without_ext);
use_external_savedir = true;
{
if (base_save_dir && strlen(base_save_dir) > 0)
{
// > Build save path
snprintf(g_save_dir, sizeof(g_save_dir), "%s%c%s", base_save_dir, DIR_SLASH, name_without_ext);
use_external_savedir = true;
// > Create save directory, if required
if (!path_is_directory(g_save_dir))
{
use_external_savedir = path_mkdir(g_save_dir);
}
}
}
if (!use_external_savedir)
{
// > Use WAD directory fallback...
strlcpy(g_save_dir, g_wad_dir, sizeof(g_save_dir));
}
// > Create save directory, if required
if (!path_is_directory(g_save_dir))
use_external_savedir = path_mkdir(g_save_dir);
}
}
// > Use WAD directory fallback...
if (!use_external_savedir)
strlcpy(g_save_dir, g_wad_dir, sizeof(g_save_dir));
}
#if DEBUG
@ -835,7 +804,8 @@ bool retro_load_game(const struct retro_game_info *info)
myargv = (const char **) argv;
/* cphipps - call to video specific startup code */
if (!I_PreInitGraphics())
screen_buf = (unsigned char*)malloc(SURFACE_PIXEL_DEPTH * SCREENWIDTH * SCREENHEIGHT);
if (!screen_buf)
goto failed;
if (!D_DoomMainSetup())
@ -862,7 +832,8 @@ failed:
if (environ_cb)
environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
}
if (screen_buf) {
if (screen_buf)
{
free(screen_buf);
screen_buf = NULL;
}
@ -870,9 +841,30 @@ failed:
return false;
}
void retro_unload_game(void)
{
D_DoomDeinit();
cheats_enabled = false;
cheats_pending = false;
if (cheats_pending_list)
{
unsigned i;
for (i = 0; i < RBUF_LEN(cheats_pending_list); i++)
{
if (cheats_pending_list[i])
free(cheats_pending_list[i]);
cheats_pending_list[i] = NULL;
}
RBUF_FREE(cheats_pending_list);
}
if (screen_buf)
free(screen_buf);
screen_buf = NULL;
myargc = 0;
myargv = NULL;
}
unsigned retro_get_region(void)
@ -985,31 +977,34 @@ bool retro_serialize(void *data_, size_t size)
unsigned i;
struct extra_serialize *extra = data_;
if (gamestate == GS_LEVEL) {
int ret = G_DoSaveGameToBuffer((char *) data_ + sizeof(*extra),
size - sizeof(*extra));
if (!ret) {
return false;
}
if (viewplayer && viewplayer->mo) {
extra->prevx = viewplayer->mo->PrevX;
extra->prevy = viewplayer->mo->PrevY;
extra->prevz = viewplayer->prev_viewz;
extra->prevangle = viewplayer->prev_viewangle;
extra->prevpitch = viewplayer->prev_viewpitch;
}
if (gamestate == GS_LEVEL)
{
int ret = G_DoSaveGameToBuffer((char *) data_ + sizeof(*extra),
size - sizeof(*extra));
if (!ret)
return false;
if (viewplayer && viewplayer->mo)
{
extra->prevx = viewplayer->mo->PrevX;
extra->prevy = viewplayer->mo->PrevY;
extra->prevz = viewplayer->prev_viewz;
extra->prevangle = viewplayer->prev_viewangle;
extra->prevpitch = viewplayer->prev_viewpitch;
}
}
extra->gametic = gametic;
extra->gametic = gametic;
extra->gameticfrac = tic_vars.frac;
extra->gameaction = gameaction;
extra->turnheld = turnheld;
extra->extra_size = sizeof(*extra);
extra->autorun = autorun;
extra->gamestate = gamestate;
extra->gameaction = gameaction;
extra->turnheld = turnheld;
extra->extra_size = sizeof(*extra);
extra->autorun = autorun;
extra->gamestate = gamestate;
extra->FinaleStage = FinaleStage;
extra->FinaleCount = FinaleCount;
extra->itemOn = itemOn;
extra->whichSkull = whichSkull;
extra->itemOn = itemOn;
extra->whichSkull = whichSkull;
extra->currentMenu = 0;
extra->set_menu_itemon = set_menu_itemon;
extra->menuactive = menuactive;
@ -1030,59 +1025,52 @@ bool retro_unserialize(const void *data_, size_t size)
int gameless = 0;
if (extra->extra_size == sizeof(*extra))
gameless = (extra->gamestate != GS_LEVEL);
if (!gameless) {
int ret = G_DoLoadGameFromBuffer((char *) data_ + extra->extra_size,
size - extra->extra_size);
if (!ret)
return false;
if (!gameless)
{
int ret = G_DoLoadGameFromBuffer((char *) data_ + extra->extra_size,
size - extra->extra_size);
if (!ret)
return false;
if (viewplayer && viewplayer->mo) {
viewplayer->mo->PrevX = extra->prevx;
viewplayer->mo->PrevY = extra->prevy;
viewplayer->prev_viewz = extra->prevz;
viewplayer->prev_viewangle = extra->prevangle;
viewplayer->prev_viewpitch = extra->prevpitch;
}
if (viewplayer && viewplayer->mo)
{
viewplayer->mo->PrevX = extra->prevx;
viewplayer->mo->PrevY = extra->prevy;
viewplayer->prev_viewz = extra->prevz;
viewplayer->prev_viewangle = extra->prevangle;
viewplayer->prev_viewpitch = extra->prevpitch;
}
}
if (extra->extra_size == sizeof(*extra))
{
unsigned i;
gametic = maketic = extra->gametic;
gameaction = extra->gameaction;
turnheld = extra->turnheld;
autorun = extra->autorun;
gamestate = extra->gamestate;
FinaleStage = extra->FinaleStage;
FinaleCount = extra->FinaleCount;
WI_Load(&extra->wi_state);
itemOn = extra->itemOn;
whichSkull = extra->whichSkull;
currentMenu = menus[extra->currentMenu];
set_menu_itemon = extra->set_menu_itemon;
for (i = 0; i < NUMKEYS; i++)
gamekeydown[i] = extra->gamekeydown[i];
for (i = 0; i < MAX_BUTTON_BINDS; i++)
old_input[i] = extra->old_input[i];
menuactive = extra->menuactive;
tic_vars.frac = extra->gameticfrac;
}
{
unsigned i;
gametic = maketic = extra->gametic;
gameaction = extra->gameaction;
turnheld = extra->turnheld;
autorun = extra->autorun;
gamestate = extra->gamestate;
FinaleStage = extra->FinaleStage;
FinaleCount = extra->FinaleCount;
WI_Load(&extra->wi_state);
itemOn = extra->itemOn;
whichSkull = extra->whichSkull;
currentMenu = menus[extra->currentMenu];
set_menu_itemon = extra->set_menu_itemon;
for (i = 0; i < NUMKEYS; i++)
gamekeydown[i] = extra->gamekeydown[i];
for (i = 0; i < MAX_BUTTON_BINDS; i++)
old_input[i] = extra->old_input[i];
menuactive = extra->menuactive;
tic_vars.frac = extra->gameticfrac;
}
return true;
}
void *retro_get_memory_data(unsigned id)
{
(void)id;
return NULL;
}
size_t retro_get_memory_size(unsigned id)
{
(void)id;
return 0;
}
void retro_cheat_reset(void)
{}
void *retro_get_memory_data(unsigned id) { return NULL; }
size_t retro_get_memory_size(unsigned id) { return 0; }
void retro_cheat_reset(void) { }
void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
@ -1266,13 +1254,14 @@ static int keyboard_lut[117][2] = {
// > Now that we process input once per game tic, the period here must
// match the internal tic rate (corresponding to default 35fps)
static const int pwm_period = TICRATE;
static bool synthetic_pwm(int amplitude, int* modulation_state)
{
*modulation_state += amplitude;
if (*modulation_state < pwm_period)
return false;
*modulation_state -= pwm_period;
return true;
*modulation_state += amplitude;
if (*modulation_state < pwm_period)
return false;
*modulation_state -= pwm_period;
return true;
}
static void process_gamepad_buttons(int16_t ret, unsigned num_buttons, action_lut_t action_lut[])
@ -1306,64 +1295,66 @@ static void process_gamepad_buttons(int16_t ret, unsigned num_buttons, action_lu
static void process_gamepad_left_analog(void)
{
unsigned i;
static bool old_input_analog_l[4];
bool new_input_analog_l[4];
int analog_l_amplitude[4];
static int analog_l_modulation_state[4];
int lsx, lsy, analog_range;
unsigned i;
static bool old_input_analog_l[4];
bool new_input_analog_l[4];
int analog_l_amplitude[4];
static int analog_l_modulation_state[4];
int lsx, lsy, analog_range;
// Only run once per game tic
if(tic_vars.frac < FRACUNIT) return;
// Increase range on menu
analog_range = (menuactive)? ANALOG_RANGE*8 : ANALOG_RANGE;
// Only run once per game tic
if (tic_vars.frac < FRACUNIT)
return;
// Increase range on menu
analog_range = (menuactive)? ANALOG_RANGE*8 : ANALOG_RANGE;
lsx = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
lsy = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
lsx = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
lsy = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
// Get movement 'amplitude' on each axis
// > x-axis
analog_l_amplitude[0] = 0;
analog_l_amplitude[1] = 0;
if (lsx > analog_deadzone)
{
// Add '1' to deal with float->int rounding accuracy loss...
// (Similarly, subtract '1' when lsx is negative...)
analog_l_amplitude[0] = 1 + pwm_period * (lsx - analog_deadzone) / (analog_range - analog_deadzone);
}
if (lsx < -analog_deadzone)
analog_l_amplitude[1] = -1 * (-1 + pwm_period * (lsx + analog_deadzone) / (analog_range - analog_deadzone));
// > y-axis
analog_l_amplitude[2] = 0;
analog_l_amplitude[3] = 0;
if (lsy > analog_deadzone)
analog_l_amplitude[2] = 1 + pwm_period * (lsy - analog_deadzone) / (analog_range - analog_deadzone);
if (lsy < -analog_deadzone)
analog_l_amplitude[3] = -1 * (-1 + pwm_period * (lsy + analog_deadzone) / (analog_range - analog_deadzone));
// Get movement 'amplitude' on each axis
// > x-axis
analog_l_amplitude[0] = 0;
analog_l_amplitude[1] = 0;
for (i = 0; i < 4; i++)
{
event_t event = {0};
// Add '1' to deal with float->int rounding accuracy loss...
// (Similarly, subtract '1' when lsx is negative...)
if (lsx > analog_deadzone)
analog_l_amplitude[0] = 1 + pwm_period * (lsx - analog_deadzone) / (analog_range - analog_deadzone);
new_input_analog_l[i] = synthetic_pwm(analog_l_amplitude[i], &analog_l_modulation_state[i]);
if (lsx < -analog_deadzone)
analog_l_amplitude[1] = -1 * (-1 + pwm_period * (lsx + analog_deadzone) / (analog_range - analog_deadzone));
// > y-axis
analog_l_amplitude[2] = 0;
analog_l_amplitude[3] = 0;
if (lsy > analog_deadzone)
analog_l_amplitude[2] = 1 + pwm_period * (lsy - analog_deadzone) / (analog_range - analog_deadzone);
if (lsy < -analog_deadzone)
analog_l_amplitude[3] = -1 * (-1 + pwm_period * (lsy + analog_deadzone) / (analog_range - analog_deadzone));
if(new_input_analog_l[i] && !old_input_analog_l[i])
{
event.type = ev_keydown;
event.data1 = *((menuactive)? left_analog_lut[i].menukey : left_analog_lut[i].gamekey);
}
for (i = 0; i < 4; i++)
{
event_t event = {0};
if(!new_input_analog_l[i] && old_input_analog_l[i])
{
event.type = ev_keyup;
event.data1 = *((menuactive)? left_analog_lut[i].menukey : left_analog_lut[i].gamekey);;
}
new_input_analog_l[i] = synthetic_pwm(analog_l_amplitude[i], &analog_l_modulation_state[i]);
if(event.type == ev_keydown || event.type == ev_keyup)
D_PostEvent(&event);
if(new_input_analog_l[i] && !old_input_analog_l[i])
{
event.type = ev_keydown;
event.data1 = *((menuactive)? left_analog_lut[i].menukey : left_analog_lut[i].gamekey);
}
old_input_analog_l[i] = new_input_analog_l[i];
}
if(!new_input_analog_l[i] && old_input_analog_l[i])
{
event.type = ev_keyup;
event.data1 = *((menuactive)? left_analog_lut[i].menukey : left_analog_lut[i].gamekey);;
}
if( event.type == ev_keydown
|| event.type == ev_keyup)
D_PostEvent(&event);
old_input_analog_l[i] = new_input_analog_l[i];
}
}
static void process_gamepad_right_analog(bool pressed_y, bool pressed_l2)
@ -1550,12 +1541,12 @@ process_input(void)
}
}
void I_StartTic (void)
void I_StartTic(void)
{
if (!input_poll_cb)
return;
input_poll_cb();
process_input();
if (!input_poll_cb)
return;
input_poll_cb();
process_input();
}
static void I_UpdateVideoMode(void)
@ -1581,13 +1572,11 @@ void I_FinishUpdate (void)
video_cb(screen_buf, SCREENWIDTH, SCREENHEIGHT, SCREENPITCH);
}
void I_SetPalette (int pal)
{
}
void I_SetPalette(int pal) { }
void I_InitGraphics(void)
{
static int firsttime=1;
static int firsttime=1;
if (firsttime)
{
@ -1595,7 +1584,6 @@ void I_InitGraphics(void)
/* Set the video mode */
I_UpdateVideoMode();
}
}
@ -1660,7 +1648,7 @@ const char *I_DoomExeDir(void)
*
* cphipps - simple test for trailing slash on dir names
*/
dbool HasTrailingSlash(const char* dn)
dbool HasTrailingSlash(const char* dn)
{
size_t dn_len = strlen(dn);
return ( dn && ((dn[dn_len - 1] == '/') || (dn[dn_len - 1] == '\\')));
@ -1682,7 +1670,7 @@ char* FindFileInDir(const char* dir, const char* wfname, const char* ext)
{
if (!(p = malloc(pl)))
return NULL;
sprintf(p, "%s", wfname);
strcpy(p, wfname);
}
else
{
@ -1725,32 +1713,33 @@ char* I_FindFile(const char* wfname, const char* ext)
environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_dir);
if (system_dir && (prboom_system_dir = malloc(strlen(system_dir) + 8)))
{
sprintf(prboom_system_dir, "%s%c%s", system_dir, DIR_SLASH, "prboom");
p = FindFileInDir(prboom_system_dir, wfname, ext);
free(prboom_system_dir);
if (!p)
p = FindFileInDir(system_dir, wfname, ext);
sprintf(prboom_system_dir, "%s%c%s", system_dir, DIR_SLASH, "prboom");
p = FindFileInDir(prboom_system_dir, wfname, ext);
free(prboom_system_dir);
prboom_system_dir = NULL;
if (!p)
p = FindFileInDir(system_dir, wfname, ext);
}
// If not found, check on parent folders recursively (if configured to do so)
if (!p && find_recursive_on)
{
if ((dir = malloc(strlen(g_wad_dir) + 1)) != NULL)
{
strcpy(dir, g_wad_dir);
for (i = strlen(dir)-1; i > 1; dir[i--] = '\0')
{
if((dir[i] == '/' || dir[i] == '\\')
&& dir[i-1] != dir[i])
if ((dir = malloc(strlen(g_wad_dir) + 1)) != NULL)
{
strcpy(dir, g_wad_dir);
for (i = strlen(dir)-1; i > 1; dir[i--] = '\0')
{
dir[i] = '\0'; // remove leading slash
p = FindFileInDir(dir, wfname, ext);
if(p != NULL)
break;
if((dir[i] == '/' || dir[i] == '\\')
&& dir[i-1] != dir[i])
{
dir[i] = '\0'; // remove leading slash
p = FindFileInDir(dir, wfname, ext);
if(p != NULL)
break;
}
}
}
free(dir);
}
free(dir);
}
}
}
return p;
@ -1813,27 +1802,27 @@ int lprintf(OutputLevels pri, const char *s, ...)
if (log_cb)
{
enum retro_log_level lvl;
switch(pri)
{
case LO_DEBUG:
lvl = RETRO_LOG_DEBUG;
break;
case LO_CONFIRM:
case LO_INFO:
lvl = RETRO_LOG_INFO;
break;
case LO_WARN:
lvl = RETRO_LOG_WARN;
break;
case LO_ERROR:
case LO_FATAL:
default:
lvl = RETRO_LOG_ERROR;
break;
}
enum retro_log_level lvl;
switch(pri)
{
case LO_DEBUG:
lvl = RETRO_LOG_DEBUG;
break;
case LO_CONFIRM:
case LO_INFO:
lvl = RETRO_LOG_INFO;
break;
case LO_WARN:
lvl = RETRO_LOG_WARN;
break;
case LO_ERROR:
case LO_FATAL:
default:
lvl = RETRO_LOG_ERROR;
break;
}
log_cb(lvl, "%s", msg);
log_cb(lvl, "%s", msg);
}
return 0;

View File

@ -101,20 +101,20 @@ music_player_t* current_player = NULL;
static channel_t channels[NUM_CHANNELS];
int vol_lookup[128*256];
int vol_lookup[128*256];
static double R_ceil (double x)
static double R_ceil(double x)
{
if (x > LONG_MAX)
return x; /* big floats are all ints */
return ((long)(x+(0.99999999999999997)));
}
static const double twoTo52 = 4.50359962737049600e15; /* 0x1p52 */
static double R_floor(double x)
{
double y;
static const double twoTo52 = 4.50359962737049600e15; /* 0x1p52 */
union {double f; uint64_t i;} u = {x};
int e = u.i >> 52 & 0x7ff;
@ -134,14 +134,10 @@ static double R_floor(double x)
}
/* i_sound */
static void I_SndMixResetChannel (int channum)
{
memset (&channels[channum], 0, sizeof(channel_t));
}
/* This function loads the sound data from the WAD lump
* for a single sound effect. */
static void* I_SndLoadSample (const char* sfxname, int* len)
static void* I_SndLoadSample(const char* sfxname, int* len)
{
int i, x, padded_sfx_len, sfxlump_num, sfxlump_len;
char sfxlump_name[20];
@ -164,21 +160,21 @@ static void* I_SndLoadSample (const char* sfxname, int* len)
if (sfxlump_len < 9)
return 0;
// load it
sfxlump_data = W_CacheLumpNum (sfxlump_num);
sfxlump_sound = sfxlump_data + 8;
sfxlump_len -= 8;
/* load it */
sfxlump_data = W_CacheLumpNum (sfxlump_num);
sfxlump_sound = sfxlump_data + 8;
sfxlump_len -= 8;
// get original sample rate from DMX header
memcpy (&orig_rate, sfxlump_data+2, 2);
orig_rate = SHORT (orig_rate);
/* get original sample rate from DMX header */
memcpy(&orig_rate, sfxlump_data+2, 2);
orig_rate = SHORT (orig_rate);
times = 48000.0f / (float)orig_rate;
times = 48000.0f / (float)orig_rate;
padded_sfx_len = ((sfxlump_len*R_ceil(times) + (SAMPLECOUNT_35-1)) / SAMPLECOUNT_35) * SAMPLECOUNT_35;
padded_sfx_len = ((sfxlump_len * R_ceil(times) + (SAMPLECOUNT_35-1)) / SAMPLECOUNT_35) * SAMPLECOUNT_35;
padded_sfx_data = (uint8_t*)malloc(padded_sfx_len);
for (i=0; i < padded_sfx_len; i++)
for (i = 0; i < padded_sfx_len; i++)
{
x = R_floor ((float)i/times);
@ -194,7 +190,6 @@ static void* I_SndLoadSample (const char* sfxname, int* len)
return (void *)(padded_sfx_data);
}
//
// SFX API
// Note: this was called by S_Init.
@ -215,14 +210,14 @@ void I_SetChannels(void)
int i, j;
/* Okay, reset internal mixing channels to zero. */
for (i=0; i<NUM_CHANNELS; i++)
I_SndMixResetChannel(i);
for (i = 0; i < NUM_CHANNELS; i++)
memset(&channels[i], 0, sizeof(channel_t));
/* Generates volume lookup tables which also turn the unsigned
* samples into signed samples. */
for (i=0 ; i<128 ; i++)
for (i = 0; i < 128; i++)
{
for (j=0 ; j<256 ; j++)
for (j = 0; j < 256; j++)
vol_lookup[i*256+j] = (i*(j-128)*256)/127;
}
}
@ -230,16 +225,16 @@ void I_SetChannels(void)
void I_SetSfxVolume(int volume)
{
snd_SfxVolume = volume;
snd_SfxVolume = volume;
}
void I_SetMusicVolume(int volume)
{
snd_MusicVolume = volume;
snd_MusicVolume = volume;
#ifdef MUSIC_SUPPORT
if (current_player)
current_player->setvolume(volume);
if (current_player)
current_player->setvolume(volume);
#endif
}
@ -248,7 +243,6 @@ void I_SetMusicVolume(int volume)
int I_GetSfxLumpNum(sfxinfo_t* sfx)
{
char namebuf[9];
sprintf(namebuf, "ds%s", sfx->name);
return W_GetNumForName(namebuf);
}
@ -257,11 +251,11 @@ void I_StopSound (int handle)
{
int i;
for (i=0; i<NUM_CHANNELS; i++)
for (i = 0; i < NUM_CHANNELS; i++)
{
if (channels[i].handle==handle)
if (channels[i].handle == handle)
{
I_SndMixResetChannel(i);
memset(&channels[i], 0, sizeof(channel_t));
return;
}
}
@ -291,11 +285,11 @@ int I_StartSound (int id, int channel, int vol, int sep, int pitch, int priority
return -1;
// Loop all channels to find a free slot.
slot = -1;
slot = -1;
oldesttics = gametic;
oldestslot = 0;
for (i=0; i<NUM_CHANNELS; i++)
for (i = 0; i < NUM_CHANNELS; i++)
{
if (!channels[i].snd_start_ptr) // not playing
{
@ -318,26 +312,26 @@ int I_StartSound (int id, int channel, int vol, int sep, int pitch, int priority
// Set pointers to raw sound data start & end.
channels[slot].snd_start_ptr = (uint8_t*)S_sfx[id].data;
channels[slot].snd_end_ptr = channels[slot].snd_start_ptr + lengths[id];
channels[slot].snd_end_ptr = channels[slot].snd_start_ptr + lengths[id];
// Save starting gametic.
channels[slot].starttic = gametic;
channels[slot].starttic = gametic;
sep += 1;
// Per left/right channel.
// x^2 seperation,
// adjust volume properly.
leftvol = vol - ((vol*sep*sep) >> 16); ///(256*256);
sep -= 257;
leftvol = vol - ((vol*sep*sep) >> 16); ///(256*256);
sep -= 257;
rightvol = vol - ((vol*sep*sep) >> 16);
// Sanity check, clamp volume.
if (rightvol < 0 || rightvol > 127)
I_Error("addsfx: rightvol out of bounds");
I_Error("addsfx: rightvol out of bounds");
if (leftvol < 0 || leftvol > 127)
I_Error("addsfx: leftvol out of bounds");
I_Error("addsfx: leftvol out of bounds");
// Get the proper lookup table piece
// for this volume level???
@ -351,12 +345,11 @@ int I_StartSound (int id, int channel, int vol, int sep, int pitch, int priority
return currenthandle;
}
dbool I_SoundIsPlaying (int handle)
{
int i;
for (i=0; i<NUM_CHANNELS; i++)
for (i = 0; i < NUM_CHANNELS; i++)
{
if (channels[i].handle==handle)
return 1;
@ -379,7 +372,6 @@ dbool I_SoundIsPlaying (int handle)
// This function currently supports only 16bit.
//
void I_UpdateSound(void)
{
// Mix current sound data. Data, from raw sound, for right and left.
@ -388,21 +380,19 @@ void I_UpdateSound(void)
int16_t mad_audio_buf[SAMPLECOUNT_35 * 2] = { 0 }; // initialize all zero
// Pointers in global mixbuffer, left, right, end.
int16_t *leftout, *rightout, *leftend;
int16_t *leftend;
// Left and right channel are in global mixbuffer, alternating.
leftout = mixbuffer;
rightout = mixbuffer+1;
step = 2;
frames = 0;
int16_t *leftout = mixbuffer;
int16_t *rightout = mixbuffer+1;
step = 2;
frames = 0;
out_frames = (tic_vars.sample_step)? tic_vars.sample_step : SAMPLECOUNT_35;
#ifdef MUSIC_SUPPORT
if (music_handle && current_player)
{
current_player->render(mad_audio_buf, out_frames);
}
else
#endif
memset(mad_audio_buf, 0, out_frames * 4);
@ -419,7 +409,6 @@ void I_UpdateSound(void)
dl = mad_audio_buf[frames * 2 + 0];
dr = mad_audio_buf[frames * 2 + 1];
for (chan=0; chan<NUM_CHANNELS; chan++)
{
// Check channel, if active.
@ -436,7 +425,7 @@ void I_UpdateSound(void)
channels[chan].snd_start_ptr++;
if (!(channels[chan].snd_start_ptr < channels[chan].snd_end_ptr))
I_SndMixResetChannel (chan);
memset(&channels[chan], 0, sizeof(channel_t));
}
}
@ -475,14 +464,14 @@ void I_UpdateSoundParams (int handle, int vol, int sep, int pitch)
{
int rightvol, leftvol, i;
for (i=0; i<NUM_CHANNELS; i++)
for (i = 0; i < NUM_CHANNELS; i++)
{
if (channels[i].handle==handle)
{
sep += 1;
sep += 1;
leftvol = vol - ((vol*sep*sep) >> 16); ///(256*256);
sep -= 257;
leftvol = vol - ((vol*sep*sep) >> 16); ///(256*256);
sep -= 257;
rightvol = vol - ((vol*sep*sep) >> 16);
if (rightvol < 0 || rightvol > 127)
@ -513,26 +502,22 @@ void I_ShutdownSound(void)
}
}
void I_InitSound(void)
{
int i;
memset (&lengths, 0, sizeof(int)*NUMSFX);
for (i=1 ; i<NUMSFX ; i++)
memset(&lengths, 0, sizeof(int)*NUMSFX);
for (i = 1; i < NUMSFX; i++)
{
// Alias? Example is the chaingun sound linked to pistol.
if (!S_sfx[i].link)
{
// Load data from WAD file.
S_sfx[i].data = I_SndLoadSample( S_sfx[i].name, &lengths[i] );
}
else
{
// Previously loaded already?
S_sfx[i].data = S_sfx[i].link->data;
lengths[i] = lengths[(S_sfx[i].link - S_sfx)/sizeof(sfxinfo_t)];
}
// Alias? Example is the chaingun sound linked to pistol.
if (!S_sfx[i].link) // Load data from WAD file.
S_sfx[i].data = I_SndLoadSample( S_sfx[i].name, &lengths[i] );
else // Previously loaded already?
{
S_sfx[i].data = S_sfx[i].link->data;
lengths[i] = lengths[(S_sfx[i].link - S_sfx)/sizeof(sfxinfo_t)];
}
}
I_SetChannels();
@ -541,20 +526,18 @@ void I_InitSound(void)
log_cb(RETRO_LOG_INFO, "I_InitSound: \n");
}
dbool I_AnySoundStillPlaying(void)
dbool I_AnySoundStillPlaying(void)
{
int i;
dbool result = false;
int i;
dbool result = false;
for (i=0; i<MAX_CHANNELS; i++)
result |= channelinfo[i].data != NULL;
for (i = 0; i < MAX_CHANNELS; i++)
result |= channelinfo[i].data != NULL;
return result;
return result;
}
//
// MUSIC API.
//
/* MUSIC API */
static int looping=0;
static int musicdies=-1;
@ -562,21 +545,22 @@ static int musicdies=-1;
void I_PlaySong(int handle, int looping)
{
(void)handle;
musicdies = gametic + TICRATE*30; // ?
musicdies = gametic + TICRATE * 30;
#ifdef MUSIC_SUPPORT
if (current_player) {
current_player->play(music_handle, looping);
current_player->setvolume(snd_MusicVolume);
if (current_player)
{
current_player->play(music_handle, looping);
current_player->setvolume(snd_MusicVolume);
}
#endif
}
void I_PauseSong (int handle)
{
(void)handle;
if (current_player)
current_player->pause();
(void)handle;
if (current_player)
current_player->pause();
}
void I_ResumeSong (int handle)
@ -584,7 +568,7 @@ void I_ResumeSong (int handle)
(void)handle;
#ifdef MUSIC_SUPPORT
if (current_player)
current_player->resume();
current_player->resume();
#endif
}
@ -595,8 +579,8 @@ void I_StopSong(int handle)
looping = 0;
musicdies = 0;
if (current_player)
current_player->stop();
if (current_player)
current_player->stop();
}
void I_UnRegisterSong(int handle)
@ -618,85 +602,81 @@ int I_RegisterSong(const void* data, size_t len)
music_handle = NULL;
#if defined(MUSIC_SUPPORT)
// Now you can hear title music in deca.wad
// http://www.doomworld.com/idgames/index.php?id=8808
// Ability to use mp3 and ogg as inwad lump
if (len > 4 && memcmp(data, "MUS", 3) != 0)
{
// The header has no MUS signature
// Let's try to load this song with the music players
int i;
for (i = 0; i < NUM_MUS_PLAYERS; i++)
{
music_handle = music_players[i]->registersong(data, len);
if (music_handle) {
current_player = (music_player_t*)music_players[i];
break;
}
}
// The header has no MUS signature
// Let's try to load this song with the music players
int i;
for (i = 0; i < NUM_MUS_PLAYERS; i++)
{
music_handle = music_players[i]->registersong(data, len);
if (music_handle)
{
current_player = (music_player_t*)music_players[i];
break;
}
}
}
// e6y: from Chocolate-Doom
// Assume a MUS file and try to convert
if (!music_handle)
{
MEMFILE *instream;
MEMFILE *outstream;
int result;
int result;
MEMFILE *instream = mem_fopen_read(data, len);
MEMFILE *outstream = mem_fopen_write();
instream = mem_fopen_read(data, len);
outstream = mem_fopen_write();
// e6y: from chocolate-doom
// New mus -> mid conversion code thanks to Ben Ryves <benryves@benryves.com>
// This plays back a lot of music closer to Vanilla Doom - eg. tnt.wad map02
result = mus2mid(instream, outstream);
// e6y: from chocolate-doom
// New mus -> mid conversion code thanks to Ben Ryves <benryves@benryves.com>
// This plays back a lot of music closer to Vanilla Doom - eg. tnt.wad map02
result = mus2mid(instream, outstream);
if (result != 0)
{
size_t muslen = len;
const unsigned char *musptr = data;
if (result != 0)
{
size_t muslen = len;
const unsigned char *musptr = data;
// haleyjd 04/04/10: scan forward for a MUS header. Evidently DMX was
// capable of doing this, and would skip over any intervening data. That,
// or DMX doesn't use the MUS header at all somehow.
while (musptr < (const unsigned char*)data + len - sizeof(musheader))
{
// if we found a likely header start, reset the mus pointer to that location,
// otherwise just leave it alone and pray.
if (!strncmp((const char*)musptr, "MUS\x1a", 4))
// haleyjd 04/04/10: scan forward for a MUS header. Evidently DMX was
// capable of doing this, and would skip over any intervening data. That,
// or DMX doesn't use the MUS header at all somehow.
while (musptr < (const unsigned char*)data + len - sizeof(musheader))
{
mem_fclose(instream);
instream = mem_fopen_read(musptr, muslen);
result = mus2mid(instream, outstream);
break;
// if we found a likely header start, reset the mus pointer to that location,
// otherwise just leave it alone and pray.
if (!strncmp((const char*)musptr, "MUS\x1a", 4))
{
mem_fclose(instream);
instream = mem_fopen_read(musptr, muslen);
result = mus2mid(instream, outstream);
break;
}
musptr++;
muslen--;
}
}
musptr++;
muslen--;
}
}
if (result == 0)
{
void *outbuf;
size_t outbuf_len;
mem_get_buf(outstream, &outbuf, &outbuf_len);
music_handle = opl_synth_player.registersong(outbuf, outbuf_len);
if(music_handle)
current_player = (music_player_t*)&opl_synth_player;
}
if (result == 0)
{
void *outbuf;
size_t outbuf_len;
mem_get_buf(outstream, &outbuf, &outbuf_len);
music_handle = opl_synth_player.registersong(outbuf, outbuf_len);
if(music_handle)
current_player = (music_player_t*)&opl_synth_player;
}
mem_fclose(instream);
mem_fclose(outstream);
mem_fclose(instream);
mem_fclose(outstream);
}
// Failed to load
/* Failed to load */
if (!music_handle)
lprintf(LO_ERROR, "I_RegisterSong: couldn't load music song.\n");
lprintf(LO_ERROR, "I_RegisterSong: couldn't load music song.\n");
#endif
return !!music_handle;
@ -714,9 +694,7 @@ int I_QrySongPlaying(int handle)
int I_RegisterMusicFile( const char* filename, musicinfo_t *song )
{
#ifdef MUSIC_SUPPORT
int len;
len = M_ReadFile(filename, (uint8_t**) &song_data);
int len = M_ReadFile(filename, (uint8_t**) &song_data);
if (len == -1)
{
if (log_cb)
@ -733,8 +711,8 @@ int I_RegisterMusicFile( const char* filename, musicinfo_t *song )
return 1;
}
song->data = 0;
song->handle = 0;
song->data = 0;
song->handle = 0;
song->lumpnum = 0;
#endif
return 0;
@ -745,51 +723,49 @@ int I_RegisterMusicFile( const char* filename, musicinfo_t *song )
void I_ResampleStream (void *dest, unsigned nsamp, void (*proc)(void *dest, unsigned nsamp),
unsigned sratein, unsigned srateout)
{
unsigned i;
int j = 0;
int16_t *sout = dest;
static int16_t *sin = NULL;
static unsigned sinsamp = 0;
static unsigned remainder = 0;
unsigned step = (sratein << 16) / (unsigned) srateout;
unsigned nreq = (step * nsamp + remainder) >> 16;
unsigned i;
int j = 0;
int16_t *sout = dest;
static int16_t *sin = NULL;
static unsigned sinsamp = 0;
static unsigned remainder = 0;
unsigned step = (sratein << 16) / (unsigned) srateout;
unsigned nreq = (step * nsamp + remainder) >> 16;
if (nreq > sinsamp)
{
sin = realloc (sin, (nreq + 1) * 4);
if (!sinsamp) // avoid pop when first starting stream
sin[0] = sin[1] = 0;
sinsamp = nreq;
}
if (nreq > sinsamp)
{
sin = realloc(sin, (nreq + 1) * 4);
if (!sinsamp) // avoid pop when first starting stream
sin[0] = sin[1] = 0;
sinsamp = nreq;
}
proc (sin + 2, nreq);
proc (sin + 2, nreq);
for (i = 0; i < nsamp; i++)
{
*sout++ = ((unsigned) sin[j + 0] * (0x10000 - remainder) +
(unsigned) sin[j + 2] * remainder) >> 16;
*sout++ = ((unsigned) sin[j + 1] * (0x10000 - remainder) +
(unsigned) sin[j + 3] * remainder) >> 16;
remainder += step;
j += remainder >> 16 << 1;
remainder &= 0xffff;
}
sin[0] = sin[nreq * 2];
sin[1] = sin[nreq * 2 + 1];
for (i = 0; i < nsamp; i++)
{
*sout++ = ((unsigned) sin[j + 0] * (0x10000 - remainder) +
(unsigned) sin[j + 2] * remainder) >> 16;
*sout++ = ((unsigned) sin[j + 1] * (0x10000 - remainder) +
(unsigned) sin[j + 3] * remainder) >> 16;
remainder += step;
j += remainder >> 16 << 1;
remainder &= 0xffff;
}
sin[0] = sin[nreq * 2];
sin[1] = sin[nreq * 2 + 1];
}
void I_InitMusic(void)
{
int i;
log_cb(RETRO_LOG_INFO, "I_InitMusic\n");
for (i = 0; music_players[i]; i++)
music_players[i]->init (SAMPLERATE);
int i;
for (i = 0; music_players[i]; i++)
music_players[i]->init (SAMPLERATE);
}
void I_ShutdownMusic(void)
{
int i;
for (i = 0; music_players[i]; i++)
music_players[i]->shutdown ();
int i;
for (i = 0; music_players[i]; i++)
music_players[i]->shutdown ();
}

View File

@ -39,7 +39,6 @@
#include "doomtype.h"
#include "v_video.h"
bool I_PreInitGraphics(void); /* CPhipps - do stuff immediately on start */
void I_SetRes(void); /* set resolution */
void I_InitGraphics (void);
void I_ShutdownGraphics(void);