Dynamically build a background for BPLAN from tiles at runtime

SGDK only sub forum

Moderator: Stef

Post Reply
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Dynamically build a background for BPLAN from tiles at runtime

Post by matteus » Mon Sep 14, 2015 6:49 pm

Hi,
I have 8 different 8x8 tiles that I'd like to random load and position in BPLAN. All tiles share the same palette but I'm not sure how to assemble them into a Image at runtime. Does anyone have an advice on this? It will be warmly welcome as always :)

Hik
Very interested
Posts: 68
Joined: Thu Jul 30, 2015 11:39 pm
Location: Iceland

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by Hik » Mon Sep 14, 2015 7:23 pm

Maybe you could use a 2d array for this along with a random number generator where each number is a certain tile.
The random numbers could be put into the 2d array and the array would then build up an image.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by matteus » Mon Sep 14, 2015 7:59 pm

Hik wrote:Maybe you could use a 2d array for this along with a random number generator where each number is a certain tile.
The random numbers could be put into the 2d array and the array would then build up an image.
I've already got that far :D The issue I'm having is how do you create the image? :) I understand I can possibly use a tile-set via rescomp but I wouldn't know where to begin after that :)

Hik
Very interested
Posts: 68
Joined: Thu Jul 30, 2015 11:39 pm
Location: Iceland

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by Hik » Mon Sep 14, 2015 8:42 pm

If it has to be on the BPLAN then the best way I know of how to do it so far is to individually load each tile as an image.
I'm thinking you can just load that one palette and have all the tile images use that same one.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by matteus » Mon Sep 14, 2015 8:52 pm

Hik wrote:If it has to be on the BPLAN then the best way I know of how to do it so far is to individually load each tile as an image.
I'm thinking you can just load that one palette and have all the tile images use that same one.
That won't work as the PAL data will be different for each image you load in using ResComp :) I think I need to load a tileset and then indicate where I want each tile vdp_tile.h has the functions I'm just not sure on the combo I need :)

Hik
Very interested
Posts: 68
Joined: Thu Jul 30, 2015 11:39 pm
Location: Iceland

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by Hik » Mon Sep 14, 2015 9:18 pm

matteus wrote:
Hik wrote:If it has to be on the BPLAN then the best way I know of how to do it so far is to individually load each tile as an image.
I'm thinking you can just load that one palette and have all the tile images use that same one.
That won't work as the PAL data will be different for each image you load in using ResComp :) I think I need to load a tileset and then indicate where I want each tile vdp_tile.h has the functions I'm just not sure on the combo I need :)
The code worked for me when using sprites. I'm thinking images work the same.
Just make sure if you put the palette into palette 0 (with memcpy(&palette[0]... and VDP_setPalette(0 ... ) that you put PAL0
into the code for each image you load (right after TILE_ATTR_FULL).

Code: Select all

	// load background
	ind = TILE_USERINDEX;
	VDP_drawImageEx(BPLAN, &placeholder1_image, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 8, 8, FALSE, TRUE);
    ind += placeholder1_image.tileset->numTile;

	VDP_drawImageEx(BPLAN, &placeholder2_image, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 8, 8, FALSE, TRUE);
    ind += placeholder2_image.tileset->numTile;
    
    	memcpy(&palette[0], placeholder1_image.palette->data, 16 * 2);
	VDP_setPalette(0, placeholder1_image.palette->data);
    

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by matteus » Mon Sep 14, 2015 9:44 pm

Hik wrote:
matteus wrote:
Hik wrote:If it has to be on the BPLAN then the best way I know of how to do it so far is to individually load each tile as an image.
I'm thinking you can just load that one palette and have all the tile images use that same one.
That won't work as the PAL data will be different for each image you load in using ResComp :) I think I need to load a tileset and then indicate where I want each tile vdp_tile.h has the functions I'm just not sure on the combo I need :)
The code worked for me when using sprites. I'm thinking images work the same.
Just make sure if you put the palette into palette 0 (with memcpy(&palette[0]... and VDP_setPalette(0 ... ) that you put PAL0
into the code for each image you load (right after TILE_ATTR_FULL).

Code: Select all

	// load background
	ind = TILE_USERINDEX;
	VDP_drawImageEx(BPLAN, &placeholder1_image, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 8, 8, FALSE, TRUE);
    ind += placeholder1_image.tileset->numTile;

	VDP_drawImageEx(BPLAN, &placeholder2_image, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 8, 8, FALSE, TRUE);
    ind += placeholder2_image.tileset->numTile;
    
    	memcpy(&palette[0], placeholder1_image.palette->data, 16 * 2);
	VDP_setPalette(0, placeholder1_image.palette->data);
    
Mmm I'll have a go with this tomorrow see what happens :) I'm not sure if the method you're suggest wastes VRAM as essentially every call you're making there is allocating Vram to each tile you load. While what needs to happen in my case is I load 8 tiles at 8x8 and reference them at specific points.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by matteus » Tue Sep 15, 2015 12:19 pm

I expected the following code to simply load the tiles into VRAM at the next free position:

Code: Select all

       
        ind = IndMarker;

        VDP_setPalette(PAL2, map_tileset.palette->data);

        VDP_loadTileSet(map_tileset.tileset, ind, 1);
But what I end up with is this!
Image

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Dynamically build a background for BPLAN from tiles at runtime

Post by matteus » Tue Sep 15, 2015 12:25 pm

Fixed it!!! Wasn't updating IndMarker and for some reason it was just completely filling the rest of VRAM with the same tile?!? :)

Post Reply