home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
369b.lha
/
MED_v2.0
/
Source
/
medsrc
/
med-buff.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-05-03
|
4KB
|
132 lines
/* med-buff.c: MED buffer handling routines, by Teijo Kinnunen 1990 */
#include <math.h> /* min(), abs() */
#include "med.h"
static UBYTE *buffer, bufftracks, bufflines;
static UBYTE markblock, marktrack, markline;
extern UWORD soittolohko,currtrk,soittorivi,lohkoja;
extern void __asm PaivitaNaytto(register __d0 BOOL);
extern struct Lohko *lohko[];
void FreeBuffer()
{
if(!buffer) return;
FreeMem((void *)buffer, bufftracks * bufflines * 3);
bufftracks = bufflines = 0;
buffer = NULL;
}
BOOL AllocBuffer(UBYTE tracks,UBYTE lines) /* FALSE = ok, TRUE = error */
{
FreeBuffer(); /* free the previous buffer */
if(!(buffer = AllocMem(tracks * lines * 3,MEMF_PUBLIC))) return(TRUE);
bufftracks = tracks;
bufflines = lines;
return(FALSE);
}
void CutCopyPaste(UBYTE block,UBYTE startline,UBYTE lines,UBYTE starttrack,
UBYTE tracks,UBYTE op) /* op: 0 = copy, 1 = cut, 2 = paste, 3 = swap */
{
UBYTE ntrks = lohko[block]->numtracks; /* tracks in the block */
UBYTE *p1 = &lohko[block]->music[startline * ntrks * 3 +
starttrack * 3],*p2,linecnt,trkcnt,swb;
UBYTE *linep1,*linep2;
if(starttrack + tracks > ntrks) tracks = ntrks - starttrack;
if(startline + lines > 64) lines = 64 - startline;
if((op == 0 || op == 1) && AllocBuffer(tracks,lines)) { Ilmoita("No memory!!"); return; }
else if(op >= 2) {
if(lines > bufflines) lines = bufflines;
if(tracks > bufftracks) tracks = bufftracks;
}
if(!(p2 = buffer)) { Ilmoita("The buffer is empty."); return; }
for(linecnt = 0; linecnt < lines; linecnt++) {
linep1 = p1; linep2 = p2;
for(trkcnt = 0; trkcnt < tracks; trkcnt++) {
if(op == 0 || op == 1) { /* cut/copy */
*p2=*p1; *(p2+1)=*(p1+1); *(p2+2)=*(p1+2);
if(op == 1) *p1 = *(p1+1) = *(p1+2) = 0;
} else if(op == 2) { /* paste */
*p1=*p2; *(p1+1)=*(p2+1); *(p1+2)=*(p2+2);
} else if(op == 3) { /* swap */
swb = *p1; *p1 = *p2; *p2 = swb;
swb=*(p1+1); *(p1+1)=*(p2+1); *(p2+1)=swb;
swb=*(p1+2); *(p1+2)=*(p2+2); *(p2+2)=swb;
}
p1 += 3; p2 += 3; /* advance pointers one note */
}
p1 = linep1 + 3 * ntrks; /* advance blockptr one line */
p2 = linep2 + 3 * bufftracks; /* advance buffptr 1 line */
}
}
void SetMark()
{
markblock = (UBYTE)soittolohko;
markline = (UBYTE)soittorivi;
marktrack = (UBYTE)currtrk;
}
void HandleMark(UBYTE code)
{
UBYTE block = soittolohko,line = soittorivi,track = currtrk;
if(markblock != soittolohko && code != 0x34) {
Ilmoita("Only one block at a time!!"); return;
}
if(code == 0x34) CutCopyPaste((UBYTE)block,(UBYTE)line,
bufflines,(UBYTE)currtrk,bufftracks,2);
else CutCopyPaste((UBYTE)block,(UBYTE)min(markline,line),
(UBYTE)(abs(markline-line)+1),(UBYTE)min(marktrack,track),
(UBYTE)(abs(marktrack - track) + 1),
(UBYTE)(code == 0x32 ? 1 : 0));
PaivitaNaytto(TRUE);
}
void ExpandBlock(UWORD block)
{
UBYTE linecnt,prevblk = (UBYTE)lohkoja,tracks;
if(lohkoja >= 100) {
Ilmoita("Too many blocks!!"); return;
}
tracks = lohko[block]->numtracks;
NewBlockHere(block+1,tracks);
if(lohkoja != prevblk+1 || lohko[block+1]->numtracks != tracks) {
Ilmoita("Out of memory!!"); return; }
for(linecnt = 0; linecnt < 64; linecnt++) {
if(linecnt & 0x1) /* clear odd-numbered lines */
memset(&lohko[block+1]->music[linecnt * 3 * tracks],
0,3 * tracks);
else memcpy(&lohko[block+1]->music[linecnt * 3 * tracks],
&lohko[block]->music[(32 + linecnt / 2) *
3 * tracks], 3 * tracks);
}
for(linecnt = 63; linecnt; linecnt--) {
if(linecnt & 0x1)
memset(&lohko[block]->music[linecnt * 3 * tracks],
0,3 * tracks);
else memcpy(&lohko[block]->music[linecnt * 3 * tracks],
&lohko[block]->music[(linecnt / 2) * 3 * tracks],
3 * tracks);
}
PaivitaNaytto(TRUE);
TulostaLohkoJaSoitin();
}
void ShrinkBlock(UWORD block)
{
UBYTE linecnt,tracks = lohko[block]->numtracks;
if(!lohko[block+1]) { Ilmoita("There's no second block."); return; }
if(tracks != lohko[block+1]->numtracks) {
Ilmoita("Not the same number of tracks!!"); return; }
for(linecnt = 0; linecnt < 64; linecnt++) {
if(linecnt < 32) memcpy(&lohko[block]->music[
linecnt * 3 * tracks],&lohko[block]->music[
linecnt * 6 * tracks],3 * tracks);
else memcpy(&lohko[block]->music[linecnt * 3 * tracks],
&lohko[block+1]->music[(linecnt - 32) *
6 * tracks], 3 * tracks);
}
PaivitaNaytto(TRUE);
TulostaLohkoJaSoitin();
}