CodeWalrus

Development => Calculators => Calc Projects, Programming & Tutorials => Topic started by: Unicorn on August 05, 2016, 05:33:34 AM

Title: Unicorn's C Questions and Stuff [C] [Help]
Post by: Unicorn on August 05, 2016, 05:33:34 AM
I haven't a C topic here, so I made one! I'll just post all those things that I've experimented on here but aren't made into games, or my ideas, or my questions about things that don't yet have topics.

To start it off, a question!

How might I go about using the Tilemap functions in graphx.h? @MateoConLechuga could you help?

I realize there is this command:

void gfx_Tilemap(gfx_tilemap_t *tilemap, uint24_t x_offset, uint24_t y_offset);

But what about this info? How would I set this data up?

/* Type for tilemap */
typedef struct gfx_tilemap {
uint8_t *map;             /* pointer to indexed map array */
gfx_image_t **tiles;          /* pointer to tiles */
uint8_t tile_height;      /* individual tile height */
uint8_t tile_width;       /* individual tile width */
uint8_t draw_height;      /* number of rows to draw in the tilemap */
uint8_t draw_width;       /* number of cols to draw tilemap */
uint8_t type_width;       /* 2^type_width = tile_width */
uint8_t type_height;      /* 2^type_height = tile_height */
uint8_t height;           /* total number of rows in the tilemap */
uint8_t width;            /* total number of cols in the tilemap */
uint8_t y_loc;            /* y pixel location to begin drawing at */
uint24_t x_loc;           /* x pixel location to begin drawing at */
} gfx_tilemap_t;

typedef enum gfx_tilemap_type {
gfx_tile_2_pixel = 1,      /* Set when using 2 pixel tiles */
gfx_tile_4_pixel,          /* Set when using 4 pixel tiles */
gfx_tile_8_pixel,          /* Set when using 8 pixel tiles */
gfx_tile_16_pixel,         /* Set when using 16 pixel tiles */
gfx_tile_32_pixel,         /* Set when using 32 pixel tiles */
gfx_tile_64_pixel,         /* Set when using 64 pixel tiles */
gfx_tile_128_pixel,        /* Set when using 128 pixel tiles */
} gfx_tilemap_type_t;


And the final question: How do I load a map using it? Do I need different sprites that are how ever large my tiles are and then I call some sort of command that sets them to certain places on the tilemap for it to draw? c4ooo explained that I draw it myself and just tell the tilemap how to behave, with tile size and whatnot, which I still need to know how to do.
And I'm assuming I really should learn how to use buffers to erase things.

(I'm thinking about making some type of platformer)
Title: Re: Unicorn's C Questions and Stuff
Post by: Yuki on August 05, 2016, 06:31:12 AM
I can't help you that much, but you'd need to make a variable of type gfx_tilemap_t and basically fill it out, like tilemap->tile_height = 30 and whatnot. Same for gfx_image_t. The easiest is to read a file directly into that variable, I don't remember what's the function for it but there's probably some function for that.
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 05, 2016, 08:05:02 PM
So fill the variable gfx_tilemap_t like I would in matrix?


gfx_tilemap_t tilemap[11]

tilemap[0] = map;             /* pointer to indexed map array */
tilemap[1] = tiles;          /* pointer to tiles */

and so on.
Title: Re: Unicorn's C Questions and Stuff
Post by: Adriweb on August 05, 2016, 09:23:11 PM
You do realize that there are example files dealing with such functions ?
It should help quite a bit: https://github.com/CE-Programming/libraries/blob/master/graphics/graphics_examples/demo_5/src/main.c for instance
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 06, 2016, 12:23:39 AM
Quote from: Adriweb on August 05, 2016, 09:23:11 PM
You do realize that there are example files dealing with such functions ?
It should help quite a bit: https://github.com/CE-Programming/libraries/blob/master/graphics/graphics_examples/demo_5/src/main.c for instance
Ah, I hadn't realized/forgot! Thanks for reminding me :)

So about the above question: Anywasys, I looked into the example, and the only thing I don't really understand is how the tilemapdata.c is generated. It is located in the main src directory and these are teh contents:


#include <stdint.h>

/* This is the data that is displayed with the tilemap */
uint8_t tilemap_map[] = {
0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x22,0x14,0x15,0x04,0x05,0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x22,
0x30,0x54,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x53,0x32,0x04,0x05,0x14,0x15,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x53,0x32,
0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x01,0x10,0x10,0x10,0x00,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x50,0x51,0x51,0x52,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x00,0x10,0x10,0x10,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x60,0x61,0x61,0x62,0x00,0x00,0x00,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x01,0x10,0x10,0x10,0x00,0x00,0x01,0x00,0x01,0x00,0x40,0x42,0x04,0x05,0x14,0x15,0x00,0x60,0x61,0x61,0x62,0x00,0x00,0x00,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x70,0x71,0x71,0x72,0x00,0x00,0x00,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x01,0x00,0x01,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x00,0x01,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x01,0x00,0x00,0x00,0x01,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x10,0x10,0x10,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x43,0x32,
0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x53,0x32,
0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x20,0x22,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x00,0x00,0x00,0x01,0x00,0x01,0x10,0x10,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x01,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x01,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x00,0x10,0x10,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x30,0x32,0x14,0x15,0x04,0x05,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x01,0x10,0x10,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x30,0x32,0x04,0x05,0x14,0x15,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x30,0x32,
0x30,0x32,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x30,0x32,0x14,0x15,0x24,0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x32,
0x30,0x44,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x43,0x32,0x04,0x05,0x20,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x43,0x32,
0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x42,0x14,0x15,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x42,
};


I hope I am correct in believing that this tells the commands where to put each tile.

Thanks in advance!
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 07, 2016, 04:49:12 AM
So @MateoConLechuga cleared up what that is for me, but I still don't understand how I would generate that, if it is what I would use to put each tile it its place.

Also, how would I decompress my sprites? I was looking at an example and it doesn't seem to make much sense.
Title: Re: Unicorn's C Questions and Stuff
Post by: kotu on August 16, 2016, 04:23:41 AM
Quote from: Unicorn on August 07, 2016, 04:49:12 AM
So @MateoConLechuga cleared up what that is for me, but I still don't understand how I would generate that, if it is what I would use to put each tile it its place.
Well, normally you would use a map editor program to 'paint' your tile levels. I might be making one soon.. will let you know

Quote from: Unicorn on August 07, 2016, 04:49:12 AM
Also, how would I decompress my sprites? I was looking at an example and it doesn't seem to make much sense.
I think you want to look at the graphx1 and graphx2 examples from Mateo's sdk
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 16, 2016, 06:53:22 AM
so about the compression, apparently it doesnt make much sense, and mateo will be making it better.
Title: Re: Unicorn's C Questions and Stuff [C] [Help]
Post by: kotu on August 16, 2016, 06:56:34 AM
well, the examples show 1 example where the background tiles are compressed

maybe the same technique could be used to compress the sprites?
:)
i've not had call to look into that yet.
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 16, 2016, 07:03:38 AM
There is an example for sprites and tilemaps, but like I've said, they dont make much sense and will be reworked, according to mateo.

