From 7d52913adde150009fb47ad06eac85a5c1451cbf Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 25 Apr 2014 02:38:57 +0400 Subject: [PATCH 1/7] Make endianess detection more flexible And fix it on FreeBSD, where _BIG_ENDIAN is not a flag indicating big endian machine, but a constant to compare _BYTE_ORDER to. --- src/endian.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/m_swap.h | 4 +++- src/md5.c | 9 ++------- 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 src/endian.h diff --git a/src/endian.h b/src/endian.h new file mode 100644 index 000000000..268c08023 --- /dev/null +++ b/src/endian.h @@ -0,0 +1,45 @@ +// SONIC ROBO BLAST 2 +//----------------------------------------------------------------------------- +// Copyright (C) 2014 by Sonic Team Junior. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file endian.h +/// \brief Endian detection + +#ifndef __ENDIAN__ +#define __ENDIAN__ + +#if defined(SRB2_BIG_ENDIAN) || defined(SRB2_LITTLE_ENDIAN) + // defined externally +#else + #if defined(__FreeBSD__) + // on FreeBSD, _BIG_ENDIAN is a constant to compare + // _BYTE_ORDER to, not a big-endianess flag + #include + #if _BYTE_ORDER == _BIG_ENDIAN + #define SRB2_BIG_ENDIAN + #else + #define SRB2_LITTLE_ENDIAN + #endif + #elif defined(__BYTE_ORDER__) + // defined by at least gcc and clang + #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + #define SRB2_BIG_ENDIAN + #else + #define SRB2_LITTLE_ENDIAN + #endif + #else + // check used in vanilla SRB2 (may work incorrectly if + // _BIG_ENDIAN is used as on FreeBSD) + #if defined(_BIG_ENDIAN) + #define SRB2_BIG_ENDIAN + #else + #define SRB2_LITTLE_ENDIAN + #endif + #endif +#endif + +#endif //__ENDIAN__ diff --git a/src/m_swap.h b/src/m_swap.h index ed723dff8..74e6d119e 100644 --- a/src/m_swap.h +++ b/src/m_swap.h @@ -14,9 +14,11 @@ #ifndef __M_SWAP__ #define __M_SWAP__ +#include "endian.h" + // Endianess handling. // WAD files are stored little endian. -#ifdef _BIG_ENDIAN +#ifdef SRB2_BIG_ENDIAN #define SHORT(x) ((INT16)(\ (((UINT16)(x) & (UINT16)0x00ffU) << 8) \ diff --git a/src/md5.c b/src/md5.c index 73b767d65..aeaac2cde 100644 --- a/src/md5.c +++ b/src/md5.c @@ -44,14 +44,9 @@ #include "md5.h" -#ifdef _LIBC - #include - #if __BYTE_ORDER == __BIG_ENDIAN - #define WORDS_BIGENDIAN 1 - #endif -#endif +#include "endian.h" -#if defined (WORDS_BIGENDIAN) || defined (_BIG_ENDIAN) +#if defined (SRB2_BIG_ENDIAN) #define SWAP(n) \ (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) #else From 60a115b0a75e7677158ccc53479945e4e11be854 Mon Sep 17 00:00:00 2001 From: ilag11111 Date: Fri, 25 Apr 2014 00:37:13 -0700 Subject: [PATCH 2/7] Fix (Linux) 64-bit crash in CEZ3 (playback of sfx_litng3). --- src/sdl/mixer_sound.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 6888331de..84003a87c 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -152,7 +152,7 @@ static Mix_Chunk *ds2chunk(void *stream) if (!(frac & 0xFFFF)) // other solid multiples (change if FRACBITS != 16) newsamples = samples * (frac >> FRACBITS); else // strange and unusual fractional frequency steps, plus anything higher than 44100hz. - newsamples = FixedMul(frac, samples) + 1; // add 1 sample for security! the code below rounds up. + newsamples = FixedMul(frac, samples) + 2; // add 2 samples for security! the code below rounds up. if (newsamples >= UINT32_MAX>>2) return NULL; // would and/or did wrap, can't store. break; From 3f170769da258f0e12da941f625b9f707bed1c35 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 24 Apr 2014 20:36:18 +0400 Subject: [PATCH 3/7] Add simple CMakeLists.txt --- CMakeLists.txt | 167 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..c55d63e69 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,167 @@ +# +# Simple CMakeLists for Sonic Robo Blast 2 +# +# Tweaked for FreeBSD, but should be extendable to support other systems +# +PROJECT(SRB2) + +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) + +# +# Dependencies +# +FIND_PACKAGE(SDL) +FIND_PACKAGE(SDL_mixer) +FIND_PACKAGE(PNG) + +# +# Common stuff +# + +# not added defines: +# -DUSEASM - unportable assembler not needed +# -DHAVE_PNG - does not build (incorrect use of PNG_EXPORT etc.) +ADD_DEFINITIONS(-DDIRECTFULLSCREEN -DSDL -DHAVE_MIXER -DNOHW -DHW3SOUND -DHAVE_BLUA) + +SET(SDL_DIR sdl) # may be set to SDL2 optionally + +SET(COMMON_SRCS + src/${SDL_DIR}/dosstr.c + src/${SDL_DIR}/endtxt.c + src/${SDL_DIR}/hwsym_sdl.c + src/${SDL_DIR}/i_cdmus.c + src/${SDL_DIR}/i_main.c + src/${SDL_DIR}/i_net.c + src/${SDL_DIR}/i_system.c + src/${SDL_DIR}/i_video.c + src/${SDL_DIR}/mixer_sound.c + src/am_map.c + src/b_bot.c + src/blua/lapi.c + src/blua/lauxlib.c + src/blua/lbaselib.c + src/blua/lcode.c + src/blua/ldebug.c + src/blua/ldo.c + src/blua/ldump.c + src/blua/lfunc.c + src/blua/lgc.c + src/blua/linit.c + src/blua/llex.c + src/blua/lmem.c + src/blua/lobject.c + src/blua/lopcodes.c + src/blua/lparser.c + src/blua/lstate.c + src/blua/lstring.c + src/blua/lstrlib.c + src/blua/ltable.c + src/blua/ltablib.c + src/blua/ltm.c + src/blua/lundump.c + src/blua/lvm.c + src/blua/lzio.c + src/command.c + src/comptime.c + src/console.c + src/d_clisrv.c + src/d_main.c + src/d_net.c + src/d_netcmd.c + src/d_netfil.c + src/dehacked.c + src/f_finale.c + src/f_wipe.c + src/filesrch.c + src/g_game.c + src/g_input.c + src/hardware/hw3sound.c + src/hu_stuff.c + src/i_tcp.c + src/info.c + src/lua_baselib.c + src/lua_consolelib.c + src/lua_hooklib.c + src/lua_hudlib.c + src/lua_infolib.c + src/lua_maplib.c + src/lua_mathlib.c + src/lua_mobjlib.c + src/lua_playerlib.c + src/lua_script.c + src/lua_skinlib.c + src/lua_thinkerlib.c + src/lzf.c + src/m_anigif.c + src/m_argv.c + src/m_bbox.c + src/m_cheat.c + src/m_cond.c + src/m_fixed.c + src/m_menu.c + src/m_misc.c + src/m_queue.c + src/m_random.c + src/md5.c + src/mserv.c + src/p_ceilng.c + src/p_enemy.c + src/p_fab.c + src/p_floor.c + src/p_inter.c + src/p_lights.c + src/p_map.c + src/p_maputl.c + src/p_mobj.c + src/p_polyobj.c + src/p_saveg.c + src/p_setup.c + src/p_sight.c + src/p_spec.c + src/p_telept.c + src/p_tick.c + src/p_user.c + src/r_bsp.c + src/r_data.c + src/r_draw.c + src/r_main.c + src/r_plane.c + src/r_segs.c + src/r_sky.c + src/r_splats.c + src/r_things.c + src/s_sound.c + src/screen.c + src/sounds.c + src/st_stuff.c + src/string.c + src/tables.c + src/v_video.c + src/w_wad.c + src/y_inter.c + src/z_zone.c +) + +# +# Platform-specific stuff +# +IF(${CMAKE_SYSTEM} MATCHES "FreeBSD") + FIND_LIBRARY(KVM_LIBRARY NAMES kvm) + IF(KVM_LIBRARY) + MESSAGE(STATUS "Found libkvm: ${KVM_LIBRARY}") + SET(EXTRA_LIBRARIES ${EXTRA_LIBRARIES} ${KVM_LIBRARY}) + ELSE(KVM_LIBRARY) + MESSAGE(FATAL_ERROR "Could not find libkvm!") + ENDIF(KVM_LIBRARY) + ADD_DEFINITIONS(-DUNIXCOMMON -DLINUX -DFREEBSD) +ELSE(${CMAKE_SYSTEM} MATCHES "FreeBSD") + ADD_DEFINITIONS(-DUNIXCOMMON -DLINUX) + MESSAGE(WARNING "No specific settings for you system, it may be not supported!") +ENDIF(${CMAKE_SYSTEM} MATCHES "FreeBSD") + +# +# Targets +# +INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIRS} ${SDL_INCLUDE_DIR} ${SDL_MIXER_INCLUDE_DIRS}) +ADD_EXECUTABLE(SRB2 ${COMMON_SRCS}) +TARGET_LINK_LIBRARIES(SRB2 ${SDL_LIBRARY} ${SDL_MIXER_LIBRARIES} ${EXTRA_LIBRARIES}) From dad0ad59c18ec03d74da6f45f001c07bb1053e94 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 25 Apr 2014 02:59:08 +0400 Subject: [PATCH 4/7] Add Linux support to CMake --- CMakeLists.txt | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c55d63e69..0cffa367e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,6 @@ # # Simple CMakeLists for Sonic Robo Blast 2 # -# Tweaked for FreeBSD, but should be extendable to support other systems -# PROJECT(SRB2) CMAKE_MINIMUM_REQUIRED(VERSION 2.8) @@ -19,9 +17,8 @@ FIND_PACKAGE(PNG) # # not added defines: -# -DUSEASM - unportable assembler not needed # -DHAVE_PNG - does not build (incorrect use of PNG_EXPORT etc.) -ADD_DEFINITIONS(-DDIRECTFULLSCREEN -DSDL -DHAVE_MIXER -DNOHW -DHW3SOUND -DHAVE_BLUA) +ADD_DEFINITIONS(-DDIRECTFULLSCREEN -DSDL -DHAVE_MIXER -DNOHW -DHW3SOUND -DHAVE_BLUA -DNOASM) SET(SDL_DIR sdl) # may be set to SDL2 optionally @@ -145,15 +142,24 @@ SET(COMMON_SRCS # # Platform-specific stuff # + +MACRO(EXTRALIB NAME) + FIND_LIBRARY(${NAME}_LIBRARY NAMES ${NAME}) + IF(${NAME}_LIBRARY) + MESSAGE(STATUS "Found lib${NAME}: ${${NAME}_LIBRARY}") + SET(EXTRA_LIBRARIES ${EXTRA_LIBRARIES} ${${NAME}_LIBRARY}) + ELSE(${NAME}_LIBRARY) + MESSAGE(FATAL_ERROR "Could not find lib${NAME}!") + ENDIF(${NAME}_LIBRARY) +ENDMACRO(EXTRALIB) + IF(${CMAKE_SYSTEM} MATCHES "FreeBSD") - FIND_LIBRARY(KVM_LIBRARY NAMES kvm) - IF(KVM_LIBRARY) - MESSAGE(STATUS "Found libkvm: ${KVM_LIBRARY}") - SET(EXTRA_LIBRARIES ${EXTRA_LIBRARIES} ${KVM_LIBRARY}) - ELSE(KVM_LIBRARY) - MESSAGE(FATAL_ERROR "Could not find libkvm!") - ENDIF(KVM_LIBRARY) ADD_DEFINITIONS(-DUNIXCOMMON -DLINUX -DFREEBSD) + EXTRALIB(kvm) +ELSEIF(${CMAKE_SYSTEM} MATCHES "Linux") + ADD_DEFINITIONS(-DUNIXCOMMON -DLINUX) + EXTRALIB(m) + EXTRALIB(rt) ELSE(${CMAKE_SYSTEM} MATCHES "FreeBSD") ADD_DEFINITIONS(-DUNIXCOMMON -DLINUX) MESSAGE(WARNING "No specific settings for you system, it may be not supported!") From f7bbf8c6d0d6409577b0fe17564cc9351574ba05 Mon Sep 17 00:00:00 2001 From: ilag11111 Date: Fri, 25 Apr 2014 12:42:43 -0700 Subject: [PATCH 5/7] Improved fix by changing the formula for how memory is allocated to arbitrary sample rate conversion. --- src/sdl/mixer_sound.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 84003a87c..717f3e61c 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -152,7 +152,7 @@ static Mix_Chunk *ds2chunk(void *stream) if (!(frac & 0xFFFF)) // other solid multiples (change if FRACBITS != 16) newsamples = samples * (frac >> FRACBITS); else // strange and unusual fractional frequency steps, plus anything higher than 44100hz. - newsamples = FixedMul(frac, samples) + 2; // add 2 samples for security! the code below rounds up. + newsamples=(samples/freq + 1) *44100 ; //Result of division is not fractional, so 1 is added to ensure a roundup rather than truncation. if (newsamples >= UINT32_MAX>>2) return NULL; // would and/or did wrap, can't store. break; From b21d5c55c91e564729031deaa036c36aa386f701 Mon Sep 17 00:00:00 2001 From: ilag11111 Date: Fri, 25 Apr 2014 13:17:05 -0700 Subject: [PATCH 6/7] Uses fixed-point math to properly calculate the exact amount of space needed. --- src/sdl/mixer_sound.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 717f3e61c..98159b473 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -152,7 +152,7 @@ static Mix_Chunk *ds2chunk(void *stream) if (!(frac & 0xFFFF)) // other solid multiples (change if FRACBITS != 16) newsamples = samples * (frac >> FRACBITS); else // strange and unusual fractional frequency steps, plus anything higher than 44100hz. - newsamples=(samples/freq + 1) *44100 ; //Result of division is not fractional, so 1 is added to ensure a roundup rather than truncation. + newsamples = FixedMul(FixedDiv(samples, freq), 44100) + 1; // add 1 to counter truncation. if (newsamples >= UINT32_MAX>>2) return NULL; // would and/or did wrap, can't store. break; From 1af18c7013e4a13a5c51cdf0eed3fe88ddda1c88 Mon Sep 17 00:00:00 2001 From: ilag Date: Fri, 2 May 2014 14:24:20 -0700 Subject: [PATCH 7/7] Redo all changes I ended up losing trying to undo the mess I made. Also, fix the issue pointed out by Alam. --- src/hardware/hw_main.c | 5 +++-- src/p_mobj.c | 4 ++-- src/r_defs.h | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 0db3e9034..47ea88ce8 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -2857,9 +2857,10 @@ static void HWR_Subsector(size_t num) #ifdef DOPLANES // -------------------- WATER IN DEV. TEST ------------------------ //dck hack : use abs(tag) for waterheight - if (gr_frontsector->tag < 0) + //ilag : Since we changed to UINT16 for sector tags, simulate INT16 + if (gr_frontsector->tag > 32767) { - wh = ((-gr_frontsector->tag) <tag) < gr_frontsector->floorheight && wh < gr_frontsector->ceilingheight) { diff --git a/src/p_mobj.c b/src/p_mobj.c index 81ad1a5ee..29ba5d043 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -3670,7 +3670,7 @@ static void P_Boss3Thinker(mobj_t *mobj) // Move Boss4's sectors by delta. static boolean P_Boss4MoveCage(fixed_t delta) { - const INT16 tag = -2; + const UINT16 tag = 65534; INT32 snum; sector_t *sector; for (snum = sectors[tag%numsectors].firsttag; snum != -1; snum = sector->nexttag) @@ -3719,7 +3719,7 @@ static void P_Boss4PinchSpikeballs(mobj_t *mobj, angle_t angle, fixed_t fz) // Destroy cage FOFs. static void P_Boss4DestroyCage(void) { - const INT16 tag = -2; + const UINT16 tag = 65534; INT32 snum, next; size_t a; sector_t *sector, *rsec; diff --git a/src/r_defs.h b/src/r_defs.h index dd096c3eb..f818ed65c 100644 --- a/src/r_defs.h +++ b/src/r_defs.h @@ -242,7 +242,7 @@ typedef struct sector_s INT32 ceilingpic; INT16 lightlevel; INT16 special; - INT16 tag; + UINT16 tag; INT32 nexttag, firsttag; // for fast tag searches // origin for any sounds played by the sector