Subversion Repositories Kolibri OS

Rev

Rev 8545 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* Written by turbocat2001 (Logaev Maxim) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stddef.h>
  6. #include <kos32sys.h>
  7. #include <kolibri_gui.h>
  8. #include <kolibri_libimg.h>
  9.  
  10. #define NEW_IMG_H 128
  11. #define NEW_IMG_W 128
  12.  
  13. #define IMG_H 256
  14. #define IMG_W 256
  15.  
  16. Image *image_blend; // Create image struct
  17.  
  18. struct kolibri_system_colors sys_color_table;  // Create system colors table
  19.  
  20. char* load_img(char* fname, int32_t* read_sz){  // Image file upload function
  21.     FILE *f = fopen(fname, "rb");
  22.     if (!f) {
  23.         printf("Can't open file: %s\n", fname);
  24.         exit(0);
  25.     }
  26.     if (fseek(f, 0, SEEK_END)) {
  27.         printf("Can't SEEK_END file: %s\n", fname);
  28.         exit(0);
  29.     }
  30.     int filesize = ftell(f);
  31.     rewind(f);
  32.     char* fdata = malloc(filesize);
  33.     if(!fdata) {
  34.         printf("No memory for file %s\n", fname);
  35.         exit(0);
  36.     }
  37.     *read_sz = fread(fdata, 1, filesize, f);
  38.     if (ferror(f)) {
  39.         printf("Error reading file %s\n", fname);
  40.         exit(0);
  41.     }
  42.     fclose(f);
  43.     return fdata;
  44. }
  45.  
  46. void DrawGUI(){
  47.     begin_draw();
  48.         sys_create_window(10, 40, (IMG_W+NEW_IMG_W)+50, IMG_H+50, "Libimg", sys_color_table.work_area, 0x34);
  49.         img_draw(image_blend, 10, 10, IMG_W*2, IMG_H , 0, 0);  // Draw blended image to window
  50.     end_draw();
  51. }
  52.  
  53. int main(){
  54.     if (kolibri_libimg_init() == -1){
  55.                 printf("Error loading lib_img.obj\n");
  56.         exit(0);
  57.         }
  58.        
  59.         get_system_colors(&sys_color_table); // Get system colors theme
  60.    
  61.     uint32_t img_size;
  62.     void *file_data = load_img("logo.png", &img_size); // Get RAW data and size
  63.    
  64.     Image* image = img_decode(file_data, img_size, 0); // Decode RAW data to Image data
  65.    
  66.     if (image->Type != IMAGE_BPP32) {
  67.         image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
  68.         if (!image) {
  69.              printf("Сonvert error!: \n");  
  70.              exit(0);
  71.         }
  72.     }
  73.    
  74.     image_blend = img_create(IMG_W+NEW_IMG_W, IMG_H, IMAGE_BPP32);  // Create an empty layer
  75.     img_fill_color(image_blend, IMG_W+NEW_IMG_W, IMG_H, sys_color_table.work_area); // Fill the layer with one color
  76.     img_blend(image_blend, image, 0, 0, 0, 0, IMG_W, IMG_H);  // Blending images to display the alpha channel.
  77.     /* Reduce image size from 256x256 to 128x128 */
  78.     image = img_scale(image, 0, 0, IMG_W, IMG_H, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, NEW_IMG_W, NEW_IMG_H);
  79.     img_blend(image_blend, image, 256, 0, 0, 0, NEW_IMG_W, NEW_IMG_H);
  80.     img_destroy(image);  // Destroy image structure
  81.     free(file_data);  // Free allocated file_data buffer
  82.    
  83.     set_wanted_events_mask(0xC0000027);
  84.    
  85.     /* Main event loop */
  86.     while (1) {        
  87.                 switch(get_os_event()){
  88.                         case KOLIBRI_EVENT_REDRAW:
  89.                                 DrawGUI();
  90.                                 break;
  91.                         case KOLIBRI_EVENT_BUTTON:
  92.                                 if (get_os_button() == 1){
  93.                     return 0;
  94.                 }
  95.                                 break;
  96.         }
  97.         }
  98.         return 0;
  99. }
  100.