home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Anwendungen / glmatrix / source / glmatrix_prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-31  |  3.7 KB  |  141 lines

  1. #include <exec/memory.h>
  2. #include <libraries/iffparse.h>
  3. #include <prefs/prefhdr.h>
  4.  
  5. #include <proto/exec.h>
  6. #include <proto/dos.h>
  7. #include <proto/iffparse.h>
  8.  
  9. #ifdef __MIXED_BINARY__
  10. #include <proto/powerpc.h>
  11. #endif
  12.  
  13. #include <clib/alib_stdio_protos.h>
  14.  
  15. #include "glmatrix_prefs.h"
  16.  
  17. struct GLMatrixPrefs *glmatrix_prefs = NULL;
  18.  
  19. struct GLMatrixPrefs* read_prefs(void)
  20. {
  21.     struct IFFHandle *iff_handle = NULL;
  22.     struct ContextNode *cn = NULL;
  23.     struct StoredProperty *sp = NULL;
  24.  
  25. #ifdef __MIXED_BINARY__
  26.     struct GLMatrixPrefs *prefs = AllocVec32(sizeof(struct GLMatrixPrefs), MEMF_CLEAR | MEMF_ANY);
  27. #else
  28.     struct GLMatrixPrefs *prefs = AllocVec(sizeof(struct GLMatrixPrefs), MEMF_CLEAR | MEMF_ANY);
  29. #endif
  30.  
  31.     if (prefs != NULL)
  32.     {
  33.         /* Loading default values into prefs */
  34.         prefs->glm_Timeout = 300;
  35.         prefs->glm_Density = 20;
  36.         prefs->glm_Speed = 100;
  37.         prefs->glm_Encoding = 0;
  38.         prefs->glm_Fog = TRUE;
  39.         prefs->glm_Wave = TRUE;
  40.         prefs->glm_Rotate = TRUE;
  41.         prefs->glm_Invert = FALSE;
  42.         prefs->glm_ScreenSize = 0;
  43.  
  44.         iff_handle = AllocIFF();
  45.         if (iff_handle != NULL)
  46.         {
  47.             iff_handle->iff_Stream = (ULONG)Open(ENVPREFSFILE, MODE_OLDFILE);
  48.             if (iff_handle->iff_Stream != 0)
  49.             {
  50.                 InitIFFasDOS(iff_handle);
  51.                 if (OpenIFF(iff_handle, IFFF_READ) == 0)
  52.                 {
  53.                     LONG error = 0;
  54.                     BOOL version1 = FALSE;
  55.  
  56.                     /*PropChunk(iff_handle, ID_PREF, ID_PRHD);*/
  57.                     /*PropChunk(iff_handle, ID_PREF, ID_GLMP);*/
  58.                     PropChunk(iff_handle, ID_PREF, ID_PRHD);
  59.                     StopChunk(iff_handle, ID_PREF, ID_GLMP);
  60.  
  61.                     error = ParseIFF(iff_handle, IFFPARSE_SCAN);
  62.  
  63.                     sp = FindProp(iff_handle, ID_PREF, ID_PRHD);
  64.                     if (sp != NULL)
  65.                     {
  66.                         struct PrefHeader *head = sp->sp_Data;
  67.                         version1 = head->ph_Version == 0 ? FALSE : TRUE;
  68.                     } /* if sp */
  69.  
  70.                     cn = CurrentChunk(iff_handle);
  71.                     if (cn != NULL && cn->cn_ID == ID_GLMP)
  72.                     {
  73.                         LONG read_length = version1 ? cn->cn_Size : cn->cn_Size - (sizeof(BOOL) + sizeof(ULONG));
  74.                         ReadChunkBytes(iff_handle, (APTR)prefs, read_length);
  75.                     } /* if cn */
  76.  
  77.                     CloseIFF(iff_handle);
  78.                 } /* if OpenIFF */
  79.  
  80.                 Close((BPTR)iff_handle->iff_Stream);
  81.             } /* if iff_stream */
  82.  
  83.             FreeIFF(iff_handle);
  84.         }/* if iff_handle */
  85.     } /* if prefs */
  86.  
  87.     return prefs;
  88. }
  89.  
  90. void write_prefs(struct GLMatrixPrefs *prefs, char *filename)
  91. {
  92.     struct IFFHandle *iff_handle = NULL;
  93.  
  94.     struct PrefHeader header = {1,0,0};
  95.  
  96.     if(prefs != NULL)
  97.     {
  98.         iff_handle = AllocIFF();
  99.         if (iff_handle != NULL)
  100.         {
  101.             iff_handle->iff_Stream = (ULONG)Open(filename, MODE_NEWFILE);
  102.             if (iff_handle->iff_Stream != NULL)
  103.             {
  104.                 InitIFFasDOS(iff_handle);
  105.                 if (OpenIFF(iff_handle, IFFF_RWBITS) == 0)
  106.                 {
  107.                     PushChunk(iff_handle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN);
  108.  
  109.                     /* Write Prefs header */
  110.                     PushChunk(iff_handle, ID_PREF, ID_PRHD, sizeof(struct PrefHeader));
  111.                     WriteChunkBytes(iff_handle, &header, sizeof(header));
  112.                     PopChunk(iff_handle);
  113.  
  114.                     /* Write GLMatrix prefs */
  115.                     PushChunk(iff_handle, ID_PREF, ID_GLMP, sizeof(struct GLMatrixPrefs));
  116.                     WriteChunkBytes(iff_handle, prefs, sizeof(*prefs));
  117.                     PopChunk(iff_handle);
  118.  
  119.                     PopChunk(iff_handle);
  120.  
  121.                     CloseIFF(iff_handle);
  122.                 }
  123.  
  124.                 Close((BPTR)iff_handle->iff_Stream);
  125.             }
  126.             FreeIFF(iff_handle);
  127.         }
  128.     }
  129. }
  130.  
  131. void use_prefs(void)
  132. {
  133.     write_prefs(glmatrix_prefs, ENVPREFSFILE);
  134. }
  135.  
  136. void save_prefs(void)
  137. {
  138.     write_prefs(glmatrix_prefs, ENVARCPREFSFILE);
  139.     write_prefs(glmatrix_prefs, ENVPREFSFILE);
  140. }
  141.