The Allegro Wiki is migrating to github at https://github.com/liballeg/allegro_wiki/wiki

Python DynLib test

From Allegro Wiki
Jump to: navigation, search

This is a test to show how you can directly access the Allegro dynamic library from python under unix.

<highlightSyntax language="python">

  1. !/usr/bin/env python

import dl

al = dl.open("/usr/local/lib/liballd.so.4.2") al.call("install_allegro", 0, 0, 0) al.call("install_keyboard") al.call("install_timer") al.call("install_mouse") al.call("show_os_cursor", 2) al.call("set_color_depth", 32) al.call("set_gfx_mode", 0, 256, 256, 0, 0)

screen = al.call("gui_get_screen")

for x in range(16):

   for y in range(16):
       pal = y * 16 + x
       r = (pal & 3) >> 0
       g = (pal & 12) >> 2
       b = (pal & 48) >> 4
       a = (pal & 192) >> 6
       a += 1
       r *= a
       g *= a
       b *= a
       r = 255 * r / 12
       g = 255 * g / 12
       b = 255 * b / 12
       rgb = r + (g << 8) + (b << 16)
       al.call("rectfill", screen, x * 16, y * 16, x * 16 + 15, y * 16 + 15,
               rgb);

al.call("readkey") al.call("allegro_exit") </highlightSyntax>

This is the same program under windows. <highlightSyntax language=python> from ctypes import *;

al = CDLL("alleg42.dll");

al.install_allegro(0, 0, 0); al.install_keyboard(); al.install_timer(); al.install_mouse(); al.show_os_cursor(2); al.set_color_depth(32); al.set_gfx_mode(0, 256, 256, 0, 0); screen = al.gui_get_screen();

for x in range(16):

   for y in range(16):
       pal = y * 16 + x
       r = (pal & 3) >> 0
       g = (pal & 12) >> 2
       b = (pal & 48) >> 4
       a = (pal & 192) >> 6
       a += 1
       r *= a
       g *= a
       b *= a
       r = 255 * r / 12
       g = 255 * g / 12
       b = 255 * b / 12
       rgb = r + (g << 8) + (b << 16)
       al.rectfill(screen, x * 16, y * 16, x * 16 + 15, y * 16 + 15,
               rgb);

al.readkey(); al.allegro_exit(); </highlightSyntax>