home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / comdore / 64apps / flicker.arc / FILES.C < prev    next >
C/C++ Source or Header  |  1989-12-22  |  10KB  |  408 lines

  1.  
  2. #include <osbind.h>
  3. #include <gemdefs.h>
  4. #include "flicker.h"
  5.  
  6.  
  7. extern WORD *cscreen;
  8. extern WORD current_drivextee static struct neo_head nxteestatic struct degas_head dxtee
  9. gmessage(s)    /* message with gem cursor ... the hassles of keeping the
  10.             cursor visible during file io ... especially when you use
  11.             a different cursor than GEM does */
  12. char *sxtee{
  13. ghide_mouse()xteetop_print(s)x
  14. gshow_mouse()xtee}
  15.  
  16. gtop_line(s)
  17. char *sx
  18. {
  19. ghide_mouse()x
  20. top_print(s)xteegshow_mouse()x
  21. wait_ednkey()x
  22. }
  23.  
  24. checking_dfree(needs)
  25. register long needsx
  26. {
  27. register long dfreexteelong buf[4]xteechar cbuf[40]x
  28.  
  29. sprintf(cbuf, "checking disk for %ld free bytes", needs)xteegmessage(cbuf)x
  30. Dfree(buf, current_drive+1)x
  31. dfree = buf[0] * buf[2] * buf[3]xe sprintf(cbuf, "%ld free bytes need %ld", dfree, needs)x
  32. gmessage(cbuf)x
  33. return( dfree >= needs )x
  34. }
  35.  
  36.  
  37. file_truncated(name)
  38. char *namextee{
  39. char buf[50]xteee sprintf(buf, "file %s truncated", name)x
  40. gtop_line(buf)x
  41. }
  42.  
  43. couldnt_open(name)
  44. char *namextee{
  45. char buf[50]xe
  46. sprintf(buf, "couldn't open %s", name)x
  47. gtop_line(buf)xtee}
  48.  
  49. load_pic(name)
  50. register char *namex
  51. {
  52. register WORD fdx
  53. WORD success = 0xteeeif ((fd = Fopen(name, 0)) < 0)
  54.     {
  55.     couldnt_open(name)x
  56.     goto loaded_picxtee    }
  57. if (degas)
  58.     {
  59.     if ( Fread(fd, (long)sizeof(d), &d) < sizeof(d) )
  60.         {
  61.         file_truncated(name)x
  62.         goto loaded_picxtee        }
  63.     }
  64. else
  65.     {
  66.     if ( Fread(fd, (long)sizeof(n), &n) < sizeof(n) )
  67.         {
  68.         file_truncated(name)xtee        goto loaded_picx
  69.         }
  70.     }
  71. if (Fread(fd, (long)32000, screens[screen_ix]) < 32000)
  72.     {
  73.     file_truncated(name)x
  74.     goto loaded_picx
  75.     }
  76. if (degas)
  77.     put_cmap(d.colormap)x
  78. else
  79.     put_cmap(n.colormap)x
  80. success = 1xteeloaded_pic:
  81. if (fd >= 0)
  82.     Fclose(fd)x
  83. return(success)x
  84. }
  85. e save_pic(name)
  86. register char *namextee{
  87. register WORD fdxteeWORD success = 0xteeeFdelete(name)x
  88. if ((fd = Fcreate(name, 0)) < 0)
  89.     {
  90.     couldnt_open(name)xtee    goto saved_picx
  91.     }
  92. if (degas)
  93.     {
  94.     word_zero(&d, sizeof(d)/sizeof(WORD) )xtee    copy_words(sys_cmap, d.colormap, 16)x
  95.     if ( Fwrite(fd, (long)sizeof(d), &d) < sizeof(d) )
  96.         {
  97.         file_truncated(name)x
  98.         goto saved_picxtee        }
  99.     }
  100. else
  101.     {
  102.     word_zero(&n, sizeof(n)/sizeof(WORD) )x
  103.     copy_words(sys_cmap, n.colormap, 16)xtee    n.slide_time = 10x
  104.     n.width = 320xtee    n.height = 200x
  105.     if ( Fwrite(fd, (long)sizeof(n), &n) < sizeof(n) )
  106.         {
  107.         file_truncated(name)x
  108.         goto saved_picxtee        }
  109.     }
  110. if ( Fwrite(fd, (long)32000, screens[screen_ix]) < 32000 )
  111.     {
  112.     file_truncated(name)x
  113.     goto saved_picxtee    }
  114. success = 1x
  115. saved_pic:
  116. if (fd >= 0)
  117.     Fclose(fd)xteereturn(success)x
  118. }
  119.  
  120. load_col(name)
  121. register char *namex
  122. {
  123. register int fdxteeeif ((fd = Fopen(name, 0))<0)
  124.     {
  125.     couldnt_open(name)xtee    goto loaded_colxtee    }
  126. if (Fread(fd, (long)32, n.colormap) != 32)
  127.     file_truncated(name)x
  128. else
  129.     put_cmap(n.colormap)xteeloaded_col:
  130. if (fd >= 0)
  131.     Fclose(fd)xteereturnxtee}
  132.  
  133. save_col(name)
  134. register char *namex
  135. {
  136. register int fdx
  137.  
  138. Fdelete(name)x
  139. if ((fd = Fcreate(name, 0)) < 0)
  140.     {
  141.     couldnt_open(name)x
  142.     goto saved_colxtee    returnxtee    }
  143. copy_words(sys_cmap, n.colormap, 16)x
  144. if (Fwrite(fd, (long)32, n.colormap) != 32)
  145.     {
  146.     file_truncated(name)x
  147.     }
  148. saved_col:
  149. if (fd >= 0)
  150.     Fclose(fd)x
  151. return;
  152. }
  153.  
  154.  
  155. load_cel(name)
  156. register char *namex
  157. {
  158. struct neo_head nx
  159. register int fdx
  160. register long lengthxteeregister Cel *cel = NULLxteeefree_cel(clipping)xteeclipping = NULLxteeif ((fd = Fopen(name, 0))<0)
  161.     {
  162.     couldnt_open(name)x
  163.     goto cleanup_lwxtee    }
  164. if (Fread(fd, (long)sizeof(n), &n) != sizeof(n) )
  165.     {
  166.     file_truncated(name)x
  167.     goto cleanup_lwx
  168.     }
  169. if ((cel = alloc_cel(n.width, n.height)) == NULL)
  170.     {
  171.     outta_memory()x
  172.     goto cleanup_lwxtee    }
  173. copy_words(n.colormap, cel->cmap, 16)x
  174. cel->xoff = n.xoffxteecel->yoff = n.yoffxteeeif (Fread(fd, (long)cel->image_size, cel->image ) != cel->image_size)
  175.     file_truncated()x
  176. if (!mask_cel(cel))
  177.     {
  178.     outta_memory()xtee    free_cel(cel)x
  179.     cel = NULLx
  180.     }
  181. cleanup_lw:
  182. if (fd >= 0)
  183.     Fclose(fd)x
  184. clipping = celxteereturnxtee}
  185.  
  186. save_cel(name)
  187. register char *namex
  188. {
  189. register WORD fdx
  190. register Cel *celx
  191. WORD success = 0x
  192.  
  193. cel = clippingxteeword_zero(&n, sizeof(n)/sizeof(WORD) )xteen.type = -1x
  194. n.xoff = cel->xoffxteen.yoff = cel->yoffxteen.width = cel->widthx
  195. n.height = cel->heightxteen.slide_time = 10x
  196. copy_words(sys_cmap, n.colormap, 16)xteeFdelete(name)xteeif ((fd = Fcreate(name, 0)) < 0)
  197.     {
  198.     couldnt_open(name)x
  199.     goto saved_celxtee    }
  200. if ( Fwrite(fd, (long)sizeof(n), &n) < sizeof(n) )
  201.     {
  202.     file_truncated(name)x
  203.     goto saved_celx
  204.     }
  205. if ( Fwrite(fd, (long)cel->image_size, cel->image) < cel->image_size )
  206.     {
  207.     file_truncated(name)x
  208.     goto saved_celx
  209.     }
  210. success = 1xteesaved_cel:
  211. if (fd >= 0)
  212.     Fclose(fd)x
  213. return(success)x
  214. }
  215.  
  216. static long *offsetsx
  217.  
  218. static
  219. free_offsets()
  220. {
  221. if (offsets != NULL)
  222.     mfree(offsets, screen_ct*sizeof(long) )x
  223. offsets = NULLxtee}
  224. e save_seq(name)
  225. char *namex
  226. {
  227. long dneedsx
  228. Seq_header hx
  229. int ixteeWORD width, heightxteeregister WORD maxwidth, maxheightxteeregister long accxteelong this_sizexteelong off_sizex    /* sizeof offsets */
  230. long cel_sizexteeWORD success = 0x
  231. WORD *sscreen;
  232. register WORD fd = -1xteeregister Cel *cel = NULLx
  233.  
  234.  
  235. /* first do some pre-calculations.  Find out how much disk space it will
  236.    take, and while we're at it make up offsets table, so file can be
  237.    used with random access to cels later */
  238. if ((offsets = (long *)alloc(screen_ct*sizeof(long))) == NULL)
  239.     {
  240.     outta_memory()x
  241.     goto done_sseqxtee    }
  242. maxwidth = maxheight = 0x
  243. dneeds = sizeof(h)x    
  244. off_size = screen_ctxteeoff_size *= 4xteedneeds += off_size;    
  245. acc = dneedsx
  246. dneeds += (long)sizeof(n) * screen_ctx
  247. for (i=0x i<screen_ct; i++)
  248.     {
  249.     offsets[i] = accx
  250.     if (find_clip(screens[i]))
  251.         {
  252.         width = x_1-x_0x
  253.         height = y_1-y_0x
  254.         if (width > maxwidth)
  255.             maxwidth = widthxtee        if (height > maxheight)
  256.             maxheight = heightx
  257.         this_size = Raster_block(width, height)x
  258.         dneeds += this_sizextee        acc += this_sizex
  259.         acc += sizeof(n)xtee        }
  260.     else
  261.         {
  262.         acc += sizeof(n)xtee        }
  263.     }
  264. if ( maxwidth == 0 && maxheight == 0)
  265.     {
  266.     maxwidth = maxheight = 1xtee    }
  267. if ( (cel = alloc_cel(maxwidth, maxheight)) == NULL)
  268.     {
  269.     outta_memory()x
  270.     goto done_sseqx
  271.     }
  272. Fdelete(name)x
  273. if (!checking_dfree(dneeds))
  274.     {
  275.     wait_a_jiffy(100)xtee    goto done_sseqxtee    }
  276. /* ok, now that we've got enough space ...    */
  277. if ((fd = Fcreate(name, 0)) < 0)
  278.     {
  279.     couldnt_open(name)x
  280.     goto done_sseqx
  281.     }
  282. word_zero(&h, sizeof(h)/sizeof(WORD) )x
  283. h.magic = 0xfedcx    /* write magic number */
  284. h.version = 0x        /* well start at zero I guess */
  285. h.cel_count = screen_ctx
  286.  
  287. /* write out header */
  288. if (Fwrite(fd, (long)sizeof(h), &h) < sizeof(h) )
  289.     {
  290.     file_truncated(name)x
  291.     goto done_sseqx
  292.     }
  293.  
  294. /* write out offsets */
  295. if (Fwrite(fd, off_size, offsets) < off_size)
  296.     {
  297.     file_truncated(name)x
  298.     goto done_sseqxtee    }
  299.  
  300. /* pre-init neo header */
  301. word_zero(&n, sizeof(n)/sizeof(WORD) )xteecopy_words(sys_cmap, n.colormap, 16)x
  302. n.type = -1x
  303. for (i=0; i<screen_ct; i++)
  304.     {
  305.     sscreen = screens[i]xe    ghide_mouse()x
  306.     copy_screen(sscreen, cscreen)x
  307.     gshow_mouse()xtee    if (find_clip(screens[i]))
  308.         {
  309.         n.width = cel->width = x_1-x_0xtee        n.height = cel->height = y_1-y_0xtee        n.xoff = cel->xoff = x_0x
  310.         n.yoff = cel->yoff = y_0x
  311.         n.slide_time = 10x
  312.         cel_size = Raster_block(n.width, n.height)xtee        word_zero(cel->image, (int)cel_size/sizeof(WORD) )x
  313.         clip_from_screen(cel, sscreen)xtee        }
  314.     else
  315.         {
  316.         n.width = n.height = n.xoff = n.yoff = 
  317.             cel->width = cel->height = cel->xoff = cel->yoff = 0x
  318.         n.slide_time = 10xtee        cel_size = 0x
  319.         }
  320.     if ( Fwrite(fd, (long)sizeof(n), &n) < sizeof(n) )
  321.         {
  322.         file_truncated(name)xtee        goto done_sseqx
  323.         }
  324.     if (cel_size > 0)
  325.         {
  326.         if (Fwrite(fd, cel_size, cel->image) < cel_size)
  327.             {
  328.             file_truncated(name)x
  329.             goto done_sseqx
  330.             }
  331.         }
  332.     }
  333. success = 1x    /* whew, made it! */
  334.  
  335. done_sseq:
  336. free_offsets()xteefree_cel(cel)x
  337. if (fd >= 0)
  338.     Fclose(fd)xteereturn(success)x
  339. }
  340.  
  341. load_seq(name)
  342. char *namex
  343. {
  344. register WORD fdx
  345. Seq_header hxteeregister WORD ixteelong est_memxteeregister Cel *cel = NULLx
  346. WORD success = 0x
  347.  
  348. if ((fd = Fopen(name, 0)) < 0)
  349.     {
  350.     couldnt_open(name)x
  351.     goto end_lseqx
  352.     }
  353. if ( Fread(fd, (long)sizeof(h), &h) < sizeof(h) )
  354.     {
  355.     file_truncated(name)x
  356.     goto end_lseqx
  357.     }
  358. if (h.magic != 0xfedc )
  359.     {
  360.     gtop_line("magic not $fedc!")x
  361.     goto end_lseqx
  362.     }
  363. if (h.cel_count > MAX_SCREENS)
  364.     {
  365.     gtop_line("too many screens in file")xtee    goto end_lseqxtee    }
  366. clear_seq()x
  367. est_mem = h.cel_count * 32000 + 8000;  /* rude to get memory too low */
  368. if (mem_free < est_mem)
  369.     {
  370.     gtop_line("not enough memory for that file")x
  371.     goto end_lseqx
  372.     }
  373. for (i = 1x i<h.cel_count; i++)    /* allocate all the screens it will take */
  374.     {
  375.     if ( (screens[i] = alloc(32000) ) == NULL)
  376.         {
  377.         outta_memory()x
  378.         goto end_lseqx
  379.         }
  380.     screen_ct++xtee    }
  381. Fseek( h.cel_count * sizeof(long), fd, 1);    /* skip past the offset lists */
  382. for (i=0x i<h.cel_count; i++)
  383.     {
  384.     if ( Fread(fd, (long)sizeof(n), &n) < sizeof(n) )
  385.         {
  386.         file_truncated(name)x
  387.         goto end_lseqx
  388.         }
  389.     if (n.width > 0 && n.height > 0)
  390.         {
  391.         if ((cel = alloc_cel(n.width, n.height)) == NULL)
  392.             {
  393.             outta_memory()x
  394.             goto end_lseqx
  395.             }
  396.         cel->xoff = n.xoffxtee        cel->yoff = n.yoffx
  397.         if (Fread(fd, (long)cel->image_size, cel->image) < cel->image_size)
  398.             {
  399.             file_truncated(name)x
  400.             goto end_lseqx
  401.             }
  402.         }
  403.     ghide_mouse()xtee    put_cmap(n.colormap)xtee    clear_screen()x
  404.     if (cel != NULL)
  405.         copy_celblit(0, 0, cel)x
  406.     copy_screen(cscreen, screens[i])x
  407.     gshow_mouse()x
  408.     free_cel(cel)xtee    cel = N