Skip to content

Commit

Permalink
Fix major segmentation fault bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary-Rude committed Oct 9, 2023
1 parent 1c60ef3 commit 9e73b76
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*.elf
*.3dsx
*.3ds
*.cxi
*.smdh
romfs/
build/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ CFLAGS := -g -Wall -O2 -mword-relocations \
-fomit-frame-pointer -ffunction-sections \
$(ARCH)

CFLAGS += $(INCLUDE) -DARM11 -D_3DS
CFLAGS += $(INCLUDE) -DARM11 -D_3DS -ggdb -Og

CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11

Expand Down
4 changes: 2 additions & 2 deletions game.rsf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BasicInfo:
Title : "Snake"
ProductCode : "CTR-S-SNAK" # You can make this whatever you want, it doesn't have to follow the Nintendo standard
ProductCode : "CTR-P-SNAK" # You can make this whatever you want, it doesn't have to follow the Nintendo standard
Logo : Licensed # Nintendo / Licensed / Distributed / iQue / iQueForSystem

#RomFs:
Expand All @@ -9,7 +9,7 @@ BasicInfo:

TitleInfo:
Category : Application
UniqueId : 0xf51a8 #Something like 0x1337, but you can make it whatever you want, as long as it's a hexadecimal number (probably best to make it something actually unique though!)
UniqueId : 0x42690 #Something like 0x1337, but you can make it whatever you want, as long as it's a hexadecimal number (probably best to make it something actually unique though!)

Option:
UseOnSD : true # true if App is to be installed to SD
Expand Down
Binary file modified snake.cia
Binary file not shown.
134 changes: 57 additions & 77 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Food

struct Snake
{
int lenght;
int length;
u32 color;
struct Vector2 positions[1000];
struct Vector2 headPositon;
Expand Down Expand Up @@ -68,50 +68,6 @@ void checkCollision();
void saveHighscore();
void loadHighscore();

int main(int argc, char* argv[]) {
gfxInitDefault();
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
C2D_Prepare();
initGame();
initSnake();
spawnFood();

game.top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
game.bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);

game.dynamicBuf = C2D_TextBufNew(4096);

while (aptMainLoop())
{
if(game.status == EXIT)
break;

if(game.status == GAME_OVER)
{
saveHighscore();
game.score = 0;
initSnake();
spawnFood();
game.status = RUNNING;
}

input();
drawTopScreen();
drawBottomScreen();
int wait = snake.speed;
while(wait--)
{
gspWaitForVBlank();
}
}

C2D_Fini();
C3D_Fini();
gfxExit();
return 0;
}

void drawTopScreen()
{
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
Expand All @@ -137,7 +93,7 @@ void drawBottomScreen()
C2D_TextOptimize(&game.scoreText);
C2D_DrawText(&game.scoreText, C2D_AlignCenter | C2D_WithColor, 65, 0, 0, 1, 1, C2D_Color32(255, 255, 255, 255));

snprintf(buf, sizeof(buf), "High Score: %d", game.highscore);
snprintf(buf, sizeof(buf), "Highscore: %d", game.highscore);
C2D_TextParse(&game.scoreText, game.dynamicBuf, buf);
C2D_TextOptimize(&game.scoreText);
C2D_DrawText(&game.scoreText, C2D_AlignCenter | C2D_WithColor, 220, 0, 0, 1, 1, C2D_Color32(255, 255, 255, 255));
Expand All @@ -147,7 +103,7 @@ void drawBottomScreen()

void initGame()
{
loadHighscore();
game.highscore = 0;
game.clrClear = C2D_Color32(0, 0, 0, 1);
game.wallColor = C2D_Color32(0, 0, 0, 255);
game.score = 0;
Expand Down Expand Up @@ -192,7 +148,7 @@ void checkCollision()
game.status = GAME_OVER;
}

for(int i = 0; i < snake.lenght; i++)
for(int i = 0; i < snake.length; i++)
{
if(snake.headPositon.x == snake.positions[i].x && snake.headPositon.y == snake.positions[i].y)
{
Expand Down Expand Up @@ -221,7 +177,7 @@ int isFoodSpawnValid(int x, int y)
return 0;
}

for(int i = 0; i < snake.lenght; i++)
for(int i = 0; i < snake.length; i++)
{
l2.x = snake.positions[i].x - 20;
l2.y = snake.positions[i].y + 20;
Expand Down Expand Up @@ -260,15 +216,15 @@ void drawFood()

void initSnake()
{
snake.lenght = 5;
snake.length = 5;
snake.headPositon.x = 200;
snake.headPositon.y = 120;
snake.color = C2D_Color32(0, 255, 0, 255);
snake.dx = 10;
snake.dy = 0;
snake.speed = 6;

for(int i = 0; i < snake.lenght; i++)
for(int i = 0; i < snake.length; i++)
{
snake.positions[i].x = 190 - (i * 10);
snake.positions[i].y = 120;
Expand All @@ -279,15 +235,15 @@ void drawSnake()
{
C2D_DrawRectSolid(snake.headPositon.x, snake.headPositon.y, 0, 10, 10, snake.color);

for(int i = 0; i < snake.lenght; i++)
for(int i = 0; i < snake.length; i++)
{
C2D_DrawRectSolid(snake.positions[i].x, snake.positions[i].y, 0, 10, 10, snake.color);
}
}

void moveSnake()
{
for(int i = snake.lenght - 1; i >= 0; i--)
for(int i = snake.length - 1; i >= 0; i--)
{
snake.positions[i].x = snake.positions[i - 1].x;
snake.positions[i].y = snake.positions[i - 1].y;
Expand All @@ -303,10 +259,6 @@ void input()
{
hidScanInput();
u32 kDown = hidKeysDown();
if(kDown & KEY_START)
{
game.status = EXIT;
}
if(kDown & KEY_UP)
{
if(snake.dy != 10)
Expand Down Expand Up @@ -343,11 +295,15 @@ void input()

void addScore()
{
snake.positions[snake.lenght].x = snake.positions[snake.lenght - 1].x;
snake.positions[snake.lenght].y = snake.positions[snake.lenght - 1].y;
snake.positions[snake.length].x = snake.positions[snake.length - 1].x;
snake.positions[snake.length].y = snake.positions[snake.length - 1].y;

snake.lenght += 1;
snake.length += 1;
game.score += 1;
if(game.score > game.highscore)
{
game.highscore = game.score;
}
for(int i = 0; i < 4; i++)
{
if(game.score == 3 * (i + 1))
Expand All @@ -373,27 +329,51 @@ void saveHighscore()
{
if(game.score > game.highscore)
{
FILE* file = fopen("snake/game.dat", "r+");
char buffer[16] = {};
sprintf(buffer, "%d", game.score);
fputs(buffer, file);
fclose(file);
game.highscore = game.score;
}

}

void loadHighscore()
{
FILE* file = fopen("snake/game.dat", "r+");
if(file == NULL)

int main(int argc, char* argv[]) {
gfxInitDefault();
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
C2D_Prepare();
initGame();
initSnake();
spawnFood();

game.top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
game.bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);

game.dynamicBuf = C2D_TextBufNew(4096);

while (aptMainLoop())
{
mkdir("snake");
file = fopen("snake/game.dat", "w+");
char buffer[16] = {0};
sprintf(buffer, "%d", 0);
fputs(buffer, file);
if(game.status == EXIT)
break;

if(game.status == GAME_OVER)
{
saveHighscore();
game.score = 0;
initSnake();
spawnFood();
game.status = RUNNING;
}

input();
drawTopScreen();
drawBottomScreen();
int wait = snake.speed;
while(wait--)
{
gspWaitForVBlank();
}
}
fscanf(file, "%d", &game.highscore);
fclose(file);

C2D_Fini();
C3D_Fini();
gfxExit();
return 0;
}

0 comments on commit 9e73b76

Please sign in to comment.