Subversion Repositories Kolibri OS

Rev

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