Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <stddef.h>
  6. #include <kos/libimg.h>
  7. #include <kos32sys1.h>
  8.  
  9. #define BTN_QUIT  1
  10.  
  11. struct kolibri_system_colors sys_color_table;
  12.  
  13. char proc_info[1024];
  14. char temp_path[4096];
  15. char* image_data_rgb;  // decoded image
  16.  
  17. /* load file to heap */
  18. char*   load_file_inmem(char* fname, int32_t* read_sz);
  19.  
  20. void draw_window()
  21. {
  22.     int win_hight, win_width, i, pos_x = 5, pos_y = get_skin_height();
  23.  
  24.     begin_draw();
  25.  
  26.     sys_create_window(10, 40, 600, 400, "My window", 0xFFFFFF, 0x13);
  27.  
  28.     get_proc_info(proc_info);
  29.     win_width = *(int*)(proc_info + 0x3E); // client, 2A windows
  30.     win_hight = *(int*)(proc_info + 0x42); // client, 2E windows
  31.  
  32.     draw_bitmap(image_data_rgb, pos_x, pos_y , 400, 600);
  33.     draw_bitmap(image_data_rgb, pos_x, pos_y, 16, 16);
  34.  
  35.     end_draw();
  36. }
  37.  
  38. int main(int argc, char **argv)
  39. {
  40.     int gui_event;
  41.     uint32_t pressed_button = 0, mouse_button;
  42.     pos_t   mouse_pos;
  43.  
  44.     if (-1 == kolibri_libimg_init())  // png handling
  45.     {
  46.                 debug_board_printf("error loading 'lin_img.obj'\n");
  47.                 exit(1);
  48.         }
  49.  
  50.     // load image
  51.     const int icon_rgb_size = 2400*2000; // file demo is 2400x2000x8bpp
  52.     char *image_data, *filedata;
  53.     strcpy(temp_path, "/kolibrios/res/wallpapers/in_the_wind.png");
  54.     debug_board_write_str(temp_path);
  55.  
  56.     int32_t read_bytes;
  57.     filedata = load_file_inmem(temp_path, &read_bytes);
  58.     image_data_rgb = malloc(icon_rgb_size * 3);
  59.     image_data = img_decode(filedata, read_bytes, 0);
  60.     img_to_rgb2(image_data, image_data_rgb);
  61.     img_destroy(image_data);
  62.     free(filedata);
  63.  
  64.  
  65.  
  66.     get_system_colors(&sys_color_table);
  67.     set_event_mask(0xC0000027); // mouse events only when focused window and mouse inside
  68.  
  69.     do  /* Start of main activity loop */
  70.     {
  71. //        gui_event = wait_for_event(10); // 100 = 1 sec, case you have background work
  72.         gui_event = get_os_event();
  73.         switch(gui_event)
  74.         {
  75.         case KOLIBRI_EVENT_NONE:
  76.             // background work
  77.                         break;
  78.         case KOLIBRI_EVENT_REDRAW:
  79.             draw_window();
  80.                         break;
  81.         case KOLIBRI_EVENT_KEY:
  82.             // scroll
  83.                         break;
  84.         case KOLIBRI_EVENT_BUTTON:
  85.             pressed_button = get_os_button();
  86.             switch (pressed_button)
  87.             {
  88.               case BTN_QUIT:
  89.                 return 0;
  90.                 break;
  91.             }
  92.             break;
  93.         case KOLIBRI_EVENT_MOUSE:
  94.             mouse_pos = get_mouse_pos(POS_WINDOW); // window relative
  95.             mouse_button = get_mouse_eventstate();
  96.             debug_board_printf("mouse ev (%d,%d)%x\n", mouse_pos.x, mouse_pos.y, mouse_button);
  97.             if (mouse_button & (1<<24)) // double click
  98.             {
  99.                 debug_board_printf("double click\n");
  100.             }
  101.             // ignore
  102.             break;
  103.         }
  104.     } while(1) ; /* End of main activity loop */
  105.  
  106.   return 0;
  107. }
  108.  
  109. char*  load_file_inmem(char* fname, int32_t* read_sz)
  110. {
  111.     FILE *f = fopen(fname, "rb");
  112.     if (!f) {
  113.         debug_board_printf("Can't open file: %s", fname);
  114.         exit(1);
  115.     }
  116.     if (fseek(f, 0, SEEK_END)) {
  117.         debug_board_printf("Can't SEEK_END file: %s", fname);
  118.         exit(1);
  119.     }
  120.     int filesize = ftell(f);
  121.     rewind(f);
  122.     char* fdata = malloc(filesize);
  123.     if(!fdata) {
  124.         debug_board_printf("No memory for file %s", fname);
  125.         exit(1);
  126.     }
  127.     *read_sz = fread(fdata, 1, filesize, f);
  128.     if (ferror(f)) {
  129.         debug_board_printf("Error reading file %s", fname);
  130.         exit(1);
  131.     }
  132.     fclose(f);
  133.  
  134.     return fdata;
  135. }
  136.  
  137.  
  138. void __attribute__ ((noinline)) debug_board_write_str(const char* str){
  139.   while(*str)
  140.     debug_board_write_byte(*str++);
  141. }
  142.  
  143. void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
  144. {
  145.         va_list ap;
  146.         char log_board[300];
  147.  
  148.         va_start (ap, format);
  149.         tiny_vsnprintf(log_board, sizeof log_board, format, ap);
  150.         va_end(ap);
  151.         debug_board_write_str(log_board);
  152.  
  153. }
  154.