My code crashes; not sure why.

Discussion in '3DS - Homebrew Development and Emulators' started by HamBone41801, Mar 9, 2017.

  1. HamBone41801
    OP

    HamBone41801 K35

    Member
    624
    411
    Jan 16, 2017
    United States
    Heres my code. It compiles fine but crashes in citra.

    #include <time.h>
    #include <string.h>
    #include <stdio.h>
    #include <sf2d.h>
    #include <3ds.h>

    const int NumRows = 240;
    const int NumColumns = 400;

    void printToConsole(char* msg)
    {
    time_t unixTime = time(NULL);
    struct tm* timeStruct = gmtime((const time_t *)&unixTime);

    int hours = timeStruct->tm_hour;
    int minutes = timeStruct->tm_min;
    int seconds = timeStruct->tm_sec;

    printf("[%02i:%02i:%02i] %s\n", hours, minutes, seconds, msg);
    }

    int checkCollisions(int arr[][NumColumns], int arr2[][NumColumns])
    {
    int i = 0;
    int j = 0;
    for(i = 0; i <= NumRows; i++)
    {
    for(j = 0; j <= NumColumns; j++)
    {
    if(arr[j] == 1 && arr2[j] == 1)
    {
    return 1;
    }
    else
    {
    return 0;
    }
    }
    }
    return 0;
    }

    int main()
    {

    int paddleOneX = 10;
    int paddleOneY = 80;

    int paddleTwoX = 380;
    int paddleTwoY = 80;

    int ballX = 195;
    int ballY = 115;

    int ballCollider[NumRows][NumColumns];
    int paddleOneCollider[NumRows][NumColumns];
    int paddleTwoCollider[NumRows][NumColumns];

    sf2d_init();
    sf2d_set_clear_color(RGBA8(0x00, 0x00, 0x00, 0xFF));

    consoleInit(GFX_BOTTOM, NULL);

    printToConsole("Hi!");

    while (aptMainLoop())
    {
    hidScanInput();
    u32 kUp = hidKeysUp();
    u32 kHeld = hidKeysHeld();

    if (kUp & KEY_START)
    {
    break;
    }

    //GAME_CODE////////////////////////////////////////////////

    // Paddle Movement
    if(kHeld & KEY_UP)
    {
    if(paddleOneY != 0)
    {
    paddleOneY--;
    }
    }
    if(kHeld & KEY_DOWN)
    {
    if(paddleOneY != 160)
    {
    paddleOneY++;
    }
    }

    if(((paddleTwoY + 40) > (ballY + 5)) & (paddleTwoY != 0))
    {
    paddleTwoY--;
    }
    if(((paddleTwoY + 40) < (ballY + 5)) & (paddleTwoY != 160))
    {
    paddleTwoY++;
    }

    // Ball Movement
    ballX--;
    //Collisions

    if(checkCollisions(ballCollider, paddleOneCollider) == 1)
    {
    break;
    }
    else if(checkCollisions(ballCollider, paddleTwoCollider) == 1)
    {

    }

    ///////////////////////////////////////////////////////////

    //DRAW_CODE////////////////////////////////////////////////

    sf2d_start_frame(GFX_TOP, GFX_LEFT);
    sf2d_draw_rectangle(ballX, ballY, 10, 10, RGBA8(0xFF, 0xFF, 0xFF, 0xFF)); // Ball
    sf2d_draw_rectangle(paddleOneX, paddleOneY, 10, 80, RGBA8(0xFF, 0xFF, 0xFF, 0xFF)); // Paddle One
    sf2d_draw_rectangle(paddleTwoX, paddleTwoY, 10, 80, RGBA8(0xFF, 0xFF, 0xFF, 0xFF)); // Paddle Two
    sf2d_end_frame();

    ///////////////////////////////////////////////////////////

    sf2d_swapbuffers();
    }

    sf2d_fini();
    return 0;
    }
     
  2. Kartik

    Kartik Gbatemp advanced fan

    Member
    349
    257
    Jun 6, 2015
    India
    github
    You cannot use ctrulib 's consoleInit() with sf2d.Use sftd instead.
    https://github.com/xerpi/sftdlib
     
    Last edited by Kartik, Mar 9, 2017
  3. erman1337

    erman1337 GBAtemp Maniac

    Member
    1,211
    931
    Sep 27, 2015
    Belgium
    Brussels
    put printf on every line to pinpoint the problematic line