summaryrefslogtreecommitdiffstats
path: root/ksnake/level.cpp
blob: 8636081881f96b2bb2b8669f55b9afa46ed5f1cd (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
/**
 * Copyright Michel Filippi <mfilippi@sade.rhein-main.de>
 *           Robert Williams
 *           Andrew Chant <andrew.chant@utoronto.ca>
 *           André Luiz dos Santos <andre@netvision.com.br>
 *           Benjamin Meyer <ben+ksnake@meyerhome.net>
 *
 * This file is part of the ksnake package
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <qbitmap.h>
#include <qimage.h>

#include "level.h"
#include "board.h"
#include "bitmaps.h"
#include "levels.h"

const uchar *numbers[10] = { zero_bits, one_bits, two_bits,
				 three_bits, four_bits, five_bits,
				 six_bits, seven_bits, eight_bits, nine_bits
};

Level::Level(Board *b)
{
	board = b;
	create(Intro);
}

void Level::nextLevel()
{
	if (level < leV->max())
		level++;
}

void Level::create(Img img)
{
	switch(img){
		case Banner:
			createBanner();
		break;
		case Room:
			createRoom();
		break;
		case Intro:
			makeImageFromData(intro_bits);
		break;
		case GameOver:
			makeImageFromData(gameover_bits);
		break;
	}
}

void Level::createRoom()
{
	QImage image = leV->getImage(level);
	initBoard(image);
}

void Level::makeImageFromData(const uchar *buf)
{
	QBitmap bitmap(BoardWidth, BoardWidth, buf, true);
	QImage image = bitmap.convertToImage();
	initBoard (image);
}

void Level::initBoard(const QImage &image)
{
	int index = 0;
	uchar *b;

	for ( int y = 0;y < image.height();y++ ) {
		b = image.scanLine(y);
		for ( int x = 0;x < image.width();x++ ) {

			if ( image.bitOrder() == QImage::BigEndian ) {
				if (((*b >> (7 - (x & 7))) & 1) == 1)
					board->set(index, brick);
				else board->set(index, empty);
				} else {
				if (((*b >> (x & 7)) & 1) == 1)
					board->set(index, brick);
				else board->set(index, empty);
			}

			if ( (x & 7) == 7 )
				b++;
			index++;
		}
	}
}

void Level::createBanner()
{
	makeImageFromData(level_bits);

	QString num;
	num.setNum(level);
	if(level < 10)
		num.insert(0,'0');

	QString left = num.left(1);
	QString right = num.right(1);

	drawNumber ( 606, numbers[left.toInt()] );
	drawNumber ( 614, numbers[right.toInt()] );
}

void Level::drawNumber(int beginAt, const uchar *buf)
{
	QBitmap bitmap(7, 9, buf, true);
	QImage image = bitmap.convertToImage();

	int index = beginAt;
	uchar *b;

	for ( int y = 0;y < image.height();y++ )
	{
		b = image.scanLine(y);
		for ( int x = 0;x < image.width();x++ )
		{
			if ( image.bitOrder() == QImage::BigEndian )
			{
				if (((*b >> (7 - (x & 7))) & 1) == 1)
					board->set(index, brick);
			} else {
				if (((*b >> (x & 7)) & 1) == 1)
					board->set(index, brick);
			}
			if ( (x & 7) == 7 )
				b++;
			index++;
		}
		index += 28;
	}
}