controller.c 4.8 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 <ultra64.h>

#include <ramrom.h>

#include <stdlib.h>
#include <string.h>

/* app specific includes */
#include "fill.h"
#include "timer.h"

OSMesgQueue	controllerMsgQ;
OSMesg		controllerMsgBuf;

int initControllers(void);
OSContStatus     statusdata[MAXCONTROLLERS];
OSContPad        controllerdata[MAXCONTROLLERS];

static u16 		button;
static u16      lastbutton;
static s16 		stickX, stickY;      
static u16		press;
static u16		press_up, press_down;
int 			ControllerMode = 0;

/*
 *
 * Return the lowest number controller connected to system
 */
int initControllers()
{
    OSMesgQueue     serialMsgQ;
    OSMesg          serialMsg;
    int             i;
    u8              pattern;

    osCreateMesgQueue(&serialMsgQ, &serialMsg, 1);
    osSetEventMesg(OS_EVENT_SI, &serialMsgQ, (OSMesg)1);

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

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

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

void ResetParameters(void)
{
	extern void initTri(void);

  	UseZMode            =         1;
  	UseAAMode           =         1;
  	TimerTicks          =         7;
  	UseTextureMode      =         0;
  
  	initTri();
  
  	TriNx = 1;
  	TriNy = 1;
  	TriNz = 1;
}

void ReadController0(void)
{
    
  (void)osRecvMesg(&controllerMsgQ, NULL, OS_MESG_BLOCK);
  osContGetReadData(controllerdata);

  button = controllerdata[ActiveController].button;
  stickX = controllerdata[ActiveController].stick_x;
  stickY = controllerdata[ActiveController].stick_y;

  press = lastbutton ^ button;
  press_up = lastbutton & press;
  press_down = button & press;
  lastbutton = button;

  if (stickX > 15)
    {
      TriDx += 0.1;      
	  if(TriDx > 2.0) TriDx = 2.0;
    }
  else if (stickX < -15)
    {
      TriDx -= 0.1;
	  if(TriDx < -2.0) TriDx = -2.0;
    }
  else {
	if(TriDx)
		ControllerInput = 1;
	TriDx = 0.0;
  }

  if (stickY > 15)
    {
      TriDy += 0.1;      
	  if(TriDy > 2.0) TriDy = 2.0;
    }
  else if (stickY < -15)
    {
      TriDy -= 0.1;
	  if(TriDy < -2.0) TriDy = -2.0;
    }
  else {
	if(TriDy)
		ControllerInput = 1;
    TriDy = 0.0;
  }

  if (press_down & CONT_UP)
    {

	  TriNy++;
	  if(TriNy > MAXTRISPERYAXIS)
		TriNy = MAXTRISPERYAXIS;

      ControllerInput = 1;
    }
	    
  if (press & CONT_DOWN)
    {
	  TriNy--;
	  if(TriNy < 1)
		TriNy = 1;

      ControllerInput = 1;
    }

  if (press_down & CONT_RIGHT)
    {
	  TriNx++;
	  if(TriNx > MAXTRISPERXAXIS)
		TriNx = MAXTRISPERXAXIS;

      ControllerInput = 1;
    }
	    
  if (press_down & CONT_LEFT)
    {
	  TriNx--;
	  if(TriNx < 1) TriNx = 1;

      ControllerInput = 1;
    }


  if (press_down & CONT_A)
    {
      UseAAMode = 1 - UseAAMode;
      ControllerInput = 1;
    }

  if (press_down & CONT_B)
    {
      UseZMode = 1 - UseZMode;
      ControllerInput = 1;
    }

  if (press_down & CONT_C)
    {
	  TriSortOrder = 1 - TriSortOrder;
      ControllerInput = 1;
    }

  if (press_down & CONT_F)
    {      
	  DoClrClr = 1 - DoClrClr;
      ControllerInput = 1; 
    }

  if (press_down & CONT_D)
    {
	  TriDeltaXYPos -= DELTAXY_INCR;
	  if(TriDeltaXYPos < -15)
		TriDeltaXYPos = -15;

      ControllerInput  = 1;
    }

  if (press_down & CONT_E)
    {      
	  TriDeltaXYPos += DELTAXY_INCR;
	  if(TriDeltaXYPos > 15)
		TriDeltaXYPos = 15;

      ControllerInput  = 1; 
    }


  if (press_down & CONT_L)
    {      
	  TriNz--;
	  if(TriNz < 1)
		TriNz = 1;

	  ControllerInput = 1; 
    }

  if (press_down & CONT_R)
    {      
	  TriNz++;
	  if(TriNz > MAXTRISPERZAXIS)
		TriNz = MAXTRISPERZAXIS;

      ControllerInput = 1;
    }

  if (press_down & CONT_G)
    {
	  ActiveVert++;
	  if(ActiveVert == 3)
		ActiveVert = 0;
    }

  if (press_down & CONT_START)
    {
      ResetParameters();
      ControllerInput = 1;
    }
}


void ReadController(void)
{
    ReadController0();
}