Simple Steamworks API usage

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Landeel
Posts: 704
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Simple Steamworks API usage

Postby Landeel » Jan 06, 2020 19:14

This is a code fragment from my game 'Iceroyds! '.
Only basic Steamworks usage and achievements are covered.
Works both on Linux and Windows.
IMPORTANT: This code will only work with the Steamworks libraries version 1.35a

Code: Select all

#define steam_build
#define steam_appid 999999 'your steam appid here
#define steam_protection
#define steam_restart

#define printconsole print
#define printdebug print


#ifdef steam_build

   dim shared as any ptr libsteam_api
   dim shared as integer steam_failed=0
   dim shared SteamAPI_Init as function cdecl() as boolean 'S_API bool S_CALLTYPE SteamAPI_Init();
   dim shared SteamAPI_IsSteamRunning as function cdecl() as boolean
   dim shared SteamAPI_RestartAppIfNecessary as function cdecl(byval unOwnAppID as uinteger<32>) as boolean 'S_API bool S_CALLTYPE SteamAPI_RestartAppIfNecessary( uint32 unOwnAppID );
   dim shared SteamAPI_Shutdown as sub cdecl() 'S_API void S_CALLTYPE SteamAPI_Shutdown();
   
   type steam_stats_type as any ptr
   dim shared steam_stats as steam_stats_type
   
   dim shared SteamUserStats as function cdecl() as steam_stats_type    '    SteamUserStats = steam.SteamUserStats   '    SteamUserStats.restype = ctypes.c_void_p

   dim shared SteamAPI_ISteamClient_GetISteamUserStats  as function cdecl() as steam_stats_type    '    SteamUserStats = steam.SteamUserStats   '    SteamUserStats.restype = ctypes.c_void_p
   
   dim shared SteamAPI_ISteamUserStats_RequestCurrentStats as function cdecl() as boolean 'bool RequestCurrentStats();

   dim shared SteamAPI_ISteamUserStats_SetAchievement as function cdecl(stats as steam_stats_type, pchName as zstring ptr) as boolean 'bool SetAchievement( const char *pchName );   'SteamUserStats_SetAchievement = steam.SteamAPI_ISteamUserStats_SetAchievement ' 'SteamUserStats_SetAchievement.argtypes = [ ctypes.c_void_p, ctypes.c_char_p ]
   dim shared SteamAPI_ISteamUserStats_ResetAllStats as function cdecl(stats as steam_stats_type) as boolean
   dim shared SteamAPI_ISteamUserStats_StoreStats as function cdecl(stats as steam_stats_type) as boolean 'bool SetAchievement( const char *pchName );   'SteamUserStats_SetAchievement = steam.SteamAPI_ISteamUserStats_SetAchievement ' 'SteamUserStats_SetAchievement.argtypes = [ ctypes.c_void_p, ctypes.c_char_p ] 'SteamUserStats_StoreStats = steam.SteamAPI_ISteamUserStats_StoreStats  'SteamUserStats_StoreStats.argtypes = [ ctypes.c_void_p ]
   
   dim shared SteamAPI_RunCallbacks as sub cdecl() 'S_API void S_CALLTYPE SteamAPI_RunCallbacks();
   dim shared SteamAPI_ISteamUtils_GetAppID as function cdecl() as uinteger<32>  'SteamAPI_ISteamUtils_GetAppID 'uint32 GetAppID();
      
   sub steam_ResetAllStats()
      if steam_stats then
         SteamAPI_ISteamUserStats_ResetAllStats(steam_stats)
         SteamAPI_ISteamUserStats_StoreStats(steam_stats)
      else
         printdebug "STEAM : could not reset achievements"
         steam_failed=1
      end if
   end sub
      
#endif



'init steam
#ifdef steam_build
   
   #ifdef steam_protection
      if dir(exepath()+"/steam_appid.txt")<>"" then
         'if steam_appid.txt exists
         steam_failed=1
      end if
   #endif

   #ifdef __FB_WIN32__
      libsteam_api=dylibload(exepath+"/dll/steam_api.dll") 'location to dll
   #else
      #ifdef __FB_64BIT__
         libsteam_api=dylibload(exepath+"/lib64/libsteam_api.so") 'location to 64-bit linux lib
      #else
         libsteam_api=dylibload(exepath+"/lib32/libsteam_api.so") 'location to 32-bit linux lib
      #endif
   #endif
   if libsteam_api then
      printdebug "STEAM : libsteam_api loaded :",libsteam_api
   end if
   SteamAPI_Init=DyLibSymbol(libsteam_api,"SteamAPI_Init")
   SteamAPI_IsSteamRunning=DyLibSymbol(libsteam_api,"SteamAPI_IsSteamRunning")
   SteamAPI_RestartAppIfNecessary=DyLibSymbol(libsteam_api,"SteamAPI_RestartAppIfNecessary")
   SteamAPI_Shutdown=DyLibSymbol(libsteam_api,"SteamAPI_Shutdown")
   SteamAPI_ISteamUserStats_RequestCurrentStats=DyLibSymbol(libsteam_api,"SteamAPI_ISteamUserStats_RequestCurrentStats")
   SteamAPI_ISteamUserStats_SetAchievement=DyLibSymbol(libsteam_api,"SteamAPI_ISteamUserStats_SetAchievement")
   SteamAPI_ISteamUserStats_StoreStats=DyLibSymbol(libsteam_api,"SteamAPI_ISteamUserStats_StoreStats")
   SteamAPI_ISteamUserStats_ResetAllStats=DyLibSymbol(libsteam_api,"SteamAPI_ISteamUserStats_ResetAllStats")
   SteamAPI_RunCallbacks=DyLibSymbol(libsteam_api,"SteamAPI_RunCallbacks")
   SteamAPI_ISteamUtils_GetAppID=DyLibSymbol(libsteam_api,"SteamAPI_ISteamUtils_GetAppID")
   SteamUserStats=DyLibSymbol(libsteam_api,"SteamUserStats") 'Requires steam_api version 1.35a
   
   printdebug "STEAM : funcs=", SteamAPI_Init, SteamAPI_RestartAppIfNecessary, SteamAPI_Shutdown, SteamAPI_ISteamUserStats_RequestCurrentStats, SteamAPI_ISteamUserStats_SetAchievement
   
   #ifdef steam_restart
      if SteamAPI_RestartAppIfNecessary(steam_appid) then
         'restart game with steam
         printdebug "STEAM : SteamAPI_RestartAppIfNecessary(steam_appid)=true"
         end
      end if
   #endif
   
   if SteamAPI_Init() then
      printdebug "STEAM : Init ok."
   else
      'SteamAPI_Init() failed
      printdebug "STEAM : SteamAPI_Init()=false"
      steam_failed=1
   end if

   if SteamAPI_IsSteamRunning() then
      printdebug "STEAM : running."
   else
      'Steam is not running
      printdebug "STEAM : NOT running."
      steam_failed=1
   end if

   if SteamUserStats=0 then
      printdebug "STEAM : SteamUserStats is missing. Achievements will not work."
      steam_failed=1
   end if

   if SteamAPI_ISteamUserStats_SetAchievement=0 then
      printdebug "STEAM : SteamAPI_ISteamUserStats_SetAchievement is missing. Achievements will not work."
      steam_failed=1
   end if

   if SteamAPI_ISteamUserStats_StoreStats=0 then
      printdebug "STEAM : SteamAPI_ISteamUserStats_StoreStats is missing."
      steam_failed=1
   end if

   steam_stats=SteamUserStats()
   
   if steam_stats then
      SteamAPI_ISteamUserStats_StoreStats(steam_stats)
   else
      printdebug "STEAM : could not retrieve SteamUserStats"
      steam_failed=1
   end if

#endif


sub steam_SetAchievement(byval achname as string="TEST")
      printconsole "ACHIEVEMENT : "+achname
      #ifdef steam_build
         if steam_stats then
            SteamAPI_ISteamUserStats_SetAchievement(steam_stats, achname)
            SteamAPI_ISteamUserStats_StoreStats(steam_stats)
         else
            printdebug "STEAM : could not set achievement"
            steam_failed=1
         end if
      #endif
end sub



'close steam api
#ifdef steam_build
   SteamAPI_Shutdown()
   dylibfree libsteam_api : libsteam_api=0
#endif

badidea
Posts: 1893
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Simple Steamworks API usage

Postby badidea » Jan 08, 2020 21:41

Thanks for sharing. Only one 'small' thing to do for me now, is getting a game finished enough to be worth a steam release. I estimate 2030 :-)
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Simple Steamworks API usage

Postby Boromir » Jan 10, 2020 12:12

Thank you! This is pretty awesome. I've been struggling with steam achievements for a long time.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: Google [Bot] and 1 guest