controller.c 3.26 KB
/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1995, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

#include <spbench.h>

/*
 * controller.c
 *
 * Routines to access the game controller.
 */

static OSMesgQueue	controllerMsgQ;
static OSMesg		controllerMsgBuf;

static OSContStatus     statusdata[MAXCONTROLLERS];
static OSContPad        controllerdata[MAXCONTROLLERS];

static u16		lastbutton;

/*
 * initController
 *
 * Initialize controllers and return the lowest number controller
 * connected to system.
 */
int
initController(void)
{
    int             i;
    u8              pattern;

    osCreateMesgQueue(&controllerMsgQ, &controllerMsgBuf, 1);
    osSetEventMesg(OS_EVENT_SI, &controllerMsgQ, (OSMesg)0);

    osContInit(&controllerMsgQ, &pattern, &statusdata[0]);

    for (i = 0; i < MAXCONTROLLERS; i++) {
        if ((pattern & (1<<i)) &&
                !(statusdata[i].errno & CONT_NO_RESPONSE_ERROR)) {
            return i;
	}
    }
    return -1;
}

/*
 * readController
 *
 * Read controller values and adjust sprite parameters as indicated:
 *
 *	The up/down buttons increase/decrease the number of sprites drawn.
 *	The left/right buttons increase/decrease the number of sprites drawn
 *		between loads of TMEM.
 *	The trigger (G) button switches between various sprite parameter
 *		scenarios.
 *
 * This routine returns 1 if a new scenario has been selected and it is
 * necessary to re-initialize the sprite database.
 */
int
readController(int num)
{
    static u16 button;
    int changed = 0;
    int reInit = 0;
      
    osContStartReadData(&controllerMsgQ);
    (void)osRecvMesg(&controllerMsgQ, NULL, OS_MESG_BLOCK);

    osContGetReadData(controllerdata);
    button = controllerdata[num].button;

    if ((button & CONT_UP) && (lastbutton & CONT_UP)) {
	changed = 1;
	if (currentParam->numSprites < MAX_SPRITES)
	    currentParam->numSprites++;
    }
	    
    if ((button & CONT_DOWN) && (lastbutton & CONT_DOWN)) {
	changed = 1;
        if (currentParam->numSprites > 0)
	    currentParam->numSprites--;
    }
    if ((button & CONT_LEFT) && (lastbutton & CONT_LEFT)) {
	changed = 1;
	if (currentParam->loadFactor > 1)
	    currentParam->loadFactor--;
    }
    if ((button & CONT_RIGHT) && (lastbutton & CONT_RIGHT)) {
	changed = 1;
	if (currentParam->loadFactor < 500)
	    currentParam->loadFactor++;
    }
    if (!(lastbutton & CONT_G) && (button & CONT_G)) {
	if (++currentParam >= endParamTable)
	    currentParam = &paramTable[0];
	changed = 1;
        reInit = 1;
    }

    if (changed)
	printCurrentParam();

    lastbutton = button;
    return(reInit);
}