summaryrefslogtreecommitdiffstats
path: root/kmahjongg/Tileset.cpp
blob: bec2d959fea05410a15d8c810478124fb85e5cd3 (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

#include <stdlib.h>
#include "Tileset.h"
#include <tqimage.h>


#define mini_width 20
#define mini_height 28
static unsigned char mini_bits[] = {
  0xfc, 0xff, 0x0f, 0xfe, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f,
  0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f,
  0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f,
  0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f,
  0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f,
  0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f,
  0xff, 0xff, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x07, 0xff, 0xff, 0x03,
  };

#define mask_width 40
#define mask_height 56
static unsigned char mask_bits[] = {
  0xf0, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xff,
  0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0x7f, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff,
  0xff, 0xff, 0xff, 0x0f, };


// ---------------------------------------------------------

Tileset::Tileset(bool scale):
	maskBits(mask_width, mask_height, mask_bits, true),
	maskBitsMini(mini_width, mini_height, mini_bits, true)
{
	isScaled = scale;
	divisor =  (isScaled) ? 2 : 1;

	// set up tile metrics (fixed for now)
	ss     = 4;    // left/bottom shadow width
	bs     = 1;    // tile boarder width
	w      = 40;   // tile width (inc boarder & shadow)
	h      = 56;   // tile height (inc boarder and shadow)
	s      = w*h;  // RGBA's required per tile

	// Allocate memory for the 9*5 tile arrays
    	tiles         = new TQRgb [9*5*s];
    	selectedTiles = new TQRgb [9*5*s];

    	// allocate memory for single tile storage
    	selectedFace   = new TQRgb [s];
    	unselectedFace = new TQRgb [s];

	// quarter widths are used as an offset when
	// overlaying tiles in 3 dimensions.
	qw  = ((w-ss)/2) ;
	qh = ((h-ss)/2) ;

	filename = "";
}


// ---------------------------------------------------------

Tileset::~Tileset() {

	// deallocate all memory
    	delete [] tiles;
    	delete [] selectedTiles;
    	delete [] selectedFace;
    	delete [] unselectedFace;
}

// ---------------------------------------------------------
// copy a tile from a qimage into a linear array of bytes. This
// method returns the address of the byte after the copied image
// and can be used to fill a larger array of tiles.

TQRgb *Tileset::copyTileImage(short tileX, short tileY, TQRgb *to, TQImage &from) {
    TQRgb *dest = to;
    TQRgb *src;

    src = (TQRgb *) from.scanLine(tileY * h)
             +(tileX * w);
    for (short pos=0; pos < h; pos++) {
	memcpy(dest, src, w*sizeof(TQRgb));
	dest+=w;
	src += from.width();
    }
    return(dest);
}

// ----------------------------------------------------------
// Create a tile. Take a specified tile background a tile face
// (specified as an x,y coord) and a destination buffer (location
// in which is calculated from the x,y) and copy in the
// tile background, overlaying the foreground with transparency
// (the foregrounds top/left pixel is taken as the transparent
// color).


TQRgb *Tileset::createTile(short x, short y,
		TQRgb *det, TQImage &allTiles , TQRgb *face) {
  TQRgb *image ;
  TQRgb *to = det;

  // Alloc the space
  image = new TQRgb[s];

  // copy in the background
  memcpy(to, face, s*sizeof(TQRgb));

  // get the tile gylph
  copyTileImage(x, y , image, allTiles);

  // copy the image over the background using the
  // top left colour as the transparency. We step over
  // the shadow and the boarder

  TQRgb* src =  image+                        // image
    ss+             // past the left shadow
    bs+             // then the tile border
    (bs  * w);      // then step past the top border


  to += (((ss+bs))+(bs*w));


  TQRgb trans = *src;

  // start after the top border rows and go through all rows
  for( short YP=0; YP < h-ss - (2*bs); YP++) {
    // start after the shadow and border and iterate over x
    for (short xpos=0; xpos < w-ss -(2*bs) ; xpos++) {
      // skip transparent pixels
      if (*src != trans)
	*to = *src;
      src++;
      to++;
    }
    // step over the border to get to the next row
    src  += ss + (2 * bs);
    to += ss + (2 * bs);
    }

  // Free allocated space
  delete [] image;

  // calculate the address of the next tile
  return(det+s);
}

// --------------------------------------------------------
// create a pixmap for a tile. Optionally create a scalled
// version, which can be used for mini tile requirements.
// this gives us a small tile for previews and showing
// removed tiles.
void  Tileset::createPixmap(TQRgb *src, TQPixmap &dest, bool scale, bool shadow)
{

    TQImage buff;
    TQRgb   *line;

    buff.create(w, h, 32);

    for (int y=0; y<h; y++) {
	line = (TQRgb *) buff.scanLine(y);
	memcpy( line, src, w*sizeof(TQRgb));

	if (shadow) {
	  for (int spos=0; spos <w; spos++) {
	    line[spos] = TQColor(line[spos]).dark(133).rgb();
	  }
	}

	src += w;
    }


    // create the pixmap and initialise the drawing mask
    if (!scale) {
    	dest.convertFromImage(buff);
    	dest.setMask(maskBits);
    } else {
    	dest.convertFromImage(buff.smoothScale(w/2, h/2));
    	dest.setMask(maskBitsMini);
    }

}


// ---------------------------------------------------------
bool Tileset::loadTileset( const TQString& tilesetPath, const bool isPreview)
{

    TQImage qiTiles;
    TQRgb *unsel;
    TQRgb *sel;
    TQRgb *nextSel=0;
    TQRgb *nextUnsel=0;

    if (filename == tilesetPath) {
	return true;
    }


    // try to load it
    if( ! qiTiles.load( tilesetPath) )
        return( false );



    // we deal only with 32 bit images
    if (qiTiles.depth() != 32)
      qiTiles = qiTiles.convertDepth(32);



    // Read in the unselected and selected tile backgrounds
    copyTileImage(7, 4 , unselectedFace, qiTiles);
    copyTileImage(8, 4, selectedFace, qiTiles);



    // Read in the 9*5 tiles. Each tile is overlayed onto
    // the selected and unselected tile backgrounds and
    // stored.
    sel = selectedTiles;
    unsel = tiles;
    for (short atY=0; atY<5; atY++) {
	for (short atX=0; atX<9; atX++) {


	  nextUnsel = createTile(atX, atY, unsel , qiTiles, unselectedFace);

	  // for the preview dialog we do not create selected tiles
	  if (!isPreview)
	      nextSel = createTile(atX, atY, sel , qiTiles, selectedFace);
	  int pixNo = atX+(atY*9);

	  // for the preview dialog we only create the unselected mini pix
	  if (!isPreview) {
	      createPixmap(sel, selectedPix[pixNo], false, false);
	      createPixmap(unsel, unselectedPix[pixNo], false, false);
	      createPixmap(sel, selectedMiniPix[pixNo], true, false);

	      createPixmap(sel, selectedShadowPix[pixNo], false, true);
	      createPixmap(unsel, unselectedShadowPix[pixNo], false, true);
	      createPixmap(sel, selectedShadowMiniPix[pixNo], true, true);
	      createPixmap(unsel, unselectedShadowMiniPix[pixNo], true, true);
	  }

	  createPixmap(unsel, unselectedMiniPix[pixNo], true, false);
	  sel = nextSel;
	  unsel= nextUnsel;
	}
    }

    filename = tilesetPath;
    return( true );
}