Subversion Repositories Kolibri OS

Rev

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

  1. // Includes //
  2. #include <sys/kos.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7.  
  8. #include <kolibri_libimg.h>
  9. // #include "mp3.h"
  10.  
  11. // Code //
  12. #define button_color 0xbbbbbb
  13. #define button_size 44
  14.  
  15. #define field_size 4
  16.  
  17. int field[field_size][field_size] = {
  18.         {0, 0, 0, 1},
  19.         {0, 0, 1, 0},
  20.         {0, 0, 0, 1},
  21.         {1, 0, 0, 0}
  22. };
  23.  
  24. char* title = "Fridge";
  25. #define BUTTON_RESTART 99
  26.  
  27. short victory = 0;
  28.  
  29. // Load pictures //
  30. char temp_path[4096];
  31. Image* HORIZONTAL_IMAGE;
  32. Image* VERTICAL_IMAGE;
  33.  
  34. char* load_file_inmem(char* fname, int32_t* read_sz)
  35. {
  36.                 FILE *f = fopen(fname, "rb");
  37.                 if (!f) {
  38.                         exit(1);
  39.                 }
  40.                 if (fseek(f, 0, SEEK_END)) {
  41.                         exit(1);
  42.                 }
  43.                 int filefield_size = ftell(f);
  44.                 rewind(f);
  45.                 char* fdata = malloc(filefield_size);
  46.                 if(!fdata) {
  47.                         exit(1);
  48.                 }
  49.                 *read_sz = fread(fdata, 1, filefield_size, f);
  50.                 if (ferror(f)) {
  51.                         exit(1);
  52.                 }
  53.                 fclose(f);
  54.  
  55.                 return fdata;
  56. }
  57.  
  58. void load_pictures() {
  59.                 const int icon_rgb_field_size = button_size*button_size;
  60.                 Image *image_data;
  61.                 char *filedata;
  62.                
  63.                 strcpy(temp_path, "h.png");
  64.  
  65.                 int32_t read_bytes;
  66.                 filedata = load_file_inmem(temp_path, &read_bytes);
  67.                 HORIZONTAL_IMAGE = malloc(icon_rgb_field_size * 3);
  68.                
  69.                 image_data = img_decode(filedata, read_bytes, 0);
  70.                
  71.                 img_to_rgb2(image_data, HORIZONTAL_IMAGE);
  72.                
  73.                
  74.                 strcpy(temp_path, "v.png");
  75.  
  76.                 filedata = load_file_inmem(temp_path, &read_bytes);
  77.                 VERTICAL_IMAGE = malloc(icon_rgb_field_size * 3);
  78.                
  79.                 image_data = img_decode(filedata, read_bytes, 0);
  80.                
  81.                 img_to_rgb2(image_data, VERTICAL_IMAGE);
  82.                
  83.                 img_destroy(image_data);
  84.                 free(filedata);
  85. }
  86.  
  87.  
  88. // GUI functions //
  89. void RedrawButtons() {
  90.                 for (int j = 5, x = 0; x<field_size; j+=button_size, x++)
  91.                                 for (int i = 15, y = 0; y<field_size; i+=button_size, y++)
  92.                                 {
  93.                                         // 0x50 mean button without drawing, but with border when press
  94.                                         // ((y+1)*10)+x+1 mean button id
  95.                                         kos_DrawButton(i, j, button_size, button_size, (0x50 << 24) | ((y+1)*10)+x+1, 0);
  96.                                        
  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);
  99.                                 }
  100. }
  101.  
  102. void draw_game_window(){
  103.                 kos_BeginDraw();
  104.                 kos_DrawWindow(215, 100, 220, 220, title, button_color, 0x34);
  105.                 RedrawButtons();
  106.                 kos_EndDraw();
  107. }
  108.  
  109.  
  110. // Need refactoring:
  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");
  120. }
  121.  
  122. void SetUp() {
  123.                 for (int y = 0; y<field_size; y++)
  124.                         for (int x = 0; x<field_size; x++)
  125.                         {
  126.                                 field[x][y] = rand() % 2;
  127.                         }
  128. }
  129.  
  130. // Need refactoring:
  131. void draw_victory_window() {
  132.                 kos_BeginDraw();
  133.                 kos_DrawWindow(215,100,220, 220,title,button_color,0x34);
  134.                
  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);
  138.                
  139.                 kos_DrawButton(((220/2)-(50)), 140, 140, 25+12, BUTTON_RESTART, 0x9A9A9A);
  140.                 draw_text_sysNEW("Заново", 80, 145, strlen("Заново"), 0xB1, 0x000000);
  141.                 kos_EndDraw();
  142. }
  143.  
  144. void Button() {
  145.                 int id = kos_GetButtonID();
  146.                 if (id == 1) exit(0); else
  147.                 if (id == BUTTON_RESTART) {
  148.                         SetUp();
  149.                         victory = 0;
  150.                         draw_game_window();
  151.                 } else
  152.                 {
  153.                         // PlayMusic("./rotate.mp3");
  154.                        
  155.                         int x = (id/10)-1;
  156.                         int y = (id%10)-1;
  157.                        
  158.                         for (int i = 0; i<field_size; i++)
  159.                                 if (field[i][x]) field[i][x] = 0; else field[i][x] = 1;
  160.                        
  161.                         for (int i = 0; i<field_size; i++)
  162.                                 if (field[y][i]) field[y][i] = 0; else field[y][i] = 1;
  163.                                
  164.                         if (field[y][x]) field[y][x] = 0; else field[y][x] = 1;
  165.                        
  166.                         RedrawButtons();
  167.                 }
  168. }
  169.  
  170.  
  171. int FridgeOpened() {
  172.                 int fr_op = 0;
  173.                 for (int y = 0; y<field_size; y++)
  174.                                 for (int x = 0; x<field_size; x++)
  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.                 {
  189.                         printf("Can not load libimg!\n");
  190.                         exit(1);
  191.                 }
  192.                
  193.                 load_pictures();
  194.                
  195.                 while(1)
  196.                 {
  197.                         switch(kos_WaitForEvent())
  198.                         {
  199.                                 case evButton:
  200.                                         Button();
  201.                                         if (FridgeOpened()) {
  202.                                                 victory = 1;
  203.                                                 draw_victory_window();
  204.                                         }
  205.                                         break;
  206.                                 case evReDraw:
  207.                                         if (!victory) draw_game_window();
  208.                                         else draw_victory_window();
  209.                                         break;
  210.                         }
  211.                 }
  212. }
  213.