Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 3d matrix

Credits go to gnolam for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
3d matrix
vixinu
Member #7,343
June 2006
avatar

Just out of curiosity, I'm not working on anyrhin 3d, how would you do a 3d tilemap matrix, ex. a

3 x 3 x 3

?

Andrei Ellman
Member #3,434
April 2003

I'd use a 3 dimensional array.

AE.

--
Don't let the illegitimates turn you into carbon.

vixinu
Member #7,343
June 2006
avatar

Oops, I worded that wrong. I kinda meant how you programmed a 3d array.

By the way, where did you get that animation?

LennyLen
Member #5,313
December 2004
avatar

Quote:

I kinda meant how you programmed a 3d array.

Here's an example:

1#define WIDTH 20
2#define HEIGHT 25
3#define DEPTH 10
4 
5int main() {
6 
7 int x, y, z;
8 int map[WIDTH][HEIGHT][DEPTH];
9 
10 for( z = 0; z < DEPTH; z++ ) {
11 
12 for( y = 0; y < HEIGHT; y++ ) {
13 
14 for( x = 0; x < WIDTH; x++ ) {
15 
16 map[x][y][z] = 0;
17 
18 }
19
20 }
21 
22 }
23 
24 return 0;
25 
26}

Avenger
Member #4,550
April 2004

Quote:

int map[WIDTH][HEIGHT][DEPTH];

I believe this is more accurate:
int map[DEPTH][HEIGHT][WIDTH];

With arrays you have to use inverse of what you want (for optimal memory access that is).

vixinu
Member #7,343
June 2006
avatar

So how do you do a tilemap w/ a 3d array?

gnolam
Member #2,030
March 2002
avatar

By letting the array elements denote tiles. ::)

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

vixinu
Member #7,343
June 2006
avatar

But with the numbers denoting tiles.

gnolam
Member #2,030
March 2002
avatar

IHBT, IHL, HAND?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

vixinu
Member #7,343
June 2006
avatar

which are?.?.?

LennyLen
Member #5,313
December 2004
avatar

I think gnolam is wondering if you have the faintest idea of what you are talking about.

vixinu
Member #7,343
June 2006
avatar

I haven't done anything 3D before. Besides, you're not gnolam. Gnolam can speak for himself.

gnolam
Member #2,030
March 2002
avatar

I'm simply wondering if you're trolling or if you really are as thick as you appear to be...

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

LennyLen
Member #5,313
December 2004
avatar

Quote:

Gnolam can speak for himself

Yes, but I get the feeling that it would have to be censored.

Quote:

I haven't done anything 3D before.

Well for starters, you don't use tilemaps in 3D graphics. A 3d tilemap array is something different. It allows you to add layers to 2d tilemaps so that you can do things like 'fake 3d' games, and animated tiles.

vixinu
Member #7,343
June 2006
avatar

O

Jonny Cook
Member #4,055
November 2003

So you have a 3D array, right? It can be an array of ints. You loop through it, just as described by LennyLen. Each int can hold say, 1 or 0. 1 is for a tile, 0 is for no tile. Assuming you have a bitmap/model representation for the tile, you could do something like this.

    for( z = 0; z < DEPTH; z++ ) {

        for( y = 0; y < HEIGHT; y++ ) {

            for( x = 0; x < WIDTH; x++ ) {
                if (map[x][y][z]) {
                    DrawTile(x, y, z);
                }
            }
 
        }

    }

That's a very simple tile map. I'll leave it up to you to decide how DrawTile is implemented.

[edit]
It's a good idea to figure out what something is before you ask for help on how to implement it.

The face of a child can say it all, especially the mouth part of the face.

Andrei Ellman
Member #3,434
April 2003

LennyLen said:

Well for starters, you don't use tilemaps in 3D graphics. A 3d tilemap array is something different. It allows you to add layers to 2d tilemaps so that you can do things like 'fake 3d' games, and animated tiles.

You could still design a 3D level by using a 3D array to represent which blocks appear at certain discrete locations in 3D space (gap, brick wall, metal wall, etc.). You'd be using blocks instead of tiles, but you'd be using a 3D array like you would do for a 3D tilemap.

3D tilemaps are probably best sed for 2D games where the tiles consist of several layers (eg. what lies under the floor, the floor, the walls, and a transparent ceiling).

vixinu said:

By the way, where did you get that animation?

See this thread

AE.

--
Don't let the illegitimates turn you into carbon.

vixinu
Member #7,343
June 2006
avatar

