PDA

View Full Version : allegro dll



Lionheart008
19-12-2009, 00:00
hi all.

take attention: this is an experiment how to use "allegro.dll" for thinbasic.

I have found an old "allegro.dll" from year 2005 and tried to load as dll with thinbasic.
allegro is an 2d game programming library, written in c++ (came formerly from atari, so far as I can remember, long, long time ago), more infos you can find here:

a) http://www.talula.demon.co.uk/allegro/
b) http://en.wikipedia.org/wiki/Allegro_library

my question: it's possible to use the power of current release of "allegro" for thinbasic too ?

1) my example with zip-folder:


' Empty GUI script created on 12-18-2009 21:04:13 by (ThinAIR)
'-- try to load allegro bibliothek by frank

Uses "console", "ui"

Declare Function LoadLibrary Lib "KERNEL32.DLL" _
Alias "LoadLibraryA" _ '
( ByVal lpLibFileName As ASCIIZ) As Long

Declare Function GetProcAddress Lib "KERNEL32.DLL" _
Alias "GetProcAddress" _
( ByVal hModule As DWord, lpProcName As ASCIIZ ) As Long

Function TBMAIN () As Long
Dim hLib As Long
Dim osReturn As Long
Dim osType As Long

hLIB = LoadLibrary("ALLEG40.DLL")
If hLIB = 0 Then
MsgBox "Error, Allegro DLL does not exist"
Exit Function
End If

osReturn = GetProcAddress(hLib, "os_type")

osType = osReturn

MsgBox 0, "osType contains : " & Str$(osType)

End Function

this example works !

2) but I cannot catch (show list of) the properties of export data by:


Uses "EXE", "Console"
PrintL "allegro_test"

MsgBox 0, EXE_PE_GetExportList("ALLEG40.dll", $CRLF)
PrintL
PrintL "all ok with export List?"
WaitKey


any help and feedback would be appriciated and welcome. it's just a question, if this library does make sense for thinbasic if it should works with all its power ?

last edit: 23:31. fixed second code. new zipfile as attachment

good evening, frank lionhead

Michael Hartlef
19-12-2009, 00:13
change your code to




Uses "EXE","console"
PrintL "allegro_test"
Dim s As String
s= EXE_PE_GetExportList("alleg40.dll", $CRLF)
PrintL s
'PrintL "all ok with export List?"
WaitKey

Michael Hartlef
19-12-2009, 00:14
And change you please attach bigger screenshots. I can't read them. there is no need to redure their sizes if you save them as a png or jpg.

Lionheart008
19-12-2009, 00:25
hi michael :)

great! :occasion: - works now with "printl s", but why doesn't run with "msgbox" ?
pictures become bigger ;) see my first post. zip file updated.

frank

Michael Hartlef
19-12-2009, 00:38
I think it has to do with these numbers there. I don't know!

José Roca
19-12-2009, 22:29
1. The declare for LoadLibrary is wrong. Should be:



Declare Function LoadLibrary Lib "KERNEL32.DLL" _
Alias "LoadLibraryA" _ '
( lpLibFileName As ASCIIZ) As Long


2. osReturn = GetProcAddress(hLib, "os_type") doesn't return the OS type, but the address of the os_type function. With this address you have to use CALL DWORD, with an appropriate declare for the function os_type.

3. Before ending the application, you should call FreeLibrary.

Lionheart008
21-12-2009, 10:58
good morning, hello josé and thanks for advices!

perhaps you can have a look at my example, if I am on the right way, I am not sure ;)


' Empty GUI script created on 12-18-2009 21:04:13 by (ThinAIR)
'-- try to load allegro bibliothek by frank

Uses "console", "ui"

Declare Function LoadLibrary Lib "KERNEL32.DLL" _
Alias "LoadLibraryA" _ '
( ByVal lpLibFileName As ASCIIZ) As Long

Declare Function FreeLibrary Lib "KERNEL32.DLL" _
Alias "FreeLibraryA" _ '
( ByRef lpLibFileName As ASCIIZ*255 ) As DWord '%MAX_PATH

Declare Function FreeLibrary Lib "KERNEL32.DLL" _
Alias "FreeLibraryA" _ '
( ByVal lpLibFileName As ASCIIZ) As Long

Declare Function GetProcAddress Lib "KERNEL32.DLL" _
Alias "GetProcAddress" _
( ByVal hModule As DWord, lpProcName As ASCIIZ ) As Long

'- dummy defined ;) -----------------------------------------
'Declare Function os_Type Lib "ALLEG42.DLL" _
' Alias "os_Type" (ByVal param1, param2 As DWord) As Long
'- don't test it! -----------------------------------------


Function TBMAIN () As Long
Dim hLib, fLib As Long
Dim osReturn As Long
Dim osType As Long
Dim RetVal As Long
Dim s As String
Dim Result As Long

hLIB = LoadLibrary("ALLEG42.DLL")
If hLIB = 0 Then
MsgBox "Error, Allegro DLL cannot be loaded or found!"
Exit Function
End If

osReturn = GetProcAddress(hLib, "os_type")
osType = osReturn

'------ new input
'osType = os_type(param1, param2)
'Call DWord osType to RetVal '-- dummy defined, don't test it!
'------ new input end

MsgBox 0, "osType contains : " & Str$(osType)
'------ new input
'Call freelibrary("ALLEG42.DLL") To result '---------- doesn't work for thinbasic
'Call freeLibrary(hlib) to result
'------ new input end
End Function

I think I have to read whole stuff around creating and call dword / dll again. I have not fully understood this chapter, sorry. :sad5: but I don't give up!

best regards, thanks, frank

José Roca
21-12-2009, 19:02
You don't have any expertise using third party DLLs and translating C declares to Basic, yet, instead of practizing with an easy one, you choose a very difficult one :)

This is a PowerBASIC example that does what are you intending to do.



'SED_PBCC - Use the console compiler
#COMPILE EXE
#DIM ALL

DECLARE FUNCTION LoadLibrary LIB "KERNEL32.DLL" ALIAS "LoadLibraryA" (BYREF lpLibFileName AS ASCIIZ) AS DWORD
DECLARE FUNCTION FreeLibrary LIB "KERNEL32.DLL" ALIAS "FreeLibrary" (BYVAL hLibModule AS DWORD) AS LONG
DECLARE FUNCTION GetProcAddress LIB "KERNEL32.DLL" ALIAS "GetProcAddress" (BYVAL hModule AS DWORD, BYREF lpProcName AS ASCIIZ) AS DWORD

'int install_allegro(int system_id, int *errno_ptr, int (*atexit_ptr)());
DECLARE FUNCTION install_allegro CDECL LIB "alleg40.dll" ALIAS "install_allegro" (BYVAL system_id AS LONG, BYREF errno_ptr AS LONG, BYVAL atexit_ptr AS LONG) AS LONG

'void allegro_exit();
DECLARE SUB allegro_exit CDECL LIB "alleg40.dll" ALIAS "allegro_exit"

FUNCTION PBMAIN () AS LONG

LOCAL lr AS LONG
LOCAL errno AS LONG

' Initialize Allegro
' Note: errno should be a pointer to the standard C _errno global variable
' and the last parameter should be a pointer to the standard C atexit function,
' but PB doesn't use the C library, so we will pass a long variable and a null pointer.
' This means that we must close the allegro library by ourselves calling the
' allegro_exit procedure.
lr = install_allegro(0, errno, 0)
IF lr <> 0 THEN EXIT FUNCTION ' Error initializing Allegro

LOCAL hLib AS DWORD
LOCAL postype AS LONG PTR
LOCAL ostype AS LONG

' Note: os_type isn't a function, but an exported global variable
hLib = LoadLibrary("alleg40.dll")
IF hLib THEN
postype = GetProcAddress(hLib, "os_type")
IF postype THEN ostype = @postype
FreeLibrary hLib
END IF

' Note: ostype is a four character code stored in a long.
' To convert it to a string, we can use MKL$ and reverse the string.
IF ostype THEN PRINT STRREVERSE$(MKL$(ostype))

' Shutdown Allegro
allegro_exit

WAITKEY$

END FUNCTION

Lionheart008
22-12-2009, 12:57
thank you josé for this solution ! :) I have applauded to you :occasion:

my translation for thinbasic:



' Empty GUI script created on 12-18-2009 21:04:13 by (ThinAIR)
'-- try to load allegro bibliothek by frank

Uses "console", "ui"

Declare Function LoadLibrary Lib "KERNEL32.DLL" Alias "LoadLibraryA" (ByRef lpLibFileName As ASCIIZ) As DWord
Declare Function FreeLibrary Lib "KERNEL32.DLL" Alias "FreeLibrary" (ByVal hLibModule As DWord) As Long
Declare Function GetProcAddress Lib "KERNEL32.DLL" Alias "GetProcAddress" (ByVal hModule As DWord, ByRef lpProcName As ASCIIZ) As DWord

'int install_allegro(int system_id, int *errno_ptr, int (*atexit_ptr)());
Declare Function install_allegro CDECL Lib "alleg42.dll" Alias "install_allegro" (ByVal system_id As Long, ByRef errno_ptr As Long, ByVal atexit_ptr As Long) As Long

'void allegro_exit();
Declare Sub allegro_exit CDECL Lib "alleg42.dll" Alias "allegro_exit"


Function TBMAIN () As Long

Local lr As Long
Local errno As Long

' Initialize Allegro
' Note: errno should be a pointer to the standard C _errno global variable
' and the last parameter should be a pointer to the standard C atexit function,
' but PB doesn't use the C library, so we will pass a long variable and a null pointer.
' This means that we must close the allegro library by ourselves calling the
' allegro_exit procedure.
lr = install_allegro(0, errno, 0)
If lr <> 0 Then Exit Function ' Error initializing Allegro

Local hLib As DWord
Local postype As Long 'PTR
Local ostype As Long

' Note: os_type isn't a function, but an exported global variable
hLib = LoadLibrary("alleg42.dll")
If hLib Then
postype = GetProcAddress(hLib, "os_type")
End If
If postype Then
ostype = PEEK(Long, postype) '@postype
FreeLibrary(hLib)
End If

' Note: ostype is a four character code stored in a long.
' To convert it to a string, we can use MKL$ and reverse the string.
If ostype Then
MsgBox 0, "hello ostype: " + StrReverse$(MKL$(ostype)), %MB_OK, " test allegro"
End If

' Shutdown Allegro
allegro_exit

MsgBox 0, "allegro test was successful! "
PrintL "-- end of allegro test :) - push any key to exit! --"
WaitKey

End Function

'-- result for powerbasic : "wxp"
'-- result for thinbasic : "wxp"




MsgBox 0, "hello ostype: " + StrReverse$(MKL$(ostype)), %MB_OK, " test allegro"

good to know:

' Note: os_type isn't a function, but an exported global variable
hLib = LoadLibrary("alleg40.dll")
IF hLib THEN
postype = GetProcAddress(hLib, "os_type")
IF postype THEN ostype = @postype
FreeLibrary hLib
END IF

both results are the same, I am very glad! 8)

for a) powerbasic : "wxp"
for b) thinbasic: "wxp"

info: zip file I add with tb example and allegro422.dll.

best regards, merry christmas for you josé too, frank
(what about the weather in spain ? there's no rain, no snow, only pure sunshine ?)

José Roca
22-12-2009, 16:06
After the warmest Autumn on record, we have been hit by a Siberian cold wave and it is raining and snowing a lot.

kryton9
22-12-2009, 22:16
Frank, I applaud you-- taking on challenging things. When I study these types of conversions it makes sense, but looking at something finished and making it happen are two different things. Keep up the studies and your efforts at being David and taking on Goliath, it is inspirational for me. When you go to Jose's site and see all the incredible work he has done in bringing the massive mess of things from the c world to PowerBasic, it is mind numbing the work and effort he has put in!

José Roca
22-12-2009, 22:35
One clarification. The variable errno must be accessible to Allegro until you close that library. Therefore, if you are going to declare it outside the main window procedure, make it static or global.

TomLebowski
23-12-2009, 18:57
thanks a lot to frank and jose for conversion allegro to thinbasic. its possible to make an include file like "freeimage.inc" ? I know allegro.bi from freebasic some years ago, perhaps you can use this files for translation to thinbasic. I am sure there is a serious way. I`ve got "wxp" after allegro42.dll testing :) so far as I know there is a new update for allegro at her websites. bye, tom

José Roca
23-12-2009, 21:14
its possible to make an include file like "freeimage.inc" ?


Everything is possible, but it would be a lot of work. It is a "very C" library, using all kind of "tricks" that that language supports. Exported static variables and arrays, string codes and float values packed in longs, pointers to structures with members that are function pointers, etc. Look at what has been needed just to get the OS version...

The (outdated) translation to FreeBasic doesn't help a bit. FreeBasic is a C compiler with some Basic-like syntax, but following C rules.



so far as I know there is a new update for allegro at her websites.


But no DLL.

Michael Hartlef
24-12-2009, 10:09
thanks a lot to frank and jose for conversion allegro to thinbasic. its possible to make an include file like "freeimage.inc" ? I know allegro.bi from freebasic some years ago, perhaps you can use this files for translation to thinbasic. I am sure there is a serious way. I`ve got "wxp" after allegro42.dll testing :) so far as I know there is a new update for allegro at her websites. bye, tom


Hi Tom,

can you share your motivation to use it with thinBasic?

Cheers
Michael