summaryrefslogtreecommitdiff
path: root/src/pixmap.cxx
blob: d23181c5b2f8e257723aee4df6db51754b7d8f61 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
 * 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 <stdio.h>
#include <stdlib.h>
#include <string>

#ifdef _WIN32
#include <io.h>
#define access _access
#else /* _WIN32 */
#include <unistd.h>
#endif /* !_WIN32 */

#include <SDL2/SDL_image.h>

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

/////////////////////////////////////////////////////////////////////////////

// Constructeur.

CPixmap::CPixmap ()
{
  Sint32 i;

  m_bDebug   = true;
  m_bPalette = true;

  m_mouseSprite    = SPRITE_WAIT;
  m_bBackDisplayed = false;

  m_lpSDLBlupi = nullptr;

  for (i = 0; i < MAXCURSORS; i++)
    m_lpSDLCursors[i] = nullptr;

  m_lpCurrentCursor = nullptr;
}

// Destructeur.

CPixmap::~CPixmap ()
{
  unsigned int i;

  for (i = 0; i < countof (m_lpSDLCursors); i++)
  {
    if (m_lpSDLCursors[i])
    {
      SDL_FreeCursor (m_lpSDLCursors[i]);
      m_lpSDLCursors[i] = nullptr;
    }
  }

  for (auto tex : m_SDLTextureInfo)
  {
    if (tex.second.texture)
      SDL_DestroyTexture (tex.second.texture);
    if (tex.second.texMask)
      SDL_DestroyTexture (tex.second.texMask);
  }

  if (m_lpSDLBlupi)
    SDL_FreeSurface (m_lpSDLBlupi);
}

// Cr�e l'objet DirectDraw principal.
// Retourne false en cas d'erreur.

bool
CPixmap::Create (Point dim)
{
  m_dim = dim;

  m_clipRect.left   = 0;
  m_clipRect.top    = 0;
  m_clipRect.right  = dim.x;
  m_clipRect.bottom = dim.y;

  return true;
}

// Effectue un appel BltFast.
// Les modes sont 0=transparent, 1=opaque.

Sint32
CPixmap::BltFast (Sint32 chDst, size_t channel, Point dst, Rect rcRect)
{
  Sint32 res, limit;

  // Effectue un peu de clipping.
  if (dst.x < m_clipRect.left)
  {
    rcRect.left += m_clipRect.left - dst.x;
    dst.x = m_clipRect.left;
  }
  limit = (m_clipRect.right - dst.x) + rcRect.left;
  if (rcRect.right > limit)
    rcRect.right = limit;
  if (dst.y < m_clipRect.top)
  {
    rcRect.top += m_clipRect.top - dst.y;
    dst.y = m_clipRect.top;
  }
  limit = (m_clipRect.bottom - dst.y) + rcRect.top;
  if (rcRect.bottom > limit)
    rcRect.bottom = limit;

  if (rcRect.left >= rcRect.right || rcRect.top >= rcRect.bottom)
    return 0;

  if (chDst < 0)
  {
    SDL_Rect srcRect, dstRect;
    srcRect.x = rcRect.left;
    srcRect.y = rcRect.top;
    srcRect.w = rcRect.right - rcRect.left;
    srcRect.h = rcRect.bottom - rcRect.top;
    dstRect   = srcRect;
    dstRect.x = dst.x;
    dstRect.y = dst.y;

    res = SDL_RenderCopy (
      g_renderer, m_SDLTextureInfo[channel].texture, &srcRect, &dstRect);
  }
  else
  {
    SDL_Rect srcRect, dstRect;
    srcRect.x = rcRect.left;
    srcRect.y = rcRect.top;
    srcRect.w = rcRect.right - rcRect.left;
    srcRect.h = rcRect.bottom - rcRect.top;
    dstRect   = srcRect;
    dstRect.x = dst.x;
    dstRect.y = dst.y;

    SDL_SetRenderTarget (g_renderer, m_SDLTextureInfo[chDst].texture);
    res = SDL_RenderCopy (
      g_renderer, m_SDLTextureInfo[channel].texture, &srcRect, &dstRect);
    SDL_SetRenderTarget (g_renderer, nullptr);
  }

  return res;
}

// Effectue un appel BltFast.
// Les modes sont 0=transparent, 1=opaque.

