TIP: Click on subject to list as thread! ANSI
echo: msged_echo
to: All
from: Paul Edwards
date: 1996-06-24 01:05:34
subject: ansi

I have the start of an ANSI version.  I now have a version that works under
Amigados (very rough of course).  Here is the main ANSI routine...

/*
 *  ANSI.C
 *
 *  Written by Paul Edwards et al and released to the public 
 *  domain.
 *
 *  Screen definitions & routines using ANSI codes.
 */

#include 
#include 

#if (defined(MSDOS) || defined(OS2))
#include 
#endif

#include "winsys.h"
#include "unused.h"

int vcol, vrow;                 /* cursor position         */
int color;                      /* current color on screen */
int cur_start = 0;
int cur_end = 0;

#ifdef SASC
int akbhit(void);
void devinit(void);
void devfin(void);
#endif

TERM term =
{
#ifdef SASC
    78,
#else
    80,
#endif        
    23,
    0
};

static char *scrnbuf;

#define EBUFSZ 100
static EVT EVent[EBUFSZ];       /* event circular queue */
static int ebufin = 0;          /* event in */
static int ebufout = 0;         /* event out */

static int mykbhit(void);
static int FullBuffer(void);

int TTScolor(unsigned int Attr)
{
    color = Attr;
    return 1;
}

int TTBeep(void)
{
    return 1;
}

int TTCurSet(int st)
{
    return (0);
}

int TTgotoxy(int row, int col)
{
    vrow = row;
    vcol = col;
    printf("\x1b[%d;%dH", row + 1, col + 1);
    return 1;
}

int TTgetxy(int *row, int *col)
{
    *row = vrow;
    *col = vcol;
    return 1;
}

int TTPutChr(unsigned int Ch)
{
    putchar(Ch & 0xff);
    scrnbuf[vrow * term.NCol + vcol] = (Ch & 0xff);
    return 1;
}

int TTWriteStr(unsigned short *b, int len, int row, int col)
{
    int x;
    
    TTgotoxy(row, col);
    for (x = 0; x < len; x++)
    {
        scrnbuf[vrow * term.NCol + vcol + x] = (*b & 0xff);
        putchar(*b & 0xff);
        b++;
    }
    TTgotoxy(row, col + len);
    return 1;
}

int TTStrWr(unsigned char *s, int row, int col)
{
    size_t len = strlen(s);
    
    TTgotoxy(row, col);
    memcpy(&scrnbuf[vrow * term.NCol + vcol], s, len);
    printf("%s", s);
    fflush(stdout);
    TTgotoxy(row, col + len);
    return 1;
}

int TTReadStr(unsigned short *b, int len, int row, int col)
{
    int x;
    
    for (x = 0; x < len; x++)
    {
        b[x] = scrnbuf[row * term.NCol + col + x];
    }
    return 1;
}

int TTScroll(int x1, int y1, int x2, int y2, int lines, int Dir)
{
    int y;
    
    if (Dir) /* up */
    {
        for (y = y1; y < y2; y++)
        {
            TTgotoxy(y, x1);
            memcpy(&scrnbuf[y * term.NCol + x1],
                   &scrnbuf[(y + 1) * term.NCol + x1],
                   x2 - x1 + 1);            
            fwrite(&scrnbuf[y * term.NCol + x1], 1, x2 - x1 + 1, stdout);
        }
        TTgotoxy(y, x1);
        memset(&scrnbuf[y * term.NCol + x1], ' ', x2 - x1 + 1);
        fwrite(&scrnbuf[y * term.NCol + x1], 1, x2 - x1 + 1, stdout);
    }
    else
    {
        for (y = y2; y > y1; y--)
        {
            TTgotoxy(y, x1);
            fwrite(&scrnbuf[y * term.NCol + x1], 1, x2 - x1 + 1, stdout);
            memcpy(&scrnbuf[y * term.NCol + x1],
                   &scrnbuf[(y - 1) * term.NCol + x1],
                   x2 - x1 + 1);            
        }
        TTgotoxy(y, x1);
        memset(&scrnbuf[y * term.NCol + x1], ' ', x2 - x1 + 1);
        fwrite(&scrnbuf[y * term.NCol + x1], 1, x2 - x1 + 1, stdout);
    }
    fflush(stdout);
    return 1;
}

int TTClear(int x1, int y1, int x2, int y2)
{
    int x, y;
    
    for (y = y1; y <= y2; y++)
    {
        TTgotoxy(y, x1);
        for (x = x1; x <= x2; x++)
        {
            putchar(' ');
        }
    }
    return 1;
}

int TTEeol(void)
{
    printf("\x1b[2K");
    return 1;
}

int TTChngRez(int a)
{
    unused(a);
    return 1;
}

int TTdelay(int mil)
{
    return (0);
}

unsigned int TTGetKey(void)
{
    unsigned int ch;
    
    ch = getchar();
    if (ch == '\n')
    {
        ch = '\r';
    }
    return (ch);
}

void TTSendMsg(unsigned int msg, int x, int y, unsigned int msgtype)
{
    if (((ebufin + 1) % EBUFSZ) != ebufout)
    {
        EVent[ebufin].msg = msg;
        EVent[ebufin].x = x;
        EVent[ebufin].y = y;
        EVent[ebufin].msgtype = msgtype;
        ebufin = (ebufin + 1) % EBUFSZ;
    }
}

int collect_events(int delay)
{
    if (mykbhit())
    {
        TTSendMsg(TTGetKey(), 0, 0, WM_CHAR);
    }
    return 0;
}

int TTkopen(void)
{
#ifdef SASC
    devinit();
#else
    setbuf(stdin, NULL);
#endif
    scrnbuf = malloc(term.NRow * term.NCol);
    memset(scrnbuf, ' ', term.NRow * term.NCol);
    return (0);
}

int TTkclose(void)
{
#ifdef SASC
    devfin();
#endif    
    free(scrnbuf);
    return (0);
}

void MouseOFF(void)
{
}

void MouseON(void)
{
}

void MouseInit(void)
{
}

int GetMouInfo(int *x, int *y)
{
     return (0);
}

int TTGetMsg(EVT * e)
{
    while (ebufin == ebufout)
        collect_events(1);

    e->msg = EVent[ebufout].msg;
    e->x = EVent[ebufout].x;
    e->y = EVent[ebufout].y;
    e->msgtype = EVent[ebufout].msgtype;
    e->id = 0;

    ebufout = (ebufout + 1) % EBUFSZ;

    return e->msg;
}

int TTPeekQue(void)
{
    collect_events(0);
    return (ebufin != ebufout);
}

void TTClearQue(void)
{
    ebufin = ebufout;
}

int TTGetChr(void)
{
    EVT e;
    
    TTGetMsg(&e);
    return e.msg;
}

int TTopen(void)
{
    vcol = vrow = 0;
    color = 0x07;

    TTkopen();
    return 1;
}

int TTclose(void)
{
    TTkclose();
    return 1;
}

static int mykbhit(void)
{
    int ret;
    
    if (FullBuffer())
    {
        return (0);
    }
#ifdef SASC
    ret = akbhit();
#else        
    ret = kbhit();
#endif
    return (ret);    
}

int dv_running(void)
{
    return 0;
}

static int FullBuffer(void)
{
    if (((ebufin + 1) % EBUFSZ) != ebufout)
    {
        return (0);
    }
    else
    {
        return (1);
    }
}
@EOT:

---

SD®¯[â
* Origin: X (3:711/934.9)
SEEN-BY: 633/267 270
@PATH: 711/934 808 50/99 635/544 727 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.