/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at RISC OS path @.^.LICENCE
 * or  http://www.riscosdev.com/lanman98/LICENCE.CDDL
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the Licence file. If applicable, add the
 * following below this CDDL HEADER, with the fields enclosed by
 * brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
 
/*
 *   Portions Copyright 1996 Warm Silence Software Ltd.
 */

/******************************************************/
/*                                                    */
/* Name: PGgr.c                                       */
/* Author: Paul Gardiner.                             */
/* Function: Provides a library of graphics functions */
/*                                                    */ 
/******************************************************/

#include "kernel.h"
#include "swis.h"
static _kernel_swi_regs regs;
#include "PGgr.h"
#include "PGgrH.h"


struct gr_data_s xyzGRxyz = {0, 0, 7};

void GrClear(void)
{
    regs.r[0] = 16;
    _kernel_swi(OS_WriteC, &regs, &regs);
}

void GrColFG(int colour)
{
    xyzGRxyz.fgcol = colour;
    if(xyzGRxyz.indirected)
    {
        regs.r[0] = 0;
        regs.r[1] = colour;
        _kernel_swi(OS_SetColour, &regs, &regs);
    }
    else
    {
        regs.r[0] = colour;
        _kernel_swi(Wimp_SetColour, &regs, &regs);
    }
}

void GrColBG(int colour)
{
    xyzGRxyz.bgcol = colour;
    if(xyzGRxyz.indirected)
    {
        regs.r[0] = 0x10;
        regs.r[1] = colour;
        _kernel_swi(OS_SetColour, &regs, &regs);
    }
    else
    {
        regs.r[0] = colour + 0x80;
        _kernel_swi(Wimp_SetColour, &regs, &regs);
    }
}

void GrOrigin(int x, int y)
{
    static char vdu[6] = {0, 29, 0, 0, 0, 0};

    ((short *) vdu)[1] = x;
    ((short *) vdu)[2] = y;
    regs.r[0] = (int) vdu + 1;
    regs.r[1] = 5;
    _kernel_swi(OS_WriteN, &regs, &regs);
}

void GrMove(int x, int y)
{
    regs.r[0] = 68;
    regs.r[1] = x;
    regs.r[2] = y;
    _kernel_swi(OS_Plot, &regs, &regs);
}

void GrDraw(int x, int y)
{
    regs.r[0] = 5;
    regs.r[1] = x;
    regs.r[2] = y;
    _kernel_swi(OS_Plot, &regs, &regs);
}

void GrTriangle(int x, int y)
{
    regs.r[0] = 85;
    regs.r[1] = x;
    regs.r[2] = y;
    _kernel_swi(OS_Plot, &regs, &regs); 
}

void GrRectangle(int x, int y)
{
    regs.r[0] = 101;
    regs.r[1] = x;
    regs.r[2] = y;
    _kernel_swi(OS_Plot, &regs, &regs); 
}

void GrPoint(int x, int y)
{
    regs.r[0] = 69;
    regs.r[1] = x;
    regs.r[2] = y;
    _kernel_swi(OS_Plot, &regs, &regs); 
}

int GrGetPix(int x, int y)
{
    regs.r[0] = x;
    regs.r[1] = y;
    _kernel_swi(OS_ReadPoint, &regs, &regs);
    return regs.r[2];
}
