#include <gfx/gfx.h>
#include <gfx/private.h>
#include <psxgpu.h>
#include <errno.h>
#include <stdbool.h>

static GsDispEnv dispenv;
enum {ENV_Y = SCREEN_H + 16};

static GsDrawEnv drawenv =
{
    .y = ENV_Y,
    .w = SCREEN_W,
    .h = SCREEN_H
};

int screen_w = SCREEN_W, screen_h = SCREEN_H;

void gfx_swapbuffers(void)
{
    const short y = drawenv.y;

    drawenv.y = dispenv.y;
    dispenv.y = y;
    GsSetDrawEnv(&drawenv);
    GsSetDispEnv(&dispenv);
}

void gfx_initenvs(void)
{
    GsSetDrawEnv(&drawenv);
    GsSetDispEnv(&dispenv);
}

int gfx_toggle_fullscreen(void)
{
    errno = ENOTSUP;
    return -1;
}

int gfx_set_fullscreen(const short w, const short h)
{
    errno = ENOTSUP;
    return -1;
}

bool gfx_fullscreen_available(void)
{
    return false;
}

bool gfx_fullscreen(void)
{
    return true;
}

int gfx_display_size(short *const w, short *const h)
{
    *w = SCREEN_W;
    *h = SCREEN_H;
    return 0;
}

bool gfx_inside_drawenv(const short x, const short y, const short w,
    const short h)
{
    return (x + w >= 0)
        && x < drawenv.w
        && (y + h >= 0)
		&& y < drawenv.h;
}
