/* TS: GRP3.TS */
 
/* Graphix routine for GNU-C */
/* & Key input routine */
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#define WORD(x) *((unsigned short int *)(x)) 
#define DWORD(x) *((unsigned int *)(x)) 
 
/* patch of EGB_B2.S */
void EGB_putBlockColor(void ) 
{
    ;
}
 
void EGB_init(char *, int );
void EGB_color(char *, int , int );
void EGB_singleLine(char *, int , int , int , int );
void EGB_singlePset(char *, int , int );
void EGB_resolution(char *, int , int );
void EGB_box(char *, int , int , int , int );
void EGB_maru(char *, int , int , int );
void EGB_setPalette(char *, int , int , int , int );
void EGB_sjisString(char *, char *);
void EGB_writePage(char *, int );
int EGB_displayPage(int , char *, int );
void EGB_displayStart(char *, int , int , int );
void EGB_viewportRect(char *, int , int , int , int );
void EGB_writeMode(int );
void EGB_paintColor(char *, int );
void EGB_closePaint(char *, char *);
void EGB_paintMode(char *, int );
void EGB_getBlock(char *, char *);
void EGB_getBlockZoom(char *, char *);
void EGB_putBlock(char *, int , char *);
void EGB_putBlockZoom(char *, int , char *);
void CRTC(int , int );
void screenterm(char *);
 
unsigned int KYB_read(int , unsigned int *);
 
 
static char gwork[2000];
static int XMAX = 640, YMAX = 480;
 
/* GNU-C (native mode) */
 
/* basic key_read routine */
unsigned int input() 
{
    unsigned int encode;
    return KYB_read(0, &encode);
}
unsigned int inkey() 
{
    unsigned int encode;
    return KYB_read(1, &encode);
}
void anykey() 
{
    input();
}
 
/* util */
void wait(int x) 
{
    int i;
    for (i = 0; i <= x; i++){
    }
}
 
int rnd(int x) 
{
    return (rand() % x);
}
/* graphix */
 
void wpage(int a) 
{
    EGB_writePage(gwork, a);
}
 
int dpage(int a, int x) 
{
    int c;
     
    c = EGB_displayPage(x, gwork, a);
    return c;
}
 
void graphix() 
{
    EGB_init(gwork, 1536);
}
 
void graphix_off() 
{
    screenterm(gwork);
}
 
void screen(int a, int b) 
{
    EGB_resolution(gwork, a, b);
    switch (b){
        case 1:{XMAX = 640;YMAX = 400;}
        case 2:{XMAX = 640;YMAX = 200;}
        case 3:{XMAX = 640;YMAX = 480;}
        case 4:{XMAX = 640;YMAX = 400;}
        case 5:{XMAX = 256;YMAX = 256;}
        case 6:{XMAX = 256;YMAX = 256;}
        case 7:{XMAX = 256;YMAX = 240;}
        case 8:{XMAX = 256;YMAX = 240;}
        case 9:{XMAX = 360;YMAX = 240;}
        case 10:{XMAX = 320;YMAX = 240;}
        case 11:{XMAX = 320;YMAX = 240;}
        case 12:{XMAX = 640;YMAX = 480;}
        case 13:{XMAX = 640;YMAX = 400;}
        case 14:{XMAX = 720;YMAX = 480;}
        case 15:{XMAX = 320;YMAX = 380;}
        case 16:{XMAX = 320;YMAX = 480;}
        case 17:{XMAX = 512;YMAX = 480;}
        case 18:{XMAX = 512;YMAX = 480;}
    }
}
 
void sposition(int x, int y) 
{
    EGB_displayStart(gwork, 0, x, y);
}
 
void flame_address(int page, int x) 
{
    if (page){
        CRTC(21, x);
    }
    else {
        CRTC(17, x);
    }
}
void scroll(int x, int y) 
{
    EGB_displayStart(gwork, 1, x, y);
}
 
void zoom(int x, int y) 
{
    EGB_displayStart(gwork, 2, x, y);
}
 
void width(int x, int y) 
{
    EGB_displayStart(gwork, 3, x, y);
}
 
void view(int x, int y, int a, int b) 
{
    EGB_viewportRect(gwork, x, y, a, b);
}
 
void color(int c) 
{
    EGB_color(gwork, 0, c);
}
void paint_color(int c) 
{
    EGB_paintColor(gwork, c);
}
 
void paint_mode(int x) 
{
    EGB_paintMode(gwork, x);
}
 
void palette(int a, int r, int g, int b) 
{
    EGB_setPalette(gwork, a, g, r, b);
}
 
void write_logic(int x) 
{
    EGB_writeMode(x);
}
 
void pset(int x, int y) 
{
    EGB_singlePset(gwork, x, y);
}
 
void line(int x, int y, int a, int b) 
{
    EGB_singleLine(gwork, x, y, a, b);
}
 
void box(int x, int y, int a, int b) 
{
    EGB_box(gwork, x, y, a, b);
}
 
void maru(int x, int y, int a) 
{
    EGB_maru(gwork, x, y, a);
}
 
void symbol(int x, int y, char mess[255]) 
{
     
    char param[260];
     
    WORD(param) = x;
    WORD(param + 2) = y;
    WORD(param + 4) = strlen(mess);
    strcpy(param + 6, mess);
     
    EGB_sjisString(gwork, param);
}
 
void get_block(char *t, int x, int y, int a, int b, int h, int v) 
{
    /* h,v : data size */
    char param[17];
     
    DWORD(param) = (int )t;
    WORD(param + 4) = 0x14;
    WORD(param + 6) = x;
    WORD(param + 8) = y;
    WORD(param + 10) = a;
    WORD(param + 12) = b;
    if (h == 0 && v == 0){
        EGB_getBlock(gwork, param);
    }
    else {
        WORD(param + 14) = h;
        WORD(param + 16) = v;
        EGB_getBlockZoom(gwork, param);
    }
}
 
void put_block(char *t, int x, int y, int a, int b, int h, int v) 
{
    char param[17];
     
    DWORD(param) = (int )t;
    WORD(param + 4) = 0x14;
    WORD(param + 6) = x;
    WORD(param + 8) = y;
    WORD(param + 10) = a;
    WORD(param + 12) = b;
    if (h == 0 && v == 0){
        EGB_putBlock(gwork, 0, param);
    }
    else {
        WORD(param + 14) = h;
        WORD(param + 16) = v;
        EGB_putBlockZoom(gwork, 0, param);
    }
}
/* TS-END */
