From 28e2417ef99d00e017eba328765e93c5428ff9b8 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Thu, 9 Sep 2021 11:40:54 +0300 Subject: [PATCH] Made it compile with GCC on Linux. Fixed GCC warnings and Windows specifics. Restored C++11, switch to 14 was not supposed to happen. Not 100% sure about my Find* module section. --- .gitignore | 4 + CMakeLists.txt | 34 ++++- CMakeModules/FindSDL2.cmake | 173 ++++++++++++++++++++++++ CMakeModules/FindSDL2_mixer.cmake | 100 ++++++++++++++ README.md | 13 +- SpaceCadetPinball/SpaceCadetPinball.cpp | 5 +- SpaceCadetPinball/TBall.cpp | 11 +- SpaceCadetPinball/TDemo.cpp | 16 +-- SpaceCadetPinball/TLightGroup.cpp | 2 +- SpaceCadetPinball/TPinballComponent.cpp | 4 +- SpaceCadetPinball/TPinballTable.cpp | 2 +- SpaceCadetPinball/TPlunger.cpp | 2 +- SpaceCadetPinball/TTableLayer.cpp | 4 +- SpaceCadetPinball/TTextBoxMessage.cpp | 2 +- SpaceCadetPinball/control.cpp | 100 +++++++------- SpaceCadetPinball/gdrv.cpp | 8 +- SpaceCadetPinball/gdrv.h | 4 +- SpaceCadetPinball/high_score.cpp | 34 ++--- SpaceCadetPinball/imgui_sdl.cpp | 11 +- SpaceCadetPinball/loader.cpp | 22 ++- SpaceCadetPinball/midi.cpp | 35 +++-- SpaceCadetPinball/midi.h | 14 +- SpaceCadetPinball/options.cpp | 10 +- SpaceCadetPinball/partman.cpp | 7 +- SpaceCadetPinball/partman.h | 2 +- SpaceCadetPinball/pb.cpp | 8 +- SpaceCadetPinball/pch.h | 29 +++- SpaceCadetPinball/pinball.cpp | 8 +- SpaceCadetPinball/render.cpp | 16 +-- SpaceCadetPinball/render.h | 6 +- SpaceCadetPinball/score.cpp | 12 +- SpaceCadetPinball/winmain.cpp | 11 +- SpaceCadetPinball/zdrv.cpp | 4 +- 33 files changed, 523 insertions(+), 190 deletions(-) create mode 100644 CMakeModules/FindSDL2.cmake create mode 100644 CMakeModules/FindSDL2_mixer.cmake diff --git a/.gitignore b/.gitignore index 0af0471..1753fa8 100644 --- a/.gitignore +++ b/.gitignore @@ -269,3 +269,7 @@ __pycache__/ #CMake generated out/ /cmake-build-debug + +# Windows XP stuff +DebugWinXp/ +ReleaseWinXp/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ed0281..ad8cdb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,29 @@ cmake_minimum_required(VERSION 3.16) project(SpaceCadetPinball) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 11) -set(SDL2_DIR "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2-2.0.16") -set(SDL2_mixer_DIR "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2_mixer-2.0.4") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMakeModules") + +# On Windows, set paths to SDL-devel packages here +if(WIN32) + set(SDL2_PATH "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2") + set(SDL2_MIXER_PATH "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2_mixer") +endif() find_package(SDL2 REQUIRED) FIND_PACKAGE(SDL2_mixer REQUIRED) +include_directories(${SDL2_INCLUDE_DIR} ${SDL2_MIXER_INCLUDE_DIR}) +get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES) +foreach(dir ${dirs}) + message(STATUS "Include dir='${dir}'") +endforeach() + -include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_MIXER_INCLUDE_DIRS}) set(SOURCE_FILES SpaceCadetPinball/control.cpp @@ -156,4 +168,16 @@ set(SOURCE_FILES add_executable(SpaceCadetPinball ${SOURCE_FILES}) -target_link_libraries(SpaceCadetPinball ${SDL2_LIBRARIES} ${SDL2_MIXER_LIBRARIES}) +target_link_libraries(SpaceCadetPinball ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARY}) + +# On Windows, copy DLL to output +if(WIN32) + list(GET SDL2_LIBRARY 1 SDL2_LIB_PATH) + get_filename_component(SDL2_DLL_PATH ${SDL2_LIB_PATH} DIRECTORY) + get_filename_component(SDL2_MIXER_DLL_PATH ${SDL2_MIXER_LIBRARY} DIRECTORY) + message(STATUS "copy paths='${SDL2_DLL_PATH}' '${SDL2_MIXER_DLL_PATH}'") + add_custom_command(TARGET SpaceCadetPinball POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SDL2_DLL_PATH}/SDL2.dll" $ + COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SDL2_MIXER_DLL_PATH}/SDL2_mixer.dll" $ + ) +endif() \ No newline at end of file diff --git a/CMakeModules/FindSDL2.cmake b/CMakeModules/FindSDL2.cmake new file mode 100644 index 0000000..652bf43 --- /dev/null +++ b/CMakeModules/FindSDL2.cmake @@ -0,0 +1,173 @@ + +# This module defines +# SDL2_LIBRARY, the name of the library to link against +# SDL2_FOUND, if false, do not try to link to SDL2 +# SDL2_INCLUDE_DIR, where to find SDL.h +# +# This module responds to the the flag: +# SDL2_BUILDING_LIBRARY +# If this is defined, then no SDL2main will be linked in because +# only applications need main(). +# Otherwise, it is assumed you are building an application and this +# module will attempt to locate and set the the proper link flags +# as part of the returned SDL2_LIBRARY variable. +# +# Don't forget to include SDLmain.h and SDLmain.m your project for the +# OS X framework based version. (Other versions link to -lSDL2main which +# this module will try to find on your behalf.) Also for OS X, this +# module will automatically add the -framework Cocoa on your behalf. +# +# +# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration +# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library +# (SDL2.dll, libsdl2.so, SDL2.framework, etc). +# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again. +# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value +# as appropriate. These values are used to generate the final SDL2_LIBRARY +# variable, but when these values are unset, SDL2_LIBRARY does not get created. +# +# +# $SDL2DIR is an environment variable that would +# correspond to the ./configure --prefix=$SDL2DIR +# used in building SDL2. +# l.e.galup 9-20-02 +# +# Modified by Eric Wing. +# Added code to assist with automated building by using environmental variables +# and providing a more controlled/consistent search behavior. +# Added new modifications to recognize OS X frameworks and +# additional Unix paths (FreeBSD, etc). +# Also corrected the header search path to follow "proper" SDL guidelines. +# Added a search for SDL2main which is needed by some platforms. +# Added a search for threads which is needed by some platforms. +# Added needed compile switches for MinGW. +# +# On OSX, this will prefer the Framework version (if found) over others. +# People will have to manually change the cache values of +# SDL2_LIBRARY to override this selection or set the CMake environment +# CMAKE_INCLUDE_PATH to modify the search paths. +# +# Note that the header path has changed from SDL2/SDL.h to just SDL.h +# This needed to change because "proper" SDL convention +# is #include "SDL.h", not . This is done for portability +# reasons because not all systems place things in SDL2/ (see FreeBSD). + +#============================================================================= +# Copyright 2003-2009 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +# message("") + +SET(SDL2_SEARCH_PATHS + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt + ${SDL2_PATH} +) + +FIND_PATH(SDL2_INCLUDE_DIR SDL.h + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES include/SDL2 include + PATHS ${SDL2_SEARCH_PATHS} +) + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(PATH_SUFFIXES lib64 lib/x64 lib) +else() + set(PATH_SUFFIXES lib/x86 lib) +endif() + +FIND_LIBRARY(SDL2_LIBRARY_TEMP + NAMES SDL2 + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES ${PATH_SUFFIXES} + PATHS ${SDL2_SEARCH_PATHS} +) + +IF(NOT SDL2_BUILDING_LIBRARY) + IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") + # Non-OS X framework versions expect you to also dynamically link to + # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms + # seem to provide SDL2main for compatibility even though they don't + # necessarily need it. + FIND_LIBRARY(SDL2MAIN_LIBRARY + NAMES SDL2main + HINTS + $ENV{SDL2DIR} + PATH_SUFFIXES ${PATH_SUFFIXES} + PATHS ${SDL2_SEARCH_PATHS} + ) + ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") +ENDIF(NOT SDL2_BUILDING_LIBRARY) + +# SDL2 may require threads on your system. +# The Apple build may not need an explicit flag because one of the +# frameworks may already provide it. +# But for non-OSX systems, I will use the CMake Threads package. +IF(NOT APPLE) + FIND_PACKAGE(Threads) +ENDIF(NOT APPLE) + +# MinGW needs an additional link flag, -mwindows +# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows +IF(MINGW) + SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW") +ENDIF(MINGW) + +IF(SDL2_LIBRARY_TEMP) + # For SDL2main + IF(NOT SDL2_BUILDING_LIBRARY) + IF(SDL2MAIN_LIBRARY) + SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP}) + ENDIF(SDL2MAIN_LIBRARY) + ENDIF(NOT SDL2_BUILDING_LIBRARY) + + # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa. + # CMake doesn't display the -framework Cocoa string in the UI even + # though it actually is there if I modify a pre-used variable. + # I think it has something to do with the CACHE STRING. + # So I use a temporary variable until the end so I can set the + # "real" variable in one-shot. + IF(APPLE) + SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa") + ENDIF(APPLE) + + # For threads, as mentioned Apple doesn't need this. + # In fact, there seems to be a problem if I used the Threads package + # and try using this line, so I'm just skipping it entirely for OS X. + IF(NOT APPLE) + SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT}) + ENDIF(NOT APPLE) + + # For MinGW library + IF(MINGW) + SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP}) + ENDIF(MINGW) + + # Set the final string here so the GUI reflects the final state. + SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found") + # Set the temp variable to INTERNAL so it is not seen in the CMake GUI + SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "") +ENDIF(SDL2_LIBRARY_TEMP) + +# message("") + +INCLUDE(FindPackageHandleStandardArgs) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR) \ No newline at end of file diff --git a/CMakeModules/FindSDL2_mixer.cmake b/CMakeModules/FindSDL2_mixer.cmake new file mode 100644 index 0000000..9bf2719 --- /dev/null +++ b/CMakeModules/FindSDL2_mixer.cmake @@ -0,0 +1,100 @@ +# Locate SDL_MIXER library +# +# This module defines: +# +# :: +# +# SDL2_MIXER_LIBRARIES, the name of the library to link against +# SDL2_MIXER_INCLUDE_DIRS, where to find the headers +# SDL2_MIXER_FOUND, if false, do not try to link against +# SDL2_MIXER_VERSION_STRING - human-readable string containing the version of SDL_MIXER +# +# +# +# For backward compatibility the following variables are also set: +# +# :: +# +# SDLMIXER_LIBRARY (same value as SDL2_MIXER_LIBRARIES) +# SDLMIXER_INCLUDE_DIR (same value as SDL2_MIXER_INCLUDE_DIRS) +# SDLMIXER_FOUND (same value as SDL2_MIXER_FOUND) +# +# +# +# $SDLDIR is an environment variable that would correspond to the +# ./configure --prefix=$SDLDIR used in building SDL. +# +# Created by Eric Wing. This was influenced by the FindSDL.cmake +# module, but with modifications to recognize OS X frameworks and +# additional Unix paths (FreeBSD, etc). + +#============================================================================= +# Copyright 2005-2009 Kitware, Inc. +# Copyright 2012 Benjamin Eikel +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +find_path(SDL2_MIXER_INCLUDE_DIR SDL_mixer.h + HINTS + ENV SDL2MIXERDIR + ENV SDL2DIR + PATH_SUFFIXES SDL2 + # path suffixes to search inside ENV{SDLDIR} + include/SDL2 include + PATHS ${SDL2_MIXER_PATH} + ) + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(VC_LIB_PATH_SUFFIX lib/x64) +else() + set(VC_LIB_PATH_SUFFIX lib/x86) +endif() + +find_library(SDL2_MIXER_LIBRARY + NAMES SDL2_mixer + HINTS + ENV SDL2MIXERDIR + ENV SDL2DIR + PATH_SUFFIXES lib bin ${VC_LIB_PATH_SUFFIX} + PATHS ${SDL2_MIXER_PATH} + ) + +if(SDL2_MIXER_INCLUDE_DIR AND EXISTS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h") + file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+[0-9]+$") + file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+[0-9]+$") + file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+[0-9]+$") + string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MAJOR "${SDL2_MIXER_VERSION_MAJOR_LINE}") + string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MINOR "${SDL2_MIXER_VERSION_MINOR_LINE}") + string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_PATCH "${SDL2_MIXER_VERSION_PATCH_LINE}") + set(SDL2_MIXER_VERSION_STRING ${SDL2_MIXER_VERSION_MAJOR}.${SDL2_MIXER_VERSION_MINOR}.${SDL2_MIXER_VERSION_PATCH}) + unset(SDL2_MIXER_VERSION_MAJOR_LINE) + unset(SDL2_MIXER_VERSION_MINOR_LINE) + unset(SDL2_MIXER_VERSION_PATCH_LINE) + unset(SDL2_MIXER_VERSION_MAJOR) + unset(SDL2_MIXER_VERSION_MINOR) + unset(SDL2_MIXER_VERSION_PATCH) +endif() + +set(SDL2_MIXER_LIBRARIES ${SDL2_MIXER_LIBRARY}) +set(SDL2_MIXER_INCLUDE_DIRS ${SDL2_MIXER_INCLUDE_DIR}) + +include(FindPackageHandleStandardArgs) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_mixer + REQUIRED_VARS SDL2_MIXER_LIBRARIES SDL2_MIXER_INCLUDE_DIRS + VERSION_VAR SDL2_MIXER_VERSION_STRING) + +# for backward compatibility +set(SDLMIXER_LIBRARY ${SDL2_MIXER_LIBRARIES}) +set(SDLMIXER_INCLUDE_DIR ${SDL2_MIXER_INCLUDE_DIRS}) +set(SDLMIXER_FOUND ${SDL2_MIXER_FOUND}) + +mark_as_advanced(SDL2_MIXER_LIBRARY SDL2_MIXER_INCLUDE_DIR) \ No newline at end of file diff --git a/README.md b/README.md index dd21e38..2f3bd4a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # SpaceCadetPinball **Summary:** Reverse engineering of `3D Pinball for Windows – Space Cadet`, a game bundled with Windows. -**How to play:** Place compiled exe into a folder containing original game resources (not included).\ +**How to play:** Place compiled executable into a folder containing original game resources (not included).\ Supports data files from Windows and Full Tilt versions of the game. \ \ @@ -20,8 +20,15 @@ Supports data files from Windows and Full Tilt versions of the game. * All subs were decompiled, C pseudo code was converted to compilable C++. Loose (namespace?) subs were assigned to classes. **Compiling:**\ -Project uses `C++11` features and depends on Windows libs.\ -Compile with Visual Studio; tested with 2017 and 2019. +Project uses `C++11` and depends on `SDL2` libs.\ +On Windows:\ +Download and unpack devel packages for `SDL2` and `SDL2_mixer`.\ +Set paths to them in CMakeLists.txt, see suggested placement in /Libs.\ +Compile with Visual Studio; tested with 2019. + +On Linux:\ +Install devel packages for `SDL2` and `SDL2_mixer`.\ +Compile with CMake; tested with GCC 10. **Plans:** * ~~Decompile original game~~ diff --git a/SpaceCadetPinball/SpaceCadetPinball.cpp b/SpaceCadetPinball/SpaceCadetPinball.cpp index fb53717..4bf6368 100644 --- a/SpaceCadetPinball/SpaceCadetPinball.cpp +++ b/SpaceCadetPinball/SpaceCadetPinball.cpp @@ -3,7 +3,6 @@ #include "pch.h" -#include #include "objlist_class.h" #include "partman.h" #include "gdrv.h" @@ -38,7 +37,7 @@ int main(int argc, char* argv[]) auto xx = sizeof(datFileHeader); - strcpy_s(winmain::DatFileName, "PINBALL.DAT"); + strncpy(winmain::DatFileName, "PINBALL.DAT", sizeof winmain::DatFileName); pb::init(); auto datFile = pb::record_table; @@ -71,7 +70,7 @@ int main(int argc, char* argv[]) { auto rsc = pinball::get_rc_string(i, 0); if (rsc) - printf_s("%d:\t%s\n", i, rsc); + printf("%d:\t%s\n", i, rsc); } //DatParser::Parse(dataFileName); diff --git a/SpaceCadetPinball/TBall.cpp b/SpaceCadetPinball/TBall.cpp index 893b254..eeb035c 100644 --- a/SpaceCadetPinball/TBall.cpp +++ b/SpaceCadetPinball/TBall.cpp @@ -58,7 +58,7 @@ TBall::TBall(TPinballTable* table) : TPinballComponent(table, -1, false) } while (index < visualCount); } - RenderSprite = render::create_sprite(VisualType::Ball, nullptr, nullptr, 0, 0, nullptr); + RenderSprite = render::create_sprite(VisualTypes::Ball, nullptr, nullptr, 0, 0, nullptr); PinballTable->CollisionCompOffset = Offset; Position.Z = Offset; } @@ -138,10 +138,9 @@ void TBall::throw_ball(TBall* ball, vector_type* acceleration, float angleMult, { ball->CollisionComp = nullptr; ball->Acceleration = *acceleration; - float rnd = static_cast(rand()); - float angle = (1.0f - (rnd * 0.00003051850947599719f + rnd * 0.00003051850947599719f)) * angleMult; + float rnd = RandFloat(); + float angle = (1.0f - (rnd + rnd)) * angleMult; maths::RotateVector(&ball->Acceleration, angle); - rnd = static_cast(rand()); - ball->Speed = (1.0f - (rnd * 0.00003051850947599719f + rnd * 0.00003051850947599719f)) * (speedMult1 * - speedMult2) + speedMult1; + rnd = RandFloat(); + ball->Speed = (1.0f - (rnd + rnd)) * (speedMult1 * speedMult2) + speedMult1; } diff --git a/SpaceCadetPinball/TDemo.cpp b/SpaceCadetPinball/TDemo.cpp index 0618e12..78da1e8 100644 --- a/SpaceCadetPinball/TDemo.cpp +++ b/SpaceCadetPinball/TDemo.cpp @@ -105,8 +105,7 @@ void TDemo::Collision(TBall* ball, vector_type* nextPosition, vector_type* direc case 1400: if (!FlipLeftTimer && !FlipLeftFlag) { - float time = FlipTimerTime1 + FlipTimerTime2 - static_cast(rand()) * - 0.00003051850947599719f * (FlipTimerTime2 + FlipTimerTime2); + float time = FlipTimerTime1 + FlipTimerTime2 - RandFloat() * (FlipTimerTime2 + FlipTimerTime2); FlipLeftTimer = timer::set(time, this, FlipLeft); } break; @@ -116,8 +115,7 @@ void TDemo::Collision(TBall* ball, vector_type* nextPosition, vector_type* direc case 1402: if (!FlipRightTimer && !FlipRightFlag) { - float time = FlipTimerTime1 + FlipTimerTime2 - static_cast(rand()) * - 0.00003051850947599719f * (FlipTimerTime2 + FlipTimerTime2); + float time = FlipTimerTime1 + FlipTimerTime2 - RandFloat() * (FlipTimerTime2 + FlipTimerTime2); FlipRightTimer = timer::set(time, this, FlipRight); } break; @@ -128,7 +126,7 @@ void TDemo::Collision(TBall* ball, vector_type* nextPosition, vector_type* direc if (!PlungerFlag) { PinballTable->Message(1004, ball->TimeNow); - float time = static_cast(rand()) * 0.00003051850947599719f + 2.0f; + float time = RandFloat() + 2.0f; PlungerFlag = timer::set(time, this, PlungerRelease); } break; @@ -172,8 +170,8 @@ void TDemo::FlipRight(int timerId, void* caller) } demo->PinballTable->Message(1002, pb::time_next); demo->FlipRightFlag = 1; - float time = demo->UnFlipTimerTime1 + demo->UnFlipTimerTime2 - static_cast(rand()) * - 0.00003051850947599719f * (demo->UnFlipTimerTime2 + demo->UnFlipTimerTime2); + float time = demo->UnFlipTimerTime1 + demo->UnFlipTimerTime2 - RandFloat() * + (demo->UnFlipTimerTime2 + demo->UnFlipTimerTime2); timer::set(time, demo, UnFlipRight); } } @@ -190,8 +188,8 @@ void TDemo::FlipLeft(int timerId, void* caller) } demo->PinballTable->Message(1000, pb::time_next); demo->FlipLeftFlag = 1; - float time = demo->UnFlipTimerTime1 + demo->UnFlipTimerTime2 - static_cast(rand()) * - 0.00003051850947599719f * (demo->UnFlipTimerTime2 + demo->UnFlipTimerTime2); + float time = demo->UnFlipTimerTime1 + demo->UnFlipTimerTime2 - RandFloat() * + (demo->UnFlipTimerTime2 + demo->UnFlipTimerTime2); timer::set(time, demo, UnFlipLeft); } } diff --git a/SpaceCadetPinball/TLightGroup.cpp b/SpaceCadetPinball/TLightGroup.cpp index 17798b4..7faec1b 100644 --- a/SpaceCadetPinball/TLightGroup.cpp +++ b/SpaceCadetPinball/TLightGroup.cpp @@ -175,7 +175,7 @@ int TLightGroup::Message(int code, float value) if (rand() % 100 > 70) { auto light = List->Get(i); - auto randVal = static_cast(rand()) * 0.00003051850947599719f * value * 3.0f + 0.1f; + auto randVal = RandFloat() * value * 3.0f + 0.1f; light->Message(9, randVal); } } diff --git a/SpaceCadetPinball/TPinballComponent.cpp b/SpaceCadetPinball/TPinballComponent.cpp index 816f2da..66d309e 100644 --- a/SpaceCadetPinball/TPinballComponent.cpp +++ b/SpaceCadetPinball/TPinballComponent.cpp @@ -56,7 +56,7 @@ TPinballComponent::TPinballComponent(TPinballTable* table, int groupIndex, bool ListZMap = new objlist_class(0, 4); for (int index = 0; index < ListBitmap->GetCount(); index++) { - assertm(ListBitmap->Get(index)->BitmapType == BitmapType::Spliced, "Wrong zMap padding"); + assertm(ListBitmap->Get(index)->BitmapType == BitmapTypes::Spliced, "Wrong zMap padding"); ListZMap->Add(visual.ZMap); } } @@ -78,7 +78,7 @@ TPinballComponent::TPinballComponent(TPinballTable* table, int groupIndex, bool } RenderSprite = render::create_sprite( - visualCount > 0 ? VisualType::Sprite : VisualType::None, + visualCount > 0 ? VisualTypes::Sprite : VisualTypes::None, rootBmp, zMap, rootBmp->XPosition - table->XOffset, diff --git a/SpaceCadetPinball/TPinballTable.cpp b/SpaceCadetPinball/TPinballTable.cpp index 50d392c..3595934 100644 --- a/SpaceCadetPinball/TPinballTable.cpp +++ b/SpaceCadetPinball/TPinballTable.cpp @@ -253,7 +253,7 @@ TPinballComponent* TPinballTable::find_component(int groupIndex) return obj; } } - _itoa_s(groupIndex, Buffer, 10); + snprintf(Buffer, sizeof Buffer, "%d", groupIndex); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Table cant find (lh):", Buffer, nullptr); return nullptr; } diff --git a/SpaceCadetPinball/TPlunger.cpp b/SpaceCadetPinball/TPlunger.cpp index b3b3e7b..65b6f27 100644 --- a/SpaceCadetPinball/TPlunger.cpp +++ b/SpaceCadetPinball/TPlunger.cpp @@ -38,7 +38,7 @@ void TPlunger::Collision(TBall* ball, vector_type* nextPosition, vector_type* di { if (PinballTable->TiltLockFlag) Message(1017, 0.0); - coef = static_cast(rand()) * 0.00003051850947599719f * Boost * 0.1f + Boost; + coef = RandFloat() * Boost * 0.1f + Boost; maths::basic_collision(ball, nextPosition, direction, Elasticity, Smoothness, Threshold, coef); } diff --git a/SpaceCadetPinball/TTableLayer.cpp b/SpaceCadetPinball/TTableLayer.cpp index 9558796..e80a2b2 100644 --- a/SpaceCadetPinball/TTableLayer.cpp +++ b/SpaceCadetPinball/TTableLayer.cpp @@ -33,7 +33,7 @@ TTableLayer::TTableLayer(TPinballTable* table): TCollisionComponent(table, -1, f rect.YPosition = 0; rect.Width = bmp->Width; rect.Height = bmp->Height; - render::create_sprite(VisualType::None, bmp, visual.ZMap, 0, 0, &rect); + render::create_sprite(VisualTypes::None, bmp, visual.ZMap, 0, 0, &rect); PinballTable->SoundIndex1 = visual.SoundIndex4; PinballTable->SoundIndex2 = visual.SoundIndex3; @@ -112,7 +112,7 @@ TTableLayer::~TTableLayer() int TTableLayer::FieldEffect(TBall* ball, vector_type* vecDst) { - vecDst->X = GraityDirX - (0.5f - static_cast(rand()) * 0.00003051850947599719f + ball->Acceleration.X) * + vecDst->X = GraityDirX - (0.5f - RandFloat() + ball->Acceleration.X) * ball->Speed * GraityMult; vecDst->Y = GraityDirY - ball->Acceleration.Y * ball->Speed * GraityMult; return 1; diff --git a/SpaceCadetPinball/TTextBoxMessage.cpp b/SpaceCadetPinball/TTextBoxMessage.cpp index 7f3b9b5..975fe00 100644 --- a/SpaceCadetPinball/TTextBoxMessage.cpp +++ b/SpaceCadetPinball/TTextBoxMessage.cpp @@ -13,7 +13,7 @@ TTextBoxMessage::TTextBoxMessage(char* text, float time) const auto textLen = strlen(text) + 1; Text = memory::allocate(textLen); if (Text) - strcpy_s(Text, textLen, text); + strncpy(Text, text, textLen); } else Text = nullptr; diff --git a/SpaceCadetPinball/control.cpp b/SpaceCadetPinball/control.cpp index 8769326..e866268 100644 --- a/SpaceCadetPinball/control.cpp +++ b/SpaceCadetPinball/control.cpp @@ -954,7 +954,7 @@ int control::AddRankProgress(int rank) { middleCircle->Message(41, 5.0); auto rankText = pinball::get_rc_string(RankRcArray[midActiveCount], 1); - sprintf_s(Buffer, pinball::get_rc_string(83, 0), rankText); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(83, 0), rankText); control_mission_text_box_tag.Component->Display(Buffer, 8.0); control_soundwave10_tag.Component->Play(); } @@ -1078,7 +1078,7 @@ void control::DeploymentChuteToEscapeChuteOneWayControl(int code, TPinballCompon { control_soundwave3_tag.Component->Play(); int score = TableG->AddScore(caller->get_scoring(count - 1)); - sprintf_s(Buffer, pinball::get_rc_string(21, 0), score); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(21, 0), score); control_info_text_box_tag.Component->Display(Buffer, 2.0); if (!light_on(&control_lite56_tag)) { @@ -1141,7 +1141,7 @@ void control::LaunchRampControl(int code, TPinballComponent* caller) { someFlag = 1; int addedScore = SpecialAddScore(TableG->ScoreSpecial1); - sprintf_s(Buffer, pinball::get_rc_string(10, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(10, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); } if (light_on(&control_lite55_tag)) @@ -1381,7 +1381,7 @@ void control::BonusLaneRolloverControl(int code, TPinballComponent* caller) if (light_on(&control_lite16_tag)) { int addedScore = SpecialAddScore(TableG->ScoreSpecial2); - sprintf_s(Buffer, pinball::get_rc_string(3, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(3, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); control_lite16_tag.Component->Message(20, 0.0); control_soundwave50_1_tag.Component->Play(); @@ -1884,7 +1884,7 @@ void control::BlackHoleKickoutControl(int code, TPinballComponent* caller) if (code == 63) { int addedScore = TableG->AddScore(caller->get_scoring(0)); - sprintf_s(Buffer, pinball::get_rc_string(80, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(80, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); caller->Message(55, -1.0); } @@ -1912,7 +1912,7 @@ void control::GravityWellKickoutControl(int code, TPinballComponent* caller) case 63: { auto addedScore = TableG->AddScore(caller->get_scoring(0)); - sprintf_s(Buffer, pinball::get_rc_string(81, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(81, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); control_lite62_tag.Component->Message(20, 0.0); caller->ActiveFlag = 0; @@ -1925,11 +1925,11 @@ void control::GravityWellKickoutControl(int code, TPinballComponent* caller) auto score = reinterpret_cast(caller); if (score) { - sprintf_s(Buffer, pinball::get_rc_string(82, 0), score); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(82, 0), score); } else { - sprintf_s(Buffer, pinball::get_rc_string(45, 0)); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(45, 0)); } control_info_text_box_tag.Component->Display(Buffer, 2.0); control_lite62_tag.Component->Message(4, 0.0); @@ -2199,14 +2199,14 @@ void control::HyperspaceKickOutControl(int code, TPinballComponent* caller) case 0: { auto addedScore = TableG->AddScore(caller->get_scoring(0)); - sprintf_s(Buffer, pinball::get_rc_string(12, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(12, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); break; } case 1: { auto addedScore = SpecialAddScore(TableG->ScoreSpecial3); - sprintf_s(Buffer, pinball::get_rc_string(14, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(14, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); TableG->ScoreSpecial3 = 20000; break; @@ -2215,7 +2215,7 @@ void control::HyperspaceKickOutControl(int code, TPinballComponent* caller) { DrainBallBlockerControl(52, control_block1_tag.Component); auto addedScore = TableG->AddScore(caller->get_scoring(2)); - sprintf_s(Buffer, pinball::get_rc_string(2, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(2, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); break; } @@ -2223,7 +2223,7 @@ void control::HyperspaceKickOutControl(int code, TPinballComponent* caller) { ExtraBallLightControl(19, nullptr); auto addedScore = TableG->AddScore(caller->get_scoring(3)); - sprintf_s(Buffer, pinball::get_rc_string(8, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(8, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); break; } @@ -2243,7 +2243,7 @@ void control::HyperspaceKickOutControl(int code, TPinballComponent* caller) { someFlag = 1; auto addedScore = SpecialAddScore(TableG->ScoreSpecial1); - sprintf_s(Buffer, pinball::get_rc_string(10, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(10, 0), addedScore); control_info_text_box_tag.Component->Display(Buffer, 2.0); } if (light_on(&control_lite26_tag)) @@ -2504,7 +2504,7 @@ void control::BallDrainControl(int code, TPinballComponent* caller) if (!TableG->TiltLockFlag) { int time = SpecialAddScore(TableG->ScoreSpecial2); - sprintf_s(Buffer, pinball::get_rc_string(94, 0), time); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(94, 0), time); control_info_text_box_tag.Component->Display(Buffer, 2.0); } if (TableG->ExtraBalls) @@ -2689,7 +2689,7 @@ void control::AlienMenacePartTwoController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(107, 0), + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(107, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; @@ -2712,7 +2712,7 @@ void control::AlienMenacePartTwoController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(130, 0), 4.0); int addedScore = SpecialAddScore(750000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(7)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -2744,7 +2744,7 @@ void control::BlackHoleThreatController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(124, 0), 4.0); int addedScore = SpecialAddScore(1000000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(8)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -2826,7 +2826,7 @@ void control::BugHuntController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(125, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(125, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -2869,7 +2869,7 @@ void control::BugHuntController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(126, 0), 4.0); int addedScore = SpecialAddScore(750000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(7)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -2897,7 +2897,7 @@ void control::CosmicPlagueController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(139, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(139, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -2944,7 +2944,7 @@ void control::CosmicPlaguePartTwoController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(141, 0), 4.0); int addedScore = SpecialAddScore(1750000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(11)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -2969,7 +2969,7 @@ void control::DoomsdayMachineController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(137, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(137, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -2988,7 +2988,7 @@ void control::DoomsdayMachineController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(138, 0), 4.0); int addedScore = SpecialAddScore(1250000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(9)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3042,7 +3042,7 @@ void control::GameoverController(int code, TPinballComponent* caller) } if (playerNScoreText != nullptr) { - sprintf_s(Buffer, playerNScoreText, playerScore); + snprintf(Buffer, sizeof Buffer, playerNScoreText, playerScore); control_mission_text_box_tag.Component->Display(Buffer, 3.0); int msgField = nextPlayerId == TableG->PlayerCount ? 0x200 : nextPlayerId | 0x100; control_mission_text_box_tag.Component->MessageField = msgField; @@ -3082,7 +3082,7 @@ void control::GameoverController(int code, TPinballComponent* caller) } if (highScoreNText != nullptr) { - sprintf_s(Buffer, highScoreNText, highScore); + snprintf(Buffer, sizeof Buffer, highScoreNText, highScore); control_mission_text_box_tag.Component->Display(Buffer, 3.0); int msgField = nextHidhscoreId == 5 ? 0 : nextHidhscoreId | 0x200; control_mission_text_box_tag.Component->MessageField = msgField; @@ -3110,7 +3110,7 @@ void control::LaunchTrainingController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(110, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(110, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3128,7 +3128,7 @@ void control::LaunchTrainingController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(111, 0), 4.0); int addedScore = SpecialAddScore(500000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(6)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3155,7 +3155,7 @@ void control::MaelstromController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(148, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(148, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3210,7 +3210,7 @@ void control::MaelstromPartEightController(int code, TPinballComponent* caller) control_lite198_tag.Component->MessageField = 1; MissionControl(66, nullptr); int addedScore = SpecialAddScore(5000000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); control_info_text_box_tag.Component->Display(pinball::get_rc_string(48, 0), 4.0); if (!AddRankProgress(18)) { @@ -3343,7 +3343,7 @@ void control::MaelstromPartThreeController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(150, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(150, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3397,7 +3397,7 @@ void control::MaelstromPartTwoController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(149, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(149, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3449,7 +3449,7 @@ void control::PracticeMissionController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(107, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(107, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3472,7 +3472,7 @@ void control::PracticeMissionController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(108, 0), 4.0); int addedScore = SpecialAddScore(500000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(6)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3502,7 +3502,7 @@ void control::ReconnaissanceController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(134, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(134, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3535,7 +3535,7 @@ void control::ReconnaissanceController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(136, 0), 4.0); int addedScore = SpecialAddScore(1250000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(9)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3566,7 +3566,7 @@ void control::ReentryTrainingController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(112, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(112, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3586,7 +3586,7 @@ void control::ReentryTrainingController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(113, 0), 4.0); int addedScore = SpecialAddScore(500000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(6)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3627,7 +3627,7 @@ void control::RescueMissionController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(129, 0), 4.0); int addedScore = SpecialAddScore(750000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(7)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3682,7 +3682,7 @@ void control::SatelliteController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(132, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(132, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3700,7 +3700,7 @@ void control::SatelliteController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(133, 0), 4.0); int addedScore = SpecialAddScore(1250000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(9)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3745,7 +3745,7 @@ void control::ScienceMissionController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(114, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(114, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -3773,7 +3773,7 @@ void control::ScienceMissionController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(115, 0), 4.0); int addedScore = SpecialAddScore(750000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(9)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3810,7 +3810,7 @@ void control::SecretMissionGreenController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(145, 0), 4.0); int addedScore = SpecialAddScore(1500000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(10)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -3913,7 +3913,7 @@ void control::SelectMissionController(int code, TPinballComponent* caller) MissionControl(66, nullptr); int addedScore = SpecialAddScore( mission_select_scores[control_lite56_tag.Component->MessageField - 2]); - sprintf_s(Buffer, pinball::get_rc_string(77, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(77, 0), addedScore); control_mission_text_box_tag.Component->Display(Buffer, 4.0); } return; @@ -4051,7 +4051,7 @@ void control::SelectMissionController(int code, TPinballComponent* caller) { auto missionText = pinball:: get_rc_string(MissionRcArray[control_lite56_tag.Component->MessageField - 2], 1); - sprintf_s(Buffer, pinball::get_rc_string(106, 0), missionText); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(106, 0), missionText); control_mission_text_box_tag.Component->Display(Buffer, -1.0); if (light_on(&control_lite318_tag)) control_lite318_tag.Component->Message(20, 0.0); @@ -4117,7 +4117,7 @@ void control::SpaceRadiationController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(121, 0), 4.0); int addedScore = SpecialAddScore(1000000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(8)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -4170,7 +4170,7 @@ void control::StrayCometController(int code, TPinballComponent* caller) MissionControl(66, nullptr); control_mission_text_box_tag.Component->Display(pinball::get_rc_string(119, 0), 4.0); int addedScore = SpecialAddScore(1000000); - sprintf_s(Buffer, pinball::get_rc_string(78, 0), addedScore); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(78, 0), addedScore); if (!AddRankProgress(8)) { control_mission_text_box_tag.Component->Display(Buffer, 8.0); @@ -4214,7 +4214,7 @@ void control::TimeWarpController(int code, TPinballComponent* caller) { return; } - sprintf_s(Buffer, pinball::get_rc_string(146, 0), control_lite56_tag.Component->MessageField); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(146, 0), control_lite56_tag.Component->MessageField); control_mission_text_box_tag.Component->Display(Buffer, -1.0); return; } @@ -4265,7 +4265,7 @@ void control::TimeWarpPartTwoController(int code, TPinballComponent* caller) { control_middle_circle_tag.Component->Message(33, 5.0); int rank = control_middle_circle_tag.Component->Message(37, 0.0); - sprintf_s(Buffer, pinball::get_rc_string(174, 0), pinball::get_rc_string(RankRcArray[rank - 1], 1)); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(174, 0), pinball::get_rc_string(RankRcArray[rank - 1], 1)); control_mission_text_box_tag.Component->Display(Buffer, 8.0); } } @@ -4278,7 +4278,7 @@ void control::TimeWarpPartTwoController(int code, TPinballComponent* caller) { int rank = control_middle_circle_tag.Component->Message(37, 0.0); control_middle_circle_tag.Component->Message(41, 5.0); - sprintf_s(Buffer, pinball::get_rc_string(173, 0), pinball::get_rc_string(RankRcArray[rank], 1)); + snprintf(Buffer, sizeof Buffer, pinball::get_rc_string(173, 0), pinball::get_rc_string(RankRcArray[rank], 1)); } if (!AddRankProgress(12)) { diff --git a/SpaceCadetPinball/gdrv.cpp b/SpaceCadetPinball/gdrv.cpp index 43e5a3b..587cda3 100644 --- a/SpaceCadetPinball/gdrv.cpp +++ b/SpaceCadetPinball/gdrv.cpp @@ -48,7 +48,7 @@ int gdrv::create_bitmap(gdrv_bitmap8* bmp, int width, int height) bmp->Stride = 4 - width % 4 + width; bmp->Height = height; - bmp->BitmapType = BitmapType::DibBitmap; + bmp->BitmapType = BitmapTypes::DibBitmap; bmp->BmpBufPtr1 = memory::allocate(bmp->Height * bmp->Stride); return 0; } @@ -61,7 +61,7 @@ int gdrv::create_raw_bitmap(gdrv_bitmap8* bmp, int width, int height, int flag) bmp->Stride = width - width % 4 + 4; unsigned int sizeInBytes = height * bmp->Stride; bmp->Height = height; - bmp->BitmapType = BitmapType::RawBitmap; + bmp->BitmapType = BitmapTypes::RawBitmap; char* buf = memory::allocate(sizeInBytes); bmp->BmpBufPtr1 = buf; if (!buf) @@ -73,7 +73,7 @@ int gdrv::create_spliced_bitmap(gdrv_bitmap8* bmp, int width, int height, int si { bmp->Width = width; bmp->Stride = width; - bmp->BitmapType = BitmapType::Spliced; + bmp->BitmapType = BitmapTypes::Spliced; bmp->Height = height; char* buf = memory::allocate(size); bmp->BmpBufPtr1 = buf; @@ -133,7 +133,7 @@ int gdrv::destroy_bitmap(gdrv_bitmap8* bmp) if (!bmp) return -1; - if (bmp->BitmapType != BitmapType::None) + if (bmp->BitmapType != BitmapTypes::None) { memory::free(bmp->BmpBufPtr1); } diff --git a/SpaceCadetPinball/gdrv.h b/SpaceCadetPinball/gdrv.h index bdd2506..851eb0d 100644 --- a/SpaceCadetPinball/gdrv.h +++ b/SpaceCadetPinball/gdrv.h @@ -1,6 +1,6 @@ #pragma once -enum class BitmapType : char +enum class BitmapTypes : char { None = 0, RawBitmap = 1, @@ -14,7 +14,7 @@ struct gdrv_bitmap8 int Width; int Height; int Stride; - BitmapType BitmapType; + BitmapTypes BitmapType; int XPosition; int YPosition; }; diff --git a/SpaceCadetPinball/high_score.cpp b/SpaceCadetPinball/high_score.cpp index 2d30bf5..bdeeb30 100644 --- a/SpaceCadetPinball/high_score.cpp +++ b/SpaceCadetPinball/high_score.cpp @@ -53,13 +53,13 @@ int high_score::read(high_score_struct* table, int* ptrToSmth) for (auto position = 0; position < 5; ++position) { auto tablePtr = &table[position]; - _itoa_s(position, Buffer, 10); - strcat_s(Buffer, ".Name"); + snprintf(Buffer, sizeof Buffer, "%d", position); + strcat(Buffer, ".Name"); options::get_string(optPath, Buffer, buf1, "", 32); buf1[32] = 0; - strcpy_s(tablePtr->Name, buf1); - _itoa_s(position, Buffer, 10); - strcat_s(Buffer, ".Score"); + strncpy(tablePtr->Name, buf1, sizeof tablePtr->Name); + snprintf(Buffer, sizeof Buffer, "%d", position); + strcat(Buffer, ".Score"); options::get_string(optPath, Buffer, buf1, "", 300); tablePtr->Score = atol(buf1); for (int i = (int)strlen(tablePtr->Name); --i >= 0; scoreSum += tablePtr->Name[i]) @@ -89,12 +89,12 @@ int high_score::write(high_score_struct* table, int* ptrToSmth) const char* optPath = pinball::get_rc_string(166, 0); for (auto position = 0; position < 5; ++position) { - _itoa_s(position, Buffer, 10); - strcat_s(Buffer, ".Name"); + snprintf(Buffer, sizeof Buffer, "%d", position); + strcat(Buffer, ".Name"); options::set_string(optPath, Buffer, tablePtr->Name); - _itoa_s(position, Buffer, 10); - strcat_s(Buffer, ".Score"); - _ltoa_s(tablePtr->Score, buf, 300, 10); + snprintf(Buffer, sizeof Buffer, "%d", position); + strcat(Buffer, ".Score"); + snprintf(buf, 300, "%d", tablePtr->Score); options::set_string(optPath, Buffer, buf); for (int i = (int)strlen(tablePtr->Name); --i >= 0; scoreSum += tablePtr->Name[i]) { @@ -152,7 +152,7 @@ int high_score::place_new_score_into(high_score_struct* table, int score, LPSTR posTable->Score = score; if (strlen(scoreStr) >= 31) scoreStr[31] = 0; - strcpy_s(posTable->Name, scoreStr); + strncpy(posTable->Name, scoreStr, sizeof posTable->Name); posTable->Name[31] = 0; } return position; @@ -160,7 +160,7 @@ int high_score::place_new_score_into(high_score_struct* table, int score, LPSTR void high_score::scramble_number_string(int Value, char* Buffer) { - _ltoa_s(Value, Buffer, 300, 10); + snprintf(Buffer, 300, "%d", Value); } void high_score::show_high_score_dialog(high_score_struct* table) @@ -177,7 +177,7 @@ void high_score::show_and_set_high_score_dialog(high_score_struct* table, int sc dlg_score = score; dlg_hst = table; dlg_enter_name = 1; - strncpy_s(default_name, defaultName, 32); + strncpy(default_name, defaultName, sizeof default_name); ShowDialog = true; } @@ -210,11 +210,11 @@ void high_score::RenderHighScoreDialog() { ImGui::TableNextRow(); ImGui::TableNextColumn(); - _itoa_s(row, buf, 10); + snprintf(buf, sizeof buf, "%d", row); ImGui::TextUnformatted(buf); auto score = tablePtr->Score; - ImGui::TableNextColumn(); + ImGui::TableNextColumn(); if (dlg_enter_name == 1 && dlg_position == row) { score = dlg_score; @@ -239,9 +239,9 @@ void high_score::RenderHighScoreDialog() if (ImGui::Button("Ok")) { if (dlg_enter_name) - { + { default_name[31] = 0; - place_new_score_into(dlg_hst, dlg_score, default_name, dlg_position); + place_new_score_into(dlg_hst, dlg_score, default_name, dlg_position); } ImGui::CloseCurrentPopup(); } diff --git a/SpaceCadetPinball/imgui_sdl.cpp b/SpaceCadetPinball/imgui_sdl.cpp index 8769830..afb3055 100644 --- a/SpaceCadetPinball/imgui_sdl.cpp +++ b/SpaceCadetPinball/imgui_sdl.cpp @@ -17,6 +17,13 @@ namespace { + // make_unique is C++14 + template + std::unique_ptr make_unique(Args&&... args) + { + return std::unique_ptr(new T(std::forward(args)...)); + } + struct Device* CurrentDevice = nullptr; namespace TupleHash @@ -417,7 +424,7 @@ namespace const InterpolatedFactorEquation shadeColor(Color(v1.col), Color(v2.col), Color(v3.col), v1.pos, v2.pos, v3.pos); - auto cached = std::make_unique(); + auto cached = make_unique(); DrawTriangleWithColorFunction(renderInfo, [&](float x, float y) { const float u = textureU.Evaluate(x, y); const float v = textureV.Evaluate(x, y); @@ -454,7 +461,7 @@ namespace return; } - auto cached = std::make_unique(); + auto cached = make_unique(); DrawTriangleWithColorFunction(renderInfo, [&color](float, float) { return color; }, cached.get()); if (!cached->Texture) return; diff --git a/SpaceCadetPinball/loader.cpp b/SpaceCadetPinball/loader.cpp index cddebae..8c8765a 100644 --- a/SpaceCadetPinball/loader.cpp +++ b/SpaceCadetPinball/loader.cpp @@ -145,14 +145,22 @@ int loader::get_sound_id(int groupIndex) datFieldTypes::ShortValue)); if (value && *value == 202) { - /*FT sounds are in SOUND subfolder*/ - char filePath[300]{}, fileName2[100]{}; - auto fileName = partman::field(loader_table, soundGroupId, datFieldTypes::String); - sprintf_s(fileName2, pb::FullTiltMode ? "SOUND\\%s" : "%s", fileName); - pinball::make_path_name(filePath, fileName2); + char filePath[300]{}; + std::string fileName = partman::field(loader_table, soundGroupId, datFieldTypes::String); - FILE* file; - if (!fopen_s(&file, filePath, "rb")) + // File name is in lower case, while game data is in upper case. + std::transform(fileName.begin(), fileName.end(), fileName.begin(), [](unsigned char c) { return std::toupper(c); }); + if (pb::FullTiltMode) + { + // FT sounds are in SOUND subfolder + fileName.insert(0, 1, PathSeparator); + fileName.insert(0, "SOUND"); + } + + pinball::make_path_name(filePath, fileName.c_str()); + + FILE* file = fopen(filePath, "rb"); + if (file) { fread(&wavHeader, 1, sizeof wavHeader, file); fclose(file); diff --git a/SpaceCadetPinball/midi.cpp b/SpaceCadetPinball/midi.cpp index 76cbaf6..a8429b4 100644 --- a/SpaceCadetPinball/midi.cpp +++ b/SpaceCadetPinball/midi.cpp @@ -105,15 +105,21 @@ void midi::music_shutdown_ft() delete TrackList; } -Mix_Music* midi::load_track(LPCSTR fileName) +Mix_Music* midi::load_track(std::string fileName) { char filePath[256]; - char fileName2[256]; - strcpy_s(fileName2, "sound\\"); - strcat_s(fileName2, fileName); - pinball::make_path_name(filePath, fileName2, 254u); - strcat_s(filePath, ".MDS"); + // File name is in lower case, while game data is in upper case. + std::transform(fileName.begin(), fileName.end(), fileName.begin(), [](unsigned char c) { return std::toupper(c); }); + if (pb::FullTiltMode) + { + // FT sounds are in SOUND subfolder + fileName.insert(0, 1, PathSeparator); + fileName.insert(0, "SOUND"); + } + fileName += ".MDS"; + + pinball::make_path_name(filePath, fileName.c_str(), 254u); auto midi = MdsToMidi(filePath); if (!midi) return nullptr; @@ -125,11 +131,10 @@ Mix_Music* midi::load_track(LPCSTR fileName) if (!audio) return nullptr; - // Dump converted MIDI file - /*FILE* fileHandle; - strcpy_s(fileName2, fileName); - strcat_s(fileName2, ".midi"); - fopen_s(&fileHandle, fileName2, "wb"); + // Dump converted MIDI file + /*strncpy(fileName2, fileName, sizeof fileName2); + strcat(fileName2, ".midi"); + FILE* fileHandle = fopen(fileName2, "wb"); fwrite(midi->data(), 1, midi->size(), fileHandle); fclose(fileHandle);*/ @@ -178,8 +183,8 @@ int midi::stop_ft() /// Vector that contains MIDI file std::vector* midi::MdsToMidi(char* file) { - FILE* fileHandle; - if (fopen_s(&fileHandle, file, "rb")) + FILE* fileHandle = fopen(file, "rb"); + if (!fileHandle) return nullptr; fseek(fileHandle, 0, SEEK_END); @@ -236,7 +241,7 @@ std::vector* midi::MdsToMidi(char* file) } auto srcPtr = dataChunk->Blocks; - std::vector midiEvents{}; + std::vector midiEvents{}; for (auto blockIndex = dataChunk->BlocksPerChunk; blockIndex; blockIndex--) { auto eventSizeInt = streamIdUsed ? 3 : 2; @@ -256,7 +261,7 @@ std::vector* midi::MdsToMidi(char* file) } // MIDS events can be out of order in the file - std::sort(midiEvents.begin(), midiEvents.end(), [](const auto& lhs, const auto& rhs) + std::sort(midiEvents.begin(), midiEvents.end(), [](const midi_event& lhs, const midi_event& rhs) { return lhs.iTicks < rhs.iTicks; }); diff --git a/SpaceCadetPinball/midi.h b/SpaceCadetPinball/midi.h index bec2d2e..cbc9099 100644 --- a/SpaceCadetPinball/midi.h +++ b/SpaceCadetPinball/midi.h @@ -44,19 +44,12 @@ struct riff_header riff_data Data; }; -struct midi_event_x2 +struct midi_event { DWORD iTicks; DWORD iEvent; }; -struct midi_event_x3 -{ - DWORD iTicks; - DWORD iStreamID; - DWORD iEvent; -}; - struct midi_header { explicit midi_header(uint16_t tickdiv) @@ -85,8 +78,7 @@ struct midi_track static_assert(sizeof(riff_block) == 0xC, "Wrong size of riff_block"); static_assert(sizeof(riff_data) == 0x18, "Wrong size of riff_data"); static_assert(sizeof(riff_header) == 0x38, "Wrong size of riff_header"); -static_assert(sizeof(midi_event_x3) == 12, "Wrong size of midi_event3"); -static_assert(sizeof(midi_event_x2) == 8, "Wrong size of midi_event2"); +static_assert(sizeof(midi_event) == 8, "Wrong size of midi_event2"); static_assert(sizeof(midi_header) == 14, "Wrong size of midi_header"); static_assert(sizeof(midi_track) == 8, "Wrong size of midi_track"); @@ -107,7 +99,7 @@ private: static int some_flag1; static int music_init_ft(); static void music_shutdown_ft(); - static Mix_Music* load_track(LPCSTR fileName); + static Mix_Music* load_track(std::string fileName); static int play_ft(Mix_Music* midi); static int stop_ft(); static std::vector* MdsToMidi(char* file); diff --git a/SpaceCadetPinball/options.cpp b/SpaceCadetPinball/options.cpp index 699c910..c60e278 100644 --- a/SpaceCadetPinball/options.cpp +++ b/SpaceCadetPinball/options.cpp @@ -132,7 +132,7 @@ void options::path_init(LPCSTR regPath) char* buf = memory::allocate(strlen(regPath) + 1); OptionsRegPath = buf; if (buf) - strcpy_s(buf, strlen(regPath) + 1, regPath); + strncpy(buf, regPath, strlen(regPath) + 1); } void options::path_uninit() @@ -152,11 +152,11 @@ LPCSTR options::path(LPCSTR regPath) if (!buf) return OptionsRegPath; } - strcpy_s(buf, 2000, OptionsRegPath); + strncpy(buf, OptionsRegPath, 2000); if (!regPath) return OptionsRegPathCur; - strcat_s(OptionsRegPathCur, 2000, "\\"); - strcat_s(OptionsRegPathCur, 2000, regPath); + strcat(OptionsRegPathCur, "\\"); + strcat(OptionsRegPathCur, regPath); return OptionsRegPathCur; } @@ -190,7 +190,7 @@ void options::set_int(LPCSTR optPath, LPCSTR lpValueName, int data) void options::get_string(LPCSTR optPath, LPCSTR lpValueName, LPSTR dst, LPCSTR defaultValue, int iMaxLength) { - strncpy_s(dst, iMaxLength, defaultValue, iMaxLength); + strncpy(dst, defaultValue, iMaxLength); if (!OptionsRegPath) return; diff --git a/SpaceCadetPinball/partman.cpp b/SpaceCadetPinball/partman.cpp index da9db0b..09a1025 100644 --- a/SpaceCadetPinball/partman.cpp +++ b/SpaceCadetPinball/partman.cpp @@ -15,11 +15,10 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful dat8BitBmpHeader bmpHeader{}; dat16BitBmpHeader zMapHeader{}; - FILE* fileHandle; - fopen_s(&fileHandle, lpFileName, "rb"); + FILE* fileHandle = fopen(lpFileName, "rb"); if (fileHandle == nullptr) return nullptr; - fread(&header, 1, sizeof datFileHeader, fileHandle); + fread(&header, 1, sizeof header, fileHandle); if (strcmp("PARTOUT(4.0)RESOURCE", header.FileSignature) != 0) { fclose(fileHandle); @@ -46,7 +45,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful memory::free(datFile); return nullptr; } - strcpy_s(descriptionBuf, lenOfStr + 1, header.Description); + strncpy(descriptionBuf, header.Description, lenOfStr + 1); } if (header.Unknown) diff --git a/SpaceCadetPinball/partman.h b/SpaceCadetPinball/partman.h index d8856d8..0d9a7cb 100644 --- a/SpaceCadetPinball/partman.h +++ b/SpaceCadetPinball/partman.h @@ -92,7 +92,7 @@ static_assert(sizeof(dat8BitBmpHeader) == 14, "Wrong size of dat8BitBmpHeader"); #pragma pack(push, 1) -struct __declspec(align(1)) dat16BitBmpHeader +struct dat16BitBmpHeader { int16_t Width; int16_t Height; diff --git a/SpaceCadetPinball/pb.cpp b/SpaceCadetPinball/pb.cpp index ccc1b66..e358b34 100644 --- a/SpaceCadetPinball/pb.cpp +++ b/SpaceCadetPinball/pb.cpp @@ -40,7 +40,7 @@ int pb::init() char dataFilePath[300]; ++memory::critical_allocation; - strcpy_s(datFileName, winmain::DatFileName); + strncpy(datFileName, winmain::DatFileName, sizeof datFileName); pinball::make_path_name(dataFilePath, datFileName, 300); record_table = partman::load_records(dataFilePath, fullscrn::GetResolution(), FullTiltMode); @@ -479,12 +479,12 @@ void pb::keydown(int key) break; case 'h': char String1[200]; - strcpy_s(String1, pinball::get_rc_string(26, 0)); + strncpy(String1, pinball::get_rc_string(26, 0), sizeof String1); high_score::show_and_set_high_score_dialog(highscore_table, 1000000000, 1, String1); break; case 'm': char buffer[20]; - sprintf_s(buffer, "%zu", memory::use_total); + snprintf(buffer, sizeof buffer, "%zu", memory::use_total); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Mem:", buffer, winmain::MainWindow); break; case 'r': @@ -569,7 +569,7 @@ void pb::end_game() int position = high_score::get_score_position(highscore_table, scores[i]); if (position >= 0) { - strcpy_s(String1, pinball::get_rc_string(scoreIndex[i] + 26, 0)); + strncpy(String1, pinball::get_rc_string(scoreIndex[i] + 26, 0), sizeof String1); high_score::show_and_set_high_score_dialog(highscore_table, scores[i], position, String1); } } diff --git a/SpaceCadetPinball/pch.h b/SpaceCadetPinball/pch.h index f28fffd..9936666 100644 --- a/SpaceCadetPinball/pch.h +++ b/SpaceCadetPinball/pch.h @@ -9,6 +9,10 @@ #ifndef PCH_H #define PCH_H + +// GCC does not have *_s functions +#define _CRT_SECURE_NO_WARNINGS + // TODO: add headers that you want to pre-compile here #include #include @@ -16,11 +20,13 @@ #include #include /*For control template*/ #include -//#include +#include //#include //#include #include #include +#include +#include #define SDL_MAIN_HANDLED #include "SDL.h" @@ -32,19 +38,27 @@ //https://github.com/Tyyppi77/imgui_sdl 01deb04b102b6a1c15c7fdec1977a2c96a885e6f #include "imgui_sdl.h" -typedef unsigned long DWORD; +typedef uint32_t DWORD; typedef char* LPSTR; typedef const char* LPCSTR; #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) +// +//#define min(a,b) (((a)<(b))?(a):(b)) +//#define max(a,b) (((a)>(b))?(a):(b)) + +constexpr char PathSeparator = +#ifdef _WIN32 +'\\'; +#else +'/'; +#endif + /*Use (void) to silent unused warnings.*/ #define assertm(exp, msg) assert(((void)msg, exp)) -/*Sound uses PlaySound*/ -#undef PlaySound - inline size_t pgm_save(int width, int height, char* data, FILE* outfile) { @@ -54,4 +68,9 @@ inline size_t pgm_save(int width, int height, char* data, FILE* outfile) return n; } +inline float RandFloat() +{ + return static_cast(std::rand()) / (RAND_MAX); +} + #endif //PCH_H diff --git a/SpaceCadetPinball/pinball.cpp b/SpaceCadetPinball/pinball.cpp index 8dd7299..f99fed7 100644 --- a/SpaceCadetPinball/pinball.cpp +++ b/SpaceCadetPinball/pinball.cpp @@ -214,7 +214,7 @@ int LoadStringAlt(uint32_t uID, LPSTR lpBuffer, int cchBufferMax) return 0; } - strncpy_s(lpBuffer, cchBufferMax, str->second, cchBufferMax); + strncpy(lpBuffer, str->second, cchBufferMax); return 1; } @@ -251,10 +251,10 @@ int pinball::make_path_name(LPSTR lpFilename, LPCSTR lpString2, int nSize) auto base_path = SDL_GetBasePath(); if (!base_path) { - strcat_s(lpFilename, nSize, "?"); + strcat(lpFilename,"?"); return 1; } - strcpy_s(lpFilename, nSize, base_path); - strcat_s(lpFilename, nSize, lpString2); + strncpy(lpFilename, base_path, nSize); + strcat(lpFilename, lpString2); return 0; } diff --git a/SpaceCadetPinball/render.cpp b/SpaceCadetPinball/render.cpp index 894a67f..72acc52 100644 --- a/SpaceCadetPinball/render.cpp +++ b/SpaceCadetPinball/render.cpp @@ -68,9 +68,9 @@ void render::update() for (int index = 0; index < many_dirty; ++dirtyPtr, ++index) { auto curSprite = *dirtyPtr; - if ((*dirtyPtr)->VisualType != VisualType::None) + if ((*dirtyPtr)->VisualType != VisualTypes::None) { - if ((*dirtyPtr)->VisualType == VisualType::Sprite) + if ((*dirtyPtr)->VisualType == VisualTypes::Sprite) { if (curSprite->BmpRectCopy.Width > 0) maths::enclosing_box(&curSprite->BmpRectCopy, &curSprite->BmpRect, &curSprite->DirtyRect); @@ -118,8 +118,8 @@ void render::update() for (int index = 0; index < many_dirty; ++index) { auto sprite = *dirtyPtr; - if ((*dirtyPtr)->DirtyRect.Width > 0 && (sprite->VisualType == VisualType::None || sprite->VisualType == - VisualType::Sprite)) + if ((*dirtyPtr)->DirtyRect.Width > 0 && (sprite->VisualType == VisualTypes::None || sprite->VisualType == + VisualTypes::Sprite)) repaint(*dirtyPtr); ++dirtyPtr; } @@ -214,11 +214,11 @@ void render::paint() void render::sprite_modified(render_sprite_type_struct* sprite) { - if (sprite->VisualType != VisualType::Ball && many_dirty < 999) + if (sprite->VisualType != VisualTypes::Ball && many_dirty < 999) dirty_list[many_dirty++] = sprite; } -render_sprite_type_struct* render::create_sprite(VisualType visualType, gdrv_bitmap8* bmp, zmap_header_type* zMap, +render_sprite_type_struct* render::create_sprite(VisualTypes visualType, gdrv_bitmap8* bmp, zmap_header_type* zMap, int xPosition, int yPosition, rectangle_type* rect) { auto sprite = memory::allocate(); @@ -256,14 +256,14 @@ render_sprite_type_struct* render::create_sprite(VisualType visualType, gdrv_bit sprite->ZMap = zMap; sprite->ZMapOffestX = 0; sprite->ZMapOffestY = 0; - if (!zMap && visualType != VisualType::Ball) + if (!zMap && visualType != VisualTypes::Ball) { sprite->ZMap = background_zmap; sprite->ZMapOffestY = xPosition - zmap_offset; sprite->ZMapOffestX = yPosition - zmap_offsetY; } sprite->BmpRectCopy = sprite->BmpRect; - if (visualType == VisualType::Ball) + if (visualType == VisualTypes::Ball) { ball_list[many_balls++] = sprite; } diff --git a/SpaceCadetPinball/render.h b/SpaceCadetPinball/render.h index 88173bd..050983e 100644 --- a/SpaceCadetPinball/render.h +++ b/SpaceCadetPinball/render.h @@ -3,7 +3,7 @@ #include "maths.h" #include "zdrv.h" -enum class VisualType : char +enum class VisualTypes : char { None = 0, Sprite = 1, @@ -16,7 +16,7 @@ struct render_sprite_type_struct gdrv_bitmap8* Bmp; zmap_header_type* ZMap; char UnknownFlag; - VisualType VisualType; + VisualTypes VisualType; int16_t Depth; rectangle_type BmpRectCopy; int ZMapOffestY; @@ -46,7 +46,7 @@ public: static void update(); static void paint(); static void sprite_modified(render_sprite_type_struct* sprite); - static render_sprite_type_struct* create_sprite(VisualType visualType, gdrv_bitmap8* bmp, + static render_sprite_type_struct* create_sprite(VisualTypes visualType, gdrv_bitmap8* bmp, zmap_header_type* zMap, int xPosition, int yPosition, rectangle_type* rect); static void remove_sprite(render_sprite_type_struct* sprite); diff --git a/SpaceCadetPinball/score.cpp b/SpaceCadetPinball/score.cpp index e7f7201..6d6942b 100644 --- a/SpaceCadetPinball/score.cpp +++ b/SpaceCadetPinball/score.cpp @@ -245,7 +245,7 @@ void score::update(scoreStruct* score) erase(score, 0); if (score->Score >= 0) { - _ltoa_s(score->Score, scoreBuf, 10); + snprintf(scoreBuf, sizeof scoreBuf, "%d", score->Score); for (ptrdiff_t index = strlen(scoreBuf) - 1; index >= 0; index--) { unsigned char curChar = scoreBuf[index]; @@ -281,26 +281,26 @@ void score::string_format(int score, char* str) } else { - strcpy_s(separator, ","); + strncpy(separator, ",", sizeof separator); int scoreMillions = score % 1000000000 / 1000000; if (score / 1000000000 <= 0) { if (static_cast(scoreMillions) <= 0) { if (score % 1000000 / 1000 <= 0) - sprintf_s(str, 36, "%ld", score); + snprintf(str, 36, "%ld", score); else - sprintf_s(str, 36, "%ld%s%03ld", score % 1000000 / 1000, separator, score % 1000); + snprintf(str, 36, "%ld%s%03ld", score % 1000000 / 1000, separator, score % 1000); } else { - sprintf_s(str, 36, "%ld%s%03ld%s%03ld", scoreMillions, separator, score % 1000000 / 1000, separator, + snprintf(str, 36, "%ld%s%03ld%s%03ld", scoreMillions, separator, score % 1000000 / 1000, separator, score % 1000); } } else { - sprintf_s( + snprintf( str, 36, "%ld%s%03ld%s%03ld%s%03ld", diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index e187b71..027cc20 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -72,12 +72,11 @@ int winmain::WinMain(LPCSTR lpCmdLine) /*Check for full tilt .dat file and switch to it automatically*/ char cadetFilePath[300]{}; pinball::make_path_name(cadetFilePath, "CADET.DAT", 300); - FILE* cadetDat; - fopen_s(&cadetDat, cadetFilePath, "r"); + FILE* cadetDat = fopen(cadetFilePath, "r"); if (cadetDat) { fclose(cadetDat); - strcpy_s(DatFileName, "CADET.DAT"); + strncpy(DatFileName, "CADET.DAT", sizeof DatFileName); pb::FullTiltMode = true; } @@ -179,7 +178,7 @@ int winmain::WinMain(LPCSTR lpCmdLine) { char buf[60]; auto elapsedSec = static_cast(curTime - prevTime) * 0.001f; - sprintf_s(buf, "Updates/sec = %02.02f Frames/sec = %02.02f ", + snprintf(buf, sizeof buf, "Updates/sec = %02.02f Frames/sec = %02.02f ", 300.0f / elapsedSec, frameCounter / elapsedSec); SDL_SetWindowTitle(window, buf); frameCounter = 0; @@ -440,7 +439,7 @@ void winmain::RenderUi() if (ImGui::BeginMenu("Help")) { #ifndef NDEBUG - if (ImGui::MenuItem("ImGui Demo")) + if (ImGui::MenuItem("ImGui Demo", nullptr, ShowImGuiDemo)) { ShowImGuiDemo ^= true; } @@ -689,7 +688,7 @@ void winmain::memalloc_failure() char* caption = pinball::get_rc_string(170, 0); char* text = pinball::get_rc_string(179, 0); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, caption, text, MainWindow); - _exit(1); + std::exit(1); } void winmain::a_dialog() diff --git a/SpaceCadetPinball/zdrv.cpp b/SpaceCadetPinball/zdrv.cpp index 0003d01..30a26bf 100644 --- a/SpaceCadetPinball/zdrv.cpp +++ b/SpaceCadetPinball/zdrv.cpp @@ -54,7 +54,7 @@ void zdrv::paint(int width, int height, gdrv_bitmap8* dstBmp, int dstBmpXOff, in int dstZMapXOff, int dstZMapYOff, gdrv_bitmap8* srcBmp, int srcBmpXOff, int srcBmpYOff, zmap_header_type* srcZMap, int srcZMapXOff, int srcZMapYOff) { - if (srcBmp->BitmapType == BitmapType::Spliced) + if (srcBmp->BitmapType == BitmapTypes::Spliced) { /*Spliced bitmap is also a zMap, how convenient*/ paint_spliced_bmp(srcBmp->XPosition, srcBmp->YPosition, dstBmp, dstZMap, srcBmp); @@ -121,7 +121,7 @@ void zdrv::paint_flat(int width, int height, gdrv_bitmap8* dstBmp, int dstBmpXOf void zdrv::paint_spliced_bmp(int xPos, int yPos, gdrv_bitmap8* dstBmp, zmap_header_type* dstZmap, gdrv_bitmap8* srcBmp) { - assertm(srcBmp->BitmapType == BitmapType::Spliced, "Wrong bmp type"); + assertm(srcBmp->BitmapType == BitmapTypes::Spliced, "Wrong bmp type"); int xOffset = xPos - pb::MainTable->XOffset; int yOffset = dstBmp->Height - srcBmp->Height - (yPos - pb::MainTable->YOffset); if (yOffset < 0)