/*
 
 * src/system.c
 
 *
 
 * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
 
 *
 
 * The use and distribution terms for this software are contained in the file
 
 * named README, which can be found in the root of this distribution. By
 
 * using this software in any fashion, you are agreeing to be bound by the
 
 * terms of this license.
 
 *
 
 * You must not remove this notice, or any other, from this software.
 
 */
 
 
 
#include <SDL2/SDL.h>
 
 
 
#include <signal.h>
 
 
 
#include "system.h"
 
#if defined(_WIN32)
 
#include "windows.h"
 
char *sys_getbasepath (void)
 
{
 
   static char app_path[1024] = "";
 
   if (app_path[0] == 0)
 
   {
 
      GetModuleFileName (NULL, app_path, 1024);
 
      if (strrchr (app_path
, '\\') != NULL
)  
   }
 
   return (app_path);
 
}
 
#elif defined(__APPLE__)
 
#include <CoreFoundation/CoreFoundation.h>
 
#include <dlfcn.h>
 
void MessageBox (void *handle, char *msg, char *title, int buttons)
 
{
 
   const void *keys[2];
 
   const void *values[2];
 
   SInt32 result;
 
 
 
   if (title == NULL)
 
      title = "";
 
 
 
   keys[0] = kCFUserNotificationAlertHeaderKey;
 
   values[0] = CFStringCreateWithCString (kCFAllocatorDefault, title, kCFStringEncodingUTF8);;
 
 
 
   keys[1] = kCFUserNotificationAlertMessageKey;
 
   values[1] = CFStringCreateWithCString (kCFAllocatorDefault, msg, kCFStringEncodingUTF8);
 
 
 
   result = 0;
 
   CFUserNotificationCreate
 
   (
 
      kCFAllocatorDefault,
 
      0,
 
      kCFUserNotificationPlainAlertLevel,
 
      &result,
 
      CFDictionaryCreate
 
      (
 
         0,
 
         keys,
 
         values,
 
         sizeof (keys) / sizeof (*keys),
 
         &kCFTypeDictionaryKeyCallBacks,
 
         &kCFTypeDictionaryValueCallBacks
 
      )
 
   );
 
}
 
#define MB_OK 0
 
char *sys_getbasepath (void)
 
{
 
   static char app_path[1024] = "";
 
   if (app_path[0] == 0)
 
   {
 
      Dl_info addr_info;
 
      if (dladdr (sys_getbasepath, &addr_info) == 0)
 
         return (".");
 
      strcpy_s (app_path, 1024, addr_info.dli_fname);
 
      if (strrchr (app_path
, '/') != NULL
)  
      if (strrchr (app_path
, '/') != NULL
)  
      strcat_s (app_path, 1024, "/Resources");
 
   }
 
   return (app_path);
 
}
 
#else // !_WIN32 && !__APPLE__
 
#define MessageBox(handle,msg,title,btns) fprintf (stderr, msg)
 
#define MB_OK 0
 
char *sys_getbasepath (void)
 
{
 
   return (".");
 
}
 
#endif // _WIN32 || __APPLE__
 
 
 
 
 
/*
 
 * Panic
 
 */
 
void sys_panic (char *err, ...)
 
{
 
   va_list argptr;
 
   char s[1024];
 
 
 
   /* prepare message */
 
   vsprintf_s (s, sizeof (s), err, argptr);
 
 
 
   /* print message and die */
 
   MessageBox (NULL, s, NULL, MB_OK);
 
}
 
 
 
 
 
/*
 
 * Print a message
 
 */
 
void sys_printf (char *msg, ...)
 
{
 
   va_list argptr;
 
   char s[2048];
 
 
 
   /* prepare message */
 
   vsprintf_s (s, sizeof (s), msg, argptr);
 
 
 
   MessageBox (NULL, s, NULL, MB_OK);
 
}
 
 
 
 
 
/*
 
 * Return number of microseconds elapsed since first call
 
 */
 
U32 sys_gettime (void)
 
{
 
   static U32 ticks_base = 0;
 
   U32 ticks;
 
 
 
   ticks = SDL_GetTicks ();
 
 
 
   if (!ticks_base)
 
      ticks_base = ticks;
 
 
 
   return ticks - ticks_base;
 
}
 
 
 
 
 
/*
 
 * Sleep a number of microseconds
 
 */
 
void sys_sleep (int s)
 
{
 
   SDL_Delay (s);
 
}
 
 
 
 
 
/*
 
 * Initialize system
 
 */
 
void sys_init (int argc, char **argv)
 
{
 
   sysarg_init (argc, argv);
 
   sysvid_init ();
 
   sysjoy_init ();
 
 
 
    if (sysarg_args_nosound == 0)
 
      syssnd_init ();
 
 
 
}
 
 
 
 
 
/*
 
 * Shutdown system
 
 */
 
void sys_shutdown (void)
 
{
 
   syssnd_shutdown ();
 
   sysjoy_shutdown ();
 
   sysvid_shutdown ();
 
}