cp_loader_it_patterns.cpp 4.7 KB

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