furnace/src/engine/pattern.cpp

100 lines
2.6 KiB
C++
Raw Normal View History

2022-02-14 22:12:20 -05:00
/**
* Furnace Tracker - multi-system chiptune tracker
2023-01-19 19:18:40 -05:00
* Copyright (C) 2021-2023 tildearrow and contributors
2022-02-14 22:12:20 -05:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2021-12-09 01:44:40 -05:00
#include "engine.h"
2022-08-16 04:19:16 -04:00
#include "../ta-log.h"
2021-12-09 01:44:40 -05:00
static DivPattern emptyPat;
DivPattern::DivPattern() {
memset(data,-1,DIV_MAX_ROWS*DIV_MAX_COLS*sizeof(short));
for (int i=0; i<DIV_MAX_ROWS; i++) {
2021-12-13 17:09:46 -05:00
data[i][0]=0;
data[i][1]=0;
}
2021-12-09 01:44:40 -05:00
}
DivPattern* DivChannelData::getPattern(int index, bool create) {
if (data[index]==NULL) {
if (create) {
data[index]=new DivPattern;
} else {
return &emptyPat;
}
}
return data[index];
}
2022-08-16 04:19:16 -04:00
std::vector<std::pair<int,int>> DivChannelData::optimize() {
std::vector<std::pair<int,int>> ret;
for (int i=0; i<DIV_MAX_PATTERNS; i++) {
2021-12-15 17:32:08 -05:00
if (data[i]!=NULL) {
2022-08-16 04:19:16 -04:00
// compare
for (int j=0; j<DIV_MAX_PATTERNS; j++) {
2022-08-16 04:19:16 -04:00
if (j==i) continue;
if (data[j]==NULL) continue;
if (memcmp(data[i]->data,data[j]->data,DIV_MAX_ROWS*DIV_MAX_COLS*sizeof(short))==0) {
2022-08-16 04:19:16 -04:00
delete data[j];
data[j]=NULL;
logV("%d == %d",i,j);
ret.push_back(std::pair<int,int>(j,i));
}
}
2021-12-15 17:32:08 -05:00
}
}
2022-08-16 04:19:16 -04:00
return ret;
2021-12-15 17:32:08 -05:00
}
2022-08-16 04:19:16 -04:00
std::vector<std::pair<int,int>> DivChannelData::rearrange() {
std::vector<std::pair<int,int>> ret;
for (int i=0; i<DIV_MAX_PATTERNS; i++) {
2022-08-16 04:19:16 -04:00
if (data[i]==NULL) {
for (int j=i; j<DIV_MAX_PATTERNS; j++) {
2022-08-16 04:19:16 -04:00
if (data[j]!=NULL) {
data[i]=data[j];
data[j]=NULL;
logV("%d -> %d",j,i);
ret.push_back(std::pair<int,int>(j,i));
if (++i>=DIV_MAX_PATTERNS) break;
2022-08-16 04:19:16 -04:00
}
2022-01-16 23:21:27 -05:00
}
}
2022-08-16 04:19:16 -04:00
}
return ret;
}
2022-01-16 23:21:27 -05:00
2022-08-16 04:19:16 -04:00
void DivChannelData::wipePatterns() {
for (int i=0; i<DIV_MAX_PATTERNS; i++) {
2022-08-16 04:19:16 -04:00
if (data[i]!=NULL) {
delete data[i];
data[i]=NULL;
2022-01-16 23:21:27 -05:00
}
}
2022-08-16 04:19:16 -04:00
}
2022-01-16 23:21:27 -05:00
2022-08-16 04:19:16 -04:00
void DivPattern::copyOn(DivPattern* dest) {
dest->name=name;
memcpy(dest->data,data,sizeof(data));
2022-01-16 23:21:27 -05:00
}
2021-12-13 17:09:46 -05:00
DivChannelData::DivChannelData():
effectCols(1) {
memset(data,0,DIV_MAX_PATTERNS*sizeof(void*));
2021-12-09 01:44:40 -05:00
}