Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Allegro]Scrollowanie mapki

Ostatnio zmodyfikowano 2009-02-22 18:03
Autor Wiadomość
PhonniX
Temat założony przez niniejszego użytkownika
» 2009-02-22 16:52:38
Nie wiem, zrobiłem mniej wiecej to co napisałeś, ale nadal to samo co wcześniej :/

C/C++
#include <allegro.h>

class Cpostac
{
public:
    int x, y;
    short int kierunek, klatka;
};
// Zmienna naszej klasy
Cpostac ludek;
// Inicjowanie timera
volatile long speed = 0;
void increment_speed()
{
    speed++;
}
END_OF_FUNCTION( increment_speed );
LOCK_VARIABLE( speed );
LOCK_FUNCTION( increment_speed );

// Zmienne potrzebne do mapy
int mapa_x = 0, mapa_y = 0;
int scrollX = 0;
int scrollY = 0;
BITMAP * teren = NULL;
BITMAP * bufor = NULL;

// DEFINICJA MAPY
short int map[ 40 ][ 20 ] =
{
    15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 4, 9, 10, 10, 10, 8, 3, 5, 15, 0, 2, 2, 2, 1, 15,
    2, 1, 15, 15, 15, 15, 7, 10, 10, 10, 6, 15, 15, 15, 7, 10, 10, 10, 6, 15,
    10, 6, 15, 15, 15, 15, 4, 9, 10, 10, 6, 15, 15, 15, 4, 3, 3, 3, 5, 15,
    10, 12, 2, 1, 15, 15, 15, 7, 10, 8, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    10, 10, 10, 6, 15, 15, 15, 4, 3, 5, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2,
    10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2, 13, 10, 10,
    3, 3, 3, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 7, 10, 10, 10, 8, 3,
    15, 15, 15, 15, 15, 0, 1, 15, 15, 15, 15, 15, 0, 2, 13, 10, 10, 10, 6, 15,
    15, 15, 15, 15, 15, 4, 5, 15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 6, 15,
    15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 4, 9, 10, 10, 10, 8, 3, 5, 15, 0, 2, 2, 2, 1, 15,
    2, 1, 15, 15, 15, 15, 7, 10, 10, 10, 6, 15, 15, 15, 7, 10, 10, 10, 6, 15,
    10, 6, 15, 15, 15, 15, 4, 9, 10, 10, 6, 15, 15, 15, 4, 3, 3, 3, 5, 15,
    10, 12, 2, 1, 15, 15, 15, 7, 10, 8, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    10, 10, 10, 6, 15, 15, 15, 4, 3, 5, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2,
    10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2, 13, 10, 10,
    3, 3, 3, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 7, 10, 10, 10, 8, 3,
    15, 15, 15, 15, 15, 0, 1, 15, 15, 15, 15, 15, 0, 2, 13, 10, 10, 10, 6, 15,
    15, 15, 15, 15, 15, 4, 5, 15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 6, 15,
    15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 4, 9, 10, 10, 10, 8, 3, 5, 15, 0, 2, 2, 2, 1, 15,
    2, 1, 15, 15, 15, 15, 7, 10, 10, 10, 6, 15, 15, 15, 7, 10, 10, 10, 6, 15,
    10, 6, 15, 15, 15, 15, 4, 9, 10, 10, 6, 15, 15, 15, 4, 3, 3, 3, 5, 15,
    10, 12, 2, 1, 15, 15, 15, 7, 10, 8, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    10, 10, 10, 6, 15, 15, 15, 4, 3, 5, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2,
    10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2, 13, 10, 10,
    3, 3, 3, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 7, 10, 10, 10, 8, 3,
    15, 15, 15, 15, 15, 0, 1, 15, 15, 15, 15, 15, 0, 2, 13, 10, 10, 10, 6, 15,
    15, 15, 15, 15, 15, 4, 5, 15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 6, 15,
    15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 4, 9, 10, 10, 10, 8, 3, 5, 15, 0, 2, 2, 2, 1, 15,
    2, 1, 15, 15, 15, 15, 7, 10, 10, 10, 6, 15, 15, 15, 7, 10, 10, 10, 6, 15,
    10, 6, 15, 15, 15, 15, 4, 9, 10, 10, 6, 15, 15, 15, 4, 3, 3, 3, 5, 15,
    10, 12, 2, 1, 15, 15, 15, 7, 10, 8, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    10, 10, 10, 6, 15, 15, 15, 4, 3, 5, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2,
    10, 10, 10, 6, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 2, 2, 13, 10, 10,
    3, 3, 3, 5, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 7, 10, 10, 10, 8, 3,
    15, 15, 15, 15, 15, 0, 1, 15, 15, 15, 15, 15, 0, 2, 13, 10, 10, 10, 6, 15,
    15, 15, 15, 15, 15, 4, 5, 15, 15, 15, 15, 15, 7, 10, 10, 10, 10, 10, 6, 15
};

