mirror of
https://github.com/ondra-novak/gates_of_skeldal.git
synced 2025-07-15 18:56:41 -04:00
add steam and achivements, improve console
This commit is contained in:
parent
9bfb0f1d5d
commit
f49a7490c1
13 changed files with 360 additions and 258 deletions
|
@ -11,6 +11,7 @@ target_sources(skeldal_platform PRIVATE
|
|||
error.cpp
|
||||
timer.cpp
|
||||
getopt.c
|
||||
achievements.cpp
|
||||
)
|
||||
|
||||
set(all_libs
|
||||
|
@ -19,7 +20,7 @@ set(all_libs
|
|||
skeldal_platform
|
||||
skeldal_sdl
|
||||
skeldal_libs
|
||||
${SDL2_LIBRARIES}
|
||||
${SDL2_LIBRARIES}
|
||||
${STANDARD_LIBRARIES})
|
||||
|
||||
if(WIN32)
|
||||
|
@ -34,7 +35,24 @@ if(WIN32)
|
|||
windows/skeldal.rc
|
||||
)
|
||||
target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS)
|
||||
target_link_libraries(skeldal ${all_libs})
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
# 64-bit
|
||||
set(STEAMLIB ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api64.lib)
|
||||
set(STEAMDLL ${STEAMWORKS_SDK_DIR}redistributable_bin/win64/steam_api64.dll)
|
||||
target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS_64)
|
||||
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)
|
||||
target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS_32)
|
||||
endif()
|
||||
|
||||
target_link_libraries(skeldal ${all_libs} ${STEAMLIB})
|
||||
|
||||
add_custom_command(TARGET skeldal POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${STEAMDLL} $<TARGET_FILE_DIR:skeldal>
|
||||
)
|
||||
|
||||
message(STATUS "Building for Windows")
|
||||
|
||||
elseif(UNIX AND NOT APPLE)
|
||||
|
@ -46,13 +64,22 @@ elseif(UNIX AND NOT APPLE)
|
|||
target_sources(skeldal_bin PRIVATE
|
||||
linux/app_start.cpp
|
||||
)
|
||||
target_compile_definitions(skeldal_platform PRIVATE PLATFORM_LINUX)
|
||||
target_compile_definitions(skeldal_bin PRIVATE PLATFORM_LINUX)
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
# 64-bit
|
||||
set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/linux64/libsteam_api.so)
|
||||
target_compile_definitions(skeldal_bin PRIVATE PLATFORM_LINUX_64)
|
||||
else()
|
||||
# 32-bit
|
||||
set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/linux32/libsteam_api.so)
|
||||
target_compile_definitions(skeldal_bin PRIVATE PLATFORM_LINUX_32)
|
||||
endif()
|
||||
add_custom_command(
|
||||
TARGET skeldal_bin POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_LIST_DIR}/linux/skeldal.sh
|
||||
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/skeldal.sh)
|
||||
target_link_libraries(skeldal_bin ${all_libs})
|
||||
target_link_libraries(skeldal_bin ${all_libs} ${STEAMLIB})
|
||||
message(STATUS "Building for Linux")
|
||||
|
||||
elseif(APPLE)
|
||||
|
@ -64,8 +91,10 @@ elseif(APPLE)
|
|||
linux/app_start.cpp
|
||||
)
|
||||
target_compile_definitions(mylib PRIVATE PLATFORM_MACOS)
|
||||
set(STEAMLIB ${STEAMWORKS_SDK_DIR}/redistributable_bin/osx/libsteam_api.dylib)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
|
||||
message(STATUS "Building for macOS")
|
||||
target_link_libraries(skeldal ${all_libs})
|
||||
target_link_libraries(skeldal ${all_libs} ${STEAMLIB})
|
||||
else()
|
||||
error("Platform not detected, please add new platform here")
|
||||
endif()
|
||||
|
|
103
platform/achievements.cpp
Normal file
103
platform/achievements.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
#include "achievements.h"
|
||||
#include <steam/steam_api.h>
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
#include <sstream>
|
||||
|
||||
extern "C" {
|
||||
#include <libs/event.h>
|
||||
}
|
||||
|
||||
static char steam_available = 0;
|
||||
static char steam_initialized = 0;
|
||||
|
||||
|
||||
void run_steam_callbacks(const EVENT_MSG *msg,void **) {
|
||||
if (msg->msg == E_IDLE) {
|
||||
SteamAPI_RunCallbacks();
|
||||
}
|
||||
}
|
||||
|
||||
void achievements_init()
|
||||
{
|
||||
if (!steam_initialized) {
|
||||
steam_initialized = 1;
|
||||
steam_available = SteamAPI_Init();
|
||||
if (steam_available) {
|
||||
send_message(E_ADD, E_IDLE, &run_steam_callbacks);
|
||||
SteamUserStats()->RequestUserStats(SteamUser()->GetSteamID());
|
||||
} else {
|
||||
steam_available = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void achievements_shutdown()
|
||||
{
|
||||
if (steam_available) {
|
||||
send_message(E_DONE, E_IDLE, &run_steam_callbacks);
|
||||
SteamAPI_Shutdown();
|
||||
steam_available = 0;
|
||||
steam_initialized = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int8_t set_achievement(const char *id)
|
||||
{
|
||||
achievements_init();
|
||||
|
||||
if (!steam_available) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (SteamUserStats() && SteamUserStats()->SetAchievement(id)) {
|
||||
SteamUserStats()->StoreStats();
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int8_t clear_achievement(const char *id)
|
||||
{
|
||||
achievements_init();
|
||||
|
||||
if (!steam_available) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (SteamUserStats() && SteamUserStats()->ClearAchievement(id)) {
|
||||
SteamUserStats()->StoreStats();
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
char is_steam_available()
|
||||
{
|
||||
return steam_available;
|
||||
}
|
||||
|
||||
char *get_steam_status()
|
||||
{
|
||||
std::ostringstream oss;
|
||||
int num_achievements = SteamUserStats()->GetNumAchievements();
|
||||
oss << "SteamAPI_Init: " << ( SteamAPI_Init() ? "success" : "failure") << "\n";
|
||||
oss << "UserStats pointer: " << SteamUserStats() << "\n";
|
||||
oss << "Is Steam overlay enabled:" << (SteamUtils()->IsOverlayEnabled() ? "yes" : "no") << "\n";
|
||||
oss << "AppID: "<< SteamUtils()->GetAppID() << "\n";
|
||||
oss << "Num Achievements: " << num_achievements << "\n";
|
||||
for (int i = 0; i < num_achievements; ++i) {
|
||||
const char* name = SteamUserStats()->GetAchievementName(i);
|
||||
bool achieved = false;
|
||||
SteamUserStats()->GetAchievement(name, &achieved);
|
||||
|
||||
oss << "[" << i << "] " << name << " - " << (achieved ? "Yes" : "No") << "\n";
|
||||
}
|
||||
|
||||
std::string str = oss.str();
|
||||
char *out = strdup(str.c_str());
|
||||
return out;
|
||||
}
|
36
platform/achievements.h
Normal file
36
platform/achievements.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
#include <stdint.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Initialize Steam (if available). Optional to call.
|
||||
// If not called manually, will auto-init on first achievement set.
|
||||
void achievements_init();
|
||||
void achievements_shutdown();
|
||||
|
||||
/// Set an achievement by its API name (if Steam available, otherwise ignored)
|
||||
/**
|
||||
* @param id The API name of the achievement to set.
|
||||
* @return 0 on success, -1 steam is not running (for diagnostic only)
|
||||
*/
|
||||
int8_t set_achievement(const char* id);
|
||||
|
||||
// Clear an achievement by its API name (if Steam available, otherwise ignored)
|
||||
/**
|
||||
* @param id The API name of the achievement to set.
|
||||
* @return 0 on success, -1 steam is not running (for diagnostic only)
|
||||
*/
|
||||
int8_t clear_achievement(const char* id);
|
||||
|
||||
/// returns whether steam is available
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
char is_steam_available();
|
||||
|
||||
char *get_steam_status();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -11,6 +11,15 @@
|
|||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <steam/steam_api.h>
|
||||
#include <stdbool.h>
|
||||
#include <thread>
|
||||
#include <mutex>.
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
#include <string_view>
|
||||
#include <stop_token>
|
||||
|
||||
void SDLContext::SDL_Deleter::operator ()(SDL_Window* window) {
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue