TinyCircuits Forum

TinyCircuits Products => Tiny Arcade & Pocket Arcade => Topic started by: bkumanchik on November 09, 2021, 03:37:16 PM

Title: Text/font
Post by: bkumanchik on November 09, 2021, 03:37:16 PM
How do I write text to the screen while using the  drawBuffer()   function to draw my sprites?

I've created this function (see below) and tried calling it from several different places in the main loop and from within the  drawBuffer()  function, but it doesn't work

Do I have to draw less of the screen with sprites and reserve the top of my screen for text or can I mix text with sprites?

Thanks,

Brian

void writeText() {
  display.setFont(thinPixel7_10ptFontInfo); 
  display.setCursor(0,0);
  display.fontColor(TS_16b_White,TS_16b_DarkBlue);
  display.print("SCORE:");
}

Title: Re: Text/font
Post by: lennevia on November 12, 2021, 01:58:48 PM
Hiya,

Can you share what hardware you are using and the entire program?

For sending text to a screen, you can look at the basic example for the TinyScreen library mentioned in this tutorial: https://learn.tinycircuits.com/Processors/TinyScreen+_Setup_Tutorial/

Otherwise, if you are using Wireling screens, we have an example of text and sprites being printed to the screen at the same time: https://tinycircuits.com/blogs/learn/moving-and-making-graphics-for-a-wireling-0-42-0-69-0-96-screen

Let me know if those tutorials help!

Best,
Réna
Title: Re: Text/font
Post by: bkumanchik on November 13, 2021, 03:36:38 PM
Sorry, I'm using a PocketArcade and the Arduino IDE, I can display sprites and I can display text but I can't seem to display text with sprites using the drawbuffer() function shown in the TinyBrick tutorial:

Thanks,

Brian

Title: Re: Text/font
Post by: lennevia on November 16, 2021, 12:06:27 PM
With your current setup it looks like you need to modify the drawbuffer() function to incorporate printing text. We did something similar in the FlappyBirdz game with a score variable needing printed after being converted to a string. Here's a snippet of the solution in FlappyBirdz, you will likely come up with something more optimizedfor your program:

Code: [Select]
void drawBuffer() {
  uint8_t lineBuffer[96 * 64 * 2];
  display.startData();
  for (int y = 0; y < 64; y++) {
    for (int b = 0; b < 96; b++) {
      lineBuffer[b * 2] = TS_16b_Blue >> 8;
      lineBuffer[b * 2 + 1] = TS_16b_Blue;
    }
    for (int spriteIndex = 0; spriteIndex < amtSprites; spriteIndex++) {
      ts_sprite *cs = spriteList[spriteIndex];
      if (y >= cs->y && y < cs->y + cs->height) {
        int endX = cs->x + cs->width;
        if (cs->x < 96 && endX > 0) {
          int bitmapNumOffset = cs->bitmapNum * cs->width * cs->height;
          int xBitmapOffset = 0;
          int xStart = 0;
          if (cs->x < 0)xBitmapOffset -= cs->x;
          if (cs->x > 0)xStart = cs->x;
          int yBitmapOffset = (y - cs->y) * cs->width;
          for (int x = xStart; x < endX; x++) {
            unsigned int color = cs->bitmap[bitmapNumOffset + xBitmapOffset + yBitmapOffset++];
            if (color != ALPHA) {
              lineBuffer[(x) * 2] = color >> 8;
              lineBuffer[(x) * 2 + 1] = color;
            }
          }
        }
      }
    }
    putString(y, score.x, score.y, score.stringChars, lineBuffer, liberationSans_16ptFontInfo);
    if (startScreen) {
      putString(y, 1, 38, score.stringChars, lineBuffer, liberationSans_10ptFontInfo);
      char hs[10];
      sprintf(hs, "%d", highScore);
      putString(y, 74, 38, hs, lineBuffer, liberationSans_10ptFontInfo);

    }
    display.writeBuffer(lineBuffer, 96 * 2);

  }


  display.endTransfer();
}


void putString(int y, int fontX, int fontY, char * string, uint8_t * buff, const FONT_INFO& fontInfo) {
  const FONT_CHAR_INFO* fontDescriptor = fontInfo.charDesc;
  int fontHeight = fontInfo.height;
  if (y >= fontY && y < fontY + fontHeight) {
    const unsigned char* fontBitmap = fontInfo.bitmap;
    int fontFirstCh = fontInfo.startCh;
    int fontLastCh = fontInfo.endCh;
    //if(!_fontFirstCh)return 1;
    //if(ch<_fontFirstCh || ch>_fontLastCh)return 1;
    //if(_cursorX>xMax || _cursorY>yMax)return 1;
    int stringChar = 0;
    int ch = string[stringChar++];
    while (ch) {
      uint8_t chWidth = pgm_read_byte(&fontDescriptor[ch - fontFirstCh].width);
      int bytesPerRow = chWidth / 8;
      if (chWidth > bytesPerRow * 8)
        bytesPerRow++;
      unsigned int offset = pgm_read_word(&fontDescriptor[ch - fontFirstCh].offset) + (bytesPerRow * fontHeight) - 1;

      for (uint8_t byte = 0; byte < bytesPerRow; byte++) {
        uint8_t data = pgm_read_byte(fontBitmap + offset - (y - fontY) - ((bytesPerRow - byte - 1) * fontHeight));
        uint8_t bits = byte * 8;
        for (int i = 0; i < 8 && (bits + i) < chWidth; i++) {
          if (data & (0x80 >> i)) {
            buff[(fontX) * 2] = TS_16b_Yellow >> 8;
            buff[(fontX) * 2 + 1] = TS_16b_Yellow;
            // setPixelInBuff(y,16+fontX,0);
            //lineBuffer[16+fontX]=0;
          } else {
            //SPDR=_fontBGcolor;
          }
          fontX++;
        }
      }
      fontX += 2;
      ch = string[stringChar++];
    }
  }
}
Title: Re: Text/font
Post by: bkumanchik on November 16, 2021, 12:13:41 PM
Thanks, I'll try this ASAP and get back to you.

Brian
Title: Re: Text/font
Post by: bkumanchik on November 16, 2021, 12:22:40 PM
Can I place a gamepad controllable sprite  on the screen without using the drawbuffer() function ? when I do I get flashing if I want the sprite to erase itself as it moves.

Brian
Title: Re: Text/font
Post by: lennevia on November 17, 2021, 06:33:11 PM
Brian,

Can you attach your program with what you've changed? I'm not sure what you mean by flashing. Are you using the drawBuffer() more than once in your loop()?

Thanks,
Réna
Title: Re: Text/font
Post by: bkumanchik on November 17, 2021, 07:28:05 PM
I wanted to see if I could place a sprite and move it around WITHOUT using the drawbuffer function - if the SDK allows for that - or the drawbuffer function (or something similar) IS required to handle sprites on the PocketArcade. I can use the drawbuffer just fine but adding text into that routine is a little above my current programming skill level as of now. Handling sprites on the Thumby's MicroPython web IDE is much more understandable for me. Some SDK's just require a "clear the screen, set up drawing of sprites/text, draw screen" sort of scenario and I was wondering if that works with the PocketArcade using the given SDK.

I hope this helps, If this is not possible, I'll send you my code thus far and maybe you can show me how to get some text working with what I have.


Thanks for your help,

Brian
Title: Re: Text/font
Post by: lennevia on November 18, 2021, 04:51:21 PM
Brian,

You do need the displaybuffer() function in order to display sprites on the screen. If you look inside the function, the list of sprites in your program are cycled through and displayed one by one. You are welcome to come up with a different method, but this is how all of our game examples work.

You can download the full FlappyBirdz example here which might help: https://tinycircuits.com/blogs/games/178269895-flappy-birdz

Let me know if that helps or if you still have some things to work through!

Best,
Réna
Title: Re: Text/font
Post by: bkumanchik on November 18, 2021, 06:20:22 PM
OK, I figured, I just wanted to make sure that was the case. I have attached my code example, If you run it you can move the turret left/right with the d-pad and fire with button 2, you can shoot the invader and he will explode and reset to a random x and back to the top of the screen, if the turret gets hit it explodes and resets to the center x position.

If you look in my main loop I am using the drawBuffer function and I have commented-out my writeText function, if you uncomment the writeText function and comment the drawBuffer function, you will see my intended text that I'd like to have on the screen with the graphics.

I have tried to implement the writeText function with the drawBuffer function as in the flappy bird demo but I can't seem to get it to work.  Anyhow that's where I'm at at the moment.

Thanks,

Brian