GLBasic forum

Codesnippets => Code Snippets => Topic started by: spacefractal on 2017-Aug-30

Title: DRAWRECT using POLYVECTOR from a Image
Post by: spacefractal on 2017-Aug-30
On Android we cant use DRAWRECT and other commands. Its might fails on some devices and this will NOT been fixed at all. Its impossible.

But we can use POLYVECTOR, which works fine on Android. Here is a possible work around:

Code (glbasic) Select

GLOBAL IS24BIT=FALSE // FALSE ON ANDROID, TRUE ON OTHER PLATFORMS).

FUNCTION PaintColorRectRGB_START: imageID
IF IS24BIT=TRUE THEN RETURN
LOCAL ww, hh
GETSPRITESIZE imageID, ww, hh
IF ww=0 THEN RETURN

STARTPOLY imageID, 2;
ENDFUNCTION

FUNCTION PaintColorRectRGB: x, y, w, h, rr%, gg%, bb%, id=0
//IF x>ScreenWidth THEN RETURN
//IF y>ScreenHeight THEN RETURN

IF IS24BIT=FALSE
rr=INTEGER(rr/16)
gg=INTEGER(gg/16)
bb=INTEGER(bb/16)

LOCAL xx%=rr*64+gg*4
LOCAL yy%=bb*4

IF xx>512
xx=xx-512
yy=yy+64
ENDIF
xx=xx*2+1
yy=yy*2+1
IF xx>512
xx=xx-512
yy=yy+256
ENDIF
POLYVECTOR x, y, xx+1, yy
POLYVECTOR x, y+h, xx+1, yy+1
POLYVECTOR x+w, y, xx+1, yy
POLYVECTOR x+w, y+h, xx+1, yy+1
POLYNEWSTRIP
ELSE
DRAWRECT x, y, w, h, RGB(rr, gg, bb)
ENDIF
ENDFUNCTION

FUNCTION PaintColorRectRGB_END:
IF IS24BIT=TRUE THEN RETURN
ENDPOLY
ENDFUNCTION


This code can been little bit slower, but better alternative.

This code was used in CatchOut to avoid DRAWRECT on Android, which can fails on some devices and even crash out.

The limit here is you can only use a palette from 4096 colors.
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: MrPlow on 2017-Aug-30
Hi Sf,

For PC use is this faster than regular Drawrect or more intensive?
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: MrPlow on 2017-Aug-30
tried to use it with loop of drawrects to replace but cant seem to figure out if I am missing something

I run the start part then

Loop
    Run the regular paint image
endloop
Do I need to close it off?
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: spacefractal on 2017-Aug-30
You need to start, then draw and then close it'd off. This is how polyester works. You can draw more than one in a batch. However drawrect is faster. This was a safe operation on Android. Hence the ekstra variable for other systems.....
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: Kitty Hello on 2017-Nov-25
Another option is to use mem2sprite and do a 1x1 pixel sprite. Use smoothshading false!
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: Qedo on 2017-Dec-03
sorry but two things I did not understand:
1) @spacefractal: there is a limit of 4096 colors for android?
2) @Kitty Hello: What do you mean by "option is to use mem2sprite and do a 1x1 pixel sprite" about DRAWRECT?
ciao
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: spacefractal on 2018-Jan-01
no android is 16bit color, rgb656. Its just me that wanted to do a 4096 color limit to avoid clutter to many colors on the image. you can do the image bigger with more colors.

Im just used here with 4096, which was nice for CatchOut game, where im used it.
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: MrPlow on 2018-Feb-20
Here are my DRAWRECT replacements, is there an ideal way to to DRAWLINE using Polyvector?
I assume DRAWLINE is also broken on Android??


Code (glbasic) Select


FUNCTION NewStar: x#,y#,w,h,col
        // polyvector way of doing it:
       STARTPOLY -1
        POLYVECTOR x+(w/2), y, 0,0,col
        POLYVECTOR x, y+(h/2), 0,0,col
        POLYVECTOR x+(w/2), y+h, 0,0,col
        POLYVECTOR x+w, y+(h/2), 0,0,col
ENDPOLY

ENDFUNCTION

FUNCTION NewRect: x#,y#,w,h,col
        // polyvector way of doing it:
       STARTPOLY -1
        POLYVECTOR x, y, 0,0,col
        POLYVECTOR x, y+h, 0,0,col
        POLYVECTOR x+w, y+h, 0,0,col
        POLYVECTOR x+w, y, 0,0,col
ENDPOLY


ENDFUNCTION
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: Slydog on 2018-Feb-27
For a DRAWLINE replacement, I found some old code for drawing a line using POLYVECTORs.
You specify the start and end point, the line thickness, and the line colour.

http://www.glbasic.com/forum/index.php?topic=8796.msg74810#msg74810 (http://www.glbasic.com/forum/index.php?topic=8796.msg74810#msg74810)
Title: Re: DRAWRECT using POLYVECTOR from a Image
Post by: MrPlow on 2018-Mar-13
Thanks for this!

Quote from: Slydog on 2018-Feb-27
For a DRAWLINE replacement, I found some old code for drawing a line using POLYVECTORs.
You specify the start and end point, the line thickness, and the line colour.

http://www.glbasic.com/forum/index.php?topic=8796.msg74810#msg74810 (http://www.glbasic.com/forum/index.php?topic=8796.msg74810#msg74810)