Need help with drawing part of an image

Hello,

Thank you Bodmer for your awesome library.

I'm trying to draw part of an image ( a sprite from a spritesheet ).

My image is 64 by 64.

I can draw the image entirely, the top half ( 64x32 ) but when i try to draw the left half (32x64) or juste the top-left square (32x32) it seems to take part of the image and mix it and stretch it.

Original image:

Sprite-0002

Code:



// Set delay after plotting the sprite
#define DELAY 30


#include "sample_images.h"

TFT_eSPI    tft = TFT_eSPI();         // Declare object "tft"

TFT_eSprite spr2 = TFT_eSprite(&tft);
TFT_eSprite spr3 = TFT_eSprite(&tft);
TFT_eSprite spr4 = TFT_eSprite(&tft);
TFT_eSprite spr5 = TFT_eSprite(&tft);

void setup()
{
  Serial.begin(9600);
  Serial.println();

  delay(50);

  tft.init();
  tft.setRotation(0);

  spr2.setColorDepth(8);
  spr3.setColorDepth(8);
  spr4.setColorDepth(8);
  spr5.setColorDepth(8);

  spr2.createSprite(64,64);
  spr2.setSwapBytes(true);

  spr3.createSprite(64,32);
  spr3.setSwapBytes(true);

  spr4.createSprite(32,64);
  spr4.setSwapBytes(true);

  spr5.createSprite(32,32);
  spr5.setSwapBytes(true);
  
  tft.fillScreen(TFT_CYAN);
  
  

  spr2.pushImage(0, 0, 64,64, (uint16_t *)stars2);
  spr2.pushSprite(0, 0, 0);

  spr3.pushImage(0, 0, 64,32, (uint16_t *)stars2);
  spr3.pushSprite(0, 96, 0);

  spr4.pushImage(0, 0, 32, 64, (uint16_t *)stars2);
  spr4.pushSprite(0, 160, 0);

  spr5.pushImage(0, 0, 32,32, (uint16_t *)stars2);
  spr5.pushSprite(0, 256, 0);

  
}

void loop(void)
{
  
}


Am i using the correct method ?
If i'm using the wrong method i can't unterstand why sp3 would work but not sp4 and spr 5.

Any help is welcome.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

Result:

20220423_145038

Sorry for the spaghetti.
I had no warning message exept the "need for approval".
The preview panel was hidden by a welcome panel.
To edit it, I had to take out one image since i am a "new" user but when i first copypasted the post no warnings.

thanks

I've never used this but reading the API, if you want to extract a subset of your sprite (a windowed area) you can use the pushSprite() method with the right parameters

what about testing with

#include <TFT_eSPI.h>
#include "sample_images.h"
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft);

void setup() {
  tft.init();
  tft.setRotation(0);
  tft.fillScreen(TFT_CYAN);

  spr2.setColorDepth(8);
  spr2.createSprite(64, 64);
  spr2.setSwapBytes(true);
  spr2.pushImage(0, 0, 64, 64, (uint16_t *)stars2);

  spr2.pushSprite(0, 0, 0);
  
  // Push a windowed area of the sprite to the TFT at tx, ty
  spr2.pushSprite(0, 96, 0, 0, 64, 32);
  spr2.pushSprite(0, 160, 0, 0, 32, 32);
  spr2.pushSprite(0, 256, 0, 32, 32, 32);
}

void loop(void) { }

Thank You @J-M-L , using pushSprite like you say work but that way i can't have transparency.

Since my goal is a game with isometric tiles i need transparency.
I will have to forget spritesheets until i find a workaround.

By looking at how the cropping is done and how transparency is managed in the other functions, you could possibly write your own method in the library or subclass to handle transparency

The challenge with a cropped image is that the pixels are not contiguous in memory so you need to draw line by line

Thanks to Bodmer, I managed to print all four part of a 32x128 image.

The image:

Sprite-0002 - Copie

The code with a litlle animation :

#include <TFT_eSPI.h>
#include "sample_images.h"
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr2 = TFT_eSprite(&tft);
TFT_eSprite spr3 = TFT_eSprite(&tft);

void setup() {
  tft.init();
  tft.setRotation(0);
  tft.fillScreen(TFT_CYAN);

  spr2.setColorDepth(16);
  spr3.setColorDepth(16);

  spr2.createSprite(128,32);
  spr2.setSwapBytes(true);
  
  spr3.createSprite(32,32);
  spr3.setSwapBytes(true);

  spr2.pushImage(0, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr2.pushSprite(0, 0, TFT_WHITE);
  
  spr3.pushImage(0, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(0, 64, TFT_WHITE);

  spr3.pushImage(-32, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(64, 64, TFT_WHITE);

  spr3.pushImage(-64, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(128, 64, TFT_WHITE);

  spr3.pushImage(-96, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(192, 64, TFT_WHITE);
}
void loop(void) {

  spr3.pushImage(0, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(32,128, TFT_WHITE);
  delay(1000);

  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(32,128, TFT_WHITE);
  spr3.pushImage(-32, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(32,128, TFT_WHITE);
  delay(1000);

  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(32,128, TFT_WHITE);
  spr3.pushImage(-64, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(32,128, TFT_WHITE);
  delay(1000);

  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(32,128, TFT_WHITE);
  spr3.pushImage(-96, 0, 128, 32, (uint16_t *)stars3,TFT_BLACK);
  spr3.pushSprite(32,128, TFT_WHITE);
  delay(1000);

  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(32,128, TFT_WHITE);
 }

@J-M-L I will look into your suggestion when i will be more confortable with c++.