This has nothing to do with anything in the topic thus far, but I get this error message when I run my code in g++:

error message:

maptest.cpp: In function 'int _mangled_main(int, char**)':
maptest.cpp:27: error: expected initializer before 'bmp'
maptest.cpp:50: error: 'x' was not declared in this scope
maptest.cpp:62: error: 'tilesize' was not declared in this scope
/usr/local/include/allegro/datafile.h:81: error: too few arguments to function 'BITMAP* load_bitmap(const char*, RGB*)'
maptest.cpp:67: error: at this point in file

code:

1#include <allegro.h>
2 
3int main(int argc, char *argv[])
4{
5allegro_init();
6install_keyboard();
7set_color_depth(16);
8set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
9 
10BITMAP *buffer = NULL;
11buffer = create_bitmap(640, 480);
12blit(buffer, screen, 0, 0, 0, 0, 640, 480);
13 
14//load bitmap
15 
16BITMAP *bmp
17 
18bmp = load_bitmap("Woodland_Tileset.png");
19 
20//load tilemap
21 
22int tilemap[5][5] = {
23 { 96, 96, 96, 96, 96 },
24 { 96, 96, 0, 0, 96 },
25 { 96, 0, 0, 0, 96 },
26 { 96, 0, 0, 96, 96 },
27 { 96, 96, 96, 96, 96 }
28};
29 
30int tile_number = 96; //Set to the tile number wanted
31BITMAP* tileset; //Set to tileset to be loaded
32int tile_size = 32; //Set this to how many pixels big each tile is
33int set_x = tile_number % (tileset->w / tile_size);
34int set_y = tile_number / (tileset->w / tile_size);
35 
36//Put tileset on screen
37 
38for (int y = 0; y < 5; y++)
39{
40 //Find out what tileset index to use based on the tilemap at the current (x, y) coordinates
41 int set_x = tilemap[y][x] % (tileset->w / tile_size);
42 int set_y = tilemap[y][x] / (tileset->w / tile_size);
43 //Put it on the buffer
44 // blit(tileset, buffer,
45 //Blit from the tileset to the screen
46 // set_x * tile_size, set_y * tile_size,
47 //starting at the pixel coordinates of the desired tile
48 // x * tile_size, y * tile_size,
49 //put on the screen at the proper place
50 // tile_size, tile_size);
51 //and put only 1 tile's worth of pixels
52 //To put this all together:
53 blit(tileset, buffer, set_x * tile_size, set_y * tilesize, x * tilesize, y * tilesize, tile_size, tile_size);
54}
55 
56BITMAP *sprite;
57 
58sprite = load_bitmap("Sprite.png");
59 
60draw_sprite(buffer, sprite, 0, 0);
61 
62int sprite_x = 0;
63int sprite_y = 0;
64 
65while(!key[KEY_ESC])
66{
67 if(key[KEY_RIGHT])
68 {
69 sprite_x + 32;
70 }
71 else if(key[KEY_LEFT])
72 {
73 sprite_x - 32;
74 }
75 else if(key[KEY_UP])
76 {
77 sprite_y - 32;
78 }
79 else if(key[KEY_DOWN])
80 {
81 sprite_y + 32;
82 }
83}
84 
85destroy_bitmap(sprite);
86destroy_bitmap(buffer);
87return 0;
88}
89END_OF_MAIN()

gnolam
Member #2,030
March 2002
avatar

I stand by my previous comment(s).

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

vixinu
Member #7,343
June 2006
avatar

Which ones?

Neil Walker
Member #210
April 2000
avatar

He's wondering if you're trolling.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

vixinu
Member #7,343
June 2006
avatar

No.

[edit]I found a few reasons for a few of the bugs, and cut it down to specific code. Now the error is:

maptest.cpp:26: error: at this point in file

the code is: (starting line 10)

