home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / chip_20022115.iso / amiga / chipgame / scummvm_aga.lha / ScummVM_AGA / src / sys.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-01-05  |  3.4 KB  |  166 lines

  1. /* ScummVM - Scumm Interpreter
  2.  * Copyright (C) 2001  Ludvig Strigeus
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  *
  18.  * $Header: /cvsroot/scummvm/scummvm/sys.cpp,v 1.4 2001/11/06 20:00:47 strigeus Exp $
  19.  *
  20.  */
  21.  
  22. #include "stdafx.h"
  23. #include "scumm.h"
  24.  
  25. void *Scumm::fileOpen(const char *filename, int mode) {
  26.     _fileMode = mode;
  27.     _whereInResToRead = 0;    
  28.     clearFileReadFailed(_fileHandle);
  29.  
  30.     if (mode==1)
  31.         return fopen(filename, "rb");
  32.     
  33.     if (mode==2) {
  34.         error("fileOpen: write not supported");
  35.     }
  36.     
  37.     return NULL;
  38. }
  39.  
  40. void Scumm::fileClose(void *file) {
  41.     if (_fileMode==1 || _fileMode==2)
  42.         fclose((FILE*)file);
  43. }
  44.  
  45. bool Scumm::fileReadFailed(void *file) {
  46.     return _fileReadFailed != 0;
  47. }
  48.  
  49. void Scumm::clearFileReadFailed(void *file) {
  50.     _fileReadFailed = false;
  51. }
  52.  
  53. bool Scumm::fileEof(void *file) {
  54.     FILE *a = (FILE*)file;
  55.     return feof((FILE*)file) != 0;
  56. }
  57.  
  58. void Scumm::fileSeek(void *file, long offs, int whence) {
  59.     switch(_fileMode) {
  60.     case 1: case 2:
  61.         fseek((FILE*)file, offs, whence);
  62.         return;
  63.     case 3:
  64.         _whereInResToRead = offs;
  65.         return;
  66.     }
  67. }
  68.  
  69. void Scumm::fileRead(void *file, void *ptr, uint32 size) {
  70.     byte *ptr2 = (byte*)ptr, *src;
  71.  
  72.     switch(_fileMode) {
  73.     case 1:
  74.         if (size==0)
  75.             return;
  76.  
  77.         if ((uint32)fread(ptr2, size, 1, (FILE*)file) != 1)
  78.             _fileReadFailed = true;
  79.  
  80.         do {
  81.             *ptr2++    ^= _encbyte;
  82.         } while(--size);
  83.  
  84.         return;
  85.  
  86.     case 3:
  87.         if (size==0)
  88.             return;
  89.  
  90.         src = getResourceAddress(rtTemp, 3) + _whereInResToRead;
  91.         _whereInResToRead += size;
  92.         do {
  93.             *ptr2++ = *src++ ^ _encbyte;
  94.         } while (--size);
  95.         return;
  96.     }
  97. }
  98.  
  99. int Scumm::fileReadByte() {
  100.     byte b;
  101.     byte *src;
  102.  
  103.     switch(_fileMode) {
  104.     case 1:
  105.         if (fread(&b,1,1,(FILE*)_fileHandle) != 1)
  106.             _fileReadFailed = true;
  107.         return b ^ _encbyte;
  108.  
  109.     case 3:
  110.         src = getResourceAddress(rtTemp, 3) + _whereInResToRead;
  111.         _whereInResToRead++;
  112.         return *src ^ _encbyte;
  113.     }
  114.     return 0;
  115. }
  116.  
  117. uint Scumm::fileReadWordLE() {
  118.     uint a = fileReadByte();
  119.     uint b = fileReadByte();
  120.     return a|(b<<8);
  121. }
  122.  
  123. uint32 Scumm::fileReadDwordLE() {
  124.     uint a = fileReadWordLE();
  125.     uint b = fileReadWordLE();
  126.     return (b<<16)|a;
  127. }
  128.  
  129. uint Scumm::fileReadWordBE() {
  130.     uint b = fileReadByte();
  131.     uint a = fileReadByte();
  132.     return a|(b<<8);
  133. }
  134.  
  135. uint32 Scumm::fileReadDwordBE() {
  136.     uint b = fileReadWordBE();
  137.     uint a = fileReadWordBE();
  138.     return (b<<16)|a;
  139. }
  140.  
  141. byte *Scumm::alloc(int size) {
  142.     byte *me = (byte*)::calloc(size+4,1);
  143.     if (me==NULL)
  144.         return NULL;
  145.  
  146.     *((uint32*)me) = 0xDEADBEEF;
  147.     return me + 4;
  148. }
  149.  
  150. void Scumm::free(void *mem) {
  151.     if (mem) {
  152.         byte *me = (byte*)mem - 4;
  153.         if ( *((uint32*)me) != 0xDEADBEEF) {
  154.             error("Freeing invalid block.");
  155.         }
  156.  
  157.         *((uint32*)me) = 0xC007CAFE;
  158.         ::free(me);
  159.     }
  160. }
  161.  
  162. bool Scumm::checkFixedDisk() {
  163.     return true;
  164. }
  165.  
  166.