4 n__allegro__4_8h
gullradriel edited this page 2026-10-12 07:59:13 +02:00

include/nilorea/n_allegro_4.h

Allegro 4 compatibility layer on top of Allegro 5. More...

Structures

Name
struct RGB
palette entry, unused in 32 bit mode
struct BITMAP
Allegro 4 bitmap.

Detailed Description

Allegro 4 compatibility layer on top of Allegro 5.

Lets old Allegro 4 / AllegroGL / AllegTTF programs build and run on Allegro 5 with their drawing, input, timer and sound calls unchanged: replace the <allegro.h>, <alleggl.h>, <winalleg.h> and "allegttf.h" includes with this header and link the program with nilorea.

This header deliberately exposes Allegro 4 names (BITMAP, screen, key, clear(), line(), rest(), font, ...) at global scope, which is the whole point of it and also why it is NOT included by nilorea.h: include it only from a program that wants the Allegro 4 API. It does not include any Allegro 5 header, so a translation unit sees one API only.

Model:

  • Only 32 bit colour is supported. Colours are Allegro 4 style ints, 0xAARRGGBB, where an alpha byte of 0 (what makecol() produces) means opaque, as in Allegro 4 where the alpha byte of makecol() is unused.
  • The mask colour is magenta (makecol(255,0,255)). Masked draws (draw_sprite, masked_blit, rotate_sprite, ...) use a cached copy of the sprite with the mask colour turned transparent, rebuilt only after the sprite changed.
  • screen is an off screen bitmap. It is shown on allegro_gl_flip(), or right after a draw to it when the mode is not an OpenGL one (Allegro 4 showed screen drawing immediately).
  • key[], mouse_x, mouse_y, mouse_b and the readkey() buffer are kept up to date by a background thread, like Allegro 4's interrupts.
  • install_int() callbacks run on a timer thread: they must not draw.
  • Drawing must happen on the thread that called set_gfx_mode().
  • MIDI is played through libfluidsynth, loaded at run time, with the first General MIDI soundfont found (or $NILOREA_SOUNDFONT). Without either, MIDI calls succeed and stay silent.
  • File names are resolved case insensitively and with '' accepted as a separator, since most Allegro 4 games were written on Windows.
  • lexLoadMenuFromFile() / lexMenuRun() re-implement the old nilorea INI driven menu module used by some of those games: a [menu] section with bg= and item0= ... itemN=, and a [menu.item] section per item with x, y, h_align (left/center/right: the part of the image at x), v_align (bottom/middle/top: the side of y the image hangs, so bottom makes y the top edge), image and an optional click sound. Castagnier Mickael

1.0

08/10/2026

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */


#ifndef __NILOREA_ALLEGRO_4_COMPAT__
#define __NILOREA_ALLEGRO_4_COMPAT__

