Weird ili9341 glitches

Hi guys

Ok I'm working on adding some gaming aspects to an ili9341 library and I'm having trouble with display coordinates. I have a function called tilemap that is supposed to put the tiles for a world map into a ram array that I can use to display all the different tiles in certain positions.

So as an example I made two 16x16 tiles and used them to make a 6x6 tilemap and called them at 0 and 0. But what happens is it cuts off a third of the map that way. So I tried moving the coordinates more to center and it shrinks it more.

So I added a player Sprite to the mix and put him at 50 and 50 but he doesn't appear at 50 and 50. So I decide to try and center him on the screen by dividing the resolution to half at 160 X 110. But he only moved a tiny bit from the original location.

The bitmap functions are the same as the adafruit GFX but I can't figure out why I can't center the bitmaps.

I've not seen any "glitches" like this on a working ILI9341 display so the problem must be in the sketch you have written, in the TFT library you are using or a bad setup for a different display driver/pixel count display. Most libraries are well debugged so check to see if you have a badly hacked copy.

Run an example from the library that draws over the whole screen. rather than code you have generated to see if the display works. Check the library is compatible with the display driver you have and is correctly configuring the display with the actual width and height of the display (pixel count width and height).

The code bugs should be easy to track down if you add some debug print statements so, for example, you can see the results of any coordinate calculations you are performing.

Watch out for variable over-flows that cause coordinates to go negative as the library draw function will probably just abort and return without warning.

I’m actually using sumotoys ILI93xx_master library on git hub that claims to be fasters than Paul stroffegen and KurtE’s libraries. Before updating the library I fully tested the teensy and the ili9341 screen before adding any extra functions like the tilemap() function. Every thing worked but im starting to think its my code.

here are the bitmap commands

//GAMEBUINO----SUMMONER123------for tilemap--Duhjoker
	void drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color);
	boolean getBitmapPixel(const uint8_t* bitmap, uint8_t x, uint8_t y);

	void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint16_t color);
	void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color);

and here are the .cpp version

//GAMEBUINO
void TFT_ILI93XX::drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color) {
	int8_t i, j, byteWidth = (w + 7) / 8;
	dw += dx;
	dh += dy;
	int8_t largest = 0;
	int8_t largesty = 0;
	for (j = 0; j < h; j++) {
		for (i = 0; i < w; i++) {
			if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (B10000000 >> (i % 8))) {
				int8_t drawX = x + i;
				int8_t drawY = y + j;

				if (drawX >= dx && drawX < dw && drawY >= dy && drawY < dh) {
					drawPixel(x, y, color);
				}
			}
		}
	}
}


void TFT_ILI93XX::drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint16_t color) {
	drawTilemap(x, y, tilemap, spritesheet, 0, 0, TFT_ILI93XX_TFTWIDTH, TFT_ILI93XX_TFTHEIGHT, color);
}

void TFT_ILI93XX::drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color) {
	uint8_t tilemap_width = pgm_read_byte(tilemap);
	uint8_t tilemap_height = pgm_read_byte(tilemap + 1);
	uint8_t tile_width = pgm_read_byte(tilemap + 2);
	uint8_t tile_height = pgm_read_byte(tilemap + 3);
	tilemap += 4; // now the first tiyleis at tilemap
	uint8_t ddw = dw + dx;
	uint8_t ddh = dh + dy;
	uint8_t maxDdx = (dw - x + tile_width - 1) / tile_width;
	uint8_t maxDdy = (dh - y + tile_height - 1) / tile_height;
	if (tilemap_width < maxDdx) {
		maxDdx = tilemap_width;
	}
	if (tilemap_height < maxDdy) {
		maxDdy = tilemap_height;
	}
	int8_t startDdx = (-x) / tile_width;
	int8_t startDdy = (-y) / tile_height;
	if (startDdx < 0) {
		startDdx = 0;
	}
	if (startDdy < 0) {
		startDdy = 0;
	}
	if (flagcollision)numcolision = 0;                                 //Line 735 - clear numcolision - ADD by Summoner123

	for (uint8_t ddy = startDdy; ddy < maxDdy; ddy++) {
		for (uint8_t ddx = startDdx; ddx < maxDdx; ddx++) {
			int8_t drawX = ddx*tile_width + x + dx;
			int8_t drawY = ddy*tile_height + y + dy;
			uint8_t tile = pgm_read_byte(tilemap + ddy*tilemap_width + ddx);
			if (drawX >= dx && drawY >= dy && drawX <= (ddw - tile_width) && drawY <= (ddh - tile_height)) {
				drawBitmap1(drawX, drawY, spritesheet[tile], tile_width, tile_height, color);

				if (flagcollision) {
					solid[numcolision].x = drawX;                     //Save X coordinate      - ADD by Summoner123
					solid[numcolision].y = drawY;                     //Save Y coordinate      - ADD by Summoner123
					solid[numcolision].spritecol = spritesheet[tile]; //Save Sprite of tile    - ADD by Summoner123
					numcolision++;                                    //Increment numcolision  - ADD by Summoner123
				}
			}
			else { // we need to draw a partial bitmap
				drawBitmap(drawX, drawY, tile_width, tile_height, spritesheet[tile], dx, dy, dw, dh, color);
			}
		}
	}
}

here is the rest

oid TFT_ILI93XX::drawBitmap1(int16_t x, int16_t y,
	const uint8_t *bitmap, int16_t w, int16_t h,
	uint16_t color) {

	int16_t i, j, byteWidth = (w + 7) / 8;

	for (j = 0; j<h; j++) {
		for (i = 0; i<w; i++) {
			if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
				drawPixel(x + i, y + j, color);
			}
		}
	}
}

boolean TFT_ILI93XX::getBitmapPixel(const uint8_t* bitmap, uint8_t x, uint8_t y) {
	return pgm_read_byte(bitmap + 2 + y * ((pgm_read_byte(bitmap) + 7) / 8) + (x >> 3)) & (B10000000 >> (x % 8));
}

the basic setup zip is the sketch and the other the current update of the library

Heres what i get from the tilemap

TFT_ILI93XX-master_6.zip (217 KB)

basicSetup5.zip (2.93 KB)

Not sure what display you are using but using an 8 bit value for coordinates would not be a good idea if you have a display that is 320 pixels wide.

Sorry, I can't spend any time looking at this. I think you should be able to work out what is happening quite quickly by adding some debug statements.

I see the problem now. Thank you. I poured over all that for hours. Some times you just need a second set of eyes.