Dark Bit Factory & Gravity

PROGRAMMING => Other languages => Blitz => Topic started by: Paul on January 03, 2007

Title: Writepixel dll[BB2D]
Post by: Paul on January 03, 2007
Hi
Would it be possible to make a "Writepixel dll" for Blitz in C++? :inspired:
Since the built in one is so slow.  :(

I can unfortunately not program in C++ and absolutely not make dlls. I'm thinking it could be done by letting blitz send an array to the dll and the dll drawing the whole screen, maybe masking some colour(s).



Title: Re: Writepixel dll
Post by: mike_g on January 03, 2007
Sorry, I dont know how that could be done but have you tried using SDL (http://www.libsdl.org/index.php)?
Title: Re: Writepixel dll
Post by: Ghost^BHT on January 03, 2007
 Blitz+ (although I don't have it) is faster than blitz or blitz3d for pixelwise operations. If you search you can also find some functions written by coders that uses banks to mimic the locked pixel command of B+ and that's supposed to be faster.
Title: Re: Writepixel dll
Post by: Paul on January 03, 2007
It could be done using api_setpixel but thats probably slower than the built in ones in blitz
Title: Re: Writepixel dll
Post by: Jim on January 03, 2007
In blitz it seems to always be faster to draw into a bank or array and copy it to the screen wholesale, even if that means using writepixelfast to move it over.

api_SetPixel is calling Windows GDI?  If so that is REALLY slow.

Jim
Title: Re: Writepixel dll
Post by: Stonemonkey on January 04, 2007
I have no idea if this'll work or how to get it working in blitz but it's a little dll written in freebasic that uses tiny_ptc that i'd started messing around with ages ago. You won't be able to use any of blitz's graphics functions with this though if you can get it working.

http://www.stonemonkie.pwp.blueyonder.co.uk/sm_dll.zip

EDIT: Just tried to get this working in blitz but can't seem to work it out.
Title: Re: Writepixel dll
Post by: Paul on January 04, 2007
Would it be possible to run a sub from a dll?
Do you do it the same way as you run a function???
Title: Re: Writepixel dll
Post by: Stonemonkey on January 04, 2007
I think so but with blitz the dll has to be placed in the userlibs folder along with a .decls file which in this case should look something like:

file 'sm_dll.decls'
Code: [Select]
.lib "sm_dll.dll"

create_buffer%( w%,h% ):"create_buffer"
setup_graphics%( w%,h% ):"setup_graphics"
close_graphics( b% ):"close_graphics"
flip_buffer( b% ):"flip_buffer"
clear_buffer( bi%,c% ):"clear_buffer"

set_pixel( bi%,x%,y%,c% ):"set_pixel"
get_pixel%( bi%,x%,y% ):"get_pixel"

But there's something wrong somewhere.

Title: Re: Writepixel dll
Post by: Paul on January 04, 2007
I've found that the builtin commands (writepixelfast) is about 2X the speed of this but its a good start :D and you dont need to think about the lockbuffer widh i usually forget

decels by paul at least some of them seem to work

Code: [Select]
.lib "sm_dll.dll"
create_buffer%(wwidth,height):"create_buffer@8"
setup_graphics(wwidth,height):"setup_graphics@8"
close_graphics(bufferi):"close_graphics@4"
flip_buffer(bufferi):"flip_buffer@4"
clear_buffer(bufferi):"clear_buffer@8"

set_pixel(bufferi,x,y,argb):"set_pixel@16"
get_pixel%(bufferi,x,y):"get_pixel@12"
set_pixel_fast(bufferi,x,y,argb):"set_pixel_fast@16"
get_pixel_fast%(bufferi,x,y):"get_pixel_fast@12"

fill_rect(bufferi,x0,y0,x1,y1,argb):"fill_rect@24"
fill_triangle(bufferi,x0,y0,x1,y1,x2,y2,argb):"fill_triangle@32"
fill_gtriangle(bufferi,x0,y0,argb0,x1,y1,argb1,x2,y2,argb2):"fill_gtriangle@40"

;::::::::::::::::::::::::::::::::::::::::

setup_graphics(400,300)
buff=create_buffer(400,300)
flip_buffer(buff)

While Not KeyHit(1)
fill_rect(buff,10,10,100,100,255)
flip_buffer(buff)
Wend

;:::::::::::::::::::::::::::::::::::::::::::::::::

Edit:
Code: [Select]
setup_graphics(400,300)
buff=create_buffer(400,300)
flip_buffer(buff)

While Not KeyHit(1)
;fill_rect(buff,10,10,100,100,255)
For x=0 To 400
For y=0 To 300
set_pixel_fast(buff,x,y,255)
Next
Next
flip_buffer(buff)
Wend
Title: Re: Writepixel dll
Post by: Stonemonkey on January 04, 2007
Sorry, I'm off out now but well done on getting that working so far.
Title: Re: Writepixel dll
Post by: Paul on January 04, 2007
I'm wondering something else is it faster having a bank or an array for your screen array.
Title: Re: Writepixel dll
Post by: Stonemonkey on January 04, 2007
When writing software 3d stuff in blitz a while ago I tried both but even though using an array involved writing everything to the screen buffer at the end I found hardly any difference and just stuck to using arrays as far as i can remember.
Title: Re: Writepixel dll
Post by: Stonemonkey on January 04, 2007
just noticed, your test code should be:
Code: [Select]
buff=setup_graphics(400,300)

While Not KeyHit(1)
   ;fill_rect(buff,10,10,100,100,255)
   For x=0 To 399
      For y=0 To 299
         set_pixel_fast(buff,x,y,255)
      Next
   Next
   flip_buffer(buff)
Wend

setup_graphics creates a buffer and returns it's handle although what you had should probably work too.
Another thing is that you would've been writing outside the buffer with 0 to 400 and 0 to 300 and set_pixel_fast doesn't have any bounds checking.
Title: Re: Writepixel dll
Post by: Stonemonkey on January 04, 2007
OK, I've got it working but it's very slllloooooowwwwwwwww. Not much use at all.

EDIT: oops, not used blitz for a while and had debug on. It's faster now but it's still messing up the keyboard, must be something to do with tiny_ptc.
Title: Re: Writepixel dll
Post by: Paul on January 04, 2007
An other thing...
If you use that test you will need to change the .decls file from

Code: [Select]
setup_graphics(wwidth,height):"setup_graphics@8"
to               
Code: [Select]
setup_graphics%(wwidth,height):"setup_graphics@8"
Title: Re: Writepixel dll
Post by: Stonemonkey on January 04, 2007
yep, and the tri functions take single floats for the screen coords so i have something like:

Code: [Select]
.lib "sm_dll.dll"

create_buffer%( wwidth%,height% ):"create_buffer@8"
setup_graphics%( wwidth%,height% ):"setup_graphics@8"
close_graphics( bufferi% ):"close_graphics@4"
flip_buffer( bufferi% ):"flip_buffer@4"
clear_buffer( bufferi% ):"clear_buffer@8"

set_pixel( bufferi%,x%,y%,argb% ):"set_pixel@16"
get_pixel%( bufferi%,x%,y% ):"get_pixel@12"
set_pixel_fast( bufferi%,x%,y%,argb% ):"set_pixel_fast@16"
get_pixel_fast%( bufferi%,x%,y% ):"get_pixel_fast@12"

fill_rect( bufferi%,x0%,y0%,x1%,y1%,argb% ):"fill_rect@24"
fill_triangle( bufferi%,x0#,y0#,x1#,y1#,x2#,y2#,argb% ):"fill_triangle@32"
fill_gtriangle( bufferi%,x0#,y0#,argb0%,x1#,y1#,argb1%,x2#,y2#,argb2% ):"fill_gtriangle@40"

also since i'm not able to read from the keyboard i was exiting with X on the window but the program was still running and i had to close it 'blitzcc.exe' from the task manager.
Title: Re: Writepixel dll
Post by: Paul on January 04, 2007
To read from the keyboard I would use the winapi, api_GetKeyState(), Note that the keys have different values.
And

Code: [Select]
api_GetCursorPos%(lpPoint)
together with

Code: [Select]
api_ScreenToClient(hWnd,lpPoint)
But then you would need to know the window handle to the sm_lib window

Edit:
Here bouth the keyboard and the mouse work :D
you probably will ned to add this to your user32.decls
Code: [Select]
FindWindow%( class%,Text$ ):"FindWindowA"
notice the class% instead of Class$

Code: [Select]
buff=setup_graphics(400,300)
flip_buffer(buff)


lpPoint = CreateBank(8)

Window=FindWindow(0,"SM_graphics");Find the WIndow

While Not quit
If api_GetKeyState(27)>2 Then quit=1;escape
api_GetCursorPos%(lpPoint)
api_ScreenToClient(Window,lpPoint)
mx = PeekInt(lpPoint,0)
my = PeekInt(lpPoint,4)
If mx=>0 And my=>0 And mx<400 And my<300
set_pixel_fast(buff,mx,my,255)
EndIf

flip_buffer(buff)

Wend
api_PostMessage(Window, $10, 0, 0)
End
Title: Re: Writepixel dll
Post by: Paul on January 04, 2007
Just hit the key(mouse) you want to find out the code for

Code: [Select]
Dim k(256)
For i=0 To 256
k(i)=api_GetKeyState(i)
Next
While Not KeyHit(1)
For i=0 To 256
If api_GetKeyState(i)<>k(i)
k(i)=api_GetKeyState(i)
Print i
EndIf
Next
Wend
Title: Re: Writepixel dll
Post by: Stonemonkey on January 05, 2007
Tiny_ptc isn't the best thing to be using here but I was curious as to what I could do anyway, if i come up with anything else I'll let you know. Probably better to have something written in c++ for something like this anyway.
Title: Re: Writepixel dll
Post by: Paul on January 05, 2007
I think so too and if i knew how to do c++ I would try, but i don,t :(
It feels so different to blitz with all the include files and i cant get my head arround it
Title: Re: Writepixel dll
Post by: Devils Child on July 08, 2007
i made a test on this dll.
i compared this to writepixelfast.
when i try to fill a 1280x1024 screen, i get 40 fps using writepixelfast and 12 fps using this sm lib...
so blitz isnt so slow anyway, huh?
Title: Re: Writepixel dll
Post by: Stonemonkey on July 08, 2007
Yeah, I never took it any further.