shithub: candycrisis

Download patch

ref: def29be003fc02173a96f61f6942e769914b8144
parent: f43923be18b8a246b8d5783f5df3288019b2d690
author: Iliyas Jorio <iliyas@jor.io>
date: Fri Jun 23 18:35:30 EDT 2023

Use SDL's built-in string functions

--- a/src/graphics.c
+++ b/src/graphics.c
@@ -1,6 +1,5 @@
 // graphics.c
 
-#include <stdlib.h>
 #include "version.h"
 #include "SDLU.h"
 #include "main.h"
--- a/src/gworld.c
+++ b/src/gworld.c
@@ -7,10 +7,6 @@
 #include "blitter.h"
 #include "graphics.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #define STBI_ONLY_JPEG
 #define STBI_ONLY_PNG
 #define STB_IMAGE_IMPLEMENTATION
--- a/src/hiscore.c
+++ b/src/hiscore.c
@@ -2,10 +2,6 @@
 
 #include "SDLU.h"
 
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
 #include "main.h"
 #include "gworld.h"
 #include "graphics.h"
@@ -210,8 +206,8 @@
 		// If the user holds delete while opening the high scores,
 		// clear the high score table.
 		
-		memcpy( &scores, &defaultScores, sizeof( scores ) );
-		memcpy( &best,   &defaultBest,   sizeof( best   ) );
+		SDL_memcpy( &scores, &defaultScores, sizeof( scores ) );
+		SDL_memcpy( &best,   &defaultBest,   sizeof( best   ) );
 	}
 	
 	hiScoreSurface = LoadPICTAsSurface( picBackdrop + (100 * RandomBefore(kLevels)), 32 );
@@ -273,7 +269,7 @@
 		
 		dPoint.h = 470;
 
-		stringLength = sprintf( myString, "%d", scores[count].score );
+		stringLength = SDL_snprintf( myString, sizeof(myString), "%d", scores[count].score );
 		for( length=0; length < stringLength; length++ )
 		{
 			SurfaceBlitCharacter( font, myString[length], &dPoint, r, g, b, 1 );
@@ -313,7 +309,7 @@
 	if( in->value > best.value && in->value > evenBetter.value )
 	{
 		PlayMono( kContinueSnd );
-		memcpy( &evenBetter, in, sizeof( Combo ) );
+		SDL_memcpy( &evenBetter, in, sizeof( Combo ) );
 	}	
 }
 
@@ -337,7 +333,7 @@
 	levelCap = kLevels;
 	if( (level < 1 || level > levelCap) && level != kTutorialLevel ) 
 	{
-		memcpy( &best, &defaultBest, sizeof(best) );
+		SDL_memcpy( &best, &defaultBest, sizeof(best) );
 		showStartMenu = true;
 		return;
 	}
@@ -354,9 +350,9 @@
 	{
 		SurfaceBlitCharacter( font, *scan, &dPoint, 255, 255, 255, 1 );
 	}
-		
-	sprintf( bestInfo, "%s (%d points)", best.name, best.value );
 
+	SDL_snprintf( bestInfo, sizeof(bestInfo), "%s (%d points)", best.name, best.value );
+
     font = GetFont(widescreen ? picFont : picHiScoreFont);
 	dPoint.v = widescreen? 388: 410;
 	dPoint.h = 320 - (GetTextWidth( font, bestInfo ) / 2);
@@ -368,7 +364,7 @@
 
 	SDLU_ReleaseSurface( backdropSurface );
 	
-	memcpy( grid[0], best.grid, kGridAcross * kGridDown );
+	SDL_memcpy( grid[0], best.grid, kGridAcross * kGridDown );
 	ResolveSuction( 0 );
 	RedrawBoardContents( 0 );
 	RefreshAll( );
