Clothe 2D stick figure

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: Clothe 2D stick figure

It works may be you missed some "_" in your file names !
e.g. "upperLegRight.png" must be "upper_Leg_Right.png"

By the way why you use the sane images twice ?

For example you need only to load "upper_Leg_Left.png" or "upper_Leg_Right.png"

With other word's you can draw one loaded image many times on different positions, sizes or rotation.

you can do this also:
var upperLegLeft = LoadIt("upper_Leg_Left.png")
var upperRightLeft = upperLegLeft

here are a zip file complete with multiput, FBImage and all images.
(should work on Linux/Windows 32/64-bit)

Stop the Windows only f*****s :-)

Joshy

Code: Select all

`#include once "FBImage.bi"#include once "multiput.bi"'MultiPut [destination],[xmidpos],[ymidpos],source,[xScale],[yScale],[Trans]'some useful definesConst Pi = 4 * Atn(1)Dim Shared As Double TwoPi = 8 * Atn(1)Dim Shared As Double RtoD = 180 / Pi   ' radians * RtoD = degreesDim Shared As Double DtoR = Pi / 180   ' degrees * DtoR = radianssub drawLine(x1 As Integer,y1 As Integer,x2 As Integer,y2 As Integer,size As Integer,c As ulong)  var dx = x2 - x1  var dy = y2 - y1  if dx = 0 andalso dy=0 then    circle (x1, y1), size, c, , , , f         elseif abs(dx) > abs(dy) then    var m = dy / dx    for x as Integer = x1 To x2 step sgn(dx)      circle (x,m * (x - x1) + y1), size, c, , , , f    next  else    var m =dx / dy    for y as Integer = y1 To y2 step sgn(dy)      circle (m * (y - y1) + x1,y), size, c, , , ,f    next  end ifend subchdir exepath()function LoadWithError(image as string) as any ptr  var ret = LoadRGBAFile(image)  if ret=0 then     print "error: can't load " & image & " !"    print "reason: " & *GetLastResult()    beep :sleep :end  end if    return retend function  screenres 640,480,32'load images into bitmapsvar head          = LoadWithError("head.png")var kneck         = LoadWithError("kneck.png")var torso         = LoadWithError("torso.png")var upperArmLeft  = LoadWithError("upper_Arm_Left.png")var lowerArmLeft  = LoadWithError("lower_Arm_Left.png")var leftHand      = LoadWithError("left_Hand.png")var upperLegLeft  = LoadWithError("upper_Leg_Left.png")var lowerLegLeft  = LoadWithError("lower_Leg_Left.png")var leftShoe      = LoadWithError("left_Shoe.png")var upperArmRight = upperArmLeftvar lowerArmRight = lowerArmLeftvar rightHand     = leftHandvar upperLegRight = upperLegLeftvar lowerLegRight = lowerLegLeftvar rightShoe     = leftShoedim shared as integer selected   'selected bone numberselected = 1                     'first bone as bone(0) is a dummydim shared as single mag         'magnification of imagemag = 1type BONE    as integer r       'reset start angle    as integer p       'pointer to previous bone    as integer x1      'absolute start    as integer y1    as integer x2      'end  point    as integer y2    as single  a       'angle of joint    as single  a2      'save computed angles    as single  aMin    'limits of joint movement    as single  aMax    as single  s       'size    as ulong   c       'color    as any ptr imgend typedim shared as BONE bones(0 to 15)'order in which to draw components for overlapdim shared as integer Order(1 to 15)for i as integer = 1 to 15    read order(i)next i'=================== LEG 1 ================bones(0).x2 = 320   'start pointbones(0).y2 = 240bones(1).r = 1bones(1).a = 53bones(2).a = 53bones(3).a = 239bones(1).s = 85bones(2).s = 75bones(3).s = 31bones(1).c = rgb(255,0,0)bones(2).c = rgb(255,0,0)bones(3).c = rgb(255,0,0)bones(1).p = 0bones(2).p = 1bones(3).p = 2bones(1).img = upperLegLeftbones(2).img = lowerLegLeftbones(3).img = leftShoe'==================== LEG 2 =====================bones(4).r = 1bones(4).a = 90 '59bones(5).a = 53bones(6).a = 239bones(4).s = 85bones(5).s = 75bones(6).s = 31bones(4).c = rgb(0,255,0)bones(5).c = rgb(0,255,0)bones(6).c = rgb(0,255,0)bones(4).p = 0bones(5).p = 4bones(6).p = 5bones(4).img = upperLegRightbones(5).img = lowerLegRightbones(6).img = rightShoe'===================== TORSO ========================bones(7).r = 1bones(7).a = 270bones(7).s = 85bones(7).c = rgb(0,0,255)bones(7).p = 0bones(7).img = torso'===================== ARM 1 =========================bones(8).r = 1bones(8).a = 81bones(9).a = 294bones(10).a = 33bones(8).s = 59bones(9).s = 42bones(10).s = 27bones(8).c = rgb(0,255,255)bones(9).c = rgb(0,255,255)bones(10).c = rgb(0,255,255)bones(8).p = 7bones(9).p = 8bones(10).p = 9bones(8).img = upperArmLeftbones(9).img = lowerArmLeftbones(10).img = leftHand'===================== ARM 2 =========================bones(11).r = 1bones(11).a = 135bones(12).a = 343bones(13).a = 320bones(11).s = 59bones(12).s = 42bones(13).s = 27bones(11).c = rgb(155,155,0)bones(12).c = rgb(155,155,0)bones(13).c = rgb(155,155,0)bones(11).p = 7bones(12).p = 11bones(13).p = 12bones(11).img = upperArmRightbones(12).img = lowerArmRightbones(13).img = rightHand'==================  KNECK AND HEAD ====================bones(14).r = 1bones(14).a = 270bones(14).s = 29bones(14).c = rgb(200,0,200)bones(14).p = 7bones(14).img = kneckbones(15).a = 90bones(15).s = 18bones(15).c = rgb(100,30,100)bones(15).p = 14bones(15).img = headdim shared as single anglesub drawBones()    color rgb(255,255,255)    screenlock    cls    locate 2,1    print " Tap space bar to move through pivot points"    print    print " You can select front pivots by clicking them with left mouse button"    print    print " Use left/right arrow keys OR keys [A] and [D] to rotate selected pivot"       'compute end point positions of each bone x1,y1,and x2,y2    angle = 0    for i as integer = 1 to 15        color bones(i).c        if bones(i).r = 1 then angle = 0:print        if bones(i).a < 0   then bones(i).a = bones(i).a + 360        if bones(i).a > 359 then bones(i).a = bones(i).a - 360        angle = angle + bones(i).a        bones(i).a2 = angle               'save computed angles for images        if angle < 0   then angle = angle + 360        if angle > 359 then angle = angle - 360        bones(i).x1 = bones( bones(i).p ).x2        bones(i).y1 = bones( bones(i).p ).y2        bones(i).x2 = bones(i).x1 + cos(angle*DtoR) * bones(i).s * mag        bones(i).y2 = bones(i).y1 + sin(angle*DtoR) * bones(i).s * mag    next i       'draw the bones    dim as integer ii    for i as integer = 1 to 15        'line (bones(i).x1,bones(i).y1)-(bones(i).x2,bones(i).y2),bones(i).c        'drawLine (bones(i).x1,bones(i).y1,bones(i).x2,bones(i).y2,2,bones(i).c)        'circle (bones(i).x1,bones(i).y1),3,rgb(200,200,200)        ii = order(i)        multiput 0,bones(ii).x1,bones(ii).y1,bones(ii).img,mag,mag,bones(ii).a2,1    next i       'show selected pivot    for i as integer = 1 to 15        if selected = i then            circle (bones(i).x1,bones(i).y1),8,rgb(255,255,0),,,,f            draw string (bones(i).x1-6,bones(i).y1-4), str(selected),rgb(0,0,0)        end if    next i          screenunlockend subdim as string keydim as integer mx,my,mbdrawBones()dim as single dd,dx,dydo        'enable to click select front most pivot        getmouse mx,my,,mb        if mb = 1 then            for i as integer = 0 to 15                dx = mx-bones(i).x1                dy = my-bones(i).y1                dd = sqr(dx^2+dy^2)                if dd < 5 then                    selected = i                end if            next i        end if               'Use key to rotate selected pivot        key = inkey        'can use the left/right arrow keys        If key=Chr(255) +"K" Then bones(selected).a = bones(selected).a - 1        If key=Chr(255) +"M" Then bones(selected).a = bones(selected).a + 1        'or can sue the [A] and [D] key        If key= "a" Then bones(selected).a = bones(selected).a - 1        If key= "d" Then bones(selected).a = bones(selected).a + 1               'Tap space bar to move selection to next pivot number        if key = " " then selected = selected + 1        if selected = 16 then selected = 1               drawBones()               circle (320,240),15,rgb(255,255,0)  'ABSOLUTE START COORD           sleep 2loop until  multikey(&H01)sleep'order in which to draw bones for overlapdata 1,2,3,8,9,10,7,4,5,6,11,12,13,14,15`
BasicCoder2
Posts: 3328
Joined: Jan 01, 2009 7:03

Re: Clothe 2D stick figure

D.J.Peters wrote:By the way why you use the same images twice ?

In a more detailed image the left and right arm and leg look different so may as well code for them having two different images up front.