Skip to content

Commit 420c18e

Browse files
author
MaximDude
committed
Set up graphic output
1 parent d8e542c commit 420c18e

File tree

1 file changed

+212
-25
lines changed

1 file changed

+212
-25
lines changed

cccp-bender-main.bmx

Lines changed: 212 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,36 @@ Strict
1010
Import MaxGUI.Drivers
1111
Import BRL.FileSystem
1212

13-
AppTitle = "CCCP Bender v0.1"
14-
15-
'App quitting bool
16-
Global quitResult = False
13+
'Version
14+
Global appVersion:String = "0.1"
1715

1816
'File I/O
1917
Global importedFile:String = Null
2018
Global fileFilers:String = "Image Files:png,jpg,bmp"
2119

2220
'Output Settings
23-
Global SCALE:String = "1"
24-
Global FRAMES:String = "7"
25-
Global BACKGROUND_RED:String = "255"
26-
Global BACKGROUND_GREEN:String = "200"
27-
Global BACKGROUND_BLUE:String = "255"
21+
Global SCALE:Int = 1
22+
Global FRAMES:Int = 7
23+
Global BACKGROUND_RED:Int = 50
24+
Global BACKGROUND_GREEN:Int = 170
25+
Global BACKGROUND_BLUE:Int = 255
26+
Global TILESIZE:Int = 32
27+
28+
'Bools
29+
Global quitResult = False
30+
Global mainToWork = False
31+
Global doDraw = False
32+
33+
'Transition between windows
34+
Function FAppUpdate()
35+
If mainToWork = False
36+
If importedFile <> Null Then
37+
FAppWork()
38+
TGraphicOutput.FGraphicBoot()
39+
mainToWork = True
40+
EndIf
41+
EndIf
42+
EndFunction
2843

2944
'GUI Elements
3045
Type TAppGUI
@@ -62,26 +77,183 @@ Type TAppGUI
6277
'Workspace Window Instructions Panel
6378
Global workHelpPanel:TGadget
6479
Global workHelpTextbox:TGadget
80+
EndType
81+
82+
'Output Window Title
83+
AppTitle = "CCCP Bender v"+appVersion+" - Output"
84+
85+
'Output Window Elements
86+
Type TGraphicOutput
87+
'Graphic Assets
88+
Global sourceImage:TImage
89+
Global boneImage:TImage[BONES]
90+
'sourceImage = LoadImage(importedFile,0)
6591

66-
'Tansition between windows bool
67-
Global mainToWork = False
92+
'Limb Parts
93+
Global JointX:Float[BONES]
94+
Global JointY:Float[BONES]
95+
Global BoneLength:Float[BONES]
96+
97+
'Precalc for drawing
98+
Global Angle[BONES,FRAMES]
99+
Global XBone[BONES,FRAMES]
100+
Global YBone[BONES,FRAMES]
68101

