dealing with matrices (arrays)

SGDK only sub forum

Moderator: Stef

nemezes
Interested
Posts: 19
Joined: Sat Mar 31, 2018 1:09 pm

dealing with matrices (arrays)

Post by nemezes » Mon Jul 02, 2018 3:43 am

hello, I am starting to study how to code with SGDK.

I am also new to C programming.

I wanna know how to declare a matrix (array) and how to read it.

like this:

declare a matrix (array) with 5 rows x 8 columns:

Code: Select all

1, 0, 0, 0, 0, 0, 0, 0
1, 0, 0, 2, 2, 2, 0 ,0
1, 0, 0, 0, 0, 0, 0, 0
1, 0, 0, 0, 0, 0, 0, 0
1, 1, 1, 1, 1, 1, 1, 1
then I wanna transform it in a variable like Matrix[x=row,y=column] to read like Matrix[1,1] will return 1, Matrix[2,4] will return 2.

is it possible? how I declare it? :D :?:

thanks in advance.

User avatar
Stef
Very interested
Posts: 2757
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: dealing with matrices (arrays)

Post by Stef » Mon Jul 02, 2018 8:24 am

You should really get a bit more familiar with C before trying to develop with SGDK. Handling a new programming language and a new platform is imo too much to tackle at once ! Of course you can do what you're talking about in C :
https://www.tutorialspoint.com/cprogram ... arrays.htm

But that is a basic basic concept of C so you should really get into a C tutorial first, then when you feel a bit more confortable with it you may try to play with SGDK :)

Chilly Willy
Very interested
Posts: 2571
Joined: Fri Aug 17, 2007 9:33 pm

Re: dealing with matrices (arrays)

Post by Chilly Willy » Mon Jul 02, 2018 3:40 pm

While you can always do your own indexing to use multi-dimensional arrays in a single dimension, C can also handle multi-dimensional arrays as well.

https://www.tutorialspoint.com/cprogram ... arrays.htm

nemezes
Interested
Posts: 19
Joined: Sat Mar 31, 2018 1:09 pm

Re: dealing with matrices (arrays)

Post by nemezes » Mon Jul 02, 2018 4:42 pm

thank you all!

it took me some time, but I get succeed of what I want to do.

I already know how to deal with the limitations of mega drive since I was working with BEX (BasiEgaXorz).

I am trying to reproduce the engine that I used from BEX to SGDK. it uses a collision array and the background over it.

the collision array is working, it is showed below. I map the position x,y of the player with the array mapa[y][x].
1=empty, 2=solid, 3=platform jump through :D

in theory, it can go up to 15 maps of 64 tiles width = 7680 pixels width. in this case, we should create an array of mapa[28][960]. it will have the mapa[28][960] running in "secret" and the graphich background on screen.

Code: Select all

