summaryrefslogtreecommitdiff
path: root/modules/chibi/cp_loader_it_patterns.cpp
blob: 528d99fff7eefaab67064deecb1d882cfba5b48c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*************************************************************************/
/*  cp_loader_it_patterns.cpp                                            */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "cp_loader_it.h"


CPLoader::Error CPLoader_IT::load_patterns() {


	for (int i=0;i<header.patnum;i++) {

		if (i>=CPSong::MAX_PATTERNS)
			break;
			
		/* Position where pattern offsets are stored */
		file->seek(0xC0+header.ordnum+header.insnum*4+header.smpnum*4+i*4);
		uint32_t pattern_offset=file->get_dword();
		
		if (pattern_offset==0) {

			continue;
		}
			
		uint16_t pat_size;
		uint16_t pat_length;

		int row=0,flag,channel,j;
		uint8_t aux_byte;
		uint32_t reserved;
		uint8_t chan_mask[64]; //mask cache for each
		CPNote last_value[64]; //last value of each

		for (j=0;j<64;j++) {

			chan_mask[j]=0;
			last_value[j].clear();
		}

		file->seek(pattern_offset);

		pat_size=file->get_word();
		pat_length=file->get_word();
		reserved=file->get_dword();

		song->get_pattern(i)->set_length( pat_length );
		
		do {

			aux_byte=file->get_byte();
			flag=aux_byte;

			if ( flag==0 ) {

				row++;
			} else {

				channel=(flag-1) & 63;

				if ( flag & 128 ) {

					aux_byte=file->get_byte();
					chan_mask[channel]=aux_byte;
				}

				CPNote note; //note used for reading

				if ( chan_mask[channel]&1 ) { // read note
			
					aux_byte=file->get_byte();
					
					if ( aux_byte<120 )
						note.note=aux_byte;
					else if ( aux_byte==255 ) 	
						note.note=CPNote::OFF;
					else if ( aux_byte==254 )
						note.note=CPNote::CUT;

					last_value[channel].note=note.note;
				}
					

				if ( chan_mask[channel]&2 ) {

					aux_byte=file->get_byte();
					if ( aux_byte<100 )
						note.instrument=aux_byte-1;

					last_value[channel].instrument=note.instrument;
				}
				if ( chan_mask[channel]&4 ) {

					aux_byte=file->get_byte();
					if ( aux_byte<213 )
						note.volume=aux_byte;

					last_value[channel].volume=note.volume;
				}
				if ( chan_mask[channel]&8 ) {

					aux_byte=file->get_byte();
					if ( aux_byte>0 ) 
						note.command=aux_byte-1;
					
					
					last_value[channel].command=note.command;

					note.parameter=file->get_byte();
					
					last_value[channel].parameter=note.parameter;
				}

				if ( chan_mask[channel]&16 ) {

					note.note=last_value[channel].note;
				}

				if ( chan_mask[channel]&32 ) {

					note.instrument=last_value[channel].instrument;
				}
				if ( chan_mask[channel]&64 ) {

					note.volume=last_value[channel].volume;
				}
				if ( chan_mask[channel]&128 ) {

					note.command=last_value[channel].command;
					note.parameter=last_value[channel].parameter;
				}
				
				song->get_pattern(i)->set_note(channel,row,note);
			}
			
			
		} while(row<pat_length);

	}

	return FILE_OK;
}