Initial commit.
[amiga/xmodule.git] / Hooks / GetFT2.c
1 /*
2 **      GetFT2.c
3 **
4 **      Copyright (C) 1995 Bernardo Innocenti
5 **
6 **      Load a FastTracker ][ module with any number of tracks.
7 */
8
9 #include <exec/memory.h>
10 #include <libraries/XModule.h>
11
12 #include <clib/exec_protos.h>
13 #include <clib/dos_protos.h>
14 #include <clib/xmodule_protos.h>
15
16 #include <pragmas/exec_sysbase_pragmas.h>
17 #include <pragmas/dos_pragmas.h>
18 #include <pragmas/xmodule_pragmas.h>
19
20 #include "Gui.h"
21 #include "FastTracker2.h"
22
23
24 /* Convert an Intel style WORD to Motorola format */
25 #define I2M(x) ( (UWORD) ( (((UWORD)(x)) >> 8) | (((UWORD)(x)) << 8) ) )
26
27 /* Convert an Intel style LONG to Motorola format */
28 #define I2ML(x) ( I2M((x)>>16) | (I2M((x))<<16) )
29
30
31
32 LONG GetFT2 (struct SongInfo *si, BPTR fh)
33 {
34         ULONG i, j;             /* Loop counters */
35         struct FT2Header ft2hd;
36
37
38         /* Read module header */
39         if (Read (fh, &ft2hd, sizeof (ft2hd)) != sizeof (ft2hd))
40                 return ERR_READWRITE;
41
42         /* Convert from Intel's shitty endian */
43         ft2hd.HeaderSize        = I2ML(ft2hd.HeaderSize);
44         ft2hd.Length            = I2M(ft2hd.Length);
45         ft2hd.Restart           = I2M(ft2hd.Restart);
46         ft2hd.Channels          = I2M(ft2hd.Channels);
47         ft2hd.NumPatt           = I2M(ft2hd.NumPatt);
48         ft2hd.NumInstr          = I2M(ft2hd.NumInstr);
49         ft2hd.Flags                     = I2M(ft2hd.Flags);
50         ft2hd.DefTempo          = I2M(ft2hd.DefTempo);
51         ft2hd.DefBPM            = I2M(ft2hd.DefBPM);
52
53         if (!xmSetSongLen (si, ft2hd.Length))
54                 return ERROR_NO_FREE_STORE;
55
56         if (Read (fh, si->Sequence, si->Length * sizeof (UWORD)) !=
57                 si->Length * sizeof (UWORD))
58                 return ERR_READWRITE;
59
60         for (i = 0; i < si->Length; i++)
61                 si->Sequence[i] = I2M(si->Sequence[i]);
62
63         si->Restart = ft2hd.Restart;
64         si->GlobalSpeed = ft2hd.DefTempo;
65         si->GlobalTempo = ft2hd.DefBPM;
66
67         return RETURN_OK;
68 }