About the tilemapper, it seems like you make a spritesheet and then generate a map using an array somehow. I need to figure out how to do THAT.
Title: Re: Unicorn's C Questions and Stuff [C] [Help]
Post by: kotu on August 16, 2016, 07:08:38 AM
Well, to create custom maps, you can either code an array BY HAND, which is pretty .... well, it doesn't make it too easy... OR you can use a tilemap editor program, which is easier and more creative. Something like this...
(http://www.mapeditor.org/img/screenshot-terrain.png)

There are some listed here that should do what you want...
http://gamedev.stackexchange.com/questions/225/tools-for-creating-2d-tile-based-maps (http://gamedev.stackexchange.com/questions/225/tools-for-creating-2d-tile-based-maps)
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 16, 2016, 07:16:31 AM
So the problem with that is knowing whetjer it will output the same type of array needed. I would have to look more closly at it, but that seems like it would be a problem.
Title: Re: Unicorn's C Questions and Stuff [C] [Help]
Post by: kotu on August 16, 2016, 07:23:33 AM
Well, if you check the link I posted, there are over 10 editors listed on that page, they probably are quite customisable in their output.

But like I said I might be making one myself soon. If I do it will probably be exactly the format you require.
Title: Re: Unicorn's C Questions and Stuff [C] [Help]
Post by: kotu on August 16, 2016, 10:19:11 AM
Also, things get a little more complex than Mateo's tilemap example.  :P

http://b52i.imgup.net/tilesetd87b.png (http://b52i.imgup.net/tilesetd87b.png)

If you look at the above image, you will see some of the tiles have yellow background. These have to be drawn over a 'ground' tile (one without any transparency). This means if you want to have transparent tiles like this in your game, you basically need twice the amount of data in your tilemap, one for base tiles, and one for overlay tiles.

Also, for each tile, you need a way of knowing if your character can pass through that tile or not.

If I make a custom tool to help it will allow for use of transparent tiles, or not, to make things simpler if you dont want them. Also it will save the 'block-passable' data too if that is required. [*EDIT* just realised that doesn't have to be saved out]  I should let the user choose 32x32 or 16x16 tiles.

Any input on what a tool should be like and what features it should have would be recommended!!

Thanks
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 17, 2016, 05:20:26 PM
I suggest you make a topic about your tool, to avoid hijacking mine ;)

But anyways, the yellowish orange is the transparent color, which you define in the tilemap command, I believe, and I do know what transparent means, having used sprites for a while. ;)

You will definitely want to implement transparency, as well as the X by Y of the tiles, and map.
Title: Re: Unicorn's C Questions and Stuff [C] [Help]
Post by: MateoConLechuga on August 17, 2016, 09:37:11 PM
Yes, tilemaps are implemented very nicely and are insanely customizable using the C libraries. This makes them somewhat difficult to understand without too much documentation, but basically you can draw foreground tiles over a predrawn background tilemap using the two different tilemap drawing functions. It also supports arbitrary powers of 2 for the width and height of tiles, in addition to drawing locations. Enjoy, and it is all about the exploration of what is available. I can still hardly believe how much there is. :)
Title: Re: Unicorn's C Questions and Stuff
Post by: Unicorn on August 18, 2016, 05:05:14 AM
So, @MateoConLechuga,  if I am going to create a tilemap using an editor, what format should I save it in? Base64, CSV, XML? Those are just formats that I saw Tiled had as options.