#ifdef __cplusplus
extern "C" {
#endif


#include <stdint.h>
#include <stdio.h>

#define ALLEGRO_ERROR_SIZE 256
extern char allegro_error[ALLEGRO_ERROR_SIZE];

/* Things Allegro 4 needed for DOS / Windows interrupt handlers. */
#define END_OF_MAIN()
#define END_OF_FUNCTION(x)
#define END_OF_STATIC_FUNCTION(x)
#define LOCK_VARIABLE(x)
#define LOCK_FUNCTION(x)

/* Text encoding, only plain ASCII / UTF-8 strings are handled. */
#define U_ASCII 0x41534338
#define U_UTF8 0x55544638
void set_uformat(int type);
int ustricmp(const char* s1, const char* s2);

typedef struct BITMAP {
    int w;
    int h;
    unsigned char** line;
    void* priv;
} BITMAP;

typedef struct RGB {
    unsigned char r;
    unsigned char g;
    unsigned char b;
    unsigned char filler;
} RGB;

typedef struct FONT FONT;
typedef struct SAMPLE SAMPLE;
typedef struct MIDI MIDI;

extern BITMAP* screen;
extern FONT* font;

#define SCREEN_W (screen ? screen->w : 0)
#define SCREEN_H (screen ? screen->h : 0)

/* Graphics modes. */
#define GFX_TEXT -1
#define GFX_AUTODETECT 0
#define GFX_AUTODETECT_FULLSCREEN 1
#define GFX_AUTODETECT_WINDOWED 2
#define GFX_SAFE 3
#define GFX_DIRECTX 4
#define GFX_DIRECTX_WIN 5
#define GFX_OPENGL_WINDOWED 6
#define GFX_OPENGL_FULLSCREEN 7
#define GFX_OPENGL 8

int allegro_init(void);
void allegro_exit(void);
void allegro_message(const char* format, ...);
int alert(const char* s1, const char* s2, const char* s3, const char* b1, const char* b2, int c1, int c2);
void set_color_depth(int depth);
int get_color_depth(void);
int set_gfx_mode(int card, int w, int h, int v_w, int v_h);
void set_window_title(const char* name);
int set_close_button_callback(void (*proc)(void));

/* AllegroGL. */
int install_allegro_gl(void);
void allegro_gl_set_allegro_mode(void);
void allegro_gl_unset_allegro_mode(void);
void allegro_gl_flip(void);
#ifndef GL_COLOR_BUFFER_BIT
#define GL_COLOR_BUFFER_BIT 0x00004000
#define glClear(mask) ((void)(mask))
#endif

/* Keyboard. Key codes are Allegro 5's, so key[] is indexed like it. */
int install_keyboard(void);
int poll_keyboard(void);
int keypressed(void);
int readkey(void);
void clear_keybuf(void);
#define KEY_MAX 227
extern volatile char key[KEY_MAX];
extern volatile int key_shifts;

#define KEY_A 1
#define KEY_B 2
#define KEY_C 3
#define KEY_D 4
#define KEY_E 5
#define KEY_F 6
#define KEY_G 7
#define KEY_H 8
#define KEY_I 9
#define KEY_J 10
#define KEY_K 11
#define KEY_L 12
#define KEY_M 13
#define KEY_N 14
#define KEY_O 15
#define KEY_P 16
#define KEY_Q 17
#define KEY_R 18
#define KEY_S 19
#define KEY_T 20
#define KEY_U 21
#define KEY_V 22
#define KEY_W 23
#define KEY_X 24
#define KEY_Y 25
#define KEY_Z 26
#define KEY_0 27
#define KEY_1 28
#define KEY_2 29
#define KEY_3 30
#define KEY_4 31
#define KEY_5 32
#define KEY_6 33
#define KEY_7 34
#define KEY_8 35
#define KEY_9 36
#define KEY_F1 47
#define KEY_F2 48
#define KEY_F3 49
#define KEY_F4 50
#define KEY_F5 51
#define KEY_F6 52
#define KEY_F7 53
#define KEY_F8 54
#define KEY_F9 55
#define KEY_F10 56
#define KEY_F11 57
#define KEY_F12 58
#define KEY_ESC 59
#define KEY_BACKSPACE 63
#define KEY_TAB 64
#define KEY_ENTER 67
#define KEY_SPACE 75
#define KEY_INSERT 76
#define KEY_DEL 77
#define KEY_HOME 78
#define KEY_END 79
#define KEY_PGUP 80
#define KEY_PGDN 81
#define KEY_LEFT 82
#define KEY_RIGHT 83
#define KEY_UP 84
#define KEY_DOWN 85
#define KEY_ENTER_PAD 91
#define KEY_LSHIFT 215
#define KEY_RSHIFT 216
#define KEY_LCONTROL 217
#define KEY_RCONTROL 218
#define KEY_ALT 219
#define KEY_ALTGR 220

/* Mouse. */
int install_mouse(void);
int poll_mouse(void);
void show_mouse(BITMAP* bmp);
void get_mouse_mickeys(int* mickeyx, int* mickeyy);
void position_mouse(int x, int y);
extern volatile int mouse_x;
extern volatile int mouse_y;
extern volatile int mouse_z;
extern volatile int mouse_b;

/* Timers. */
#define TIMERS_PER_SECOND 1193181L
#define SECS_TO_TIMER(x) ((long)(x) * TIMERS_PER_SECOND)
#define MSEC_TO_TIMER(x) ((long)(x) * (TIMERS_PER_SECOND / 1000))
#define BPS_TO_TIMER(x) (TIMERS_PER_SECOND / (long)(x))
#define BPM_TO_TIMER(x) ((60 * TIMERS_PER_SECOND) / (long)(x))
int install_timer(void);
int install_int(void (*proc)(void), int speed);
int install_int_ex(void (*proc)(void), long speed);
void remove_int(void (*proc)(void));
void rest(unsigned int ms);

/* Bitmaps. */
BITMAP* create_bitmap(int width, int height);
BITMAP* create_video_bitmap(int width, int height);
BITMAP* create_bitmap_ex(int depth, int width, int height);
void destroy_bitmap(BITMAP* bmp);
BITMAP* load_bitmap(const char* filename, RGB* pal);
int save_bitmap(const char* filename, BITMAP* bmp, const RGB* pal);
int bitmap_color_depth(BITMAP* bmp);
int bitmap_mask_color(BITMAP* bmp);
int is_memory_bitmap(BITMAP* bmp);
void clear(BITMAP* bmp);
void clear_bitmap(BITMAP* bmp);
void clear_to_color(BITMAP* bmp, int color);

void blit(BITMAP* source, BITMAP* dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height);
void masked_blit(BITMAP* source, BITMAP* dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height);
void stretch_blit(BITMAP* source, BITMAP* dest, int source_x, int source_y, int source_width, int source_height, int dest_x, int dest_y, int dest_width, int dest_height);
void masked_stretch_blit(BITMAP* source, BITMAP* dest, int source_x, int source_y, int source_w, int source_h, int dest_x, int dest_y, int dest_w, int dest_h);
void draw_sprite(BITMAP* bmp, BITMAP* sprite, int x, int y);
void draw_sprite_h_flip(BITMAP* bmp, BITMAP* sprite, int x, int y);
void draw_sprite_v_flip(BITMAP* bmp, BITMAP* sprite, int x, int y);
void draw_sprite_vh_flip(BITMAP* bmp, BITMAP* sprite, int x, int y);
void draw_trans_sprite(BITMAP* bmp, BITMAP* sprite, int x, int y);
void stretch_sprite(BITMAP* bmp, BITMAP* sprite, int x, int y, int w, int h);

typedef int32_t fixed;
#define itofix(x) ((fixed)((x) * 65536))
#define fixtoi(x) ((int)(((x) + 0x8000) >> 16))
#define ftofix(x) ((fixed)((x) * 65536.0 + ((x) < 0 ? -0.5 : 0.5)))
#define fixtof(x) ((double)(x) / 65536.0)
void rotate_sprite(BITMAP* bmp, BITMAP* sprite, int x, int y, fixed angle);
void rotate_scaled_sprite(BITMAP* bmp, BITMAP* sprite, int x, int y, fixed angle, fixed scale);
void pivot_sprite(BITMAP* bmp, BITMAP* sprite, int x, int y, int cx, int cy, fixed angle);
void pivot_scaled_sprite(BITMAP* bmp, BITMAP* sprite, int x, int y, int cx, int cy, fixed angle, fixed scale);

/* Primitives. */
#define DRAW_MODE_SOLID 0
#define DRAW_MODE_TRANS 5
void drawing_mode(int mode, BITMAP* pattern, int x_anchor, int y_anchor);
void solid_mode(void);
void set_trans_blender(int r, int g, int b, int a);
void set_alpha_blender(void);
void putpixel(BITMAP* bmp, int x, int y, int color);
int getpixel(BITMAP* bmp, int x, int y);
void line(BITMAP* bmp, int x1, int y1, int x2, int y2, int color);
void hline(BITMAP* bmp, int x1, int y, int x2, int color);
void vline(BITMAP* bmp, int x, int y1, int y2, int color);
void rect(BITMAP* bmp, int x1, int y1, int x2, int y2, int color);
void rectfill(BITMAP* bmp, int x1, int y1, int x2, int y2, int color);
void circle(BITMAP* bmp, int x, int y, int radius, int color);
void circlefill(BITMAP* bmp, int x, int y, int radius, int color);
void ellipse(BITMAP* bmp, int x, int y, int rx, int ry, int color);
void ellipsefill(BITMAP* bmp, int x, int y, int rx, int ry, int color);
void triangle(BITMAP* bmp, int x1, int y1, int x2, int y2, int x3, int y3, int color);

/* Direct pixel access, as used by libraries like PMASK. */
void bmp_select(BITMAP* bmp);
uintptr_t bmp_write_line(BITMAP* bmp, int y);
uintptr_t bmp_read_line(BITMAP* bmp, int y);
void bmp_unwrite_line(BITMAP* bmp);
uint32_t _a4_pixel_from_color(int color);
#define bmp_write32(addr, c) (*((uint32_t*)(uintptr_t)(addr)) = _a4_pixel_from_color(c))
#define bmp_write24(addr, c) bmp_write32(addr, c)
#define bmp_write16(addr, c) bmp_write32(addr, c)
#define bmp_write15(addr, c) bmp_write32(addr, c)
#define bmp_write8(addr, c) bmp_write32(addr, c)

/* Colours. */
int makecol(int r, int g, int b);
int makecol32(int r, int g, int b);
int makeacol(int r, int g, int b, int a);
int makeacol32(int r, int g, int b, int a);
int getr(int c);
int getg(int c);
int getb(int c);
int geta(int c);

/* Text. A colour of -1 draws white, a background of -1 is transparent. */
int text_length(const FONT* f, const char* str);
int text_height(const FONT* f);
void textout_ex(BITMAP* bmp, const FONT* f, const char* s, int x, int y, int color, int bg);
void textout_centre_ex(BITMAP* bmp, const FONT* f, const char* s, int x, int y, int color, int bg);
void textout_right_ex(BITMAP* bmp, const FONT* f, const char* s, int x, int y, int color, int bg);
void textprintf_ex(BITMAP* bmp, const FONT* f, int x, int y, int color, int bg, const char* format, ...);
void textprintf_centre_ex(BITMAP* bmp, const FONT* f, int x, int y, int color, int bg, const char* format, ...);
void textprintf_right_ex(BITMAP* bmp, const FONT* f, int x, int y, int color, int bg, const char* format, ...);
void destroy_font(FONT* f);

/* AllegTTF. */
#define ALLEGTTF_NOSMOOTH 0
#define ALLEGTTF_TTFSMOOTH 1
#define ALLEGTTF_REALSMOOTH 2
FONT* load_ttf_font(const char* filename, int points, int smooth);
FONT* load_ttf_font_mem(const char* filename, int points, int smooth);

/* Sound. */
#define DIGI_AUTODETECT -1
#define DIGI_NONE 0
#define MIDI_AUTODETECT -1
#define MIDI_NONE 0
int install_sound(int digi, int midi, const char* cfg_path);
void remove_sound(void);
void set_volume(int digi_volume, int midi_volume);
SAMPLE* load_sample(const char* filename);
void destroy_sample(SAMPLE* spl);
int play_sample(const SAMPLE* spl, int vol, int pan, int freq, int loop);
void stop_sample(const SAMPLE* spl);
void adjust_sample(const SAMPLE* spl, int vol, int pan, int freq, int loop);
MIDI* load_midi(const char* filename);
void destroy_midi(MIDI* midi);
int play_midi(MIDI* midi, int loop);
void stop_midi(void);
void midi_pause(void);
void midi_resume(void);

/* Old nilorea Allegro 4 helpers. */
#ifndef GET
#define GET 4321
#endif
#ifndef SET
#define SET 1234
#endif
#define MODE_STD 0
#define MODE_OGL 1
int gfx_mode(int mode, int w, int h, int v_w, int v_h, int depth, double r, double g, double b, double a);
int gfx_status(int action, int value);
void fill_str(char* str, int c, size_t len);
int a4_get_keyboard(char* keybuf, int* cur, int flags, int max);
#define get_keyboard a4_get_keyboard

/* Old nilorea INI driven menu module. */
#define LEX_MENU_NORMAL 0
#define LEX_MENU_OVER 1
typedef struct LexMenu LexMenu;
LexMenu* lexLoadMenuFromFile(const char* file, const char* section);
BITMAP* lexMenuGetBitmap(LexMenu* menu, int item, int state);
int lexMenuRun(LexMenu* menu, BITMAP* buf);
LexMenu* lexMenuDestroy(LexMenu* menu);


#ifdef __cplusplus
}
#endif

#endif