Author Topic: JAPI 2.0 DLLC  (Read 6876 times)

0 Members and 1 Guest are viewing this topic.

JRS

  • Guest
JAPI 2.0 DLLC
« on: May 24, 2013, 10:37:19 AM »
I generated a generic C DLL of JAPI 2.0 and gave it a quick try with the Mandel example. The zip includes the japi.dll and japi.h files so trying this with O2 should be rather easy.



Code: [Select]
' JAPI 2.0 DLLC

DECLARE SUB dllfile ALIAS "dllfile" LIB "DLLC"
DECLARE SUB dllproc ALIAS "dllproc" LIB "DLLC"
DECLARE SUB dllcall ALIAS "dllcall" LIB "DLLC"

japi = dllfile("japi.dll")

j_start = dllproc(japi, "j_start i = ()")
j_frame = dllproc(japi, "j_frame i = (c * label)")
j_menubar = dllproc(japi, "j_menubar i = ( i obj)")
j_menu = dllproc(japi, "j_menu i = (i obj, c *label)")
j_menuitem = dllproc(japi, "j_menuitem i = (i obj, c *label)")
j_canvas = dllproc(japi, "j_canvas i = (i obj, i width , i height)")
j_setpos = dllproc(japi, "j_setpos (i obj, i xpos, i ypos)")
j_pack = dllproc(japi, "j_pack (i obj)")
j_show = dllproc(japi, "j_show (i obj)")
j_getaction = dllproc(japi, "j_getaction i = ()")
j_nextaction = dllproc(japi, "j_nextaction i = ()")
j_setcolor = dllproc(japi, "j_setcolor (i obj, i r, i g, i b)")
j_drawpixel = dllproc(japi, "j_drawpixel (i obj, i x, i y)")
j_quit = dllproc(japi, "j_quit ()")

CONST J_TRUE = 1
CONST J_FALSE = 0


xstart = -1.8
xend   =  0.8
ystart = -1.0
yend   =  1.0

hoehe  = 240
breite = 320

if (dllcall(j_start) = J_FALSE) then
  print("JAPI interface failed to start.\n")
  end
endif

jframe  = dllcall(j_frame,"JAPI 2.0 DLLC")
menubar = dllcall(j_menubar,jframe)
jfile   = dllcall(j_menu,menubar,"File")
calc    = dllcall(j_menu,menubar,"Calc")
quit    = dllcall(j_menuitem,jfile,"Quit")
start   = dllcall(j_menuitem,calc,"Start")
jstop   = dllcall(j_menuitem,calc,"Stop")

canvas  = dllcall(j_canvas,jframe,breite,hoehe)
dllcall(j_setpos,canvas,10,60)
dllcall(j_pack,jframe)
dllcall(j_show,jframe)

obj = 0
do_work = 0

while((obj <> jframe) and (obj <> quit))

    if(do_work = 1) then
        obj = dllcall(j_getaction)
    else
        obj = dllcall(j_nextaction)
    endif      

    if(obj = start) then
        x = -1
        y = -1
        do_work = 1
    endif

    if(obj = jstop) then
        do_work = 0
    endif
    
    if(do_work = 1) then
        x = (x+1) % breite
       if(x = 0) then
            y = (y+1) % hoehe
       endif
       if((x = breite-1) and (y = hoehe-1)) then
            do_work = 0
        else
            zre = xstart + x*(xend-xstart)/breite
            zim = ystart + y*(yend-ystart)/hoehe
            it = mandel(zre,zim,512)
            dllcall(j_setcolor,canvas,it*11,it*13,it*17)
            dllcall(j_drawpixel,canvas,x,y)
        endif
    endif

wend

dllcall(j_quit)


function mandel(zre,zim,maxiter)
    mx = 0.0
    my = 0.0
    iter=0
    betrag=0.0
 
    while ((iter < maxiter) and (betrag < 4.0))
        iter = iter+1
        tmp = mx*mx-my*my+zre
        my = 2*mx*my+zim
        mx = tmp
        betrag = (mx*mx + my*my)
    wend
    mandel=iter
