You are on page 1of 19

MAB604 Tpicos Especiais em Computao Grfica

Usando Allegro
LCG /COPPE Claudio Esperana Ricardo Farias

Introduo
Biblioteca para construo de jogos Free Source Voltada mais especialmente para
jogos 2D Grande comunidade de desenvolvedores Links:

Biblioteca: http://alleg.sourceforge.net Aplicaes: http://www.allegro.cc

Principais Caractersticas
Multi-plataforma
DOS Windows Linux Mac (OS X) BeOS QNX

Primitivas Grficas
Retas, crculos, elipses Sprites Polgonos (slidos, texturados, transparentes) Texto em modo grfico Animaes (fli)

Caractersticas (cont.)
Som
Midi: Suporte para midi nativo (com at 64 sons simultneos) Rontrole e resposta dinmica a eventos MIDI
music note on, note off, main volume, pan, pitch bend, and program change

Suporte a patches GM (general midi) Suporte a patches wavetable


SF2 (Sound Font 2) e GUS (Gravis UltraSound)

Wave: Suporte nativo a formatos WAV e VOC Streaming Permite modificar volume, pan, pitch, etc enquanto toca

Caractersticas (cont.)
Suporte para interfaces grficas
simples Aritmtica ponto fixo Gerencia mouse, teclado, joystick Suporta arquivos de configurao e de dados comprimidos Temporizadores de alta resoluo Vrios Add-Ons disponveis

Instalando Allegro
Fontes disponveis no site
http://alleg.sourceforge.net Windows usando Mingw32 (http://www.mingw.org)
Baixar, descompactar e instalar o arquivo http://alleg.sourceforge.net/files/dx70_mgw.zip no diretrio do mingw32 Usar o shell do msys cd /c/allegro export MINGDIR=/c/mingw32 ./fix.sh mingw32 make make install

Usando Allegro
Incluir <allegro.h> depois de todos
os demais includes Chamar allegro_init() prximo do incio do programa Colocar a macro END_OF_MAIN() logo aps a chave final da rotina main() Ao compilar, incluir a biblioteca alleg:
gcc -o test test.c -lalleg

Exemplo
#include <stdio.h> #include <allegro.h>

int main (void) { allegro_init(); check_cpu(); printf("\nAllegro reports OK!\n"); printf("\nThis test compiled with %s\n",allegro_id); printf("Your CPU Vendor : %s\n",cpu_vendor);
return 0; } END_OF_MAIN()

Modo Grfico
Especificado antes de comear a desenhar:
int set_gfx_mode (int card, int w, int h, int v_w, int v_h); Onde:

card usualmente GFX_AUTODETECT, GFX_AUTODETECT_FULLSCREEN ou GFX_AUTODETECT_WINDOWED w , h largura e altura v_w , v_h largura e altura virtual (normalmente, 0)

BITMAPs
Desenho se d sobre estruturas chamadas
BITMAPs Tela um BITMAP especial chamado screen BITMAPs adicionais podem ser criados, ex.:

Tamanho em memria do bitmap depende


do modelo de cor (bits por pixel)
void set_color_depth(int depth); Default: 8 Chamado antes de set_gfx_mode

BITMAP *bmp = create_bitmap(320, 200);

Cores
Modelo depende do modo grfico Primitivas recebem um parmetro
inteiro para representar cor Para construir esse inteiro de maneira porttil pode-se usar a rotina
int makecol(int r, int g, int b); Componentes rgb entre 0 e 255

Algumas Primitivas Grficas



void void void void void void void void void putpixel (BITMAP *bmp, int x, int y, int color); vline(BITMAP *bmp, int x, int y1, int y2, int color); hline(BITMAP *bmp, int x1, int y, int x2, int color); line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color); triangle(BITMAP *bmp, int x1, y1, x2, y2, x3, y3, int color); rect(BITMAP *bmp, int x1, int y1, int x2, int y2, int color); rectfill(BITMAP *bmp, int x1, int y1, int x2, int y2, int color); circle(BITMAP *bmp, int x, int y, int radius, int color); circlefill(BITMAP *bmp, int x, int y, int radius, int color);

Perifricos de Entrada
Para usar teclado, mouse ou joystick,
chamar rotina de setup apropriada
install_keyboard () install_mouse () install_joystick ()

Teclado
Leitura com ou sem espera possvel instalar rotinas callback

Exemplo
#include <stdio.h>

#include <allegro.h> int main(void) { int i; allegro_init(); install_keyboard(); set_color_depth (16); if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320, 200, 0, 0) < 0) { set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); allegro_message("Unable to set any graphic mode\n%s\n", allegro_error); return 1; } for (i = 0; i < 64000; i++) { putpixel(screen, rand() % SCREEN_W, rand() % SCREEN_H, makecol (rand () % 256,rand () % 256,rand () % 256)); } readkey(); return 0; } END_OF_MAIN();

Animao
Objetos em movimento so
fundamentais em jogos Ingredientes:
timers double-buffering vsync () blitting sprites

Exemplo Animao
int x; BITMAP * dblbuffer; allegro_init(); install_keyboard(); install_timer(); /* needed for `rest' function */ if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) < 0) { ... } clear(screen); /** Sem Double Buffering ou vsync () **/ circlefill(screen, 0, 100, 50, 15); /* draw first time */ for (x = 1; x < 320; x++) { rest(10); /* slow it down so we can see it */ circlefill(screen, x - 1, 100, 50, 0); /* erase from last place */ circlefill(screen, x, 100, 50, 15); /* redraw at new place */ }

Exemplo Animao c/ Double Buffer


dblbuffer = create_bitmap(SCREEN_W, SCREEN_H); clear(dblbuffer);

circlefill(dblbuffer, 0, 100, 50, 15);

/* draw first time */

vsync(); blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

for (x = 1; x < 320; x++) { rest(10); /* slow it down so we can see it */ circlefill(dblbuffer, x - 1, 100, 50, 0); /* erase from last place */ circlefill(dblbuffer, x, 100, 50, 15); /* redraw at new place */ textout(dblbuffer, font, "Double buffering (mode 13h)", 0, 0, 15); vsync(); blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); }
// Now, remember to FREE the memory you previously allocated destroy_bitmap(dblbuffer);

Exerccio
Construir uma verso
de Brickout usando Allegro
Fase 1: Somente a bola quicando nas quatro paredes Fase 2: Bola quicando em trs paredes e na raquete embaixo Fase 3: Bola quicando em paredes e tijolos, score, etc

Bibliografia
Site principal do Allegro
http://alleg.sourceforge.net

Site principal de aplicaes (games)


em allegro
http://www.allegro.cc

Tutorial allegro vivace


http://www.grandgent.com/gfoot/vivace /vivace.html

You might also like