2 * Copyright 2008 Long Huynh Huu <long.upcase@googlemail.com>
3 * Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include <QtGui/QPainter>
24 void TileSet::initPixmap(int s
, const QPixmap
&pix
, int w
, int h
, const QRect
®ion
)
26 if (w
!= region
.width() || h
!= region
.height()) {
27 QPixmap tile
= pix
.copy(region
);
28 _pixmap
[s
] = QPixmap(w
, h
);
29 _pixmap
[s
].fill(QColor(0,0,0,0));
30 QPainter
p(&_pixmap
[s
]);
31 p
.drawTiledPixmap(0, 0, w
, h
, tile
);
34 _pixmap
[s
] = pix
.copy(region
);
38 TileSet::TileSet(const QPixmap
&pix
, int w1
, int h1
, int w2
, int h2
)
44 _w3
= pix
.width() - (w1
+ w2
);
45 _h3
= pix
.height() - (h1
+ h2
);
46 int w
= w2
; while (w
< 32 && w2
> 0) w
+= w2
;
47 int h
= h2
; while (h
< 32 && h2
> 0) h
+= h2
;
49 initPixmap(0, pix
, _w1
, _h1
, QRect(0, 0, _w1
, _h1
));
50 initPixmap(1, pix
, w
, _h1
, QRect(_w1
, 0, w2
, _h1
));
51 initPixmap(2, pix
, _w3
, _h1
, QRect(_w1
+w2
, 0, _w3
, _h1
));
52 initPixmap(3, pix
, _w1
, h
, QRect(0, _h1
, _w1
, h2
));
53 initPixmap(4, pix
, w
, h
, QRect(_w1
, _h1
, w2
, h2
));
54 initPixmap(5, pix
, _w3
, h
, QRect(_w1
+w2
, _h1
, _w3
, h2
));
55 initPixmap(6, pix
, _w1
, _h3
, QRect(0, _h1
+h2
, _w1
, _h3
));
56 initPixmap(7, pix
, w
, _h3
, QRect(_w1
, _h1
+h2
, w2
, _h3
));
57 initPixmap(8, pix
, _w3
, _h3
, QRect(_w1
+w2
, _h1
+h2
, _w3
, _h3
));
60 TileSet::TileSet(const QPixmap
&pix
, int w1
, int h1
, int w3
, int h3
, int x1
, int y1
, int w2
, int h2
)
61 : _w1(w1
), _h1(h1
), _w3(w3
), _h3(h3
)
66 int x2
= pix
.width() - _w3
;
67 int y2
= pix
.height() - _h3
;
68 int w
= w2
; while (w
< 32 && w2
> 0) w
+= w2
;
69 int h
= h2
; while (h
< 32 && h2
> 0) h
+= h2
;
71 initPixmap(0, pix
, _w1
, _h1
, QRect(0, 0, _w1
, _h1
));
72 initPixmap(1, pix
, w
, _h1
, QRect(x1
, 0, w2
, _h1
));
73 initPixmap(2, pix
, _w3
, _h1
, QRect(x2
, 0, _w3
, _h1
));
74 initPixmap(3, pix
, _w1
, h
, QRect(0, y1
, _w1
, h2
));
75 initPixmap(4, pix
, w
, h
, QRect(x1
, y1
, w2
, h2
));
76 initPixmap(5, pix
, _w3
, h
, QRect(x2
, y1
, _w3
, h2
));
77 initPixmap(6, pix
, _w1
, _h3
, QRect(0, y2
, _w1
, _h3
));
78 initPixmap(7, pix
, w
, _h3
, QRect(x1
, y2
, w2
, _h3
));
79 initPixmap(8, pix
, _w3
, _h3
, QRect(x2
, y2
, _w3
, _h3
));
82 TileSet::TileSet(const TileSet
&other
)
83 : _w1(other
._w1
), _h1(other
._h1
), _w3(other
._w3
), _h3(other
._h3
)
85 for (int i
=0; i
<9; i
++) {
86 _pixmap
[i
] = other
._pixmap
[i
];
90 TileSet
& TileSet::operator=(const TileSet
&other
)
96 for (int i
=0; i
<9; i
++) {
97 _pixmap
[i
] = other
._pixmap
[i
];
102 inline bool bits(TileSet::Tiles flags
, TileSet::Tiles testFlags
)
104 return (flags
& testFlags
) == testFlags
;
107 void TileSet::render(const QRect
&r
, QPainter
*p
, Tiles t
) const
109 if (_pixmap
[0].isNull())
113 r
.getRect(&x0
, &y0
, &w
, &h
);
121 if (bits(t
, Top
|Left
)) p
->drawPixmap(x0
, y0
, _pixmap
[0]);
122 if (bits(t
, Top
|Right
)) p
->drawPixmap(x2
, y0
, _pixmap
[2]);
123 if (bits(t
, Bottom
|Left
)) p
->drawPixmap(x0
, y2
, _pixmap
[6]);
124 if (bits(t
, Bottom
|Right
)) p
->drawPixmap(x2
, y2
, _pixmap
[8]);
126 if (t
& Top
) p
->drawTiledPixmap(x1
, y0
, w
, _h1
, _pixmap
[1]);
127 if (t
& Bottom
) p
->drawTiledPixmap(x1
, y2
, w
, _h3
, _pixmap
[7]);
128 if (t
& Left
) p
->drawTiledPixmap(x0
, y1
, _w1
, h
, _pixmap
[3]);
129 if (t
& Right
) p
->drawTiledPixmap(x2
, y1
, _w3
, h
, _pixmap
[5]);
131 if (t
& Center
) p
->drawTiledPixmap(x1
, y1
, w
, h
, _pixmap
[4]);