Using more than 256 sprites with tilemap function?

Hi guys 

I’m having trouble with the gamebuino’s tilemap function. I would like to use more than 256 sprites in my sprite sheet and I have made a few changes that I thought would help but for any sprite after 256 that I try to print out using the tilemap function just prints out a random sprite from the list.

Here we have summoner123’s original function…

void Display::drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet){
    drawTilemap(x,y,tilemap,spritesheet,0,0,LCDWIDTH,LCDHEIGHT);
}
void Display::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){
    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)){
                drawBitmap(drawX,drawY,tile_width,tile_height,spritesheet[tile]);

				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);
            }
        }
    }
}

we should all know how to use the function by now but if you don’t, let me know and I will make a short example.

Now we have my updated version. The first thing I changed was being able to print out maps bigger than 256 tiles. The commented out parts are the original pices of code.

void GrafxT3::drawTilemap(int x, int y, const uint16_t *tilemap, const uint16_t **spritesheet, const uint16_t * palette){
	drawTilemap(x, y, tilemap, spritesheet, 0, 0, GrafxT3_TFTHEIGHT, GrafxT3_TFTWIDTH, palette);
}

void GrafxT3::drawTilemap(int x, int y, const uint16_t *tilemap, const uint16_t **spritesheet, uint16_t dx, uint16_t dy, uint16_t dw, uint16_t dh, const uint16_t * palette){
 //  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
 uint16_t tilemap_width = pgm_read_byte(tilemap)* 256 + pgm_read_byte(tilemap + 1);
 uint16_t tilemap_height = pgm_read_byte(tilemap + 2)* 256 + pgm_read_byte(tilemap + 3);
 uint16_t tile_width = pgm_read_byte(tilemap + 4);
 uint16_t tile_height = pgm_read_byte(tilemap + 5);
 tilemap += 6; // now the first tile is at tilemap

	uint16_t ddw = dw + dx;
	uint16_t ddh = dh + dy;
	uint16_t maxDdx = (dw - x + tile_width - 1) / tile_width;
	uint16_t maxDdy = (dh - y + tile_height - 1) / tile_height;
	if (tilemap_width < maxDdx){
		maxDdx = tilemap_width;
	}
	if (tilemap_height < maxDdy){
		maxDdy = tilemap_height;
	}
	int16_t startDdx = (-x) / tile_width;
	int16_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 (uint16_t ddy = startDdy; ddy < maxDdy; ddy++){
		for (uint16_t ddx = startDdx; ddx < maxDdx; ddx++){
			int16_t drawX = ddx*tile_width + x + dx;
			int16_t drawY = ddy*tile_height + y + dy;
			uint16_t tile = pgm_read_byte(tilemap + ddy*tilemap_width + ddx);
			if (drawX >= dx && drawY >= dy && drawX <= (ddw - tile_width) && drawY <= (ddh - tile_height)){
				writeRectNBPP(drawX, drawY,tile_width, tile_height, 4, spritesheet[tile], palette );

				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
				writeRect4BPPtm(drawX, drawY, tile_width, tile_height, spritesheet[tile], dx, dy, dw, dh, palette);
			}
		}
	}
}

we use it like this…

const uint16_t arakeen1l1[] = {0,25,0,25,
16,16,
109,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,110,
108,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,107,
108,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,107,

1 is 256 then we add 25 to it. If the map is smaller than 256 tiles we go 0,175, or however big it is.

As you can see I have it set up to use unsigned 16 for the spritesheet and my all my bitmaps and maps use unsigned as well. So I’m really confused as to why I’m getting this glitch.

Can someone please take a look and see what I’m doing wrong?

I am not convinced, I suffer from similar symptoms in the past.

Using 16 x 16,256 sprites. requires 8192 bytes of memory.
Does the actual program store sprites in PROGMEM?

My flash chip is 1mb.

Although const can not be changed, it is copied to RAM.
PROGMEM is not copied from flash to RAM.
maybe.

So I asked if you are using PROGMEM.

In a way yes. Teensy uses flash over progmem, it was explained to me that its the same thing. You use

const uint16_t bitmap[] progmem = {

or

const uint16_t bitmap[] = {

they go to the same place.

post 183
https://forum.pjrc.com/threads/45022-Using-frame-buffering-to-solidify-tft-displayed-objects/page8

Yes.
I have learned a lot.

At least you learned it the first time. Lol!!! that wasn’t the first time it was explained to me.

Does anyone have any clues as to why I cant print more tiles than 256?