end function



X

JRS

  • Guest
Re: JAPI 2.0 DLLC
« Reply #1 on: May 24, 2013, 04:59:52 PM »
Charles,

I tried to convert this example to O2 using the C header include option. It doesn't seem like O2 is seeing the japi.h prototypes for the functions and thinks they are undefined arrays. :-(

Code: [Select]
library "japi.dll"
include once "japi.h"

I could always convert the DLLC proto's to O2 external proto's if the japi.h method isn't going to work.


John
« Last Edit: May 24, 2013, 09:08:09 PM by JRS »

Charles Pegge

  • Guest
Re: JAPI 2.0 DLLC
« Reply #2 on: May 25, 2013, 12:41:36 AM »
Hi John,

A few issues:

1 header lines are not cr terminated chr(13,10)
2 the extern word being used as a declaration prefix.
3 types in the declaration prototype are not followed by a variable name.

I might be able to fix all of these on the Oxygen side.


JRS

  • Guest
Re: JAPI 2.0 DLLC
« Reply #3 on: May 25, 2013, 06:58:01 AM »
Thanks Charles for having a peek. I'm a little farther down the road with the DLLC definition of JAPI which would port to O2 rather easily.

I'm more concerned about the e-mail issue as I feel I'm talking to a ghost.  :'(

Charles Pegge

  • Guest
Re: JAPI 2.0 DLLC
« Reply #4 on: May 25, 2013, 07:36:26 AM »
Hi John I'm very close to a successful port to Oxygen, with the new C header accommodations. Can do half a Mandelbrot :)

Brains are still too overloaded for email, unfortunately. I have no further capacity.

PS: japi uses cdecl. Now I can get a whole Mandelbrot

JRS

  • Guest
Re: JAPI 2.0 DLLC
« Reply #5 on: May 25, 2013, 07:43:43 AM »
Cool!

About the e-mail, as long as you're not pissed at me for something I did wrong, focusing on what is important to you is all that matters.

Can you post or get the JAPI O2 changes to me in some manor?

Quote
PS: japi uses cdecl. Now I can get a whole Mandelbrot

Strange that DLLC worked without the cdecl in the prototype.

Charles Pegge

  • Guest
Re: JAPI 2.0 DLLC
« Reply #6 on: May 25, 2013, 09:49:25 AM »
Not at all John. I will eventually get to the email. I had to reduce my cognitive load temporarily.

Here is the enhanced Oxygen to hand japi.h unmodified:

http://www.oxygenbasic.org/forum/index.php?topic=749

DLLC cleans the stack before returning to SB, so, in fact cdecl / stdcall makes no difference. But this is not the case in Oxygen where the is a repeated call to a cdecl function in a long iteration.


X

JRS

  • Guest
Re: JAPI 2.0 DLLC
« Reply #7 on: May 25, 2013, 10:03:43 AM »
Works great Charles!

This was a good stress tester for the JAPI interface. This truly shows the difference between a compiled vs interpretive based application.

This might a nice addition to the O2 project directory.

Thanks for taking a time out to get this working.

Attached is a zip that contains the following:

japi.dll
japi.h
Oxygen.dll
mandel.o2bas
mandel.exe

Note: Java JRE (Java Runtime Environment) should be installed on your PC.


X
« Last Edit: May 25, 2013, 12:50:39 PM by JRS »

JRS

  • Guest
O2 JAPI 2.0
« Reply #8 on: May 25, 2013, 04:46:57 PM »
This is the drawables example converted from SB to O2.



