Beispiel #1
0
void
set_system_mouse_cursor (ALLEGRO_SYSTEM_MOUSE_CURSOR id)
{
  if (! al_set_system_mouse_cursor (display, id))
    error (0, 0, "%s (%i): cannot set system mouse cursor",
           __func__, id);
}
static void ImGui_ImplAllegro5_UpdateMouseCursor()
{
    ImGuiIO& io = ImGui::GetIO();
    if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
        return;

    ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
    if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
    {
        // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
        al_set_mouse_cursor(g_Display, g_MouseCursorInvisible);
    }
    else
    {
        ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
        switch (imgui_cursor)
        {
        case ImGuiMouseCursor_TextInput:    cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break;
        case ImGuiMouseCursor_ResizeAll:    cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break;
        case ImGuiMouseCursor_ResizeNS:     cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break;
        case ImGuiMouseCursor_ResizeEW:     cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break;
        case ImGuiMouseCursor_ResizeNESW:   cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break;
        case ImGuiMouseCursor_ResizeNWSE:   cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break;
        }
        al_set_system_mouse_cursor(g_Display, cursor_id);
    }
}
Beispiel #3
0
/*
 * keyboard driver is called first, so initialize allegro here 
 */
int init_allegro(void){

    if(!al_init())
    {
        fprintf(stderr,"Error!, Allegro has failed to initialize.\n");
        return 0;
    }  else {
        //fprintf(stderr,"al_init successful\n");
    }

    //al_set_new_display_option(ALLEGRO_CAN_DRAW_INTO_BITMAP,1,ALLEGRO_REQUIRE);
    display = al_create_display(800, 600);
    if(display == NULL)
    {
        fprintf(stderr,"Error!, Failed to create the display.");
        return 0;
    }    
    al_set_system_mouse_cursor(display,ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT);

    if(!al_install_keyboard())
    {
        fprintf(stderr,"Error!, Failed to install keyboard.\n");
        return 0;
    }  else {
        //fprintf(stderr,"al_install_keyboard successful\n");
    }   

    if(!al_install_mouse())
    {
        fprintf(stderr,"Error!, Failed to install mouse.");
        return 0;
    }    

    a_event_queue_k = al_create_event_queue();
    if(a_event_queue_k == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the keyboard event queue.");
       return 0;
    }    
    al_register_event_source(a_event_queue_k, al_get_keyboard_event_source());

    a_event_queue_m = al_create_event_queue();
    if(a_event_queue_m == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the mouse event queue.");
       return 0;
    }    
    al_register_event_source(a_event_queue_m, al_get_mouse_event_source());

    a_event_queue_d = al_create_event_queue();
    if(a_event_queue_d == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the display event queue.");
       return 0;
    }    
    al_register_event_source(a_event_queue_d, al_get_display_event_source(display));

    return 1; //ok    
}
Beispiel #4
0
int init_fail(ALLEGRO_DISPLAY *janela, ALLEGRO_FONT *fonte, ALLEGRO_EVENT_QUEUE *fila_eventos, ALLEGRO_BITMAP *imagem, ALLEGRO_TIMER *timer, ALLEGRO_BITMAP *trilha)
{
    if (!al_init())
    {
        fprintf(stderr, "Falha ao inicializar a Allegro.\n");
        return -1;
    }
    if (!al_install_mouse())
    {
        fprintf(stderr, "Falha ao inicializar o mouse.\n");
        al_destroy_display(janela);
        return -1;
    }
    if (!janela)
    {
        fprintf(stderr, "Falha ao criar janela.\n");
        return -1;
    }
    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;
    }
    if (!imagem)
    {
        fprintf(stderr, "Falha ao carregar imagem.\n");
        al_destroy_bitmap(imagem);
        al_destroy_display(janela);
        return -1;
    }
    if (!trilha)
    {
        fprintf(stderr, "Falha ao carregar imagem da trilha.\n");
        al_destroy_bitmap(trilha);
        al_destroy_display(janela);
        return -1;
    }
    if (!fila_eventos)
    {
        fprintf(stderr, "Falha ao inicializar o fila de eventos.\n");
        al_destroy_display(janela);
        return -1;
    }
    if(!fonte)
    {
        fprintf(stderr, "Falha ao inicializar a fonte.\n");
        al_destroy_font(fonte);
        al_destroy_display(janela);
        return -1;
    }
    if(!timer)
    {
        fprintf(stderr, "Falha ao inicializar o timer.\n");
        al_destroy_timer(timer);
        al_destroy_display(janela);
        return -1;
    }
}
Beispiel #5
0
void ponteiroMouse (ALLEGRO_DISPLAY *janela, telaAlternativa *posicaoAlt){

    if (posicaoAlt->mousePoderes_x > posicaoAlt->alt_Ax -  90  /* 765 */ && 
        posicaoAlt->mousePoderes_x < posicaoAlt->alt_Ax +  70  /* 878 */ &&
        posicaoAlt->mousePoderes_y > posicaoAlt->alt_Ay -  0  /* 172 */  &&
        posicaoAlt->mousePoderes_y < posicaoAlt->alt_Ay +  100  /* 266 */){

            al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT);
            posicaoAlt->altCorA = 100;

    }

    else if (posicaoAlt->mousePoderes_x > posicaoAlt->alt_Bx -  90  && 
             posicaoAlt->mousePoderes_x < posicaoAlt->alt_Bx +  70  &&
             posicaoAlt->mousePoderes_y > posicaoAlt->alt_By -  0   &&
             posicaoAlt->mousePoderes_y < posicaoAlt->alt_By +  100 ){

           al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT);
           posicaoAlt->altCorB = 100;

    }

    else if (posicaoAlt->mousePoderes_x > posicaoAlt->alt_Cx -  90  && 
             posicaoAlt->mousePoderes_x < posicaoAlt->alt_Cx +  70  &&
             posicaoAlt->mousePoderes_y > posicaoAlt->alt_Cy -  0   &&
             posicaoAlt->mousePoderes_y < posicaoAlt->alt_Cy +  100 ){
         
           al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT);
           posicaoAlt->altCorC = 100;

    }

    else{
        al_set_system_mouse_cursor(janela, ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT);
        posicaoAlt->altCorA = 245;
        posicaoAlt->altCorB = 245;
        posicaoAlt->altCorC = 245;
    }


}
Beispiel #6
0
void Mouse::Update() {

    _prevState = _curState;
    al_get_mouse_state(&_curState);

    if(_cursor) {
        al_set_mouse_cursor(_parent_display, _cursor);
    } else {
        al_set_system_mouse_cursor(_parent_display, _cursor_id);
    }
    _isVisible ?  al_show_mouse_cursor(_parent_display) : al_hide_mouse_cursor(_parent_display);
}
Beispiel #7
0
    v8::Handle<v8::Value> setCursor(const v8::Arguments& args) {

        ALLEGRO_SYSTEM_MOUSE_CURSOR cursor = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
        if (args.Length() > 0) {
            cursor = (ALLEGRO_SYSTEM_MOUSE_CURSOR)ToInt32(args[0]);
        }

        if (al_set_system_mouse_cursor(Game::allegro.display, cursor)) {
            return v8::True();
                
        } else {
            return v8::False();
        }

    }
Beispiel #8
0
void Mouse::SetImage(Sprite* image, int focusX, int focusY) {
    if(_image) {
        al_destroy_mouse_cursor(_cursor);
    }
    if(image) {
        delete _image;
        _image = nullptr;
        _image = Sprite::CreateSprite(*image);
    } else {
        focusX = 0;
        focusY = 0;
    }
    _focus = Vector2D(focusX, focusY);
    if(image) {
        _cursor = al_create_mouse_cursor(_image->GetImage(), _focus.GetX(), _focus.GetY());
    } else {
        al_set_system_mouse_cursor(_parent_display, _cursor_id);
    }
}
void ImGui_ImplA5_NewFrame()
{
    if (!g_Texture) 
        Imgui_ImplA5_CreateDeviceObjects();

    ImGuiIO &io = ImGui::GetIO();

    // Setup display size (every frame to accommodate for window resizing)
    int w, h;
    w = al_get_display_width(g_Display);
    h = al_get_display_height(g_Display);
    io.DisplaySize = ImVec2((float)w, (float)h);

    // Setup time step
    double current_time = al_get_time();
    io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
    g_Time = current_time;

    // Setup inputs
    ALLEGRO_KEYBOARD_STATE keys;
    al_get_keyboard_state(&keys);
    io.KeyCtrl = al_key_down(&keys, ALLEGRO_KEY_LCTRL) || al_key_down(&keys, ALLEGRO_KEY_RCTRL);
    io.KeyShift = al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT);
    io.KeyAlt = al_key_down(&keys, ALLEGRO_KEY_ALT) || al_key_down(&keys, ALLEGRO_KEY_ALTGR);

    ALLEGRO_MOUSE_STATE mouse;
    if (keys.display == g_Display) 
    {
        al_get_mouse_state(&mouse);
        io.MousePos = ImVec2((float)mouse.x, (float)mouse.y);
    }
    else 
    {
        io.MousePos = ImVec2(-1, -1);
    }

    al_get_mouse_state(&mouse);
    io.MouseDown[0] = mouse.buttons & (1 << 0);
    io.MouseDown[1] = mouse.buttons & (1 << 1);
    io.MouseDown[2] = mouse.buttons & (1 << 2);

    // Hide OS mouse cursor if ImGui is drawing it
    if (io.MouseDrawCursor)
    {
        al_set_mouse_cursor(g_Display, g_MouseCursorInvisible);
    }
    else
    {
        ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT;
        switch (ImGui::GetMouseCursor())
        {
        case ImGuiMouseCursor_TextInput:    cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT; break;
        case ImGuiMouseCursor_Move:         cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE; break;
        case ImGuiMouseCursor_ResizeNS:     cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N; break;
        case ImGuiMouseCursor_ResizeEW:     cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E; break;
        case ImGuiMouseCursor_ResizeNESW:   cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE; break;
        case ImGuiMouseCursor_ResizeNWSE:   cursor_id = ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW; break;
        }
        al_set_system_mouse_cursor(g_Display, cursor_id);
    }

    // Start the frame
    ImGui::NewFrame();
}
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));

}
Beispiel #11
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;
}
/** Sets cursor to systems mouse cursor */
void cbeSetSystemCursor(CBEnchanted *cb) {
	int32_t t = cb->popValue().toInt();
	bool success = false;
	cb->inputInterface->setCustomCursor(0);
	cb->inputInterface->setCursorVisible(true);
	switch (t) {
		case 2: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_ARROW); break;
		case 3: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_BUSY); break;
		case 4: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_QUESTION); break;
		case 5: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT); break;
		case 6: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE); break;
		case 7: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N); break;
		case 8: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_W); break;
		case 9: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_S); break;
		case 10: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E); break;
		case 11: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW); break;
		case 12: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SW); break;
		case 13: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SE); break;
		case 14: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE); break;
		case 15: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_PROGRESS); break;
		case 16: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION); break;
		case 17: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_LINK); break;
		case 18: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT); break;
		case 19: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE); break;
		default: success = al_set_system_mouse_cursor(cb->gfxInterface->getWindow(), ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT); break;
	}
	cb->pushValue((int32_t)success);
}
/*
 * keyboard driver is called first, so initialize allegro here 
 */
int init_allegro(void){

    if(!al_init())
    {
#if _ANDROID_
	__android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Allegro has failed to initialize.");	
#else      
        fprintf(stderr,"Error!, Allegro has failed to initialize.\n");
#endif
	return -1;
    }  else {
        //fprintf(stderr,"al_init successful\n");
    }
#if _ANDROID_    
    //al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
    al_set_new_display_flags(ALLEGRO_WINDOWED);
    //al_set_new_display_option(ALLEGRO_COLOR_SIZE,32,ALLEGRO_SUGGEST);
    //al_set_new_display_option(ALLEGRO_CAN_DRAW_INTO_BITMAP,1,ALLEGRO_REQUIRE);
#endif  
    //al_set_new_display_option(ALLEGRO_COLOR_SIZE,32,ALLEGRO_SUGGEST);
    display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
    //display = al_create_display(800, 600);

    if(display == NULL)
    {
#if _ANDROID_
	__android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Failed to create the display.");	
#else      
        fprintf(stderr,"Error!, Failed to create the display.");
#endif
	return -1;
    }    
    al_set_system_mouse_cursor(display,ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT);

    if(!al_install_keyboard())
    {
        fprintf(stderr,"Error!, Failed to install keyboard.\n");
        return -1;
    }  else {
        //fprintf(stderr,"al_install_keyboard successful\n");
    }   

    if(!al_install_mouse())
    {
        fprintf(stderr,"Error!, Failed to install mouse.");
        return -1;
    }    

    a_event_queue_k = al_create_event_queue();
    if(a_event_queue_k == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the keyboard event queue.");
       return -1;
    }    
    al_register_event_source(a_event_queue_k, al_get_keyboard_event_source());

    a_event_queue_m = al_create_event_queue();
    if(a_event_queue_m == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the mouse event queue.");
       return -1;
    }    
#if _ANDROID_    
//do below at touch input
#else
    al_register_event_source(a_event_queue_m, al_get_mouse_event_source());
#endif
    a_event_queue_d = al_create_event_queue();
    if(a_event_queue_d == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the display event queue.");
       return -1;
    }    
    al_register_event_source(a_event_queue_d, al_get_display_event_source(display));

#if _ANDROID_
    if(!al_install_touch_input())
    {
      __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Failed to install touch_input.");	
    } else {
      __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Installed touch_input successfully.");	
      al_set_mouse_emulation_mode(ALLEGRO_MOUSE_EMULATION_5_0_x);
      
      al_register_event_source(a_event_queue_m, al_get_touch_input_mouse_emulation_event_source());
    }
#endif 

#if _ANDROID_
//allow to use al_open to read (only) files from the apk store, e.g. fonts
    al_android_set_apk_file_interface();
#endif

    return 1; //ok    
}
Beispiel #14
0
int main(int argc, char const *argv[])
{
    int n_hordas = 0;             //Numero de hordas chamadas
    bool nova_horda = true;       //Chama nova horda

    bool torre_mouse = false;     //Se a torre está no mouse
    bool info_torre = false;      //Chama a funçao de informaçoes da torre
    bool compra_torre = false;    //Exibe as informaçoes da torre a ser comprada
    bool upgrade_torre;           //Guarda os upgrades da torre]

    int tower_posx = 0;           //Posiçao x de determinada torre
    int tower_posy = 0;           //Posiçao y de determinada torre
    int torre_ID;                 //Identifica as torres
    int t = 0;                    //Contagem das torres
    int t_1, t_2;                 //Contagem para disparo

    int r;                        //Variável para colunas
    int l;                        //Variável para linhas
    bool render = false;          //Renderizaçao

    int resposta = 0;             //Resposta se os monstros estão todos mortos

    int gamestate = 0;            //Gamestates

    //Setup inicial
    Sistema sistema;

    Monstro monstro[tipos_monstros][n_monstros];

    Tipo tipo_torre;
    Tipo tipo1;
    Tipo tipo2;
    Tipo upgrade1_torre1;
    Torre torre[100];

    //Declaracao vairaveis 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
    ALLEGRO_BITMAP *trilha = NULL;
    ALLEGRO_BITMAP *fundao = NULL;
    ALLEGRO_BITMAP *spawn = NULL;
    ALLEGRO_BITMAP *the_end = NULL;
    ALLEGRO_BITMAP *monstro2 = NULL;
    ALLEGRO_BITMAP *torre1 = NULL;
    ALLEGRO_FONT *fonte40 = NULL;

    //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();

    //Setup inicial do sistema, monstros e torres
    init_horda(monstro, n_monstros, n_hordas, tipos_monstros);
    init_system(sistema);

    setup_torre1(tipo1);
    setup_torre2(tipo2);

    upgrade1_tower1(upgrade1_torre1);

    //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.jpg");
    trilha = al_load_bitmap("fundoc.jpg");
    fundao = al_load_bitmap("fundod.jpg");
    spawn = al_load_bitmap("spawn.jpg");
    the_end = al_load_bitmap("the end.jpg");
    monstro2 = al_load_bitmap("virus2.jpg");
    torre1 = al_load_bitmap("halter.png");
    timer = al_create_timer(1.0 / fps);
    fonte = al_load_font("arial.ttf", 12, 0);
    fonte40 = al_load_font("arial.ttf", 40, 0);

    //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, trilha); //Fun�ao de teste de inicializaçao do allegro

    //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);       //Espera por eventos

        if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            GameOver = true;
        }

        switch(gamestate)
        {
        case 0:  //Menu inicial
        {
            if(evento.type == ALLEGRO_EVENT_TIMER)
            {
                render = true;
            }
            if(evento.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                switch(evento.keyboard.keycode)
                {
                case ALLEGRO_KEY_ENTER:
                    gamestate = 1;
                    break;
                case ALLEGRO_KEY_BACKSPACE:
                    gamestate = 2;
                    break;
                }
            }
            break;
        }

        case 1:  //Jogo
        {
            if(evento.type == ALLEGRO_EVENT_TIMER)
            {
                if(info_torre)
                {
                    if (torre[torre_ID].upgrade == 0 && sistema.money >= 60)
                    {
                        mapa[25][30] = 12;
                    }
                }
                if(!info_torre)
                {
                    mapa[25][30] = 0;
                }

                for(int j = 0; j < t; j++)  //Loop para o disparo das torres
                {
                    if(torre[j].n == 1)
                    {
                        if(t_1 >= fps*(torre[j].fire_rate))
                        {
                            fire_tiro(torre, monstro, t, n_monstros, tipos_monstros); //Dispara tiros
                            t_1 = 0;
                        }
                    }

                    if(torre[j].n == 2)
                    {
                        if(t_2 >= fps*(torre[j].fire_rate))
                        {
                            fire_tiro(torre, monstro, t, n_monstros, tipos_monstros); //Dispara tiros
                            t_2 = 0;
                        }
                    }
                }

                update_horda(monstro, sistema, mapa, n_monstros, tipos_monstros);
                update_tiro(torre, monstro, t, n_monstros, tipos_monstros);
                colisao_horda(torre, monstro, t, n_monstros, sistema, &resposta, tipos_monstros);

                t_1++;
                t_2++;
                render = true;
                if(sistema.lives <= 0)
                    gamestate = 2;
            }



            else if(evento.type == ALLEGRO_EVENT_MOUSE_AXES)
            {
                pos_x = evento.mouse.x; //Armazena a posiçao x do mouse
                pos_y = evento.mouse.y; //Armazena a posiçao y do mouse

                r = pos_x/l_celula; // Atribui uma celula de coluna
                l = pos_y/a_celula; // Atribui uma celula de linha
            }

            else if(evento.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
            {
                switch (mapa[l][r])
                {
                case 10:
                    info_torre = false;
                    compra_torre = true;
                    tipo_torre = tipo1;
                    if(sistema.money >= tipo_torre.price && evento.mouse.button & 1)
                        torre_mouse = true;
                    break;
                case 20:
                    info_torre = false;
                    compra_torre = true;
                    tipo_torre = tipo2;
                    if(sistema.money >= tipo_torre.price && evento.mouse.button & 1)
                        torre_mouse = true;
                    break;
                case 11:
                    torre_ID = find_tower_ID(torre, t, r, l);
                    info_torre = true;
                    break;
                case 12:
                    torre_ID = find_tower_ID(torre, t, r, l);
                    info_torre = true;
                    break;
                case 21:
                    torre_ID = find_tower_ID(torre, t, r, l);
                    info_torre = true;
                    break;
                default:
                    info_torre = false;
                }

                if(torre_mouse && (mapa[l][r] == 0 || mapa[l][r] == 5) && evento.mouse.button & 1) //Posicionamento da torre enquanto ela estiver no mouse
                {
                    setup_tower(torre, tipo_torre, t, r, l);
                    sistema.money -= tipo_torre.price;      //Pagamento da torre
                    torre_mouse = false;
                    compra_torre = false;
                    t++;
                }

                if(torre_mouse && evento.mouse.button & 2)  //Cancela compra
                {
                    torre_mouse = false;
                    compra_torre = false;
                }

                if(compra_torre && mapa[l][r] != 10 && mapa[l][r] != 20)  //Termina a exibiçao da torre a ser comprada
                {
                    compra_torre = false;
                }

                if(info_torre && mapa[l][r] == 12 )
                {
                    sistema.money -= 60;
                    torre_ID = find_tower_ID(torre, t, r, l);
                    upgrade_tower(torre, upgrade1_torre1, torre_ID);
                    //upgrade_torre = true;
                }
            }

            else if(evento.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                printf("resposta = %d\n", resposta);
                if(resposta == 1 || n_hordas == 0)
                {
                    switch(evento.keyboard.keycode)
                    {
                    case ALLEGRO_KEY_SPACE: //Inicializa uma nova horda
                        start_horda(monstro, n_monstros, n_hordas, tipos_monstros);
                        n_hordas++;
                        break;
                    }
                }
            }
        }
        break;

    case 2: //Fim de jogo
        {
            if(evento.type == ALLEGRO_EVENT_TIMER)
            {
                render = true;
            }
            if(evento.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                switch(evento.keyboard.keycode)
                {
                case ALLEGRO_KEY_R:
                    init_system(sistema);
                    init_horda(monstro, n_monstros, n_hordas, tipos_monstros);
                    restart_tower(torre, t);
                    n_hordas = 0;
                    setup_array(mapa);
                    gamestate = 1;
                    break;
                case ALLEGRO_KEY_ESCAPE:
                    GameOver = true;
                    break;
                }
            }
            break;
        }
    }

    if(render && al_is_event_queue_empty(fila_eventos))
    {
        render = false;

        if(gamestate == 0)
        {
            al_clear_to_color(al_map_rgb(255,255,255));
            al_draw_textf(fonte, al_map_rgb(0, 0, 255), LARGURA_TELA/2, (ALTURA_TELA/2) - 20, 0, "Pressione ENTER para Jogar");
            al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) + 20, 0, "Pressione BACKSPACE para Sair");
        }
        if(gamestate == 1)
        {
            al_clear_to_color(al_map_rgb(61, 10, 10));

            draw_background(mapa, fonte, trilha, fundao, spawn, the_end); //Desenha o plano de fundo
            draw_towers(mapa, sistema, fonte, the_end, torre1); //Desenha as torres

            al_draw_textf(fonte, al_map_rgb(255, 255, 255), 900, 15, ALLEGRO_ALIGN_LEFT, "Vidas do sistema %i", sistema.lives);
            al_draw_textf(fonte, al_map_rgb(255, 255, 255), 900, 35, ALLEGRO_ALIGN_LEFT, "Bitcoins %.2f", sistema.money);
            al_draw_textf(fonte, al_map_rgb(255, 255, 255), 100, 15, ALLEGRO_ALIGN_LEFT, "Monstros mortos: %i  Wave: %i", sistema.score, n_hordas);
            /*
            Mouse debug     al_draw_textf(fonte, al_map_rgb(0, 0, 0), pos_x, pos_y, ALLEGRO_ALIGN_LEFT, "l:%i r:%i", l, r);
                            al_draw_textf(fonte, al_map_rgb(0, 0, 0), pos_x, pos_y + 15, ALLEGRO_ALIGN_CENTRE, "mapa[l][r]: %i", mapa[l][r]);
            */
            draw_horda(monstro, n_monstros, imagem, tipos_monstros, monstro2); //Desenha os montros

            if(torre_mouse)
            {
                draw_mouse_tower(r, l, tipo_torre); //Desenha a torre somente enquanto ela estiver no mouse
            }
            if(info_torre)
            {
                show_tower_information(torre, torre_ID, fonte); //info torres
                if (mapa[25][30] == 12)
                {
                    al_draw_filled_circle(25 * l_celula + (l_celula/2), 30 * a_celula + (a_celula/2), l_celula/2, al_map_rgb(40, 150, 10));
                }
            }
            if(compra_torre)
            {
                buy_tower(tipo_torre, fonte); //Exibe as informaçoes da torre a ser comprada
            }

            draw_tiro(torre, t); //Desenha os tiros
        }
        if(gamestate == 2)
        {
            al_clear_to_color(al_map_rgb(255,255,255));
            al_draw_textf(fonte40, al_map_rgb(255, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) - 100, 0, "Game Over");
            al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) - 20, 0, "Pressione R para Jogar Novamente");
            al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA/2, (ALTURA_TELA/2) + 20, 0, "Pressione ESC para Sair");
        }

        al_flip_display();
    }
}

destroy_al(janela, fonte, fila_eventos, imagem, timer); //Destroi as vari�veis allegro

return 0;
}