Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stddef.h>
  5. #include <clayer/libimg.h>
  6. #include <kos32sys1.h>
  7.  
  8. struct kolibri_system_colors sys_color_table;
  9.  
  10. char path[4096];
  11. char* picture;
  12. int x_size = 200, y_size = 150;
  13.  
  14. char* load_file_inmem(char* fname, int32_t* read_sz)
  15. {
  16.     FILE *f = fopen(fname, "rb");
  17.     if (!f) {
  18.         printf("Can't open file: %s\n", fname);
  19.     }
  20.     if (fseek(f, 0, SEEK_END)) {
  21.         printf("Can't SEEK_END file: %s\n", fname);
  22.     }
  23.     int filesize = ftell(f);
  24.     rewind(f);
  25.     char* fdata = malloc(filesize);
  26.     if(!fdata) {
  27.         printf("No memory for file %s\n", fname);
  28.     }
  29.     *read_sz = fread(fdata, 1, filesize, f);
  30.     if (ferror(f)) {
  31.         printf("Error reading file %s\n", fname);
  32.     }
  33.     fclose(f);
  34.  
  35.     return fdata;
  36. }
  37.  
  38. void draw_window()
  39. {
  40.     BeginDraw();
  41.         DrawWindow(10, 40, x_size + 50, y_size + 50, "Libimg", sys_color_table.work_area, 0x34);
  42.        
  43.         // Draw Picture
  44.         draw_bitmap(picture, 10, 10, x_size, y_size);
  45.        
  46.     EndDraw();
  47. }
  48.  
  49. int main()
  50. {
  51.     if (kolibri_libimg_init() == -1)
  52.     {
  53.                 printf("Error loading lib_img.obj\n");
  54.         }
  55.  
  56.     // Load Image
  57.     const int icon_rgb_size = x_size * y_size;
  58.     char *image_data,
  59.          *filedata;
  60.    
  61.     strcpy(path, "kolibrios.jpg"); // Filename
  62.     int32_t read_bytes;
  63.     filedata = load_file_inmem(path, &read_bytes);
  64.     picture = malloc(icon_rgb_size * 3);
  65.     image_data = img_decode(filedata, read_bytes, 0);
  66.     img_to_rgb2(image_data, picture);
  67.     img_destroy(image_data);
  68.     free(filedata);
  69.         // End Load Image
  70.  
  71.         get_system_colors(&sys_color_table);
  72.     set_event_mask(0xC0000027);
  73.  
  74.     while (1) {
  75.                 switch(get_os_event())
  76.                 {
  77.                         case KOLIBRI_EVENT_REDRAW:
  78.                                 draw_window();
  79.                                 break;
  80.                         case KOLIBRI_EVENT_BUTTON:
  81.                                 if (get_os_button() == 1) exit(0);
  82.                                 break;
  83.                 }
  84.         }
  85.         return 0;
  86. }
  87.