2020-12-12 17:49:35 +01:00
|
|
|
|
// Includes //
|
2020-09-21 11:10:57 +02:00
|
|
|
|
#include <kos32sys.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
2020-09-26 14:21:25 +02:00
|
|
|
|
#include <stdio.h>
|
2020-10-02 09:23:01 +02:00
|
|
|
|
#include <time.h>
|
2020-12-12 17:49:35 +01:00
|
|
|
|
|
2020-12-05 10:20:50 +01:00
|
|
|
|
#include <kolibri_libimg.h>
|
2020-12-12 17:49:35 +01:00
|
|
|
|
// #include "mp3.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// C-- event defines //
|
2020-10-02 09:23:01 +02:00
|
|
|
|
#define evReDraw 1
|
|
|
|
|
#define evKey 2
|
|
|
|
|
#define evButton 3
|
|
|
|
|
#define evExit 4
|
|
|
|
|
#define evDesktop 5
|
|
|
|
|
#define evMouse 6
|
|
|
|
|
#define evIPC 7
|
|
|
|
|
#define evNetwork 8
|
|
|
|
|
#define evDebug 9
|
2020-09-21 11:10:57 +02:00
|
|
|
|
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
// Code //
|
|
|
|
|
#define button_color 0xbbbbbb
|
|
|
|
|
#define button_size 44
|
2020-09-21 11:10:57 +02:00
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
#define field_size 4
|
2020-09-21 11:10:57 +02:00
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
int field[field_size][field_size] = {
|
2020-09-21 11:10:57 +02:00
|
|
|
|
{0, 0, 0, 1},
|
|
|
|
|
{0, 0, 1, 0},
|
|
|
|
|
{0, 0, 0, 1},
|
2020-09-21 18:37:54 +02:00
|
|
|
|
{1, 0, 0, 0}
|
2020-09-21 11:10:57 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
char* title = "Fridge";
|
2020-12-12 17:49:35 +01:00
|
|
|
|
#define BUTTON_RESTART 99
|
2020-09-21 11:10:57 +02:00
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
short victory = 0;
|
2020-09-26 14:21:25 +02:00
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
// Load pictures //
|
2020-09-26 14:21:25 +02:00
|
|
|
|
char temp_path[4096];
|
2020-12-12 17:49:35 +01:00
|
|
|
|
char* HORIZONTAL_IMAGE;
|
|
|
|
|
char* VERTICAL_IMAGE;
|
2020-09-26 14:21:25 +02:00
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
char* load_file_inmem(char* fname, int32_t* read_sz)
|
2020-09-26 14:21:25 +02:00
|
|
|
|
{
|
2020-10-02 09:23:01 +02:00
|
|
|
|
FILE *f = fopen(fname, "rb");
|
|
|
|
|
if (!f) {
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (fseek(f, 0, SEEK_END)) {
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2020-12-12 17:49:35 +01:00
|
|
|
|
int filefield_size = ftell(f);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
rewind(f);
|
2020-12-12 17:49:35 +01:00
|
|
|
|
char* fdata = malloc(filefield_size);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
if(!fdata) {
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2020-12-12 17:49:35 +01:00
|
|
|
|
*read_sz = fread(fdata, 1, filefield_size, f);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
if (ferror(f)) {
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
|
|
return fdata;
|
2020-09-26 14:21:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
void load_pictures() {
|
|
|
|
|
const int icon_rgb_field_size = button_size*button_size;
|
2020-10-02 09:23:01 +02:00
|
|
|
|
char *image_data,
|
|
|
|
|
*filedata;
|
|
|
|
|
|
|
|
|
|
strcpy(temp_path, "h.png");
|
2020-09-26 14:21:25 +02:00
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
int32_t read_bytes;
|
|
|
|
|
filedata = load_file_inmem(temp_path, &read_bytes);
|
2020-12-12 17:49:35 +01:00
|
|
|
|
HORIZONTAL_IMAGE = malloc(icon_rgb_field_size * 3);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
|
|
|
|
|
image_data = img_decode(filedata, read_bytes, 0);
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
img_to_rgb2(image_data, HORIZONTAL_IMAGE);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
strcpy(temp_path, "v.png");
|
2020-09-26 14:21:25 +02:00
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
filedata = load_file_inmem(temp_path, &read_bytes);
|
2020-12-12 17:49:35 +01:00
|
|
|
|
VERTICAL_IMAGE = malloc(icon_rgb_field_size * 3);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
|
|
|
|
|
image_data = img_decode(filedata, read_bytes, 0);
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
img_to_rgb2(image_data, VERTICAL_IMAGE);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
|
|
|
|
|
img_destroy(image_data);
|
|
|
|
|
free(filedata);
|
2020-09-26 14:21:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
// GUI functions //
|
2020-10-02 09:23:01 +02:00
|
|
|
|
void redraw_buttons() {
|
2020-12-12 17:49:35 +01:00
|
|
|
|
for (int j = 5, x = 0; x<field_size; j+=button_size, x++)
|
|
|
|
|
for (int i = 15, y = 0; y<field_size; i+=button_size, y++)
|
2020-10-02 09:23:01 +02:00
|
|
|
|
{
|
2020-12-12 17:49:35 +01:00
|
|
|
|
// 0x50 mean button without drawing, but with border when press
|
|
|
|
|
// ((y+1)*10)+x+1 mean button id
|
|
|
|
|
define_button(65536 * i + (button_size), 65536 * j + (button_size), (0x50 << 24) | ((y+1)*10)+x+1, 0);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
if (field[x][y]) draw_bitmap(VERTICAL_IMAGE, i, j, button_size, button_size);
|
|
|
|
|
else draw_bitmap(HORIZONTAL_IMAGE, i, j, button_size, button_size);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-26 14:21:25 +02:00
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
void draw_game_window(){
|
|
|
|
|
BeginDraw();
|
2020-12-12 17:49:35 +01:00
|
|
|
|
DrawWindow(215, 100, 220, 220, title, button_color, 0x34);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
redraw_buttons();
|
|
|
|
|
EndDraw();
|
|
|
|
|
}
|
2020-09-26 14:21:25 +02:00
|
|
|
|
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
// Need refactoring:
|
2020-10-02 09:23:01 +02:00
|
|
|
|
static inline
|
|
|
|
|
void draw_text_sysNEW(const char *text, int x, int y, int len, int fontType, color_t color)
|
|
|
|
|
{
|
|
|
|
|
__asm__ __volatile__(
|
|
|
|
|
"int $0x40"
|
|
|
|
|
::"a"(4),"d"(text),
|
|
|
|
|
"b"((x << 16) | y),
|
|
|
|
|
"S"(len),"c"(fontType<<24+color)
|
|
|
|
|
:"memory");
|
2020-09-26 14:21:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
void SetUp() {
|
2020-12-12 17:49:35 +01:00
|
|
|
|
for (int y = 0; y<field_size; y++)
|
|
|
|
|
for (int x = 0; x<field_size; x++)
|
|
|
|
|
{
|
|
|
|
|
field[x][y] = rand() % 2;
|
|
|
|
|
}
|
2020-09-21 11:10:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
// Need refactoring:
|
2020-10-02 09:23:01 +02:00
|
|
|
|
void draw_victory_window() {
|
|
|
|
|
BeginDraw();
|
2020-12-12 17:49:35 +01:00
|
|
|
|
DrawWindow(215,100,220, 220,title,button_color,0x34);
|
2020-09-21 11:10:57 +02:00
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
draw_text_sysNEW("Ну вы, и", 10, 10, strlen("Ну вы, и"), 0xB1, 0x000000);
|
|
|
|
|
draw_text_sysNEW("медвежатник,", 10, 50, strlen("Ну вы, и медвежатник,"), 0xB1, 0x000000);
|
|
|
|
|
draw_text_sysNEW("Шеф!", 12, 90, strlen("Шеф!"), 0xB1, 0x000000);
|
2020-09-21 11:10:57 +02:00
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
define_button(65536 * ((220/2)-(50)) + 140, 65536 * 140 + 25+12, BUTTON_RESTART, 0x9A9A9A);
|
2020-10-02 09:23:01 +02:00
|
|
|
|
draw_text_sysNEW("Заново", 80, 145, strlen("Заново"), 0xB1, 0x000000);
|
|
|
|
|
EndDraw();
|
2020-09-21 11:10:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Button() {
|
|
|
|
|
int id = get_os_button();
|
|
|
|
|
if (id == 1) exit(0); else
|
2020-12-12 17:49:35 +01:00
|
|
|
|
if (id == BUTTON_RESTART) {
|
2020-10-02 09:23:01 +02:00
|
|
|
|
SetUp();
|
|
|
|
|
vict = 0;
|
|
|
|
|
draw_game_window();
|
|
|
|
|
} else
|
2020-09-21 11:10:57 +02:00
|
|
|
|
{
|
2020-12-05 10:20:50 +01:00
|
|
|
|
// PlayMusic("./rotate.mp3");
|
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
int x = (id/10)-1;
|
|
|
|
|
int y = (id%10)-1;
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
for (int i = 0; i<field_size; i++)
|
2020-10-02 09:23:01 +02:00
|
|
|
|
if (field[i][x]) field[i][x] = 0; else field[i][x] = 1;
|
|
|
|
|
|
2020-12-12 17:49:35 +01:00
|
|
|
|
for (int i = 0; i<field_size; i++)
|
2020-10-02 09:23:01 +02:00
|
|
|
|
if (field[y][i]) field[y][i] = 0; else field[y][i] = 1;
|
2020-09-26 14:30:45 +02:00
|
|
|
|
|
2020-10-02 09:23:01 +02:00
|
|
|
|
if (field[y][x]) field[y][x] = 0; else field[y][x] = 1;
|
|
|
|
|
|
|
|
|
|
redraw_buttons();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int fridge_opened() {
|
|
|
|
|
int fr_op = 0;
|
2020-12-12 17:49:35 +01:00
|
|
|
|
for (int y = 0; y<field_size; y++)
|
|
|
|
|
for (int x = 0; x<field_size; x++)
|
2020-10-02 09:23:01 +02:00
|
|
|
|
{
|
|
|
|
|
fr_op += field[x][y];
|
|
|
|
|
}
|
|
|
|
|
if (fr_op == 0) return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
|
|
if (kolibri_libimg_init() == -1)
|
|
|
|
|
{
|
|
|
|
|
printf("Can not load libimg.obj!\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2020-12-12 17:49:35 +01:00
|
|
|
|
|
|
|
|
|
load_pictures();
|
2020-10-02 09:23:01 +02:00
|
|
|
|
|
|
|
|
|
draw_game_window();
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
switch(get_os_event())
|
|
|
|
|
{
|
|
|
|
|
case evButton:
|
|
|
|
|
Button();
|
|
|
|
|
if (fridge_opened()) {
|
2020-12-12 17:49:35 +01:00
|
|
|
|
victory = 1;
|
2020-10-02 09:23:01 +02:00
|
|
|
|
draw_victory_window();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case evKey:
|
|
|
|
|
get_key();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case evReDraw:
|
2020-12-12 17:49:35 +01:00
|
|
|
|
if (!victory) draw_game_window();
|
2020-10-02 09:23:01 +02:00
|
|
|
|
else draw_victory_window();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-09-21 11:10:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|