Code: OxygenBasic
  1. ' drawables.o2bas
  2.    
  3. extern lib "japi.dll" cdecl
  4.   include once "japi.h"
  5. end extern
  6.  
  7. #lookahead
  8.  
  9. if( j_start() = J_FALSE ) then
  10.    print "can't connect to JAPI server"  
  11.   end
  12. endif
  13.  
  14. sys jframe = j_frame("OxygenBasic JAPI 2.0")
  15. j_setborderlayout(jframe)
  16.  
  17. sys menubar = j_menubar(jframe)
  18. sys jfile   = j_menu(menubar,"File")
  19. sys jprint  = j_menuitem(jfile,"Print")
  20. sys save    = j_menuitem(jfile,"Save BMP")
  21. sys quit    = j_menuitem(jfile,"Quit")
  22. sys canvas  = j_canvas(jframe,400,400)
  23. j_pack(jframe)
  24. j_show(jframe)
  25.  
  26. sys a=drawgraphics(canvas,0,0,j_getwidth(canvas)-10,j_getheight(canvas)-10)
  27.  
  28. sys obj=0
  29. while((obj <> jframe) and (obj <> quit))
  30.     obj = j_nextaction()
  31.  
  32.     if(obj = canvas) then
  33.         j_setnamedcolorbg(canvas,J_WHITE)
  34.        call drawgraphics(canvas,10,10,j_getwidth(canvas)-10,j_getheight(canvas)-10)
  35.     endif
  36.  
  37.     if(obj = jprint) then
  38.        jprinter = j_printer(jframe)
  39.        if(jprinter > 0) then
  40.           call drawgraphics(jprinter,40,40,j_getwidth(jprinter)-80,j_getheight(jprinter)-80)
  41.           j_print(jprinter)
  42.        endif
  43.     endif
  44.  
  45.     if(obj = save) then
  46.        rem    NOTE: problems with WinNT 24 Bit Colordepth (use 16 Bit)
  47.        image = j_image(600,800)
  48.        call drawgraphics(image,0,0,400,400)
  49.        if(j_saveimage(image,"test.bmp",J_BMP)=J_FALSE) then
  50.            a=j_alertbox(jframe,"Problems","Can't save image","OK")
  51.       endif
  52.     endif
  53.  
  54. wend
  55. j_quit()
  56.  
  57.  
  58.        
  59. function drawgraphics(drawable,xmin,ymin,xmax,ymax)
  60.  
  61.     fntsize=10
  62.     j_setfontsize(drawable,fntsize)
  63.     j_setnamedcolor(drawable,J_RED)
  64.  
  65.     rem   Drawings
  66.     j_drawline(drawable,xmin,ymin,xmax-1,ymax-1)
  67.     j_drawline(drawable,xmin,ymax-1,xmax-1,ymin)
  68.     j_drawrect(drawable,xmin,ymin,xmax-xmin-1,ymax-xmin-1)
  69.  
  70.     j_setnamedcolor(drawable,J_BLACK)
  71.     j_drawline(drawable,xmin,ymax-30,xmax-1,ymax-30)
  72.     tmpstr$ = "XMax = "+Str$(xmax)
  73.     j_drawstring(drawable,xmax/2-j_getstringwidth(drawable,tmpstr$)/2,ymax-40,tmpstr$)
  74.  
  75.     j_drawline(drawable,xmin+30,ymin,xmin+30,ymax-1)
  76.     tmpstr$ = "YMax = "+Str$(ymax)
  77.     j_drawstring(drawable,xmin+50,40,tmpstr$)
  78.  
  79.     j_setnamedcolor(drawable,J_MAGENTA)
  80.     for i=1 to 10
  81.         j_drawoval(drawable,xmin+(xmax-xmin)/2,ymin+(ymax-ymin)/2,(xmax-xmin)/20*i,(ymax-ymin)/20*i)
  82.     next i
  83.        
  84.     rem   Text
  85.     j_setnamedcolor(drawable,J_BLUE)
  86.     y=ymin
  87.     for i=5 to 22
  88.         j_setfontsize(drawable,i)
  89.         x = xmax-j_getstringwidth(drawable,"JAPI Test Text")
  90.         y = y+j_getfontheight(drawable)
  91.         j_drawstring(drawable,x,y,"JAPI Test Text")
  92.     next i
  93.  
  94.     rem   Images
  95.     twux = j_loadimage("images/twux.gif")
  96.     if(twux > 0) then
  97.         j_drawimage(drawable,twux,100,200)
  98.         j_drawscaledimage(drawable,twux,10,0,35,30,100,300,110,138)
  99.     endif
  100.            
  101. end function
  102.  

