1164 lines · 25.1 KB · C
RawDownload
// for finding memory leaks in debug mode with Visual Studio #if defined _DEBUG && defined _MSC_VER#include <crtdbg.h>#endif #include <stdio.h>#include <string.h>#include <stdlib.h>#include <SDL2/SDL.h>#include "tinyfiledialogs/tinyfiledialogs.h"#include "config.h"#include "palette.h"#include "gui.h" // number of frames#define BUTTON_DELAY 15#define BUTTON_REPEAT_DELAY 2 static bool rainbowHeldDown, rainbowDownHeldDown, rainbowUpHeldDown, spreadFlag;static uint8_t whichSliderHeldDown, spreadFrom;static uint16_t oldColorVal;static uint32_t buttonCounter, randSeed; extern const uint32_t arrowDown[42];extern const uint32_t arrowUp[42];extern const uint32_t editText[42880];extern const uint8_t visualsData[31680];extern const uint8_t spectrumData[11000];extern const uint8_t samplerData[42880];extern const uint8_t songNameData[7040];extern const uint8_t patternEditorData[42880];extern const uint8_t diskOpData[31680];extern const uint8_t fontData[5168]; // globalizedbool topScreenShown = false, bottomScreenShown = false, analyzerShown = false;uint8_t currColor = 0, rainbowPos = 0, colorsMax = 48;uint16_t *theRightColors = vuColors; // Delphi/Pascal LCG Random()static inline uint32_t myRandom(uint32_t limit){	randSeed = randSeed * 134775813 + 1;	return ((int64_t)randSeed * limit) >> 32;} static void blit(const uint8_t *srcPtr, int32_t xPos, int32_t yPos, int32_t w, int32_t h){	uint32_t pixel; 	uint32_t *dstPtr = &frameBuffer[(yPos * SCREEN_W) + xPos];	for (int32_t y = 0; y < h; y++)	{		for (int32_t x = 0; x < w; x++)		{			const uint8_t pal = srcPtr[x]; 				 if (pal ==  8) pixel = 0x373737; // sample middle line			else if (pal ==  9) pixel = 0x666666; // sample mark #1			else if (pal == 10) pixel = 0xCCCCCC; // sample mark #2			else pixel = RGB12_to_RGB24(palette[pal]); 			dstPtr[x] = pixel;		} 		srcPtr += w;		dstPtr += SCREEN_W;	}} static void blit32(int32_t xPos, int32_t yPos, int32_t w, int32_t h, const uint32_t *srcPtr){	uint32_t *dstPtr = &frameBuffer[(yPos * SCREEN_W) + xPos];	for (int32_t y = 0; y < h; y++)	{		memcpy(dstPtr, srcPtr, w * sizeof (int32_t)); 		srcPtr += w;		dstPtr += SCREEN_W;	}} void charOut(int32_t xPos, int32_t yPos, uint32_t color, char chr){	const uint8_t *srcPtr = &fontData[(chr * (8 * 5)) + 1];	uint32_t *dstPtr = &frameBuffer[(yPos * SCREEN_W) + xPos]; 	for (int32_t y = 0; y < 5; y++)	{		for (int32_t x = 0; x < 6; x++)		{			if (srcPtr[x])				dstPtr[x] = color;		} 		srcPtr += 8;		dstPtr += SCREEN_W;	}} void textOut(int32_t xPos, int32_t yPos, uint32_t color, const char *text){	int32_t x = xPos;	while (*text != '\0')	{		charOut(x, yPos, color, *text++);		x += 7;	}} void textOutShadow(int32_t xPos, int32_t yPos, uint32_t fgColor, uint32_t bgColor, const char *text){	int32_t x = xPos;	while (*text != '\0')	{		charOut(x+1, yPos+1, bgColor, *text);		charOut(x+0, yPos+0, fgColor, *text); 		text++;		x += 7;	}} void hLine(int32_t x, int32_t y, int32_t w, uint32_t color){#ifdef _DEBUG	if (x >= SCREEN_W || y >= SCREEN_H || x+w > SCREEN_W)		__debugbreak();#endif 	uint32_t *dstPtr = &frameBuffer[(y * SCREEN_W) + x];	for (int32_t i = 0; i < w; i++)		dstPtr[i] = color;} void vLine(int32_t x, int32_t y, int32_t h, uint32_t color){#ifdef _DEBUG	if (y >= SCREEN_W || x >= SCREEN_W || y+h > SCREEN_H)		__debugbreak();#endif 	uint32_t *dstPtr = &frameBuffer[(y * SCREEN_W) + x];	for (int32_t i = 0; i < h; i++)	{		*dstPtr  = color;		 dstPtr += SCREEN_W;	}} static void drawSpectrumAnalyzer(int32_t xPos, int32_t yPos){	randSeed = 8675309; 	int32_t drawX = xPos + 129;	for (int32_t i = 0; i < 23; i++)	{		const int32_t h = myRandom(35+1); 		int32_t drawY = 35 - h;		const uint32_t *srcPtr = &spectrumAnalyzerBMP[h];		uint32_t *dstPtr = &frameBuffer[((yPos+drawY+59) * SCREEN_W) + drawX]; 		for (int32_t y = 0; y <= h; y++)		{			for (int32_t x = 0; x < 6; x++)				dstPtr[x] = *srcPtr; 			dstPtr += SCREEN_W;			srcPtr--;		} 		drawX += 8;	}} static void drawVUMeters(int32_t xPos, int32_t yPos){	randSeed = 81549300; 	int32_t drawX = xPos + 55;	for (int32_t i = 0; i < 4; i++)	{		int32_t h = myRandom(48+1); 		int32_t drawY  = 48 - h;		const uint32_t *srcPtr = &vuMeterBMP[((48 - 1) * 10) - (drawY * 10)];		uint32_t *dstPtr = &frameBuffer[((yPos+drawY+140) * SCREEN_W) + drawX]; 		for (int32_t y = 0; y < h; y++)		{			for (int32_t x = 0; x < 10; x++)				dstPtr[x]= srcPtr[x]; 			dstPtr += SCREEN_W;			srcPtr -= 10;		} 		drawX += 72;	}} static void drawPatternCursor(int32_t xPos, int32_t yPos){	const uint32_t *srcPtr = patternCursorBMP;	uint32_t *dstPtr = &frameBuffer[((yPos+188) * SCREEN_W) + (xPos+30)]; 	for (int32_t y = 0; y < 14; y++)	{		for (int32_t x = 0; x < 11; x++)		{			const uint32_t pixel = srcPtr[x];			if (pixel != RGB24_COLORKEY)				dstPtr[x] = pixel;		} 		srcPtr += 11;		dstPtr += SCREEN_W;	}} void drawTracker(void){	const int32_t x = TRACKER_X;	const int32_t y = TRACKER_Y; 	if (!topScreenShown)		blit(visualsData, x, y, 320, 99);	else		blit(diskOpData,  x, y, 320, 99); 	if (!topScreenShown && analyzerShown)	{		blit(spectrumData, x+120, y+44, 200, 55);		drawSpectrumAnalyzer(x, y);	} 	blit(songNameData, x, y+99, 320, 22); 	if (!bottomScreenShown)		blit(patternEditorData, x, y+121, 320, 134);	else		blit(samplerData, x, y+121, 320, 134); 	if (!bottomScreenShown)	{		drawVUMeters(x, y);		drawPatternCursor(x, y);	}} void handleSlidersHeldDown(void){	int8_t value;	int32_t mx; 	if (whichSliderHeldDown == 0)		return; // no slider held down 	if (whichSliderHeldDown <= 3)	{		// palette RGB sliders 		mx = mouse.x - (COLORPICKER1_X+26);		if (mx < 0)			mx = 0; 		value = (int8_t)(mx / 3);		if (value > 15)			value = 15; 		oldColorVal = palette[currColor]; 		palette[currColor] &= ~(15    << (4 * (3 - whichSliderHeldDown)));		palette[currColor] |=  (value << (4 * (3 - whichSliderHeldDown))); 		if (palette[currColor] != oldColorVal)		{			configIsSaved = false; 			if (currColor == 5)				updatePatternCursorBMP(); 			drawColorPicker1();			drawTracker();			redrawScreen = true;		}	}	else if (whichSliderHeldDown <= 6)	{		// visualizer RGB sliders 		mx = mouse.x - (COLORPICKER2_X+26);		if (mx < 0)			mx = 0; 		value = (int8_t)(mx / 3);		if (value > 15)			value = 15; 		oldColorVal = theRightColors[rainbowPos]; 		theRightColors[rainbowPos] &= ~(15    << (4 * (6 - whichSliderHeldDown)));		theRightColors[rainbowPos] |=  (value << (4 * (6 - whichSliderHeldDown))); 		if (theRightColors[rainbowPos] != oldColorVal)		{			configIsSaved = false; 			if (theRightColors == vuColors)				updateVuMeterBMP();			else				updateSpectrumAnalyzerBMP(); 			drawColorPicker2();			drawTracker();			redrawScreen = true;		}	}} void handleRainbowHeldDown(void){	if (!rainbowHeldDown)		return; 	int32_t my = mouse.y - (COLORPICKER2_Y+3); 	     if (my <  0) my = 0;	else if (my > 47) my = 47; 	if (theRightColors == analyzerColors && my > 35)		my = 35; 	uint8_t oldOffset = rainbowPos; 	rainbowPos = (uint8_t)my;	if (rainbowPos != oldOffset)	{		setUndo2Color(rainbowPos);		drawColorPicker2();		redrawScreen = true;	}} static void rainbowUp(void){	uint16_t tmp = *theRightColors;	for (int32_t i = 0; i < colorsMax-1; i++)		theRightColors[i] = theRightColors[i+1];	theRightColors[colorsMax-1] = tmp; 	if (theRightColors == vuColors)		updateVuMeterBMP();	else		updateSpectrumAnalyzerBMP(); 	drawColorPicker2();	drawTracker();	redrawScreen = true;} static void rainbowDown(void){	uint16_t tmp = theRightColors[colorsMax-1];	for (int32_t i = colorsMax-1; i >= 1; i--)		theRightColors[i] = theRightColors[i-1];	theRightColors[0] = tmp; 	if (theRightColors == vuColors)		updateVuMeterBMP();	else		updateSpectrumAnalyzerBMP(); 	drawColorPicker2();	drawTracker();	redrawScreen = true;} static void rainbowSpread(void){	spreadFlag = true;	spreadFrom = rainbowPos;} #ifndef __APPLE__static void colorPicker1(void){	const uint16_t color = colorPicker(palette[currColor]);	if (color != 0xFFFF)	{		palette[currColor] = color & 0xFFF; 		if (currColor == 5)			updatePatternCursorBMP(); 		drawColorPicker1();		drawTracker();		redrawScreen = true;		configIsSaved = false;	}} static void colorPicker2(void){	const uint16_t color = colorPicker(theRightColors[rainbowPos]);	if (color != 0xFFFF)	{		theRightColors[rainbowPos] = color & 0xFFF;		if (theRightColors == vuColors)			updateVuMeterBMP();		else			updateSpectrumAnalyzerBMP(); 		drawColorPicker2();		drawTracker();		redrawScreen = true;		configIsSaved = false;	}}#endif static bool handleColorPicker1(int32_t mx, int32_t my){	uint8_t newColor; 	// color picker #1 buttons	if (mx >= COLORPICKER1_X && mx < COLORPICKER1_X+108 && my >= COLORPICKER1_Y+33 && my < COLORPICKER1_Y+44)	{		if (mx >= COLORPICKER1_X+79) // "DEF"		{			configIsSaved = false;			setDefaultPalette();			updatePatternCursorBMP();			drawTracker();			drawColorPicker1();			redrawScreen = true;			return true;		}		else if (mx >= COLORPICKER1_X+33) // "CANCEL"		{			configIsSaved = false;			cancel1Color();			updatePatternCursorBMP();			drawTracker();			drawColorPicker1();			redrawScreen = true;			return true;		}		else // "UNDO"		{			configIsSaved = false;			undo1Color();			updatePatternCursorBMP();			drawTracker();			drawColorPicker1();			redrawScreen = true;			return true;		}	} 	// color picker #1 sliders	if (mx >= COLORPICKER1_X+0 && mx < COLORPICKER1_X+79)	{		if (my >= COLORPICKER1_Y+0 && my < COLORPICKER1_Y+11)		{			whichSliderHeldDown = 1; // #1 R			return true;		}		else if (my >= COLORPICKER1_Y+11 && my < COLORPICKER1_Y+22)		{			whichSliderHeldDown = 2; // #1 G			return true;		}		else if (my >= COLORPICKER1_Y+22 && my < COLORPICKER1_Y+33)		{			whichSliderHeldDown = 3; // #1 B			return true;		}	} 	// color picker #1 colors	if (mx >= COLORPICKER1_X+82 && my >= COLORPICKER1_Y+5 && my < COLORPICKER1_Y+28)	{		if (mx < (COLORPICKER1_X+92))		{			// left colors			newColor = (uint8_t)((my - (COLORPICKER1_Y+5)) / 6);			if (currColor != newColor)			{				currColor = newColor;				setUndo1Color(currColor);				drawColorPicker1();				redrawScreen = true;			} 			return true;		}		else if (mx >= COLORPICKER1_X+95 && mx < COLORPICKER1_X+105)		{

Lines 1–500 of 1164.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t