// Funkcja wyświetlająca mapę:
void wys_mape()
{
    int startMapX, startMapY;
    for( startMapX = 0; startMapX < 16; startMapX++ )
    {
        for( startMapY = 0; startMapY < 12; startMapY++ )
        {
            blit( teren, bufor,
            ( map[ startMapY + mapa_y ][ startMapX + mapa_x ] % 4 ) * 40,
            ( map[ startMapY + mapa_y ][ startMapX + mapa_x ] / 4 ) * 40,
            startMapX * 40, startMapY * 40, 40, 40 );
        }
    }
};

// Funkcja Główna
int main()
{
    allegro_init();
    install_keyboard();
    set_color_depth( 16 );
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0 );
    install_timer();
    install_int_ex( increment_speed, BPS_TO_TIMER( 80 ) );
   
    BITMAP * ludekb = NULL;
   
    // Tworzenie bufora
    bufor = create_bitmap( 640, 480 );
    if( !bufor )
    {
        set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 );
        allegro_message( "Nie mogę utworzyć bufora !" );
        allegro_exit();
        return 0;
    }
    // Ładowanie grafiki
    ludekb = load_bmp( "postac.bmp", default_palette );
    teren = load_bmp( "teren.bmp", default_palette );
   
    // Ustalanie początkowej pozycji ludzika
    int frame = 0;
    ludek.x = 300;
    ludek.y = 200;
    ludek.klatka = 0;
    ludek.kierunek = 0;
   
    while( !key[ KEY_ESC ] )
    {
        while( speed > 0 )
        {
            if( key[ KEY_LEFT ] ) { scrollX--; mapa_x--; ludek.kierunek = 1; ludek.x -= 2; frame++; }
            if( key[ KEY_RIGHT ] ) { scrollX++; mapa_x++; ludek.kierunek = 2; ludek.x += 2; frame++; }
            if( key[ KEY_UP ] ) { scrollY--; mapa_y--; ludek.kierunek = 3; ludek.y -= 2; frame++; }
            if( key[ KEY_DOWN ] ) { scrollY++; mapa_y++; ludek.kierunek = 0; ludek.y += 2; frame++; }
            speed--;
           
            int startMapaX = scrollX / 40;
            int startMapaY = scrollY / 40;
           
            if( frame >= 40 ) frame = 0;
           
        }
        // Czyszczenie bufora
        clear_to_color( bufor, makecol( 150, 150, 150 ) );
       
        // Wyświetlanie mapy
        wys_mape();
       
        // Sterowanie animacją
        if( frame >= 0 && frame < 10 ) { ludek.klatka = 0; }
        else if( frame >= 10 && frame < 20 ) { ludek.klatka = 1; }
        else if( frame >= 20 && frame < 30 ) { ludek.klatka = 2; }
        else if( frame >= 30 && frame < 40 ) { ludek.klatka = 3; }
        //Wyświetlanie ludzika
        masked_blit( ludekb, bufor, ludek.klatka * 32, ludek.kierunek * 48, 300, 200, 32, 48 );
       
        blit( bufor, screen, 0, 0, 0, 0, 640, 480 );
    }
    // Usuwanie wszystkiego z pamięci.
    remove_int( increment_speed );
    destroy_bitmap( ludekb );
    destroy_bitmap( teren );
    destroy_bitmap( bufor );
    allegro_exit();
    return 0;
}
END_OF_MAIN();
P-4350
DejaVu
» 2009-02-22 17:29:07
A to działa?
C/C++
void wys_mape()
{
    int startMapaX = scrollX / 40;
    int startMapaY = scrollY / 40;
   
    for( int x = startMapaX; x < startMapaX + 16; x++ )
    {
        for( int y = startMapaY; y < startMapaY + 12; y++ )
        {
            blit( teren, bufor,
            ( map[ y ][ x ] % 4 ) * 40,
            ( map[ y ][ x ] / 4 ) * 40,
            ( x - startMapaX ) * 40 -( scrollX % 40 ),
            ( y - startMapaY ) * 40 -( scrollY % 40 ),
            40, 40 );
        }
    }
}
P-4351
PhonniX
Temat założony przez niniejszego użytkownika
» 2009-02-22 18:03:50
Działa :) dziękuje za pomoc.
P-4353
1 « 2 »
Poprzednia strona Strona 2 z 2