Subversion Repositories Kolibri OS

Rev

Rev 8324 | Rev 8379 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8378 maxcodehac 1
// Includes //
8071 maxcodehac 2
#include 
3
#include 
4
#include 
8080 maxcodehac 5
#include 
8084 maxcodehac 6
#include 
8378 maxcodehac 7
 
8324 maxcodehac 8
#include 
8378 maxcodehac 9
// #include "mp3.h"
10
 
11
 
12
// C-- event defines //
8084 maxcodehac 13
#define evReDraw  1
14
#define evKey     2
15
#define evButton  3
16
#define evExit    4
17
#define evDesktop 5
18
#define evMouse   6
19
#define evIPC     7
20
#define evNetwork 8
21
#define evDebug   9
8071 maxcodehac 22
 
23
 
8378 maxcodehac 24
// Code //
25
#define button_color 0xbbbbbb
26
#define button_size 44
8084 maxcodehac 27
 
8378 maxcodehac 28
#define field_size 4
8071 maxcodehac 29
 
8378 maxcodehac 30
int field[field_size][field_size] = {
8071 maxcodehac 31
	{0, 0, 0, 1},
32
	{0, 0, 1, 0},
33
	{0, 0, 0, 1},
8072 maxcodehac 34
	{1, 0, 0, 0}
8071 maxcodehac 35
};
36
 
8084 maxcodehac 37
char* title = "Fridge";
8378 maxcodehac 38
#define BUTTON_RESTART 99
8071 maxcodehac 39
 
8378 maxcodehac 40
short victory = 0;
8084 maxcodehac 41
 
8378 maxcodehac 42
// Load pictures //
8080 maxcodehac 43
char temp_path[4096];
8378 maxcodehac 44
char* HORIZONTAL_IMAGE;
45
char* VERTICAL_IMAGE;
8080 maxcodehac 46
 
8378 maxcodehac 47
char* load_file_inmem(char* fname, int32_t* read_sz)
8080 maxcodehac 48
{
8084 maxcodehac 49
		FILE *f = fopen(fname, "rb");
50
		if (!f) {
51
			exit(1);
52
		}
53
		if (fseek(f, 0, SEEK_END)) {
54
			exit(1);
55
		}
8378 maxcodehac 56
		int filefield_size = ftell(f);
8084 maxcodehac 57
		rewind(f);
8378 maxcodehac 58
		char* fdata = malloc(filefield_size);
8084 maxcodehac 59
		if(!fdata) {
60
			exit(1);
61
		}
8378 maxcodehac 62
		*read_sz = fread(fdata, 1, filefield_size, f);
8084 maxcodehac 63
		if (ferror(f)) {
64
			exit(1);
65
		}
66
		fclose(f);
8080 maxcodehac 67
 
8084 maxcodehac 68
		return fdata;
8080 maxcodehac 69
}
70
 
8378 maxcodehac 71
void load_pictures() {
72
		const int icon_rgb_field_size = button_size*button_size;
8084 maxcodehac 73
		char *image_data,
74
			 *filedata;
75
 
76
		strcpy(temp_path, "h.png");
8080 maxcodehac 77
 
8084 maxcodehac 78
		int32_t read_bytes;
79
		filedata = load_file_inmem(temp_path, &read_bytes);
8378 maxcodehac 80
		HORIZONTAL_IMAGE = malloc(icon_rgb_field_size * 3);
8084 maxcodehac 81
 
82
		image_data = img_decode(filedata, read_bytes, 0);
83
 
8378 maxcodehac 84
		img_to_rgb2(image_data, HORIZONTAL_IMAGE);
8084 maxcodehac 85
 
86
 
87
		strcpy(temp_path, "v.png");
8080 maxcodehac 88
 
8084 maxcodehac 89
		filedata = load_file_inmem(temp_path, &read_bytes);
8378 maxcodehac 90
		VERTICAL_IMAGE = malloc(icon_rgb_field_size * 3);
8084 maxcodehac 91
 
92
		image_data = img_decode(filedata, read_bytes, 0);
93
 
8378 maxcodehac 94
		img_to_rgb2(image_data, VERTICAL_IMAGE);
8084 maxcodehac 95
 
96
		img_destroy(image_data);
97
		free(filedata);
8080 maxcodehac 98
}
99
 
100
 
