prepare linux build, fix some memory leaks

This commit is contained in:
Ondřej Novák 2025-04-20 18:16:29 +02:00
parent 0fa5c48519
commit e7db30ca27
13 changed files with 133 additions and 94 deletions

View file

@ -3,19 +3,22 @@ project(skeldal)
# Najít SDL2 knihovnu # Najít SDL2 knihovnu
find_package(SDL2 REQUIRED) find_package(SDL2 REQUIRED)
set(STEAMWORKS_SDK_DIR "${CMAKE_SOURCE_DIR}/external/steamworks/")
# Check if Steamworks SDK directories exist set(STEAMWORKS_SDK_DIR "" CACHE PATH "Path to Steamworks SDK directory")
if(NOT EXISTS "${STEAMWORKS_SDK_DIR}/public")
message(FATAL_ERROR "❌ Could not find Steamworks SDK 'public' headers. if(NOT STEAMWORKS_SDK_DIR)
Make sure to download the Steamworks SDK and place it in:${STEAMWORKS_SDK_DIR} set(POSSIBLE_STEAMWORKS_DIR "${CMAKE_SOURCE_DIR}/external/steamworks")
Expected directory: ${STEAMWORKS_SDK_DIR}/public if(EXISTS "${POSSIBLE_STEAMWORKS_DIR}/public")
") if(NOT EXISTS "${STEAMWORKS_SDK_DIR}/redistributable_bin")
endif() set(STEAMWORKS_SDK_DIR "${POSSIBLE_STEAMWORKS_DIR}" CACHE PATH "Path to Steamworks SDK directory" FORCE)
if(NOT EXISTS "${STEAMWORKS_SDK_DIR}/redistributable_bin") endif()
message(FATAL_ERROR "❌ Could not find Steamworks SDK 'redistributable_bin' libraries. endif()
Make sure to download the Steamworks SDK and place it in: ${STEAMWORKS_SDK_DIR} if (NOT STEAMWORKS_SDK_DIR)
Expected directory: ${STEAMWORKS_SDK_DIR}/redistributable_bin message("!!! Steam is not installed")
") message("!!! compiling without steam and without achievements support")
message("!!! To enable steam, set STEAMWORKS_SDK_DIR to correct value")
set(STEAMWORKS_SDK_DIR "none" CACHE PATH "Path to Steamworks SDK directory" FORCE)
endif()
endif() endif()
if (MSVC) if (MSVC)
@ -26,6 +29,18 @@ else()
set(STANDARD_LIBRARIES "pthread") set(STANDARD_LIBRARIES "pthread")
endif() endif()
if(${STEAMWORKS_SDK_DIR} STREQUAL "none")
set(STEAM_ENABLED 0)
else()
add_compile_definitions(STEAM_ENABLED)
include_directories(${STEAMWORKS_SDK_DIR}/public)
set(STEAM_ENABLED 1)
endif()
include_directories(.) include_directories(.)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/)
@ -33,7 +48,6 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/)
include_directories( ${SDL2_INCLUDE_DIRS}) include_directories( ${SDL2_INCLUDE_DIRS})
include_directories(${STEAMWORKS_SDK_DIR}/public)
add_subdirectory(libs) add_subdirectory(libs)
add_subdirectory(platform) add_subdirectory(platform)
add_subdirectory(game) add_subdirectory(game)

View file

@ -2,6 +2,7 @@
#include <libs/event.h> #include <libs/event.h>
#include <platform/achievements.h> #include <platform/achievements.h>
#include "globals.h" #include "globals.h"
#include "../platform/error.h"
#include "../platform/platform.h" #include "../platform/platform.h"
#include <ctype.h> #include <ctype.h>
@ -281,24 +282,6 @@ static PARSED_COMMAND parse_command(const char *cmd) {
extern int ghost_walls; extern int ghost_walls;
extern int nofloors; extern int nofloors;
static int add_file_to_console(const char *name, LIST_FILE_TYPE _, size_t __, void *ctx) {
int *cnt = (void *)ctx;
char buff[20] = "";
for (int i = 0; i < 19; ++i) buff[i] = ' ';
buff[19] = 0;
int l = strlen(name);
if (l > 3 && istrcmp(name+l-4,".MAP") == 0) {
if (l>19) l = 19;
memcpy(buff, name, l);
wzprintf("%s", buff);
if (++(*cnt) == 6) {
wzprintf("\n");
*cnt = 0;
}
}
return 0;
}
static int process_on_off_command(const char *cmd, char on) { static int process_on_off_command(const char *cmd, char on) {
if (istrcmp(cmd, "inner-eye") == 0) { if (istrcmp(cmd, "inner-eye") == 0) {
@ -358,9 +341,8 @@ static int process_on_off_command(const char *cmd, char on) {
void postavy_teleport_effect(int sector,int dir,int postava,char effect); void postavy_teleport_effect(int sector,int dir,int postava,char effect);
static int command_ls_callback(const char*name, void* ctx) { static void command_ls_callback(const char *name, void* ctx) {
wzprintf("%s\n", name); wzprintf("%s\n", name);
return 0;
} }
static int process_actions(const char *command) { static int process_actions(const char *command) {
@ -651,6 +633,25 @@ static int process_with_params(const char *cmd, const char *args) {
free(data); free(data);
return 1; return 1;
} }
if (istrcmp(cmd, "rm") == 0) {
if (strcmp(args, "*") == 0) {
temp_storage_clear();
} else {
int32_t sz =temp_storage_find(args);
if (sz < 0) return 0;
temp_storage_delete(args);
}
return 1;
}
if (istrcmp(cmd, "crash") == 0) {
throw_exception(args);
return 1;
}
return 0; return 0;
} }

View file

@ -38,7 +38,7 @@
#define SSAVE_VERSION 0 #define SSAVE_VERSION 0
static TMPFILE_WR *story=NULL; //static TMPFILE_WR *story=NULL;
static char load_another; static char load_another;
static unsigned long current_campaign = 0; static unsigned long current_campaign = 0;
static long prev_game_time_save = -999; static long prev_game_time_save = -999;
@ -505,7 +505,7 @@ static void add_status_file(FILE *f, const char *name, size_t sz, void *data) {
fwrite(name, 1, name_size_b, f); fwrite(name, 1, name_size_b, f);
uint32_t data_size = sz; uint32_t data_size = sz;
fwrite(&data_size,1,sizeof(data_size),f); fwrite(&data_size,1,sizeof(data_size),f);
fwrite(data, 1,data_size, f); if (data && data_size) fwrite(data,1,data_size, f);
} }
static void pack_status_file_cb(const char *name, void *ctx) { static void pack_status_file_cb(const char *name, void *ctx) {
@ -942,6 +942,7 @@ typedef struct {
char skip_autosave; char skip_autosave;
} TSAVEGAME_CB_STATE; } TSAVEGAME_CB_STATE;
/*
static char is_same_prefix(const char *name, const char *prev_name) { static char is_same_prefix(const char *name, const char *prev_name) {
const char *sep1 = strrchr(name, '.'); const char *sep1 = strrchr(name, '.');
const char *sep2 = strrchr(prev_name, '.'); const char *sep2 = strrchr(prev_name, '.');
@ -950,7 +951,7 @@ static char is_same_prefix(const char *name, const char *prev_name) {
if (strncmp(name, prev_name,sep1-name) == 0) return 1; if (strncmp(name, prev_name,sep1-name) == 0) return 1;
return 0; return 0;
} }
*/
static int get_all_savegames_callback(const char *name, LIST_FILE_TYPE type , size_t tm, void *ctx) { static int get_all_savegames_callback(const char *name, LIST_FILE_TYPE type , size_t tm, void *ctx) {
if (istrncmp(name, "sav.", 4) != 0 if (istrncmp(name, "sav.", 4) != 0
&& istrcmp(name+strlen(name)-4,".sav") != 0) && istrcmp(name+strlen(name)-4,".sav") != 0)
@ -974,11 +975,11 @@ static int get_all_savegames_callback(const char *name, LIST_FILE_TYPE type , s
} }
return 0; return 0;
} }
/*
static int compare_strings (const void *a, const void *b) { static int compare_strings (const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b); return strcmp(*(const char **)a, *(const char **)b);
} }
*/
static int compare_strings_third_back (const void *a, const void *b) { static int compare_strings_third_back (const void *a, const void *b) {
const char *sa = *(const char **)a; const char *sa = *(const char **)a;
const char *sb = *(const char **)b; const char *sb = *(const char **)b;
@ -995,7 +996,7 @@ static int compare_strings_third_back (const void *a, const void *b) {
return -strcmp(ba,bb); return -strcmp(ba,bb);
} }
/*
static int dedup_strings_prefix(TSTR_LIST lst, int count) { static int dedup_strings_prefix(TSTR_LIST lst, int count) {
int j = -1; int j = -1;
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
@ -1014,7 +1015,7 @@ static int dedup_strings_prefix(TSTR_LIST lst, int count) {
} }
/*
static void load_specific_file(int slot_num,char *filename,void **out,int32_t *size) //call it in task! static void load_specific_file(int slot_num,char *filename,void **out,int32_t *size) //call it in task!
{ {
FILE *slot; FILE *slot;
@ -1596,7 +1597,7 @@ void wire_ask_gamename(int id)
} }
static void save_as_dialog(int pos); static void save_as_dialog(int pos);
static const char *find_autosave(const char *name);
#define CLK_SAVELOAD 11 #define CLK_SAVELOAD 11
@ -1799,24 +1800,20 @@ void wire_save_load(char save) {
void open_story_file() void open_story_file()
{ {
story=temp_storage_append(STORY_BOOK);
SEND_LOG("(STORY) Story temp file is opened....");
} }
void write_story_text(char *text) void write_story_text(char *text)
{ {
int l = strlen(text); int l = strlen(text);
TMPFILE_WR *story = temp_storage_append(STORY_BOOK);
temp_storage_write( text, l, story); temp_storage_write( text, l, story);
temp_storage_write("\n", 1, story); temp_storage_write("\n", 1, story);
temp_storage_close_wr(story);
} }
void close_story_file() void close_story_file()
{ {
if (story!=NULL) temp_storage_close_wr(story);
story=NULL;
SEND_LOG("(STORY) Story temp file is closed...");
} }
#if 0 #if 0
@ -1909,6 +1906,7 @@ void do_autosave() {
remove(build_pathname(2, gpathtable[SR_SAVES],n)); remove(build_pathname(2, gpathtable[SR_SAVES],n));
} }
} }
release_list(st.files);
} }
save_game(get_save_game_slot_id(), game_name,1); save_game(get_save_game_slot_id(), game_name,1);
} }

View file

@ -463,7 +463,7 @@ void type_text_v2(va_list args)
case 8:if (pos>0) case 8:if (pos>0)
{ {
pos--; pos--;
strcpy(&text[pos],&text[pos+1]); memmove(text+pos,text+pos+1, strlen(text+pos+1)+1);
len--; len--;
} }
break; break;

View file

@ -1078,7 +1078,7 @@ char check_jidlo_voda(THUMAN *p)
char check_map_specials(THUMAN *p) char check_map_specials(THUMAN *p)
{ {
char c=0; char c=0;
if (p->inmaphash != current_map_hash) return; if (p->inmaphash != current_map_hash) return c;
switch(mglob.map_effector) switch(mglob.map_effector)
{ {
case ME_NORMAL:break; case ME_NORMAL:break;
@ -1144,16 +1144,16 @@ void real_regeneration(THE_TIMER *t)
int i; int i;
THUMAN *p; THUMAN *p;
for(i=0;i<POCET_POSTAV;i++) for(i=0;i<POCET_POSTAV;i++) {
{
p=&postavy[i]; p=&postavy[i];
if (p->used) if (p->used) {
if (p->lives) { if (p->lives) {
pomala_regenerace_postavy(p); pomala_regenerace_postavy(p);
} else { } else {
umirani_postavy(p); umirani_postavy(p);
} }
} }
}
send_message(E_KOUZLO_KOLO); send_message(E_KOUZLO_KOLO);
sleep_ticks+=MAX_SLEEP/30; sleep_ticks+=MAX_SLEEP/30;
if (sleep_ticks>MAX_SLEEP) sleep_ticks=MAX_SLEEP; if (sleep_ticks>MAX_SLEEP) sleep_ticks=MAX_SLEEP;

View file

@ -358,6 +358,7 @@ int load_map(const char *filename)
{ {
load_enemies(temp,size,&ofsts,mob_template,mob_size); load_enemies(temp,size,&ofsts,mob_template,mob_size);
ablock_free(mob_template); ablock_free(mob_template);
mob_template=NULL;
SEND_LOG("(GAME) Loading enemies from map template..."); SEND_LOG("(GAME) Loading enemies from map template...");
} }
free(temp); free(temp);
@ -365,6 +366,7 @@ int load_map(const char *filename)
case A_MAPMACR: case A_MAPMACR:
SEND_LOG("(GAME) Loading multiactions..."); SEND_LOG("(GAME) Loading multiactions...");
load_macros(size,temp); load_macros(size,temp);
free(temp);
break; break;
case A_MAPVYK: case A_MAPVYK:
map_vyk=temp; map_vyk=temp;
@ -373,6 +375,7 @@ int load_map(const char *filename)
case A_MOBS: case A_MOBS:
mob_template=load_mob_legacy_format_direct(temp, &size,0); mob_template=load_mob_legacy_format_direct(temp, &size,0);
mob_size=size; mob_size=size;
free(temp);
break; break;
case A_MOBSND: case A_MOBSND:
snd_load=1; snd_load=1;
@ -391,11 +394,13 @@ int load_map(const char *filename)
else else
{ {
if (temp!=NULL)free(temp); if (temp!=NULL)free(temp);
ablock_free(mob_template);
fclose(f); fclose(f);
return -3; return -3;
} }
} }
while (nmapend); while (nmapend);
ablock_free(mob_template);
fclose(f); fclose(f);
flag_map=(char *)getmem(mapsize*4); flag_map=(char *)getmem(mapsize*4);
memset(minimap,0,sizeof(minimap)); memset(minimap,0,sizeof(minimap));

View file

@ -538,11 +538,13 @@ void hide_ms_cursor()
} }
void redraw_ms_cursor_on_screen(void) { void redraw_ms_cursor_on_screen(void) {
#ifdef FORCE_SOFTWARE_CURSOR
if (mssavebuffer) { if (mssavebuffer) {
integer xs=*(integer *)mssavebuffer; integer xs=*(integer *)mssavebuffer;
integer ys=*((integer *)mssavebuffer+1); integer ys=*((integer *)mssavebuffer+1);
showview(mscuroldx,mscuroldy,xs,ys); showview(mscuroldx,mscuroldy,xs,ys);
} }
#endif
} }

View file

@ -35,23 +35,27 @@ if(WIN32)
windows/skeldal.rc windows/skeldal.rc
) )
target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS) target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS)
if(CMAKE_SIZEOF_VOID_P EQUAL 8) if(STEAM_ENABLED)
# 64-bit if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(STEAMLIB ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api64.lib) # 64-bit
set(STEAMDLL ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api64.dll) set(STEAMLIB ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api64.lib)
target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS_64) set(STEAMDLL ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api64.dll)
else()
# 32-bit
set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/win32/steam_api.lib)
set(STEAMDLL ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api.dll)
endif()
else() else()
# 32-bit set(STEAMLIB "")
set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/win32/steam_api.lib) set(STEAMDLL "")
set(STEAMDLL ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api.dll)
target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS_32)
endif() endif()
target_link_libraries(skeldal ${all_libs} ${STEAMLIB}) target_link_libraries(skeldal ${all_libs} ${STEAMLIB})
add_custom_command(TARGET skeldal POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different if(STEAMDLL)
${STEAMDLL} $<TARGET_FILE_DIR:skeldal> add_custom_command(TARGET skeldal POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
) ${STEAMDLL} $<TARGET_FILE_DIR:skeldal>)
endif()
message(STATUS "Building for Windows") message(STATUS "Building for Windows")
@ -65,14 +69,14 @@ elseif(UNIX AND NOT APPLE)
linux/app_start.cpp linux/app_start.cpp
) )
target_compile_definitions(skeldal_bin PRIVATE PLATFORM_LINUX) target_compile_definitions(skeldal_bin PRIVATE PLATFORM_LINUX)
if(CMAKE_SIZEOF_VOID_P EQUAL 8) if(STEAM_ENABLED)
# 64-bit if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/linux64/libsteam_api.so) # 64-bit
target_compile_definitions(skeldal_bin PRIVATE PLATFORM_LINUX_64) set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/linux64/libsteam_api.so)
else() else()
# 32-bit # 32-bit
set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/linux32/libsteam_api.so) set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/linux32/libsteam_api.so)
target_compile_definitions(skeldal_bin PRIVATE PLATFORM_LINUX_32) endif()
endif() endif()
add_custom_command( add_custom_command(
TARGET skeldal_bin POST_BUILD TARGET skeldal_bin POST_BUILD

View file

@ -1,9 +1,10 @@
#include "achievements.h"
#include <steam/steam_api.h>
#include "error.h"
#include <string.h> #include <string.h>
#include <sstream> #include <sstream>
#include "achievements.h"
#include "error.h"
#include "platform.h" #include "platform.h"
#ifdef STEAM_ENABLED
#include <steam/steam_api.h>
extern "C" { extern "C" {
#include <libs/event.h> #include <libs/event.h>
@ -104,3 +105,11 @@ char *get_steam_status()
char *out = strdup(str.c_str()); char *out = strdup(str.c_str());
return out; return out;
} }
#else
void steam_init() {}
void steam_shutdown() {}
int8_t set_achievement(const char *id) {return -1;}
int8_t clear_achievement(const char *id) {return -1;}
char is_steam_available() {return 0;}
char *get_steam_status() {return NULL;}
#endif

View file

@ -34,3 +34,7 @@ void send_log_impl(const char *format, ...) {
#endif #endif
} }
void throw_exception(const char *text) {
throw std::runtime_error(std::string("Invoked crash:") + text);
}

View file

@ -8,6 +8,7 @@ extern "C" {
void send_log_impl(const char *format, ...); void send_log_impl(const char *format, ...);
void display_error(const char *format, ...); void display_error(const char *format, ...);
void throw_exception(const char *text);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
temp_file=$(mktemp /tmp/skeldal.XXXXXX.log) temp_file=$(mktemp /tmp/skeldal.XXXXXX.log)
CURDIR=`dirname "$0"`
`dirname $0`/skeldal_bin $* > "$temp_file" 2>&1 LD_LIBRARY_PATH=$CURDIR:$LD_LIBRARY_PATH "$CURDIR/skeldal_bin" $* > "$temp_file" 2>&1
exit_code=$? exit_code=$?
@ -24,3 +24,5 @@ if [ $exit_code -ne 0 ]; then
fi fi
rm $temp_file rm $temp_file
exit $exit_code exit $exit_code

View file

@ -11,7 +11,6 @@
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <steam/steam_api.h>
#include <stdbool.h> #include <stdbool.h>
#include <thread> #include <thread>
#include <mutex> #include <mutex>