fix16 mapa[28][40] =
{
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //0
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //1
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //2
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //3
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //4
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //5
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //6
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //7
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //8
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //9
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //10
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //11
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //12
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //13
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //14
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //15
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //16
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //17
        {1,2,1,1,1,1,1,1,1,1,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //18
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //19
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,3,3,3,3,3,3,2,1,1}, //20
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //21
        {1,2,1,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //22
        {1,2,1,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //23
        {1,2,1,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //24
        {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, //25
        {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, //26
        {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}  //27
};
it is easier to work with SGDK than BEX, but I have to know how the structure of the code works.

I just dont get what "u16" and "u32" are for.

now I have to know how to clean up the sprites as in BEX. I suppose that the max size of image for background is 64x64 tiles, then I will have to figure out how to scrolling bigger maps in a way I understand. also, how to make a main loop for the game, to go with different levels, each level has his own collision array. :D

here is a small preview of what I achieved: http://www.youtube.com/watch?v=7IFMk5avsgU

Chilly Willy
Very interested
Posts: 2571
Joined: Fri Aug 17, 2007 9:33 pm

Re: dealing with matrices (arrays)

Post by Chilly Willy » Mon Jul 02, 2018 10:42 pm

u16 is an unsigned short int, while u32 is an unsigned long int (the long part is optional). If your collision array can only have values between 0 and 255, you should make it a byte array to take less space. If it's in the rom, it's not as big a deal, but you should use const in front of the array type to make sure it goes into rom, like this

Code: Select all

const unsigned char mapa[28][40] =
{
        {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1}, //0
        etc...
}
That makes a read-only array of unsigned bytes.

nemezes
Interested
Posts: 19
Joined: Sat Mar 31, 2018 1:09 pm

Re: dealing with matrices (arrays)

Post by nemezes » Tue Jul 03, 2018 2:19 am

thanks, Chilly Willy. I followed your tip.

another doubt: is there a way to load a map gfx that is about 120 tiles width x 28 tiles height (it uses only 3 different tiles), and then draw only tiles from 30 to 55, like draw only 25 tiles width x 28 tiles height?

also, is there a simple tool to export an image to u32 array? like it is in scrolling example. I tried imagenesis, but the C export is a char unsigned array.

I looked VDP_drawImageEx function at sprite sample that comes with SGDK but it only draws everything, starting from tile 0.

I also looked the function VDP_setMapEx, it may fit to what I want as it draws part of a map, but I have to load a map of tiles and I dont get how to load this map, I think it is just to export the image to C.

Miquel
Very interested
Posts: 292
Joined: Sat Jul 30, 2016 12:33 am

Re: dealing with matrices (arrays)

Post by Miquel » Tue Jul 03, 2018 12:09 pm

About that matrix:
- You definitely want the first dimension of the matrix to be a 2^n number, like:
map[?][32]
to avoid divisions and such.
- Probably the contiguous space better be the columns, this way you can have variable width (works because for the looks of it you only do x-axis scroll).
If you are looking for a visual representation of the collision matrix consider using a conversion tool.
- Consider using structures of 2x2 tiles
- Consider linking the 2x2 tiles structures to a 2x2 (or even 1x1) collision structures (like your mapa variable but directly associated to tiles)
At light speed you can see quite clear close and distant starts alike with a solid trail of light, like in a highway close ones go more quickly.
But the close ones have a blue glow (but not a trail), kind of Doppler effect perhaps?

User avatar
Stef
Very interested
Posts: 2757
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: dealing with matrices (arrays)

Post by Stef » Tue Jul 03, 2018 12:21 pm

You have "rescomp" which is an embedded resource compiler for SGDK. You can look into the included "sprite" sample to see how resource are declared (.res file). Basically you can use a big PNG image file (in 4bpp ideally but 8bpp is also supported) then rescomp will convert it to "Image" structure which internally contain a "Map" structure.

You can look at these topics which speak about that :
viewtopic.php?f=19&t=1908
viewtopic.php?f=19&t=2661

nemezes
Interested
Posts: 19
Joined: Sat Mar 31, 2018 1:09 pm

Re: dealing with matrices (arrays)

Post by nemezes » Tue Jul 03, 2018 2:32 pm

Stef wrote:
Tue Jul 03, 2018 12:21 pm
You have "rescomp" which is an embedded resource compiler for SGDK. You can look into the included "sprite" sample to see how resource are declared (.res file). Basically you can use a big PNG image file (in 4bpp ideally but 8bpp is also supported) then rescomp will convert it to "Image" structure which internally contain a "Map" structure.

You can look at these topics which speak about that :
viewtopic.php?f=19&t=1908
viewtopic.php?f=19&t=2661
the first topic give me all the answers I needed, for now it is working.

thanks! :D

nemezes
Interested
Posts: 19
Joined: Sat Mar 31, 2018 1:09 pm

Re: dealing with matrices (arrays)

Post by nemezes » Wed Jul 04, 2018 12:12 pm

for one level, everything is working, big map scrolling, collisions and so on.

the problem I get now is to add more levels using the same core code as I did in BEX.

small small example of what I am trying to do. I write the erros and warnings I get.

Code: Select all

static void set_level1()
{
    fix16 mapa[4][8] =
    {
        {1,2,1,1,2,1,2,1},
        {1,2,1,1,2,1,2,1},
        {1,2,1,1,2,1,2,1},
        {1,2,1,1,2,1,2,1}
    };

    VDP_setPalette(PAL1, level_image1.palette->data);
    VDP_loadTileSet(level_image1.tileset, 10, TRUE);
    Map *map = unpackMap(level_image1.map, NULL);
}

static void set_level2()
{
    fix16 mapa[4][16] =
    {
        {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1},
        {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1},
        {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1},
        {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1}
    };

    VDP_setPalette(PAL1, level_image2.palette->data);
    VDP_loadTileSet(level_image2.tileset, 10, TRUE);
    Map *map = unpackMap(level_image2.map, NULL);
}

///// code code

int main() {

	//code code code
	
	while(TRUE)
	{
		playing=1;
		
		if (level==1)
		{
			set_level1();
		}
		else if (level==2)
		{
			set_level2();
		}
		
		while(playing==1)
		{
		
			//code code code
			
			VDP_setMapEx(PLAN_A, map, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE,10), 0, 0, 0, 0, 4, 8);
			// get a error saying that map is undeclared
			// also a warning saying map is unused
			
			//code code code
			
			if (mapa[xR][yR]==2)
			// dont get any error, but a warning saying mapa is unused 
			{
				// code to playing=0 then set level=2 and restart everything
			}
		
		}
		
	}

}
is it possible to do something like that?

User avatar
Stef
Very interested
Posts: 2757
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: dealing with matrices (arrays)

Post by Stef » Wed Jul 04, 2018 12:27 pm

Outch ! Oh well you definitely need to understand how variable and scope work in C ;)
Here you are declaring some local variables and you try to use them globally, so the first thing to do is to put them global at least.
Here's a modified / fixed version of the code, still that is not the best approach for it but at least it should remove your errors and make things work :)

Code: Select all

#define MAX_COLUMNS = 16;

Map* map = NULL;
u8 mapa[4][MAX_COLUMN];

const u8 mapa_level1[4][8] =
{
    {1,2,1,1,2,1,2,1},
    {1,2,1,1,2,1,2,1},
    {1,2,1,1,2,1,2,1},
    {1,2,1,1,2,1,2,1}
};

const u8 mapa_level2[4][16] =
{
    {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1},
    {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1},
    {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1},
    {1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1}
};

static void set_level1()
{
    memcpy(mapa, mapa_level1, sizeof(mapa_level1));
    VDP_setPalette(PAL1, level_image1.palette->data);
    VDP_loadTileSet(level_image1.tileset, 10, TRUE);
    if (map != NULL) freeMap(map);
    map = unpackMap(level_image1.map, NULL);
}

static void set_level2()
{
    memcpy(mapa, mapa_level2, sizeof(mapa_level2));
    VDP_setPalette(PAL1, level_image2.palette->data);
    VDP_loadTileSet(level_image2.tileset, 10, TRUE);
    if (map != NULL) freeMap(map);
    map = unpackMap(level_image2.map, NULL);
}

///// code code

int main()
{
	//code code code
	
	while(TRUE)
	{
		playing=1;
		
		if (level==1)
		{
			set_level1();
		}
		else if (level==2)
		{
			set_level2();
		}
		
		while(playing==1)
		{
			//code code code
			
			VDP_setMapEx(PLAN_A, map, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE,10), 0, 0, 0, 0, 4, 8);
			
			//code code code
			
			if (mapa[xR][yR]==2)
			{
				// code to playing=0 then set level=2 and restart everything
			}
		}
	}
}

nemezes
Interested
Posts: 19
Joined: Sat Mar 31, 2018 1:09 pm

Re: dealing with matrices (arrays)

Post by nemezes » Wed Jul 04, 2018 2:21 pm

thank you so much, stef.

I will stop on "coding" and start to study the basics of C, it is better than everytime keeping asking. :roll:

I still did not try what you said, but here is what I get with just one level and a map with 128 tiles width:
https://www.youtube.com/watch?v=ySt0omsbZXE

when I use your tips, I will post another video with a game with more levels. :D

next step: I will try to add some sprites over the scenario.

User avatar
Stef
Very interested
Posts: 2757
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: dealing with matrices (arrays)

Post by Stef » Wed Jul 04, 2018 2:50 pm

That looks already quite nice for a beginning :)
Don't hesitate to use the "sprite" sample provided in SGDK, it really helps understanding all basic methods =)

nemezes
Interested
Posts: 19
Joined: Sat Mar 31, 2018 1:09 pm

Re: dealing with matrices (arrays)

Post by nemezes » Wed Jul 04, 2018 3:02 pm

Stef wrote:
Wed Jul 04, 2018 2:50 pm
That looks already quite nice for a beginning :)
Don't hesitate to use the "sprite" sample provided in SGDK, it really helps understanding all basic methods =)
as I said, I have some small experience working with BEX, so I know the limitations of mega drive and a little about the coding logic, also, in my regular job, I work a lot with math. :mrgreen:

I just have to know the relations of one code (BEX) to the other (SGDK).

when you said that the variables arent global, I associate what I used in BEX that I had to use "global variable as integer".

it is good to know that SGDK has negative numbers and fractions (1/2=0.5), in BEX was a pain to deal with the negative numbers and I couldnt use 0.5.

User avatar
Stef
Very interested
Posts: 2757
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: dealing with matrices (arrays)

Post by Stef » Wed Jul 04, 2018 4:34 pm

I meant a beginning with SGDK ;)
SGDK provides already many useful methods and tools, the key is to know how to use them efficiently, it's also why i told you to look over the sprite example as mimicking what you used to do in BEX is far from being optimal when you use SGDK.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest