furnace/src/engine/pattern.cpp

104 lines
2.7 KiB
C++
Raw Normal View History

2022-02-15 03:12:20 +00:00
/**
* Furnace Tracker - multi-system chiptune tracker
2023-01-20 00:18:40 +00:00
* Copyright (C) 2021-2023 tildearrow and contributors
2022-02-15 03:12:20 +00: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 06:44:40 +00:00
#include "engine.h"
2022-08-16 08:19:16 +00:00
#include "../ta-log.h"
2021-12-09 06:44:40 +00:00
static DivPattern emptyPat;
DivPattern::DivPattern() {
2023-04-27 06:23:54 +00:00
clear();
2021-12-09 06:44:40 +00: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 08:19:16 +00: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 22:32:08 +00:00
if (data[i]!=NULL) {
2022-08-16 08:19:16 +00:00
// compare
for (int j=0; j<DIV_MAX_PATTERNS; j++) {
2022-08-16 08:19:16 +00: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 08:19:16 +00:00
delete data[j];
data[j]=NULL;
logV("%d == %d",i,j);
ret.push_back(std::pair<int,int>(j,i));
}
}
2021-12-15 22:32:08 +00:00
}
}
2022-08-16 08:19:16 +00:00
return ret;
2021-12-15 22:32:08 +00:00
}
2022-08-16 08:19:16 +00: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 08:19:16 +00:00
if (data[i]==NULL) {
for (int j=i; j<DIV_MAX_PATTERNS; j++) {
2022-08-16 08:19:16 +00: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 08:19:16 +00:00
}
2022-01-17 04:21:27 +00:00
}
}
2022-08-16 08:19:16 +00:00
}
return ret;
}
2022-01-17 04:21:27 +00:00
2022-08-16 08:19:16 +00:00
void DivChannelData::wipePatterns() {
for (int i=0; i<DIV_MAX_PATTERNS; i++) {
2022-08-16 08:19:16 +00:00
if (data[i]!=NULL) {
delete data[i];
data[i]=NULL;
2022-01-17 04:21:27 +00:00
}
}
2022-08-16 08:19:16 +00:00
}
2022-01-17 04:21:27 +00:00
2022-08-16 08:19:16 +00:00
void DivPattern::copyOn(DivPattern* dest) {
dest->name=name;
memcpy(dest->data,data,sizeof(data));
2022-01-17 04:21:27 +00:00
}
2023-04-27 06:23:54 +00:00
void DivPattern::clear() {
memset(data,-1,DIV_MAX_ROWS*DIV_MAX_COLS*sizeof(short));
for (int i=0; i<DIV_MAX_ROWS; i++) {
data[i][0]=0;
data[i][1]=0;
}
}
2021-12-13 22:09:46 +00:00
DivChannelData::DivChannelData():
effectCols(1) {
memset(data,0,DIV_MAX_PATTERNS*sizeof(void*));
2021-12-09 06:44:40 +00:00
}