69-
'Transition between windows
70-
Function FAppUpdate()
71-
If mainToWork = False
72-
If importedFile <> Null Then
73-
FAppWork()
74-
mainToWork = True
75-
EndIf
102+
'Constants
103+
Const BONES = 8
104+
Const LIMBS = BONES/2
105+
Const UPPER_BONE = 0
106+
Const LOWER_BONE = 1
107+
Const REL_ANG = 180
108+
109+
'Variables
110+
Global AngA:Float
111+
Global AngB:Float
112+
Global AngC:Float
113+
114+
Function FLawOfCosines(ab,bc,ca)
115+
AngA = ACos((ca^2+ab^2-bc^2)/(2*ca*ab))
116+
AngB = ACos((bc^2+ab^2-ca^2)/(2*bc*ab))
117+
AngC = 180-(AngA+AngB)
118+
End Function
119+
120+
'Create output window and draw assets
121+
Function FGraphicBoot()
122+
SetGraphicsDriver GLMax2DDriver()
123+
Graphics(640,480,0,0,0)
124+
'SetScale(SCALE,SCALE)
125+
'Window background color
126+
SetClsColor(BACKGROUND_RED,BACKGROUND_GREEN,BACKGROUND_BLUE)
127+
SetMaskColor(255,0,255)
128+
DrawImage(sourceImage,0,0)
129+
130+
For Local b = 0 To BONES-1 ' Because I can't (?) set handles on inidividial anim image frames, I must use my own frame sys.
131+
boneImage[b] = CreateImage(TILESIZE, TILESIZE, 1, MASKEDIMAGE)
132+
GrabImage boneImage[b], b*TILESIZE, 0
133+
Next
134+
135+
' Bones should be centered on image by setting up a TILESIZE grid in photoshop with a subdiv of 2.
136+
' However, I rotate around the upper end of bone.
137+
138+
For Local i = 0 To BONES-1 ' Set up default bone sizes.
139+
JointX[i] = TILESIZE/2
140+
JointY[i] = TILESIZE/3.6
141+
boneLength[i] = (TILESIZE/2 -JointY[i])*2
142+
SetImageHandle(boneImage[i], JointX[i], JointY[i])
143+
Next
144+
145+
FBend()
146+
FDraw()
147+
doDraw = True
148+
Flip(1)
149+
Cls
150+
EndFunction
151+
152+
'Sprite rotation
153+
Function FBend()
154+
Local frm = 0
155+
Local MaxExtend:Float = 0.99 ' 1.0 is not a triangle and might cause trouble?
156+
Local MinExtend:Float = 0.30 ' Possibly make definable in GUI (slider)
157+
Local StepSize:Float = (MaxExtend-MinExtend)/(FRAMES-1) ' -1 to make inclusive of last value (full range)
158+
Local b, l, f, x, y, AirLen, UpperLen, LowerLen
159+
For Local l = 0 To LIMBS-1
160+
For Local f = 0 To FRAMES-1
161+
b = l*2
162+
x = f * TILESIZE + 96
163+
y = l * TILESIZE * 1.5 + 200
164+
UpperLen = BoneLength[b] ' e.g. upper leg
165+
LowerLen = BoneLength[b+1] ' e.g. lower leg
166+
AirLen = (StepSize*f + MinExtend) * (UpperLen + LowerLen) ' Sum of the two bones * step scaler for frame. (hip-ankle)
167+
FLawOfCosines(AirLen, UpperLen, LowerLen)
168+
Angle[b,f] = AngB ' Geez this was kinda tricky, angles upon angles.
169+
XBone[b,f] = x
170+
YBone[b,f] = y
171+
x:-Sin(Angle[b,f])*UpperLen ' Position of knee.
172+
y:+Cos(Angle[b,f])*UpperLen ' Could just use another angle of the triangle though, but I didn't.
173+
Angle[b+1,f] = AngC + AngB + 180 ' It looks correct on screen so I'm just gonna leave it at that!
174+
XBone[b+1,f] = x
175+
YBone[b+1,f] = y
176+
Next
177+
Next
178+
EndFunction
179+
180+
Function FSetSpot()
181+
Local xm = MouseX()
182+
Local ym = MouseY()
183+
If ym < (TILESIZE/2-2) And ym > 0 And xm > 0 And xm < TILESIZE*BONES ' Clicked in region? Possibly dupe points for rear limbs.
184+
Local b = xm/TILESIZE
185+
JointX[b] = TILESIZE/2 ' X is always at center, so kinda pointless to even bother.
186+
JointY[b] = ym ' Determines length
187+
boneLength[b] = (TILESIZE/2 -ym)*2
188+
SetImageHandle(boneImage[b], JointX[b], JointY[b]) ' Rotation handle.
76189
EndIf
77190
EndFunction
191+
192+
'Draw bone marks
193+
Function FDrawMark(x,y)
194+
SetRotation(0)
195+
SetColor(0,0,80)
196+
x:+1 y:+1 'add a shade for clarity on bright colours
197+
DrawLine(x-2,y,x+2,y)
198+
DrawLine(x,y-2,x,y+2)
199+
x:-1 y:-1 'Cross
200+
SetColor(255,230,80)
201+
DrawLine(x-2,y,x+2,y)
202+
DrawLine(x,y-2,x,y+2)
203+
End Function
204+
205+
Function FDraw()
206+
SetColor(255,255,255)
207+
DrawImage(sourceImage,0,0)
208+
SetColor(255,230,80)
209+
'Footer text
210+
DrawText("TBA",0,480-15)
211+
'Draw the + marks.
212+
For Local i = 0 To BONES-1
213+
FDrawMark(JointX[i]+i*TILESIZE,JointY[i])
214+
FDrawMark(JointX[i]+i*TILESIZE,JointY[i]+BoneLength[i])
215+
Next
216+
217+
SetColor(255,255,255)
218+
For Local f = 0 To FRAMES-1
219+
' These might be in a specific draw-order for joint overlapping purposes
220+
Local b
221+
b = 0 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
222+
b = 1 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
223+
b = 2 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
224+
b = 3 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
225+
b = 4 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
226+
b = 5 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
227+
b = 6 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
228+
b = 7 SetRotation(Angle[b,f]) DrawImage(boneImage[b],XBone[b,f],YBone[b,f])
229+
Next
230+
231+
SetRotation(0)
232+
End Function
233+
234+
Function FOutputUpdate()
235+
If MouseDown(1) Then' Left mouse to adjust bone spots.
236+
FSetSpot()
237+
FBend()
238+
doDraw = True
239+
EndIf
240+
If doDraw
241+
SetClsColor(BACKGROUND_RED,BACKGROUND_GREEN,BACKGROUND_BLUE)
242+
FDraw()
243+
Flip(1)
244+
Cls
245+
doDraw = False
246+
Else
247+
Delay(20)
248+
EndIf
249+
End Function
78250
EndType
79251

