0

Okay so I'm pretty new to programming, so I thought as my first real programming challenge I would make a chess program to teach openings. Inside my game game loop I have everything I need, except at some point in the game loop I want to stop for input from the user, and wait to process that information before drawing the screen and continuing the loop. However when I do that, if the user waits to long to input(~8 seconds)(btw the input in console input for now, that will change later) then the game just crashes and I get the standard ubuntu "do you want to force quit or wait" message. I would like to keep this message from popping up, but I don’t know how.

Here's my code btw:

#include <stdio.h>
#include <string.h>
#include <time.h>
#include "SDL2/SDL.h"

void drawBoard(SDL_Renderer *renderer)
{}

void drawPieces(char fen[100])
{}

int processEvents(SDL_Window *window)
{}

I'm leaving these functions out because the are long and not super important(if you want me to I’ll put them up later)

void next_move(char firstmove[10], char response[10])
{
    int cont = 0;
    char move[6];
    printf("%s\n>>", firstmove);
    while(cont == 0)
    {
        scanf("%s", move);
        if(strcmp(move, response) == 0)
            cont = 1;
        else
            printf("Incorrect.\n%s\n>>", firstmove);
    }
}

void caro_kann()
{
    printf("YOU HAVE SELECTED The Caro Kann Opening ( main line )\n");
    next_move("1. e4", "c6");
    next_move("2. d4", "d5");
    next_move("3. Nc6", "dxe4");
    next_move("4. Nxe4", "Bf5");
    next_move("5. Ng3", "Bg6");
    next_move("6. h4", "h6");
    next_move("7. Nf3", "Nd7");
    printf("success\n");
}

int main()
{
    int done = 0;
    SDL_Event event;

    SDL_Window *window;
    SDL_Renderer *renderer;

    //initialize everything
    SDL_Init(SDL_INIT_VIDEO);

    //setting up the window
    window = SDL_CreateWindow("PlatoChess ALPHA 1.0", 
                           SDL_WINDOWPOS_UNDEFINED,
                           SDL_WINDOWPOS_UNDEFINED,
                           800,
                           800,
                           0);

    //Setting up the renderer
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    //setting up the program loop
    while(!done)
    {
        if(processEvents(window) == 1)
            done = 1;

        caro_kann();

        drawBoard(renderer);

        SDL_Delay(10); //to cap the FPS
    }

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);

    SDL_Quit();

    return 0;
}

Any help would be much appreciated!

Mr.Prime
  • 21
  • 3

1 Answers1

0

First of all, "do you want to force quit or wait" is not a crash. All it means is that your program isn't processing events (which is true - you're in a blocking scanf and doing nothing). Even if you're waiting for something to happen, you still should process window events and update display if necessary, otherwise user can't close your program and if it gets overshadowed by another program (or just minimised) its display will be completely wrong.

Basically you shouldn't block in wait for input. Your options are (sorted by ascending difficulty, based entirely on my opinion):

  • change input scheme (e.g. use keyboard events that SDL gives you instead of stdin)

  • perform nonblocking read from stdin when input is available

  • use separate input thread (requires sync)

Community
  • 1
  • 1
keltar
  • 17,711
  • 2
  • 37
  • 42