@@ -411,8 +407,8 @@
 		for( count=0; count<=9; count++ )
 		{
 			if( score >= scores[count].score )
-			{				
-				sprintf( rank, "%d points", score );
+			{
+				SDL_snprintf( rank, sizeof(rank), "%d points", score );
 				highScoreLevel = count;
 				break;
 			}
@@ -429,7 +425,7 @@
 	if( evenBetter.value > best.value && highScoreLevel != -1 )
 	{
 		
-		sprintf( text, "You got a high score and the best combo!" );
+		SDL_snprintf( text, sizeof(text), "You got a high score and the best combo!" );
 
 		highScoreText = text;
 		highScoreRank = rank;
@@ -440,8 +436,8 @@
 		{
 			highScoreName[kNameLength] = '\0';
 
-			memcpy( &best, &evenBetter, sizeof(Combo) );
-			strcpy( best.name, highScoreName );
+			SDL_memcpy( &best, &evenBetter, sizeof(Combo) );
+			SDL_strlcpy( best.name, highScoreName, sizeof(best.name) );
 
 			for( item=8; item>=highScoreLevel; item-- )
 			{
@@ -449,7 +445,7 @@
 			}
 			
 			scores[highScoreLevel].score = score;
-			strcpy( scores[highScoreLevel].name, highScoreName );				
+			SDL_strlcpy( scores[highScoreLevel].name, highScoreName, sizeof(scores[highScoreLevel].name) );
 		}
 	}
 	
@@ -457,7 +453,7 @@
 		
 	else if( evenBetter.value > best.value )
 	{
-		sprintf( text, "Congratulations! %s got best combo!", playerName );
+		SDL_snprintf( text, sizeof(text), "Congratulations! %s got best combo!", playerName );
 		
 		highScoreText = text;
 		highScoreRank = "";
@@ -468,8 +464,8 @@
 		{
 			highScoreName[kNameLength] = '\0';
 
-			memcpy( &best, &evenBetter, sizeof(Combo) );
-			strcpy( best.name, highScoreName );
+			SDL_memcpy( &best, &evenBetter, sizeof(Combo) );
+			SDL_strlcpy( best.name, highScoreName, sizeof(best.name) );
 		}
 	}
 
@@ -488,11 +484,11 @@
 
 			for( item=8; item>=highScoreLevel; item-- )
 			{
-				memmove( &scores[item+1], &scores[item], sizeof( HighScore ) );
+				SDL_memmove( &scores[item+1], &scores[item], sizeof( HighScore ) );
 			}
 			
 			scores[highScoreLevel].score = score;
-			strcpy( scores[highScoreLevel].name, highScoreName );				
+			SDL_strlcpy( scores[highScoreLevel].name, highScoreName, sizeof(scores[highScoreLevel].name) );
 		}
 	}
 }
--- a/src/keyselect.c
+++ b/src/keyselect.c
@@ -2,9 +2,6 @@
 
 #include "SDLU.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include <ctype.h>
 
 #include "main.h"
--- a/src/level.c
+++ b/src/level.c
@@ -1,6 +1,5 @@
 // level.c
 
-#include <stdlib.h>
 #include <math.h>
 
 #include "SDLU.h"
@@ -156,7 +155,7 @@
 #endif
 	
 redo:
-	memcpy(titleItems, k_titleItemDefs, sizeof(titleItems));
+	SDL_memcpy(titleItems, k_titleItemDefs, sizeof(titleItems));
 
 	combo[0] = combo[1] = 0;
 	comboBright[0] = comboBright[1] = 0;
@@ -226,7 +225,7 @@
 			item->rect.left = dPoint.h;
 			item->rect.top = dPoint.v - 6;
 			item->rect.bottom = dPoint.v + 16 + 6;
