polkom21 Temat założony przez niniejszego użytkownika |
[Allegro 4] Kolizje. » 2012-12-18 18:12:40 Próbowałem już kilkukrotnie napisać kolizje dla gracza i mapy ale nie wychodzi mi to i nie mam pojęcia czemu. W chwili obecnej mam coś takiego: #include <allegro.h> #include "global.h" #include "graphic.h"
#define gravitation 1
volatile long speed = 0; void increment_speed() { speed++; } END_OF_FUNCTION( increment_speed ); LOCK_VARIABLE( speed ); LOCK_FUNCTION( increment_speed );
int main() { allegro_init(); set_color_depth( 32 ); set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0 ); install_keyboard(); install_timer(); install_int_ex( increment_speed, BPS_TO_TIMER( 200 ) ); BITMAP * buffor = NULL; buffor = create_bitmap( 800, 600 ); DATAFILE * date = NULL; date = load_datafile( "graphic.dat" ); if( !date ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "Blad ladowania grafik." ); allegro_exit(); return 0; } BITMAP * person = NULL; person = create_bitmap( 40, 40 ); clear_to_color( person, makecol( 0, 0, 0 ) ); int person_x = 100, person_y = 100; int rozmiar_x = 0, rozmiar_y = 0; const char * filemap = "map.txt"; int map[ 100 ][ 100 ]; LoadMap( filemap, map, rozmiar_x, rozmiar_y ); cout << "map X: " << rozmiar_x << " map Y: " << rozmiar_y << endl; while( !key[ KEY_ESC ] ) { while( speed > 0 ) { int posX = person_x / 40, posY = person_y / 40; if( map[ posX ][ posY++ ] == 0 ) { person_y = person_y + gravitation; } if( key[ KEY_LEFT ] ) person_x--; if( key[ KEY_RIGHT ] ) person_x++; speed--; } show_position( person_x, person_y ); clear_to_color( buffor, makecol( 255, 255, 255 ) ); DrawMap( map, rozmiar_x, rozmiar_y, buffor, date ); masked_blit( person, buffor, 0, 0, person_x, person_y - 40, person->w, person->h ); blit( buffor, screen, 0, 0, 0, 0, 800, 600 ); } remove_int( increment_speed ); unload_datafile( date ); destroy_bitmap( person ); destroy_bitmap( buffor ); allegro_exit(); return 0; } END_OF_MAIN();
global.h #ifndef GLOBAL_H_INCLUDED #define GLOBAL_H_INCLUDED
#include "console.h" #include <fstream>
using namespace std;
int loadCounterX = 0, loadCounterY = 0, mapSizeX, mapSizeY;
void LoadMap( const char * filename, int map[ 100 ][ 100 ], int & rozmiar_x, int & rozmiar_y ) { fstream plik; plik.open( filename, ios::in ); plik >> rozmiar_x >> rozmiar_y; for( int y = 0; y < rozmiar_y; y++ ) { for( int x = 0; x < rozmiar_x; x++ ) { plik >> map[ x ][ y ]; } } plik.close(); }
void DrawMap( int map[ 100 ][ 100 ], int sizeMapX, int sizeMapY, BITMAP * buffor, DATAFILE * date ) { for( int x = 0; x < sizeMapX; x++ ) { for( int y = 0; y < sizeMapY; y++ ) { int id = map[ x ][ y ], positionX = x * 40, positionY = y * 40; masked_blit(( BITMAP * ) date[ id ].dat, buffor, 0, 0, positionX, positionY, 40, 40 ); } } }
#endif
Zrobiłem już grawitację i kolizje żeby postać nie spadała całkowicie poza mapę ale mam problem z kolizją jeżeli chodzi o prawą i lewą stronę oraz górę. |