Sint32
CPixmap::BltFast (
  SDL_Texture * lpSDL, size_t channel, Point dst, Rect rcRect,
  SDL_BlendMode mode)
{
  Sint32 res;

  SDL_Rect srcRect, dstRect;
  srcRect.x = rcRect.left;
  srcRect.y = rcRect.top;
  srcRect.w = rcRect.right - rcRect.left;
  srcRect.h = rcRect.bottom - rcRect.top;
  dstRect   = srcRect;
  dstRect.x = dst.x;
  dstRect.y = dst.y;

  SDL_BlendMode oMode;
  SDL_GetTextureBlendMode (m_SDLTextureInfo[channel].texture, &oMode);
  if (oMode != mode)
    SDL_SetTextureBlendMode (m_SDLTextureInfo[channel].texture, mode);

  SDL_SetRenderTarget (g_renderer, lpSDL);
  res = SDL_RenderCopy (
    g_renderer, m_SDLTextureInfo[channel].texture, &srcRect, &dstRect);
  SDL_SetRenderTarget (g_renderer, nullptr);

  if (oMode != mode)
    SDL_SetTextureBlendMode (m_SDLTextureInfo[channel].texture, oMode);

  return res;
}

/**
 * \brief Reload textures created with access target flag.
 *
 * \returns true on success.
 */
bool
CPixmap::ReloadTargetTextures ()
{
  for (auto & tex : m_SDLTextureInfo)
  {
    if (!tex.second.target)
      continue;

    if (!Cache (
          tex.first, tex.second.file, tex.second.dimTotal, tex.second.dimIcon))
      return false;
  }

  return true;
}

// Cache une image contenant des ic�nes.

bool
CPixmap::Cache (
  size_t channel, const std::string & pFilename, Point totalDim, Point iconDim)
{
  std::string   file       = GetBaseDir () + pFilename;
  SDL_Surface * surface    = IMG_Load (file.c_str ());
  bool          blupiChSet = false;

  if (channel == CHBLUPI && !m_lpSDLBlupi)
  {
    m_lpSDLBlupi = surface;
    blupiChSet   = true;
  }

  SDL_Texture * texture = SDL_CreateTextureFromSurface (g_renderer, surface);
  Uint32        format;
  Sint32        access, w, h;
  SDL_QueryTexture (texture, &format, &access, &w, &h);

  if (m_SDLTextureInfo.find (channel) == m_SDLTextureInfo.end ())
  {
    m_SDLTextureInfo[channel].texture = SDL_CreateTexture (
      g_renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, w, h);

    if (!m_SDLTextureInfo[channel].texture)
    {
      SDL_LogError (
        SDL_LOG_CATEGORY_APPLICATION,
        "Couldn't create texture from surface: %s", SDL_GetError ());
      return false;
    }

    SDL_SetTextureBlendMode (
      m_SDLTextureInfo[channel].texture, SDL_BLENDMODE_BLEND);
  }

  SDL_SetRenderTarget (g_renderer, m_SDLTextureInfo[channel].texture);
  SDL_SetRenderDrawColor (g_renderer, 0, 0, 0, 0);
  SDL_RenderClear (g_renderer);
  SDL_SetRenderTarget (g_renderer, nullptr);

  m_SDLTextureInfo[channel].texMask  = channel == CHMASK2 ? texture : nullptr;
  m_SDLTextureInfo[channel].target   = true;
  m_SDLTextureInfo[channel].dimIcon  = iconDim;
  m_SDLTextureInfo[channel].dimTotal = totalDim;
  m_SDLTextureInfo[channel].file     = pFilename;

  SDL_SetRenderTarget (g_renderer, m_SDLTextureInfo[channel].texture);
  SDL_RenderCopy (g_renderer, texture, nullptr, nullptr);
  SDL_SetRenderTarget (g_renderer, nullptr);

  if (!m_SDLTextureInfo[channel].texMask)
    SDL_DestroyTexture (texture);

  if (!blupiChSet)
    SDL_FreeSurface (surface);

  return true;
}

// Cache une image globale.

bool
CPixmap::Cache (size_t channel, const std::string & pFilename, Point totalDim)
{
  Point iconDim;

  iconDim.x = 0;
  iconDim.y = 0;

  return Cache (channel, pFilename, totalDim, iconDim);
}

// Cache une image provenant d'un bitmap.