JRS

  • Guest
O2 JAPI 2.0
« Reply #9 on: May 25, 2013, 06:11:02 PM »
I got this to work but how SB and O2 sees arrays is different. Maybe Charles can decipher what is going on.



Code: [Select]
' graphic.o2bas

extern lib "japi.dll" cdecl
  include once "japi.h"
end extern

#lookahead

if( j_start() = J_FALSE ) then
   print "can't connect to JAPI server"  
   end
endif

sys i,x,y
dim as sys x[9]
dim as sys y[9]

for i = 0 to 9
   x[i]=(i+1)*10
    if mod(i,1)<>0 then
       y[i]=10
    else
       y[i]=90
    end if    
next i


sys jframe   = j_frame("OxygenBasic JAPI 2.0")
j_setsize(jframe,720,260)

sys canvas  = j_canvas(jframe,700,230)
j_setpos(canvas,10,30)

j_show(jframe)
j_pack(jframe)

j_setnamedcolor(canvas,J_BLUE)

j_translate(canvas,10,10)
j_drawline(canvas,10,10,90,90)
j_drawstring(canvas,0,105,"Line")

j_translate(canvas,100,0)
j_drawpolygon(canvas,10,x,y)
j_drawstring(canvas,0,105,"Polygon")

j_translate(canvas,100,0)
j_drawrect(canvas,10,10,80,80)
j_drawstring(canvas,0,105,"Rectangle")

j_translate(canvas,100,0)
j_drawroundrect(canvas,10,10,80,80,20,20)
j_drawstring(canvas,0,105,"RoundRect")

j_translate(canvas,100,0)
j_drawcircle(canvas,50,50,40)
j_drawstring(canvas,0,105,"Circle")

j_translate(canvas,100,0)
j_drawoval(canvas,50,50,40,20)
j_drawstring(canvas,0,105,"Oval")

j_translate(canvas,100,0)
j_drawarc(canvas,50,50,40,30,113,210)
j_drawstring(canvas,0,105,"Arc")

j_translate(canvas,-600,100)
j_drawpolyline(canvas,10,x,y)
j_drawstring(canvas,0,105,"Polyline")

j_translate(canvas,100,0)
j_fillpolygon(canvas,10,x,y)
j_drawstring(canvas,0,105,"FillPolygon")

j_translate(canvas,100,0)
j_fillrect(canvas,10,10,80,80)
j_drawstring(canvas,0,105,"FillRectangle")

j_translate(canvas,100,0)
j_fillroundrect(canvas,10,10,80,80,20,20)
j_drawstring(canvas,0,105,"FillRoundRect")

j_translate(canvas,100,0)
j_fillcircle(canvas,50,50,40)
j_drawstring(canvas,0,105,"FillCircle")

j_translate(canvas,100,0)
j_filloval(canvas,50,50,40,20)
j_drawstring(canvas,0,105,"FillOval")

j_translate(canvas,100,0)
j_fillarc(canvas,50,50,40,30,113,210)
j_drawstring(canvas,0,105,"FillArc")

while(j_nextaction()<>jframe)
wend
      
j_quit()



Charles Pegge

  • Guest
Re: JAPI 2.0 DLLC
« Reply #10 on: May 25, 2013, 06:40:44 PM »
Thanks John, I have added a ProjectsB/Japi folder to OxygenBasic.

PS:

the default indexbase is 1, but you can set it to any value. 0, -1 etc.

Arrays are normally static

For the first element, the index may be omitted
x[1]==x

There are no boundary checks

indexbase 1
sys x[10]
sys y[10]

