Storing images or image data on SD (gaming)

Status
Not open for further replies.
I got it working properly. I went ahead and just made 4 new if functions using the extra parenthesis example posted earlier in the thread and transcribed it to the other three movements. I made progress by adding last x and last y in the loop past the last movement if. left started repeating along with down and right. I looked a little closer and saw the move repeat integers were off. The pwm 400's need to be set to 2 and the 600's set to one and all is working.

Code:
  ///////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////
  int y = ss1.analogRead(2);
  int x = ss1.analogRead(3);
#define MoveRepeatRate 100 // set this for how long in milis to wait for auto repeat move

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if ( (x > 600) && ( (last_x < 600) || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate ) ) ) {
    Serial.println(F("UP")); ///RIGHT
    MoveRepeat = 0;
    player.player_directionRepeat = 1;
    tft.writeRectNBPP(player.player_x, player.player_y,  16, 16, 4, paulrearwa, palette); //// this mixed with the call below it calls bitmaps for walking
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrearwb, palette); ///// read above

    player.player_direction = 1; ////when movement ends player bitmap faces this direction paul rear
    player.player_y -= 4;    ///move player y - 4 pixels
    if (checkcolision())
    {
      player.player_y += 4; ///causes player to stop when collision happens
      player.player_directionRepeat = 0;
    }
    last_x = x;   ////added this
    last_y = y;   ////added this
  }
  if (player.player_y <= 16) {  ////keeps player from walking off the screen.
    player.player_y = 16;

  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  if ( (x < 400) && ( (last_x > 400) || ( 2 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate ) ) ) {
    Serial.println(F("DOWN")); ///LEFT
    MoveRepeat = 0;
    player.player_directionRepeat = 2;
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfrontwa, palette);
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulfrontwb, palette);

    player.player_direction = 2; ////when movement ends player bitmap faces this direction paul rear
    player.player_y += 4;    ///move player y - 4 pixels
    if (checkcolision())
    {
      player.player_y -= 4;
      player.player_directionRepeat = 0;
    }
    last_x = x;    ////added this
    last_y = y;    ////added this
  }
  if (player.player_y >= 224) {
    player.player_y = 224;
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////
  if ( (y < 400) && ( (last_y > 400) || ( 2 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate ) ) ) {
    Serial.println(F("LEFT"));   ///DOWN
    MoveRepeat = 0;
    player.player_directionRepeat = 2;
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulleftw, palette);
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulleft, palette);

    player.player_direction = 3;
    player.player_x -= 4;
    if (checkcolision())
    {
      player.player_x += 4;
      player.player_directionRepeat = 0;
    }
    last_x = x;      ////added this
    last_y = y;       ////added this
  }
  if (player.player_x >= 304) {
    player.player_x = 304;
  }
  ///////////////////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////////////////
  if ( (y > 600) && ( (last_y < 600) || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate ) ) ) {
    Serial.println(F("RIGHT")); ///UP
    MoveRepeat = 0;
    player.player_directionRepeat = 1;
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrightw, palette);
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulright, palette);

    player.player_direction = 4;
    player.player_x += 4;
    if (checkcolision())
    {
      player.player_x -= 4;
      player.player_directionRepeat = 0;
    }
    last_x = x;      ////added this
    last_y = y;      ////added this
  }
  if (player.player_x <= 16) {
    player.player_x = 16;
  }


Thank you by the way!! You taught me new piece of programming!!!!
 
Glad you got it working! - the idea seemed sound enough from what I saw - but I was only seeing pieces and not what/how you did or what you saw to debug it.
 
its lil splotchy though and now I'm having a collision problem. Its colliding but my popup messages only flash because now the player wont stay on the collision to keep the text bubble up on the screen. something to do with move direction repeat 0 being with in the collision call. I tried to move it out of the collision () brackets but it causes the repeat to fail.

Code:
  ///////////////////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////////////////
  if ( (y > 600) && ( (last_y < 600) || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate ) ) ) {
    Serial.println(F("RIGHT")); ///UP
    MoveRepeat = 0;
    player.player_directionRepeat = 1;
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrightw, palette);
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulright, palette);

    player.player_direction = 4;
    player.player_x += 4;
    if (checkcolision())
    {
      player.player_x -= 0;
      player.player_directionRepeat = 0;  ////// messes with collision
    }
    last_x = x;      ////added this
    last_y = y;      ////added this
  }
  if (player.player_x <= 16) {
    player.player_x = 16;
  }

the analog stick works ok. when using the stick sans repeat the the controls are super responsive. With the repeat it might move the first time in a given direction or the fifth time.
 
I changed the repeat frequency to 50 which looks better with the animation but the response time is still spotty and some times you have to push the stick in the reverse direction before the opposite side works. Other times it works fine. I'm wondering if decreasing the pwm frequency in the function would help.
 
I fixed the collision so the popups stay up while the player is touching a trigger bitmap. it was as simple as replacing the integer in direction repeat to the same integer used in the direction repeat at top. I know that sounds confusing so ill post the correction....

Code:
  ///////////////////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////////////////
  if ( (y > 600) && ( (last_y < 600) || ( 1 == player.player_directionRepeat && MoveRepeat >= MoveRepeatRate ) ) ) {
    Serial.println(F("RIGHT")); ///UP
    MoveRepeat = 0;
    player.player_directionRepeat = 1;
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulrightw, palette);
    tft.writeRectNBPP(player.player_x, player.player_y, 16, 16, 4, paulright, palette);

    player.player_direction = 4;
    player.player_x += 4;
    if (checkcolision())
    {
      player.player_x -= 0;
      player.player_directionRepeat = 1;   /////////////change this 
    }
    last_x = x;      ////added this
    last_y = y;      ////added this
  }
  if (player.player_x <= 16) {
    player.player_x = 16;
  }

marked as change this
 
On my GameR console that uses buttons, I can move the player in a northwest direction by pressing both left and up buttons. I would like to be able to use slanted directions but I cant tell if its gonna be a hardware problem since currently moving the stick at an angle does nothing but up or left. I think I can figure out the mixture of pwms but I'm not sure how I would make a direction which is either x or y to x and y
 
That goes back to a question posed for directionRepeat. Perhaps '|' OR'ing the L (0x1),R(0x2) with either U(0x4),D(0x8) when both are indicated. OR'ing for setting then '&' AND testing and then masking it away when no longer indicated.
 
Why not map the analog joystick values from 0...1024 to a point on the unit circle (-1.0...1.0)?

Code:
include <math.h>

float xcoord = (float) x * (2/1024) - 1.0;
float ycoord = (float) y * (2/1024) - 1.0;

Then you can check the angle the joystick is being moved towards with

Code:
float angle = degrees(atan2f(xcoord, ycoord));

And how far it's being pushed (from 0 to 1.0) with

Code:
float length = sqrtf(xcoord * xcoord + ycoord * ycoord)
 
I'm trying to add a button function for the collision triggers for room changes and text and what ever so when I'm bumping into people objects they only popup when collision is made and "B" button is pressed. The one I'm working on is supposed to do 4 things, make a pop up message, add an item to inventory, serial print a message and return true so its solid.

I'm trying to get it to at least but so far no go. In fact it does nothing at all. But it does compile.
Code:
////////////////works
else if ((tft.solid[i].spritecol == fremenfbrownl1) && player.room == 3) {
          tft.Popup(F(" ""Many things can be made from spice"" "), 1, 55, 20);
          tft.Popup3(F(" ""solids."" "), 1, 130, 30);
          return true;
        }

//////////////////////////////////////////////////////////////////////////////////////
////does nothing
        else if ((tft.solid[i].spritecol == chest) && player.room == 3 && (! (buttons & (1 << BUTTON_A)))) {
          tft.Popup(F(" ""You got Spice"" "), 1, 90, 25);
          added = addItemToInventory(ITEM_Spice);
          if (added)  Serial.println("Added a Spice into slot 0");

          return true;
        }

heres the function in its entirety

Code:
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////collision/////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
bool checkcolision(void) // Transformed it into a function
{
  for (uint16_t i = 0; i < tft.numcolision + 1; i++)
  {
    if (tft.collideRectRect(player.player_x, player.player_y, 16, 16, tft.solid[i].x, tft.solid[i].y, 16, 16))
    {
      //// for inventory ////
      bool added = false;
      for (uint8_t i = 0; i <  NUMBER_OF_SLOTS; i++) {

        Slot *slot = &slots[i];
        slot->quantity = 0;
        slot->itemId = SLOT_AVAILABLE;

      }

      if (!digitalRead(IRQ_PIN2)) {
        uint32_t buttons = ss2.digitalReadBulk(button_mask2);

        if (tft.solid[i].spritecol == bedtop)return true;
        else if (tft.solid[i].spritecol == bedbot)return true;
        else if (tft.solid[i].spritecol == bones11)return true;
        else if (tft.solid[i].spritecol == bones12)return true;
        else if (tft.solid[i].spritecol == bones13)return true;
        else if (tft.solid[i].spritecol == bones21)return true;
        else if (tft.solid[i].spritecol == bones22)return true;
        else if (tft.solid[i].spritecol == bones23)return true;
        else if (tft.solid[i].spritecol == bookstop )return true;
        else if (tft.solid[i].spritecol == booksbot)return true;
       //////etc
 else if ((tft.solid[i].spritecol == stonecavei1) && player.room == 1) {
          player.room = 2;
          player.player_x = 128;
          player.player_y = 140;
          cameraX = -0;
          cameraY = -704;
          cameraXMin = -0;
          cameraYMin = -704;
        }
        else if ((tft.solid[i].spritecol == stonecaveo1) && player.room == 2) {
          player.room = 1;
          player.player_x = 144;
          player.player_y = 160;
          cameraX = -416;
          cameraY = -368;
          cameraXMin = -960;
          cameraYMin = -1024;
        }
        else if ((tft.solid[i].spritecol == ladderu1) && player.room == 2) {
          player.room = 3;
          player.player_x = 144;
          player.player_y = 170;
          cameraX = -128;
          cameraY = -254;
          cameraXMin = -336;
          cameraYMin = -448;
        }
        else if ((tft.solid[i].spritecol == ladderd1) && player.room == 3) {
          player.room = 2;
          player.player_x = 80;
          player.player_y = 112;
          cameraX = -0;
          cameraY = -0;
          cameraXMin = -0;
          cameraYMin = -704;
        }

 else if ((tft.solid[i].spritecol == fremenfbrownl1) && player.room == 3) {
          tft.Popup(F(" ""Many things can be made from spice"" "), 1, 55, 20);
          tft.Popup3(F(" ""solids."" "), 1, 130, 30);
          return true;
        }


      else if ((tft.solid[i].spritecol == chest) && player.room == 3 && (! (buttons & (1 << BUTTON_A)))) {
          tft.Popup(F(" ""You got Spice"" "), 1, 90, 25);
          added = addItemToInventory(ITEM_Spice);
          if (added)  Serial.println("Added a Spice into slot 0");

          return true;
        }

      }

    }
    return false; // Return false if don't touch anything
  }
}
 
Status
Not open for further replies.
Back
Top