Get Allegro up and running
#include "allegro5/allegro.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_primitives.h"
//
// data
//
typedef struct Vector
{
double x = 0.f, y = 0.f, z = 0.f;
} vector;
struct Shape
{
int n = 3;
ALLEGRO_VERTEX *v;
} shape;
//
// functions
//
void setPerspective()
{
ALLEGRO_TRANSFORM projection;
ALLEGRO_DISPLAY *display =
al_get_current_display();
double sw = al_get_display_width(display);
double sh = al_get_display_height(display);
// radians
float f = 90 * ALLEGRO_PI / 180;
// must identity
al_identity_transform(&projection);
// example from projection2.c
// 0,0,0 in centre of screen
al_perspective_transform(&projection,
(-1 * sw / sh * f), f, 1,
(f * sw / sh), -f, 1000);
al_use_projection_transform(&projection);
}
void drawScene()
{
setPerspective();
al_clear_to_color(al_map_rgb(50.f, 50.f, 150.f));
al_set_render_state(ALLEGRO_DEPTH_TEST, 1);
al_clear_depth_buffer(1);
ALLEGRO_TRANSFORM camera;
// no identity needed
al_build_camera_transform(
&camera,
0, 0, 50,
0, 0, 0,
0, 1, 0);
al_use_transform(&camera);
al_draw_prim(shape.v, NULL, NULL,
0, shape.n + 1,
ALLEGRO_PRIM_TRIANGLE_LIST);
}
void makeShape()
{
shape.v = (ALLEGRO_VERTEX *)realloc(shape.v, shape.n * sizeof *shape.v);
float depth = 0.f;
float size = 10.f;
float x = 0.f;
ALLEGRO_COLOR c1 = al_map_rgb(
100.f, 0.f, 0.f);
shape.v[0] = {.x = x, .y = 0.f, .z = depth, .u = 20.f, .v = 20., .color = c1};
shape.v[1] = {.x = x + size, .y = 0.f, .z = depth, .u = 20.f, .v = 20.f, .color = c1};
shape.v[2] = {.x = x + size, .y = size, .z = depth, .u = 20.f, .v = 20.f, .color = c1};
}
int main(int argc, char *argv[])
{
al_init();
al_init_primitives_addon();
ALLEGRO_DISPLAY *display =
al_create_display(720, 1600);
ALLEGRO_TIMER *timer =
al_create_timer(1.0f / 30.0f);
ALLEGRO_EVENT_QUEUE *queue =
al_create_event_queue();
ALLEGRO_EVENT event;
bool running = true;
bool redraw = false;
al_register_event_source(
queue,
al_get_timer_event_source(timer));
makeShape();
al_start_timer(timer);
while (running)
{
al_wait_for_event(queue, &event);
switch (event.type)
{
case ALLEGRO_EVENT_TIMER:
{
redraw = true;
break;
}
case ALLEGRO_EVENT_DISPLAY_CLOSE:
{
running = false;
break;
}
case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING:
{
al_stop_timer(timer);
redraw = false;
al_acknowledge_drawing_halt(display);
break;
}
case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING:
{
al_acknowledge_drawing_resume(display);
al_start_timer(timer);
break;
}
}
//
// draw
//
if (redraw &&
al_event_queue_is_empty(queue))
{
drawScene();
al_flip_display();
redraw = false;
}
}
al_destroy_display(display);
return 0;
}