Clothe 2D stick figure

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

Re: Clothe 2D stick figure

Post by D.J.Peters »

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
download: figure2d.zip

Code: Select all



#include once "FBImage.bi"
#include once "multiput.bi"

'MultiPut [destination],[xmidpos],[ymidpos],source,[xScale],[yScale],[Trans]

'some useful defines
Const Pi = 4 * Atn(1)
Dim Shared As Double TwoPi = 8 * Atn(1)
Dim Shared As Double RtoD = 180 / Pi   ' radians * RtoD = degrees
Dim Shared As Double DtoR = Pi / 180   ' degrees * DtoR = radians

sub 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 if
end sub
chdir 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 ret
end function  

screenres 640,480,32

'load images into bitmaps
var 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 = upperArmLeft
var lowerArmRight = lowerArmLeft
var rightHand     = leftHand
var upperLegRight = upperLegLeft
var lowerLegRight = lowerLegLeft
var rightShoe     = leftShoe

dim shared as integer selected   'selected bone number
selected = 1                     'first bone as bone(0) is a dummy
dim shared as single mag         'magnification of image
mag = 1

type 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 img
end type

dim shared as BONE bones(0 to 15)
'order in which to draw components for overlap
dim 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 point
bones(0).y2 = 240

bones(1).r = 1

bones(1).a = 53
bones(2).a = 53
bones(3).a = 239

bones(1).s = 85
bones(2).s = 75
bones(3).s = 31

bones(1).c = rgb(255,0,0)
bones(2).c = rgb(255,0,0)
bones(3).c = rgb(255,0,0)

bones(1).p = 0
bones(2).p = 1
bones(3).p = 2

bones(1).img = upperLegLeft
bones(2).img = lowerLegLeft
bones(3).img = leftShoe

'==================== LEG 2 =====================
bones(4).r = 1

bones(4).a = 90 '59
bones(5).a = 53
bones(6).a = 239

bones(4).s = 85
bones(5).s = 75
bones(6).s = 31

bones(4).c = rgb(0,255,0)
bones(5).c = rgb(0,255,0)
bones(6).c = rgb(0,255,0)

bones(4).p = 0
bones(5).p = 4
bones(6).p = 5

bones(4).img = upperLegRight
bones(5).img = lowerLegRight
bones(6).img = rightShoe

'===================== TORSO ========================

bones(7).r = 1
bones(7).a = 270
bones(7).s = 85
bones(7).c = rgb(0,0,255)
bones(7).p = 0

bones(7).img = torso

'===================== ARM 1 =========================

bones(8).r = 1

bones(8).a = 81
bones(9).a = 294
bones(10).a = 33

bones(8).s = 59
bones(9).s = 42
bones(10).s = 27

bones(8).c = rgb(0,255,255)
bones(9).c = rgb(0,255,255)
bones(10).c = rgb(0,255,255)

bones(8).p = 7
bones(9).p = 8
bones(10).p = 9

bones(8).img = upperArmLeft
bones(9).img = lowerArmLeft
bones(10).img = leftHand


'===================== ARM 2 =========================

bones(11).r = 1

bones(11).a = 135
bones(12).a = 343
bones(13).a = 320

bones(11).s = 59
bones(12).s = 42
bones(13).s = 27

bones(11).c = rgb(155,155,0)
bones(12).c = rgb(155,155,0)
bones(13).c = rgb(155,155,0)

bones(11).p = 7
bones(12).p = 11
bones(13).p = 12

bones(11).img = upperArmRight
bones(12).img = lowerArmRight
bones(13).img = rightHand

'==================  KNECK AND HEAD ====================

bones(14).r = 1
bones(14).a = 270
bones(14).s = 29
bones(14).c = rgb(200,0,200)
bones(14).p = 7
bones(14).img = kneck

bones(15).a = 90
bones(15).s = 18
bones(15).c = rgb(100,30,100)
bones(15).p = 14
bones(15).img = head



dim shared as single angle

sub 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
   
   
    screenunlock
end sub

dim as string key
dim as integer mx,my,mb

drawBones()
dim as single dd,dx,dy
do

        '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 2
loop until  multikey(&H01)



sleep

'order in which to draw bones for overlap
data 1,2,3,8,9,10,7,4,5,6,11,12,13,14,15
BasicCoder2
Posts: 3908
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Clothe 2D stick figure

Post by BasicCoder2 »

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.
Post Reply