Skip to content

Commit

Permalink
Use SDL's built-in string functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Jun 23, 2023
1 parent f43923b commit def29be
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 84 deletions.
1 change: 0 additions & 1 deletion src/graphics.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// graphics.c

#include <stdlib.h>
#include "version.h"
#include "SDLU.h"
#include "main.h"
Expand Down
4 changes: 0 additions & 4 deletions src/gworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 19 additions & 23 deletions src/hiscore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -210,8 +206,8 @@ void ShowHiscore( void )
// 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 );
Expand Down Expand Up @@ -273,7 +269,7 @@ void ShowHiscore( void )

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 );
Expand Down Expand Up @@ -313,7 +309,7 @@ void SubmitCombo( Combo *in )
if( in->value > best.value && in->value > evenBetter.value )
{
PlayMono( kContinueSnd );
memcpy( &evenBetter, in, sizeof( Combo ) );
SDL_memcpy( &evenBetter, in, sizeof( Combo ) );
}
}

Expand All @@ -337,7 +333,7 @@ void ShowBestCombo( void )
levelCap = kLevels;
if( (level < 1 || level > levelCap) && level != kTutorialLevel )
{
memcpy( &best, &defaultBest, sizeof(best) );
SDL_memcpy( &best, &defaultBest, sizeof(best) );
showStartMenu = true;
return;
}
Expand All @@ -354,8 +350,8 @@ void ShowBestCombo( void )
{
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;
Expand All @@ -368,7 +364,7 @@ void ShowBestCombo( void )

SDLU_ReleaseSurface( backdropSurface );

memcpy( grid[0], best.grid, kGridAcross * kGridDown );
SDL_memcpy( grid[0], best.grid, kGridAcross * kGridDown );
ResolveSuction( 0 );
RedrawBoardContents( 0 );
RefreshAll( );
Expand Down Expand Up @@ -411,8 +407,8 @@ void AddHiscore( int score )
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;
}
Expand All @@ -429,7 +425,7 @@ void AddHiscore( int score )
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;
Expand All @@ -440,24 +436,24 @@ void AddHiscore( int score )
{
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-- )
{
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) );
}
}

// See if best combo has been won

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 = "";
Expand All @@ -468,8 +464,8 @@ void AddHiscore( int score )
{
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) );
}
}

Expand All @@ -488,11 +484,11 @@ void AddHiscore( int score )

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) );
}
}
}
3 changes: 0 additions & 3 deletions src/keyselect.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include "SDLU.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "main.h"
Expand Down
7 changes: 3 additions & 4 deletions src/level.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// level.c

#include <stdlib.h>
#include <math.h>

#include "SDLU.h"
Expand Down Expand Up @@ -156,7 +155,7 @@ void GameStartMenu( void )
#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;
Expand Down Expand Up @@ -226,7 +225,7 @@ void GameStartMenu( void )
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];
Expand Down Expand Up @@ -367,7 +366,7 @@ void GameStartMenu( void )
{
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;
Expand Down
16 changes: 7 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,8 @@

#include "main.h"

#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

#include "hiscore.h"
#include "control.h"
Expand Down Expand Up @@ -435,10 +433,10 @@ void Error( const char* extra )
{
char error[1024];

snprintf(error, sizeof(error),
SDL_snprintf(error, sizeof(error),
"Sorry, a critical error has occurred. Please report the following error message:\n %s", extra);

fprintf(stderr, "%s\n", error);
// fprintf(stderr, "%s\n", error);

SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Candy Crisis", error, NULL);

Expand Down Expand Up @@ -604,11 +602,11 @@ const char* QuickResourceName( const char* prefix, int id, const char* extension
static char name[1024];
if (id)
{
snprintf( name, sizeof(name), "%s%s_%d%s", candyCrisisResources, prefix, id, extension );
SDL_snprintf( name, sizeof(name), "%s%s_%d%s", candyCrisisResources, prefix, id, extension );
}
else
{
snprintf( name, sizeof(name), "%s%s%s", candyCrisisResources, prefix, extension );
SDL_snprintf( name, sizeof(name), "%s%s%s", candyCrisisResources, prefix, extension );
}

return name;
Expand All @@ -623,7 +621,7 @@ static MBoolean CheckDataPath(void)
void Initialize(void)
{
#if _WIN32
snprintf(candyCrisisResources, sizeof(candyCrisisResources), "CandyCrisisResources\\");
SDL_snprintf(candyCrisisResources, sizeof(candyCrisisResources), "CandyCrisisResources\\");
#elif __APPLE__
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];

Expand All @@ -637,15 +635,15 @@ void Initialize(void)
// go up two levels
for (int i = 0; i < 2; i++)
{
char* slashPos = strrchr(pathbuf, '/');
char* slashPos = SDL_strrchr(pathbuf, '/');
if (!slashPos)
{
Error("can't find Resources folder");
}
*slashPos = '\0';
}

snprintf(candyCrisisResources, sizeof(candyCrisisResources), "%s/Resources/", pathbuf);
SDL_snprintf(candyCrisisResources, sizeof(candyCrisisResources), "%s/Resources/", pathbuf);
#else
char* basePath = SDL_GetBasePath();

Expand Down
1 change: 0 additions & 1 deletion src/music.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// music.c

#include <string.h>
#include "main.h"
#include "music.h"
#include "gworld.h"
Expand Down
2 changes: 0 additions & 2 deletions src/opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "SDLU.h"

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "main.h"
#include "level.h"
Expand Down
14 changes: 5 additions & 9 deletions src/pause.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -547,8 +543,8 @@ static void DrawContinueContents( int *item, int shade )
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 );

Expand Down Expand Up @@ -644,7 +640,7 @@ static void DrawHiScoreContents( int *item, int shade )
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 );
Expand Down Expand Up @@ -830,7 +826,7 @@ static MBoolean ContinueSelected( int *item, unsigned char inKey, SDL_Keycode in

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 )
Expand Down Expand Up @@ -907,7 +903,7 @@ static MBoolean ControlsSelected( int *item, unsigned char inKey, SDL_Keycode in

case kControlsReset:
PlayMono(kClick);
memcpy(playerKeys, defaultPlayerKeys, sizeof(playerKeys));
SDL_memcpy(playerKeys, defaultPlayerKeys, sizeof(playerKeys));
break;
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/players.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -833,7 +830,7 @@ void RetrieveBlobs( int player )

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];
Expand Down
2 changes: 0 additions & 2 deletions src/random.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// random.c

#include <stdlib.h>

#include "main.h"
#include "random.h"

Expand Down
7 changes: 2 additions & 5 deletions src/score.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include "SDLU.h"

#include <stdio.h>
#include <string.h>

#include "main.h"
#include "score.h"
#include "gworld.h"
Expand Down Expand Up @@ -95,7 +92,7 @@ void ShowScore( int player )

if( control[player] != kNobodyControl )
{
sprintf( myString, "%d", displayedScore[player] );
SDL_snprintf( myString, sizeof(myString), "%d", displayedScore[player] );

SDLU_AcquireSurface( scoreSurface );

Expand All @@ -113,7 +110,7 @@ void ShowScore( int player )
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 );
Expand Down
1 change: 0 additions & 1 deletion src/soundfx.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// soundfx.c

#include "support/cmixer.h"
#include <stdio.h>

#include "main.h"
#include "soundfx.h"
Expand Down
Loading

0 comments on commit def29be

Please sign in to comment.