Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_native_dialog.h>
- #include <allegro5/allegro_image.h>
- #include <allegro5/allegro_audio.h>
- #include <allegro5/allegro_acodec.h>
- #include <sstream>
- #include <vector>
- #define ScreenWidth 800
- #define ScreenHeight 600
- int main()
- {
- const int FPS = 8;
- const int interval = 4;
- enum direction { S = 0, A = 4, W = 8, D = 12 };
- if(!al_init()){
- al_show_native_message_box(NULL, NULL, NULL, "Could not initialize allegro!", NULL, 0);
- return -1;
- }
- al_set_new_display_flags(ALLEGRO_WINDOWED);
- ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);
- al_set_window_position(display, 200, 100);
- al_set_window_title(display, "Greverus");
- if(!display){
- al_show_native_message_box(display, "Simple Title", "Display Settings", "Display window was not created successfully!", NULL, ALLEGRO_MESSAGEBOX_QUESTION);
- return -1;
- }
- bool done = false, draw = true, active = false;
- float x = 10, y = 10, moveSpeed = 10;
- int dir = S, prevDir, index = 0;
- int imageNumber = -1;
- bool startAnimation = false;
- al_install_keyboard();
- al_install_mouse();
- al_init_image_addon();
- ALLEGRO_BITMAP *person = al_load_bitmap("Data/textures/human/Ganondorf/ganondorf_sprite_att.png");
- std::vector<int> source;
- std::vector<int> width;
- ALLEGRO_COLOR pixel, lastPixel, color;
- source.push_back(0);
- width.push_back(0);
- for(int i = 0; i < al_get_bitmap_width(person); i++){
- color = al_map_rgba(255, 7, 49, 255);
- pixel = al_get_pixel(person, i, 0);
- if(memcmp(&pixel, &lastPixel, sizeof(ALLEGRO_COLOR))){
- if(!memcmp(&pixel, &color, sizeof(ALLEGRO_COLOR))){
- source.push_back(i);
- if(source.size() == 2)
- width.push_back(i);
- else
- width.push_back(i - width[width.size() - 1]);
- }
- }else if(i == al_get_bitmap_width(person) - 1){
- width.push_back(i - width[width.size() - 1]);
- }
- lastPixel = pixel;
- }
- al_convert_mask_to_alpha(person, al_map_rgba(255, 7, 49, 255));
- ALLEGRO_KEYBOARD_STATE keyState;
- ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
- ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
- al_register_event_source(event_queue, al_get_keyboard_event_source());
- al_register_event_source(event_queue, al_get_timer_event_source(timer));
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_register_event_source(event_queue, al_get_mouse_event_source());
- al_start_timer(timer);
- while(!done){
- ALLEGRO_EVENT events;
- al_wait_for_event(event_queue, &events);
- al_get_keyboard_state(&keyState);
- if(events.type == ALLEGRO_EVENT_KEY_UP){
- switch(events.keyboard.keycode)
- {
- case ALLEGRO_KEY_ESCAPE:
- done = true;
- }
- }else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
- done = true;
- }else if(events.type == ALLEGRO_EVENT_TIMER){
- active = true;
- prevDir = dir;
- if(al_key_down(&keyState, ALLEGRO_KEY_S)){
- y += moveSpeed;
- dir = S;
- }else if(al_key_down(&keyState, ALLEGRO_KEY_W)){
- y -= moveSpeed;
- dir = W;
- }else if(al_key_down(&keyState, ALLEGRO_KEY_D)){
- x += moveSpeed;
- dir = D;
- }else if(al_key_down(&keyState, ALLEGRO_KEY_A)){
- x -= moveSpeed;
- dir = A;
- }else if(al_key_down(&keyState, ALLEGRO_KEY_SPACE) && !startAnimation){
- startAnimation = true;
- }else{
- active = false;
- }
- if(startAnimation){
- imageNumber++;
- if(imageNumber >= source.size()){
- imageNumber = -1;
- startAnimation = false;
- }
- }
- draw = true;
- }
- if(draw){
- if(!startAnimation){
- al_draw_bitmap(person, x, y, 0);
- }else{
- al_draw_bitmap_region(person, source[imageNumber], 0, width[imageNumber], al_get_bitmap_height(person), x, y, 0);
- }
- al_flip_display();
- al_clear_to_color(al_map_rgb(0, 0, 0));
- }
- }
- al_destroy_display(display);
- al_destroy_timer(timer);
- //for(int i = 0; i < 16; i++){
- // al_destroy_bitmap(playerWalk[i]);
- //}
- al_destroy_event_queue(event_queue);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement