-
-
Save rafaeltoledo/6e157f233e94168d1af0fb2ee988cc19 to your computer and use it in GitHub Desktop.
Allegro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inclui o arquivo de cabeçalho da biblioteca Allegro 5 | |
#include <allegro5/allegro.h> | |
// Nossa conhecida função main... | |
int main(void) | |
{ | |
// Variável representando a janela principal | |
ALLEGRO_DISPLAY *janela = NULL; | |
// Inicializamos a biblioteca | |
al_init(); | |
// Criamos a nossa janela - dimensões de 640x480 px | |
janela = al_create_display(640, 480); | |
// Preenchemos a janela de branco | |
al_clear_to_color(al_map_rgb(255, 255, 255)); | |
// Atualiza a tela | |
al_flip_display(); | |
// Segura a execução por 10 segundos | |
al_rest(10.0); | |
// Finaliza a janela | |
al_destroy_display(janela); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inclui o cabeçalho da bibilioteca Allegro 5 | |
#include <allegro5/allegro.h> | |
// Inclui o cabeçalho do add-on para uso de imagens | |
#include <allegro5/allegro_image.h> | |
int main(void) | |
{ | |
// Variável representando a janela principal | |
ALLEGRO_DISPLAY *janela = NULL; | |
// Variável representando a imagem | |
ALLEGRO_BITMAP *imagem = NULL; | |
// Inicializa a Allegro | |
al_init(); | |
// Inicializa o add-on para utilização de imagens | |
al_init_image_addon(); | |
// Configura a janela | |
janela = al_create_display(640, 480); | |
// Carrega a imagem | |
imagem = al_load_bitmap("sunshine.jpg"); | |
// Desenha a imagem na tela | |
al_draw_bitmap(imagem, 0, 0, 0); | |
// Atualiza a tela | |
al_flip_display(); | |
// Segura a execução por 10 segundos | |
al_rest(10.0); | |
// Finaliza a janela | |
al_destroy_display(janela); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Os arquivos de cabeçalho | |
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_image.h> | |
// Para utilizarmos o fprintf | |
#include <stdio.h> | |
// Atributos da tela | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
int main(void) | |
{ | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_BITMAP *imagem = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar a Allegro.\n"); | |
return -1; | |
} | |
if (!al_init_image_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar add-on allegro_image.\n"); | |
return -1; | |
} | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar janela.\n"); | |
return -1; | |
} | |
imagem = al_load_bitmap("imagem.png"); | |
if (!imagem) | |
{ | |
fprintf(stderr, "Falha ao carregar o arquivo de imagem.\n"); | |
al_destroy_display(janela); | |
return -1; | |
} | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
al_destroy_display(janela); | |
return -1; | |
} | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
al_draw_bitmap(imagem, 0, 0, 0); | |
al_flip_display(); | |
while (1) | |
{ | |
ALLEGRO_EVENT evento; | |
ALLEGRO_TIMEOUT timeout; | |
al_init_timeout(&timeout, 0.05); | |
int tem_eventos = al_wait_for_event_until(fila_eventos, &evento, &timeout); | |
if (tem_eventos && evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
break; | |
} | |
al_draw_bitmap(imagem, 0, 0, 0); | |
al_flip_display(); | |
} | |
al_destroy_display(janela); | |
al_destroy_event_queue(fila_eventos); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Os arquivos de cabeçalho | |
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_font.h> | |
#include <allegro5/allegro_ttf.h> | |
// Para utilizarmos a função fprintf | |
#include <stdio.h> | |
// Atributos da tela | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
int main(void) | |
{ | |
// A nossa janela | |
ALLEGRO_DISPLAY *janela = NULL; | |
// O nosso arquivo de fonte | |
ALLEGRO_FONT *fonte = NULL; | |
// Inicialização da biblioteca Allegro | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar a Allegro.\n"); | |
return -1; | |
} | |
// 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; | |
} | |
// Criação da nossa janela | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar janela.\n"); | |
return -1; | |
} | |
// Carregando o arquivo de fonte | |
fonte = al_load_font("BRADHITC.TTF", 48, 0); | |
if (!fonte) | |
{ | |
al_destroy_display(janela); | |
fprintf(stderr, "Falha ao carregar fonte.\n"); | |
return -1; | |
} | |
// Preenchemos a tela com a cor branca | |
al_clear_to_color(al_map_rgb(255, 255, 255)); | |
// Texto alinhado à esquerda | |
al_draw_text(fonte, al_map_rgb(255, 0, 0), 10, 10, ALLEGRO_ALIGN_LEFT, "Esquerda"); | |
// Texto alinhado à direita | |
al_draw_text(fonte, al_map_rgb(0, 255, 0), LARGURA_TELA - 10, 50, ALLEGRO_ALIGN_RIGHT, "Direita"); | |
// Texto centralizado | |
al_draw_text(fonte, al_map_rgb(0, 0, 255), LARGURA_TELA / 2, 90, ALLEGRO_ALIGN_CENTRE, "Centro"); | |
// Exemplo de impressão de valores variáveis | |
int i = 2; | |
char *texto = "Uma string qualquer"; | |
al_draw_textf(fonte, al_map_rgb(0, 0, 0), LARGURA_TELA / 2, 250, ALLEGRO_ALIGN_CENTRE, "Teste %d - %s", i, texto); | |
// Atualizamos a tela | |
al_flip_display(); | |
// E aguardamos 10 segundos | |
al_rest(10.0); | |
// Desalocação da fonte e da janela | |
al_destroy_font(fonte); | |
al_destroy_display(janela); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Os arquivos de cabeçalho | |
#include <allegro5/allegro.h> | |
#include <stdio.h> | |
// Atributos da tela | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
int main(void) | |
{ | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
ALLEGRO_BITMAP *botao_sair = NULL, *area_central = 0; | |
// Flag que condicionará nosso looping | |
int sair = 0; | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar a Allegro.\n"); | |
return -1; | |
} | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar janela.\n"); | |
return -1; | |
} | |
// Configura o título da janela | |
al_set_window_title(janela, "Rotinas de Mouse - www.rafaeltoledo.net"); | |
// 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; | |
} | |
// Alocamos o retângulo central da tela | |
area_central = al_create_bitmap(LARGURA_TELA / 2, ALTURA_TELA / 2); | |
if (!area_central) | |
{ | |
fprintf(stderr, "Falha ao criar bitmap.\n"); | |
al_destroy_display(janela); | |
return -1; | |
} | |
// Alocamos o botão para fechar a aplicação | |
botao_sair = al_create_bitmap(100, 50); | |
if (!botao_sair) | |
{ | |
fprintf(stderr, "Falha ao criar botão de saída.\n"); | |
al_destroy_bitmap(area_central); | |
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; | |
} | |
// Dizemos que vamos tratar os eventos vindos do mouse | |
al_register_event_source(fila_eventos, al_get_mouse_event_source()); | |
// Flag indicando se o mouse está sobre o retângulo central | |
int na_area_central = 0; | |
while (!sair) | |
{ | |
// Verificamos se há eventos na fila | |
while (!al_is_event_queue_empty(fila_eventos)) | |
{ | |
ALLEGRO_EVENT evento; | |
al_wait_for_event(fila_eventos, &evento); | |
// Se o evento foi de movimentação do mouse | |
if (evento.type == ALLEGRO_EVENT_MOUSE_AXES) | |
{ | |
// Verificamos se ele está sobre a região do retângulo central | |
if (evento.mouse.x >= LARGURA_TELA / 2 - al_get_bitmap_width(area_central) / 2 && | |
evento.mouse.x <= LARGURA_TELA / 2 + al_get_bitmap_width(area_central) / 2 && | |
evento.mouse.y >= ALTURA_TELA / 2 - al_get_bitmap_height(area_central) / 2 && | |
evento.mouse.y <= ALTURA_TELA / 2 + al_get_bitmap_height(area_central) / 2) | |
{ | |
na_area_central = 1; | |
} | |
else | |
{ | |
na_area_central = 0; | |
} | |
} | |
// Ou se o evento foi um clique do mouse | |
else if (evento.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) | |
{ | |
if (evento.mouse.x >= LARGURA_TELA - al_get_bitmap_width(botao_sair) - 10 && | |
evento.mouse.x <= LARGURA_TELA - 10 && evento.mouse.y <= ALTURA_TELA - 10 && | |
evento.mouse.y >= ALTURA_TELA - al_get_bitmap_height(botao_sair) - 10) | |
{ | |
sair = 1; | |
} | |
} | |
} | |
// Limpamos a tela | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
// Colorimos o bitmap correspondente ao retângulo central, | |
// com a cor condicionada ao conteúdo da flag na_area_central | |
al_set_target_bitmap(area_central); | |
if (!na_area_central) | |
{ | |
al_clear_to_color(al_map_rgb(255, 255, 255)); | |
} | |
else | |
{ | |
al_clear_to_color(al_map_rgb(0, 255, 0)); | |
} | |
// Colorimos o bitmap do botão de sair | |
al_set_target_bitmap(botao_sair); | |
al_clear_to_color(al_map_rgb(255, 0, 0)); | |
// Desenhamos os retângulos na tela | |
al_set_target_bitmap(al_get_backbuffer(janela)); | |
al_draw_bitmap(area_central, LARGURA_TELA / 2 - al_get_bitmap_width(area_central) / 2, | |
ALTURA_TELA / 2 - al_get_bitmap_height(area_central) / 2, 0); | |
al_draw_bitmap(botao_sair, LARGURA_TELA - al_get_bitmap_width(botao_sair) - 10, | |
ALTURA_TELA - al_get_bitmap_height(botao_sair) - 10, 0); | |
// Atualiza a tela | |
al_flip_display(); | |
} | |
// Desaloca os recursos utilizados na aplicação | |
al_destroy_bitmap(botao_sair); | |
al_destroy_bitmap(area_central); | |
al_destroy_display(janela); | |
al_destroy_event_queue(fila_eventos); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_font.h> | |
#include <allegro5/allegro_ttf.h> | |
#include <allegro5/allegro_image.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
ALLEGRO_BITMAP *fundo = NULL; | |
ALLEGRO_FONT *fonte = NULL; | |
bool inicializar(); | |
int main(void) | |
{ | |
bool sair = false; | |
int tecla = 0; | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
al_draw_bitmap(fundo, 0, 0, 0); | |
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_UP: | |
tecla = 1; | |
break; | |
case ALLEGRO_KEY_DOWN: | |
tecla = 2; | |
break; | |
case ALLEGRO_KEY_LEFT: | |
tecla = 3; | |
break; | |
case ALLEGRO_KEY_RIGHT: | |
tecla = 4; | |
break; | |
} | |
} | |
else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
if (tecla) | |
{ | |
al_draw_bitmap(fundo, 0, 0, 0); | |
switch (tecla) | |
{ | |
case 1: | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
ALTURA_TELA / 2 - al_get_font_ascent(fonte) / 2, | |
ALLEGRO_ALIGN_CENTRE, "Seta para cima"); | |
break; | |
case 2: | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
ALTURA_TELA / 2 - al_get_font_ascent(fonte) / 2, | |
ALLEGRO_ALIGN_CENTRE, "Seta para baixo"); | |
break; | |
case 3: | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
ALTURA_TELA / 2 - al_get_font_ascent(fonte) / 2, | |
ALLEGRO_ALIGN_CENTRE, "Seta esquerda"); | |
break; | |
case 4: | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
ALTURA_TELA / 2 - al_get_font_ascent(fonte) / 2, | |
ALLEGRO_ALIGN_CENTRE, "Seta direita"); | |
break; | |
} | |
tecla = 0; | |
} | |
al_flip_display(); | |
} | |
al_destroy_display(janela); | |
al_destroy_event_queue(fila_eventos); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
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"); | |
fonte = al_load_font("comic.ttf", 72, 0); | |
if (!fonte) | |
{ | |
fprintf(stderr, "Falha ao carregar \"fonte comic.ttf\".\n"); | |
al_destroy_display(janela); | |
return false; | |
} | |
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("bg.jpg"); | |
if (!fundo) | |
{ | |
fprintf(stderr, "Falha ao carregar imagem de fundo.\n"); | |
al_destroy_display(janela); | |
al_destroy_event_queue(fila_eventos); | |
return false; | |
} | |
al_register_event_source(fila_eventos, al_get_keyboard_event_source()); | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_audio.h> | |
#include <allegro5/allegro_acodec.h> | |
#include <stdio.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_AUDIO_STREAM *musica = NULL; | |
ALLEGRO_SAMPLE *sample = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
bool inicializar(); | |
int main(void) | |
{ | |
bool sair = false; | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
al_attach_audio_stream_to_mixer(musica, al_get_default_mixer()); | |
al_set_audio_stream_playing(musica, true); | |
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_UP) | |
{ | |
if (evento.keyboard.keycode == ALLEGRO_KEY_SPACE) | |
{ | |
al_play_sample(sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL); | |
} | |
} | |
else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
} | |
al_destroy_audio_stream(musica); | |
al_destroy_sample(sample); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar Allegro.\n"); | |
return false; | |
} | |
if (!al_install_audio()) | |
{ | |
fprintf(stderr, "Falha ao inicializar áudio.\n"); | |
return false; | |
} | |
if (!al_init_acodec_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar codecs de áudio.\n"); | |
return false; | |
} | |
if (!al_reserve_samples(1)) | |
{ | |
fprintf(stderr, "Falha ao alocar canais de áudio.\n"); | |
return false; | |
} | |
if (!al_install_keyboard()) | |
{ | |
fprintf(stderr, "Falha ao inicializar teclado.\n"); | |
return false; | |
} | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar a janela.\n"); | |
return false; | |
} | |
sample = al_load_sample("palmas.wav"); | |
if (!sample) | |
{ | |
fprintf(stderr, "Falha ao carregar sample.\n"); | |
al_destroy_display(janela); | |
return false; | |
} | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
al_destroy_display(janela); | |
al_destroy_sample(sample); | |
return false; | |
} | |
al_set_window_title(janela, "Testando sons"); | |
musica = al_load_audio_stream("mus.ogg", 4, 1024); | |
if (!musica) | |
{ | |
fprintf(stderr, "Falha ao carregar audio.\n"); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
al_destroy_sample(sample); | |
return false; | |
} | |
al_register_event_source(fila_eventos, al_get_keyboard_event_source()); | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_primitives.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
bool inicializar(); | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_DISPLAY *janela = NULL; | |
int main(void) | |
{ | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
// Linha: x1, y1, x2, y2, cor, espessura | |
al_draw_line(20.0, 40.0, 40.0, 60.0, al_map_rgb(255, 0, 0), 1.0); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Triângulo: x1, y1, x2, y2, x3, y3, cor, espessura | |
al_draw_triangle(70.0, 30.0, 20.0, 55.0, 110.0, 250.0, al_map_rgb(255, 255, 255), 5.0); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Triângulo preenchido: x1, y1, x2, y2, x3, y3, cor | |
al_draw_filled_triangle(40.0, 90.0, 120.0, 246.0, 400.0, 23.0, al_map_rgb(255, 255, 0)); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Retângulo: x1, y1, x2, y2, cor, espessura | |
al_draw_rectangle(70.0, 30.0, 110.0, 250.0, al_map_rgb(255, 0, 255), 6.0); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Retângulo preenchido: x1, y1, x2, y2, cor | |
al_draw_filled_rectangle(88.0, 10.0, 340.0, 77.0, al_map_rgb(0, 255, 255)); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Elipse: x1, y1, raio x, raio y, cor, espessura | |
al_draw_ellipse(70.0, 90.0, 20.0, 55.0, al_map_rgb(255, 255, 255), 5.0); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Elipse preenchido: x1, y1, raio x, raio y, cor | |
al_draw_filled_ellipse(98.0, 145.0, 25.0, 15.0, al_map_rgb(128, 255, 128)); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Círculo: x1, y1, raio, cor, espessura | |
al_draw_circle(250.0, 300.0, 70.0, al_map_rgb(128, 0, 0), 2.0); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
// Círculo preenchido: x1, y1, raio, cor | |
al_draw_filled_circle(350.0, 50.0, 43.0, al_map_rgb(0, 0, 255)); | |
al_flip_display(); | |
al_rest(2.0); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_flip_display(); | |
al_rest(0.5); | |
al_destroy_display(janela); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar a biblioteca Allegro.\n"); | |
return false; | |
} | |
if (!al_init_primitives_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar add-on de primitivas.\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, "Testando allegro_primitives"); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_primitives.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
bool inicializar(); | |
int main(void) | |
{ | |
bool sair = false; | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
float raio = 30.0; | |
float x = raio; | |
float y = raio; | |
int dir_x = 1, dir_y = 1; | |
while (!sair) | |
{ | |
if (!al_is_event_queue_empty(fila_eventos)) | |
{ | |
ALLEGRO_EVENT evento; | |
al_wait_for_event(fila_eventos, &evento); | |
if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
al_draw_filled_circle(x, y, raio, al_map_rgb(255, 0, 0)); | |
al_flip_display(); | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
x += 1.0 * dir_x; | |
y += 1.0 * dir_y; | |
if (x >= LARGURA_TELA - raio) | |
{ | |
dir_x = -1; | |
x = LARGURA_TELA - raio; | |
} | |
else if (x <= raio) | |
{ | |
dir_x = 1; | |
x = raio; | |
} | |
if (y >= ALTURA_TELA - raio) | |
{ | |
dir_y = -1; | |
y = ALTURA_TELA - raio; | |
} | |
else if (y <= raio) | |
{ | |
dir_y = 1; | |
y = raio; | |
} | |
al_rest(0.005); | |
} | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar Allegro.\n"); | |
return false; | |
} | |
if (!al_init_primitives_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar add-on allegro_primitives.\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, "Animando!!!"); | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
al_destroy_display(janela); | |
return false; | |
} | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_font.h> | |
#include <allegro5/allegro_ttf.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
bool inicializar(); | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL, *fila_contador = NULL; | |
ALLEGRO_TIMER *timer = NULL, *contador = 0; | |
ALLEGRO_FONT *fonte = NULL; | |
int main(void) | |
{ | |
bool sair = false; | |
int r = 0, g = 0, b = 0; | |
int min = 0, seg = 0; | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
al_start_timer(timer); | |
al_start_timer(contador); | |
while (!sair) | |
{ | |
if (!al_is_event_queue_empty(fila_eventos)) | |
{ | |
ALLEGRO_EVENT evento; | |
al_wait_for_event(fila_eventos, &evento); | |
if (evento.type == ALLEGRO_EVENT_TIMER) | |
{ | |
if (++r >= 255) r = 0; | |
if (++g >= 255) g = 0; | |
if (++b >= 255) b = 0; | |
} | |
else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
if (!al_is_event_queue_empty(fila_contador)) | |
{ | |
ALLEGRO_EVENT evento; | |
al_wait_for_event(fila_contador, &evento); | |
if (evento.type == ALLEGRO_EVENT_TIMER) | |
{ | |
seg++; | |
if (seg == 60) | |
{ | |
min++; | |
seg = 0; | |
} | |
} | |
} | |
al_clear_to_color(al_map_rgb(r, g, b)); | |
al_draw_textf(fonte, al_map_rgb(255, 0, 0), LARGURA_TELA / 2, 200, ALLEGRO_ALIGN_CENTRE, "%d:%d", min, seg); | |
al_flip_display(); | |
} | |
al_destroy_timer(timer); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar Allegro.\n"); | |
return false; | |
} | |
al_init_font_addon(); | |
if (!al_init_ttf_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar allegro_ttf.\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, "Testandos Timers..."); | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
al_destroy_display(janela); | |
return false; | |
} | |
timer = al_create_timer(0.1); | |
if (!timer) | |
{ | |
fprintf(stderr, "Falha ao criar timer.\n"); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return false; | |
} | |
fonte = al_load_font("font.ttf", 120, 0); | |
if (!fonte) | |
{ | |
fprintf(stderr, "Falha ao carregar fonte.\n"); | |
al_destroy_timer(timer); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return false; | |
} | |
contador = al_create_timer(1.0); | |
if (!contador) | |
{ | |
fprintf(stderr, "Falha ao criar timer.\n"); | |
al_destroy_font(fonte); | |
al_destroy_timer(timer); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return false; | |
} | |
fila_contador = al_create_event_queue(); | |
if (!fila_contador) | |
{ | |
fprintf(stderr, "Falha ao criar fila do contador.\n"); | |
al_destroy_timer(contador); | |
al_destroy_font(fonte); | |
al_destroy_timer(timer); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return false; | |
} | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
al_register_event_source(fila_eventos, al_get_timer_event_source(timer)); | |
al_register_event_source(fila_contador, al_get_timer_event_source(contador)); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void *funcao(ALLEGRO_THREAD thr, void *argumentos); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_image.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_BITMAP *imagem = NULL; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos; | |
ALLEGRO_THREAD *thread = NULL; | |
bool inicializar(); | |
bool carregar_arquivos(); | |
void finalizar(); | |
void *minha_thread(ALLEGRO_THREAD *thr, void *dados); | |
bool sair = false; | |
int main(void) | |
{ | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
if (!carregar_arquivos()) | |
{ | |
return -1; | |
} | |
// Cria a thread e a dispara | |
thread = al_create_thread(minha_thread, NULL); | |
al_start_thread(thread); | |
al_draw_bitmap(imagem, 0, 0, 0 ); | |
al_flip_display(); | |
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_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
} | |
finalizar(); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar Allegro.\n"); | |
return false; | |
} | |
if (!al_init_image_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar add-on para imagens.\n"); | |
return false; | |
} | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
return false; | |
} | |
al_set_window_title(janela, "Testando Threads"); | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
al_destroy_display(janela); | |
return false; | |
} | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
return true; | |
} | |
bool carregar_arquivos() | |
{ | |
imagem = al_load_bitmap("imagem.jpg"); | |
if (!imagem) | |
{ | |
fprintf(stderr, "Falha ao carregar imagem.\n"); | |
al_destroy_display(janela); | |
al_destroy_event_queue(fila_eventos); | |
return false; | |
} | |
return true; | |
} | |
void finalizar() | |
{ | |
al_destroy_thread(thread); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_bitmap(imagem); | |
al_destroy_display(janela); | |
} | |
void *minha_thread(ALLEGRO_THREAD *thr, void *dados) | |
{ | |
while(!sair) | |
{ | |
al_set_window_title(janela, "Thread disparada"); | |
al_rest(0.25); | |
al_set_window_title(janela, "Thread disparada."); | |
al_rest(0.25); | |
al_set_window_title(janela, "Thread disparada. ."); | |
al_rest(0.25); | |
al_set_window_title(janela, "Thread disparada. . ."); | |
al_rest(0.25); | |
} | |
return NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_image.h> | |
#include <allegro5/allegro_font.h> | |
#include <allegro5/allegro_ttf.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
const int FRAMES_POR_SEGUNDO = 60; | |
ALLEGRO_BITMAP *fundo = NULL; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
ALLEGRO_FONT *fonte = NULL; | |
double tempoInicial = 0; | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao iniciar a Allegro.\n"); | |
return false; | |
} | |
if (!al_init_image_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar allegro_image.\n"); | |
return false; | |
} | |
al_init_font_addon(); | |
if (!al_init_ttf_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar allegro_ttf.\n"); | |
return false; | |
} | |
if (!al_install_keyboard()) | |
{ | |
fprintf(stderr, "Falha ao inicializar teclado.\n"); | |
return false; | |
} | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar a janela.\n"); | |
return false; | |
} | |
al_set_window_title(janela, "Teste: Taxa de Frames"); | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
return false; | |
} | |
al_register_event_source(fila_eventos, al_get_keyboard_event_source()); | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
return true; | |
} | |
bool carregarArquivos() | |
{ | |
fundo = al_load_bitmap("background.jpg"); | |
if (!fundo) | |
{ | |
fprintf(stderr, "Falha ao carregar plano de fundo.\n"); | |
return false; | |
} | |
fonte = al_load_font("BAVEUSE.TTF", 32, 0); | |
if (!fonte) | |
{ | |
fprintf(stderr, "Falha ao carregar fonte.\n"); | |
return false; | |
} | |
return true; | |
} | |
void finalizar() | |
{ | |
al_destroy_bitmap(fundo); | |
al_destroy_font(fonte); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
} | |
void iniciarTimer() | |
{ | |
tempoInicial = al_get_time(); | |
} | |
double obterTempoTimer() | |
{ | |
return al_get_time() - tempoInicial; | |
} | |
int main(void) | |
{ | |
bool sair = false; | |
int frame = 0; | |
bool limitado = true; | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
if (!carregarArquivos()) | |
{ | |
return -1; | |
} | |
while (!sair) | |
{ | |
iniciarTimer(); | |
while (!al_is_event_queue_empty(fila_eventos)) | |
{ | |
ALLEGRO_EVENT evento; | |
al_wait_for_event(fila_eventos, &evento); | |
if (evento.type == ALLEGRO_EVENT_KEY_UP) | |
{ | |
if (evento.keyboard.keycode == ALLEGRO_KEY_ENTER) | |
{ | |
limitado = !limitado; | |
} | |
} | |
else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
al_draw_bitmap(fundo, 0, 0, 0); | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
((ALTURA_TELA + al_get_font_ascent(fonte) * 2) | |
/ FRAMES_POR_SEGUNDO) * (frame % FRAMES_POR_SEGUNDO) | |
- al_get_font_ascent(fonte), ALLEGRO_ALIGN_CENTRE, | |
"Testando Taxa de Frames"); | |
al_flip_display(); | |
frame++; | |
if (limitado && (obterTempoTimer() < 1.0 / FRAMES_POR_SEGUNDO)) | |
{ | |
al_rest((1.0 / FRAMES_POR_SEGUNDO) - obterTempoTimer()); | |
} | |
} | |
finalizar(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_primitives.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
ALLEGRO_JOYSTICK *joystick = NULL; | |
bool inicializar(); | |
int main(void) | |
{ | |
bool sair = false; | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
const int raio = 25; | |
int x = raio; | |
int y = raio; | |
int inc_x = 0, inc_y = 0; | |
ALLEGRO_COLOR cor = al_map_rgb(255, 255, 255); | |
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_JOYSTICK_AXIS) | |
{ | |
if (evento.joystick.axis == 0) | |
{ | |
inc_x = evento.joystick.pos; | |
} | |
else if (evento.joystick.axis == 1) | |
{ | |
inc_y = evento.joystick.pos; | |
} | |
} | |
else if (evento.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) | |
{ | |
switch (evento.joystick.button) | |
{ | |
case 1: | |
cor = al_map_rgb(255, 0, 0); | |
break; | |
case 2: | |
cor = al_map_rgb(0, 0, 255); | |
break; | |
case 3: | |
cor = al_map_rgb(0, 255, 0); | |
break; | |
case 0: | |
default: | |
cor = al_map_rgb(255, 255, 255); | |
break; | |
} | |
} | |
else if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
x += inc_x; | |
if (x > LARGURA_TELA - raio) | |
{ | |
x = LARGURA_TELA - raio; | |
} | |
else if (x < raio) | |
{ | |
x = raio; | |
} | |
y += inc_y; | |
if (y > ALTURA_TELA - raio) | |
{ | |
y = ALTURA_TELA - raio; | |
} | |
else if (y < raio) | |
{ | |
y = raio; | |
} | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_draw_filled_circle(x, y, raio, cor); | |
al_flip_display(); | |
} | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar a Allegro.\n"); | |
return false; | |
} | |
if (!al_init_primitives_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar add-on de primitivas.\n"); | |
return false; | |
} | |
if (!al_install_joystick()) | |
{ | |
fprintf(stderr, "Falha ao inicializar joystick.\n"); | |
return false; | |
} | |
al_reconfigure_joysticks(); | |
if (al_get_num_joysticks() == 0) | |
{ | |
fprintf(stderr, "Nenhum joystick encontrado.\n"); | |
return false; | |
} | |
joystick = al_get_joystick(al_get_num_joysticks() - 1); | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar janela.n"); | |
return false; | |
} | |
al_set_window_title(janela, "Testando Joystick"); | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
al_destroy_display(janela); | |
return false; | |
} | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
al_register_event_source(fila_eventos, al_get_joystick_event_source()); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_image.h> | |
#include <allegro5/allegro_font.h> | |
#include <allegro5/allegro_ttf.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdbool.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_BITMAP *fundo = NULL; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_FONT *fonte = NULL; | |
ALLEGRO_EVENT_QUEUE *fila_eventos = NULL; | |
char str[17]; | |
void manipular_entrada(ALLEGRO_EVENT evento); | |
void exibir_texto_centralizado(); | |
bool inicializar(); | |
bool carregar_arquivos(); | |
void finalizar(); | |
int main(void) | |
{ | |
bool sair = false; | |
bool concluido = false; | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
strcpy(str, ""); | |
if (!carregar_arquivos()) | |
{ | |
return -1; | |
} | |
while (!sair) | |
{ | |
while (!al_is_event_queue_empty(fila_eventos)) | |
{ | |
ALLEGRO_EVENT evento; | |
al_wait_for_event(fila_eventos, &evento); | |
if (!concluido) | |
{ | |
manipular_entrada(evento); | |
if (evento.type == ALLEGRO_EVENT_KEY_DOWN && evento.keyboard.keycode == ALLEGRO_KEY_ENTER) | |
{ | |
concluido = true; | |
} | |
} | |
if (evento.type == ALLEGRO_EVENT_DISPLAY_CLOSE) | |
{ | |
sair = true; | |
} | |
} | |
al_draw_bitmap(fundo, 0, 0, 0); | |
if (!concluido) | |
{ | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
(ALTURA_TELA / 2 - al_get_font_ascent(fonte)) / 2, | |
ALLEGRO_ALIGN_CENTRE, "Melhor Pontuação! Nome:"); | |
} | |
else | |
{ | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
(ALTURA_TELA / 2 - al_get_font_ascent(fonte)) / 2, | |
ALLEGRO_ALIGN_CENTRE, "1º Lugar"); | |
} | |
exibir_texto_centralizado(); | |
al_flip_display(); | |
} | |
finalizar(); | |
return 0; | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar a biblioteca Allegro.\n"); | |
return false; | |
} | |
if (!al_install_keyboard()) | |
{ | |
fprintf(stderr, "Falha ao inicializar teclado.\n"); | |
return false; | |
} | |
if (!al_init_image_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar allegro_image.\n"); | |
return false; | |
} | |
al_init_font_addon(); | |
if (!al_init_ttf_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar allegro_ttf.\n"); | |
return false; | |
} | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar a janela.\n"); | |
return false; | |
} | |
fila_eventos = al_create_event_queue(); | |
if (!fila_eventos) | |
{ | |
fprintf(stderr, "Falha ao criar fila de eventos.\n"); | |
return false; | |
} | |
al_set_window_title(janela, "Entrada de Texto"); | |
al_register_event_source(fila_eventos, al_get_display_event_source(janela)); | |
al_register_event_source(fila_eventos, al_get_keyboard_event_source()); | |
return true; | |
} | |
bool carregar_arquivos() | |
{ | |
fundo = al_load_bitmap("bg.jpg"); | |
if (!fundo) | |
{ | |
fprintf(stderr, "Falha ao carregar \"bg.jpg\".\n"); | |
return false; | |
} | |
fonte = al_load_font("comic.ttf", 42, 0); | |
if (!fonte) | |
{ | |
fprintf(stderr, "Falha ao carregar \"comic.ttf\".\n"); | |
return false; | |
} | |
return true; | |
} | |
void finalizar() | |
{ | |
al_destroy_bitmap(fundo); | |
al_destroy_font(fonte); | |
al_destroy_event_queue(fila_eventos); | |
al_destroy_display(janela); | |
} | |
void manipular_entrada(ALLEGRO_EVENT evento) | |
{ | |
if (evento.type == ALLEGRO_EVENT_KEY_CHAR) | |
{ | |
if (strlen(str) <= 16) | |
{ | |
char temp[] = {evento.keyboard.unichar, '\0'}; | |
if (evento.keyboard.unichar == ' ') | |
{ | |
strcat(str, temp); | |
} | |
else if (evento.keyboard.unichar >= '0' && | |
evento.keyboard.unichar <= '9') | |
{ | |
strcat(str, temp); | |
} | |
else if (evento.keyboard.unichar >= 'A' && | |
evento.keyboard.unichar <= 'Z') | |
{ | |
strcat(str, temp); | |
} | |
else if (evento.keyboard.unichar >= 'a' && | |
evento.keyboard.unichar <= 'z') | |
{ | |
strcat(str, temp); | |
} | |
} | |
if (evento.keyboard.keycode == ALLEGRO_KEY_BACKSPACE && strlen(str) != 0) | |
{ | |
str[strlen(str) - 1] = '\0'; | |
} | |
} | |
} | |
void exibir_texto_centralizado() | |
{ | |
if (strlen(str) > 0) | |
{ | |
al_draw_text(fonte, al_map_rgb(255, 255, 255), LARGURA_TELA / 2, | |
(ALTURA_TELA - al_get_font_ascent(fonte)) / 2, | |
ALLEGRO_ALIGN_CENTRE, str); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_native_dialog.h> | |
int main(void) | |
{ | |
if (al_show_native_message_box(NULL, "www.rafaeltoledo.net", | |
"Pergunta:", "Deseja exibir a mensagem de erro?", | |
NULL, ALLEGRO_MESSAGEBOX_YES_NO)) | |
{ | |
al_show_native_message_box(NULL, "www.rafaeltoledo.net", | |
"Então...", "Estou exibindo a mensagem de erro.", | |
NULL, ALLEGRO_MESSAGEBOX_ERROR); | |
} | |
else | |
{ | |
al_show_native_message_box(NULL, "www.rafaeltoledo.net", | |
"Então", "Vamos com essa mensagem de dúvida...", | |
NULL, ALLEGRO_MESSAGEBOX_QUESTION); | |
} | |
if (al_show_native_message_box(NULL, "www.rafaeltoledo.net", | |
"Outra pergunta...", "Vou exibir uma última mensagem antes de sair, certo?", | |
NULL, ALLEGRO_MESSAGEBOX_OK_CANCEL)) | |
{ | |
al_show_native_message_box(NULL, "www.rafaeltoledo.net", | |
"É a última mensagem, prometo!", "Mensagem, mensagem, mensagem, mensagem", | |
NULL, ALLEGRO_MESSAGEBOX_WARN); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_image.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
const int LARGURA_TELA = 640; | |
const int ALTURA_TELA = 480; | |
ALLEGRO_DISPLAY *janela = NULL; | |
ALLEGRO_BITMAP *fundo = NULL; | |
void fadeout(int velocidade); | |
void fadein(ALLEGRO_BITMAP *imagem, int velocidade); | |
bool inicializar(); | |
int main() | |
{ | |
if (!inicializar()) | |
{ | |
return -1; | |
} | |
fadein(fundo, 1); | |
al_rest(3.0); | |
fadeout(1); | |
return 0; | |
} | |
void fadeout(int velocidade) | |
{ | |
ALLEGRO_BITMAP *buffer = NULL; | |
buffer = al_create_bitmap(LARGURA_TELA, ALTURA_TELA); | |
al_set_target_bitmap(buffer); | |
al_draw_bitmap(al_get_backbuffer(janela), 0, 0, 0); | |
al_set_target_bitmap(al_get_backbuffer(janela)); | |
if (velocidade <= 0) | |
{ | |
velocidade = 1; | |
} | |
else if (velocidade > 15) | |
{ | |
velocidade = 15; | |
} | |
int alfa; | |
for (alfa = 0; alfa <= 255; alfa += velocidade) | |
{ | |
al_clear_to_color(al_map_rgba(0, 0, 0, 0)); | |
al_draw_tinted_bitmap(buffer, al_map_rgba(255 - alfa, 255 - alfa, 255 - alfa, alfa), 0, 0, 0); | |
al_flip_display(); | |
al_rest(0.005); // Não é necessário caso haja controle de FPS | |
} | |
al_destroy_bitmap(buffer); | |
} | |
void fadein(ALLEGRO_BITMAP *imagem, int velocidade) | |
{ | |
if (velocidade < 0) | |
{ | |
velocidade = 1; | |
} | |
else if (velocidade > 15) | |
{ | |
velocidade = 15; | |
} | |
int alfa; | |
for (alfa = 0; alfa <= 255; alfa += velocidade) | |
{ | |
al_clear_to_color(al_map_rgb(0, 0, 0)); | |
al_draw_tinted_bitmap(imagem, al_map_rgba(alfa, alfa, alfa, alfa), 0, 0, 0); | |
al_flip_display(); | |
al_rest(0.005); // Não é necessário caso haja controle de FPS | |
} | |
} | |
bool inicializar() | |
{ | |
if (!al_init()) | |
{ | |
fprintf(stderr, "Falha ao inicializar a Allegro.\n"); | |
return false; | |
} | |
if (!al_init_image_addon()) | |
{ | |
fprintf(stderr, "Falha ao inicializar allegro_image.\n"); | |
return false; | |
} | |
janela = al_create_display(LARGURA_TELA, ALTURA_TELA); | |
if (!janela) | |
{ | |
fprintf(stderr, "Falha ao criar janela.\n"); | |
return false; | |
} | |
fundo = al_load_bitmap("background.jpg"); | |
if (!fundo) | |
{ | |
fprintf(stderr, "Falha ao carregar plano de fundo.\n"); | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment