How do I draw a .png at a given location

I am trying to draw a png at a given location on an LCD. I have found the following example code which works and I can change the the location of the LCD but I want to be able to change the location dynamically by passing integer variables to the function but it looks like the function I call, then calls another function. I confess, this code is completely beyond me but I have tried to understand it. Could someone explain the workings of the code so that I might figure it out?

#include <PNGdec.h>
#include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
#include <SPI.h>
#include "world.h"

#define LCD_WIDTH 320
#define LCD_HEIGHT 240

// Invoke library
TFT_eSPI tft = TFT_eSPI(LCD_HEIGHT, LCD_WIDTH);
PNG png;


void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}


void drawWorld() {
  int16_t rc = png.openFLASH((uint8_t *)world, sizeof(world), mapDraw);
  //int rc;
  //rc = png.open("world.png", myOpen, myClose, myRead, mySeek, PNGDrawLarge);
  if (rc == PNG_SUCCESS) {
    Serial.println("Successfully opened png file");
    Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
    tft.startWrite();
    uint32_t dt = millis();
    rc = png.decode(NULL, 0);
    Serial.print(millis() - dt); Serial.println("ms");
    tft.endWrite();
    // png.close(); // not needed for memory->memory decode
  }
}

//=========================================v==========================================
//                                      pngDraw
//====================================================================================
// This next function will be called during decoding of the png file to
// render each image line to the TFT.  If you use a different TFT library
// you will need to adapt this function to suit.
// Callback function to draw pixels to the display
void mapDraw(PNGDRAW *pDraw) {
  uint16_t lineBuffer[320];
  png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
  tft.pushImage(6, 5 + pDraw->y, pDraw->iWidth, 1, lineBuffer);
}

Welcome to the forum

Please post an example of how you do that

if I edit the following line by changing the 6 and 5, the png will be displayed at the coordinates set but I want to pass this information as variables :-

tft.pushImage(6, 5 + pDraw->y, pDraw->iWidth, 1, lineBuffer);

Then simply replace the values with variables

int x = 6;
int y = 5;
tft.pushImage(x, y + pDraw->y, pDraw->iWidth, 1, lineBuffer);

But the function is called with :-

drawWorld();

I want to call it with something like :-

drawWorld(x,y);

which would require drawWorld() passing the variables to mapDraw(PNGDRAW *pDraw) but how?

You can do that, but you need to add those call arguments to the drawWorld argument list, e.g.
void drawWorld (int x, int y) {

Another option is to declare/define x and y as global variables and access them inside the drawWorld function.

If it were only that simple.

To draw the png I use : -

drawWorld();

Which in turn calls


void mapDraw(PNGDRAW *pDraw)

The line in mapDraw is where the coordinates are used. So I need to pass variables all the way through to mapDraw(). Now notice how mapDraw() is referenced in drawWorld()? The question is, how do I get variables in to mapDraw() from drawWorld() ?

Same two ways. Global variables are the easiest to implement, although I would name x and y something sensible, like xPictureOrigin and yPictureOrigin.