1#include <allegro.h>
2 
3int main()
4{
5allegro_init();
6set_color_depth(16);
7set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
8 
9BITMAP *buffer = NULL;
10buffer = create_bitmap(640, 480);
11blit(buffer, screen, 0, 0, 0, 0, 640, 480);
12 
13//load bitmap
14 
15BITMAP *bmp;
16 
17bmp = load_bitmap("Woodland_Tileset.png");
18 
19//load tilemap
20 
21int tilemap[5][5] = {
22 { 96, 96, 96, 96, 96 },
23 { 96, 96, 0, 0, 96 },
24 { 96, 0, 0, 0, 96 },
25 { 96, 0, 0, 96, 96 },
26 { 96, 96, 96, 96, 96 }
27};
28 
29int tile_number = 96; //Set to the tile number wanted
30BITMAP* tileset; //Set to tileset to be loaded
31int tile_size = 32; //Set this to how many pixels big each tile is
32int set_x = tile_number % (tileset->w / tile_size);
33int set_y = tile_number / (tileset->w / tile_size);
34 
35//Put tileset on screen
36 
37for (int y = 0; y < 5; y++)
38{
39 for (int x = 0; x < 5; x++)
40 {
41 int tilesize = 32;
42 //Find out what tileset index to use based on the tilemap at the current (x, y) coordinates
43 int set_x = tilemap[y][x] % (tileset->w / tile_size);
44 int set_y = tilemap[y][x] / (tileset->w / tile_size);
45 //Put it on the buffer
46 // blit(tileset, buffer,
47 //Blit from the tileset to the screen
48 // set_x * tile_size, set_y * tile_size,
49 //starting at the pixel coordinates of the desired tile
50 // x * tile_size, y * tile_size,
51 //put on the screen at the proper place
52 // tile_size, tile_size);
53 //and put only 1 tile's worth of pixels
54 //To put this all together:
55 blit(tileset, buffer, set_x * tile_size, set_y * tilesize, x * tilesize, y * tilesize, tile_size, tile_size);
56}}
57 
58destroy_bitmap(buffer);
59return 0;
60}
61END_OF_MAIN()

LennyLen
Member #5,313
December 2004
avatar

Try using load_bitmap() correctly.

deps
Member #3,858
September 2003
avatar

Quote:

Try using load_bitmap() correctly.

And check the manual if you have any questions on how to use it.
You can click on it in the code you posted for a quick link.

---
Layers are good, all good things have layers, like onions. Good things also stink. - Trezker
I now have a page and a blog!

vixinu
Member #7,343
June 2006
avatar

I've read the manual, here is my new error and code:

maptest.cpp:36: error: expected primary-expression before 'palette'

1#include <allegro.h>
2 
3int main()
4{
5allegro_init();
6set_color_depth(16);
7set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
8 
9BITMAP *buffer = NULL;
10buffer = create_bitmap(640, 480);
11blit(buffer, screen, 0, 0, 0, 0, 640, 480);
12 
13//load bitmap
14 
15typedef struct RGB
16{
17 unsigned char r, g, b;
18} RGB;
19typedef struct PALETTE
20{
21 RGB PALETTE[PAL_SIZE];
22} PALETTE;
23 
24BITMAP *bmp;
25PALETTE *palette;
26set_color_conversion(COLORCONV_REDUCE_TO_256);
27bmp = load_bitmap("Woodland_Tileset.bmp", RGB palette);
28 
29//load tilemap
30 
31int tilemap[5][5] = {
32 { 96, 96, 96, 96, 96 },
33 { 96, 96, 0, 0, 96 },
34 { 96, 0, 0, 0, 96 },
35 { 96, 0, 0, 96, 96 },
36 { 96, 96, 96, 96, 96 }
37};
38 
39int tile_number = 96; //Set to the tile number wanted
40BITMAP* tileset; //Set to tileset to be loaded
41int tile_size = 32; //Set this to how many pixels big each tile is
42int set_x = tile_number % (tileset->w / tile_size);
43int set_y = tile_number / (tileset->w / tile_size);
44 
45//Put tileset on screen
46 
47for (int y = 0; y < 5; y++)
48{
49 for (int x = 0; x < 5; x++)
50 {
51 int tilesize = 32;
52 //Find out what tileset index to use based on the tilemap at the current (x, y) coordinates
53 int set_x = tilemap[y][x] % (tileset->w / tile_size);
54 int set_y = tilemap[y][x] / (tileset->w / tile_size);
55 //Put it on the buffer
56 // blit(tileset, buffer,
57 //Blit from the tileset to the screen
58 // set_x * tile_size, set_y * tile_size,
59 //starting at the pixel coordinates of the desired tile
60 // x * tile_size, y * tile_size,
61 //put on the screen at the proper place
62 // tile_size, tile_size);
63 //and put only 1 tile's worth of pixels
64 //To put this all together:
65 blit(tileset, buffer, set_x * tile_size, set_y * tilesize, x * tilesize, y * tilesize, tile_size, tile_size);
66}}
67 
68destroy_bitmap(buffer);
69return 0;
70}
71END_OF_MAIN()

 1   2 


Go to: