int main(void) { ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_MONITOR_INFO info; int w = 640, h = 480; bool done = false; bool need_redraw = true; bool background = false; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); if (!al_init_ttf_addon()) { abort_example("Failed to init TTF addon.\n"); return 1; } al_get_num_video_adapters(); al_get_monitor_info(0, &info); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_ALL, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 8, ALLEGRO_SUGGEST); al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); example.display = al_create_display(w, h); if (!example.display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } example.font = al_load_font("data/DejaVuSans.ttf", 40, 0); if (!example.font) { abort_example("Error loading data/DejaVuSans.ttf\n"); return 1; } example.font2 = al_load_font("data/DejaVuSans.ttf", 12, 0); if (!example.font2) { abort_example("Error loading data/DejaVuSans.ttf\n"); return 1; } example.mysha = al_load_bitmap("data/mysha.pcx"); if (!example.mysha) { abort_example("Error loading data/mysha.pcx\n"); return 1; } example.obp = al_load_bitmap("data/obp.jpg"); if (!example.obp) { abort_example("Error loading data/obp.jpg\n"); return 1; } init(); timer = al_create_timer(1.0 / FPS); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); al_register_event_source(queue, al_get_display_event_source(example.display)); al_start_timer(timer); while (!done) { ALLEGRO_EVENT event; w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!background && need_redraw && al_is_event_queue_empty(queue)) { double t = -al_get_time(); redraw(); t += al_get_time(); example.direct_speed_measure = t; al_flip_display(); need_redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_CHAR: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: background = true; al_acknowledge_drawing_halt(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: background = false; break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_TIMER: update(); need_redraw = true; break; } } return 0; }
int main(void) { ALLEGRO_THREAD *thread[NUM_THREADS]; ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; bool need_draw; int i; for (i = 0; i < 256; i++) { sin_lut[i] = 128 + (int) (127.0 * sin(i / 8.0)); } if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_keyboard(); al_install_mouse(); display = al_create_display(W * IMAGES_PER_ROW, H * NUM_THREADS / IMAGES_PER_ROW); if (!display) { abort_example("Error creating display\n"); return 1; } timer = al_install_timer(1.0/3); if (!timer) { abort_example("Error creating timer\n"); return 1; } queue = al_create_event_queue(); if (!queue) { abort_example("Error creating event queue\n"); return 1; } al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); /* Note: * Right now, A5 video displays can only be accessed from the thread which * created them (at lesat for OpenGL). To lift this restriction, we could * keep track of the current OpenGL context for each thread and make all * functions accessing the display check for it.. not sure it's worth the * additional complexity though. */ al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_RGB_888); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); for (i = 0; i < NUM_THREADS; i++) { thread_info[i].bitmap = al_create_bitmap(W, H); if (!thread_info[i].bitmap) { goto Error; } thread_info[i].mutex = al_create_mutex(); if (!thread_info[i].mutex) { goto Error; } thread_info[i].cond = al_create_cond(); if (!thread_info[i].cond) { goto Error; } thread_info[i].is_paused = false; thread_info[i].random_seed = i; thread[i] = al_create_thread(thread_func, &thread_info[i]); if (!thread[i]) { goto Error; } } set_target(0, -0.56062033041600878303, -0.56064322926933807256); set_target(1, -0.57798076669230014080, -0.63449861991138123418); set_target(2, 0.36676836392830602929, -0.59081385302214906030); set_target(3, -1.48319283039401317303, -0.00000000200514696273); set_target(4, -0.74052910500707636032, 0.18340899525730713915); set_target(5, 0.25437906525768350097, -0.00046678223345789554); set_target(6, -0.56062033041600878303, 0.56064322926933807256); set_target(7, -0.57798076669230014080, 0.63449861991138123418); set_target(8, 0.36676836392830602929, 0.59081385302214906030); for (i = 0; i < NUM_THREADS; i++) { al_start_thread(thread[i]); } al_start_timer(timer); need_draw = true; while (true) { if (need_draw && al_event_queue_is_empty(queue)) { show_images(); need_draw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_TIMER) { need_draw = true; } else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { int n = (event.mouse.y / H) * IMAGES_PER_ROW + (event.mouse.x / W); if (n < NUM_THREADS) { double x = event.mouse.x - (event.mouse.x / W) * W; double y = event.mouse.y - (event.mouse.y / H) * H; /* Center to the mouse click position. */ if (thread_info[n].is_paused) { thread_info[n].target_x = x / W - 0.5; thread_info[n].target_y = y / H - 0.5; } toggle_pausedness(n); } } else if (event.type == ALLEGRO_EVENT_DISPLAY_EXPOSE) { need_draw = true; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } need_draw = true; } } for (i = 0; i < NUM_THREADS; i++) { /* Set the flag to stop the thread. The thread might be waiting on a * condition variable, so signal the condition to force it to wake up. */ al_set_thread_should_stop(thread[i]); al_lock_mutex(thread_info[i].mutex); al_broadcast_cond(thread_info[i].cond); al_unlock_mutex(thread_info[i].mutex); /* al_destroy_thread() implicitly joins the thread, so this call is not * strictly necessary. */ al_join_thread(thread[i], NULL); al_destroy_thread(thread[i]); } al_destroy_event_queue(queue); al_uninstall_timer(timer); al_destroy_display(display); return 0; Error: return 1; }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *cursor; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; ALLEGRO_FONT *font; int mx = 0; int my = 0; int mz = 0; int mw = 0; int mmx = 0; int mmy = 0; int mmz = 0; int mmw = 0; bool in = true; bool buttons[NUM_BUTTONS] = {false}; int i; float p = 0.0; ALLEGRO_COLOR black; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_primitives_addon(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); actual_buttons = al_get_mouse_num_buttons(); if (actual_buttons > NUM_BUTTONS) actual_buttons = NUM_BUTTONS; al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); return 1; } al_hide_mouse_cursor(display); cursor = al_load_bitmap("data/cursor.tga"); if (!cursor) { abort_example("Error loading cursor.tga\n"); return 1; } font = al_load_font("data/fixed_font.tga", 1, 0); if (!font) { abort_example("data/fixed_font.tga not found\n"); return 1; } black = al_map_rgb_f(0, 0, 0); queue = al_create_event_queue(); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); while (1) { if (al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0)); for (i = 0; i < actual_buttons; i++) { draw_mouse_button(i, buttons[i]); } al_draw_bitmap(cursor, mx, my, 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_draw_textf(font, black, 5, 5, 0, "dx %i, dy %i, dz %i, dw %i", mmx, mmy, mmz, mmw); al_draw_textf(font, black, 5, 25, 0, "x %i, y %i, z %i, w %i", mx, my, mz, mw); al_draw_textf(font, black, 5, 45, 0, "p = %g", p); al_draw_textf(font, black, 5, 65, 0, "%s", in ? "in" : "out"); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); mmx = mmy = mmz = 0; al_flip_display(); } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_MOUSE_AXES: mx = event.mouse.x; my = event.mouse.y; mz = event.mouse.z; mw = event.mouse.w; mmx = event.mouse.dx; mmy = event.mouse.dy; mmz = event.mouse.dz; mmw = event.mouse.dw; p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = true; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = false; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY: in = true; break; case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: in = false; break; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { goto done; } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_CLOSE: goto done; } } done: al_destroy_event_queue(queue); return 0; }
int main(int argc, char **argv) { ////////////// if (argc < 2) { cout << "Podaj adres serwera" << endl; return 1; } string server = string(argv[1]); if (!connect_to_server(server)) { cout << "Połączenie nie powiodło się." << endl; return 1; } ////////////// ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0; bool redraw = true; if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize the mouse!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); if(!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_mouse_event_source()); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(1) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); ///////////////// int n = service_websockets(); string s; while (receive_packet(s)) { cout << "ODEBRAŁEM: " << s << endl; stringstream ss; ss << s; string p; ss >> p; if (p == "DRAW") { int x,y; ss >> x; ss >> y; al_draw_bitmap(bouncer, x, y, 0); } } ///////////////// if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES || ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) { bouncer_x = ev.mouse.x; bouncer_y = ev.mouse.y; } else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { stringstream ss; ss << "DRAW " << ev.mouse.x << " " << ev.mouse.y; send_packet(ss.str()); } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_flip_display(); } }
int main(void) { srand(static_cast<int>(time(nullptr) % 100 )); bool keys[10] = {false, false, false, false, false, false, false, false, false, false}; bool done = false; bool redraw = true; int FPS = 60; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //initialize allegro if(!al_init()) { al_show_native_message_box(NULL, NULL, NULL, "failed to initialize allegro!", NULL, NULL); return -1; } //create our display object display = al_create_display(app_width, app_height); //test display object if(!display) { al_show_native_message_box(NULL, NULL, NULL, "failed to initialize display!", NULL, NULL); return -1; } al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_FONT *font12 = al_load_font("arial.ttf", 12, 0); ALLEGRO_FONT *font24 = al_load_font("arial.ttf", 24, 0); ALLEGRO_FONT *font36 = al_load_font("arial.ttf", 36, 0); ALLEGRO_FONT *font18 = al_load_font("arial.ttf", 18, 0); al_init_primitives_addon(); al_install_keyboard(); al_install_mouse(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); //al_hide_mouse_cursor(display); al_start_timer(timer); Tilemap tilemap; /*int mountain_index = rand() % (tilemap_size_num_tiles_x * tilemap_size_num_tiles_y); tilemap.tile_[300].height_ = 10; tilemap.tile_[300].erosion_givingness = 0; int deep_index = rand() % (tilemap_size_num_tiles_x * tilemap_size_num_tiles_y); tilemap.tile_[301].height_ = -10; tilemap.tile_[301].erosion_givingness = 0;*/ std::vector<Sprite> tree; Sprite p1(tile_render_size*1.5,tile_render_size*1.5,color_p1); Sprite p2(tile_render_size*2.5,tile_render_size*1.5,color_p2); Timer tilemap_time; int num_natural_erosions = 0; int sec_per_natural_erosion = 1; int num_tree_periods = 0; int sec_per_tree_period = 0.3; while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if(ev.mouse.button & 1) { p1.x = ev.mouse.x; p1.y = ev.mouse.y; } else if (ev.mouse.button & 2) { p2.x = ev.mouse.x; p2.y = ev.mouse.y; } } else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES) { } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_W: keys[W] = true; break; case ALLEGRO_KEY_A: keys[A] = true; break; case ALLEGRO_KEY_S: keys[S] = true; break; case ALLEGRO_KEY_D: keys[D] = true; break; case ALLEGRO_KEY_E: keys[E] = true; std::cout << "E:\n"; //std::cout << "\n" << tilemap.tile[1][1].height() << "\n"; ErodeTilemap(tilemap); //std::cout << "\n" << tilemap.tile[1][1].height() << "\n"; break; case ALLEGRO_KEY_N: keys[N] = true; Tilemap new_tilemap; //for ( int i = 0; i < 10; i++ ) new_tilemap = ErodeTilemap(new_tilemap); tilemap = new_tilemap; tree.clear(); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_W: keys[W] = false; break; case ALLEGRO_KEY_A: keys[A] = false; break; case ALLEGRO_KEY_S: keys[S] = false; break; case ALLEGRO_KEY_D: keys[D] = false; break; case ALLEGRO_KEY_E: keys[E] = false; break; case ALLEGRO_KEY_N: keys[N] = false; break; case ALLEGRO_KEY_ESCAPE: done = true; break; } } else if(ev.type == ALLEGRO_EVENT_TIMER) { if ( keys[UP] ) p1.y -= 1; if ( keys[DOWN] ) p1.y += 1; if ( keys[RIGHT] ) p1.x += 1; if ( keys[LEFT] ) p1.x -= 1; if ( keys[W] ) p2.y -= 1; if ( keys[S] ) p2.y += 1; if ( keys[D] ) p2.x += 1; if ( keys[A] ) p2.x -= 1; /*ALLEGRO_MOUSE_STATE MouseState; al_get_mouse_state(&MouseState); if ( MouseState.buttons & 1 ) { p1.x = ev.mouse.x; }*/ if ( tilemap_time.now() > num_natural_erosions * sec_per_natural_erosion ) { ErodeTilemap(tilemap); num_natural_erosions++; //kill underwater and beach trees: for( int i = 0; i < tree.size(); i++ ) { if ( 0.2 > tilemap.tile_[(int)tree[i].x%tile_render_size,(int)tree[i].y/tile_render_size].height() ) { tree.erase(tree.begin()+i); } } } if ( tilemap_time.now() > num_tree_periods * sec_per_tree_period ) { for ( int i = 0; i < tilemap_size_num_tiles_x * tilemap_size_num_tiles_y; i++) { if ( tilemap.tile_[i].height_ > 1 ) { if ( 0.05 > (float)(rand() % 10000) / 100 ) { int x = i_t_x(i)*tile_render_size + rand()%tile_render_size; int y = i_t_y(i)*tile_render_size + rand()%tile_render_size; int g = 102+rand()%102; tree.push_back(Sprite(x,y,Color(0,g,0))); } } } } if ( collision(p1,p2) ) std::cout << "collision!\n"; redraw = true; } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; // Draw tilemap tiles: // i and j changes are hack that will cause problems later for ( int i = 1; i < view_tile_count_x+1; i++ ) { for ( int j = 1; j < view_tile_count_y+1; j++ ) { al_draw_filled_rectangle(i*tile_render_size,j*tile_render_size,i*tile_render_size+tile_render_size,j*tile_render_size+tile_render_size, al_map_rgb(tilemap.tile_[xy_t_i(i,j)].color_.r,tilemap.tile_[xy_t_i(i,j)].color_.g,tilemap.tile_[xy_t_i(i,j)].color_.b)); //al_draw_textf(font12, al_map_rgb(255,255,255), i*tile_render_size + tile_render_size/2,j*tile_render_size, ALLEGRO_ALIGN_CENTRE, "%.1fm", tilemap.tile_[xy_t_i(i,j)].height() ); //al_draw_textf(font12, al_map_rgb(255,255,255), i*tile_render_size + tile_render_size/2,(j+.5)*tile_render_size, ALLEGRO_ALIGN_CENTRE, "%d", i + j * tilemap_size_num_tiles_x ); } } // Draw player 1 //al_draw_filled_rectangle(p1.x-p1.radius, p1.y-p1.radius, p1.x+p1.radius, p1.y+p1.radius, al_map_rgb(204, 0, 0)); al_draw_filled_circle(p1.x, p1.y, p1.radius, al_map_rgb(p1.color.r, p1.color.g, p1.color.b)); al_draw_text(font24, al_map_rgb(0,0,0), p1.x, p1.y-p1.radius, ALLEGRO_ALIGN_CENTRE, "1"); // Draw player 2 //al_draw_filled_rectangle(p2.x-p2.radius, p2.y-p2.radius, p2.x+p2.radius, p2.y+p2.radius, al_map_rgb(0, 102, 204)); al_draw_filled_circle(p2.x, p2.y, p2.radius, al_map_rgb(p2.color.r, p2.color.g, p2.color.b)); al_draw_text(font24, al_map_rgb(0,0,0), p2.x, p2.y-p2.radius, ALLEGRO_ALIGN_CENTRE, "2"); // Draw trees for( int i = 0; i < tree.size(); i++ ) { al_draw_filled_circle(tree[i].x, tree[i].y, tree[i].radius, al_map_rgb(tree[i].color.r, tree[i].color.g, tree[i].color.b)); } // Draw info al_draw_textf(font12, al_map_rgb(255,255,255), tilemap_size_num_tiles_x*tile_render_size,0,0, "tilemap_time.now(): %.1f --- ErodeTilemap occurs every 10s!", tilemap_time.now() ); al_draw_textf(font12, al_map_rgb(255,255,255), tilemap_size_num_tiles_x*tile_render_size,12,0, "ErodeTilemap occurs every %ds!", sec_per_natural_erosion ); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } al_destroy_font(font12); al_destroy_font(font18); al_destroy_font(font24); al_destroy_font(font36); al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_display(display); //destroy our display object return 0; }
//MAIN// int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_BITMAP *image = NULL, *image1 = NULL, *image2 = NULL, *image3 = NULL; ALLEGRO_FONT *font1 = NULL, *font2 = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; if(!al_init()) { return -1; } if(!al_install_keyboard()) { return -1; } if(!al_install_mouse()) { return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { return -1; } event_queue = al_create_event_queue(); if(!event_queue) { return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_init_primitives_addon(); al_init_font_addon(); al_init_ttf_addon(); if(!al_init_image_addon()) { printf("Problema com inicialização image_addon"); return -1; } font1 = al_load_ttf_font("pirulen.ttf",12,0 ); if (!font1) { printf("Problema ao carregar pirulen.ttf"); return -1; } image = al_load_bitmap("iceblue2.png"); if(!image) { printf("Problema ao carregar iceblue2.png"); return -1; } image1 = al_load_bitmap("IceBlue1.png"); if(!image1) { printf("Problema ao carregar IceBlue1.png"); return -1; } image2 = al_load_bitmap("NG.png"); if(!image2) { printf("Problema ao carregar NG.png"); return -1; } image3 = al_load_bitmap("retanrosa.png"); if(!image3) { printf("Problema ao carregar retanrosa.png"); return -1; } al_draw_bitmap(image,0,0,0); al_draw_bitmap(image1,10,10,10); al_draw_bitmap(image2,180,45,15); al_draw_bitmap(image3,310,45,15); //FUNÇÃO GERA RETANGULO// x1_ini=30.0, y1_ini=170.0, x2_ini=100.0, y2_ini=240; x1 = x1_ini,y1= y1_ini,x2 = x2_ini,y2 = y2_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 193)); y1=y1+80; y2=y2+80; } y1=y1_ini; y2=y2_ini; x1=x1+80; x2=x2+80; } font2 = al_load_ttf_font("pirulen.ttf",23,0 ); if (!font2) { return -1; } al_flip_display(); //LOGICA// while(!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=4; linha>=1; linha--) { if ((matriz[linha][coluna]!=0) && (matriz[linha-1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha-1][coluna]; matriz[linha-1][coluna]=nt; } } } } for (coluna=0; coluna<5; coluna++) { for (linha=0; linha<4; linha++) { if ((matriz[linha][coluna]==matriz[linha+1][coluna])&&(matriz[linha][coluna]!=0)) { matriz[linha+1][coluna]+=matriz[linha][coluna]; matriz[linha][coluna]=0; linha++; } } } for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=4; linha>=1; linha--) { if ((matriz[linha][coluna]!=0) && (matriz[linha-1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha-1][coluna]; matriz[linha-1][coluna]=nt; } } } } break; case ALLEGRO_KEY_DOWN: for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=0; linha<4; linha++) { if ((matriz[linha][coluna]!=0) && (matriz[linha+1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha+1][coluna]; matriz[linha+1][coluna]=nt; } } } } for (coluna=0; coluna<5; coluna++) { for (linha=4; linha>=1; linha--) { if ((matriz[linha][coluna]==matriz[linha-1][coluna])&&(matriz[linha][coluna]!=0)) { matriz[linha][coluna]+=matriz[linha-1][coluna]; matriz[linha-1][coluna]=0; linha--; } } } for (coluna=0; coluna<5; coluna++) { for (i=3; i>=0; i--) { for (linha=0; linha<4; linha++) { if ((matriz[linha][coluna]!=0) && (matriz[linha+1][coluna]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha+1][coluna]; matriz[linha+1][coluna]=nt; } } } } break; case ALLEGRO_KEY_LEFT: for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=4; coluna>=1; coluna--) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna-1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna-1]; matriz[linha][coluna-1]=nt; } } } } for (linha=0; linha<5; linha++) { for (coluna=0; coluna<4; coluna++) { if ((matriz[linha][coluna]==matriz[linha][coluna+1])&&(matriz[linha][coluna]!=0)) { matriz[linha][coluna+1]+=matriz[linha][coluna]; matriz[linha][coluna]=0; coluna++; } } } for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=4; coluna>=1; coluna--) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna-1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna-1]; matriz[linha][coluna-1]=nt; } } } } break; case ALLEGRO_KEY_RIGHT: for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=0; coluna<4; coluna++) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna+1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna+1]; matriz[linha][coluna+1]=nt; } } } } for (linha=0; linha<5; linha++) { for (coluna=4; coluna>=1; coluna--) { if ((matriz[linha][coluna]==matriz[linha][coluna-1])&&(matriz[linha][coluna]!=0)) { matriz[linha][coluna-1]+=matriz[linha][coluna]; matriz[linha][coluna]=0; coluna--; } } } for (linha=0; linha<5; linha++) { for (i=3; i>=0; i--) { for (coluna=0; coluna<4; coluna++) { if ((matriz[linha][coluna]!=0) && (matriz[linha][coluna+1]==0)) { nt=matriz[linha][coluna]; matriz[linha][coluna]=matriz[linha][coluna+1]; matriz[linha][coluna+1]=nt; } } } } break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } //GERADOR RANDOM// random(&rcoluna,&rlinha); while (matriz[rlinha][rcoluna]!=0) { random(&rcoluna,&rlinha); } matriz[rlinha][rcoluna]= (rand()%4 == 3)? 4 : 2; for (linha=0; linha<=4; linha++) { for (coluna=0; coluna<=4; coluna++) { if (matriz[linha][coluna]!=0) i++; } } if (i==25) break; //SCORE// al_draw_bitmap(image3,310,45,15); score=0; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { score+=matriz[ql][qc]; } } al_draw_textf(font1, al_map_rgb(255,255,255),365,68,ALLEGRO_ALIGN_CENTRE,"%i",score); } //FUNÇÃO GERA RETANGULO// x1_ini=30.0, y1_ini=170.0, x2_ini=100.0, y2_ini=240; x1 = x1_ini,y1= y1_ini,x2 = x2_ini,y2 = y2_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { if(matriz[ql][qc]==0) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 193)); } else if(matriz[ql][qc]==2) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 182, 151)); } else if(matriz[ql][qc]==4) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 141, 132)); } else if(matriz[ql][qc]==8) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 100)); } else if(matriz[ql][qc]==16) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 128, 1)); } else if(matriz[ql][qc]==32) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 128)); } else if(matriz[ql][qc]==64) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 30)); } else if(matriz[ql][qc]==128) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(108, 128, 255)); } else if(matriz[ql][qc]==256) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(108, 188, 255)); } else if(matriz[ql][qc]==512) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(15, 203, 80)); } else if(matriz[ql][qc]==1024) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(140, 255, 198)); } else if(matriz[ql][qc]==2048) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 255, 26)); } else if(matriz[ql][qc]==4069) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 0)); } else { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 225, 255)); } y1=y1+80; y2=y2+80; } y1=y1_ini; y2=y2_ini; x1=x1+80; x2=x2+80; } //FUNÇÃO GERA MATRIZ// text2x_ini=64, text2y_ini=192; text2x=text2x_ini, text2y=text2y_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { if (matriz[ql][qc]==0) { c1=255, c2=211, c3=193; } else { c1=255, c2=255, c3=255; } al_draw_textf(font2, al_map_rgb(c1,c2,c3),text2x,text2y,ALLEGRO_ALIGN_CENTRE, "%i",matriz[ql][qc]); text2y=text2y+80; } text2y=text2y_ini; text2x=text2x+80; al_flip_display(); } //MOUSE EVENT// if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { if(ev.mouse.x >= al_get_bitmap_width(image2) && ev.mouse.x <= SCREEN_W -10 && ev.mouse.y <= SCREEN_H - 10 && ev.mouse.y >= al_get_bitmap_height(image2) - 10) { al_draw_bitmap(image3,310,45,15); score=0; text2x_ini=64, text2y_ini=192; text2x=text2x_ini, text2y=text2y_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { c1=255, c2=211, c3=193; matriz[ql][qc]=0; al_draw_textf(font2, al_map_rgb(c1,c2,c3),text2x,text2y,ALLEGRO_ALIGN_CENTRE, "%i",matriz[ql][qc]); text2y=text2y+80; } text2y=text2y_ini; text2x=text2x+80; } x1_ini=30.0, y1_ini=170.0, x2_ini=100.0, y2_ini=240; x1 = x1_ini,y1= y1_ini,x2 = x2_ini,y2 = y2_ini; for(qc=0; qc<5; qc++) { for (ql=0; ql<5; ql++) { al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 211, 193)); y1=y1+80; y2=y2+80; } y1=y1_ini; y2=y2_ini; x1=x1+80; x2=x2+80; } al_flip_display(); } } } system("cls"); printf("GAME OVER!"); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
int main(int argc, char *argv[]) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *evqueue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_KEYBOARD_STATE keyboard_state; ALLEGRO_EVENT event; ALLEGRO_FONT *font; ALLEGRO_BITMAP *clock_hand, *clock_quadrant, *bow, *sword; float clock_ray = 0, clock_angle = 0; int clock_ray_alpha; float soul_interval = SOUL_TIME_INTERVAL; FuzzyPlayer *player, *cpu; FuzzyGame * game; bool running = true; bool redraw = true; int map_x = 13*16, map_y = 5*16; int screen_width = WINDOW_WIDTH; int screen_height = WINDOW_HEIGHT; double curtime; /* Initialization */ fuzzy_iz_error(al_init(), "Failed to initialize allegro"); fuzzy_load_addon("image", al_init_image_addon()); fuzzy_load_addon("primitives", al_init_primitives_addon()); fuzzy_load_addon("keyboard", al_install_keyboard()); fuzzy_load_addon("mouse", al_install_mouse()); al_init_font_addon(); fuzzy_iz_error(timer = al_create_timer(1.0 / FPS), "Cannot create FPS timer"); fuzzy_iz_error(evqueue = al_create_event_queue(), "Cannot create event queue"); fuzzy_iz_error(display = al_create_display(screen_width, screen_height), "Cannot initialize display"); al_set_window_title(display, WINDOW_TITLE); fuzzy_iz_error(font = al_load_font(fuzzy_res(FONT_FOLDER, "fixed_font.tga"), 0, 0), "Cannot load 'fixed_font.tga'"); clock_hand = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "clock_hand.png")); fuzzy_iz_error(clock_hand, "Cannot load clock handle"); clock_quadrant = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "clock_quadrant.png")); fuzzy_iz_error(clock_hand, "Cannot load clock quadrant"); bow = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "bow.png")); fuzzy_iz_error(clock_hand, "Cannot load bow image"); sword = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "sword.png")); fuzzy_iz_error(clock_hand, "Cannot load sword image"); /* Queue setup */ al_register_event_source(evqueue, al_get_display_event_source(display)); al_register_event_source(evqueue, al_get_timer_event_source(timer)); al_register_event_source(evqueue, al_get_keyboard_event_source()); al_register_event_source(evqueue, al_get_mouse_event_source()); /* Game setup */ game = fuzzy_game_new("level000.tmx"); player = fuzzy_player_new(game, FUZZY_PLAYER_LOCAL, "Dolly"); cpu = fuzzy_player_new(game, FUZZY_PLAYER_CPU, "CPU_0"); fuzzy_chess_add(game, player, FUZZY_FOO_LINK, 34, 30); fuzzy_chess_add(game, player, FUZZY_FOO_LINK, 33, 30); fuzzy_chess_add(game, cpu, FUZZY_FOO_LINK, 40, 30); bool showing_area = false; FuzzyChess *chess, *focus = NULL; al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(game->map->bitmap, -map_x, -map_y, 0); al_flip_display(); #if DEBUG ALLEGRO_BITMAP *icon; int fps, fps_accum; double fps_time; icon = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "icon.tga")); if (icon) al_set_display_icon(display, icon); fps_accum = fps_time = 0; fps = FPS; #endif /* Server connection */ int svsock; //~ FuzzyMessage * sendmsg = fuzzy_message_new(); svsock = fuzzy_server_connect(FUZZY_DEFAULT_SERVER_ADDRESS, FUZZY_DEFAULT_SERVER_PORT); _aaa_menu(game, svsock); /* MAIN loop */ player->soul_time = al_get_time(); al_start_timer(timer); while (running) { /* wait until an event happens */ al_wait_for_event(evqueue, &event); switch (event.type) { case ALLEGRO_EVENT_TIMER: /* check soul ticks */ curtime = al_get_time(); while (curtime - player->soul_time >= soul_interval) { //~ fuzzy_debug("Soul tick!"); player->soul_time += soul_interval; player->soul_points += SOUL_POINTS_BOOST; clock_ray = 1; } clock_angle = (curtime - player->soul_time)/soul_interval * FUZZY_2PI; if (clock_ray) { clock_ray = (curtime - player->soul_time)/RAY_TIME_INTERVAL * 50 + 40; clock_ray_alpha = (curtime - player->soul_time)/RAY_TIME_INTERVAL*(55) + 200; if (clock_ray >= 90) clock_ray = 0; } al_get_keyboard_state(&keyboard_state); if (al_key_down(&keyboard_state, ALLEGRO_KEY_RIGHT)) { map_x += 5; if (map_x > (game->map->tot_width - screen_width)) map_x = game->map->tot_width - screen_width; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_LEFT)) { map_x -= 5; if (map_x < 0) map_x = 0; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_UP)) { map_y -= 5; if (map_y < 0) map_y = 0; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_DOWN)) { map_y += 5; if (map_y > (game->map->tot_height - screen_height)) map_y = game->map->tot_height - screen_height; } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_O)) { soul_interval = fuzzy_max(0.1, soul_interval - 0.05); } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_P)) { soul_interval += 0.05; } redraw = true; break; case ALLEGRO_EVENT_KEY_DOWN: if(! focus) break; _attack_area_off(); switch(event.keyboard.keycode) { case ALLEGRO_KEY_W: _chess_move(game, player, focus, focus->x, focus->y-1); break; case ALLEGRO_KEY_A: _chess_move(game, player, focus, focus->x-1, focus->y); break; case ALLEGRO_KEY_S: _chess_move(game, player, focus, focus->x, focus->y+1); break; case ALLEGRO_KEY_D: _chess_move(game, player, focus, focus->x+1, focus->y); break; case ALLEGRO_KEY_K: _attack_area_on(); break; case ALLEGRO_KEY_SPACE: /* switch attack type */ if (! focus) break; if (focus->atkarea == &FuzzyMeleeMan) focus->atkarea = &FuzzyRangedMan; else focus->atkarea = &FuzzyMeleeMan; break; } break; case ALLEGRO_EVENT_DISPLAY_CLOSE: running = false; break; case ALLEGRO_EVENT_KEY_UP: break; case ALLEGRO_EVENT_KEY_CHAR: break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if(event.mouse.button == RIGHT_BUTTON) { _attack_area_on(); } else if(event.mouse.button == LEFT_BUTTON) { /* world to tile coords */ int tx = (event.mouse.x+map_x) / game->map->tile_width; int ty = (event.mouse.y+map_y) / game->map->tile_height; #ifdef DEBUG printf("SELECT %d %d\n", tx, ty); #endif if(showing_area && fuzzy_chess_inside_target_area(game, focus, tx, ty)) { /* select attack target */ if (fuzzy_map_spy(game->map, FUZZY_LAYER_SPRITES, tx, ty) == FUZZY_CELL_SPRITE) { if (fuzzy_chess_local_attack(game, player, focus, tx, ty)) _attack_area_off(); } } else { /* select chess */ chess = fuzzy_chess_at(game, player, tx, ty); if (chess && focus != chess) { _attack_area_off(); if (focus != NULL) { // already has a focus effect, just move it fuzzy_sprite_move(game->map, FUZZY_LAYER_BELOW, focus->x, focus->y, tx, ty); } else { fuzzy_sprite_create(game->map, FUZZY_LAYER_BELOW, GID_TARGET, tx, ty); } focus = chess; } else if (! chess) { if (showing_area) { // just hide the attack area _attack_area_off(); } else if(focus) { // remove the focus fuzzy_sprite_destroy(game->map, FUZZY_LAYER_BELOW, focus->x, focus->y); focus = NULL; } } } } break; default: #ifdef DEBUG //~ fprintf(stderr, "Unknown event received: %d\n", event.type); #endif break; } if (redraw && al_is_event_queue_empty(evqueue)) { curtime = al_get_time(); fuzzy_map_update(game->map, curtime); // Clear the screen al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(game->map->bitmap, -map_x, -map_y, 0); #ifdef GRID_ON /* Draw the grid */ int tw = game->map->tile_width; int ty = game->map->tile_height; int x, y; for (x=(tw-map_x)%tw; x<screen_width; x+=tw) al_draw_line(x, 0, x, screen_height, al_map_rgba(7,7,7,100), 1); for (y=(ty-map_y)%ty; y<screen_height; y+=ty) al_draw_line(0, y, screen_width, y, al_map_rgba(7,7,7,100), 1); #endif #if DEBUG al_draw_filled_rounded_rectangle(screen_width-100, 4, screen_width, 30, 8, 8, al_map_rgba(0, 0, 0, 200)); al_draw_textf(font, al_map_rgb(255, 255, 255), screen_width-50, 8, ALLEGRO_ALIGN_CENTRE, "FPS: %d", fps); #endif /* draw SP count */ al_draw_filled_rounded_rectangle(4, screen_height-170, 175, screen_height-4, 8, 8, al_map_rgba(0, 0, 0, 200)); al_draw_textf(font, al_map_rgb(255, 255, 255), 15, screen_height-163, ALLEGRO_ALIGN_LEFT, "SP: %d", player->soul_points); /* draw Soul Clock */ al_draw_scaled_bitmap(clock_quadrant, 0, 0, 301, 301, 20, screen_height-80-139/2, 139, 139, 0); al_draw_scaled_rotated_bitmap(clock_hand, 160, 607, 90, screen_height-80, 0.11, 0.11, clock_angle, 0); al_draw_circle(90, screen_height-80, clock_ray, al_map_rgb(80, clock_ray_alpha, 80), 2.0); /* draw weapon */ if (focus) { ALLEGRO_BITMAP * weapon; if (focus->atkarea == &FuzzyMeleeMan) weapon = sword; else weapon = bow; al_draw_scaled_bitmap(weapon, 0, 0, 90, 90, 20, 20, 60, 60, 0); } al_flip_display(); #if DEBUG fps_accum++; if (curtime - fps_time >= 1) { fps = fps_accum; fps_accum = 0; fps_time = curtime; } #endif redraw = false; } } /* Cleanup */ //~ void * retval; //~ char srvkey[FUZZY_SERVERKEY_LEN]; //~ fuzzy_protocol_server_shutdown(svsock, sendmsg, srvkey); //~ fuzzy_message_del(sendmsg); fuzzy_game_free(game); al_destroy_event_queue(evqueue); al_destroy_display(display); al_destroy_timer(timer); return 0; }
void engine_run(struct State *first) { int redraw = FALSE; if (engine_active) { puts("WARNING: Calling game_run() more than once"); return; } change_state(first); // Generate display events al_register_event_source(engine.event_queue, al_get_display_event_source(engine.display)); // Timer events al_register_event_source(engine.event_queue, al_get_timer_event_source(engine.timer)); // Keyboard events al_register_event_source(engine.event_queue, al_get_keyboard_event_source()); al_start_timer(engine.timer); engine_active = TRUE; // Main game loop while (engine_active) { ALLEGRO_EVENT event; al_wait_for_event(engine.event_queue, &event); // Event processing engine.states[current_state]->_events(&event); // If the close button was pressed... if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { engine_active = FALSE; break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { keys[event.keyboard.keycode] = TRUE; // Escape key will end the game if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { engine_active = FALSE; break; } } else if (event.type == ALLEGRO_EVENT_KEY_UP) { keys[event.keyboard.keycode] = FALSE; } else if (event.type == ALLEGRO_EVENT_TIMER) { engine.states[current_state]->_update(); redraw = TRUE; } if (redraw && al_event_queue_is_empty(engine.event_queue)) { redraw = FALSE; al_set_target_backbuffer(engine.display); al_clear_to_color(engine.bg_color); engine.states[current_state]->_draw(); al_flip_display(); } } while (current_state >= 0) { engine.states[current_state--]->_end(); } al_destroy_display(engine.display); al_destroy_timer(engine.timer); al_destroy_event_queue(engine.event_queue); al_destroy_font(font); }
static bool init(void) { if(!al_init()) { fprintf(stderr, "failed to initialize allegro.\n"); return false; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize keyboard.\n"); return false; } if(!al_init_image_addon()) { fprintf(stderr, "failed to initialize image system.\n"); return false; } al_init_font_addon(); if(!al_init_ttf_addon()) { fprintf(stderr, "failed to initialize ttf system.\n"); return false; } /* sound */ if(!al_install_audio()) { fprintf(stderr, "failed to initialize audio system.\n"); return false; } if(!al_init_acodec_addon()) { fprintf(stderr, "failed to initialize audio codecs.\n"); return false; } if(!al_reserve_samples(10)) { fprintf(stderr, "failed to reserve audio samples.\n"); return false; } /* fonts */ asteroids.small_font = al_load_ttf_font("data/vectorb.ttf", 12, 0); asteroids.large_font = al_load_ttf_font("data/vectorb.ttf", 24, 0); /* lives sprite */ asteroids.lives_sprite = al_load_bitmap("data/sprites/ship/ship.png"); if(!asteroids.lives_sprite) { fprintf(stderr, "failed to load lives sprite.\n"); return false; } /* sprite preloading */ if(!level_init()) return false; if(!ship_init()) return false; if(!missile_init()) return false; if(!saucer_init()) return false; if(!asteroid_init()) return false; if(!explosion_init()) return false; asteroids.timer = al_create_timer(1.0 / FPS); if(!asteroids.timer) { fprintf(stderr, "failed to create timer.\n"); return false; } asteroids.event_queue = al_create_event_queue(); if(!asteroids.event_queue) { fprintf(stderr, "failed to create event queue.\n"); return false; } if(FULLSCREEN) al_set_new_display_flags(ALLEGRO_FULLSCREEN); al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); asteroids.display = al_create_display(SCREEN_W, SCREEN_H); if(!asteroids.display) { fprintf(stderr, "failed to create display.\n"); return false; } /* TODO: show on mouse movement */ al_hide_mouse_cursor(asteroids.display); al_register_event_source(asteroids.event_queue, al_get_display_event_source(asteroids.display)); al_register_event_source(asteroids.event_queue, al_get_timer_event_source(asteroids.timer)); al_register_event_source(asteroids.event_queue, al_get_keyboard_event_source()); return true; }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; int redraw = 0, i; bool quit = false; if (!al_init()) { abort_example("Could not initialise Allegro\n"); return 1; } al_init_primitives_addon(); al_install_mouse(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); srand(time(NULL)); white = al_map_rgba_f(1, 1, 1, 1); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display\n"); return 1; } al_set_window_title(display, "Allegro Logo Generator"); al_install_keyboard(); /* Read logo parameters from logo.ini (if it exists). */ config = al_load_config_file("logo.ini"); if (!config) config = al_create_config(); for (i = 0; param_names[i]; i++) { char const *value = al_get_config_value(config, "logo", param_names[i]); if (value) strncpy(param_values[i], value, sizeof(param_values[i])); } font = al_load_font("data/DejaVuSans.ttf", 12, 0); if (!font) { abort_example("Could not load font\n"); return 1; } timer = al_create_timer(1.0 / 60); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (!quit) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_KEY_CHAR) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { quit = true; } else if (event.keyboard.keycode == ALLEGRO_KEY_ENTER) { if (editing) { regenerate = true; editing = false; } else { cursor = 0; editing = true; } } else if (event.keyboard.keycode == ALLEGRO_KEY_UP) { if (selection > 0) { selection--; cursor = 0; editing = false; } } else if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) { if (param_names[selection + 1]) { selection++; cursor = 0; editing = false; } } else { int c = event.keyboard.unichar; if (editing) { if (c >= 32) { ALLEGRO_USTR *u = al_ustr_new(param_values[selection]); al_ustr_set_chr(u, cursor, c); cursor++; al_ustr_set_chr(u, cursor, 0); strncpy(param_values[selection], al_cstr(u), sizeof param_values[selection]); al_ustr_free(u); } } else { if (c == 'r') randomize(); if (c == 's') save(); } } } if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if (event.mouse.button == 1) { mouse_click(event.mouse.x, event.mouse.y); } } if (event.type == ALLEGRO_EVENT_TIMER) redraw++; if (redraw && al_is_event_queue_empty(queue)) { redraw = 0; render(); al_flip_display(); } } /* Write modified parameters back to logo.ini. */ for (i = 0; param_names[i]; i++) { al_set_config_value(config, "logo", param_names[i], param_values[i]); } al_save_config_file("logo.ini", config); al_destroy_config(config); return 0; }
int main(int argc, char** argv) { time_t t; srand((unsigned) time(&t)); if (!al_init()) { std::cout << "Failed to start Allegro."; return -1; } if (!al_init_image_addon()) { std::cout << "Failed to start Allegro Imagea person who gives information to the police or to some other authority about the bad behavior or criminal activity of someone else Addon.\n"; return -1; } if (!al_init_primitives_addon()) { std::cout << "Failed to start Allegro Primitives Addon.\n"; return -1; } if (!al_init_font_addon()) { std::cout << "Failed to start Allegro Font Addon.\n"; return -1; } if (!al_init_ttf_addon()) { std::cout << "Failed to start Allegro TTF Addon.\n"; return -1; } ALLEGRO_DISPLAY* main_window = al_create_display(800, 600); if (!main_window) { std::cout << "Failed to create display."; return -1; } ALLEGRO_EVENT_QUEUE* eq = al_create_event_queue(); if (!eq) { std::cout << "Failed to create event queue."; return -1; } ALLEGRO_TIMER* fps_timer = al_create_timer(1.0 / 60); if (!fps_timer) { std::cout << "Failed to create timer."; return -1; } ALLEGRO_TIMER* count_timer = al_create_timer(1.0); if (!count_timer) { std::cout << "Failed to create count timer."; return -1; } if (!al_install_keyboard()) { std::cout << "Failed to install keyboard."; return -1; } if (!al_install_mouse() || !al_set_mouse_cursor(main_window, al_create_mouse_cursor(al_load_bitmap("resources/sprites/UI/cursor/clicker.png"), 16, 31))) { std::cout << "Failed to install mouse."; return -1; } al_register_event_source(eq, al_get_timer_event_source(fps_timer)); al_register_event_source(eq, al_get_timer_event_source(count_timer)); al_register_event_source(eq, al_get_keyboard_event_source()); al_register_event_source(eq, al_get_mouse_event_source()); al_register_event_source(eq, al_get_display_event_source(main_window)); Game base; if (!base.init()) { std::cout << "Failed to initialise game! Quitting...\n"; return -1; } bool ready_to_draw = false; bool ready_to_draw_fps = false; al_start_timer(fps_timer); al_start_timer(count_timer); int fps_count = 0; ALLEGRO_FONT* fps_font = al_load_ttf_font("resources/fonts/MontereyFLF.ttf", 11, 0); if (!fps_font) { std::cout << "Failed to load font for fps counter!"; return -1; } while (true) { while (!al_event_queue_is_empty(eq)) { ALLEGRO_EVENT ev; al_get_next_event(eq, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { if (ev.timer.source == fps_timer) { ready_to_draw = true; } else if (ev.timer.source == count_timer) { ready_to_draw_fps = true; } } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { std::cout << "Safely quitting."; al_destroy_display(main_window); al_destroy_event_queue(eq); al_destroy_timer(fps_timer); return 0; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) //current keyboard input section { std::cout << "Safely quitting."; al_destroy_display(main_window); al_destroy_event_queue(eq); al_destroy_timer(fps_timer); return 0; } } base.runEvents(ev); } if (ready_to_draw) { fps_count++; base.runLogic(); base.runDisplay(); char buffer[25]; if (ready_to_draw_fps) { sprintf(buffer, "%d", fps_count); fps_count = 0; ready_to_draw_fps = false; } al_draw_text(fps_font, al_map_rgb(0,255,0), 5, 5, 0, buffer); al_flip_display(); ready_to_draw = false; } } }
static SCM game_init (SCM game_smob, SCM s_width, SCM s_height, SCM s_fullscreen) { Game *game = check_game(game_smob); int width = scm_to_int (s_width); int height = scm_to_int (s_height); bool fullscreen = scm_to_bool (s_fullscreen); /* Initialize Allegro things */ /* TODO: Handle these errors in a proper way */ if(!al_init ()) { fprintf (stderr, "failed to initialize allegro!\n"); exit (-1); } if (!al_init_image_addon ()) { fprintf (stderr, "failed to initialize image addon!\n"); exit (-1); } al_init_font_addon (); if(!al_init_ttf_addon ()) { fprintf (stderr, "failed to initialize ttf addon!\n"); exit (-1); } if(!al_install_audio ()) { fprintf (stderr, "failed to initialize audio addon!\n"); exit (-1); } if(!al_init_acodec_addon ()) { fprintf (stderr, "failed to initialize audio codecs addon!\n"); exit (-1); } if (!al_reserve_samples (16)) { fprintf (stderr, "failed to reserve samples!\n"); exit (-1); } if(!al_install_keyboard ()) { fprintf (stderr, "failed to initialize keyboard!\n"); exit (-1); } if (fullscreen) { al_set_new_display_flags (ALLEGRO_FULLSCREEN); } game->display = al_create_display (width, height); if (!game->display) { fprintf (stderr, "failed to create display!\n"); } game->timer = al_create_timer (game->timestep); game->event_queue = al_create_event_queue (); al_register_event_source (game->event_queue, al_get_display_event_source (game->display)); al_register_event_source (game->event_queue, al_get_timer_event_source (game->timer)); al_register_event_source (game->event_queue, al_get_keyboard_event_source ()); return SCM_UNSPECIFIED; }
void engine_run(struct State *s) { int redraw = FALSE; if (engine_active) { return; } change_state(s, NULL); // Generate display events al_register_event_source(engine.event_queue, al_get_display_event_source(engine.display)); // Timer events al_register_event_source(engine.event_queue, al_get_timer_event_source(engine.timer)); // Keyboard events al_register_event_source(engine.event_queue, al_get_keyboard_event_source()); // Mouse events al_register_event_source(engine.event_queue, al_get_mouse_event_source()); al_start_timer(engine.timer); engine_active = TRUE; // Main game loop while (engine_active) { ALLEGRO_EVENT event; al_wait_for_event(engine.event_queue, &event); // Event processing engine.states[current_state]->_events(&event, &engine.sm); // If the close button was pressed... if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { engine_active = FALSE; break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { keys[event.keyboard.keycode] = TRUE; // F4 key will toggle full-screen if (event.keyboard.keycode == ALLEGRO_KEY_F4) { al_stop_timer(engine.timer); if (al_get_display_flags(engine.display) & ALLEGRO_FULLSCREEN_WINDOW) { al_toggle_display_flag(engine.display, ALLEGRO_FULLSCREEN_WINDOW, 0); } else { al_toggle_display_flag(engine.display, ALLEGRO_FULLSCREEN_WINDOW, 1); } aspect_ratio_transform(); al_start_timer(engine.timer); } } else if (event.type == ALLEGRO_EVENT_KEY_UP) { keys[event.keyboard.keycode] = FALSE; } else if (event.type == ALLEGRO_EVENT_TIMER) { engine.states[current_state]->_update(&engine.sm); redraw = TRUE; } if (redraw && engine_active && al_is_event_queue_empty(engine.event_queue)) { redraw = FALSE; if (MAINCONF->buffer) { al_set_target_bitmap(engine.buffer); } else { al_set_target_backbuffer(engine.display); } al_clear_to_color(engine.bg_color); engine.states[current_state]->_draw(&engine.sm); if (MAINCONF->buffer) { al_set_target_backbuffer(engine.display); al_clear_to_color(C_BLACK); al_draw_bitmap(engine.buffer, 0, 0, 0); } al_flip_display(); } } while (loaded_count > 0) { engine.loaded_states[--loaded_count]->_free(); } al_destroy_display(engine.display); al_destroy_timer(engine.timer); al_destroy_event_queue(engine.event_queue); al_destroy_font(font); if (MAINCONF->buffer) { al_destroy_bitmap(engine.buffer); } }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *tapl = NULL; ALLEGRO_BITMAP *tapr = NULL; ALLEGRO_BITMAP *bounce = NULL; float tapl_x = 0.0; float tapl_y = SCREEN_H / 2.0 - TAP_HEIGHT / 2.0; float tapr_x = SCREEN_W - TAP_WIDTH - 1.0; float tapr_y = SCREEN_H / 2.0 - TAP_HEIGHT / 2.0; float bounce_x = SCREEN_W / 2.0 - BOUNCE_SIZE / 2.0; float bounce_y = SCREEN_H / 2.0 - BOUNCE_SIZE / 2.0; float bounce_dx = -4.0, bounce_dy = 4.0; bool key[6] = { false, false, false, false, false, false }; bool redraw = true; bool doexit = false; if(!al_init()) { //iniciando o allegro fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_install_keyboard()) { //instalando o teclado fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } timer = al_create_timer(1.0 / FPS); //criando temporizador (FPS) display = al_create_display(SCREEN_W, SCREEN_H); //criando o display tapl = al_create_bitmap(TAP_WIDTH, TAP_HEIGHT); //criando o rebatedor esquerdo (vermelho) tapr = al_create_bitmap(TAP_WIDTH, TAP_HEIGHT); //criando o rebatedor direito (azul) bounce = al_create_bitmap(BOUNCE_SIZE, BOUNCE_SIZE); //criando a bounce (branco) //setando cores dos bitmaps al_set_target_bitmap(tapl); //setando o target pro rebatedor esquerdo al_clear_to_color(al_map_rgb(255, 0, 0)); //setando a cor do target (rebatedor esquerdo) al_set_target_bitmap(tapr); //setando o target pro rebatedor direito al_clear_to_color(al_map_rgb(0, 0, 255)); //setando a cor do target (rebatedor direito) al_set_target_bitmap(bounce); //setando o target pra bounce al_clear_to_color(al_map_rgb(255, 255, 255)); //setando a cor do target al_set_target_bitmap(al_get_backbuffer(display)); //setando o target pro display event_queue = al_create_event_queue(); //iniciando o leitor de eventos al_register_event_source(event_queue, al_get_display_event_source(display)); //registrando os eventos do display al_register_event_source(event_queue, al_get_timer_event_source(timer)); //registrando os eventos do temporizador al_register_event_source(event_queue, al_get_keyboard_event_source()); //registrando os eventos do teclado al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(key[KEY_ENTER]) { redraw = true; } if(key[KEY_P]) { redraw = false; } if(ev.type == ALLEGRO_EVENT_TIMER) { if(redraw == true){ if( bounce_x - bounce_dx < tapl_x + (TAP_WIDTH + 14.0)&& bounce_x + bounce_dx > tapl_x - TAP_WIDTH && bounce_y - bounce_dy < tapl_y + TAP_HEIGHT && bounce_y + bounce_dy > tapl_y - TAP_HEIGHT ) { bounce_dx = bounce_dx + 1; } if( bounce_x - bounce_dx < tapr_x + (TAP_WIDTH + 0.0)&& bounce_x + bounce_dx > tapr_x - (TAP_WIDTH + 15.0)&& bounce_y - bounce_dy < tapr_y + TAP_HEIGHT && bounce_y + bounce_dy > tapr_y - TAP_HEIGHT ) { bounce_dx -= 11; } if(bounce_x < 0) { al_destroy_timer(timer); } if(bounce_x > SCREEN_W - BOUNCE_SIZE) { al_destroy_timer(timer); } if(bounce_y < 0 || bounce_y > SCREEN_H - BOUNCE_SIZE) { //aqui rebate normal no eixo da altura bounce_dy = -bounce_dy; } bounce_x += bounce_dx; bounce_y += bounce_dy; if(key[KEY_W] && tapl_y >= 4.0) { tapl_y -= 4.0; } if(key[KEY_S] && tapl_y <= SCREEN_H - TAP_HEIGHT - 4.0) { tapl_y += 4.0; } if(key[KEY_UP] && tapr_y >= 4.0) { tapr_y -= 4.0; } if(key[KEY_DOWN] && tapr_y <= SCREEN_H - TAP_HEIGHT - 4.0) { tapr_y += 4.0; } } } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_W: key[KEY_W] = true; break; case ALLEGRO_KEY_S: key[KEY_S] = true; break; case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_P: key[KEY_ENTER] = false; key[KEY_P] = true; break; case ALLEGRO_KEY_ENTER: key[KEY_P] = false; key[KEY_ENTER] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_W: key[KEY_W] = false; break; case ALLEGRO_KEY_S: key[KEY_S] = false; break; case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(tapl, tapl_x, tapl_y, 0); al_draw_bitmap(tapr, tapr_x, tapr_y, 0); al_draw_bitmap(bounce, bounce_x, bounce_y, 0); al_flip_display(); } } al_destroy_bitmap(tapl); al_destroy_bitmap(tapr); al_destroy_bitmap(bounce); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main(void) { bool done = false; int curFrame = 0; int frameCount = 0; int frameDelay = 5; const int maxFrame = 8; //Initialisers ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; ALLEGRO_BITMAP *image[maxFrame]; if (!al_init()) //initialize and check Allegro { fprintf(stderr, "failed to initialize allegro!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } display = al_create_display(scrn_W, scrn_H); //create our display object if (!display) //Check display { fprintf(stderr, "failed to create display!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); //create box limit if (!bouncer) { //Check creation of box limit fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } timer = al_create_timer(1.0 / FPS); //Create Timer if (!timer) //Check timer creation { fprintf(stderr, "failed to create timer!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } event_queue = al_create_event_queue(); //Create event queue if (!event_queue) { //Check event queue creation fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_init_primitives_addon(); al_install_keyboard(); al_install_mouse(); al_init_image_addon(); //Init bitmap image[0] = al_load_bitmap("./images/dragon/fliegt e0000.bmp"); image[1] = al_load_bitmap("./images/dragon/fliegt e0001.bmp"); image[2] = al_load_bitmap("./images/dragon/fliegt e0002.bmp"); image[3] = al_load_bitmap("./images/dragon/fliegt e0003.bmp"); image[4] = al_load_bitmap("./images/dragon/fliegt e0004.bmp"); image[5] = al_load_bitmap("./images/dragon/fliegt e0005.bmp"); image[6] = al_load_bitmap("./images/dragon/fliegt e0006.bmp"); image[7] = al_load_bitmap("./images/dragon/fliegt e0007.bmp"); for (int i = 0; i < maxFrame; i++) { al_convert_mask_to_alpha(image[i], al_map_rgb(106, 76, 48)); } //end bitmap //Event queue - register listeners al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_display_event_source(display)); //end event queue //Colours black = al_map_rgb(0, 0, 0); white = al_map_rgb(255, 255, 255); red = al_map_rgb(255, 0, 0); green = al_map_rgb(0, 255, 0); blue = al_map_rgb(0, 0, 255); //End Colours al_start_timer(timer); //End initialisers while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: break; case ALLEGRO_KEY_RIGHT: break; case ALLEGRO_KEY_UP: break; case ALLEGRO_KEY_DOWN: break; } } else if (ev.type == ALLEGRO_EVENT_TIMER) { if (++frameCount >= frameDelay) { if (++curFrame >= maxFrame) { curFrame = 0; } frameCount = 0; } } al_draw_bitmap(image[curFrame], 200, 100, 0); al_flip_display(); al_clear_to_color(black); } //Destruction for (int i = 0; i < maxFrame; i++) { al_destroy_bitmap(image[i]); } al_destroy_bitmap(bouncer); al_destroy_event_queue(event_queue); al_destroy_display(display); al_destroy_timer(timer); //end Distruction return 0; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; float angle = 0; float length = 0; float vel_x, vel_y; float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE_Y / 2.0; float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE_Y / 2.0; bool key[5] = { false, false, false, false, false }; bool redraw = true; bool doexit = false; if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } bouncer = al_create_bitmap(BOUNCER_SIZE_X,BOUNCER_SIZE_Y); if(!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(0, 0, 0)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { if(key[KEY_UP] && length < 15) { length += 1; } else if(length > 0) { length -= 0.1; } if(key[KEY_DOWN] && length > 0) { length -= 1; } if(key[KEY_LEFT]) { angle = (angle - 0.1); } if(key[KEY_RIGHT]) { angle = (angle + 0.1); } if(key[KEY_SPACE]) { } vel_x = length * cos(angle); vel_y = length * sin(angle); bouncer_x += vel_x; bouncer_y += vel_y; redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; case ALLEGRO_KEY_SPACE: key[KEY_SPACE] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; case ALLEGRO_KEY_SPACE: key[KEY_SPACE] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(240,240,240)); al_draw_rotated_bitmap(bouncer, BOUNCER_SIZE_X / 2, BOUNCER_SIZE_Y / 2, bouncer_x, bouncer_y, angle, angle); al_flip_display(); } } al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int init::init_all() { //this->display; srand (time(NULL)); if(!al_init()) { fprintf(stderr, "failed to initialize randomness!\n"); return -1; } if(!al_init_primitives_addon()) { fprintf(stderr, "failed to initialize primitives addon!\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize keyboard!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize mouse!\n"); return -1; } if(FULLSCREEN) { ALLEGRO_DISPLAY_MODE disp_data; al_get_display_mode(0, &disp_data); al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_OPENGL); fprintf(stderr,"%i %i",disp_data.width,disp_data.height); this->screenWidth = disp_data.width; this->screenHeight = disp_data.height; this->display = al_create_display(disp_data.width, disp_data.height); } else { this->display = al_create_display(screenWidth,screenHeight); } if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; } al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR); al_init_font_addon(); if(!al_init_ttf_addon()) { fprintf(stderr, "failed to initialize font addon!\n"); return -1; } this->timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } this->font = al_load_ttf_font("resources/pirulen.ttf",14,0); if (!font) { fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); return -1; } if (!al_init_image_addon()) { fprintf(stderr, "Could not initialize image addon.\n"); return -1; } this->event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); } al_register_event_source(this->event_queue, al_get_timer_event_source(this->timer)); al_register_event_source(this->event_queue, al_get_keyboard_event_source()); al_register_event_source(this->event_queue, al_get_display_event_source(this->display)); al_register_event_source(this->event_queue, al_get_mouse_event_source()); al_start_timer(timer); return 0; }
int main(int argc, char *argv[]) { if(!al_init()) { fprintf(stderr, "Failed to initialize allegro!\n"); return -1; } ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; bool redraw = true; timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create the timer.\n"); return -1; } display = al_create_display(1024, 768); if(!display) { fprintf(stderr, "Failed to create the display.\n"); al_destroy_timer(timer); return -1; } event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event queue.\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(true) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) redraw = true; else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { fprintf(stderr, "display closed\n"); break; } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(99, 99, 99)); al_flip_display(); } } al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main(int, char**) { // Setup Allegro al_init(); al_install_keyboard(); al_install_mouse(); al_init_primitives_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE); ALLEGRO_DISPLAY* display = al_create_display(1280, 720); al_set_window_title(display, "Dear ImGui Allegro 5 example"); ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); // Setup Dear ImGui binding IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls ImGui_ImplAllegro5_Init(display); // Setup style ImGui::StyleColorsDark(); //ImGui::StyleColorsClassic(); // Load Fonts // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. // - Read 'misc/fonts/README.txt' for more instructions and details. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! //io.Fonts->AddFontDefault(); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); //IM_ASSERT(font != NULL); bool show_demo_window = true; bool show_another_window = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); // Main loop bool running = true; while (running) { // Poll and handle events (inputs, window resize, etc.) // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. ALLEGRO_EVENT ev; while (al_get_next_event(queue, &ev)) { ImGui_ImplAllegro5_ProcessEvent(&ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) running = false; if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ImGui_ImplAllegro5_InvalidateDeviceObjects(); al_acknowledge_resize(display); ImGui_ImplAllegro5_CreateDeviceObjects(); } } // Start the Dear ImGui frame ImGui_ImplAllegro5_NewFrame(); ImGui::NewFrame(); // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. { static float f = 0.0f; static int counter = 0; ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state ImGui::Checkbox("Another Window", &show_another_window); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) counter++; ImGui::SameLine(); ImGui::Text("counter = %d", counter); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); ImGui::End(); } // 3. Show another simple window. if (show_another_window) { ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) ImGui::Text("Hello from another window!"); if (ImGui::Button("Close Me")) show_another_window = false; ImGui::End(); } // Rendering ImGui::Render(); al_clear_to_color(al_map_rgba_f(clear_color.x, clear_color.y, clear_color.z, clear_color.w)); ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData()); al_flip_display(); } // Cleanup ImGui_ImplAllegro5_Shutdown(); ImGui::DestroyContext(); al_destroy_event_queue(queue); al_destroy_display(display); return 0; }
int main(int argc, char *argv[]) { // -------- VARIÁVEIS DO JOGO -------- ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font20 = NULL; ALLEGRO_SAMPLE *ataque = NULL; ALLEGRO_SAMPLE *morte_inimigo = NULL; ALLEGRO_SAMPLE *morte_personagem = NULL; ALLEGRO_SAMPLE *hit = NULL; ALLEGRO_SAMPLE *item = NULL; ALLEGRO_AUDIO_STREAM *musica = NULL; ALLEGRO_AUDIO_STREAM *track_menu = NULL; ALLEGRO_BITMAP *start = NULL; bool fim = false; bool menu = true; int flag = 0; bool desenha = true; bool game_over = false; bool teclas[] = {false, false, false, false, false, false, false}; int i; int dificuldade; // ___________________________________ // -------- INICIALIZAÇÃO DE OBJETOS -------- dificuldade = 0; Personagem personagem_principal; Projetil* balas = new Projetil [NUM_BALAS]; Projetil* balas_2 = new Projetil [NUM_BALAS]; Inimigo* inimigos = new Inimigo[NUM_INIMIGOS]; Inimigo* inimigos2 = new Inimigo [NUM_INIMIGOS]; Coracao* coracoes = new Coracao[NUM_ITENS]; Speed* speed = new Speed[NUM_ITENS]; ItemPontos* pontos = new ItemPontos[NUM_ITENS]; Estrelas estrelas_pf[NUM_PLANOS][NUM_ESTRELAS]; Selecionar select; // __________________________________________ // -------- INICIALIZAÇÃO DA ALLEGRO E DO DISPLAY -------- ALLEGRO_DISPLAY *display = NULL; if (!al_init()) { al_show_native_message_box(NULL, "AVISO!", "ERRO!", "ERRO AO INICIALIZAR A ALLEGRO!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return -1; } display = al_create_display(LARGURA_T, ALTURA_T); if (!display) { al_show_native_message_box(NULL, "AVISO!", "ERRO!", "ERRO AO CRIAR O DISPLAY!", NULL, ALLEGRO_MESSAGEBOX_ERROR); return -1; } al_set_window_title(display, "Cosmos Guardian"); // ____________________________________________________ // -------- INICIALIZAÇÃO DE ADDONS E INSTALAÇÕES -------- al_init_primitives_addon(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(100); // _______________________________________________________ // -------- CRIAÇÃO DE FILAS E DEMAIS DISPOSITIVOS -------- fila_eventos = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); font20 = al_load_font("BADABB__.ttf", 20, 0); // ________________________________________________________ // -------- REGISTRO DE SOURCES -------- al_register_event_source(fila_eventos, al_get_display_event_source(display)); al_register_event_source(fila_eventos, al_get_keyboard_event_source()); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); // _____________________________________ // -------- FUNÇÕES INICIAIS -------- srand(time(NULL)); personagem_principal.InitPersonagem(); select.InitSelecionar(); //Inicialização de projeteis for (i = 0; i < NUM_BALAS; i++) { balas[i].InitBalas(); balas_2[i].InitBalas(); } //Inicialização de inimigos for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].InitInimigo(7, 46, 85, 1 , 0); inimigos2[i].InitInimigo(3, 55, 94, 3, 0); } //Inicialização de itens for (i = 0; i < NUM_ITENS; i++) { coracoes[i].InitItem(); speed[i].InitItem(); pontos[i].InitItem(); } //Inicialização do Plano de Fundo InitPlanoFundo(estrelas_pf, NUM_PLANOS, NUM_ESTRELAS); //Setando Sons e Imagens select.bmp = al_load_bitmap("select.png"); ataque = al_load_sample("laser.wav"); morte_inimigo = al_load_sample("dead.wav"); morte_personagem = al_load_sample("death.wav"); hit = al_load_sample("hit.wav"); item = al_load_sample("item.wav"); musica = al_load_audio_stream("trilha_sonora.ogg", 4, 1024); track_menu = al_load_audio_stream("menu.ogg", 4, 1024); personagem_principal.bmp = al_load_bitmap("ship.png"); for (i = 0; i < NUM_BALAS; i++) { balas[i].bmp = al_load_bitmap("bala.png"); balas_2[i].bmp = al_load_bitmap("bala.png"); } for (i = 0; i < NUM_INIMIGOS; i++){ inimigos[i].bmp = al_load_bitmap("enemyRed.png"); inimigos2[i].bmp = al_load_bitmap("enemyWhite.png"); } start = al_load_bitmap("start.jpg"); for (i = 0; i < NUM_ITENS; i++){ coracoes[i].imagem = al_load_bitmap("heart.png"); speed[i].imagem = al_load_bitmap("speed.png"); pontos[i].imagem = al_load_bitmap("pontos.png"); } // __________________________________ // ----------------- LOOP PRINCIPAL ----------------- al_start_timer(timer); while(!fim) { ALLEGRO_EVENT ev; al_wait_for_event(fila_eventos, &ev); // -------- EVENTOS E LÓGICA DO JOGO -------- if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { fim = true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: fim = true; break; case ALLEGRO_KEY_UP: teclas[CIMA] = true; break; case ALLEGRO_KEY_DOWN: teclas[BAIXO] = true; break; case ALLEGRO_KEY_LEFT: teclas[ESQUERDA] = true; break; case ALLEGRO_KEY_RIGHT: teclas[DIREITA] = true; break; case ALLEGRO_KEY_SPACE: teclas[ESPACO] = true; AtiraBalas(balas, NUM_BALAS, personagem_principal, personagem_principal.y + 12); AtiraBalas(balas_2, NUM_BALAS, personagem_principal, personagem_principal.y + 70); al_play_sample(ataque, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL); break; case ALLEGRO_KEY_ENTER: teclas[ENTER] = true; break; case ALLEGRO_KEY_BACKSPACE: teclas[BACKSPACE] = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: teclas[CIMA] = false; break; case ALLEGRO_KEY_DOWN: teclas[BAIXO] = false; break; case ALLEGRO_KEY_LEFT: teclas[ESQUERDA] = false; break; case ALLEGRO_KEY_RIGHT: teclas[DIREITA] = false; break; case ALLEGRO_KEY_ENTER: teclas[ENTER] = false; break; case ALLEGRO_KEY_BACKSPACE: teclas[BACKSPACE] = false; break; } } else if (ev.type == ALLEGRO_EVENT_TIMER) { desenha = true; if (teclas[CIMA]) personagem_principal.MoveCima(); if (teclas[BAIXO]) personagem_principal.MoveBaixo(ALTURA_T); if (teclas[ESQUERDA]) personagem_principal.MoveEsquerda(); if (teclas[DIREITA]) personagem_principal.MoveDireita(LARGURA_T); if (teclas[ESPACO]) { for (i = 0; i < NUM_BALAS; i++) { balas[i].AtualizaBalas(0); balas_2[i].AtualizaBalas(0); } } // Movimentação no menu if(menu) { al_attach_audio_stream_to_mixer(track_menu, al_get_default_mixer()); al_set_audio_stream_playing(track_menu, true); if (teclas[CIMA] && select.y!=235) select.y -= 70; if (teclas[BAIXO] && select.y!=305) select.y += 70; if (teclas[ENTER] && select.y==235) { menu = false; al_set_audio_stream_playing(track_menu, false); } } // Acontecimentos do Jogo if(!game_over && !menu) { al_attach_audio_stream_to_mixer(musica, al_get_default_mixer()); al_set_audio_stream_playing(musica, true); AtualizaPlanoFundo(estrelas_pf, NUM_PLANOS, NUM_ESTRELAS); { //Gera e atualiza inimigos for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].GeraInimigos(); inimigos2[i].GeraInimigos(); inimigos[i].AtualizaInimigos(); inimigos[i].InimigoColidido(personagem_principal, hit); inimigos2[i].AtualizaInimigos(); inimigos2[i].InimigoColidido(personagem_principal, hit); } } //Checa colisões de projeteis BalaColidida(balas, NUM_BALAS, inimigos, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); BalaColidida(balas_2, NUM_BALAS, inimigos, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); BalaColidida(balas, NUM_BALAS, inimigos2, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); BalaColidida(balas_2, NUM_BALAS, inimigos2, NUM_INIMIGOS, personagem_principal, dificuldade, morte_inimigo); // Faz os testes relacionado aos itens for (i = 0; i < NUM_ITENS; i++) { coracoes[i].GeraItens(inimigos[i]); coracoes[i].AtualizaItens(); coracoes[i].ItemColidido(personagem_principal, item); speed[i].GeraItens(inimigos[i]); speed[i].AtualizaItens(); speed[i].ItemColidido(personagem_principal, item); pontos[i].GeraItens(inimigos[i]); pontos[i].AtualizaItens(); pontos[i].ItemColidido(personagem_principal, item); } if ((dificuldade+1)%16 == 0) // Dificuldade aumenta a cada 15 pontos. { for (i = 0; i < NUM_INIMIGOS; i++){ inimigos[i].velocidade++; inimigos2[i].velocidade++; } dificuldade = 0; } if (personagem_principal.vidas <= 0) { al_play_sample(morte_personagem, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL); game_over = true; } } // Reinicializa o jogo else if (!menu) { al_set_audio_stream_playing(musica, false); if (teclas[ENTER]) { al_destroy_audio_stream(musica); personagem_principal.InitPersonagem(); for (i = 0; i < NUM_BALAS; i++) { balas[i].InitBalas(); balas_2[i].InitBalas(); } for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].InitInimigo(7, 46, 85, 1 , 0); inimigos2[i].InitInimigo(3, 55, 94, 3, 0); } for (i = 0; i < NUM_ITENS; i++) { coracoes[i].InitItem(); speed[i].InitItem(); pontos[i].InitItem(); } personagem_principal.bmp = al_load_bitmap("ship.png"); musica = al_load_audio_stream("trilha_sonora.ogg", 4, 1024); for (i = 0; i < NUM_BALAS; i++) { balas[i].bmp = al_load_bitmap("bala.png"); balas_2[i].bmp = al_load_bitmap("bala.png"); } for (i = 0; i < NUM_INIMIGOS; i++) { inimigos[i].bmp = al_load_bitmap("enemyRed.png"); inimigos2[i].bmp = al_load_bitmap("enemyWhite.png"); coracoes[i].imagem = al_load_bitmap("heart.png"); speed[i].imagem = al_load_bitmap("heart.png"); } game_over = false; } } } // _________________________________________ // ---------------- DESENHO ---------------- if(desenha && al_is_event_queue_empty(fila_eventos)) { desenha = false; // Desenhos da Tela do Menu if(menu) { al_draw_bitmap(start, 0, 0, 0); if (select.ativo) al_draw_bitmap(select.bmp, select.x, select.y, 0); if (teclas[ENTER] && flag == 0) { flag = 1; teclas[BACKSPACE] = false; select.ativo = false; start = al_load_bitmap("como_jogar.jpg"); al_draw_bitmap(start, 0, 0, 0); } else if (teclas[BACKSPACE] && flag == 1) { flag = 0; select.ativo = true; start = al_load_bitmap("start.jpg"); al_draw_bitmap(start, 0, 0, 0); } } // Jogo normal, desenho de todos os objetos if(!game_over && !menu) { DesenhaPlanoFundo(estrelas_pf, NUM_PLANOS, NUM_ESTRELAS); personagem_principal.DesenhaPersonagem(); for (i = 0; i < NUM_BALAS; i++) { balas[i].DesenhaBalas(); balas_2[i].DesenhaBalas(); } for (i = 0; i < NUM_INIMIGOS; i++){ inimigos[i].DesenhaInimigos(); inimigos2[i].DesenhaInimigos(); } for (i = 0; i < NUM_ITENS; i++) { coracoes[i].DesenhaItens(); speed[i].DesenhaItens(); pontos[i].DesenhaItens(); } al_draw_textf(font20, al_map_rgb(255, 255, 255), 0, 0, 0, "VIDAS: %d / PONTOS: %d", personagem_principal.vidas, personagem_principal.pontos); } //Tela de fim de jogo else if (!menu) { al_draw_textf(font20, al_map_rgb(255, 255, 255), LARGURA_T / 2, ALTURA_T / 2, ALLEGRO_ALIGN_CENTRE, "FIM DE JOGO. SEUS PONTOS FORAM: %d. TECLE ENTER PARA JOGAR NOVAMENTE OU ESC PARA SAIR DO JOGO.", personagem_principal.pontos); } al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } // _________________________________________ } // _________________________________________________ // -------- FINALIZAÇÕES DO PROGRAMA -------- delete[] inimigos; delete[] inimigos2; delete[] balas; delete[] balas_2; delete[] coracoes; delete[] speed; delete[] pontos; al_destroy_bitmap(start); al_destroy_sample(ataque); al_destroy_sample(morte_inimigo); al_destroy_sample(morte_personagem); al_destroy_sample(hit); al_destroy_sample(item); al_destroy_audio_stream(musica); al_destroy_audio_stream(track_menu); al_destroy_display(display); al_destroy_timer(timer); al_destroy_font(font20); al_destroy_event_queue(fila_eventos); //___________________________________________ return 0; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *bmp; ALLEGRO_FONT *f; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; bool redraw; int min_w, min_h, max_w, max_h; int ret_min_w, ret_min_h, ret_max_w, ret_max_h; bool constr_min_w, constr_min_h, constr_max_w, constr_max_h; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_set_new_display_flags(ALLEGRO_RESIZABLE | ALLEGRO_GENERATE_EXPOSE_EVENTS); display = al_create_display(640, 480); if (!display) { abort_example("Unable to set any graphic mode\n"); } bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Unable to load image\n"); } f = al_load_font("data/a4_font.tga", 0, 0); if (!f) { abort_example("Failed to load a4_font.tga\n"); } min_w = 640; min_h = 480; max_w = 800; max_h = 600; constr_min_w = constr_min_h = constr_max_w = constr_max_h = true; if (!al_set_window_constraints( display, constr_min_w ? min_w : 0, constr_min_h ? min_h : 0, constr_max_w ? max_w : 0, constr_max_h ? max_h : 0)) { abort_example("Unable to set window constraints.\n"); } al_apply_window_constraints(display, true); queue = al_create_event_queue(); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); redraw = true; while (true) { if (redraw && al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(255, 0, 0)); al_draw_scaled_bitmap(bmp, 0, 0, al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), 0, 0, al_get_display_width(display), al_get_display_height(display), 0); /* Display screen resolution */ al_draw_textf(f, al_map_rgb(255, 255, 255), 0, 0, 0, "Resolution: %dx%d", al_get_display_width(display), al_get_display_height(display)); if (!al_get_window_constraints(display, &ret_min_w, &ret_min_h, &ret_max_w, &ret_max_h)) { abort_example("Unable to get window constraints\n"); } al_draw_textf(f, al_map_rgb(255, 255, 255), 0, al_get_font_line_height(f), 0, "Min Width: %d, Min Height: %d, Max Width: %d, Max Height: %d", ret_min_w, ret_min_h, ret_max_w, ret_max_h); al_draw_textf(f, al_map_rgb(255, 255, 255), 0, al_get_font_line_height(f) * 2,0, "Toggle Restriction: Min Width: Z, Min Height: X, Max Width: C, Max Height: V"); al_flip_display(); redraw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(event.display.source); redraw = true; } if (event.type == ALLEGRO_EVENT_DISPLAY_EXPOSE) { redraw = true; } if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.keyboard.keycode == ALLEGRO_KEY_Z) { constr_min_w = ! constr_min_w; } else if (event.keyboard.keycode == ALLEGRO_KEY_X) { constr_min_h = ! constr_min_h; } else if (event.keyboard.keycode == ALLEGRO_KEY_C) { constr_max_w = ! constr_max_w; } else if (event.keyboard.keycode == ALLEGRO_KEY_V) { constr_max_h = ! constr_max_h; } redraw = true; if (!al_set_window_constraints(display, constr_min_w ? min_w : 0, constr_min_h ? min_h : 0, constr_max_w ? max_w : 0, constr_max_h ? max_h : 0)) { abort_example("Unable to set window constraints.\n"); } al_apply_window_constraints(display, true); } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } } al_destroy_bitmap(bmp); al_destroy_display(display); return 0; }
int main(){ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; bool redraw = true; if(!al_init()){ fprintf(stderr, "fallo para iniciar allegro\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "Fallo al iniciar el timer\n"); return -1; } display = al_create_display(640, 480); if(!display) { fprintf(stderr, "Fallo al crear el display\n"); al_destroy_timer(timer); return -1; } event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "fallo al crear la cola de eventos\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_clear_to_color(al_map_rgb(0,120,120)); al_flip_display(); al_start_timer(timer); while(1){ ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER){ redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if(redraw && al_is_event_queue_empty(event_queue)){ redraw = false; al_clear_to_color(al_map_rgb(0,120,120)); al_flip_display(); } } al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return EXIT_SUCCESS; }
int inicializadores(int *LARGURA_TELA, int *ALTURA_TELA, float *FPS, ALLEGRO_DISPLAY *janela , ALLEGRO_EVENT_QUEUE *fila_eventos, ALLEGRO_BITMAP *poderes, ALLEGRO_FONT *fonte_equacao, ALLEGRO_FONT *fonte_pontos, ALLEGRO_TIMER *timer){ if (!al_init()){ fprintf(stderr, "Falha ao inicializar a Allegro.\n"); return -1; } al_init_image_addon(); // Inicialização do add-on para uso de fontes al_init_font_addon(); // Inicialização do add-on para uso de fontes True Type if (!al_init_ttf_addon()){ fprintf(stderr, "Falha ao inicializar add-on allegro_ttf.\n"); return -1; } janela = al_create_display(LARGURA_TELA, ALTURA_TELA); if (!janela){ fprintf(stderr, "Falha ao criar janela.\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer){ fprintf(stderr, "failed to create timer!\n"); return -1; } // Configura o título da janela al_set_window_title(janela, "PENAS - Telas de Poderes -"); fonte_equacao = al_load_font("fontes/letra_equacao.ttf", 36, 0); if (!fonte_equacao){ al_destroy_display(janela); fprintf(stderr, "Falha ao carregar fonte.\n"); return -1; } fonte_pontos = al_load_font("fontes/letra_equacao.ttf", 26, 0); if (!fonte_pontos){ al_destroy_display(janela); fprintf(stderr, "Falha ao carregar fonte.\n"); return -1; } // Torna apto o uso de mouse na aplicação if (!al_install_mouse()){ fprintf(stderr, "Falha ao inicializar o mouse.\n"); al_destroy_display(janela); return -1; } // Atribui o cursor padrão do sistema para ser usado if (!al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT)){ fprintf(stderr, "Falha ao atribuir ponteiro do mouse.\n"); al_destroy_display(janela); return -1; } fila_eventos = al_create_event_queue(); if (!fila_eventos){ fprintf(stderr, "Falha ao inicializar o fila de eventos.\n"); al_destroy_display(janela); return -1; } if (!al_init_primitives_addon()){ fprintf(stderr, "Falha ao inicializar add-on de primitivas.\n"); return false; } // Dizemos que vamos tratar os eventos vindos do mouse al_register_event_source(fila_eventos, al_get_mouse_event_source()); al_register_event_source(fila_eventos, al_get_display_event_source(janela)); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); }
int main() { // don't forget to put allegro-5.0.10-monolith-md-debug.lib const float FPS = 60.0f; ALLEGRO_DISPLAY *display; if(!al_init()) { al_show_native_message_box(NULL,"Error","Error", "Cannot initialize Allegro", NULL, NULL); return -1; } display = al_create_display(ScreenWidth, ScreenHeight); if(!display) { al_show_native_message_box(NULL,"Error","Error", "Cannot create dsiplay", NULL, NULL); return -1; } al_set_window_position(display, 100, 100); al_install_keyboard(); al_install_mouse(); al_init_image_addon(); //al_init_acodec_addon(); al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_TIMER *timer = al_create_timer(1.0f / FPS); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); ALLEGRO_KEYBOARD_STATE keyState; 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)); bool done = false; InputManager input; ScreenManager::GetInstance().Initialize(); ScreenManager::GetInstance().LoadContent(); std::vector<int> keys; //these two below plus the IsKeyReleased are example how to use simultaneous keys keys.push_back(ALLEGRO_KEY_DOWN); keys.push_back(ALLEGRO_KEY_ESCAPE); float fade = 0.0f; al_start_timer(timer); while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); al_get_keyboard_state(&keyState); if(input.IsKeyReleased(ev,keys)) done=true; //closes the game //barnhen to check begin if(input.IsKeyPressed(ev, ALLEGRO_KEY_RIGHT)) fade++; else if(input.IsKeyPressed(ev, ALLEGRO_KEY_LEFT)) fade--; //barnhen to check end ScreenManager::GetInstance().Update(ev); ScreenManager::GetInstance().Draw(display); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } ScreenManager::GetInstance().UnloadContent(); //Destroyers al_destroy_display(display); al_destroy_timer(timer); al_destroy_event_queue(event_queue); //std::cin.get(); return 0; }
int main() { //shell vars bool render = false; //allegro vars ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //allegro init functions printf ("Initializing allegro\n"); if (!al_init()) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Creating display\n"); display = al_create_display(WIDTH, HEIGHT); if (!display) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Installing addons\n"); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); al_init_image_addon(); al_install_keyboard(); al_install_mouse(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(10); //project inits srand(time(NULL)); printf("Initializing timer\n"); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); printf("Registering event sources\n"); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); printf("Init mouse and keyboard\n"); init_keyboard(); init_mouse(); printf("Loading assets\n"); load_bitmaps(); load_fonts(); load_samples(); printf ("Creating manager\n"); push_state(new TitleMenu()); printf("Beginning game\n"); while (!is_game_over()) { //declare an event ALLEGRO_EVENT event; //monitor event sources al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { end_game(); } else if (event.type == ALLEGRO_EVENT_TIMER) { render = true; update_mouse(); update_keyboard(); handle_key(); update_game(); } // Render screen if (render && al_event_queue_is_empty(event_queue)) { render = false; render_game(); al_flip_display(); } } unload_assets(); al_destroy_event_queue(event_queue); al_destroy_display(display); al_destroy_timer(timer); return 0; }
int main(int argc, char** argv) { init(); ALLEGRO_TIMER* timer; ALLEGRO_EVENT_QUEUE* queue; ALLEGRO_DISPLAY* display; bool redraw = true; display = al_create_display(sysConfig::width, sysConfig::height); al_set_window_title(display, "test"); fps f; timer = al_create_timer(1.0/60); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); ResourceManager::Instance(); #if _DEBUG_ gameManager g; /*xiayuInt t; stringManager s1; s1.init(); xiayuWString str(L"a.b"); string str1 = str.ToString(); ResourceManager* r = ResourceManager::Instance(); script s = Parse::read_next_script(); while (1) { s = Parse::read_next_script(); }*/ #endif while (true) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_TIMER) redraw = true; if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) { keyManager::keyDown[event.keyboard.keycode] = true; } if (event.type == ALLEGRO_EVENT_KEY_UP && event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) { keyManager::keyDown[event.keyboard.keycode] = false; keyManager::keyUP[event.keyboard.keycode] = true; keyManager::press = true; } if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { keyManager::keyDown[ALLEGRO_KEY_ESCAPE] = !keyManager::keyDown[ALLEGRO_KEY_ESCAPE]; } if (event.type == ALLEGRO_EVENT_MOUSE_AXES) { keyManager::mouseX = event.mouse.x; keyManager::mouseY = event.mouse.y; } if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { if (event.mouse.button == 1) { keyManager::mouseLeftDown = true; }else if (event.mouse.button = 3) { keyManager::mouseRightDown = true; } } if (redraw && al_is_event_queue_empty(queue)) { redraw = false; f.updata(); g.update(); al_clear_to_color(al_map_rgb_f(1, 0, 0)); f.draw(); g.draw(); al_flip_display(); } } }
int main(void) { ALLEGRO_DISPLAY *display[2]; ALLEGRO_EVENT event; ALLEGRO_EVENT_QUEUE *events; ALLEGRO_BITMAP *pictures[2]; ALLEGRO_BITMAP *target; int width, height; int i; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_keyboard(); al_install_mouse(); al_init_image_addon(); events = al_create_event_queue(); al_set_new_display_flags(ALLEGRO_WINDOWED|ALLEGRO_RESIZABLE); /* Create two windows. */ display[0] = al_create_display(W, H); pictures[0] = al_load_bitmap("data/mysha.pcx"); if (!pictures[0]) { abort_example("failed to load mysha.pcx\n"); return 1; } display[1] = al_create_display(W, H); pictures[1] = al_load_bitmap("data/allegro.pcx"); if (!pictures[1]) { abort_example("failed to load allegro.pcx\n"); return 1; } /* This is only needed since we want to receive resize events. */ al_register_event_source(events, al_get_display_event_source(display[0])); al_register_event_source(events, al_get_display_event_source(display[1])); al_register_event_source(events, al_get_keyboard_event_source()); while (1) { /* read input */ while (!al_is_event_queue_empty(events)) { al_get_next_event(events, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN) { ALLEGRO_KEYBOARD_EVENT *key = &event.keyboard; if (key->keycode == ALLEGRO_KEY_ESCAPE) { goto done; } } if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ALLEGRO_DISPLAY_EVENT *de = &event.display; al_acknowledge_resize(de->source); } if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN) { printf("%p switching in\n", event.display.source); } if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) { printf("%p switching out\n", event.display.source); } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { int i; for (i = 0; i < 2; i++) { if (display[i] == event.display.source) display[i] = 0; } al_destroy_display(event.display.source); for (i = 0; i < 2; i++) { if (display[i]) goto not_done; } goto done; not_done: ; } } for (i = 0; i < 2; i++) { if (!display[i]) continue; target = al_get_backbuffer(display[i]); width = al_get_bitmap_width(target); height = al_get_bitmap_height(target); al_set_target_bitmap(target); al_draw_scaled_bitmap(pictures[0], 0, 0, al_get_bitmap_width(pictures[0]), al_get_bitmap_height(pictures[0]), 0, 0, width / 2, height, 0); al_draw_scaled_bitmap(pictures[1], 0, 0, al_get_bitmap_width(pictures[1]), al_get_bitmap_height(pictures[1]), width / 2, 0, width / 2, height, 0); al_flip_display(); } al_rest(0.001); } done: al_destroy_bitmap(pictures[0]); al_destroy_bitmap(pictures[1]); return 0; }
int main(void) { int vMapa = 0; int vMatriz_Mapa[21][27],i=0,j=0; FILE *fMapa = fopen("dormitorio_masculino.txt", "r"); // Carrega o arquivo de texto da fase em questão; char vLe_Char; while((vLe_Char = getc(fMapa) ) != EOF ) // Grava Caracter enquanto não chegar ao final do arquivo; { if ( i < 27 ) // Enquanto estivar na linha; { vMatriz_Mapa[j][i] = atoi(&vLe_Char); // Carrega a matriz com os caracteres que representam as imagens; i++; } else // senao passa para a proxima linha; { j++; i=0; } } fclose(fMapa); for (j=0;j<19;j++){ for (i=0;i<27;i++){ // if(vMatriz_Mapa[j/32][i/32]==2){ printf("%d",vMatriz_Mapa[j][i]); } printf("\n"); } bool sair = false; int tecla = 0,top = 0, left = 0, right = 0, down=0; int x=192,y=64; if (!al_init()) { fprintf(stderr, "Falha ao inicializar a Allegro.\n"); return false; } al_init_font_addon(); if (!al_init_ttf_addon()) { fprintf(stderr, "Falha ao inicializar add-on allegro_ttf.\n"); return false; } if (!al_init_image_addon()) { fprintf(stderr, "Falha ao inicializar add-on allegro_image.\n"); return false; } if (!al_install_keyboard()) { fprintf(stderr, "Falha ao inicializar o teclado.\n"); return false; } janela = al_create_display(LARGURA_TELA, ALTURA_TELA); if (!janela) { fprintf(stderr, "Falha ao criar janela.\n"); return false; } al_set_window_title(janela, "Utilizando o Teclado"); fila_eventos = al_create_event_queue(); if (!fila_eventos) { fprintf(stderr, "Falha ao criar fila de eventos.\n"); al_destroy_display(janela); return false; } fundo = al_load_bitmap("dormitorio_masculino.bmp"); if (!fundo) { fprintf(stderr, "Falha ao carregar imagem de fundo.\n"); al_destroy_display(janela); al_destroy_event_queue(fila_eventos); return false; } timer = al_create_timer(6/60.0); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); al_register_event_source(fila_eventos, al_get_keyboard_event_source()); al_register_event_source(fila_eventos, al_get_display_event_source(janela)); al_draw_bitmap(fundo, 0, 0, 0); ALLEGRO_BITMAP *peterSET = al_load_bitmap("sabrina3.bmp"); al_convert_mask_to_alpha(peterSET,al_map_rgb(255,0,255)); ALLEGRO_BITMAP *peterCHAR[12]; peterCHAR[0] = al_create_bitmap(32,32); peterCHAR[0] = al_create_sub_bitmap(peterSET, 0, 0, 32, 32); peterCHAR[1] = al_create_bitmap(32,32); peterCHAR[1] = al_create_sub_bitmap(peterSET, 32, 0, 32, 32); peterCHAR[2] = al_create_bitmap(32,32); peterCHAR[2] = al_create_sub_bitmap(peterSET, 64, 0, 32, 32); peterCHAR[3] = al_create_bitmap(32,32); peterCHAR[3] = al_create_sub_bitmap(peterSET, 0, 32, 32, 32); peterCHAR[4] = al_create_bitmap(32,32); peterCHAR[4] = al_create_sub_bitmap(peterSET, 32, 32, 32, 32); peterCHAR[5] = al_create_bitmap(32,32); peterCHAR[5] = al_create_sub_bitmap(peterSET, 64, 32, 32, 32); peterCHAR[6] = al_create_bitmap(32,32); peterCHAR[6] = al_create_sub_bitmap(peterSET, 0, 64, 32, 32); peterCHAR[7] = al_create_bitmap(32,32); peterCHAR[7] = al_create_sub_bitmap(peterSET, 32, 64, 32, 32); peterCHAR[8] = al_create_bitmap(32,32); peterCHAR[8] = al_create_sub_bitmap(peterSET, 64, 64, 32, 32); peterCHAR[9] = al_create_bitmap(32,32); peterCHAR[9] = al_create_sub_bitmap(peterSET, 0, 96, 32, 32); peterCHAR[10] = al_create_bitmap(32,32); peterCHAR[10] = al_create_sub_bitmap(peterSET, 32, 96, 32, 32); peterCHAR[11] = al_create_bitmap(32,32); peterCHAR[11] = al_create_sub_bitmap(peterSET, 64, 96, 32, 32); al_draw_bitmap(fundo, 0, 0, 0); al_draw_bitmap(peterCHAR[10],x,y,0); al_flip_display(); al_start_timer(timer); while (!sair) { while(!al_is_event_queue_empty(fila_eventos)) { ALLEGRO_EVENT evento; al_wait_for_event(fila_eventos, &evento); if (evento.type == ALLEGRO_EVENT_KEY_DOWN) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: sair = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SELECT]=true; break; } }else if (evento.type == ALLEGRO_EVENT_KEY_UP) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SELECT]=false; break; } } else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { sair = true; } else if (evento.type == ALLEGRO_EVENT_TIMER){ if(keys[SELECT]){ if(vMatriz_Mapa[y/32][(x+32)/32]==4){ return 0; } } if (keys[UP]){ if(vMatriz_Mapa[(y-32)/32][x/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(top == 0) { y=y-32; al_draw_bitmap(peterCHAR[10],x,y,0); top = 1; } else if(top == 1) { y=y-32; al_draw_bitmap(peterCHAR[9],x,y,0); top=2; } else if (top == 2) { y=y-32; al_draw_bitmap(peterCHAR[11],x,y,0); top =0; } printf("x = %d e y = %d\n",x,y); al_flip_display(); } } if (keys[DOWN]){ if(vMatriz_Mapa[(y+32)/32][x/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(down == 0) { y=y+32; al_draw_bitmap(peterCHAR[1],x,y,0); down = 1; } else if(down == 1) { y=y+32; al_draw_bitmap(peterCHAR[0],x,y,0); down = 2; } else if (down == 2) { y=y+32; al_draw_bitmap(peterCHAR[2],x,y,0); down = 0; } } } printf("x = %d e y = %d\n",x,y); al_flip_display(); } if (keys[LEFT]){ if(vMatriz_Mapa[y/32][(x-32)/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(left == 0) { x=x-32; al_draw_bitmap(peterCHAR[4],x,y,0); left = 1; } else if(left == 1) { x=x-32; al_draw_bitmap(peterCHAR[3],x,y,0); left=2; } else if (left == 2) { x=x-32; al_draw_bitmap(peterCHAR[5],x,y,0); left =0; } al_flip_display(); printf("x = %d e y = %d\n",x,y); } } if (keys[RIGHT]){ if(vMatriz_Mapa[y/32][(x+32)/32]==0){ al_draw_bitmap(fundo, 0, 0, 0); if(right == 0) { x=x+32; al_draw_bitmap(peterCHAR[7],x,y,0); right = 1; } else if(right == 1) { x=x+32; al_draw_bitmap(peterCHAR[6],x,y,0); right=2; } else if (right == 2) { x=x+32; al_draw_bitmap(peterCHAR[8],x,y,0); right =0; } printf("x = %d e y = %d\n",x,y); al_flip_display(); } } } } al_destroy_display(janela); al_destroy_event_queue(fila_eventos); return 0; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *buffer = NULL; bool key[5] = { false, false, false, false, false }; bool redraw = true; bool doexit = false; if (!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if (!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } if (!al_init_primitives_addon()) { fprintf(stderr, "failed to initialize the primitives library!\n"); } timer = al_create_timer(1.0 / FPS); if (!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if (!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } al_init_image_addon(); // Next, initiale the buffer buffer = al_create_bitmap(SCREEN_W, SCREEN_H); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if (!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); // al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); // This is our rootnode. const struct rectangle dimensions( { 0.0f, 0.0, 2048.0f, 2048.0f }); World rootNode(dimensions); Player player; Image background("http://force.mjay.me/farthestbackground.png", 10.0f); Image aNotherButAbitCloser("http://force.mjay.me/alittbitcloser.png", 1.0f); InpenetrableBlock p(point( { 200.0f, 200.0f })); InpenetrableBlock p1(point( { 250.0f, 300.0f })); InpenetrableBlock p2(point( { 350.0f, 150.0f })); rootNode.AddNode(background); background.AddNode(aNotherButAbitCloser); aNotherButAbitCloser.AddNode(p); p.AddNode(p1); p1.AddNode(p2); al_flip_display(); al_start_timer(timer); while (!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); /** * The allegro event timer. This timer, * fires each 1/fps. */ if (ev.type == ALLEGRO_EVENT_TIMER) { // This is our force window. struct point vector( { 0.0f, 0.0f }); float multiplier = 1.0f; if (key[KEY_SHIFT]) { multiplier = 2.0f; } if (key[KEY_UP]) { vector.y = -4.5f * multiplier; } if (key[KEY_DOWN]) { vector.y = 4.5f * multiplier; } if (key[KEY_LEFT]) { vector.x = -4.5f * multiplier; } if (key[KEY_RIGHT]) { vector.x = 4.5f * multiplier; } // do not copy this, reference it, RAII. rootNode.Process(vector, player); // Once, all forces have been calculated, do the // actual move player.Update(); redraw = true; } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; // Theese guys, transfer events into main event loop, async. } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; case ALLEGRO_KEY_LSHIFT: key[KEY_SHIFT] = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; case ALLEGRO_KEY_LSHIFT: key[KEY_SHIFT] = false; break; } } if (redraw && al_is_event_queue_empty(event_queue)) { redraw = false; // Allways, use the buffer, to draw on. Clear it, and draw it scaled onto the display. al_set_target_bitmap(buffer); al_clear_to_color(al_map_rgb(0, 0, 0)); // This is our sliding window. const struct rectangle playerCoordinates = player.GetDimensions(); struct rectangle window( { playerCoordinates.x, playerCoordinates.y, SCREEN_W, SCREEN_H }); // Draw all scene-components, according to the window. rootNode.Draw(window); struct rectangle pcwindow( { playerCoordinates.x - SCREEN_W / 2, playerCoordinates.y - SCREEN_H / 2, SCREEN_W, SCREEN_H }); player.Draw(pcwindow); // Next, draw the buffer onto the display (scaling or not). al_set_target_bitmap(al_get_backbuffer(display)); al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(buffer, 0, 0, 0); //al_draw_scaled_bitmap(buffer, 0,0, SCREEN_W, SCREEN_H, 1.0, 1.0, 1.0, 1.0, 0); al_flip_display(); } } //al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main(int argc, char const *argv[]) { int i = 0; int t = 1; int n_mostros = 5; bool click = false; bool nova_horda = true; bool render = false; bool torre_mouse = false; char a; char b; int r; int l; Sistema sistema; Torre torre[10]; Tiro tiro[10]; Monstro monstro[n_mostros]; Coord coordenada[A*B]; //Declara�ao vair�veis allegro ALLEGRO_DISPLAY *janela = NULL; //Vari�vel para a janela ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; // '' para eventos ALLEGRO_BITMAP *imagem = NULL; // '' para imagem ALLEGRO_TIMER *timer = NULL; // '' para o tempo (fps) ALLEGRO_FONT *fonte = NULL; // '' para fonte //Inicializa o allegro, mouse e add-ons al_init(); al_install_mouse(); al_init_primitives_addon(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); a_coord(coordenada, fonte); init_horda(monstro, n_mostros); init_system(sistema); initTorre(torre, tiro, t-1); //Atribui atributos às variáveis allegro janela = al_create_display(LARGURA_TELA, ALTURA_TELA); fila_eventos = al_create_event_queue(); imagem = al_load_bitmap("virus.png"); timer = al_create_timer(1.0 / fps); fonte = al_load_font("arial.ttf", 12, 0); //Fonte DejaVu //Inicializa o mouse e tempo al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT); al_start_timer(timer); al_install_keyboard(); init_fail(janela, fonte, fila_eventos, imagem, timer); //Fun�ao de teste //Regista os eventos da janela, mouse e timer na vari�vel de eventos (fila_eventos) al_register_event_source(fila_eventos, al_get_display_event_source(janela)); al_register_event_source(fila_eventos, al_get_mouse_event_source()); al_register_event_source(fila_eventos, al_get_keyboard_event_source()); al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); al_clear_to_color(al_map_rgb(235, 235, 235)); //Limpa a tela al_flip_display(); //Atualiza a tela //Loop principal while (!GameOver) { ALLEGRO_EVENT evento; //Variavel para eventos al_wait_for_event(fila_eventos, &evento); if(evento.type == ALLEGRO_EVENT_TIMER) //Evento de renderiza�ao { i++; render = true; FireTiro(tiro, torre, monstro, t-1); UpdateTiro(tiro, monstro, t-1); update_horda(monstro, mapa, n_mostros); colisao_horda(tiro, monstro, n_mostros); } if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { GameOver = true; } else if(evento.type == ALLEGRO_EVENT_MOUSE_AXES) { pos_x = evento.mouse.x; pos_y = evento.mouse.y; a = coordenada[(pos_x/l_celula)].letra[0]; b = coordenada[(pos_x/l_celula)].letra[1]; l = coordenada[(pos_y/a_celula)].numero; r = conversao_coordenadas(coordenada, a, b); } else if(evento.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { if (mapa[pos_y/a_celula][pos_x/l_celula] == 9) { torre_mouse = true; torre[t].in_mouse = true; } if(torre_mouse && !click) { if (mapa[pos_y/a_celula][pos_x/l_celula] != 9) { mapa[pos_y/a_celula][pos_x/l_celula] = 10; torre_mouse = false; torre[t].in_mouse = false; t++; } } click = !click; } else if(evento.type == ALLEGRO_EVENT_KEY_DOWN) { switch(evento.keyboard.keycode) { case ALLEGRO_KEY_SPACE: start_horda(monstro, n_mostros); break; } } else if(render && al_is_event_queue_empty(fila_eventos)) { render = false; coor_matrix(mapa, coordenada, fonte); al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/4, 50, ALLEGRO_ALIGN_CENTRE, "Taxa de Frames: %i", i); al_draw_textf(fonte, al_map_rgb(0, 0, 0), pos_x, pos_y, ALLEGRO_ALIGN_LEFT, " x:%i y:%i", pos_x, pos_y); draw_horda(monstro, n_mostros, imagem); if(torre_mouse) { draw_tower(r, l, torre, t); } drawTiro(tiro, t-1); al_flip_display(); al_clear_to_color(al_map_rgb(255,255,255)); } } destroy_al(janela, fonte, fila_eventos, imagem, timer); //Destroi as vari�veis allegro return 0; }