summaryrefslogtreecommitdiff
path: root/src/misc.cxx
blob: 3884ec7983d9272dfa78ff74acf9bb43cb0b2347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * This file is part of the planetblupi source code
 * Copyright (C) 1997, Daniel Roux & EPSITEC SA
 * Copyright (C) 2017, Mathieu Schroeter
 * http://epsitec.ch; http://www.blupi.org; http://github.com/blupi-games
 *
 * This program 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.
 *
 * This program 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 this program. If not, see http://gnu.org/licenses
 */

#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <direct.h>
#define mkdir(a, b) _mkdir (a)
#else /* _WIN32 */
#include <sys/stat.h>
#endif /*! _WIN32 */

#include <SDL2/SDL_log.h>
#include <SDL2/SDL_mouse.h>

#include "blupi.h"
#include "def.h"
#include "misc.h"

// Affiche un message de debug.

void
OutputDebug (const char * pMessage)
{
  SDL_LogDebug (SDL_LOG_CATEGORY_APPLICATION, "%s", pMessage);
}

// Conversion de la position de la souris.

Point
ConvLongToPos (LParam lParam)
{
  Point pos;

  pos.x = LOWORD (lParam); // horizontal position of cursor
  pos.y = HIWORD (lParam); // vertical position of cursor
  return pos;
}

static int g_seed;

/* Initialize the Microsoft pseudo-random generator */
void
InitRandom ()
{
  g_seed = 1;
  // srand (1);
}

/* We are not using rand from stdlib because on Linux the pseudo-generator
 * is using an other algorithm. Then the behaviour is not the same on all
 * platforms.
 * See http://stackoverflow.com/a/1280765/842097
 */
static int
ms_rand ()
{
  g_seed = g_seed * 0x343fd + 0x269EC3;
  return (g_seed >> 0x10) & 0x7FFF;
}

/* Returns a random number between two values (included). */
Sint32
Random (Sint32 min, Sint32 max)
{
  Sint32 n;

  n = ms_rand (); // replace rand ();
  n = min + (n % (max - min + 1));

  return (Sint32) n;
}

std::string
GetLocale ()
{
  return gettext ("en");
}

// Retourne le nom de dossier en cours.

std::string
GetBaseDir ()
{
  return GetShareDir () + "planetblupi/";
}

std::string
GetShareDir ()
{
  static std::string basePath;

  if (!basePath.size ())
  {
    auto sdlBasePath = SDL_GetBasePath ();

    sdlBasePath[strlen (sdlBasePath) - 1] = '\0';

    basePath = sdlBasePath;

    std::replace (basePath.begin (), basePath.end (), '\\', '/');
    basePath = basePath.substr (0, basePath.find_last_of ("//") + 1);
    SDL_free (sdlBasePath);
  }

  return basePath + "share/";
}

// Ajoute le chemin permettant de lire un fichier
// utilisateur.

void
AddUserPath (std::string & pFilename)
{
  const char * pText;
  size_t       pos;
  char         last;

  char *      temp = SDL_GetPrefPath ("Epsitec SA", "Planet Blupi");
  std::string path = temp;

  pText = strstr (pFilename.c_str (), "/");
  if (pText != nullptr)
  {
    pos = path.size () + (pText - pFilename.c_str ()) + 1;
    path += pFilename;
    last      = path[pos];
    path[pos] = 0;
    mkdir (path.c_str (), 0755);
    path[pos] = last;
  }
  else
    path += pFilename;

  pFilename = path;
  SDL_free (temp);
}

bool
FileExists (
  const std::string & filename, std::string & absolute, enum Location location)
{
  absolute = filename;
  FILE * file;

  switch (location)
  {
  case LOCATION_BASE:
    absolute = GetBaseDir () + filename;
    break;

  case LOCATION_USER:
    AddUserPath (absolute);
    break;

  default:
  case LOCATION_ABSOLUTE:
    break;
  }

  file = fopen (absolute.c_str (), "rb");
  if (file == nullptr)
    return false;

  fclose (file);
  return true;
}