for i = 1 to 10
   x[ i]=i*10
    'if mod(i-1,1)<>0 then
    if i and 1 'odd
       y[ i]=10
    else
       y[ i]=90
    end if    
next

« Last Edit: May 25, 2013, 07:06:53 PM by Charles Pegge »

JRS

  • Guest
Re: JAPI 2.0 DLLC
« Reply #11 on: May 25, 2013, 06:46:20 PM »
This may be of help as it shows both YABASIC and SB as a template before being generated as the final language code.

Code: [Select]
rem Example graphic.bas

YABASIC    dim x(9)
YABASIC    dim y(9)
YABASIC    for i = 0 to 9
YABASIC       read x(i)
YABASIC       read y(i)
YABASIC    next i    
SCRIBA    import japi.bas
SCRIBA    for i = 0 to 9
SCRIBA       x[i]=(i+1)*10
SCRIBA        if(even(i)) then
SCRIBA           y[i]=10
SCRIBA        else
SCRIBA           y[i]=90
SCRIBA        endif    
SCRIBA    next i
    
    if( j_start() = J_FALSE ) then
        print "can't connect to JAPI server"  
        end
    endif
 
    jframe   = j_frame("Graphik Primitiven")
    j_setsize(jframe,720,260)

    canvas  = j_canvas(jframe,700,230)
    j_setpos(canvas,10,30)

    j_show(jframe)
    j_pack(jframe)

    j_setnamedcolor(canvas,J_BLUE)

rem      Normal

    j_translate(canvas,10,10)
    j_drawline(canvas,10,10,90,90)
    j_drawstring(canvas,0,105,"Line")

    j_translate(canvas,100,0)
SCRIBA    j_drawpolygon(canvas,10,x,y)
YABASIC    j_drawpolygon(canvas,10,x(),y())
    j_drawstring(canvas,0,105,"Polygon")

    j_translate(canvas,100,0)
    j_drawrect(canvas,10,10,80,80)
    j_drawstring(canvas,0,105,"Rectangle")

    j_translate(canvas,100,0)
    j_drawroundrect(canvas,10,10,80,80,20,20)
    j_drawstring(canvas,0,105,"RoundRect")

    j_translate(canvas,100,0)
    j_drawcircle(canvas,50,50,40)
    j_drawstring(canvas,0,105,"Circle")
print "6"

    j_translate(canvas,100,0)
    j_drawoval(canvas,50,50,40,20)
    j_drawstring(canvas,0,105,"Oval")

    j_translate(canvas,100,0)
    j_drawarc(canvas,50,50,40,30,113,210)
    j_drawstring(canvas,0,105,"Arc")


rem     Filled

    j_translate(canvas,-600,100)
SCRIBA    j_drawpolyline(canvas,10,x,y)
YABASIC    j_drawpolyline(canvas,10,x(),y())
    j_drawstring(canvas,0,105,"Polyline")

    j_translate(canvas,100,0)
SCRIBA    j_fillpolygon(canvas,10,x,y)
YABASIC    j_fillpolygon(canvas,10,x(),y())
    j_drawstring(canvas,0,105,"FillPolygon")

    j_translate(canvas,100,0)
    j_fillrect(canvas,10,10,80,80)
    j_drawstring(canvas,0,105,"FillRectangle")

    j_translate(canvas,100,0)
    j_fillroundrect(canvas,10,10,80,80,20,20)
    j_drawstring(canvas,0,105,"FillRoundRect")

    j_translate(canvas,100,0)
    j_fillcircle(canvas,50,50,40)
    j_drawstring(canvas,0,105,"FillCircle")

    j_translate(canvas,100,0)
    j_filloval(canvas,50,50,40,20)
    j_drawstring(canvas,0,105,"FillOval")

    j_translate(canvas,100,0)
    j_fillarc(canvas,50,50,40,30,113,210)
    j_drawstring(canvas,0,105,"FillArc")


    while(j_nextaction()<>jframe)
wend
      
    j_quit()
    end

YABASIC    data 10,10,20,90,30,10,40,90,50,10,60,90,70,10,80,90,90,10,100,90