bool
CPixmap::Cache (size_t channel, SDL_Surface * surface, Point totalDim)
{
  // Create the offscreen surface, by loading our bitmap.
  if (
    m_SDLTextureInfo.find (channel) != m_SDLTextureInfo.end () &&
    m_SDLTextureInfo[channel].texture)
    SDL_DestroyTexture (m_SDLTextureInfo[channel].texture);

  m_SDLTextureInfo[channel].texture =
    SDL_CreateTextureFromSurface (g_renderer, surface);
  m_SDLTextureInfo[channel].target = false;

  if (!m_SDLTextureInfo[channel].texture)
    return false;

  m_SDLTextureInfo[channel].dimTotal = totalDim;
  m_SDLTextureInfo[channel].dimIcon  = totalDim;
  return true;
}

// Modifie la r�gion de clipping.

void
CPixmap::SetClipping (Rect clip)
{
  m_clipRect = clip;
}

// Retourne la r�gion de clipping.

Rect
CPixmap::GetClipping ()
{
  return m_clipRect;
}

// Teste si un point fait partie d'une ic�ne.

bool
CPixmap::IsIconPixel (size_t channel, Sint32 rank, Point pos)
{
  Sint32 nbx, nby;

  auto texInfo = m_SDLTextureInfo.find (channel);
  if (texInfo == m_SDLTextureInfo.end ())
    return false;

  if (texInfo->second.dimIcon.x == 0 || texInfo->second.dimIcon.y == 0)
    return false;

  nbx = texInfo->second.dimTotal.x / texInfo->second.dimIcon.x;
  nby = texInfo->second.dimTotal.y / texInfo->second.dimIcon.y;

  if (rank < 0 || rank >= nbx * nby)
    return false;

  pos.x += (rank % nbx) * texInfo->second.dimIcon.x;
  pos.y += (rank / nbx) * texInfo->second.dimIcon.y;

  SDL_Rect rect;
  rect.x       = pos.x;
  rect.y       = pos.y;
  rect.w       = 1;
  rect.h       = 1;
  Uint32 pixel = 0;

  SDL_SetRenderTarget (g_renderer, m_SDLTextureInfo[channel].texture);
  SDL_RenderReadPixels (g_renderer, &rect, 0, &pixel, 4);
  SDL_SetRenderTarget (g_renderer, nullptr);

  return !!pixel;
}

// Dessine une partie d'image rectangulaire.
// Les modes sont 0=transparent, 1=opaque.

bool
CPixmap::DrawIcon (Sint32 chDst, size_t channel, Sint32 rank, Point pos)
{
  Sint32 nbx, nby;
  Rect   rect;

  auto texInfo = m_SDLTextureInfo.find (channel);
  if (channel != CHMAP && texInfo == m_SDLTextureInfo.end ())
    return false;

  if (texInfo->second.dimIcon.x == 0 || texInfo->second.dimIcon.y == 0)
    return false;

  nbx = texInfo->second.dimTotal.x / texInfo->second.dimIcon.x;
  nby = texInfo->second.dimTotal.y / texInfo->second.dimIcon.y;

  if (rank < 0 || rank >= nbx * nby)
    return false;

  rect.left   = (rank % nbx) * texInfo->second.dimIcon.x;
  rect.top    = (rank / nbx) * texInfo->second.dimIcon.y;
  rect.right  = rect.left + texInfo->second.dimIcon.x;
  rect.bottom = rect.top + texInfo->second.dimIcon.y;

  return !BltFast (chDst, channel, pos, rect);
}

// Dessine une partie d'image rectangulaire.
// Les modes sont 0=transparent, 1=opaque.
//
// Correspondances in,out :
//   0,0     2,1    ...
//   1,16    3,17
//
//  32,32   34,33
//  33,48   35,49

bool
CPixmap::DrawIconDemi (Sint32 chDst, size_t channel, Sint32 rank, Point pos)
{
  Sint32 nbx, nby;
  Rect   rect;

  auto texInfo = m_SDLTextureInfo.find (channel);
  if (texInfo == m_SDLTextureInfo.end ())
    return false;

  if (texInfo->second.dimIcon.x == 0 || texInfo->second.dimIcon.y == 0)
    return false;

  nbx = texInfo->second.dimTotal.x / texInfo->second.dimIcon.x;
  nby = texInfo->second.dimTotal.y / (texInfo->second.dimIcon.y / 2);

  rank = (rank / 32) * 32 + ((rank % 32) / 2) + ((rank % 2) * 16);

  if (rank < 0 || rank >= nbx * nby)
    return false;

  rect.left   = (rank % nbx) * texInfo->second.dimIcon.x;
  rect.top    = (rank / nbx) * (texInfo->second.dimIcon.y / 2);
  rect.right  = rect.left + texInfo->second.dimIcon.x;
  rect.bottom = rect.top + (texInfo->second.dimIcon.y / 2);

  return !BltFast (chDst, channel, pos, rect);
}

