cp_loader_it_patterns.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*************************************************************************/
  2. /* cp_loader_it_patterns.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "cp_loader_it.h"
  30. CPLoader::Error CPLoader_IT::load_patterns() {
  31. for (int i=0;i<header.patnum;i++) {
  32. if (i>=CPSong::MAX_PATTERNS)
  33. break;
  34. /* Position where pattern offsets are stored */
  35. file->seek(0xC0+header.ordnum+header.insnum*4+header.smpnum*4+i*4);
  36. uint32_t pattern_offset=file->get_dword();
  37. if (pattern_offset==0) {
  38. continue;
  39. }
  40. uint16_t pat_size;
  41. uint16_t pat_length;
  42. int row=0,flag,channel,j;
  43. uint8_t aux_byte;
  44. uint32_t reserved;
  45. uint8_t chan_mask[64]; //mask cache for each
  46. CPNote last_value[64]; //last value of each
  47. for (j=0;j<64;j++) {
  48. chan_mask[j]=0;
  49. last_value[j].clear();
  50. }
  51. file->seek(pattern_offset);
  52. pat_size=file->get_word();
  53. pat_length=file->get_word();
  54. reserved=file->get_dword();
  55. song->get_pattern(i)->set_length( pat_length );
  56. do {
  57. aux_byte=file->get_byte();
  58. flag=aux_byte;
  59. if ( flag==0 ) {
  60. row++;
  61. } else {
  62. channel=(flag-1) & 63;
  63. if ( flag & 128 ) {
  64. aux_byte=file->get_byte();
  65. chan_mask[channel]=aux_byte;
  66. }
  67. CPNote note; //note used for reading
  68. if ( chan_mask[channel]&1 ) { // read note
  69. aux_byte=file->get_byte();
  70. if ( aux_byte<120 )
  71. note.note=aux_byte;
  72. else if ( aux_byte==255 )
  73. note.note=CPNote::OFF;
  74. else if ( aux_byte==254 )
  75. note.note=CPNote::CUT;
  76. last_value[channel].note=note.note;
  77. }
  78. if ( chan_mask[channel]&2 ) {
  79. aux_byte=file->get_byte();
  80. if ( aux_byte<100 )
  81. note.instrument=aux_byte-1;
  82. last_value[channel].instrument=note.instrument;
  83. }
  84. if ( chan_mask[channel]&4 ) {
  85. aux_byte=file->get_byte();
  86. if ( aux_byte<213 )
  87. note.volume=aux_byte;
  88. last_value[channel].volume=note.volume;
  89. }
  90. if ( chan_mask[channel]&8 ) {
  91. aux_byte=file->get_byte();
  92. if ( aux_byte>0 )
  93. note.command=aux_byte-1;
  94. last_value[channel].command=note.command;
  95. note.parameter=file->get_byte();
  96. last_value[channel].parameter=note.parameter;
  97. }
  98. if ( chan_mask[channel]&16 ) {
  99. note.note=last_value[channel].note;
  100. }
  101. if ( chan_mask[channel]&32 ) {
  102. note.instrument=last_value[channel].instrument;
  103. }
  104. if ( chan_mask[channel]&64 ) {
  105. note.volume=last_value[channel].volume;
  106. }
  107. if ( chan_mask[channel]&128 ) {
  108. note.command=last_value[channel].command;
  109. note.parameter=last_value[channel].parameter;
  110. }
  111. song->get_pattern(i)->set_note(channel,row,note);
  112. }
  113. } while(row<pat_length);
  114. }
  115. return FILE_OK;
  116. }