gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Cheat code system/ Buffer for player input

#177007 - blessingta@hotmail.co.uk - Mon Nov 21, 2011 2:58 pm

hi

I'm trying to create a system that allows the player to input cheat codes
and so far Im trying the buffer system and I've been having errors (while loop wont terminate) with this visual studio trial I've done:

Plus whats the most memory safe/easiest way of implementing a player input buffer?

Code:
// linked list gba.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
enum EKEYSTATE
{
   a,
   b,
   c,
   d,
   e

};
//BUFFER FOR CURRENT KEYS
//Purpose: to store information about the keys in the current buffer
struct SKey_buffer
{
   volatile unsigned char vuc_KEYSTATE;
   SKey_buffer * next;
   bool bHead;
};

    SKey_buffer _buff6; 
   SKey_buffer _buff5; 
   SKey_buffer _buff4; 
   SKey_buffer _buff3; 
   SKey_buffer _buff2; 
   SKey_buffer _buff1; 

   SKey_buffer* iter;//iter head
   SKey_buffer * iter_Head;//temp storage for iter head
   SKey_buffer* iter_newNULL; //initialised to current null   

   void BUFFER_keysinit();
   void  BUFFER_newKeys(volatile unsigned char a);


int _tmain(int argc, _TCHAR* argv[])
{
   BUFFER_keysinit();

       //this unsigned char will be replaced with enums after i know that the linked list is working properly
   volatile unsigned char a = 'A';
   
   int i = 0;
   while (i < 200)
   {
      BUFFER_newKeys(a);
      a++;

      std::cout<<iter->vuc_KEYSTATE<<std::endl;
      i++;
   }
   return 0;
}

//initialise key input buffer
void BUFFER_keysinit()
{
    _buff6.next = NULL;    _buff6.bHead = false;
    _buff5.next = &_buff6; _buff5.bHead = false;
    _buff4.next = &_buff5; _buff4.bHead = false;
    _buff3.next = &_buff4; _buff3.bHead = false;
    _buff2.next = &_buff3; _buff2.bHead = false;
    _buff1.next = &_buff2; _buff1.bHead = true;

   
   iter_Head = &_buff1;//head at initialisation
   iter = iter_Head; //head at start of traversing
   iter_newNULL = &_buff6; //initialised to current null

}

void BUFFER_newKeys(volatile unsigned char a)
{
   ///////////////////////////////////////////////////////////////////////////////
//section continuously executed
   //Locate current NULL
   while (iter->next != NULL)
   {
      //This will be the last iterrator prior the last,
      //NULL with wont be executed
      iter_newNULL->next = iter;
      iter = iter->next;
   }

   //iter == null
   if (iter->next == NULL)
   {
      //current NULL, = current state
      iter->vuc_KEYSTATE = a;

      //current NULL iter = New head
      //point to old head: set old head to false
      iter_Head->bHead = false;
      iter->next = iter_Head;
      iter->bHead = true;

      //NEW iter_head = "current NULL"
      iter_Head = iter;

      //active new null to null
      iter_newNULL->next = NULL;
   }
///////////////////////////////////////////////////////////////////////////////

}


 
[/code]

#177010 - ant512 - Mon Nov 21, 2011 8:58 pm

The simplest way is to store each button press in a char array, which you treat like a queue. So, you'd have this:

char cheatcode[4];

When a player presses UP, you'd have this in your array:

{ 0, 0, 0, 1}

If the player then presses DOWN, you'd have this in your array:

{ 0, 0, 1, 2}

If the player then presses RIGHT, you'd have this:

{ 0, 1, 2, 4}

Each time a new button is pressed, you shift the array up. Old numbers drop off the front. This isn't an expensive thing to do and you can add it to your input handler.

To check for a matching cheat, just compare your array with a valid cheat code. Here's a non-valid code:

char valid[] = { 1, 2, 3, 4 }
char cheat[] = { 2, 3, 3, 4}

Here's a valid code:

char valid[] = { 1, 2, 3, 4 }
char cheat[] = { 1, 3, 3, 4}

#177011 - blessingta@hotmail.co.uk - Mon Nov 21, 2011 11:55 pm

how do i add to a que? thats the problem I had been having which lead me to think that a linked list could possibly be the better option

#177012 - sverx - Tue Nov 22, 2011 11:08 am

a small array and an index (say to the last entry) could be enough :)
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS / GBA|A DS Homebrewer's Diary

#177013 - blessingta@hotmail.co.uk - Tue Nov 22, 2011 5:01 pm

Whats the "itoa" function equivalent on the GBA?

I need to convert the player's score into a char array that can displayed on screen any help?

#177014 - Miked0801 - Tue Nov 22, 2011 5:13 pm

please, just google that. It's a standard function that any experienced coder is expected to know how to do - heck it's been an interview question for me a couple of times now.

#177015 - Dwedit - Tue Nov 22, 2011 5:39 pm

blessingta@hotmail.co.uk wrote:
Whats the "itoa" function equivalent on the GBA?

I need to convert the player's score into a char array that can displayed on screen any help?


I already answered this in this post. It's easy to skim over someone else's post, but try to read more carefully.

char buffer[12];
sniprintf(buffer,12,"%d",value);
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."


Last edited by Dwedit on Tue Nov 22, 2011 5:42 pm; edited 1 time in total

#177016 - blessingta@hotmail.co.uk - Tue Nov 22, 2011 5:40 pm

devkitpro wont recognise "itoa" and returns this error message instead:

Quote:
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_sprites/source/main.c:111: undefined reference to `itoa'


yet I've included
Code:
#include <stdio.h>


I'm currently under a lot of stress with the looming deadlines so please mind me if I seem to be posting stupid questions.

#177017 - Dwedit - Tue Nov 22, 2011 5:43 pm

"itoa" is not part of the standard C library, even though "atoi" is.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#177018 - blessingta@hotmail.co.uk - Tue Nov 22, 2011 5:45 pm

thanks it worked

#177019 - Miked0801 - Tue Nov 22, 2011 6:36 pm

Code:


void itoaHelper(int i, char *a, unsigned dividend, bool printZeroes)
{
  if(dividend == 0)
  {
    *a++ = 0;
    return;
  }

  char newVal =  (i / dividend) + '0';
  bool isZero = (newVal == '0');

  if(printZeroes == false)
  {
      printZeroes = !isZero;
  }

  if((isZero && printZeroes) || !isZero)
  {
    *a++ = newVal;
  }

  itoaHelper(i % dividend, a, dividend / 10, printZeroes);
}

void itoa(int i, char *a)
{
  if(i == 0)
  {
    *a++ = '0';
  }

  if(i < 0)
  {
    i = -i;
    *a++ = '-';
  }

  itoaHelper(i, a, 1000000000, false);
}