-			int nameLength = (int) strlen(item->name);
+			int nameLength = (int) SDL_strlen(item->name);
 			for (int charNo = 0; charNo < nameLength; charNo++)
 			{
 				char c = item->name[charNo];
@@ -367,7 +366,7 @@
                 {
 				  char  number[16] = { 0 };
 				  char* scan;
-				  sprintf( number, "%d", combo[count] );
+				  SDL_snprintf( number, sizeof(number), "%d", combo[count] );
 
                   dPoint.v = meterRect[count].y + 3;
                   dPoint.h = meterRect[count].x;
binary files a/src/main.c b/src/main.c differ
--- a/src/music.c
+++ b/src/music.c
@@ -1,6 +1,5 @@
 // music.c
 
-#include <string.h>
 #include "main.h"
 #include "music.h"
 #include "gworld.h"
--- a/src/opponent.c
+++ b/src/opponent.c
@@ -3,8 +3,6 @@
 #include "SDLU.h"
 
 #include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
 
 #include "main.h"
 #include "level.h"
--- a/src/pause.c
+++ b/src/pause.c
@@ -8,10 +8,6 @@
 #include "SDLU.h"
 
 #include <math.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <ctype.h>
 
 #include "main.h"
 #include "gameticks.h"
@@ -547,9 +543,9 @@
 	static int lastCountdown = 0;
 	int index, countdown, fade;
 	int r, g, b;
-	                 
-	sprintf( line[3], "%d credit%c", credits, (credits != 1)? 's': ' ' );
 
+	SDL_snprintf( line[3], sizeof(line[3]), "%d credit%c", credits, (credits != 1)? 's': ' ' );
+
 	SDLU_AcquireSurface( drawSurface );
 
 	for( index=0; index<4; index++ )
@@ -644,7 +640,7 @@
 		SurfaceBlitCharacter( dashedLineFont, '.', &dashedLinePoint, 0, 0, 0, 0 );
 	}
 	
-	nameLength = (int) strlen(highScoreName);
+	nameLength = (int) SDL_strlen(highScoreName);
 	for( index = 0; index < nameLength; index++ )
 	{
 		SurfaceBlitCharacter( bigFont, highScoreName[index], &hPoint, 255, 255, 255, 1 );
@@ -830,7 +826,7 @@
 
 static MBoolean HiScoreSelected( int *item, unsigned char inKey, SDL_Keycode inSDLKey )
 {
-	int nameLength = (int) strlen(highScoreName);
+	int nameLength = (int) SDL_strlen(highScoreName);
 	
 	// return (SDL key)
 	if( inSDLKey == SDLK_RETURN )
@@ -907,7 +903,7 @@
 
 				case kControlsReset:
 					PlayMono(kClick);
-					memcpy(playerKeys, defaultPlayerKeys, sizeof(playerKeys));
+					SDL_memcpy(playerKeys, defaultPlayerKeys, sizeof(playerKeys));
 					break;
 			}
 		}
--- a/src/players.c
+++ b/src/players.c
@@ -23,9 +23,6 @@
 #include "score.h"
 #include "hiscore.h"
 
-#include <string.h>
-#include <stdlib.h>
-
 unsigned int  boredTime[2], hintTime[2], fadeCharTime[2], animTime[2], shadowDepth[2], hintGlow, messageTime;
 int emotions[2];
 int glowColors[][3] = { { 0,  0,  0},
@@ -833,7 +830,7 @@
 	
 	if( control[player] == kPlayerControl )
 	{
-		memcpy( potentialCombo[player].grid, grid[player], kGridAcross * kGridDown );
+		SDL_memcpy( potentialCombo[player].grid, grid[player], kGridAcross * kGridDown );
 		potentialCombo[player].a = colorA[player];
 		potentialCombo[player].b = colorB[player];
 		potentialCombo[player].m = magic[player];
--- a/src/random.c
+++ b/src/random.c
@@ -1,7 +1,5 @@
 // random.c
 
-#include <stdlib.h>
-
 #include "main.h"
 #include "random.h"
 
--- a/src/score.c
+++ b/src/score.c
@@ -2,9 +2,6 @@
 
 #include "SDLU.h"
 
-#include <stdio.h>
-#include <string.h>
-
 #include "main.h"
 #include "score.h"
 #include "gworld.h"
@@ -95,7 +92,7 @@
 	
 	if( control[player] != kNobodyControl )
 	{
-		sprintf( myString, "%d", displayedScore[player] );
+		SDL_snprintf( myString, sizeof(myString), "%d", displayedScore[player] );
 				
 		SDLU_AcquireSurface( scoreSurface );
 		
@@ -113,7 +110,7 @@
 		myRect = scoreWindowZRect;
 		myRect.right -= 2;
 		myRect.left = myRect.right - kNumberHorizSize;
-		for( count = (int) strlen(myString) - 1; count >= 0; count-- )
+		for( count = (int) SDL_strlen(myString) - 1; count >= 0; count-- )
 		{
 			DrawCharacter( myString[count], &myRect );
 			OffsetMRect( &myRect, -kNumberHorizSize - 1, 0 );
--- a/src/soundfx.c
+++ b/src/soundfx.c
@@ -1,7 +1,6 @@
 // soundfx.c
 
 #include "support/cmixer.h"
-#include <stdio.h>
 
 #include "main.h"
 #include "soundfx.h"
--- a/src/tutorial.c
+++ b/src/tutorial.c
@@ -16,9 +16,6 @@
 #include "opponent.h"
 #include "keyselect.h"
 
-#include <string.h>
-#include <stdio.h>
-
 AutoPattern tutorialPattern[] =
 {
 	{ kMessage,        0,   0,   "Welcome to the\nCandy Crisis\ntutorial!" },
@@ -392,10 +389,10 @@
 	SDL_Rect    balloonSDLRect, balloonContentsSDLRect;
 	MRect       balloonContentsRect;
 	
-	strcpy( balloonMsg, message );
+	SDL_strlcpy( balloonMsg, message, sizeof(balloonMsg) );
 	for( replace=0; replace<4; replace++ )
 	{
-		search = strstr( balloonMsg, match[replace] );
+		search = SDL_strstr( balloonMsg, match[replace] );
 		if( search )
 		{
 			char temp[256];
@@ -402,8 +399,8 @@
 			
 			search[0] = '%';
 			search[1] = 's';
-			sprintf( temp, balloonMsg, SDL_GetKeyName( playerKeys[1][replace] ) );
-			strcpy( balloonMsg, temp );
+			SDL_snprintf( temp, sizeof(temp), balloonMsg, SDL_GetKeyName( playerKeys[1][replace] ) );
+			SDL_strlcpy( balloonMsg, temp, sizeof(balloonMsg) );
 		}
 	}
 	
--- a/src/victory.c
+++ b/src/victory.c
@@ -23,9 +23,6 @@
 #include "pause.h"
 #include "font.h"
 #include <math.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
 
 MTicks winTime, loseTime;
 int winStage, loseStage;
@@ -268,7 +265,7 @@
 		char seconds[20];
 		char *scan = seconds;
 		
-		sprintf( seconds, "%d", (endTime - startTime) / 60 );
+		SDL_snprintf( seconds, sizeof(seconds), "%d", (endTime - startTime) / 60 );
 		while( *scan )
 		{
 			SurfaceBlitCharacter( zapFont, *scan++, &dPoint, 255, 255, 255, 1  );
@@ -322,7 +319,7 @@
 		char points[20];
 		char *scan = points;
 		
-		sprintf( points, "%d", bonus );
+		SDL_snprintf( points, sizeof(points), "%d", bonus );
 		while( *scan )
 		{
 			SurfaceBlitCharacter( zapFont, *scan++, &dPoint, 255, 255, 255, 1  );
--- a/src/zap.c
+++ b/src/zap.c
@@ -2,8 +2,6 @@
 
 #include "SDLU.h"
 
-#include <stdio.h>
-
 #include "main.h"
 #include "players.h"
 #include "zap.h"
@@ -22,7 +20,6 @@
 
 #include <stdlib.h>
 #include <math.h>
-#include <stdio.h>
 
 signed char death[2][kGridAcross][kGridDown];
 int zapIteration[2];
@@ -52,7 +49,7 @@
 		zapScoreG[player] = glowColors[c][1];
 		zapScoreB[player] = glowColors[c][2];
 
-	    sprintf( zapScore[player], (multiplier == 1)? "%d": "%d*%d", amount, multiplier );
+		SDL_snprintf( zapScore[player], sizeof(zapScore[player]), (multiplier == 1)? "%d": "%d*%d", amount, multiplier );
 
 		zapScoreWidth[player] = 0;
 		scan = zapScore[player];