Allegro.cc Forums » Game Design & Concepts » Isometric tile engine- sprites

 GullRaDriel Member #3,861 September 2003 Ariesln, all I can do is posting the code I use for ScreenToMap. I use a staggered map in my iso engine too.#SelectExpand 1/*!\fn ScreenToMap( int mx, int my, int *Tilex,int *Tiley,BITMAP *mousemap) 2 * 3 *\brief Convert screen coordinate to map coordinate 4 * 5 *\param mx The 'x' coordinate in pixel 6 *\param my The 'y' coordinate in pixel 7 *\param Tilex Output for the generated Tilex 8 *\param Tiley Output for the generated Tiley 9 *\param mousemap A mousemap with colors for one tile 10 * 11 *\return TRUE 12 */ 13 14int ScreenToMap( int mx, int my, int *Tilex, int *Tiley, BITMAP *mousemap ) 15 { 16 17 /* SCREEN TO MAP VARIABLES */ 18 int RegionX, RegionY, 19 RegionDX = 0, RegionDY = 0, 20 MouseMapX, MouseMapY, 21 MouseMapWidth, MouseMapHeight, c; 22 23 MouseMapWidth = mousemap -> w; 24 MouseMapHeight = mousemap -> h; 25 26 /* First step find out where we are on the mousemap */ 27 RegionX = ( mx / MouseMapWidth ); 28 RegionY = ( my / MouseMapHeight ) << 1; /* The multiplying by two is very important */ 29 30 31 /* Second Step: Find out WHERE in the mousemap our mouse is, by finding MouseMapX and MouseMapY. */ 32 33 MouseMapX = mx % MouseMapWidth; 34 MouseMapY = my % MouseMapHeight; 35 36 /* third step: Find the color in the mouse map */ 37 c = getpixel( mousemap, MouseMapX, MouseMapY ); 38 39 if ( c == makecol( 255, 0, 0 ) ) { 40 RegionDX = -1; 41 RegionDY = -1; 42 } 43 44 if ( c == makecol( 255, 255, 0 ) ) { 45 RegionDX = 0; 46 RegionDY = -1; 47 } 48 49 if ( c == makecol( 255, 255, 255 ) ) { 50 RegionDX = 0; 51 RegionDY = 0; 52 } 53 54 if ( c == makecol( 0, 255, 0 ) ) { 55 RegionDX = -1; 56 RegionDY = 1; 57 } 58 59 if ( c == makecol( 0, 0, 255 ) ) { 60 RegionDX = 0; 61 RegionDY = 1; 62 } 63 64 *Tilex = ( RegionDX + RegionX ); 65 *Tiley = ( RegionDY + RegionY ); 66 67 68 return TRUE; 69 70 } /* ScreenToMap( ... ) */ 71 72 73 74/*!\fn camera_to_scr( MAP **map , int x , int y ) 75 * 76 *\brief Center Map on given screen coordinate 77 * 78 *\param map A MAP *map to center on x,y 79 *\param x the X coordinate (in pixel) to center on; 80 *\param y the Y coordintae (in pixel) to center on; 81 * 82 *\return TRUE 83 */ 84 85int camera_to_scr( MAP **map , int x , int y ) 86 { 87 if ( x < 0 ) 88 x = 0; 89 90 if ( y < 0 ) 91 y = 0; 92 93 ( *map ) -> X = x % ( *map ) ->TILEW; 94 95 ( *map ) -> Y = y % ( *map ) ->TILEH; 96 97 ( *map ) -> ptanchorY = y / ( ( *map ) ->TILEH >> 1 ); 98 99 ( *map ) -> ptanchorY = ( *map ) -> ptanchorY >> 1; 100 101 ( *map ) -> ptanchorY = ( *map ) -> ptanchorY << 1; 102 103 ( *map ) -> ptanchorX = ( x + ( ( *map ) -> ptanchorY & 1 ) * ( ( *map ) ->TILEW >> 1 ) ) / ( *map ) ->TILEW; 104 105 return TRUE; 106 107 } /* camera_to_scr( ... )*/ The mousemap BITMAP is drawn like this: ```/* drawing mousemap */ line( ( *map ) -> mousemap , 0 , ( TILEH >> 1 ) - 2 , ( TILEW >> 1 ) - 3 , 0 , makecol( 255, 0, 0 ) ); floodfill( ( *map ) -> mousemap , 1 , 1 , makecol( 255, 0, 0 ) ); line( ( *map ) -> mousemap , ( TILEW >> 1 ) - 3 , TILEH - 1 , 0 , ( TILEH >> 1 ) + 1 , makecol( 0 , 255 , 0 ) ); floodfill( ( *map ) -> mousemap , 1 , TILEH - 2 , makecol( 0, 255, 0 ) ); line( ( *map ) -> mousemap , ( TILEW >> 1 ) + 2 , TILEH - 1 , TILEW - 1 , ( TILEH >> 1 ) + 1 , makecol( 0, 0, 255 ) ); floodfill( ( *map ) -> mousemap , TILEW - 2 , TILEH - 1 , makecol( 0, 0, 255 ) ); line( ( *map ) -> mousemap , ( TILEW >> 1 ) + 2 , 0 , TILEW - 1 , ( TILEH >> 1 ) - 2 , makecol( 255, 255 , 0 ) ); floodfill( ( *map ) -> mousemap , TILEW - 1 , 0 , makecol( 255, 255, 0 ) ); ``` "Code is like shit - it only smells if it is not yours"Allegro Wiki, full of examples and articles !!