8378 maxcodehac 101
// GUI functions //
8084 maxcodehac 102
void redraw_buttons() {
8378 maxcodehac 103
		for (int j = 5, x = 0; x
104
				for (int i = 15, y = 0; y
8084 maxcodehac 105
				{
8378 maxcodehac 106
					// 0x50 mean button without drawing, but with border when press
107
					// ((y+1)*10)+x+1 mean button id
108
					define_button(65536 * i + (button_size), 65536 * j + (button_size), (0x50 << 24) | ((y+1)*10)+x+1, 0);
8084 maxcodehac 109
 
8378 maxcodehac 110
					if (field[x][y]) draw_bitmap(VERTICAL_IMAGE, i, j, button_size, button_size);
111
					else draw_bitmap(HORIZONTAL_IMAGE, i, j, button_size, button_size);
8084 maxcodehac 112
				}
113
}
8080 maxcodehac 114
 
8084 maxcodehac 115
void draw_game_window(){
116
		BeginDraw();
8378 maxcodehac 117
		DrawWindow(215, 100, 220, 220, title, button_color, 0x34);
8084 maxcodehac 118
		redraw_buttons();
119
		EndDraw();
120
}
8080 maxcodehac 121
 
122
 
8378 maxcodehac 123
// Need refactoring:
8084 maxcodehac 124
static inline
125
void draw_text_sysNEW(const char *text, int x, int y, int len, int fontType, color_t color)
126
{
127
		__asm__ __volatile__(
128
		"int $0x40"
129
		::"a"(4),"d"(text),
130
		  "b"((x << 16) | y),
131
		  "S"(len),"c"(fontType<<24+color)
132
		 :"memory");
8080 maxcodehac 133
}
134
 
8084 maxcodehac 135
void SetUp() {
8378 maxcodehac 136
		for (int y = 0; y
137
			for (int x = 0; x
138
			{
139
				field[x][y] = rand() % 2;
140
			}
8071 maxcodehac 141
}
142
 
8378 maxcodehac 143
// Need refactoring:
8084 maxcodehac 144
void draw_victory_window() {
145
		BeginDraw();
8378 maxcodehac 146
		DrawWindow(215,100,220, 220,title,button_color,0x34);
8071 maxcodehac 147
 
8084 maxcodehac 148
		draw_text_sysNEW("Ну вы, и", 10, 10, strlen("Ну вы, и"), 0xB1, 0x000000);
149
		draw_text_sysNEW("медвежатник,", 10, 50, strlen("Ну вы, и медвежатник,"), 0xB1, 0x000000);
150
		draw_text_sysNEW("Шеф!", 12, 90, strlen("Шеф!"), 0xB1, 0x000000);
8071 maxcodehac 151
 
8378 maxcodehac 152
		define_button(65536 * ((220/2)-(50)) + 140, 65536 * 140 + 25+12, BUTTON_RESTART, 0x9A9A9A);
8084 maxcodehac 153
		draw_text_sysNEW("Заново", 80, 145, strlen("Заново"), 0xB1, 0x000000);
154
		EndDraw();
8071 maxcodehac 155
}
156
 
8084 maxcodehac 157
 
158
 
159
void Button() {
160
		int id = get_os_button();
161
		if (id == 1) exit(0); else
8378 maxcodehac 162
		if (id == BUTTON_RESTART) {
8084 maxcodehac 163
			SetUp();
164
			vict = 0;
165
			draw_game_window();
166
		} else
8071 maxcodehac 167
		{
8324 maxcodehac 168
			// PlayMusic("./rotate.mp3");
169
 
8084 maxcodehac 170
			int x = (id/10)-1;
171
			int y = (id%10)-1;
172
 
8378 maxcodehac 173
			for (int i = 0; i
8084 maxcodehac 174
				if (field[i][x]) field[i][x] = 0; else field[i][x] = 1;
175
 
8378 maxcodehac 176
			for (int i = 0; i
8084 maxcodehac 177
				if (field[y][i]) field[y][i] = 0; else field[y][i] = 1;
8081 maxcodehac 178
 
8084 maxcodehac 179
			if (field[y][x]) field[y][x] = 0; else field[y][x] = 1;
180
 
181
			redraw_buttons();
8071 maxcodehac 182
		}
183
}
8084 maxcodehac 184
 
185
 
186
int fridge_opened() {
187
		int fr_op = 0;
8378 maxcodehac 188
		for (int y = 0; y
189
				for (int x = 0; x
8084 maxcodehac 190
				{
191
					fr_op += field[x][y];
192
				}
193
		if (fr_op == 0) return 1;
194
		return 0;
195
}
196
 
197
 
198
int main()
199
{
200
		srand(time(0));
201
 
202
		if (kolibri_libimg_init() == -1)
203
		{
204
			printf("Can not load libimg.obj!\n");
205
			exit(1);
206
		}
207
 
8378 maxcodehac 208
		load_pictures();
209
 
8084 maxcodehac 210
		draw_game_window();
211
		while(1)
212
		{
213
			switch(get_os_event())
214
			{
215
				case evButton:
216
					Button();
217
					if (fridge_opened()) {
8378 maxcodehac 218
						victory = 1;
8084 maxcodehac 219
						draw_victory_window();
220
					}
221
					break;
222
 
223
				case evKey:
224
					get_key();
225
					break;
226
 
227
				case evReDraw:
8378 maxcodehac 228
					if (!victory) draw_game_window();
8084 maxcodehac 229
					else draw_victory_window();
230
					break;
231
			}
232
		}
233
}