shithub: ft²

Download patch

ref: 00e1d902b0391ce80e40e42071abcf6a95314ff8
parent: d5b304816fbd5df3153be0518b6490d69aee5de1
author: Olav Sørensen <olav.sorensen@live.no>
date: Thu Aug 3 18:42:19 EDT 2023

Fix heavily bugged "pattern expand" routine

--- a/src/ft2_pattern_ed.c
+++ b/src/ft2_pattern_ed.c
@@ -23,7 +23,6 @@
 #include "ft2_bmp.h"
 #include "ft2_structs.h"
 
-pattMark_t pattMark; // globalized
 
 // for pattern marking w/ keyboard
 static int8_t lastChMark;
@@ -42,6 +41,10 @@
 static int32_t lastMouseX, lastMouseY;
 static int32_t last_TimeH, last_TimeM, last_TimeS;
 
+static note_t tmpPattern[MAX_CHANNELS * MAX_PATT_LEN];
+
+pattMark_t pattMark; // globalized
+
 bool allocatePattern(uint16_t pattNum) // for tracker use only, not in loader!
 {
 	const bool audioWasntLocked = !audio.locked;
@@ -2704,8 +2707,7 @@
 	note_t *p = pattern[editor.editPattern];
 	if (p != NULL)
 	{
-		const int32_t length = numRows >> 1;
-		for (int32_t i = 0; i < length; i++)
+		for (int32_t i = 0; i < numRows / 2; i++)
 		{
 			for (int32_t j = 0; j < MAX_CHANNELS; j++)
 				p[(i * MAX_CHANNELS) + j] = p[((i*2) * MAX_CHANNELS) + j];
@@ -2712,13 +2714,13 @@
 		}
 	}
 
-	patternNumRows[editor.editPattern] >>= 1;
+	patternNumRows[editor.editPattern] /= 2;
 	numRows = patternNumRows[editor.editPattern];
 
 	if (song.pattNum == editor.editPattern)
 		song.currNumRows = numRows;
 
-	song.row >>= 1;
+	song.row /= 2;
 	if (song.row >= numRows)
 		song.row = numRows-1;
 
@@ -2734,7 +2736,7 @@
 void expandPattern(void)
 {
 	int16_t numRows = patternNumRows[editor.editPattern];
-	if (numRows > 128)
+	if (numRows > MAX_PATT_LEN/2)
 	{
 		okBox(0, "System message", "Pattern is too long to be expanded.");
 	}
@@ -2744,24 +2746,16 @@
 
 		if (pattern[editor.editPattern] != NULL)
 		{
-			note_t *tmpPtn = (note_t *)malloc((numRows * 2) * TRACK_WIDTH);
-			if (tmpPtn == NULL)
-			{
-				unlockMixerCallback();
-				okBox(0, "System message", "Not enough memory!");
-				return;
-			}
-
+			note_t *p = pattern[editor.editPattern];
+			memcpy(tmpPattern, p, numRows * TRACK_WIDTH);
+			
 			for (int32_t i = 0; i < numRows; i++)
 			{
 				for (int32_t j = 0; j < MAX_CHANNELS; j++)
-					tmpPtn[((i * 2) * MAX_CHANNELS) + j] = pattern[editor.editPattern][(i * MAX_CHANNELS) + j];
+					p[((i * 2) * MAX_CHANNELS) + j] = tmpPattern[(i * MAX_CHANNELS) + j];
 
-				memset(&tmpPtn[((i * 2) + 1) * MAX_CHANNELS], 0, TRACK_WIDTH);
+				memset(&p[((i * 2) + 1) * MAX_CHANNELS], 0, TRACK_WIDTH);
 			}
-
-			free(pattern[editor.editPattern]);
-			pattern[editor.editPattern] = tmpPtn;
 		}
 
 		patternNumRows[editor.editPattern] *= 2;