void Framework::TranslateAllegroEvents() { ALLEGRO_EVENT e; Event* fwE; while( al_get_next_event( eventAllegro, &e ) ) { switch( e.type ) { case ALLEGRO_EVENT_DISPLAY_CLOSE: fwE = new Event(); fwE->Type = EVENT_WINDOW_CLOSED; PushEvent( fwE ); break; case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION: al_reconfigure_joysticks(); GetJoystickIDs(); break; case ALLEGRO_EVENT_TIMER: if( e.timer.source == frameTimer ) { if( enableSlowDown ) { // Slow the game down, never process more than one update per frame framesToProcess = 1; } else { framesToProcess++; } } else { fwE = new Event(); fwE->Type = EVENT_TIMER_TICK; fwE->Data.Timer.TimerObject = (void*)e.timer.source; PushEvent( fwE ); } break; case ALLEGRO_EVENT_JOYSTICK_AXIS: fwE = new Event(); fwE->Type = EVENT_JOYSTICK_AXIS; fwE->Data.Joystick.ID = -1; for( int i = 0; i < al_get_num_joysticks(); i++ ) { if( joystickIDs.at( i ) == e.joystick.id ) { fwE->Data.Joystick.ID = i; break; } } fwE->Data.Joystick.Stick = e.joystick.stick; fwE->Data.Joystick.Axis = e.joystick.axis; fwE->Data.Joystick.Position = e.joystick.pos; PushEvent( fwE ); break; case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN: fwE = new Event(); fwE->Type = EVENT_JOYSTICK_BUTTON_DOWN; fwE->Data.Joystick.ID = -1; for( int i = 0; i < al_get_num_joysticks(); i++ ) { if( joystickIDs.at( i ) == e.joystick.id ) { fwE->Data.Joystick.ID = i; break; } } fwE->Data.Joystick.Button = e.joystick.button; PushEvent( fwE ); break; case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP: fwE = new Event(); fwE->Type = EVENT_JOYSTICK_BUTTON_UP; fwE->Data.Joystick.ID = -1; for( int i = 0; i < al_get_num_joysticks(); i++ ) { if( joystickIDs.at( i ) == e.joystick.id ) { fwE->Data.Joystick.ID = i; break; } } fwE->Data.Joystick.Button = e.joystick.button; PushEvent( fwE ); break; case ALLEGRO_EVENT_KEY_DOWN: fwE = new Event(); fwE->Type = EVENT_KEY_DOWN; fwE->Data.Keyboard.KeyCode = e.keyboard.keycode; fwE->Data.Keyboard.UniChar = e.keyboard.unichar; fwE->Data.Keyboard.Modifiers = e.keyboard.modifiers; PushEvent( fwE ); break; case ALLEGRO_EVENT_KEY_UP: fwE = new Event(); fwE->Type = EVENT_KEY_UP; fwE->Data.Keyboard.KeyCode = e.keyboard.keycode; fwE->Data.Keyboard.UniChar = e.keyboard.unichar; fwE->Data.Keyboard.Modifiers = e.keyboard.modifiers; PushEvent( fwE ); break; case ALLEGRO_EVENT_KEY_CHAR: fwE = new Event(); fwE->Type = EVENT_KEY_PRESS; fwE->Data.Keyboard.KeyCode = e.keyboard.keycode; fwE->Data.Keyboard.UniChar = e.keyboard.unichar; fwE->Data.Keyboard.Modifiers = e.keyboard.modifiers; PushEvent( fwE ); break; case ALLEGRO_EVENT_MOUSE_AXES: fwE = new Event(); fwE->Type = EVENT_MOUSE_MOVE; fwE->Data.Mouse.X = DISPLAY->ScreenXToGameX(e.mouse.x); fwE->Data.Mouse.Y = DISPLAY->ScreenYToGameY(e.mouse.y); fwE->Data.Mouse.DeltaX = DISPLAY->ScreenXToGameX(e.mouse.dx); fwE->Data.Mouse.DeltaY = DISPLAY->ScreenYToGameY(e.mouse.dy); fwE->Data.Mouse.WheelVertical = e.mouse.dz; fwE->Data.Mouse.WheelHorizontal = e.mouse.dw; fwE->Data.Mouse.Button = e.mouse.button; PushEvent( fwE ); break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: fwE = new Event(); fwE->Type = EVENT_MOUSE_DOWN; fwE->Data.Mouse.X = DISPLAY->ScreenXToGameX(e.mouse.x); fwE->Data.Mouse.Y = DISPLAY->ScreenYToGameY(e.mouse.y); fwE->Data.Mouse.DeltaX = DISPLAY->ScreenXToGameX(e.mouse.dx); fwE->Data.Mouse.DeltaY = DISPLAY->ScreenYToGameY(e.mouse.dy); fwE->Data.Mouse.WheelVertical = e.mouse.dz; fwE->Data.Mouse.WheelHorizontal = e.mouse.dw; fwE->Data.Mouse.Button = e.mouse.button; PushEvent( fwE ); break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: fwE = new Event(); fwE->Type = EVENT_MOUSE_UP; fwE->Data.Mouse.X = DISPLAY->ScreenXToGameX(e.mouse.x); fwE->Data.Mouse.Y = DISPLAY->ScreenYToGameY(e.mouse.y); fwE->Data.Mouse.DeltaX = DISPLAY->ScreenXToGameX(e.mouse.dx); fwE->Data.Mouse.DeltaY = DISPLAY->ScreenYToGameY(e.mouse.dy); fwE->Data.Mouse.WheelVertical = e.mouse.dz; fwE->Data.Mouse.WheelHorizontal = e.mouse.dw; fwE->Data.Mouse.Button = e.mouse.button; PushEvent( fwE ); break; case ALLEGRO_EVENT_DISPLAY_RESIZE: fwE = new Event(); fwE->Type = EVENT_WINDOW_RESIZE; fwE->Data.Display.X = 0; fwE->Data.Display.Y = 0; fwE->Data.Display.Width = e.display.width; fwE->Data.Display.Height = e.display.height; fwE->Data.Display.Active = true; PushEvent( fwE ); break; case ALLEGRO_EVENT_DISPLAY_SWITCH_IN: fwE = new Event(); fwE->Type = EVENT_WINDOW_ACTIVATE; fwE->Data.Display.X = 0; fwE->Data.Display.Y = 0; fwE->Data.Display.Width = e.display.width; fwE->Data.Display.Height = e.display.height; fwE->Data.Display.Active = true; PushEvent( fwE ); break; case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT: fwE = new Event(); fwE->Type = EVENT_WINDOW_DEACTIVATE; fwE->Data.Display.X = 0; fwE->Data.Display.Y = 0; fwE->Data.Display.Width = e.display.width; fwE->Data.Display.Height = e.display.height; fwE->Data.Display.Active = false; PushEvent( fwE ); break; case ALLEGRO_EVENT_AUDIO_STREAM_FINISHED: fwE = new Event(); fwE->Type = EVENT_AUDIO_STREAM_FINISHED; PushEvent( fwE ); break; default: fwE = new Event(); fwE->Type = EVENT_UNDEFINED; PushEvent( fwE ); break; } } }
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, "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 ImGui binding ImGui_ImplA5_Init(display); // Load Fonts // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details) //ImGuiIO& io = ImGui::GetIO(); //io.Fonts->AddFontDefault(); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f); //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); bool show_test_window = true; bool show_another_window = false; ImVec4 clear_color = ImColor(114, 144, 154); // Main loop bool running = true; while (running) { ALLEGRO_EVENT ev; while (al_get_next_event(queue, &ev)) { ImGui_ImplA5_ProcessEvent(&ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) running = false; if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ImGui_ImplA5_InvalidateDeviceObjects(); al_acknowledge_resize(display); Imgui_ImplA5_CreateDeviceObjects(); } } ImGui_ImplA5_NewFrame(); // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" { static float f; ImGui::Text("Hello, world!"); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::ColorEdit3("clear color", (float*)&clear_color); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f/ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair if (show_another_window) { ImGui::SetNextWindowSize(ImVec2(200, 100), ImGuiSetCond_FirstUseEver); ImGui::Begin("Another Window", &show_another_window); ImGui::Text("Hello"); ImGui::End(); } // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() if (show_test_window) { ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver); ImGui::ShowTestWindow(&show_test_window); } // Rendering al_clear_to_color(al_map_rgba_f(clear_color.x, clear_color.y, clear_color.z, clear_color.w)); ImGui::Render(); al_flip_display(); } // Cleanup ImGui_ImplA5_Shutdown(); al_destroy_event_queue(queue); al_destroy_display(display); return 0; }
static bool test(ALLEGRO_BITMAP *bitmap, ALLEGRO_FONT *font, char *message) { ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; double start_time; long frames = 0; double fps = 0; char second_line[100]; bool quit = false; queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); start_time = al_get_time(); for (;;) { if (al_get_next_event(queue, &event)) { if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { break; } if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { quit = true; break; } } } al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); /* Clear the backbuffer with red so we can tell if the bitmap does not * cover the entire backbuffer. */ al_clear_to_color(al_map_rgb(255, 0, 0)); al_draw_scaled_bitmap(bitmap, 0, 0, al_get_bitmap_width(bitmap), al_get_bitmap_height(bitmap), 0, 0, al_get_bitmap_width(al_get_target_bitmap()), al_get_bitmap_height(al_get_target_bitmap()), 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); /* Note this makes the memory buffer case much slower due to repeated * locking of the backbuffer. Officially you can't use al_lock_bitmap * to solve the problem either. */ print(font, message, 0, 0); sprintf(second_line, "%.1f FPS", fps); print(font, second_line, 0, al_get_font_line_height(font)+5); al_flip_display(); frames++; fps = (double)frames / (al_get_time() - start_time); } al_destroy_event_queue(queue); return quit; }
int main(int argc, char** argv) { int defw = 640; int defh = 480; if (argc == 3) { /*the program was run using 3 arguments argv[0] = name of the executable file argv[1] = screen width (as a string) argv[2] = screen height (as a string) */ defw = atoi(argv[1]); defh = atoi(argv[2]); } if (!al_init()) return 1; //exit if can't initialize allegro //initialize most stuff al_install_keyboard(); al_init_primitives_addon(); //run in a window al_set_new_display_flags(ALLEGRO_WINDOWED); //initialize our display and event queue ALLEGRO_DISPLAY* display = al_create_display(defw, defh); ALLEGRO_EVENT_QUEUE* eventq = al_create_event_queue(); //this timer will tick once per frame ALLEGRO_TIMER* framet = al_create_timer(1.0 / 30.0); //30 frames per second al_start_timer(framet); //register the event sources so they send events to our queue al_register_event_source(eventq, al_get_display_event_source(display)); //display/window al_register_event_source(eventq, al_get_keyboard_event_source()); //keyboard al_register_event_source(eventq, al_get_timer_event_source(framet)); //fps timer //this box is our play field (covers the whole screen) CBox fieldbox(0, 0, defw, defh); //we setup the ball at the center of the screen with white color CBall ball(CBox(defw / 2 - 10, defh / 2 - 10, 20, 20), al_map_rgb(255, 255, 255)); //we tell it to move to the left ball.setXYMovement(-5.0, 0.0); //we initialize our both players in an array CPlayer players[2] = { //red player on the left CPlayer(CBox(10, defh / 2 - 80 / 2, 20, 80), al_map_rgb(255, 0, 0)), //blue player on the right CPlayer(CBox(defw - 10 - 20, defh / 2 - 80 / 2, 20, 80), al_map_rgb(0, 0, 255)), }; /*when this variable is set to true the program will quit the main loop and free the allocated resources before quitting */ bool exit = false; while (!exit) { al_wait_for_event(eventq, NULL); ALLEGRO_EVENT ev; while (al_get_next_event(eventq, &ev)) { if (ev.type == ALLEGRO_EVENT_TIMER) { if (ev.timer.source == framet) { //fill the screen with black al_clear_to_color(al_map_rgb(0, 0, 0)); //move and draw our two players for (int i = 0; i<2; i++) { players[i].move(fieldbox); players[i].draw(); } //move, collide and draw the ball switch (ball.move(fieldbox, players)) { case 0: break; case 1: players[0].setScore(players[0].getScore() + 1); ball.setXYMovement(5.0, 0.0); break; case 2: players[1].setScore(players[1].getScore() + 1); ball.setXYMovement(-5.0, 0.0); break; } ball.draw(); //show what we've drawn al_flip_display(); } } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { //quit if the user tries to close the window if (ev.display.source == display) exit = true; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { //handle key presses switch (ev.keyboard.keycode) { case ALLEGRO_KEY_W: players[0].setYMovement(-3.0); break; case ALLEGRO_KEY_S: players[0].setYMovement(3.0); break; case ALLEGRO_KEY_UP: players[1].setYMovement(-3.0); break; case ALLEGRO_KEY_DOWN: players[1].setYMovement(3.0); break; case ALLEGRO_KEY_ESCAPE: exit = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: exit = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { int code = ev.keyboard.keycode; /*avoid clumsy movement making sure the released key corresponds with the moving direction*/ if (code == ALLEGRO_KEY_W && players[0].getYMovement() < 0) players[0].setYMovement(0.0); else if (code == ALLEGRO_KEY_S && players[0].getYMovement() > 0) players[0].setYMovement(0.0); else if (code == ALLEGRO_KEY_UP && players[1].getYMovement() < 0) players[1].setYMovement(0.0); else if (code == ALLEGRO_KEY_DOWN && players[1].getYMovement() > 0) players[1].setYMovement(0.0); } } } al_destroy_event_queue(eventq); al_destroy_timer(framet); al_destroy_display(display); }
void Tela::processa_eventos() { /* processa eventos do servidor X, atualizando a posicao do mouse * e ultima _tecla pressionada na variavel da tela. */ ALLEGRO_EVENT event; // al_wait_for_event(queue, &event); while (al_get_next_event(queue, &event)) { switch (event.type) { /* _tecla foi pressionada */ case ALLEGRO_EVENT_KEY_CHAR: { #if 0 const char* keyname = al_keycode_to_name(event.keyboard.keycode); printf("code %x modif %x char '%c' name '%s' (q=%d)\n", event.keyboard.keycode, event.keyboard.modifiers, (char)event.keyboard.keycode, keyname, 'Q'); #endif _tecla = event.keyboard.keycode; _modif = event.keyboard.modifiers; break; } #if 0 case ALLEGRO_EVENT_KEY_DOWN: { /* const char* keyname = al_keycode_to_name(event.keyboard.keycode); printf("code %d char '%c' name '%s' (q=%d)\n", event.keyboard.keycode, (char)event.keyboard.keycode, keyname, 'Q');*/ _tecla = event.keyboard.keycode; break; } #endif case ALLEGRO_EVENT_MOUSE_AXES: { _rato.x = XX2U(event.mouse.x); _rato.y = YX2U(event.mouse.y); break; } case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: { if (event.mouse.button == 1) _botao = true; break; } case ALLEGRO_EVENT_MOUSE_BUTTON_UP: { if (event.mouse.button == 1) _botao = false; break; } case ALLEGRO_EVENT_TIMER: { break; } #if 0 /* _botao de fechar janela pressionado */ case ALLEGRO_EVENT_DISPLAY_CLOSE: { return; break; } #endif default: break; } } }
int main(int argc, char **argv) { InitAllegro(); ALLEGRO_KEYBOARD_STATE klawiatura; ALLEGRO_MOUSE_STATE mysz; ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue(); ALLEGRO_EVENT_QUEUE* key_queue = al_create_event_queue(); ALLEGRO_DISPLAY* okno = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT); ALLEGRO_FONT* defaultFont = al_create_builtin_font(); ALLEGRO_TIMER* timer = al_create_timer(1.0 / 60.0); al_register_event_source(key_queue, al_get_display_event_source(okno)); al_register_event_source(event_queue, al_get_display_event_source(okno)); al_register_event_source(key_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_set_window_title(okno, "Fraktale"); al_set_target_bitmap(al_get_backbuffer(okno)); Menu* menu = new Menu(WINDOW_WIDTH, WINDOW_HEIGHT); Mandelbrot* mandelbrot = new Mandelbrot(WINDOW_WIDTH, WINDOW_HEIGHT, defaultFont); Julia* julia = new Julia(WINDOW_WIDTH, WINDOW_HEIGHT, defaultFont); Window* window = new Window(menu, mandelbrot, julia); menu->SetWindow(window); mandelbrot->SetWindow(window); julia->SetWindow(window); const ALLEGRO_COLOR backgroundColor = al_map_rgb(255, 255, 255); const ALLEGRO_COLOR frameColor = al_map_rgb(255, 255, 255); const int frameThickness = 2; ALLEGRO_USTR* str = al_ustr_new(""); int pos = (int)al_ustr_size(str); ALLEGRO_EVENT ev; double blokadaKlikniecia = al_get_time(); int poczX = -1, poczY = -1, poprzedniStan = window->stanOkna; bool petla = true, klikniecieMyszy = false, klawiszWcisniety = false, wpisywanieIteracji = false; double screenRatio = static_cast<double>(WINDOW_HEIGHT) / static_cast<double>(WINDOW_WIDTH); while (petla) { if (poprzedniStan != window->stanOkna) { blokadaKlikniecia = al_get_time(); } poprzedniStan = window->stanOkna; al_get_next_event(event_queue, &ev); al_get_mouse_state(&mysz); int mx = mysz.x; int my = mysz.y; bool koniecKlikniecia = false; if (mysz.buttons & 1 && klikniecieMyszy == false && al_get_time() > blokadaKlikniecia + 0.3) { klikniecieMyszy = true; if (window->CzyFraktal()) { poczX = mx; poczY = my; } } else if (!(mysz.buttons & 1) && klikniecieMyszy == true && al_get_time() > blokadaKlikniecia + 0.3) { klikniecieMyszy = false; int pp = window->stanOkna; int respond = window->Click(mx, my); if (respond == Responds_t::RESPOND_CLOSE_WINDOW) petla = false; if (window->CzyFraktal() && (pp == WINDOWSTATE_MANDELBROT || pp == WINDOWSTATE_JULIA)) { koniecKlikniecia = true; } if (pp == WINDOWSTATE_MENU && window->CzyFraktal()) { window->ZaladujFraktal(); } } if (koniecKlikniecia) { if (window->CzyFraktal()) { Fraktal* fraktal = window->AktualnyFraktal(); fraktal->Powieksz(poczX, mx, poczY, SkalujY(poczY, screenRatio, poczX, mx)); } poczX = -1, poczY = -1; } al_get_next_event(key_queue, &ev); if (ev.type == ALLEGRO_EVENT_KEY_DOWN && klawiszWcisniety == false && window->CzyFraktal()) { int kod = ev.keyboard.keycode; klawiszWcisniety = true; if (kod == 9) { wpisywanieIteracji = true; } else if (kod == 10) { Fraktal* fraktal = window->AktualnyFraktal(); fraktal->Resetuj(); } else if (kod >= 27 && kod <= 36 && wpisywanieIteracji) { pos += al_ustr_append_chr(str, kod + 21); } else if (kod == ALLEGRO_KEY_ENTER) { if (wpisywanieIteracji == true) wpisywanieIteracji = false; unsigned char* tmp = str->data; int t = atoi((const char*)tmp); window->SetIteracje(t); } else if (kod == ALLEGRO_KEY_BACKSPACE) { if (al_ustr_prev(str, &pos)) al_ustr_truncate(str, pos); } else if (kod == ALLEGRO_KEY_ESCAPE) { window->stanOkna = WINDOWSTATE_MENU; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { klawiszWcisniety = false; } al_clear_to_color(backgroundColor); if (wpisywanieIteracji) window->Draw(str); else window->Draw(NULL); if(poczX != -1) al_draw_rectangle(poczX, poczY, mx, SkalujY(poczY, screenRatio, poczX, mx), frameColor, frameThickness); al_flip_display(); } al_destroy_display(okno); return 0; }
void Mouse::Update() { ALLEGRO_EVENT e; ALLEGRO_EVENT ev; while( al_get_next_event( mouseQueue, &e ) ) { switch( e.type ) { case ALLEGRO_EVENT_MOUSE_AXES: Position.X = e.mouse.x; Position.Y = e.mouse.y; if( mouseDownButton != 0 && AllowBoxing && !blockBoxing ) isBoxing = true; if( !AllowBoxing && isBoxing ) isBoxing = false; // Cancel boxing if it has been disabled if( e.mouse.dx != 0 || e.mouse.dy != 0 ) { ev.user.data1 = (intptr_t)this; ev.user.data2 = (intptr_t)malloc( sizeof( Position ) ); memcpy( (void*)ev.user.data2, (void*)&Position, sizeof( Position ) ); ev.user.data3 = (intptr_t)malloc( sizeof( Position ) ); ((Vector2*)ev.user.data3)->X = e.mouse.dx; ((Vector2*)ev.user.data3)->Y = e.mouse.dy; ev.user.data4 = e.mouse.button; ev.type = ALLEGRO_EVENT_MOUSEEX_MOVE; al_emit_user_event( &mouseEventSource, &ev, &Mouse::event_destructor ); } if( e.mouse.dz != 0 ) { ev.user.data1 = (intptr_t)this; ev.user.data2 = (intptr_t)malloc( sizeof( Position ) ); memcpy( (void*)ev.user.data2, (void*)&Position, sizeof( Position ) ); ev.user.data3 = 0; ev.user.data4 = e.mouse.dz; ev.type = ALLEGRO_EVENT_MOUSEEX_WHEEL; al_emit_user_event( &mouseEventSource, &ev, &Mouse::event_destructor ); } break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: Position.X = e.mouse.x; Position.Y = e.mouse.y; mouseDownAt.X = e.mouse.x; mouseDownAt.Y = e.mouse.y; mouseDownButton = e.mouse.button; ev.user.data1 = (intptr_t)this; ev.user.data2 = (intptr_t)malloc( sizeof( Position ) ); memcpy( (void*)ev.user.data2, (void*)&Position, sizeof( Position ) ); ev.user.data3 = 0; ev.user.data4 = e.mouse.button; ev.type = ALLEGRO_EVENT_MOUSEEX_DOWN; al_emit_user_event( &mouseEventSource, &ev, &Mouse::event_destructor ); break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: blockBoxing = false; Position.X = e.mouse.x; Position.Y = e.mouse.y; ev.user.data1 = (intptr_t)this; ev.user.data2 = (intptr_t)malloc( sizeof( Position ) ); memcpy( (void*)ev.user.data2, (void*)&Position, sizeof( Position ) ); ev.user.data3 = 0; ev.user.data4 = e.mouse.button; ev.type = ALLEGRO_EVENT_MOUSEEX_UP; al_emit_user_event( &mouseEventSource, &ev, &Mouse::event_destructor ); if( mouseDownButton == e.mouse.button ) { if( abs( e.mouse.x - mouseDownAt.X ) < ClickFidelity && abs( e.mouse.y - mouseDownAt.Y ) < ClickFidelity ) { ev.user.data2 = (intptr_t)malloc( sizeof( mouseDownAt ) ); memcpy( (void*)ev.user.data2, (void*)&mouseDownAt, sizeof( mouseDownAt ) ); ev.type = ALLEGRO_EVENT_MOUSEEX_CLICK; al_emit_user_event( &mouseEventSource, &ev, &Mouse::event_destructor ); if( al_get_time() - lastClickTime < DoubleClickFidelity && al_get_time() - lastDblClickTime > DoubleClickFidelity ) { ev.user.data2 = (intptr_t)malloc( sizeof( mouseDownAt ) ); memcpy( (void*)ev.user.data2, (void*)&mouseDownAt, sizeof( mouseDownAt ) ); ev.type = ALLEGRO_EVENT_MOUSEEX_DOUBLECLICK; al_emit_user_event( &mouseEventSource, &ev, &Mouse::event_destructor ); lastDblClickTime = al_get_time(); } lastClickTime = al_get_time(); } if( isBoxing ) { ev.user.data2 = (intptr_t)malloc( sizeof( mouseDownAt ) ); ((Vector2*)ev.user.data2)->X = min(mouseDownAt.X, Position.X); ((Vector2*)ev.user.data2)->Y = min(mouseDownAt.Y, Position.Y); ev.user.data3 = (intptr_t)malloc( sizeof( mouseDownAt ) ); ((Vector2*)ev.user.data3)->X = min(mouseDownAt.X, Position.X); ((Vector2*)ev.user.data3)->Y = min(mouseDownAt.Y, Position.Y); ev.type = ALLEGRO_EVENT_MOUSEEX_BOXED; al_emit_user_event( &mouseEventSource, &ev, &Mouse::event_destructor ); isBoxing = false; } mouseDownButton = 0; } break; } } }
static inline void HandleEvent(struct Game* game, ALLEGRO_EVENT* ev) { switch (ev->type) { case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: PauseExecution(game); al_acknowledge_drawing_halt(game->display); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: al_acknowledge_drawing_resume(game->display); ReloadGamestates(game); ResumeExecution(game); break; case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT: if (game->config.autopause) { PrintConsole(game, "Focus lost, autopausing..."); PauseExecution(game); } break; case ALLEGRO_EVENT_DISPLAY_SWITCH_IN: if (game->config.autopause) { if (game->config.debug.enabled && game->config.debug.livereload) { ReloadCode(game); } ResumeExecution(game); } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: PrintConsole(game, "Resize event: %dx%d", ev->display.width, ev->display.height); #ifdef LIBSUPERDERPY_IMGUI ImGui_ImplAllegro5_InvalidateDeviceObjects(); #endif al_acknowledge_resize(game->display); #ifdef LIBSUPERDERPY_IMGUI ImGui_ImplAllegro5_CreateDeviceObjects(); #endif // SetupViewport can be expensive, so don't do it when the resize event is already outdated or doesn't change anything if (((ev->display.width != game->_priv.window_width) || (ev->display.height != game->_priv.window_height)) && (ev->display.width == al_get_display_width(game->display)) && (ev->display.height == al_get_display_height(game->display))) { SetupViewport(game); } break; case ALLEGRO_EVENT_KEY_DOWN: #ifdef ALLEGRO_ANDROID if ((ev->keyboard.keycode == ALLEGRO_KEY_MENU) || (ev->keyboard.keycode == ALLEGRO_KEY_TILDE) || (ev->keyboard.keycode == ALLEGRO_KEY_BACKQUOTE)) { #else if ((ev->keyboard.keycode == ALLEGRO_KEY_TILDE) || (ev->keyboard.keycode == ALLEGRO_KEY_BACKQUOTE)) { #endif game->_priv.showconsole = !game->_priv.showconsole; if ((ev->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) && (game->config.debug.enabled)) { game->_priv.showtimeline = game->_priv.showconsole; } } if (ev->keyboard.keycode == ALLEGRO_KEY_F12) { DrawGamestates(game); int flags = al_get_new_bitmap_flags(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); ALLEGRO_BITMAP* bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display)); al_set_new_bitmap_flags(flags); ALLEGRO_BITMAP* target = al_get_target_bitmap(); al_set_target_bitmap(bitmap); al_draw_bitmap(al_get_backbuffer(game->display), 0, 0, 0); al_set_target_bitmap(target); PrintConsole(game, "Screenshot made! Storing..."); struct ScreenshotThreadData* data = malloc(sizeof(struct ScreenshotThreadData)); data->game = game; data->bitmap = bitmap; #ifndef LIBSUPERDERPY_SINGLE_THREAD al_run_detached_thread(ScreenshotThread, data); #else ScreenshotThread(data); #endif } break; default: break; } #ifdef MAEMO5 // on Maemo we get mouse events instead of touch ones, so we'll rewrite them by ourselves if ((ev->type == ALLEGRO_EVENT_MOUSE_AXES) || (ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) || (ev->type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)) { switch (ev->type) { case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: ev->type = ALLEGRO_EVENT_TOUCH_BEGIN; break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: ev->type = ALLEGRO_EVENT_TOUCH_END; break; case ALLEGRO_EVENT_MOUSE_AXES: ev->type = ALLEGRO_EVENT_TOUCH_MOVE; break; default: break; } ALLEGRO_DISPLAY* display = ev->mouse.display; float dx = ev->mouse.dx; float dy = ev->mouse.dy; float x = ev->mouse.x; float y = ev->mouse.y; double timestamp = ev->mouse.timestamp; ev->touch.display = display; ev->touch.dx = dx; ev->touch.dy = dy; ev->touch.id = 0; ev->touch.primary = true; ev->touch.source = (ALLEGRO_TOUCH_INPUT*)al_get_touch_input_event_source(); ev->touch.timestamp = timestamp; ev->touch.x = x; ev->touch.y = y; } #endif } static inline void HandleDebugEvent(struct Game* game, ALLEGRO_EVENT* ev) { switch (ev->type) { case ALLEGRO_EVENT_KEY_DOWN: switch (ev->keyboard.keycode) { case ALLEGRO_KEY_F1: if (!game->_priv.paused) { PauseExecution(game); } else { ReloadCode(game); ResumeExecution(game); } break; case ALLEGRO_KEY_F9: game->_priv.speed = ALLEGRO_BPS_TO_SECS(60.0); game->_priv.showconsole = true; PrintConsole(game, "DEBUG: Gameplay speed: 1.00x"); break; case ALLEGRO_KEY_F10: { double speed = ALLEGRO_BPS_TO_SECS(game->_priv.speed); // inverting speed -= 10; if (speed < 10) { speed = 10; } game->_priv.speed = ALLEGRO_BPS_TO_SECS(speed); game->_priv.showconsole = true; PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0); } break; case ALLEGRO_KEY_F11: { double speed = ALLEGRO_BPS_TO_SECS(game->_priv.speed); // inverting speed += 10; if (speed > 600) { speed = 600; } game->_priv.speed = ALLEGRO_BPS_TO_SECS(speed); game->_priv.showconsole = true; PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0); } break; } break; default: break; } } static inline bool MainloopEvents(struct Game* game) { do { ALLEGRO_EVENT ev; if (game->_priv.paused && !IS_EMSCRIPTEN) { // there's no frame flipping when paused, so avoid pointless busylooping al_wait_for_event(game->_priv.event_queue, &ev); } else if (!al_get_next_event(game->_priv.event_queue, &ev)) { break; } #ifdef LIBSUPERDERPY_IMGUI ImGui_ImplAllegro5_ProcessEvent(&ev); switch (ev.type) { case ALLEGRO_EVENT_KEY_CHAR: case ALLEGRO_EVENT_KEY_DOWN: case ALLEGRO_EVENT_KEY_UP: if (igGetIO()->WantCaptureKeyboard) { continue; } break; case ALLEGRO_EVENT_MOUSE_AXES: case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: case ALLEGRO_EVENT_MOUSE_BUTTON_UP: case ALLEGRO_EVENT_TOUCH_BEGIN: case ALLEGRO_EVENT_TOUCH_CANCEL: case ALLEGRO_EVENT_TOUCH_END: case ALLEGRO_EVENT_TOUCH_MOVE: if (igGetIO()->WantCaptureMouse) { continue; } break; default: break; } #endif if (game->_priv.params.handlers.event) { if ((*game->_priv.params.handlers.event)(game, &ev)) { continue; } } if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { EventGamestates(game, &ev); return false; } HandleEvent(game, &ev); if (game->config.debug.enabled) { HandleDebugEvent(game, &ev); } EventGamestates(game, &ev); } while (!al_is_event_queue_empty(game->_priv.event_queue)); return true; } static inline bool MainloopTick(struct Game* game) { if (game->_priv.paused) { return true; } struct Gamestate* tmp = game->_priv.gamestates; #ifdef __EMSCRIPTEN__ emscripten_pause_main_loop(); #endif game->_priv.loading.to_load = 0; game->_priv.loading.loaded = 0; game->_priv.loading.lock = true; game->loading.progress = 0; // TODO: support gamestate dependences/ordering while (tmp) { if (tmp->pending_stop) { PrintConsole(game, "Stopping gamestate \"%s\"...", tmp->name); game->_priv.current_gamestate = tmp; (*tmp->api->stop)(game, tmp->data); tmp->started = false; tmp->pending_stop = false; PrintConsole(game, "Gamestate \"%s\" stopped successfully.", tmp->name); } if (tmp->pending_load) { game->_priv.loading.to_load++; } tmp = tmp->next; } tmp = game->_priv.gamestates; while (tmp) { if (tmp->pending_unload) { #ifdef __EMSCRIPTEN__ al_detach_voice(game->audio.v); #endif PrintConsole(game, "Unloading gamestate \"%s\"...", tmp->name); tmp->loaded = false; tmp->pending_unload = false; game->_priv.current_gamestate = tmp; (*tmp->api->unload)(game, tmp->data); PrintConsole(game, "Gamestate \"%s\" unloaded successfully.", tmp->name); #ifdef __EMSCRIPTEN__ al_attach_mixer_to_voice(game->audio.mixer, game->audio.v); #endif } if (tmp->pending_load) { #ifdef __EMSCRIPTEN__ al_detach_voice(game->audio.v); #endif if (tmp->show_loading && game->_priv.loading.gamestate->open) { (*game->_priv.loading.gamestate->api->start)(game, game->_priv.loading.gamestate->data); } if (!tmp->api) { if (!OpenGamestate(game, tmp, true) || !LinkGamestate(game, tmp)) { tmp->pending_load = false; tmp->pending_start = false; continue; } } if (tmp->api) { PrintConsole(game, "Loading gamestate \"%s\"...", tmp->name); game->_priv.loading.progress = 0; game->_priv.loading.current = tmp; game->_priv.current_gamestate = tmp; struct GamestateLoadingThreadData data = {.game = game, .gamestate = tmp, .bitmap_flags = al_get_new_bitmap_flags()}; game->_priv.loading.in_progress = true; double time = al_get_time(); game->_priv.loading.time = time; CalculateProgress(game); if (tmp->show_loading) { game->loading.shown = true; DrawGamestates(game); DrawConsole(game); al_flip_display(); #ifdef __EMSCRIPTEN__ emscripten_sleep(0); #endif } #ifndef LIBSUPERDERPY_SINGLE_THREAD al_run_detached_thread(GamestateLoadingThread, &data); while (game->_priv.loading.in_progress) { double delta = al_get_time() - game->_priv.loading.time; game->time += delta; // TODO: ability to disable passing time during loading game->_priv.loading.time += delta; if (game->loading.shown && game->_priv.loading.gamestate->open) { (*game->_priv.loading.gamestate->api->logic)(game, game->_priv.loading.gamestate->data, delta); } DrawGamestates(game); if (game->_priv.texture_sync) { al_convert_memory_bitmaps(); game->_priv.texture_sync = false; al_signal_cond(game->_priv.texture_sync_cond); game->_priv.loading.time = al_get_time(); // TODO: rethink time management during loading } DrawConsole(game); al_flip_display(); if (game->_priv.bsod_sync) { al_set_target_bitmap(NULL); game->_priv.bsod_sync = false; al_signal_cond(game->_priv.bsod_cond); } al_lock_mutex(game->_priv.bsod_mutex); while (game->_priv.in_bsod) { al_wait_cond(game->_priv.bsod_cond, game->_priv.bsod_mutex); } al_unlock_mutex(game->_priv.bsod_mutex); } #else GamestateLoadingThread(&data); DrawGamestates(game); DrawConsole(game); al_flip_display(); #ifdef __EMSCRIPTEN__ emscripten_sleep(0); #endif al_convert_memory_bitmaps(); #endif al_set_new_bitmap_flags(data.bitmap_flags); ReloadShaders(game, false); if (tmp->api->post_load) { PrintConsole(game, "[%s] Post-loading...", tmp->name); tmp->api->post_load(game, tmp->data); } game->_priv.loading.progress++; CalculateProgress(game); PrintConsole(game, "Gamestate \"%s\" loaded successfully in %f seconds.", tmp->name, al_get_time() - time); game->_priv.loading.loaded++; DrawGamestates(game); DrawConsole(game); al_flip_display(); #ifdef __EMSCRIPTEN__ emscripten_sleep(0); #endif tmp->loaded = true; tmp->pending_load = false; } if (tmp->show_loading && game->_priv.loading.gamestate->open) { (*game->_priv.loading.gamestate->api->stop)(game, game->_priv.loading.gamestate->data); } tmp->show_loading = true; game->loading.shown = false; game->_priv.timestamp = al_get_time(); #ifdef __EMSCRIPTEN__ al_attach_mixer_to_voice(game->audio.mixer, game->audio.v); #endif } tmp = tmp->next; }
/* dialog_message: * Sends a message to all the objects in a dialog. If any of the objects * return values other than D_O_K, returns the value and sets obj to the * object which produced it. */ int t3gui_dialog_message(T3GUI_ELEMENT *dialog, int msg, int c, int *obj) { int count, res, r, force, try; assert(dialog); force = ((msg == MSG_START) || (msg == MSG_END) || (msg >= MSG_USER)); res = D_O_K; /* If a menu spawned by a d_menu_proc object is active, the dialog engine * has effectively been shutdown for the sake of safety. This means that * we can't send the message to the other objects in the dialog. So try * first to send the message to the d_menu_proc object and, if the menu * is then not active anymore, send it to the other objects as well. */ //if (active_menu_player) { // try = 2; // menu_dialog = active_menu_player->dialog; //} //else try = 1; for (; try > 0; try--) { for (count=0; dialog[count].proc; count++) { //if ((try == 2) && (&dialog[count] != menu_dialog)) // continue; if ((force) || (!(dialog[count].flags & D_HIDDEN))) { r = t3gui_object_message(dialog+count, msg, c); if (r != D_O_K) { res |= r; if (msg != MSG_DRAW && msg != MSG_IDLE && msg != MSG_TIMER && obj) { printf("new object: %d\n", count); *obj = count; } } if ((msg == MSG_IDLE) && (dialog[count].flags & (D_DIRTY | D_HIDDEN)) == D_DIRTY) { dialog[count].flags &= ~D_DIRTY; res |= D_REDRAW; } } } //if (active_menu_player) // break; } return res; } int t3gui_do_dialog(T3GUI_ELEMENT *dialog, int focus) { return t3gui_do_dialog_interval(dialog, 0.0, focus); } int t3gui_do_dialog_interval(T3GUI_ELEMENT *dialog, double speed_sec, int focus) { T3GUI_PLAYER *player = t3gui_init_dialog(dialog, focus, 0, NULL, NULL, NULL); ALLEGRO_TIMER *timer = NULL; t3gui_listen_for_events(player, al_get_keyboard_event_source()); t3gui_listen_for_events(player, al_get_mouse_event_source()); t3gui_listen_for_events(player, al_get_joystick_event_source()); t3gui_listen_for_events(player, al_get_display_event_source(al_get_current_display())); if (speed_sec > 0.0) { timer = al_create_timer(speed_sec); t3gui_listen_for_events(player, al_get_timer_event_source(timer)); al_start_timer(timer); } ALLEGRO_EVENT_QUEUE *menu_queue = al_create_event_queue(); al_register_event_source(menu_queue, t3gui_get_player_event_source(player)); t3gui_start_dialog(player); bool in_dialog = true; bool must_draw = false; while (in_dialog) { if (must_draw) { must_draw = false; t3gui_draw_dialog(player); al_flip_display(); } al_wait_for_event(menu_queue, NULL); while (!al_is_event_queue_empty(menu_queue)) { ALLEGRO_EVENT event; al_get_next_event(menu_queue, &event); switch (event.type) { case T3GUI_EVENT_REDRAW: //printf("Redraw request\n"); must_draw = true; break; case T3GUI_EVENT_CLOSE: focus = event.user.data1; in_dialog = false; break; case T3GUI_EVENT_CANCEL: focus = -1; in_dialog = false; break; } } } t3gui_stop_dialog(player); al_destroy_timer(timer); al_destroy_event_queue(menu_queue); t3gui_shutdown_dialog(player); return focus; }
int main(){ float* terrain = (float*) malloc(RES*RES*sizeof(float)); genterrain(terrain, RES, 40000, 2.3, true); al_init(); al_install_keyboard(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_OPENGL); display = al_create_display(1280, 960); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); bool quit = false; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); float* verts = (float*) malloc(RES*RES*4*4*3); float* colors = (float*) malloc(RES*RES*4*4*3); float* vp = verts; float* cp = colors; for(int x = 0; x < RES-1; x++){ for(int z = 0; z < RES-1; z++){ *(vp++) = (x-RES/2.0+0.5)*1024.0/RES; *(vp++) = terrain[x*RES+z]; *(vp++) = (z-RES/2.0+0.5)*1024.0/RES; *(cp++) = 0.5*terrain[x*RES+z]; *(cp++) = 1-terrain[x*RES+z]; *(cp++) = 0.5*terrain[x*RES+z]; *(vp++) = (x-RES/2.0+1.5)*1024.0/RES; *(vp++) = terrain[(x+1)*RES+z]; *(vp++) = (z-RES/2.0+0.5)*1024.0/RES; *(cp++) = 0.5*terrain[(x+1)*RES+z]; *(cp++) = 1-terrain[(x+1)*RES+z]; *(cp++) = 0.5*terrain[(x+1)*RES+z]; *(vp++) = (x-RES/2.0+1.5)*1024.0/RES; *(vp++) = terrain[(x+1)*RES+z+1]; *(vp++) = (z-RES/2.0+1.5)*1024.0/RES; *(cp++) = 0.5*terrain[(x+1)*RES+z+1]; *(cp++) = 1-terrain[(x+1)*RES+z+1]; *(cp++) = 0.5*terrain[(x+1)*RES+z+1]; *(vp++) = (x-RES/2.0+0.5)*1024.0/RES; *(vp++) = terrain[x*RES+z+1]; *(vp++) = (z-RES/2.0+1.5)*1024.0/RES; *(cp++) = 0.5*terrain[x*RES+z+1]; *(cp++) = 1-terrain[x*RES+z+1]; *(cp++) = 0.5*terrain[x*RES+z+1]; } } glVertexPointer(3, GL_FLOAT, 0, verts); glColorPointer(3, GL_FLOAT, 0, colors); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); /*glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); glEnable(GL_POLYGON_SMOOTH);*/ while(!quit){ double ms = al_current_time(); ALLEGRO_EVENT event; if (al_get_next_event(event_queue, &event)){ if (ALLEGRO_EVENT_KEY_DOWN == event.type && ALLEGRO_KEY_Q == event.keyboard.keycode || ALLEGRO_EVENT_DISPLAY_CLOSE == event.type){ break; } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPerspective(45, 640.0/480.0,.1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(30, 1, 0, 0); glTranslatef(0, -100, -200); glScalef(1,20,1); glRotatef(ms*12, 0, 1, 0); glPolygonMode(GL_FRONT, GL_LINES); glDrawArrays(GL_QUADS, 0, RES*RES*4); al_flip_display(); } return 0; }
int main(int argc, const char* argv[]) { al_init(); al_init_font_addon(); al_init_ttf_addon(); al_init_image_addon(); al_init_primitives_addon(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display(800, 600); al_install_keyboard(); al_install_mouse(); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, (ALLEGRO_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()); ALLEGRO_FONT* font = al_load_ttf_font("data/times.ttf", 12, 0); Vector2 camera(-400, -300); Platforms platforms; Bitmaps bitmaps; Sprites sprites; File* file = new File(font); file->platforms = &platforms; file->bitmaps = &bitmaps; file->sprites = &sprites; Menu menu; menu.Add_entry(new Create_platform(platforms, camera, font)); menu.Add_entry(new Edit_platform(platforms, camera, font)); menu.Add_entry(file); menu.Add_entry(new Create_sprite(bitmaps, sprites, camera, font)); menu.Add_entry(new Edit_sprite(sprites, camera, font)); if(argc==2) file->Load(argv[1]); while(1) { ALLEGRO_EVENT event; if (al_get_next_event(event_queue, &event)) { if (ALLEGRO_EVENT_DISPLAY_CLOSE == event.type || ALLEGRO_EVENT_KEY_DOWN == event.type && ALLEGRO_KEY_ESCAPE == event.keyboard.keycode) { break; } if(ALLEGRO_EVENT_MOUSE_AXES) { ALLEGRO_MOUSE_STATE mstate; al_get_mouse_state(&mstate); if(al_mouse_button_down(&mstate, 3)) { camera.x -= event.mouse.dx; camera.y -= event.mouse.dy; } } menu.Event(event); } for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) { (*i)->Draw(camera); } for(Platforms::iterator i = platforms.begin(); i != platforms.end(); ++i) { (*i)->Draw(camera, al_map_rgb_f(0, 1, 0)); } al_draw_line(0, -camera.y, 800, -camera.y, al_map_rgba_f(0, 1, 0, 0.5), 0); al_draw_line(-camera.x, 0, -camera.x, 600, al_map_rgba_f(0, 0, 1, 0.5), 0); menu.Draw(); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); al_rest(0.001); } al_destroy_event_queue(event_queue); al_destroy_display(display); }
void InputCatcher::run(ALLEGRO_DISPLAY *display, InputContainer *held_keys, double *mouse_x, double *mouse_y) { // pressed_keys->reset(); held_keys->reset(); ALLEGRO_EVENT event; // Catch all events that have stacked up this frame. al_get_next_event() returns false when event_queue is empty, and contents of event are undefined while (al_get_next_event(event_queue, &event)) { switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: // Deliberate closing, not an error throw 0; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(display); break; case ALLEGRO_EVENT_KEY_DOWN: // //Debug: print the keycode number and name of the key we press //// printf("\n%i\t%s", event.keyboard.keycode, al_keycode_to_name(event.keyboard.keycode)); // // if (event.keyboard.keycode == config["jump"] or event.keyboard.keycode == config["jump_alt1"] or event.keyboard.keycode == config["jump_alt2"]) // { // pressed_keys->JUMP = true; // } // if (event.keyboard.keycode == config["crouch"] or event.keyboard.keycode == config["crouch_alt1"] or event.keyboard.keycode == config["crouch_alt2"]) // { // pressed_keys->CROUCH = true; // } // if (event.keyboard.keycode == config["left"] or event.keyboard.keycode == config["left_alt1"] or event.keyboard.keycode == config["left_alt2"]) // { // pressed_keys->LEFT = true; // } // if (event.keyboard.keycode == config["right"] or event.keyboard.keycode == config["right_alt1"] or event.keyboard.keycode == config["right_alt2"]) // { // pressed_keys->RIGHT = true; // } // if (event.keyboard.keycode == config["right"] or event.keyboard.keycode == config["right_alt1"] or event.keyboard.keycode == config["right_alt2"]) // { // pressed_keys->RIGHT = true; // } // if (event.keyboard.keycode == config["ability1"] or event.keyboard.keycode == config["ability1_alt1"] or event.keyboard.keycode == config["ability1_alt2"]) // { // pressed_keys->ABILITY_1 = true; // } // if (event.keyboard.keycode == config["ability2"] or event.keyboard.keycode == config["ability2_alt1"] or event.keyboard.keycode == config["ability2_alt2"]) // { // pressed_keys->ABILITY_2 = true; // } // if (event.keyboard.keycode == config["ultimate"] or event.keyboard.keycode == config["ultimate_alt1"] or event.keyboard.keycode == config["ultimate_alt2"]) // { // pressed_keys->ULTIMATE = true; // } // if (event.keyboard.keycode == config["reload"] or event.keyboard.keycode == config["reload_alt1"] or event.keyboard.keycode == config["reload_alt2"]) // { // pressed_keys->RELOAD = true; // } // switch (event.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: // Exit game throw 0; } // // case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: // switch (event.mouse.button) // { // case LEFT_MOUSE_BUTTON: // pressed_keys->PRIMARY_FIRE = true; // break; // case RIGHT_MOUSE_BUTTON: // pressed_keys->SECONDARY_FIRE = true; // break; // } } } ALLEGRO_KEYBOARD_STATE keystate; al_get_keyboard_state(&keystate); if (al_key_down(&keystate, config["jump"]) or al_key_down(&keystate, config["jump_alt1"]) or al_key_down(&keystate, config["jump_alt2"])) { held_keys->JUMP = true; } if (al_key_down(&keystate, config["crouch"]) or al_key_down(&keystate, config["crouch_alt1"]) or al_key_down(&keystate, config["crouch_alt2"])) { held_keys->CROUCH = true; } if (al_key_down(&keystate, config["left"]) or al_key_down(&keystate, config["left_alt1"]) or al_key_down(&keystate, config["left_alt2"])) { held_keys->LEFT = true; } if (al_key_down(&keystate, config["right"]) or al_key_down(&keystate, config["right_alt1"]) or al_key_down(&keystate, config["right_alt2"])) { held_keys->RIGHT = true; } if (al_key_down(&keystate, config["ability1"]) or al_key_down(&keystate, config["ability1_alt1"]) or al_key_down(&keystate, config["ability1_alt2"])) { held_keys->ABILITY_1 = true; } if (al_key_down(&keystate, config["ability2"]) or al_key_down(&keystate, config["ability2_alt1"]) or al_key_down(&keystate, config["ability2_alt2"])) { held_keys->ABILITY_2 = true; } if (al_key_down(&keystate, config["ultimate"]) or al_key_down(&keystate, config["ultimate_alt1"]) or al_key_down(&keystate, config["ultimate_alt2"])) { held_keys->ULTIMATE = true; } if (al_key_down(&keystate, config["reload"]) or al_key_down(&keystate, config["reload_alt1"]) or al_key_down(&keystate, config["reload_alt2"])) { held_keys->RELOAD = true; } ALLEGRO_MOUSE_STATE mousestate; al_get_mouse_state(&mousestate); // FIXME: I have no idea if these constants are correct, allegro docs don't mention the specifics, just that it starts with 1. if (mousestate.buttons & LEFT_MOUSE_BUTTON) { held_keys->PRIMARY_FIRE = true; } if (mousestate.buttons & RIGHT_MOUSE_BUTTON) { held_keys->SECONDARY_FIRE = true; } *mouse_x = mousestate.x; *mouse_y = mousestate.y; }
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"); } open_log(); 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); if (!display[0]) { abort_example("Error creating display\n"); } pictures[0] = al_load_bitmap("data/mysha.pcx"); if (!pictures[0]) { abort_example("failed to load mysha.pcx\n"); } display[1] = al_create_display(W, H); if (!display[1]) { abort_example("Error creating display\n"); } pictures[1] = al_load_bitmap("data/allegro.pcx"); if (!pictures[1]) { abort_example("failed to load allegro.pcx\n"); } /* 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) { log_printf("%p switching in\n", event.display.source); } if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) { log_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]); al_destroy_display(display[0]); al_destroy_display(display[1]); close_log(true); return 0; }