// Dessine une partie d'image rectangulaire.

bool
CPixmap::DrawIconPart (
  Sint32 chDst, size_t channel, Sint32 rank, Point pos, Sint32 startY,
  Sint32 endY)
{
  Sint32 nbx, nby;
  Rect   rect;

  auto texInfo = m_SDLTextureInfo.find (channel);
  if (texInfo == m_SDLTextureInfo.end ())
    return false;

  if (texInfo->second.dimIcon.x == 0 || texInfo->second.dimIcon.y == 0)
    return false;

  nbx = texInfo->second.dimTotal.x / texInfo->second.dimIcon.x;
  nby = texInfo->second.dimTotal.y / texInfo->second.dimIcon.y;

  if (rank < 0 || rank >= nbx * nby)
    return false;

  rect.left   = (rank % nbx) * texInfo->second.dimIcon.x;
  rect.top    = (rank / nbx) * texInfo->second.dimIcon.y;
  rect.right  = rect.left + texInfo->second.dimIcon.x;
  rect.bottom = rect.top + endY;

  pos.y += startY;
  rect.top += startY;

  return !BltFast (chDst, channel, pos, rect);
}

// Dessine une partie d'image n'importe o�.

bool
CPixmap::DrawPart (Sint32 chDst, size_t channel, Point dest, Rect rect)
{
  if (m_SDLTextureInfo.find (channel) == m_SDLTextureInfo.end ())
    return false;

  return !BltFast (chDst, channel, dest, rect);
}

// Dessine une partie d'image rectangulaire.

bool
CPixmap::DrawImage (Sint32 chDst, size_t channel, Rect rect)
{
  Point  dst;
  Sint32 res;

  if (m_SDLTextureInfo.find (channel) == m_SDLTextureInfo.end ())
    return false;

  dst.x = rect.left;
  dst.y = rect.top;

  res = BltFast (chDst, channel, dst, rect);

  if (res)
    return false;

  if (channel == CHBACK)
    m_bBackDisplayed = false;

  return true;
}

// Construit une ic�ne en utilisant un masque.

bool
CPixmap::BuildIconMask (
  size_t channelMask, Sint32 rankMask, size_t channel, Sint32 rankSrc,
  Sint32 rankDst)
{
  Sint32 nbx, nby;
  Point  posDst, posDstMask;
  Rect   rect, rectMask;
  Sint32 res;

  auto texInfo = m_SDLTextureInfo.find (channel);
  if (texInfo == m_SDLTextureInfo.end ())
    return false;

  if (texInfo->second.dimIcon.x == 0 || texInfo->second.dimIcon.y == 0)
    return false;

  nbx = texInfo->second.dimTotal.x / texInfo->second.dimIcon.x;
  nby = texInfo->second.dimTotal.y / texInfo->second.dimIcon.y;

  if (rankSrc < 0 || rankSrc >= nbx * nby)
    return false;
  if (rankDst < 0 || rankDst >= nbx * nby)
    return false;

  rect.left   = (rankSrc % nbx) * texInfo->second.dimIcon.x;
  rect.top    = (rankSrc / nbx) * texInfo->second.dimIcon.y;
  rect.right  = rect.left + texInfo->second.dimIcon.x;
  rect.bottom = rect.top + texInfo->second.dimIcon.y;
  posDst.x    = (rankDst % nbx) * texInfo->second.dimIcon.x;
  posDst.y    = (rankDst / nbx) * texInfo->second.dimIcon.y;

  res = BltFast (m_SDLTextureInfo[channel].texture, channel, posDst, rect);
  if (res)
    return false;

  auto texMaskInfo = m_SDLTextureInfo.find (channelMask);

  if (texMaskInfo->second.dimIcon.x == 0 || texMaskInfo->second.dimIcon.y == 0)
    return false;

  nbx = texMaskInfo->second.dimTotal.x / texMaskInfo->second.dimIcon.x;
  nby = texMaskInfo->second.dimTotal.y / texMaskInfo->second.dimIcon.y;

  /* Support only CHMASK1 (white) because it needs CHMASK2 (black) (hardcoded
   * here) */
  if (rankMask < 0 || rankMask >= nbx * nby || channelMask != CHMASK1)
    return false;

  rectMask.left   = (rankMask % nbx) * texMaskInfo->second.dimIcon.x;
  rectMask.top    = (rankMask / nbx) * texMaskInfo->second.dimIcon.y;
  rectMask.right  = rectMask.left + texMaskInfo->second.dimIcon.x;
  rectMask.bottom = rectMask.top + texMaskInfo->second.dimIcon.y;

  /* Multiply the white mask with the texture (no alpha) */
  res = BltFast (
    m_SDLTextureInfo[channel].texture, channelMask, posDst, rectMask,
    SDL_BLENDMODE_MOD);

  posDstMask.x = (rankMask % nbx) * texMaskInfo->second.dimIcon.x;
  posDstMask.y = (rankMask / nbx) * texMaskInfo->second.dimIcon.y;
  /* Addition the previous texture with the black mask (alpha retrieved) */
  res = BltFast (
    m_SDLTextureInfo[CHMASK2].texture, channel, posDstMask, rect,
    SDL_BLENDMODE_ADD);

  /* Blit the altered mask in the final texture */
  res = BltFast (
    m_SDLTextureInfo[channel].texture, CHMASK2, posDst, rectMask,
    SDL_BLENDMODE_NONE);

  /* Restore the black mask for the next iteration. */
  SDL_SetRenderTarget (g_renderer, m_SDLTextureInfo[CHMASK2].texture);
  SDL_RenderCopy (
    g_renderer, m_SDLTextureInfo[CHMASK2].texMask, nullptr, nullptr);
  SDL_SetRenderTarget (g_renderer, nullptr);

  return !res;
}

// Affiche le pixmap � l'�cran.
// Retourne false en cas d'erreur.

bool
CPixmap::Display ()
{
  m_bBackDisplayed = true;
  SDL_RenderPresent (g_renderer);
  return true;
}

// Change le lutin de la souris.

void
CPixmap::SetMouseSprite (MouseSprites sprite)
{
  if (m_mouseSprite == sprite)
    return;

  m_mouseSprite = sprite;

  SDL_SetCursor (m_lpSDLCursors[sprite - 1]);
}

// Montre ou cache la souris.

void
CPixmap::MouseShow (bool bShow)
{
  SDL_ShowCursor (bShow);
}

// Retourne le rectangle correspondant au sprite
// de la souris dans CHBLUPI.

Rect
CPixmap::MouseRectSprite ()
{
  Sint32 rank, nbx;
  Rect   rcRect;

  rank = 348;
  if (m_mouseSprite == SPRITE_ARROW)
    rank = 348;
  if (m_mouseSprite == SPRITE_POINTER)
    rank = 349;
  if (m_mouseSprite == SPRITE_MAP)
    rank = 350;
  if (m_mouseSprite == SPRITE_WAIT)
    rank = 351;
  if (m_mouseSprite == SPRITE_FILL)
    rank = 352;
  if (m_mouseSprite == SPRITE_ARROWL)
    rank = 353;
  if (m_mouseSprite == SPRITE_ARROWR)
    rank = 354;
  if (m_mouseSprite == SPRITE_ARROWU)
    rank = 355;
  if (m_mouseSprite == SPRITE_ARROWD)
    rank = 356;
  if (m_mouseSprite == SPRITE_ARROWDL)
    rank = 357;
  if (m_mouseSprite == SPRITE_ARROWDR)
    rank = 358;
  if (m_mouseSprite == SPRITE_ARROWUL)
    rank = 359;
  if (m_mouseSprite == SPRITE_ARROWUR)
    rank = 360;

  auto texMaskInfo = m_SDLTextureInfo.find (CHBLUPI);

  nbx = texMaskInfo->second.dimTotal.x / texMaskInfo->second.dimIcon.x;

  rcRect.left   = (rank % nbx) * texMaskInfo->second.dimIcon.x;
  rcRect.top    = (rank / nbx) * texMaskInfo->second.dimIcon.y;
  rcRect.right  = rcRect.left + texMaskInfo->second.dimIcon.x;
  rcRect.bottom = rcRect.top + texMaskInfo->second.dimIcon.y;

  return rcRect;
}

SDL_Point
CPixmap::GetCursorHotSpot (MouseSprites sprite)
{
  static const Sint32 hotspots[MAXCURSORS * 2] = {
    30, 30, // SPRITE_ARROW
    20, 15, // SPRITE_POINTER
    31, 26, // SPRITE_MAP
    25, 14, // SPRITE_ARROWU
    24, 35, // SPRITE_ARROWD
    15, 24, // SPRITE_ARROWL
    35, 24, // SPRITE_ARROWR
    18, 16, // SPRITE_ARROWUL
    32, 18, // SPRITE_ARROWUR
    17, 30, // SPRITE_ARROWDL
    32, 32, // SPRITE_ARROWDR
    30, 30, // SPRITE_WAIT
    30, 30, // SPRITE_EMPTY
    21, 51, // SPRITE_FILL
  };

  SDL_Point hotspot = {0, 0};

  if (sprite >= SPRITE_BEGIN && sprite <= SPRITE_END)
  {
    const Sint32 rank = sprite - SPRITE_BEGIN; // rank <- 0..n

    hotspot.x = hotspots[rank * 2 + 0];
    hotspot.y = hotspots[rank * 2 + 1];
  }

  return hotspot;
}

SDL_Rect
CPixmap::GetCursorRect (MouseSprites sprite)
{
  Sint32   rank;
  SDL_Rect rcRect;

  switch (sprite)
  {
  default:
  case SPRITE_ARROW:
    rank = 348;
    break;
  case SPRITE_POINTER:
    rank = 349;
    break;
  case SPRITE_MAP:
    rank = 350;
    break;
  case SPRITE_WAIT:
    rank = 351;
    break;
  case SPRITE_FILL:
    rank = 352;
    break;
  case SPRITE_ARROWL:
    rank = 353;
    break;
  case SPRITE_ARROWR:
    rank = 354;
    break;
  case SPRITE_ARROWU:
    rank = 355;
    break;
  case SPRITE_ARROWD:
    rank = 356;
    break;
  case SPRITE_ARROWDL:
    rank = 357;
    break;
  case SPRITE_ARROWDR:
    rank = 358;
    break;
  case SPRITE_ARROWUL:
    rank = 359;
    break;
  case SPRITE_ARROWUR:
    rank = 360;
    break;
  }

  auto texMaskInfo = m_SDLTextureInfo.find (CHBLUPI);

  Sint32 nbx = texMaskInfo->second.dimTotal.x / texMaskInfo->second.dimIcon.x;

  rcRect.x = (rank % nbx) * texMaskInfo->second.dimIcon.x;
  rcRect.y = (rank / nbx) * texMaskInfo->second.dimIcon.y;
  rcRect.w = texMaskInfo->second.dimIcon.x;
  rcRect.h = texMaskInfo->second.dimIcon.y;

  return rcRect;
}

void
CPixmap::LoadCursors (Uint8 scale)
{
  Uint32 rmask, gmask, bmask, amask;

/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  rmask = 0xff000000;
  gmask = 0x00ff0000;
  bmask = 0x0000ff00;
  amask = 0x000000ff;
#else
  rmask = 0x000000ff;
  gmask = 0x0000ff00;
  bmask = 0x00ff0000;
  amask = 0xff000000;
#endif

  for (int i = SPRITE_BEGIN; i <= SPRITE_END; ++i)
  {
    MouseSprites sprite = static_cast<MouseSprites> (i);

    if (m_lpSDLCursors[sprite - 1])
      SDL_FreeCursor (m_lpSDLCursors[sprite - 1]);

    SDL_Point hotspot = this->GetCursorHotSpot (sprite);
    SDL_Rect  rect    = this->GetCursorRect (sprite);

    SDL_Surface * surface = SDL_CreateRGBSurface (
      0, rect.w * scale, rect.h * scale, 32, rmask, gmask, bmask, amask);
    SDL_BlitScaled (m_lpSDLBlupi, &rect, surface, nullptr);

    // FIXME: change cursor first value to 0
    m_lpSDLCursors[sprite - 1] =
      SDL_CreateColorCursor (surface, hotspot.x * scale, hotspot.y * scale);
    SDL_FreeSurface (surface);
  }
}

void
CPixmap::ChangeSprite (MouseSprites sprite)
{
  if (m_lpCurrentCursor == m_lpSDLCursors[sprite - 1])
    return;

  SDL_SetCursor (m_lpSDLCursors[sprite - 1]);
  m_lpCurrentCursor = m_lpSDLCursors[sprite - 1];
}