|
Table of Contents
|
O que é Allegro
Allegro é uma biblioteca de funções para jogos 2D feita em C. Apesar de ter algumas funções para jogos 3D ela não é indicada para isso sendo no lugar dela uma API3d como OpenGL ou DirectX, por exemplo.
Ou nas palavras (traduzidas) do arquivo readme do Allegro
Allegro is a cross-platform library intended for use in computer games
and other types of multimedia programming. It was initially conceived on
the Atari ST, but that platform sadly died during childbirth. After a
brief stay with Borland C, it was adopted by the fantastic djgpp
compiler, where it grew to maturity. In the fullness of time it gave
birth to children of its own, who went to live in such exotic locations
as DirectX and the X Server, but the entire family is now back together
again, living in harmony as a single portable entity. How about that for
a mixture of metaphors? :-)
A wide range of extension packages and add-on modules are also
available, which can be found in the "Library Extensions" section
of the Allegro.cc website, http://www.allegro.cc/.
According to the Oxford Companion to Music, Allegro is the Italian for
"quick, lively, bright". It is also a recursive acronym which stands for
"Allegro Low Level Game Routines".
Por que usar o Allegro
Porque é uma API fácil, prática e ideal para novatos, sendo feita especialmente para jogos 2D
Como começar com Allegro
O jeito fácil é pegar uma distribuição compilada e com tudo já prontinho e só ajustar as bibs, includes nas opções de projeto que a IDE/compilador oferecem. Porém o jeito mais fácil na minha opinião é baixar o Dev-Cpp e depois de instalado clicar na opção para baixar dev-pack e escolhar o do Allegro, um dos mais recentes da lista. Daí é só instalá-lo no final do download.
Para ver se tudo está funcionando selecione a opção para criar um novo projeto e vá para a aba multinmídia e clique em Allegro Application (qualquer uma das duas versões, com biblioteca dinâmica ou estática) e daí salve o programa que aparecer e aperte para compilar (F9) ou project -> Compile and Run project.
Um hello World em allegro
#include <allegro.h>
int main(){
if( allegro_init() != 0 ) return 1;
allegro_message( "Funcionou" );
allegro_exit();
return 0;
}
END_OF_MAIN()
Exemplo de animacao em Alegro
#include <allegro.h>
#include <stdlib.h>
#include <math.h>
void setup();
void saida();
typedef struct Circle{
int x, y, raio;
} Circle;
void animCircle(){
static Circle *circ=NULL; // static age como uma var global acessivel
// somente em dado local, neste caso a func animCircle
if( !circ ){
circ = malloc( sizeof(Circle));
circ->x = circ->y = 300;
circ->raio = 10;
} else{
rectfill( screen, circ->x - circ->raio, circ->y - circ->raio, circ->x + circ->raio, circ->y + circ->raio, makecol( 0, 0, 0 ));
circ->x += rand() % 40 - 20;
circ->y += rand() % 40 - 20;
}
circle( screen, circ->x, circ->y, circ->raio, makecol( 0, 0, 255 ) );
}
int main(){
atexit( saida );
setup();
install_int( animCircle, 50 );
while( !key[KEY_ESC] ){
// verificar movimento do jogador
// verificar movimento dos oponentes
// outras coisas
rest(50);
}
exit( 0 );
return 0;
}
void setup(){
allegro_init();
// instala subsistemas
install_keyboard();
install_mouse();
install_timer();
//install_joystick();
srand( time(0) );
// configura a tela
set_color_depth( 16 ); // o padrao e 8
// presar bem atenco a armadilha da mudanca do modo grafico
set_gfx_mode( GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0 );
}
void saida(){
allegro_exit();
}
Musica em allegro
Detecćão de colisão simples com allegro
#include <allegro.h> /* Adiciona as funções do Allegro */
#include <allegro/alcompat.h>
#include <stdio.h>
volatile int a = 0; /* variável global */
void func() {
a++;
}
END_OF_FUNCTION();
int main() {
int x=275,y=75,x2=340,y2=140;
allegro_init();
install_timer();
install_mouse();
install_keyboard();
LOCK_VARIABLE(a);
LOCK_FUNCTION(func);
install_int(func,100);
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,640,480,0,0);
BITMAP *img = create_bitmap(640,480); /* Cria o BITMAP */
//while(!key[KEY_ESC]) {
//clear_bitmap(img);
//line(img,10,10, 200,200, makecol(0,0,255) ); /* Define dimensão e cor da linha */
//circle(img,300,300,100, makecol(255,0,0 ) );
//rect(img,400,400,200,200, makecol(0,255,0 ) );
//rectfill(img, 399,399,201,201, makecol(0,0,255));
//triangle(img,400,400,400,200,200,200, makecol(255,0,0 ) );
//vline(img, 10,10,100, makecol(255,0,0 ) );
//hline(img, 10,10,100, makecol(255,0,0 ) );
//blit(img,screen,0,0,0,0,640,480); /* Imprime a linha */
/*if(key[KEY_RIGHT]) {
x++;
clear_bitmap(img);
circlefill(img,x,y,100,makecol(255,255,255));
blit(img,screen,0,0,0,0,640,480);
}*/
//};
char txt[40];
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(255,0,0));
rectfill(img,x,y,x2,y2,makecol(0,0,255)); /* Retângulo Manupulável Azul */
rectfill(img,260,190,360,290,makecol(255,0,0)); /* Retângulo Estático Vermelho */
blit(img,screen,0,0,0,0,640,480);
while(!key[KEY_ESC]) {
show_mouse(img);
/* Movimentação por setas do teclado: Início */
if(key[KEY_RIGHT]) {
clear_bitmap(img);
x++;x2++;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(255,0,0));
}
if(key[KEY_LEFT]) {
clear_bitmap(img);
x--;x2--;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(255,0,0));
}
if(key[KEY_UP]) {
clear_bitmap(img);
y--;y2--;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(255,0,0));
}
if(key[KEY_DOWN]) {
clear_bitmap(img);
y++;y2++;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(255,0,0));
}
/* Fim da movimentação com Teclado e início das verificações de posicionamento */
if((x >= 259) && (x <= 360)) {
if((y2 >= 190) && (y2 <= 354)) {
clear_bitmap(img);
x++;x2++;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
textout(screen,font,"colisao detectada",250,1,makecol(255,255,0));
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(0,255,0));
}
}
if((x2 >= 259) && (x2 <= 360)) {
if((y >= 125) && (y <= 285)) {
clear_bitmap(img);
x--;x2--;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
textout(screen,font,"colisao detectada",250,1,makecol(255,255,0));
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(0,255,0));
}
}
if((y >= 190) && (y <= 290)) {
if((x >= 194) && (x <= 359)) {
clear_bitmap(img);
y++;y2++;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
textout(screen,font,"colisao detectada",250,1,makecol(255,255,0));
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(0,255,0));
}
}
if((y2 >= 190) && (y2 <= 290)) {
if((x >= 196) && (x <= 359)) {
clear_bitmap(img);
y--;y2--;
rectfill(img,x,y,x2,y2,makecol(0,0,255));
rectfill(img,260,190,360,290,makecol(255,0,0));
blit(img,screen,0,0,0,0,640,480);
textout(screen,font,"colisao detectada",250,1,makecol(255,255,0));
sprintf(txt,"X: %d Y: %d", x, y);
textout(screen,font,txt,5,5,makecol(0,255,0));
}
}
};
return 0;
}
END_OF_MAIN();
Detecção de colisão e movimento de personagem
Bibliotecas auxiliares
Bibliotecas auxiliares são aquelas relacionadas com o Allegro, mas que não vem com a API, entretanto, elas adicionam alguma funcionalidade que a API não tem, sendo por vezes muito úteis. Desse modo elas tem como pré-requisito a instalação prévia do allegro para funcionar e representam opções de linha de comando adicionais para compilar o teu programa.
alogg: Adiciona suporte de som ogg ao allegro
alpng: Adiciona suporte ao carregamento de imagens PNG pelo Allegro
links para mais informações de bibliotecas auxiiares
Allegro GUI Seção do wiki do Allegro para bibliotecas que auxiliam na construção de uma GUI
Mais links para o allegro
Uma coleção de materiais sobre allegro reunidos pelo usuário saaulx da Unidev
Wiki sobre allegro em português
Site oficial do Allegro
Página exclusiva sobre Allegro contando com jogos, tutoriais, downloads, etc...
Wiki com muita informação sobre o allegro com materiais para os diversos tipos de usuários que usam o allegro





