{$mode objfpc} {$H+}
program alegro8;
(*
allegro5 -> biblioteca alegro
al5image -> biblioteca para imagenes
*)
uses
sysutils, { Para manejar excepciones. }
allegro5,al5image;
//clase jugador----------------------------------
type
Tjugador = class
strict private
imagen: ALLEGRO_BITMAPptr;
estado_de_teclas: ALLEGRO_KEYBOARD_STATE;
x,y: real;
public
constructor Create();
{ Este método es el que carga la imagen. }
function CargaImagen (const aNombreArchivo: String): Boolean;
procedure pintar();
procedure mover();
destructor Destroy(); override;
end;
constructor Tjugador.Create();
begin
imagen := Nil; { <-- Dejar claro que NO existe la imagen. }
x := 320;
y := 240;
end;
(* Carga la imagen dada. *)
function TJugador.CargaImagen (const aNombreArchivo: String): Boolean;
begin
{ Primero: si existe una imagen anterior, elimínala. }
if Assigned (Self.imagen) then al_destroy_image (Self.imagen);
{ Ahora, carga la imagen. }
Self.imagen := al_load_bitmap (aNombreArchivo);
Result := Assigned (Self.imagen)
end;
procedure Tjugador.pintar();
begin
//pintamos imagen(imagen,x,y,voltear)
al_draw_bitmap(imagen,x,y,0);
end;
procedure Tjugador.mover();
begin
al_get_keyboard_state(estado_de_teclas);
if(al_key_down(estado_de_teclas,ALLEGRO_KEY_LEFT)) then
begin
x := x - 5;
end
else if(al_key_down(estado_de_teclas,ALLEGRO_KEY_RIGHT)) then
begin
x := x + 5;
end;
if(al_key_down(estado_de_teclas,ALLEGRO_KEY_UP)) then
begin
y := y - 5;
end
else if(al_key_down(estado_de_teclas,ALLEGRO_KEY_DOWN)) then
begin
y := y + 5;
end;
end;
destructor Tjugador.Destroy();
begin
al_destroy_bitmap(imagen);
imagen := nil;
end;
//fin clase jugador---------------------------------
var
ventana: ALLEGRO_DISPLAYptr = nil;
evento: ALLEGRO_EVENT;
cola_de_eventos: ALLEGRO_EVENT_QUEUEptr = nil;
tiempo: ALLEGRO_TIMERptr = nil;
ejecutar: Boolean;
jugador: Tjugador;
begin
//iniciar allegro
al_init();
//instalar teclado
al_install_keyboard();
//iniciar imagen
al_init_image_addon();
//configurar ventana en modo ventana
al_set_new_display_flags(ALLEGRO_WINDOWED);
//crear ventana(ancho,alto)
ventana := al_create_display(640,480);
//poner titulo a la ventana
al_set_window_title(ventana,'objeto jugador');
//ocultar raton
al_hide_mouse_cursor(ventana);
//crear cola de eventos
cola_de_eventos := al_create_event_queue();
//crear tiempo para fps
tiempo := al_create_timer(1/60);
//registrar eventos
al_register_event_source(cola_de_eventos,al_get_keyboard_event_source());
al_register_event_source(cola_de_eventos,al_get_display_event_source(ventana));
al_register_event_source(cola_de_eventos,al_get_timer_event_source(tiempo));
//inicio de variables
ejecutar := true;
{ Crea el jugador. }
jugador := Tjugador.Create();
{ Carga la imagen del jugador. }
if not jugador.CargaImagen ('alien.png') then
begin
{ CERRAR EL CONTEXTO GRÁFICO para que muestre el error. }
al_destroy_display (ventana);
{ Lanza una excepción. Deberías capturarla, pero así está bien porque
mostrará una ventana de error explicando lo que pasa. }
raise exception ('No pudo cargarse el archivo "alien.png"')
end;
//iniciamos el tiempo
al_start_timer(tiempo);
while (ejecutar = true) do
begin
//esperamos a la cola de eventos
al_wait_for_event(cola_de_eventos,@evento);
case evento.ftype of
//manejo de fps
ALLEGRO_EVENT_TIMER: begin
//limpiar pantalla con un color cualquiera
al_clear_to_color(al_map_rgb(0,0,160));
jugador.mover();
jugador.pintar();
//actualizar pantalla
al_flip_display();
end;
//si pulso escape salgo del bucle
ALLEGRO_EVENT_KEY_DOWN: begin
if (evento.keyboard.keycode = ALLEGRO_KEY_ESCAPE) then
begin
ejecutar := false;
end;
end;
end; //fin case
end; //fin bucle while
al_destroy_display(ventana); //destruir ventana
ventana := nil;
al_destroy_event_queue(cola_de_eventos); //destruir cola
cola_de_eventos := nil;
al_destroy_timer(tiempo); //destruir tiempo
tiempo := nil;
al_uninstall_keyboard(); //desinstalar teclado
jugador.Free(); //liberar jugador
jugador := nil
end. //fin programa