Author Topic: Advancted version of PaintImage()  (Read 1344 times)

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3447
    • View Profile
    • Space Fractal
Advancted version of PaintImage()
« on: 2016-May-02 »
IN few years ago im did a PaintImage, which can been used as a visual Image for scaling to Android and such device. In Karma Miwa im also used this function to do either rotating (special<0) or mirror/flipping (special=0 to 3).

Those variables is required as global variables (Those variables starting in P is the scaled calculate for the box image, can been done by using 0 as alpha:
SCALING
ScreenWidth
ScreenHeight
PX
PY
PW
PH

Here is the code:
Code: GLBasic [Select]
FUNCTION PaintImage: img, x#, y#, xspot, yspot, zoom#, alpha#, special=0

        LOCAL h#, w#, hh#, ww#, xx#, yy#, SH, SW, i#, r#, animw%, animh%, orignalframeh, originalframew
        GETSPRITESIZE img, w, h
        IF w<1 OR h<1
                RETURN 0
        ENDIF
        IF special<0
                originalframew=GetStr$(Name$+".w", "Anim"); animw=w/originalframew
                orignalframeh=GetStr$(Name$+".h", "Anim"); animh=h/orignalframeh
                w=w/animw
                h=h/animh
        ENDIF

        // math placement for hotspots
        IF xspot=-1 THEN PX=x#+0
        IF xspot=0 THEN PX=(ScreenWidth)/2.0-(w#*zoom#/2.0)+x#+0.5
        IF xspot=1 THEN PX=(ScreenWidth)-w#*zoom#-x#+0.5
        IF yspot=-1 THEN PY=y#
        IF yspot=0 THEN PY=(ScreenHeight)/2.0-(h*zoom#/2.0)+y#
        IF yspot=1 THEN PY=(ScreenHeight)-h#*zoom#-y#

        IF PY-PH>ScreenHeight OR PX-PW>ScreenWidth OR PY<-h*zoom*1.2 OR PX<-w*zoom*1.2 THEN RETURN 0
        PW=INTEGER(w*zoom#)
        PH=INTEGER(h*zoom#)

        // some alpha code in that way I prefer. Not a issue in the real alphamode, its just me :-D
        IF alpha>0
                SetAlphaMode(alpha#)
        ENDIF
        IF alpha>0
                IF special=0 OR special=1
                        ZOOMSPRITE img, PX#-((1-zoom#)*w#)/2, INTEGER(PY#-((1-zoom#)*h#)/2), PW#/w#, PH#/h#
                ELSEIF special=2
                        ZOOMSPRITE img, PX#-((1-zoom#)*w#)/2, INTEGER(PY#-((1-zoom#)*h#)/2), -PW#/w#, PH#/h#
                ELSEIF special=3
                        ZOOMSPRITE img, PX#-((1-zoom#)*w#)/2, INTEGER(PY#-((1-zoom#)*h#)/2), PW#/w#, -PH#/h#
                ELSEIF special=4
                        ZOOMSPRITE img, PX#-((1-zoom#)*w#)/2, INTEGER(PY#-((1-zoom#)*h#)/2), -PW#/w#, -PH#/h#
                ELSEIF special>500
                        ROTOZOOMSPRITE img, PX#-((1-zoom#)*w#)/2, INTEGER(PY#-((1-zoom#)*h#)/2), special-1000, zoom#
                ELSEIF special<0
                        PXC#=PX#-((1-zoom#)*w#)/2
                        PYC#=INTEGER(PY#-((1-zoom#)*h#)/2)
                        DRAWRECT PX, PY, PW, PH, RGB(200, 200, 200)
                ENDIF
        ENDIF
        RETURN 1
ENDFUNCTION

FUNCTION SetAlphaMode: alpha#=-5
        STATIC Lasta#
        IF alpha=-5
                ALPHAMODE Lasta#
                RETURN
        ENDIF
        Lasta#=alpha#
        IF alpha#>0
                IF alpha#<=1.0
                        alpha#=0-alpha#
                ELSE
                        alpha#=-1+alpha#
                ENDIF
        ELSE
                alpha#=-alpha#
        ENDIF
        ALPHAMODE alpha#
ENDFUNCTION
 

You can also add a SCALING function to setup the SCALING its should, so you can avoid the virtual screen (here taking from CatchOut):

SCALING is a GLOBAL variable.

Code: GLBasic [Select]
FUNCTION SetScaling: width=88, height=59
        LOCAL sx, sy, scaling

        GETSCREENSIZE sx,sy
        scaling=sx/width
        IF scaling*height>sy THEN scaling=sy/height

        // Round up TO a perfect scaling, IF its very close
        LOCAL sc=INTEGER(FMOD(scaling, 1)*10)          // a integer reminder of a float.
        IF sc<>0 THEN scaling=scaling+0.025            // make sure the image is used the whole screen and not lost one px.
        IF sc=9 THEN scaling=INTEGER(scaling+0.1)      // if very close to a perfect scaling (like 1.9, 2.9 etc), perfect scaling it.
        IF sc>3 OR sc<7 THEN scaling=INTEGER(scaling)+0.5      // if very close to a half pixel perfect scaling (like 1.4, 2.4 etc).

        SCALING=scaling
        SCALING=INTEGER(SCALING)
        EXTRAPIXEL=0

        LOCAL rr=MOD(SCALING, 2)

        IF rr=1 THEN EXTRAPIXEL=1
        IF INTEGER(SCALING)=5 OR INTEGER(SCALING)=3 THEN EXTRAPIXEL=-1
ENDFUNCTION
 

All my games do also uses POLYVECTOR  for drawing the map, while enemies is used this function.
« Last Edit: 2016-May-02 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1300
    • View Profile
Re: Advancted version of PaintImage()
« Reply #1 on: 2016-Jun-07 »
Nice Code!

I have had good success with my own virtual methods, but am interested to try out your PaintImage technique and see how much faster it runs!

Comp:
Speccy-48k, Speccy-128k, Amigas, PCs