80252
FAppMain()
81253

82254
Function FAppMain()
83255
'Create main app window
84-
TAppGUI.mainWindow = CreateWindow("CCCP Bender v0.1",DesktopWidth()/2-150,DesktopHeight()/2-180,300,360,Null,WINDOW_TITLEBAR)
256+
TAppGUI.mainWindow = CreateWindow("CCCP Bender v"+appVersion,DesktopWidth()/2-150,DesktopHeight()/2-180,300,360,Null,WINDOW_TITLEBAR)
85257

86258
'TAppGUI.mainWindowLabel = CreateLabel("",0,0,GadgetWidth(TAppGUI.mainWindow),100,TAppGUI.mainWindow,LABEL_LEFT)
87259

@@ -96,7 +268,9 @@ EndFunction
96268

97269
Function FAppWork()
98270
'Create workspace window
99-
TAppGUI.workWindow = CreateWindow("CCCP Bender v0.1 - Editor",DesktopWidth()/2-152,DesktopHeight()/2-222,305,455,Null,WINDOW_TITLEBAR)
271+
'TAppGUI.workWindow = CreateWindow("CCCP Bender v0.1 - Editor",DesktopWidth()/2-152,DesktopHeight()/2-222,305,455,Null,WINDOW_TITLEBAR)
272+
TAppGUI.workWindow = CreateWindow("CCCP Bender v"+appversion+" - Editor",DesktopWidth()/2-640,DesktopHeight()/2-240,305,455,Null,WINDOW_TITLEBAR)
273+
100274
TAppGUI.workWindowButtonPanel = CreatePanel(10,7,280,57,TAppGUI.workWindow,PANEL_GROUP)
101275
TAppGUI.workLoadButton = CreateButton("Load",5,0,80,30,TAppGUI.workWindowButtonPanel,BUTTON_PUSH)
102276
TAppGUI.workSaveButton = CreateButton("Save",95,0,80,30,TAppGUI.workWindowButtonPanel,BUTTON_PUSH)
@@ -133,7 +307,8 @@ Function FAppWork()
133307
EndFunction
134308

135309
While True
136-
TAppGUI.FAppUpdate()
310+
FAppUpdate()
311+
TGraphicOutput.FOutputUpdate()
137312

138313
'Print appState
139314
'Print SCALE
@@ -145,7 +320,7 @@ While True
145320

146321
'Event responses
147322

148-
If TAppGUI.mainToWork = False Then
323+
If mainToWork = False Then
149324
Select EventID()
150325
'Quitting
151326
Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
@@ -157,10 +332,11 @@ While True
157332
End
158333
'Loading
159334
Case TAppGUI.mainLoadButton
160-
importedFile = RequestFile("Select graphic file to open",fileFilers)
335+
importedFile = RequestFile("Select graphic file to open",fileFilers)
336+
TGraphicOutput.sourceImage = LoadImage(importedFile,0)
161337
EndSelect
162338
EndSelect
163-
ElseIf TAppGUI.mainToWork = True Then
339+
ElseIf mainToWork = True Then
164340

165341
'Quitting
166342
If quitResult Then
@@ -180,22 +356,33 @@ While True
180356
'Loading
181357
Case TAppGUI.workLoadButton
182358
importedFile = RequestFile("Select graphic file to open",fileFilers)
183-
359+
If importedFile = Null Then
360+
TGraphicOutput.sourceImage = LoadImage("no-input.png",0)
361+
Else
362+
TGraphicOutput.sourceImage = LoadImage(importedFile,0)
363+
EndIf
184364
'Saving
185365
Case TAppGUI.workSaveButton
186366
RequestFile("Save graphic file",fileFilers,True)
187367

188368
'Settings textbox input
189369
Case TAppGUI.settingsScaleTextbox
190370
SCALE = GadgetText(TAppGUI.settingsScaleTextbox).ToInt()
371+
SetScale(SCALE,SCALE)
372+
TILESIZE = 32 * SCALE
373+
doDraw = True
191374
Case TAppGUI.settingFramesTextbox
192375
FRAMES = GadgetText(TAppGUI.settingFramesTextbox).ToInt()
376+
doDraw = True
193377
Case TAppGUI.settingsColorRTextbox
194378
BACKGROUND_RED = GadgetText(TAppGUI.settingsColorRTextbox).ToInt()
379+
doDraw = True
195380
Case TAppGUI.settingsColorGTextbox
196381
BACKGROUND_GREEN = GadgetText(TAppGUI.settingsColorGTextbox).ToInt()
382+
doDraw = True
197383
Case TAppGUI.settingsColorBTextbox
198384
BACKGROUND_BLUE = GadgetText(TAppGUI.settingsColorBTextbox).ToInt()
385+
doDraw = True
199386
EndSelect
200387
EndSelect
201388
EndIf

0 commit comments

Comments
 (0)