#ifndef IVRAM_H_INCLUDED
#define IVRAM_H_INCLUDED


#include <sys/bios.h>
#include <resource.h>


/* frdeb */
#ifdef _DEBUG
#include "debug.h"
#else
#define BREAK
#endif


/* 画面領域 */
#define IV_CX      28
#define IV_CY      18
#define IV_CXY     (IV_CX * IV_CY)
#define IV_CW      8
#define IV_CH      8

/* iv_panic() */
#define IV_PANIC_INVALIDSP   -1

/* 未調整 */
#define IV_PALETTE_AWGB  0x7420
#define IV_PALETTE_WLDB  0x7520


/* iv_charinfo_t キャラクタ定義 */

typedef BYTE iv_charinfo_t;

#define IV_CHARINFO_UNITSIZE   (sizeof(iv_charinfo_t))

#define IV_CHARINFO_OPAQUE     0x01   /* 透明部分なし */
#define IV_CHARINFO_NORENDER   0x02   /* 完全に透明 */

#define IV_CHAR_UNITSIZE   16
#define IV_CHARTABLE_MAX   4
#define IV_CHARTABLE_UNITSIZE  (IV_CHAR_UNITSIZE * 2)


/* iv_sprite_t スプライト定義 */

typedef struct {
    WORD renderer;     /* レンダラ（x86 near 関数） */
    WORD param;        /* パラメータ */
} iv_sprite_t;

#define IV_SPRITE_UNITSIZE   (sizeof(iv_sprite_t))

/* 仮想VRAMサイズ */

#define IV_OVERLAY_MIN    4
#define IV_VVRAM_MINSIZE  (IV_OVERLAY_MIN * sizeof(WORD) * IV_CXY)


/* 'IV' リソース */

#define IV_RESID_IMAGE 0x5649   /* 'IV' */

#define IV_RESFORMAT_COLOR3  0x01
#define IV_RESFORMAT_COLOR4  0x02  /* 予約 */

/* res_image_t と基本的に同じ */

typedef struct {
    res_t header;
    BYTE  width;
    BYTE  height;
    BYTE  format;       /* IV_RESFORMAT_xx */
    BYTE  reserve;
    BYTE  clrtable[4];
} iv_res_image_t;

#define iv_res_pcharinfo(ir)  ((BYTE far*)ir + sizeof(iv_res_image_t))
#define iv_res_charmax(ir)   ((int)ir->width * (int)ir->height)
#define iv_res_pchardata(ir) (iv_res_pcharinfo(ir) + iv_res_charmax(ir))


/* プロトタイプ */

void iv_init(unsigned palette_mode);
void iv_panic(int n);
res_t far *iv_get_resource(void far* mapped, WORD type, short id);

void ivs_make_chartable(BYTE* info_buf, iv_res_image_t far *ivres);
void ivs_make_chartable_bc3(BYTE* info_buf, BYTE* char_buf, WORD far* bc3_buf, int max);
void ivs_set_chartable(int tableno, int max, BYTE* info_buf, BYTE far* char_buf);
void ivs_sprite_init(BYTE* sprite_buf, int max, BYTE* vvram_buf, int overlay_max);
void ivs_start_render(WORD flags);
void ivs_end_render(void);
void ivs_add_sprite(int charno, int x, int y);
void ivs_add_sprite_rect(int charno, int x, int y, int w, int h, int delta);
void ivs_add_opaque_rect(int charno, int x, int y, int w, int h);
void ivs_add_sprite_fill(int charno, int x, int y, int w, int h);


#define IV_STARTRENDER_NOVRAMINIT  0x0001


/* BYTE* iv_get_heap() */
extern WORD _heap;
#define iv_get_heap() ((BYTE*)((_heap + 0x0F) & 0xFFF0))





#endif /* ifndef IVRAM_H_INCLUDED */