JRS

  • Guest
Re: JAPI 2.0 DLLC
« Reply #12 on: May 25, 2013, 07:08:22 PM »


Code: [Select]
' graphic.o2bas

extern lib "japi.dll" cdecl
  include once "japi.h"
end extern

#lookahead

if( j_start() = J_FALSE ) then
   print "can't connect to JAPI server"   
   end
endif

sys i
indexbase 1
sys x[10]
sys y[10]

for i = 1 to 10
   x[ i]=i*10
    'if mod(i-1,1)<>0 then
    if i and 1 'odd
       y[ i]=10
    else
       y[ i]=90
    end if   
next

sys jframe   = j_frame("OxygenBasic JAPI 2.0")
j_setsize(jframe,720,260)

sys canvas  = j_canvas(jframe,700,230)
j_setpos(canvas,10,30)

j_show(jframe)
j_pack(jframe)

j_setnamedcolor(canvas,J_BLUE)

j_translate(canvas,10,10)
j_drawline(canvas,10,10,90,90)
j_drawstring(canvas,0,105,"Line")

j_translate(canvas,100,0)
j_drawpolygon(canvas,10,x[],y[])
j_drawstring(canvas,0,105,"Polygon")

j_translate(canvas,100,0)
j_drawrect(canvas,10,10,80,80)
j_drawstring(canvas,0,105,"Rectangle")

j_translate(canvas,100,0)
j_drawroundrect(canvas,10,10,80,80,20,20)
j_drawstring(canvas,0,105,"RoundRect")

j_translate(canvas,100,0)
j_drawcircle(canvas,50,50,40)
j_drawstring(canvas,0,105,"Circle")

j_translate(canvas,100,0)
j_drawoval(canvas,50,50,40,20)
j_drawstring(canvas,0,105,"Oval")

j_translate(canvas,100,0)
j_drawarc(canvas,50,50,40,30,113,210)
j_drawstring(canvas,0,105,"Arc")

j_translate(canvas,-600,100)
j_drawpolyline(canvas,10,x[],y[])
j_drawstring(canvas,0,105,"Polyline")

j_translate(canvas,100,0)
j_fillpolygon(canvas,10,x[],y[])
j_drawstring(canvas,0,105,"FillPolygon")

j_translate(canvas,100,0)
j_fillrect(canvas,10,10,80,80)
j_drawstring(canvas,0,105,"FillRectangle")

j_translate(canvas,100,0)
j_fillroundrect(canvas,10,10,80,80,20,20)
j_drawstring(canvas,0,105,"FillRoundRect")

j_translate(canvas,100,0)
j_fillcircle(canvas,50,50,40)
j_drawstring(canvas,0,105,"FillCircle")

j_translate(canvas,100,0)
j_filloval(canvas,50,50,40,20)
j_drawstring(canvas,0,105,"FillOval")

j_translate(canvas,100,0)
j_fillarc(canvas,50,50,40,30,113,210)
j_drawstring(canvas,0,105,"FillArc")

while(j_nextaction()<>jframe)
wend
     
j_quit()
« Last Edit: May 25, 2013, 07:16:00 PM by JRS »

Charles Pegge

  • Guest
Re: JAPI 2.0 DLLC
« Reply #13 on: May 25, 2013, 07:24:38 PM »
Yes, that it what I get. y alternates while x increases.

PS:

Filling an array with data (C style)

sys a[10]={2,4,6,8,10,12,14,16,18,20}

JRS

  • Guest
Re: JAPI 2.0 DLLC
« Reply #14 on: May 25, 2013, 07:27:09 PM »
I think this is what it should look like. (even not odd) (switched  the values with ELSE)



Code: [Select]
indexbase 1
sys x[10]
sys y[10]

for i = 1 to 10
   x[ i]=i*10
    ' if mod(i-1,1)<>0 then
    if i and 1
       y[ i]=90
    else
       y[ i]=10
    end if    
next