diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 200b48e6e..f4a496f8c 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -91,7 +91,7 @@ CONFIGURE_FILE(src/version.h.in ${Vega_Strike_BINARY_DIR}/setup/src/include/vers MESSAGE("== Vega Strike Version: ${VEGASTRIKE_VERSION_LONG_STR}") -SET(CMAKE_CXX_STANDARD 11) +SET(CMAKE_CXX_STANDARD 14) SET(CMAKE_CXX_STANDARD_REQUIRED TRUE) SET(CMAKE_CXX_EXTENSIONS ON) SET(CMAKE_C_STANDARD 11) @@ -1090,6 +1090,7 @@ ADD_LIBRARY(vegastrike-engine_com ${LIBROOTGENERIC_SOURCES} ${LIBSCRIPT_SOURCES} ${LIBGFXGENERIC_SOURCES} + src/vega_collection_utils.cpp ) #TARGET_COMPILE_FEATURES(vegastrike-engine_com PUBLIC cxx_std_11) @@ -1703,6 +1704,7 @@ IF (USE_GTEST) ${LIBRESOURCE} ${LIBCMD_SOURCES} ${LIBVS_LOGGING} + src/vega_collection_utils.cpp ) target_compile_definitions(vegastrike-testing PUBLIC "BOOST_ALL_DYN_LINK" "$<$:BOOST_DEBUG_PYTHON>") set_property(TARGET vegastrike-testing PROPERTY POSITION_INDEPENDENT_CODE TRUE) diff --git a/engine/objconv/basemaker/base_maker.cpp b/engine/objconv/basemaker/base_maker.cpp index da479c466..79ea82441 100644 --- a/engine/objconv/basemaker/base_maker.cpp +++ b/engine/objconv/basemaker/base_maker.cpp @@ -49,11 +49,11 @@ #include #if defined(__APPLE__) && defined(__MACH__) - #include - #include +# include +# include #else - #include - #include +# include +# include #endif /* diff --git a/engine/objconv/basemaker/sprite.h b/engine/objconv/basemaker/sprite.h index 4c5a8221d..21aebd78e 100644 --- a/engine/objconv/basemaker/sprite.h +++ b/engine/objconv/basemaker/sprite.h @@ -31,11 +31,11 @@ #include "vs_globals.h" #if defined(__APPLE__) && defined(__MACH__) - #include - #include +# include +# include #else - #include - #include +# include +# include #endif #include diff --git a/engine/src/cmd/drawable.cpp b/engine/src/cmd/drawable.cpp index 04e20c267..5a6da6772 100644 --- a/engine/src/cmd/drawable.cpp +++ b/engine/src/cmd/drawable.cpp @@ -1,7 +1,7 @@ /* * drawable.cpp * - * Copyright (C) 2020-2022 Daniel Horn, Roy Falk, Stephen G. Tuggy and other + * Copyright (C) 2020-2024 Daniel Horn, Roy Falk, Stephen G. Tuggy and other * Vega Strike contributors * * https://github.com/vegastrike/Vega-Strike-Engine-Source @@ -83,7 +83,7 @@ Drawable::~Drawable() { } bool Drawable::DrawableInit(const char *filename, int faction, - Flightgroup *flightgrp, const char *animationExt) { + Flightgroup *flightgrp, const char *animationExt) { string fnam(filename); string::size_type pos = fnam.find('.'); string anifilename = fnam.substr(0, pos); diff --git a/engine/src/cmd/drawable.h b/engine/src/cmd/drawable.h index dc0db5d7e..08a9f0298 100644 --- a/engine/src/cmd/drawable.h +++ b/engine/src/cmd/drawable.h @@ -1,9 +1,8 @@ -/** +/* * drawable.h * - * Copyright (C) 2020 Roy Falk, Stephen G. Tuggy and other Vega Strike - * contributors - * Copyright (C) 2022-2023 Stephen G. Tuggy, Benjamen R. Meyer + * Copyright (C) 2020-2024 Roy Falk, Stephen G. Tuggy, Benjamen R. Meyer, + * and other Vega Strike contributors * * https://github.com/vegastrike/Vega-Strike-Engine-Source * @@ -16,7 +15,7 @@ * * Vega Strike is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License diff --git a/engine/src/cmd/unit_generic.cpp b/engine/src/cmd/unit_generic.cpp index 1f4899261..8a72dfaa1 100644 --- a/engine/src/cmd/unit_generic.cpp +++ b/engine/src/cmd/unit_generic.cpp @@ -361,6 +361,7 @@ Unit::~Unit() { #endif pImage = nullptr; delete pilot; + pilot = nullptr; #ifdef DESTRUCTDEBUG VS_LOG_AND_FLUSH(trace, (boost::format("%1$d") % 5)); #endif @@ -384,10 +385,10 @@ Unit::~Unit() { #ifdef DESTRUCTDEBUG VS_LOG_AND_FLUSH(trace, (boost::format("%1$d") % 0)); #endif - for (size_t meshcount = 0; meshcount < meshdata.size(); ++meshcount) { - if (meshdata[meshcount] != nullptr) { - delete meshdata[meshcount]; - meshdata[meshcount] = nullptr; + for (auto & mesh : meshdata) { + if (mesh != nullptr) { + delete mesh; + mesh = nullptr; } } meshdata.clear(); diff --git a/engine/src/gfx/aux_texture.cpp b/engine/src/gfx/aux_texture.cpp index be788cd04..c052b72db 100644 --- a/engine/src/gfx/aux_texture.cpp +++ b/engine/src/gfx/aux_texture.cpp @@ -556,10 +556,7 @@ Texture::~Texture() { } else { original->refcount--; if (original->refcount == 0) { - if (original == this) { - VS_LOG(debug, "In Texture::~Texture() in aux_texture.cpp: original->refcount == 0, but original == this, so not deleting again. (We are already in the destructor.)"); - } else { - VS_LOG(trace, "In Texture::~Texture() in aux_texture.cpp: original->refcount == 0, and original != this, so deleting original"); + if (original != this) { delete original; original = nullptr; } diff --git a/engine/src/gfx/mesh_gfx.cpp b/engine/src/gfx/mesh_gfx.cpp index 1cc797934..6b2bedcbd 100644 --- a/engine/src/gfx/mesh_gfx.cpp +++ b/engine/src/gfx/mesh_gfx.cpp @@ -59,6 +59,7 @@ extern vector undrawn_logos; #include +#include #ifdef _MSC_VER //Undefine those nasty min/max macros @@ -438,45 +439,10 @@ Mesh::~Mesh() { meshHashTable.Delete(hash_name); } vector *hashers = bfxmHashTable.Get(hash_name); - vector::iterator finder; - if (hashers) { - // the foollowing loop has several tricks to it: - // 1. `std::vector::erase()` can take an interator and remove it from the vector; but invalidates - // the iterator in the process - // 2. To overcome the invalid iterator issue, the next previous iterator is cached - // 3. In the case that the previous iterator would be invalid (e.g it's at the start) then it needs - // to restart the loop without the loop conditions, therefore a simple GOTO is used instead to - // avoid the incrementing the iterator so that values are not skipped - // A reverse iterator could kind of help this; however, `std::vector::erase` unfortunately - // does not work on reverse iterators. - for (auto hashItem = hashers->begin(); hashItem != hashers->end(); ++hashItem) { -retryEraseItem: - if (*hashItem == this) { - bool resetIter = false; - auto cachedHashItem = hashers->begin(); - if (hashItem != hashers->begin()) { - cachedHashItem = hashItem - 1; - } else { - resetIter = true; - cachedHashItem = hashItem + 1; - } - hashers->erase(hashItem); - if (hashers->empty()) { - bfxmHashTable.Delete(hash_name); - delete hashers; - hashers = nullptr; - break; - } - - if (resetIter) { - hashItem = hashers->begin(); - // a necessary use of Goto as we do not want to use ++hashItem - goto retryEraseItem; - } else { - hashItem = cachedHashItem; - } - } - } + bool hashers_was_deleted = false; + remove_all_references_to(this, hashers, hashers_was_deleted); + if (hashers_was_deleted) { + VS_LOG_AND_FLUSH(debug, "hashers was deleted"); } if (draw_queue != nullptr) { delete[] draw_queue; diff --git a/engine/src/gfx/mesh_server.cpp b/engine/src/gfx/mesh_server.cpp index abe851780..2419076a6 100644 --- a/engine/src/gfx/mesh_server.cpp +++ b/engine/src/gfx/mesh_server.cpp @@ -1,10 +1,8 @@ -/** +/* * mesh_server.cpp * - * Copyright (c) 2001-2002 Daniel Horn - * Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors - * Copyright (c) 2019-2021 Stephen G. Tuggy, and other Vega Strike Contributors - * Copyright (C) 2022 Stephen G. Tuggy + * Copyright (c) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Benjamen R. Meyer, Roy Falk, and other Vega Strike Contributors * * https://github.com/vegastrike/Vega-Strike-Engine-Source * @@ -17,7 +15,7 @@ * * Vega Strike is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License @@ -120,17 +118,10 @@ Mesh::~Mesh() { meshHashTable.Delete(hash_name); } vector *hashers = bfxmHashTable.Get(hash_name); - vector::iterator finder; - if (hashers) { - for (int i = hashers->size() - 1; i >= 0; --i) { - if ((*hashers)[i] == this) { - hashers->erase(hashers->begin() + i); - if (hashers->empty()) { - bfxmHashTable.Delete(hash_name); - delete hashers; - } - } - } + bool hashers_was_deleted = false; + remove_all_references_to(this, hashers, hashers_was_deleted); + if (hashers_was_deleted) { + VS_LOG_AND_FLUSH(debug, "hashers was deleted"); } orig->refcount--; //printf ("orig refcount: %d",refcount); diff --git a/engine/src/gldrv/gl_globals.h b/engine/src/gldrv/gl_globals.h index 429cb4012..01387213e 100644 --- a/engine/src/gldrv/gl_globals.h +++ b/engine/src/gldrv/gl_globals.h @@ -93,8 +93,8 @@ struct GFXStats #ifndef _WIN32 //#define GL_GLEXT_PROTOTYPES - #endif + #if defined (_WIN32) || defined (__CYGWIN__) #ifndef NOMINMAX #define NOMINMAX @@ -102,6 +102,7 @@ struct GFXStats #include #include #endif + #if defined(__APPLE__) && defined(__MACH__) #include //#if defined( GL_INIT_CPP) || defined( GL_MISC_CPP) || defined( GL_STATE_CPP) @@ -121,6 +122,7 @@ struct GFXStats #include #endif + #ifdef _WIN32 # define GL_TEXTURE0_ARB 0x84C0 # define GL_TEXTURE1_ARB 0x84C1 diff --git a/engine/src/gldrv/gl_init.cpp b/engine/src/gldrv/gl_init.cpp index 3bf4951df..b65e4cfec 100644 --- a/engine/src/gldrv/gl_init.cpp +++ b/engine/src/gldrv/gl_init.cpp @@ -55,12 +55,12 @@ #endif #define GL_GLEXT_PROTOTYPES 1 #if defined(__APPLE__) && defined(__MACH__) - #include - #include - #include +# include +# include +# include #else - #include - #include +# include +# include #endif #ifdef GL_EXT_compiled_vertex_array # ifndef PFNGLLOCKARRAYSEXTPROC @@ -149,7 +149,7 @@ typedef void ( *(*get_gl_proc_fptr_t)(const GLubyte *))(); #if defined(__HAIKU__) typedef char * GET_GL_PTR_TYP; #define GET_GL_PROC glutGetProcAddress - #elif defined (__MACOSX__) + #elif defined (__APPLE__) && defined (__MACH__) typedef const char * GET_GL_PTR_TYP; void * vega_dlsym(GET_GL_PTR_TYP function_name) { return dlsym(RTLD_SELF, function_name); @@ -586,6 +586,8 @@ void GFXInit(int argc, char **argv) { char vsicon[9] = "vega.ico"; winsys_init(&argc, argv, &vsname[0], &vsicon[0]); + /* Ignore key-repeat messages */ + winsys_enable_key_repeat(false); glViewport(0, 0, g_game.x_resolution, g_game.y_resolution); static GFXColor clearcol = vs_config->getColor("space_background");; diff --git a/engine/src/gldrv/gl_init.h b/engine/src/gldrv/gl_init.h index a74fd305c..025674528 100644 --- a/engine/src/gldrv/gl_init.h +++ b/engine/src/gldrv/gl_init.h @@ -62,11 +62,11 @@ typedef void (*PFNGLUNLOCKARRAYSEXTPROC)( void ); #elif !defined (_WIN32) #if defined(__APPLE__) && defined(__MACH__) - #include - #include +# include +# include #else - #include - #include +# include +# include #endif #if !defined (GL_GLEXT_VERSION) || GL_GLEXT_VERSION < 6 diff --git a/engine/src/gldrv/winsys.cpp b/engine/src/gldrv/winsys.cpp index ae0723b9b..9f2d24c13 100644 --- a/engine/src/gldrv/winsys.cpp +++ b/engine/src/gldrv/winsys.cpp @@ -15,7 +15,7 @@ * * Vega Strike is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License @@ -25,6 +25,7 @@ #include #include +#include #include "gl_globals.h" #include "winsys.h" @@ -351,6 +352,23 @@ void winsys_shutdown() { keepRunning = false; } +/*---------------------------------------------------------------------------*/ +/*! + * Enables/disables key repeat messages from being generated + * \return + * \author jfpatry + * \date Created: 2000-10-19 + * \date Modified: 2000-10-19 + */ +void winsys_enable_key_repeat(bool enabled) { + if (enabled) { + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, + SDL_DEFAULT_REPEAT_INTERVAL); + } else { + SDL_EnableKeyRepeat(0, 0); + } +} + /*---------------------------------------------------------------------------*/ /*! * Shows/hides mouse cursor diff --git a/engine/src/gldrv/winsys.h b/engine/src/gldrv/winsys.h index b1b7f11ba..642c1bd0b 100644 --- a/engine/src/gldrv/winsys.h +++ b/engine/src/gldrv/winsys.h @@ -15,7 +15,7 @@ * * Vega Strike is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License @@ -318,6 +318,7 @@ void winsys_set_motion_func(winsys_motion_func_t func); void winsys_set_passive_motion_func(winsys_motion_func_t func); void winsys_swap_buffers(); +void winsys_enable_key_repeat(bool enabled); void winsys_warp_pointer(int x, int y); void winsys_show_cursor(bool visible); diff --git a/engine/src/owner.h b/engine/src/owner.h new file mode 100644 index 000000000..0efdf6f68 --- /dev/null +++ b/engine/src/owner.h @@ -0,0 +1,37 @@ +/* + * owner.h + * + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Benjamen R. Meyer, Roy Falk, and other Vega Strike contributors + * + * https://github.com/vegastrike/Vega-Strike-Engine-Source + * + * This file is part of Vega Strike. + * + * Vega Strike is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Vega Strike is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Vega Strike. If not, see . + */ + +#ifndef VEGA_STRIKE_OWNER_H +#define VEGA_STRIKE_OWNER_H + +#include +#include + +using std::shared_ptr; +using std::unique_ptr; + +template ::value>> +using owner = T; + +#endif //VEGA_STRIKE_OWNER_H diff --git a/engine/src/precompiled.h b/engine/src/precompiled.h deleted file mode 100644 index 4b8cae670..000000000 --- a/engine/src/precompiled.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * precompiled.h - * - * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, - * Benjamen R. Meyer, and other Vega Strike contributors. - * - * https://github.com/vegastrike/Vega-Strike-Engine-Source - * - * This file is part of Vega Strike. - * - * Vega Strike is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Vega Strike is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Vega Strike. If not, see . - */ -// NO HEADER GUARD - -#include -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 - #include -#else //_WIN32 - #if __GNUC__ != 2 - #include - #endif //__GNUC__ != 2 -#endif //_WIN32 - -#include - -#if !defined (_WIN32) && !defined (__CYGWIN__) - #include -#else //!defined (_WIN32) && !defined (__CYGWIN__) - #ifndef NOMINMAX - //tells VCC not to generate min/max macros - #define NOMINMAX - #endif //NOMINMAX - #include -#endif //!defined (_WIN32) && !defined (__CYGWIN__) - -#define GL_GLEXT_PROTOTYPES 1 - -#if defined(__APPLE__) && defined(__MACH__) - #include - #include -#else //defined (__APPLE__) || defined (MACOSX) - #include - #include -#endif //defined (__APPLE__) || defined (MACOSX) - diff --git a/engine/src/vega_collection_utils.cpp b/engine/src/vega_collection_utils.cpp new file mode 100644 index 000000000..f8f5dd966 --- /dev/null +++ b/engine/src/vega_collection_utils.cpp @@ -0,0 +1,41 @@ +/* + * vega_collection_utils.cpp + * + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Benjamen R. Meyer, Roy Falk, and other Vega Strike contributors + * + * https://github.com/vegastrike/Vega-Strike-Engine-Source + * + * This file is part of Vega Strike. + * + * Vega Strike is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Vega Strike is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Vega Strike. If not, see . + */ + +#include "vega_collection_utils.h" +#include + +template void remove_all_references_to(const T *thus, owner *> &vec, bool& collection_was_deleted) { + if (vec) { + if (!vec->empty()) { + auto first_to_remove = std::stable_partition(vec->begin(), vec->end(), + [thus](const T *pi) { return pi != thus; }); + vec->erase(first_to_remove, vec->end()); + } + if (vec->empty()) { + delete vec; + vec = nullptr; + collection_was_deleted = true; + } + } +} diff --git a/engine/src/vega_collection_utils.h b/engine/src/vega_collection_utils.h new file mode 100644 index 000000000..bdde2c4fd --- /dev/null +++ b/engine/src/vega_collection_utils.h @@ -0,0 +1,34 @@ +/* + * vega_collection_utils.h + * + * Copyright (C) 2001-2024 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * Benjamen R. Meyer, Roy Falk, and other Vega Strike contributors + * + * https://github.com/vegastrike/Vega-Strike-Engine-Source + * + * This file is part of Vega Strike. + * + * Vega Strike is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Vega Strike is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Vega Strike. If not, see . + */ + +#ifndef VEGA_STRIKE_VEGA_COLLECTION_UTILS_H +#define VEGA_STRIKE_VEGA_COLLECTION_UTILS_H + +#include +#include +#include "owner.h" + +template void remove_all_references_to(const T *thus, owner *> &vec, bool& collection_was_deleted); + +#endif //VEGA_STRIKE_VEGA_COLLECTION_UTILS_H diff --git a/engine/src/vegastrike.h b/engine/src/vegastrike.h index 93e2054a1..c82f524a3 100644 --- a/engine/src/vegastrike.h +++ b/engine/src/vegastrike.h @@ -69,17 +69,17 @@ extern float AUDIO_ATOM; #endif //defined (WIN32) || defined (__CYGWIN__) #ifndef NO_GFX - #if defined(__APPLE__) && defined(__MACH__) - #include - #include - #include - #else //defined (__APPLE__) || defined (MACOSX) - #define __glext_h_ - #include - #include - #include - #undef __glext_h_ - #endif //defined (__APPLE__) || defined (MACOSX) +# if defined(__APPLE__) && defined(__MACH__) +# include +# include +# include +# else //defined(__APPLE__) && defined(__MACH__) +# define __glext_h_ +# include +# include +# include +# undef __glext_h_ +# endif //defined(__APPLE__) && defined(__MACH__) #endif //NO_GFX #define PROG_NAME "vegastrike"