#include <system.h>
#include <system_private.h>
#include <gfx.h>
#include <net.h>
#include <sfx.h>
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>

bool system_can_exit(void)
{
    return true;
}

void system_loop(void)
{
    SDL_PumpEvents();
}

void system_deinit(void)
{
    gfx_deinit();
    sfx_deinit();
    net_deinit();
    SDL_Quit();
}

int system_init(void)
{
    if (system_init_os())
        goto failure;
    else if (SDL_Init(0))
    {
        fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
        goto failure;
    }
    else if (gfx_init() || sfx_init() || net_init())
        goto failure;

    SDL_WM_SetCaption(PROJECT_NAME, NULL);
    SDL_ShowCursor(0);
    /* SDL_WM_GrabInput(SDL_GRAB_ON); */
    SDL_EnableUNICODE(1);
    return 0;

failure:
    system_deinit();
    return -1;
}
