max2d.bmx 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. SuperStrict
  2. Rem
  3. bbdoc: Graphics/Max2D
  4. End Rem
  5. Module BRL.Max2D
  6. ModuleInfo "Version: 1.23"
  7. ModuleInfo "Author: Mark Sibly, Simon Armstrong"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.23"
  12. ModuleInfo "History: Changed to SuperStrict"
  13. ModuleInfo "History: Extended flags to Long"
  14. ModuleInfo "History: 1.22 Release"
  15. ModuleInfo "History: fixed ResetCollision not resetting recycled collision quads"
  16. ModuleInfo "History: 1.21 Release"
  17. ModuleInfo "History: makecurrent now does validate before initial cls"
  18. ModuleInfo "History: 1.20 Release"
  19. ModuleInfo "History: Fixed TImageFont.Draw so it uses float translation"
  20. ModuleInfo "History: 1.19 Release"
  21. ModuleInfo "History: Fixed collision bug with non alpha/masked images"
  22. ModuleInfo "History: 1.18 Release"
  23. ModuleInfo "History: Add Flip Hook and polledinput"
  24. ModuleInfo "History: 1.17 Release"
  25. ModuleInfo "History: Added MIPMAPPEDIMAGE to smooth fonts"
  26. ModuleInfo "History: Fixed ImageFont TImage.Load parameters in wrong order!"
  27. ModuleInfo "History: 1.16 Release"
  28. ModuleInfo "History: Improved ImageFont unicode support"
  29. ModuleInfo "History: 1.15 Release"
  30. ModuleInfo "History: Added OnEnd EndGraphics"
  31. ModuleInfo "History: 1.14 Release"
  32. ModuleInfo "History: CreateImage/LockImage now always returns RGBA8888 pixmap"
  33. ModuleInfo "History: Fixed multiple Graphics calls crashing due to using Flip before DetectSync"
  34. ModuleInfo "History: 1.13 Release"
  35. ModuleInfo "History: LoadImageFont generates filteredimage images only for smoothfont fonts"
  36. ModuleInfo "History: 1.12 Release"
  37. ModuleInfo "History: Added MIPMAPPEDIMAGE flag"
  38. ModuleInfo "History: 1.11 Release"
  39. ModuleInfo "History: Fixed Garbage at graphics startup"
  40. ModuleInfo "History: 1.10 Release"
  41. ModuleInfo "History: Fixed LockImage bug"
  42. ModuleInfo "History: 1.09 Release"
  43. ModuleInfo "History: Integrated with new graphics system"
  44. ModuleInfo "History: ImageFrames now lazily evaluated"
  45. ModuleInfo "History: Fixed GetMaskColor"
  46. ModuleInfo "History: 1.08 Release"
  47. ModuleInfo "History: Collision system optimized"
  48. ModuleInfo "History: Graphics now does an EndGraphics first"
  49. ModuleInfo "History: 1.07 Release"
  50. ModuleInfo "History: 1.06 Release"
  51. ModuleInfo "History: Added GetLineWidth:Float()"
  52. ModuleInfo "History: Added GetClsColor( red Var,green Var,blue Var )"
  53. ModuleInfo "History: Fixed Object reference bug in Collision system"
  54. ModuleInfo "History: 1.05 Release"
  55. ModuleInfo "History: Fixed AnimImage collisions"
  56. ModuleInfo "History: Fixed ImagesCollide/ImagesCollide2 parameter types"
  57. Import BRL.PolledInput
  58. Import BRL.LinkedList
  59. Import BRL.Hook
  60. Import "image.bmx"
  61. 'Import "renderimage.bmx"
  62. Import "driver.bmx"
  63. Import "imagefont.bmx"
  64. Public
  65. Struct Rect
  66. Method New (X:Int, Y:Int, width:Int, height:Int)
  67. Self.X = X
  68. Self.Y = Y
  69. Self.width = width
  70. Self.height = height
  71. EndMethod
  72. Field X:Int, Y:Int
  73. Field width:Int, height:Int
  74. EndStruct
  75. Type TMax2DGraphics Extends TGraphics
  76. Field color:SColor8
  77. Field color_alpha:Float
  78. Field clsColor:SColor8
  79. Field clsColor_alpha:Float
  80. Field line_width:Float
  81. Field tform_rot:Float
  82. Field tform_scale_x:Float
  83. Field tform_scale_y:Float
  84. Field tform_ix:Float
  85. Field tform_iy:Float
  86. Field tform_jx:Float
  87. Field tform_jy:Float
  88. Field viewport_x:Int
  89. Field viewport_y:Int
  90. Field viewport_w:Int
  91. Field viewport_h:Int
  92. Field origin_x:Float
  93. Field origin_y:Float
  94. Field handle_x:Float
  95. Field handle_y:Float
  96. Field image_font:TImageFont
  97. Field blend_mode:Int
  98. Field vres_width:Float
  99. Field vres_height:Float
  100. Field vres_mousexscale:Float
  101. Field vres_mouseyscale:Float
  102. Field g_width:Int
  103. Field g_height:Int
  104. Global default_font:TImageFont
  105. Global mask_red:Int
  106. Global mask_green:Int
  107. Global mask_blue:Int
  108. Global auto_midhandle:Int
  109. Global auto_imageflags:Int = MASKEDIMAGE|FILTEREDIMAGE
  110. Field _backendGraphics:TGraphics
  111. Field _initialized:Int
  112. Field _driver:TMax2DDriver
  113. 'currently active Graphics
  114. Global _currentGraphics:TMax2DGraphics
  115. Method Driver:TMax2DDriver() Override
  116. Return _driver
  117. End Method
  118. Method GetSettings( width:Int Var,height:Int Var,depth:Int Var,hertz:Int Var,flags:Long Var, x:Int Var, y:Int Var ) Override
  119. Local w:Int, h:Int, d:Int, r:Int, f:Long, xp:Int, yp:Int
  120. _backendGraphics.GetSettings(w, h, d, r, f, xp, yp)
  121. width = w
  122. height = h
  123. depth = d
  124. hertz = r
  125. flags = f
  126. x = -1
  127. y = -1
  128. End Method
  129. Method Close() Override
  130. If Not _backendGraphics Then Return
  131. _backendGraphics.Close()
  132. _backendGraphics = Null
  133. _driver = Null
  134. End Method
  135. Method Validate()
  136. Local w:Int, h:Int, d:Int, r:Int, f:Long, xp:Int, yp:Int
  137. _backendGraphics.GetSettings(w, h, d, r, f, xp, yp)
  138. If w<>g_width Or h<>g_height
  139. g_width=w
  140. g_height=h
  141. vres_width=w
  142. vres_height=h
  143. vres_mousexscale=1
  144. vres_mouseyscale=1
  145. EndIf
  146. SetVirtualResolution(vres_width, vres_height)
  147. SetBlend(blend_mode)
  148. SetColor(color, color_alpha)
  149. SetClsColor(clsColor, 1.0)
  150. SetLineWidth(line_width)
  151. SetRotation(tform_rot)
  152. SetScale(tform_scale_x, tform_scale_y)
  153. SetViewport(viewport_x, viewport_y, viewport_w, viewport_h)
  154. SetOrigin(origin_x, origin_y)
  155. SetHandle(-handle_x, -handle_y)
  156. SetImageFont(image_font)
  157. End Method
  158. Method MakeCurrent()
  159. _currentGraphics = Self
  160. _max2dDriver = TMax2DDriver( Driver() )
  161. Assert _max2dDriver
  162. Validate()
  163. If _initialized Then Return
  164. SetRenderImage(Null)
  165. Cls()
  166. Flip(0)
  167. Cls()
  168. Flip(0)
  169. _initialized = True
  170. End Method
  171. Function ClearCurrent()
  172. _currentGraphics = Null
  173. _max2dDriver = Null
  174. End Function
  175. Function Current:TMax2DGraphics()
  176. Return _currentGraphics
  177. End Function
  178. Function Create:TMax2DGraphics( backendGraphics:TGraphics, d:TMax2DDriver )
  179. Local gw:Int,gh:Int,gd:Int,gr:Int,gf:Long,gx:Int,gy:Int
  180. backendGraphics.GetSettings(gw,gh,gd,gr,gf,gx,gy)
  181. If Not default_font Then default_font = TImageFont.CreateDefault()
  182. Local t:TMax2DGraphics = New TMax2DGraphics
  183. t.g_width = gw
  184. t.g_height = gh
  185. t.blend_mode = MASKBLEND
  186. t.color = New SColor8(255, 255, 255)
  187. t.color_alpha = 1.0
  188. t.clsColor = New SColor8(0, 0, 0)
  189. t.clsColor_alpha = 1.0
  190. t.line_width = 1
  191. t.tform_rot = 0
  192. t.tform_scale_x = 1
  193. t.tform_scale_y = 1
  194. t.tform_ix = 1
  195. t.tform_iy = 0
  196. t.tform_jx = 1
  197. t.tform_jy = 0
  198. t.viewport_x = 0
  199. t.viewport_y = 0
  200. t.viewport_w = gw
  201. t.viewport_h = gh
  202. t.origin_x = 0
  203. t.origin_y = 0
  204. t.handle_x = 0
  205. t.handle_y = 0
  206. t.image_font = default_font
  207. t.vres_width = gw
  208. t.vres_height = gh
  209. t.vres_mousexscale = 1
  210. t.vres_mouseyscale = 1
  211. t._backendGraphics = backendGraphics
  212. t._driver = d
  213. t._initialized = False
  214. Return t
  215. End Function
  216. Method Resize(width:Int, height:Int) Override
  217. _backendGraphics.Resize(width, height)
  218. End Method
  219. Method Position(x:Int, y:Int) Override
  220. _backendGraphics.Position(x, y)
  221. End Method
  222. Method SetClsColor(red:Int, green:Int, blue:Int, alpha:Float = 1.0) Final
  223. SetClsColor(New SColor8(red, green, blue), alpha)
  224. End Method
  225. Method SetClsColor(color:SColor8, alpha:Float = 1.0)
  226. clsColor = color
  227. clsColor_alpha = alpha
  228. _max2dDriver.SetClsColor(color, alpha)
  229. End Method
  230. Method GetClsColor(red:Int Var, green:Int Var, blue:Int Var)
  231. red = clsColor.r
  232. green = clsColor.g
  233. blue = clsColor.b
  234. End Method
  235. Method GetClsColor(red:Int Var, green:Int Var, blue:Int Var, alpha:Float Var)
  236. red = clsColor.r
  237. green = clsColor.g
  238. blue = clsColor.b
  239. alpha = clsColor_alpha
  240. End Method
  241. Method GetClsColor(color:SColor8 Var)
  242. color = clsColor
  243. End Method
  244. Method GetClsColor(color:SColor8 Var, alpha:Float Var)
  245. color = clsColor
  246. alpha = clsColor_alpha
  247. End Method
  248. Method UpdateTransform()
  249. Local s:Float = Sin(tform_rot)
  250. Local c:Float = Cos(tform_rot)
  251. tform_ix = c * tform_scale_x
  252. tform_iy =-s * tform_scale_y
  253. tform_jx = s * tform_scale_x
  254. tform_jy = c * tform_scale_y
  255. _max2dDriver.SetTransform(tform_ix, tform_iy, tform_jx, tform_jy)
  256. SetCollisions2DTransform(tform_ix, tform_iy, tform_jx, tform_jy)
  257. End Method
  258. Method Plot(x:Float, y:Float)
  259. _max2dDriver.Plot(x + origin_x, y + origin_y)
  260. End Method
  261. Method DrawRect(x:Float, y:Float, width:Float, height:Float)
  262. _max2dDriver.DrawRect(handle_x, handle_y, ..
  263. handle_x + width, handle_y + height, ..
  264. x + origin_x, y + origin_y)
  265. End Method
  266. Method DrawLine(x:Float, y:Float, x2:Float, y2:Float, draw_last_pixel:Int = True)
  267. _max2dDriver.DrawLine(handle_x, handle_y, ..
  268. handle_x + x2 - x, handle_y + y2 - y, ..
  269. x + origin_x, y + origin_y)
  270. If Not draw_last_pixel Then Return
  271. Local px:Float = handle_x + x2 - x
  272. Local py:Float = handle_y + y2 - y
  273. _max2dDriver.Plot(px * tform_ix + py * tform_iy + x + origin_x, ..
  274. px * tform_jx + py * tform_jy + y + origin_y)
  275. End Method
  276. Method DrawOval(x:Float, y:Float, width:Float, height:Float)
  277. _max2dDriver.DrawOval(handle_x, handle_y, ..
  278. handle_x + width, handle_y + height, ..
  279. x + origin_x, y + origin_y)
  280. End Method
  281. Method DrawPoly(xy:Float[], indices:Int[] = Null)
  282. _max2dDriver.DrawPoly(xy, handle_x, handle_y, origin_x, origin_y, indices)
  283. End Method
  284. Method DrawText(t:String, x:Float, y:Float)
  285. image_font.Draw(t, ..
  286. x + origin_x + handle_x * tform_ix + handle_y * tform_iy, ..
  287. y + origin_y + handle_x * tform_jx + handle_y * tform_jy, ..
  288. tform_ix, tform_iy, tform_jx, tform_jy)
  289. End Method
  290. Method DrawImage(image:TImage, x:Float, y:Float, frame:Int = 0)
  291. If Not image Then Return
  292. Local x0:Float = -image.handle_x
  293. Local x1:Float = x0 + image.width
  294. Local y0:Float = -image.handle_y
  295. Local y1:Float = y0 + image.height
  296. Local iframe:TImageFrame = image.Frame(frame)
  297. If iframe
  298. iframe.Draw(x0, y0, x1, y1, x + origin_x, y + origin_y, 0, 0, image.width,image.height)
  299. EndIf
  300. End Method
  301. Method DrawImageRect(image:TImage, x:Float, y:Float, width:Float, height:Float, frame:Int = 0)
  302. If Not image Then Return
  303. Local x0:Float = -image.handle_x
  304. Local x1:Float = x0 + width
  305. Local y0:Float = -image.handle_y
  306. Local y1:Float = y0 + height
  307. Local iframe:TImageFrame = image.Frame(frame)
  308. If iframe
  309. iframe.Draw(x0, y0, x1, y1, x + origin_x, y + origin_y, 0, 0, image.width, image.height)
  310. EndIf
  311. End Method
  312. Method DrawSubImageRect(image:TImage, x:Float, y:Float, width:Float, height:Float, sx:Float, sy:Float, swidth:Float,sheight:Float, hx:Float = 0, hy:Float = 0, frame:Int = 0)
  313. If Not image Then Return
  314. Local x0:Float = -hx * width / swidth
  315. Local x1:Float = x0 + width
  316. Local y0:Float = -hy * height / sheight
  317. Local y1:Float = y0 + height
  318. Local iframe:TImageFrame = image.Frame(frame)
  319. If iframe
  320. iframe.Draw(x0, y0, x1, y1, x + origin_x, y + origin_y, sx, sy, swidth, sheight)
  321. EndIf
  322. End Method
  323. Method TileImage(image:TImage, x:Float = 0.0, y:Float = 0.0, frame:Int = 0)
  324. If Not image Then Return
  325. Local iframe:TImageFrame = image.Frame(frame)
  326. If Not iframe Then Return
  327. _max2dDriver.SetTransform(1, 0, 0, 1)
  328. Local w:Int = image.width
  329. Local h:Int = image.height
  330. Local ox:Int = viewport_x - w + 1
  331. Local oy:Int = viewport_y - h + 1
  332. Local px:Float = x + origin_x - image.handle_x
  333. Local py:Float = y + origin_y - image.handle_y
  334. Local fx:Float = px - Floor(px)
  335. Local fy:Float = py - Floor(py)
  336. Local tx:Int = Floor(px) - ox
  337. Local ty:Int = Floor(py) - oy
  338. If tx >= 0
  339. tx = tx Mod w + ox
  340. Else
  341. tx = w - -tx Mod w + ox
  342. EndIf
  343. If ty >= 0
  344. ty = ty Mod h + oy
  345. Else
  346. ty = h - -ty Mod h + oy
  347. EndIf
  348. Local vr:Int = viewport_x + viewport_w
  349. Local vb:Int = viewport_y + viewport_h
  350. Local iy:Int = ty
  351. While iy < vb
  352. Local ix:Int = tx
  353. While ix < vr
  354. iframe.Draw(0, 0, w, h, ix + fx, iy + fy, 0, 0, w, h)
  355. ix = ix + w
  356. Wend
  357. iy = iy + h
  358. Wend
  359. UpdateTransform()
  360. End Method
  361. Method SetColor( red:Int, green:Int, blue:Int )
  362. color = New SColor8(red, green, blue)
  363. _max2dDriver.SetColor(red,green,blue)
  364. End Method
  365. Method SetColor( color:SColor8 )
  366. Self.color = color
  367. _max2dDriver.SetColor(color)
  368. End Method
  369. Method SetColor( red:Int, green:Int, blue:Int, alpha:Float )
  370. color = New SColor8(red, green, blue)
  371. _max2dDriver.SetColor(red,green,blue)
  372. color_alpha = alpha
  373. _max2dDriver.SetAlpha(alpha)
  374. End Method
  375. Method SetColor( color:SColor8, alpha:Float )
  376. Self.color = color
  377. _max2dDriver.SetColor(color)
  378. self.color_alpha = alpha
  379. _max2dDriver.SetAlpha(alpha)
  380. End Method
  381. Method GetColor( red:Int Var,green:Int Var,blue:Int Var )
  382. red = color.r
  383. green = color.g
  384. blue = color.b
  385. End Method
  386. Method GetColor( red:Int Var,green:Int Var,blue:Int Var,alpha:Float Var )
  387. red = color.r
  388. green = color.g
  389. blue = color.b
  390. alpha = color_alpha
  391. End Method
  392. Method GetColor( color:SColor8 Var )
  393. color = Self.color
  394. End Method
  395. Method GetColor( color:SColor8 Var,alpha:Float Var )
  396. color = Self.color
  397. alpha = Self.color_alpha
  398. End Method
  399. Method SetBlend( blend:Int )
  400. blend_mode = blend
  401. _max2dDriver.SetBlend(blend)
  402. End Method
  403. Method GetBlend:Int()
  404. Return blend_mode
  405. End Method
  406. Method SetAlpha( alpha:Float )
  407. color_alpha = alpha
  408. _max2dDriver.SetAlpha(alpha)
  409. End Method
  410. Method GetAlpha:Float()
  411. Return color_alpha
  412. End Method
  413. Method SetLineWidth( width:Float )
  414. line_width = width
  415. _max2dDriver.SetLineWidth(width)
  416. End Method
  417. Method GetLineWidth:Float()
  418. Return line_width
  419. End Method
  420. Function SetMaskColor( red:Int, green:Int, blue:Int )
  421. mask_red = red
  422. mask_green = green
  423. mask_blue = blue
  424. End Function
  425. Function GetMaskColor( red:Int Var, green:Int Var, blue:Int Var )
  426. red = mask_red
  427. green = mask_green
  428. blue = mask_blue
  429. End Function
  430. Method SetVirtualResolution( width:Float, height:Float )
  431. vres_width = width
  432. vres_height = height
  433. vres_mousexscale = width / GraphicsWidth()
  434. vres_mouseyscale = height / GraphicsHeight()
  435. _max2dDriver.SetResolution(width, height)
  436. End Method
  437. Method VirtualResolutionWidth:Float()
  438. Return vres_width
  439. End Method
  440. Method VirtualResolutionHeight:Float()
  441. Return vres_height
  442. End Method
  443. Method VirtualMouseX:Float()
  444. Return MouseX() * vres_mousexscale
  445. End Method
  446. Method VirtualMouseY:Float()
  447. Return MouseY() * vres_mouseyscale
  448. End Method
  449. Method VirtualMouseXSpeed:Float()
  450. Return MouseXSpeed() * vres_mousexscale
  451. End Method
  452. Method VirtualMouseYSpeed:Float()
  453. Return MouseYSpeed() * vres_mouseyscale
  454. End Method
  455. Method MoveVirtualMouse( x:Float, y:Float )
  456. MoveMouse(Int(x / vres_mousexscale), Int(y / vres_mouseyscale))
  457. End Method
  458. Method SetViewport(x:Int, y:Int, width:Int, height:Int)
  459. viewport_x = x
  460. viewport_y = y
  461. viewport_w = width
  462. viewport_h = height
  463. Local x0:Int=Floor( x / vres_mousexscale )
  464. Local y0:Int=Floor( y / vres_mouseyscale )
  465. Local x1:Int=Floor( (x + width) / vres_mousexscale )
  466. Local y1:Int=Floor( (y + height) / vres_mouseyscale )
  467. _max2dDriver.SetViewport(x0, y0, (x1-x0), (y1-y0))
  468. End Method
  469. Method GetViewport( x:Int Var, y:Int Var, width:Int Var, height:Int Var )
  470. x = viewport_x
  471. y = viewport_y
  472. width = viewport_w
  473. height = viewport_h
  474. End Method
  475. Method SetOrigin( x:Float, y:Float )
  476. origin_x = x
  477. origin_y = y
  478. End Method
  479. Method GetOrigin( x:Float Var, y:Float Var )
  480. x = origin_x
  481. y = origin_y
  482. End Method
  483. Method SetHandle( x:Float, y:Float )
  484. handle_x = -x
  485. handle_y = -y
  486. End Method
  487. Method GetHandle( x:Float Var,y:Float Var )
  488. x = -handle_x
  489. y = -handle_y
  490. End Method
  491. Method SetRotation( Rotation:Float )
  492. tform_rot = Rotation
  493. UpdateTransform()
  494. End Method
  495. Method GetRotation:Float()
  496. Return tform_rot
  497. End Method
  498. Method SetScale( scale_x:Float, scale_y:Float )
  499. tform_scale_x = scale_x
  500. tform_scale_y = scale_y
  501. UpdateTransform()
  502. End Method
  503. Method GetScale( scale_x:Float Var,scale_y:Float Var )
  504. scale_x = tform_scale_x
  505. scale_y = tform_scale_y
  506. End Method
  507. Method SetTransform( Rotation:Float=0, scale_x:Float = 1, scale_y:Float = 1 )
  508. tform_rot = Rotation
  509. tform_scale_x = scale_x
  510. tform_scale_y = scale_y
  511. UpdateTransform()
  512. End Method
  513. Method SetImageFont( font:TImageFont )
  514. 'Null = use default
  515. If Not font Then font = default_font
  516. image_font = font
  517. End Method
  518. Method GetImageFont:TImageFont()
  519. Return image_font
  520. End Method
  521. Method TextWidth:Int( text:String )
  522. Local width:Int = 0
  523. For Local n:Int = 0 Until text.length
  524. Local i:Int = image_font.CharToGlyph( text[n] )
  525. If i < 0 Continue
  526. width :+ image_font.LoadGlyph(i).Advance()
  527. Next
  528. Return width
  529. End Method
  530. Method TextHeight:Int( text:String )
  531. Return image_font.height()
  532. Rem
  533. Local height:Int = 0
  534. For Local n:Int = 0 Until text.length
  535. Local c:Int = text[n] - image_font.BaseChar()
  536. If c < 0 Or c >= image_font.CountGlyphs() Then Continue
  537. Local x:Int, y:Int, w:Int, h:Int
  538. image_font.Glyph(c).GetRect(x, y, w, h)
  539. height = Max(height, h)
  540. Next
  541. Return height
  542. End Rem
  543. End Method
  544. Function AutoMidHandle( enable:Int )
  545. auto_midhandle = enable
  546. End Function
  547. Function AutoImageFlags( flags:Int )
  548. auto_imageflags = flags
  549. End Function
  550. Function GetAutoImageFlags:Int()
  551. Return auto_imageflags
  552. End Function
  553. Function LoadImage:TImage( url:Object, flags:Int = -1 )
  554. If flags = -1 Then flags = auto_imageflags
  555. Local image:TImage = TImage.Load(url, flags, mask_red, mask_green, mask_blue)
  556. If Not image Then Return Null
  557. If auto_midhandle Then MidHandleImage(image)
  558. Return image
  559. End Function
  560. Function LoadAnimImage:TImage( url:Object, cell_width:Int, cell_height:Int, first_cell:Int, cell_count:Int, flags:Int = -1 )
  561. If flags = -1 Then flags = auto_imageflags
  562. Local image:TImage = TImage.LoadAnim(url, cell_width, cell_height, first_cell, cell_count, flags, mask_red, mask_green, mask_blue)
  563. If Not image Then Return Null
  564. If auto_midhandle Then MidHandleImage(image)
  565. Return image
  566. End Function
  567. Function CreateImage:TImage( width:Int, height:Int, frames:Int = 1, flags:Int = -1 )
  568. If flags = -1 Then flags = auto_imageflags
  569. Local image:TImage = TImage.Create(width, height, frames, flags | DYNAMICIMAGE, mask_red, mask_green, mask_blue)
  570. If auto_midhandle Then MidHandleImage(image)
  571. Return image
  572. End Function
  573. Method GrabImage( image:TImage, x:Int, y:Int, frame:Int = 0 )
  574. Local pixmap:TPixmap = _max2dDriver.GrabPixmap(x, y, image.width, image.height)
  575. If image.flags & MASKEDIMAGE
  576. pixmap = MaskPixmap(pixmap, mask_red, mask_green, mask_blue)
  577. EndIf
  578. image.SetPixmap(frame,pixmap)
  579. End Method
  580. Method SetBackBuffer()
  581. _max2ddriver.SetBackBuffer()
  582. EndMethod
  583. Function CreateRenderImage:TRenderImage(width:UInt, height:UInt, flags:Int=-1)
  584. If flags = -1 Then flags = auto_imageflags
  585. Local image:TRenderImage = TRenderImage.Create(width, height, flags, mask_red, mask_green, mask_blue)
  586. If auto_midhandle Then MidHandleImage(image)
  587. Return image
  588. End Function
  589. Method SetRenderImageFrame(RenderImageFrame:TImageFrame)
  590. If Not RenderImageFrame
  591. _max2ddriver.SetBackBuffer()
  592. Else
  593. _max2ddriver.SetRenderImageFrame(RenderImageFrame)
  594. EndIf
  595. EndMethod
  596. End Type
  597. Rem
  598. bbdoc: Clear current graphics buffer
  599. about:
  600. Clears the current graphics buffer to the current cls color as determined by #SetClsColor.
  601. End Rem
  602. Function Cls()
  603. _max2dDriver.Cls()
  604. End Function
  605. Rem
  606. bbdoc: Set current #Cls color
  607. about:
  608. The @red, @green and @blue parameters should be in the range of 0 to 255.
  609. The default cls color is black.
  610. End Rem
  611. Function SetClsColor(red:Int, green:Int, blue:Int, alpha:Float)
  612. TMax2DGraphics.Current().SetClsColor(red, green, blue, alpha)
  613. End Function
  614. Rem
  615. bbdoc: Set current #Cls color
  616. about:
  617. The @red, @green and @blue parameters should be in the range of 0 to 255.
  618. The default cls color is black.
  619. End Rem
  620. Function SetClsColor(red:Int, green:Int, blue:Int)
  621. TMax2DGraphics.Current().SetClsColor(red, green, blue)
  622. End Function
  623. Rem
  624. bbdoc: Set current #Cls color
  625. about:
  626. The default cls color is black.
  627. End Rem
  628. Function SetClsColor(color:SColor8)
  629. TMax2DGraphics.Current().SetClsColor(color)
  630. End Function
  631. Rem
  632. bbdoc: Get red, green, blue and alpha component of current cls color.
  633. returns: Red, green and blue values in the range 0..255 in the variables supplied.
  634. Alpha in range 0 - 1.0
  635. End Rem
  636. Function GetClsColor(red:Int Var, green:Int Var, blue:Int Var, alpha:Float)
  637. TMax2DGraphics.Current().GetClsColor(red, green, blue, alpha)
  638. End Function
  639. Rem
  640. bbdoc: Get red, green and blue component of current cls color.
  641. returns: Red, green and blue values in the range 0..255 in the variables supplied.
  642. End Rem
  643. Function GetClsColor(red:Int Var, green:Int Var, blue:Int Var)
  644. TMax2DGraphics.Current().GetClsColor(red, green, blue)
  645. End Function
  646. Rem
  647. bbdoc: Get red, green and blue component of current cls color.
  648. returns: Red, green and blue values in the range 0..255 in the variables supplied.
  649. End Rem
  650. Function GetClsColor(color:SColor8 Var)
  651. TMax2DGraphics.Current().GetClsColor(color)
  652. End Function
  653. Rem
  654. bbdoc: Get red, green, blue and alpha component of current cls color.
  655. returns: Red, green and blue values in the range 0..255 in the variables supplied.
  656. Alpha in range 0 - 1.0
  657. End Rem
  658. Function GetClsColor(color:SColor8 Var, alpha:Float Var)
  659. TMax2DGraphics.Current().GetClsColor(color, alpha)
  660. End Function
  661. Rem
  662. bbdoc: Plot a pixel
  663. about:
  664. Sets the color of a single pixel on the back buffer to the current drawing color
  665. defined with the #SetColor command. Other commands that affect the operation of
  666. #Plot include #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  667. End Rem
  668. Function Plot(x:Float, y:Float)
  669. TMax2DGraphics.Current().Plot(x, y)
  670. End Function
  671. Rem
  672. bbdoc: Plot a pixel
  673. about:
  674. Sets the color of a single pixel on the back buffer to the current drawing color
  675. defined with the #SetColor command. Other commands that affect the operation of
  676. #Plot include #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  677. End Rem
  678. Function Plot(x:Double, y:Double)
  679. TMax2DGraphics.Current().Plot(Float(x), Float(y))
  680. End Function
  681. Rem
  682. bbdoc: Draw a rectangle
  683. about:
  684. Sets the color of a rectangular area of pixels using the current drawing color
  685. defined with the #SetColor command.
  686. Other commands that affect the operation of #DrawRect include #SetHandle, #SetScale,
  687. #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  688. End Rem
  689. Function DrawRect(x:Float, y:Float, width:Float, height:Float)
  690. TMax2DGraphics.Current().DrawRect(x, y, width, height)
  691. End Function
  692. Rem
  693. bbdoc: Draw a rectangle
  694. about:
  695. Sets the color of a rectangular area of pixels using the current drawing color
  696. defined with the #SetColor command.
  697. Other commands that affect the operation of #DrawRect include #SetHandle, #SetScale,
  698. #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  699. End Rem
  700. Function DrawRect( x:Double,y:Double,width:Double,height:Double )
  701. DrawRect( Float(x),Float(y),Float(width),Float(height) )
  702. End Function
  703. Rem
  704. bbdoc: Draw a line
  705. about:
  706. #DrawLine draws a line from @x, @y to @x2, @y2 with the current drawing color.
  707. BlitzMax commands that affect the drawing of lines include #SetLineWidth, #SetColor, #SetHandle,
  708. #SetScale, #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  709. The optional @draw_last_pixel parameter can be used to control whether the last pixel of the line is drawn or not.
  710. Not drawing the last pixel can be useful if you are using certain blending modes.
  711. End Rem
  712. Function DrawLine(x:Float, y:Float, x2:Float, y2:Float, draw_last_pixel:Int = True)
  713. TMax2DGraphics.Current().DrawLine(x, y, x2, y2, draw_last_pixel)
  714. End Function
  715. Rem
  716. bbdoc: Draw a line
  717. about:
  718. #DrawLine draws a line from @x, @y to @x2, @y2 with the current drawing color.
  719. BlitzMax commands that affect the drawing of lines include #SetLineWidth, #SetColor, #SetHandle,
  720. #SetScale, #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  721. The optional @draw_last_pixel parameter can be used to control whether the last pixel of the line is drawn or not.
  722. Not drawing the last pixel can be useful if you are using certain blending modes.
  723. End Rem
  724. Function DrawLine(x:Double, y:Double, x2:Double, y2:Double, draw_last_pixel:Int = True)
  725. TMax2DGraphics.Current().DrawLine(Float(x), Float(y), Float(x2), Float(y2), draw_last_pixel)
  726. End Function
  727. Rem
  728. bbdoc: Draw an oval
  729. about:
  730. #DrawOval draws an oval that fits in the rectangular area defined by @x, @y, @width
  731. and @height parameters.
  732. BlitzMax commands that affect the drawing of ovals include #SetColor, #SetHandle,
  733. #SetScale, #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  734. End Rem
  735. Function DrawOval(x:Float, y:Float, width:Float, height:Float)
  736. TMax2DGraphics.Current().DrawOval(x, y, width, height)
  737. End Function
  738. Rem
  739. bbdoc: Draw an oval
  740. about:
  741. #DrawOval draws an oval that fits in the rectangular area defined by @x, @y, @width
  742. and @height parameters.
  743. BlitzMax commands that affect the drawing of ovals include #SetColor, #SetHandle,
  744. #SetScale, #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  745. End Rem
  746. Function DrawOval(x:Double, y:Double, width:Double, height:Double)
  747. TMax2DGraphics.Current().DrawOval(Float(x), Float(y), Float(width), Float(height))
  748. End Function
  749. Rem
  750. bbdoc: Draw a polygon
  751. about:
  752. #DrawPoly draws a polygon with corners defined by an array of x#,y# coordinate pairs.
  753. BlitzMax commands that affect the drawing of polygons include #SetColor, #SetHandle,
  754. #SetScale, #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  755. End Rem
  756. Function DrawPoly( xy:Float[], indices:Int[] = Null )
  757. TMax2DGraphics.Current().DrawPoly(xy, indices)
  758. End Function
  759. Rem
  760. bbdoc: Draw text
  761. about:
  762. #DrawText prints strings at position @x,@y of the graphics display using
  763. the current image font specified by the #SetImageFont command.
  764. Other commands that affect #DrawText include #SetColor, #SetHandle,
  765. #SetScale, #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  766. It is recomended that the blend mode be set to ALPHABLEND using the #SetBlend
  767. command for non jagged antialiased text. Text that will be drawn at a smaller
  768. size using the #SetScale command should use fonts loaded with the SMOOTHFONT
  769. style to benefit from mip-mapped filtering, see #LoadImageFont for more information.
  770. End Rem
  771. Function DrawText(t:String, x:Float, y:Float)
  772. TMax2DGraphics.Current().DrawText(t, x, y)
  773. End Function
  774. Rem
  775. bbdoc: Draw text
  776. about:
  777. #DrawText prints strings at position @x,@y of the graphics display using
  778. the current image font specified by the #SetImageFont command.
  779. Other commands that affect #DrawText include #SetColor, #SetHandle,
  780. #SetScale, #SetRotation, #SetOrigin, #SetViewPort, #SetBlend and #SetAlpha.
  781. It is recomended that the blend mode be set to ALPHABLEND using the #SetBlend
  782. command for non jagged antialiased text. Text that will be drawn at a smaller
  783. size using the #SetScale command should use fonts loaded with the SMOOTHFONT
  784. style to benefit from mip-mapped filtering, see #LoadImageFont for more information.
  785. End Rem
  786. Function DrawText(t:String, x:Double, y:Double)
  787. TMax2DGraphics.Current().DrawText(t, Float(x), Float(y))
  788. End Function
  789. Rem
  790. bbdoc: Draw an image to the back buffer
  791. about:
  792. Drawing is affected by the current blend mode, color, scale and rotation.
  793. If the blend mode is ALPHABLEND the image is affected by the current alpha value
  794. and images with alpha channels are blended correctly with the background.
  795. End Rem
  796. Function DrawImage(image:TImage, x:Float, y:Float, frame:Int = 0)
  797. TMax2DGraphics.Current().DrawImage(image, x, y, frame)
  798. End Function
  799. Rem
  800. bbdoc: Draw an image to the back buffer
  801. about:
  802. Drawing is affected by the current blend mode, color, scale and rotation.
  803. If the blend mode is ALPHABLEND the image is affected by the current alpha value
  804. and images with alpha channels are blended correctly with the background.
  805. End Rem
  806. Function DrawImage(image:TImage, x:Double, y:Double, frame:Int = 0)
  807. TMax2DGraphics.Current().DrawImage(image, Float(x), Float(y), frame)
  808. End Function
  809. Rem
  810. bbdoc: Draw an image to a rectangular area of the back buffer
  811. about:
  812. @x, @y, @width and @height specify the destination rectangle to draw to.
  813. @frame is the image frame to draw.
  814. Drawing is affected by the current blend mode, color, scale and rotation.
  815. If the blend mode is ALPHABLEND, then the image is also affected by the current alpha value.
  816. End Rem
  817. Function DrawImageRect(image:TImage, x:Float, y:Float, width:Float, height:Float, frame:Int = 0)
  818. TMax2DGraphics.Current().DrawImageRect(image, x, y, width, height)
  819. End Function
  820. Rem
  821. bbdoc: Draw an image to a rectangular area of the back buffer
  822. about:
  823. @x, @y, @width and @height specify the destination rectangle to draw to.
  824. @frame is the image frame to draw.
  825. Drawing is affected by the current blend mode, color, scale and rotation.
  826. If the blend mode is ALPHABLEND, then the image is also affected by the current alpha value.
  827. End Rem
  828. Function DrawImageRect(image:TImage, x:Double, y:Double, width:Double, height:Double, frame:Int = 0)
  829. TMax2DGraphics.Current().DrawImageRect(image, Float(x), Float(y), Float(width), Float(height))
  830. End Function
  831. Rem
  832. bbdoc: Draw a sub rectangle of an image to a rectangular area of the back buffer
  833. about:
  834. @x, @y, @width and @height specify the destination rectangle to draw to.
  835. @sx, @sy, @swidth and @sheight specify the source rectangle within the image to draw from.
  836. @hx and @hy specify a handle offset within the source rectangle.
  837. @frame is the image frame to draw.
  838. Drawing is affected by the current blend mode, color, scale and rotation.
  839. If the blend mode is ALPHABLEND, then the image is also affected by the current alpha value.
  840. End Rem
  841. Function DrawSubImageRect(image:TImage, x:Float, y:Float, width:Float, height:Float, sx:Float, sy:Float, swidth:Float,sheight:Float, hx:Float = 0, hy:Float = 0, frame:Int = 0)
  842. TMax2DGraphics.Current().DrawSubImageRect(image, x, y, width, height, sx, sy, swidth, sheight, hx, hy, frame)
  843. End Function
  844. Rem
  845. bbdoc: Draw a sub rectangle of an image to a rectangular area of the back buffer
  846. about:
  847. @x, @y, @width and @height specify the destination rectangle to draw to.
  848. @sx, @sy, @swidth and @sheight specify the source rectangle within the image to draw from.
  849. @hx and @hy specify a handle offset within the source rectangle.
  850. @frame is the image frame to draw.
  851. Drawing is affected by the current blend mode, color, scale and rotation.
  852. If the blend mode is ALPHABLEND, then the image is also affected by the current alpha value.
  853. End Rem
  854. Function DrawSubImageRect(image:TImage, x:Double, y:Double, width:Double, height:Double, sx:Double, sy:Double, swidth:Double,sheight:Double, hx:Double = 0, hy:Double = 0, frame:Int = 0)
  855. TMax2DGraphics.Current().DrawSubImageRect(image, Float(x), Float(y), Float(width), Float(height), Float(sx), Float(sy), Float(swidth), Float(sheight), Float(hx), Float(hy), frame)
  856. End Function
  857. Rem
  858. bbdoc: Draw an image in a tiled pattern
  859. about:
  860. #TileImage draws an image in a repeating, tiled pattern, filling the current viewport.
  861. End Rem
  862. Function TileImage(image:TImage, x:Float = 0.0, y:Float = 0.0, frame:Int = 0)
  863. TMax2DGraphics.Current().TileImage(image, x, y, frame)
  864. End Function
  865. Rem
  866. bbdoc: Draw an image in a tiled pattern
  867. about:
  868. #TileImage draws an image in a repeating, tiled pattern, filling the current viewport.
  869. End Rem
  870. Function TileImage(image:TImage, x:Double = 0:Double, y:Double = 0:Double, frame:Int = 0)
  871. TMax2DGraphics.Current().TileImage(image, Float(x), Float(y), frame)
  872. End Function
  873. Rem
  874. bbdoc: Set current color
  875. about:
  876. The #SetColor command affects the color of #Plot, #DrawRect, #DrawLine, #DrawText,
  877. #DrawImage and #DrawPoly.
  878. The @red, @green and @blue parameters should be in the range of 0 to 255.
  879. End Rem
  880. Function SetColor( red:Int,green:Int,blue:Int )
  881. TMax2DGraphics.Current().SetColor(red, green, blue)
  882. End Function
  883. Rem
  884. bbdoc: Set current color and alpha (transparency) level
  885. about:
  886. The #SetColor command affects the color of #Plot, #DrawRect, #DrawLine, #DrawText,
  887. #DrawImage and #DrawPoly.
  888. The @red, @green and @blue parameters should be in the range of 0 to 255.
  889. @alpha controls the transparancy level when the ALPHABLEND blend mode is in effect.
  890. The range from 0.0 to 1.0 allows a range of transparancy from completely transparent
  891. to completely solid.
  892. End Rem
  893. Function SetColor( red:Int, green:Int, blue:Int, alpha:Float )
  894. TMax2DGraphics.Current().SetColor(red, green, blue, alpha)
  895. End Function
  896. Rem
  897. bbdoc: Set current color
  898. about:
  899. The #SetColor command affects the color of #Plot, #DrawRect, #DrawLine, #DrawText,
  900. #DrawImage and #DrawPoly.
  901. @color defines the red, green and blue values.
  902. End Rem
  903. Function SetColor( color:SColor8)
  904. TMax2DGraphics.Current().SetColor(color)
  905. End Function
  906. Rem
  907. bbdoc: Set current color and alpha (transparency)
  908. about:
  909. The #SetColor command affects the color of #Plot, #DrawRect, #DrawLine, #DrawText,
  910. #DrawImage and #DrawPoly.
  911. @color defines the red, green and blue values.
  912. @alpha controls the transparancy level when the ALPHABLEND blend mode is in effect.
  913. The range from 0.0 to 1.0 allows a range of transparancy from completely transparent
  914. to completely solid.
  915. End Rem
  916. Function SetColor( color:SColor8, alpha:Float )
  917. TMax2DGraphics.Current().SetColor(color, alpha)
  918. End Function
  919. Rem
  920. bbdoc: Get red, green and blue component of current color.
  921. returns: Red, green and blue values in the range 0..255 in the variables supplied.
  922. End Rem
  923. Function GetColor( red:Int Var,green:Int Var,blue:Int Var )
  924. TMax2DGraphics.Current().GetColor(red, green, blue)
  925. End Function
  926. Rem
  927. bbdoc: Get red, green, blue component of current color and the current alpha (transparency) value.
  928. returns: Red, green and blue values in the range 0..255 and alpha (transparency) in the range 0..1.0 in the variables supplied.
  929. End Rem
  930. Function GetColor( red:Int Var, green:Int Var, blue:Int Var, alpha:Float Var )
  931. TMax2DGraphics.Current().GetColor(red, green, blue, alpha)
  932. End Function
  933. Rem
  934. bbdoc: Get current color encoded as SColor8.
  935. returns: Red, green, blue values in the range 0..255 stored in the supplied SColor8 element.
  936. End Rem
  937. Function GetColor( color:SColor8 Var )
  938. TMax2DGraphics.Current().GetColor(color)
  939. End Function
  940. Rem
  941. bbdoc: Get current rgb color encoded as SColor8 and current alpha (transparency) value separately.
  942. returns: Red, green, blue values in the range 0..255 in the supplied SColor8 element and separately the alpha (transparency) value in the range 0..1.0.
  943. End Rem
  944. Function GetColor( color:SColor8 Var, alpha:Float Var)
  945. TMax2DGraphics.Current().GetColor(color, alpha)
  946. End Function
  947. Rem
  948. bbdoc: Set current blend mode
  949. about:
  950. SetBlend controls how pixels are combined with existing pixels in the back buffer when drawing
  951. commands are used in BlitzMax.
  952. @blend should be one of:
  953. [ @{Blend mode} | @Effect
  954. * MASKBLEND | Pixels are drawn only if their alpha component is greater than .5
  955. * SOLIDBLEND | Pixels overwrite existing backbuffer pixels
  956. * ALPHABLEND | Pixels are alpha blended with existing backbuffer pixels
  957. * LIGHTBLEND | Pixel colors are added to backbuffer pixel colors, giving a 'lighting' effect
  958. * SHADEBLEND | Pixel colors are multiplied with backbuffer pixel colors, giving a 'shading' effect
  959. ]
  960. End Rem
  961. Function SetBlend( blend:Int )
  962. TMax2DGraphics.Current().SetBlend(blend)
  963. End Function
  964. Rem
  965. bbdoc: Get current blend mode
  966. returns: The current blend mode.
  967. About:
  968. See #SetBlend for possible return values.
  969. End Rem
  970. Function GetBlend:Int()
  971. Return TMax2DGraphics.Current().GetBlend()
  972. End Function
  973. Rem
  974. bbdoc: Set current alpha level
  975. about:
  976. @alpha should be in the range 0 to 1.
  977. @alpha controls the transparancy level when the ALPHABLEND blend mode is in effect.
  978. The range from 0.0 to 1.0 allows a range of transparancy from completely transparent
  979. to completely solid.
  980. End Rem
  981. Function SetAlpha( alpha:Float )
  982. TMax2DGraphics.Current().SetAlpha(alpha)
  983. End Function
  984. Rem
  985. bbdoc: Set current alpha level
  986. about:
  987. @alpha should be in the range 0 to 1.
  988. @alpha controls the transparancy level when the ALPHABLEND blend mode is in effect.
  989. The range from 0.0 to 1.0 allows a range of transparancy from completely transparent
  990. to completely solid.
  991. End Rem
  992. Function SetAlpha( alpha:Double )
  993. TMax2DGraphics.Current().SetAlpha(Float(alpha))
  994. End Function
  995. Rem
  996. bbdoc: Get current alpha setting.
  997. returns: the current alpha value in the range 0..1.0
  998. End Rem
  999. Function GetAlpha:Float()
  1000. Return TMax2DGraphics.Current().GetAlpha()
  1001. End Function
  1002. Rem
  1003. bbdoc: Sets pixel width of lines drawn with the #DrawLine command
  1004. End Rem
  1005. Function SetLineWidth( width:Float )
  1006. TMax2DGraphics.Current().SetLineWidth(width)
  1007. End Function
  1008. Rem
  1009. bbdoc: Sets pixel width of lines drawn with the #DrawLine command
  1010. End Rem
  1011. Function SetLineWidth( width:Double )
  1012. TMax2DGraphics.Current().SetLineWidth(Float(width))
  1013. End Function
  1014. Rem
  1015. bbdoc: Get line width
  1016. returns: Current line width, in pixels
  1017. End Rem
  1018. Function GetLineWidth:Float()
  1019. Return TMax2DGraphics.Current().GetLineWidth()
  1020. End Function
  1021. Rem
  1022. bbdoc: Set current mask color
  1023. about:
  1024. The current mask color is used to build an alpha mask when images are loaded or modified.
  1025. The @red, @green and @blue parameters should be in the range of 0 to 255.
  1026. End Rem
  1027. Function SetMaskColor( red:Int, green:Int, blue:Int )
  1028. TMax2DGraphics.SetMaskColor(red, green, blue)
  1029. End Function
  1030. Rem
  1031. bbdoc: Get red, green and blue component of current mask color
  1032. returns: Red, green and blue values in the range 0..255
  1033. End Rem
  1034. Function GetMaskColor( red:Int Var, green:Int Var, blue:Int Var )
  1035. TMax2DGraphics.GetMaskColor(red, green, blue)
  1036. End Function
  1037. Rem
  1038. bbdoc: Set virtual graphics resolution
  1039. about:
  1040. SetResolution allows you to set a 'virtual' resolution independent of the graphics resolution.
  1041. This allows you to design an application to work at a fixed resolution, say 640 by 480, and run it
  1042. at any graphics resolution.
  1043. End Rem
  1044. Function SetVirtualResolution( width:Float, height:Float )
  1045. TMax2DGraphics.Current().SetVirtualResolution(width, height)
  1046. End Function
  1047. Rem
  1048. bbdoc: Set virtual graphics resolution
  1049. about:
  1050. SetResolution allows you to set a 'virtual' resolution independent of the graphics resolution.
  1051. This allows you to design an application to work at a fixed resolution, say 640 by 480, and run it
  1052. at any graphics resolution.
  1053. End Rem
  1054. Function SetVirtualResolution( width:Double, height:Double )
  1055. TMax2DGraphics.Current().SetVirtualResolution(Float(width), Float(height))
  1056. End Function
  1057. Rem
  1058. bbdoc: Get virtual graphics resolution width
  1059. End Rem
  1060. Function VirtualResolutionWidth:Float()
  1061. Return TMax2DGraphics.Current().VirtualResolutionWidth()
  1062. End Function
  1063. Rem
  1064. bbdoc: Get virtual graphics resolution height
  1065. End Rem
  1066. Function VirtualResolutionHeight:Float()
  1067. Return TMax2DGraphics.Current().VirtualResolutionHeight()
  1068. End Function
  1069. Rem
  1070. bbdoc: Get virtual mouse X coordinate
  1071. End Rem
  1072. Function VirtualMouseX:Float()
  1073. Return TMax2DGraphics.Current().VirtualMouseX()
  1074. End Function
  1075. Rem
  1076. bbdoc: Get virtual mouse Y coordinate
  1077. End Rem
  1078. Function VirtualMouseY:Float()
  1079. Return TMax2DGraphics.Current().VirtualMouseY()
  1080. End Function
  1081. Rem
  1082. bbdoc: Get virtual mouse X speed
  1083. End Rem
  1084. Function VirtualMouseXSpeed:Float()
  1085. Return TMax2DGraphics.Current().VirtualMouseXSpeed()
  1086. End Function
  1087. Rem
  1088. bbdoc: Get virtual mouse Y speed
  1089. End Rem
  1090. Function VirtualMouseYSpeed:Float()
  1091. Return TMax2DGraphics.Current().VirtualMouseYSpeed()
  1092. End Function
  1093. Rem
  1094. bbdoc: Move virtual mouse
  1095. End Rem
  1096. Function MoveVirtualMouse( x:Float, y:Float )
  1097. TMax2DGraphics.Current().MoveVirtualMouse(x, y)
  1098. End Function
  1099. Rem
  1100. bbdoc: Move virtual mouse
  1101. End Rem
  1102. Function MoveVirtualMouse( x:Double, y:Double )
  1103. TMax2DGraphics.Current().MoveVirtualMouse(Float(x), Float(y))
  1104. End Function
  1105. Rem
  1106. bbdoc: Set drawing viewport
  1107. about:
  1108. The current ViewPort defines an area within the back buffer that all drawing is clipped to. Any
  1109. regions of a DrawCommand that fall outside the current ViewPort are not drawn.
  1110. End Rem
  1111. Function SetViewport( x:Int, y:Int, width:Int, height:Int )
  1112. TMax2DGraphics.Current().SetViewport(x, y, width, height)
  1113. End Function
  1114. Rem
  1115. bbdoc: Get dimensions of current Viewport.
  1116. returns: The horizontal, vertical, width and height values of the current Viewport in the variables supplied.
  1117. End Rem
  1118. Function GetViewport( x:Int Var, y:Int Var, width:Int Var, height:Int Var )
  1119. TMax2DGraphics.Current().GetViewport(x, y, width, height)
  1120. End Function
  1121. Rem
  1122. bbdoc: Set drawing origin
  1123. about:
  1124. The current Origin is an x,y coordinate added to all drawing x,y coordinates after any rotation or scaling.
  1125. End Rem
  1126. Function SetOrigin( x:Float, y:Float )
  1127. TMax2DGraphics.Current().SetOrigin(x, y)
  1128. End Function
  1129. Rem
  1130. bbdoc: Set drawing origin
  1131. about:
  1132. The current Origin is an x,y coordinate added to all drawing x,y coordinates after any rotation or scaling.
  1133. End Rem
  1134. Function SetOrigin( x:Double, y:Double )
  1135. TMax2DGraphics.Current().SetOrigin(Float(x), Float(y))
  1136. End Function
  1137. Rem
  1138. bbdoc: Get current origin position.
  1139. returns: The horizontal and vertical position of the current origin.
  1140. End Rem
  1141. Function GetOrigin( x:Float Var, y:Float Var )
  1142. TMax2DGraphics.Current().GetOrigin(x, y)
  1143. End Function
  1144. Rem
  1145. bbdoc: Set drawing handle
  1146. about:
  1147. The drawing handle is a 2D offset subtracted from the x,y location of all
  1148. drawing commands except #DrawImage as Images have their own unique handles.
  1149. Unlike #SetOrigin the drawing handle is subtracted before rotation and scale
  1150. are applied providing a 'local' origin.
  1151. End Rem
  1152. Function SetHandle( x:Float, y:Float )
  1153. TMax2DGraphics.Current().SetHandle(x, y)
  1154. End Function
  1155. Rem
  1156. bbdoc: Set drawing handle
  1157. about:
  1158. The drawing handle is a 2D offset subtracted from the x,y location of all
  1159. drawing commands except #DrawImage as Images have their own unique handles.
  1160. Unlike #SetOrigin the drawing handle is subtracted before rotation and scale
  1161. are applied providing a 'local' origin.
  1162. End Rem
  1163. Function SetHandle( x:Double, y:Double )
  1164. TMax2DGraphics.Current().SetHandle(Float(x), Float(y))
  1165. End Function
  1166. Rem
  1167. bbdoc: Get current drawing handle.
  1168. returns: The horizontal and vertical position of the current drawing handle.
  1169. End Rem
  1170. Function GetHandle( x:Float Var,y:Float Var )
  1171. TMax2DGraphics.Current().GetHandle(x, y)
  1172. End Function
  1173. Rem
  1174. bbdoc: Set current rotation
  1175. about:
  1176. @rotation is given in degrees and should be in the range 0 to 360.
  1177. End Rem
  1178. Function SetRotation( rotation:Float )
  1179. TMax2DGraphics.Current().SetRotation(rotation)
  1180. End Function
  1181. Rem
  1182. bbdoc: Set current rotation
  1183. about:
  1184. @rotation is given in degrees and should be in the range 0 to 360.
  1185. End Rem
  1186. Function SetRotation( rotation:Double )
  1187. TMax2DGraphics.Current().SetRotation(Float(rotation))
  1188. End Function
  1189. Rem
  1190. bbdoc: Get current Max2D rotation setting.
  1191. returns: The rotation in degrees.
  1192. End Rem
  1193. Function GetRotation:Float()
  1194. Return TMax2DGraphics.Current().GetRotation()
  1195. End Function
  1196. Rem
  1197. bbdoc: Set current scale
  1198. about:
  1199. @scale_x and @scale_y multiply the width and height of drawing
  1200. commands where 0.5 will half the size of the drawing and 2.0 is equivalent
  1201. to doubling the size.
  1202. End Rem
  1203. Function SetScale( scale_x:Float, scale_y:Float )
  1204. TMax2DGraphics.Current().SetScale(scale_x, scale_y)
  1205. End Function
  1206. Rem
  1207. bbdoc: Set current scale
  1208. about:
  1209. @scale_x and @scale_y multiply the width and height of drawing
  1210. commands where 0.5 will half the size of the drawing and 2.0 is equivalent
  1211. to doubling the size.
  1212. End Rem
  1213. Function SetScale( scale_x:Double, scale_y:Double )
  1214. TMax2DGraphics.Current().SetScale(Float(scale_x), Float(scale_y))
  1215. End Function
  1216. Rem
  1217. bbdoc: Get current Max2D scale settings.
  1218. returns: The current x and y scale values in the variables supplied.
  1219. End Rem
  1220. Function GetScale( scale_x:Float Var,scale_y:Float Var )
  1221. TMax2DGraphics.Current().GetScale(scale_x, scale_y)
  1222. End Function
  1223. Rem
  1224. bbdoc: Set current rotation and scale
  1225. about:
  1226. SetTransform is a shortcut for setting both the rotation and
  1227. scale parameters in Max2D with a single function call.
  1228. End Rem
  1229. Function SetTransform( rotation:Float = 0.0, scale_x:Float = 1.0, scale_y:Float = 1.0 )
  1230. TMax2DGraphics.Current().SetTransform(Rotation, scale_x, scale_y)
  1231. End Function
  1232. Rem
  1233. bbdoc: Set current rotation and scale
  1234. about:
  1235. SetTransform is a shortcut for setting both the rotation and
  1236. scale parameters in Max2D with a single function call.
  1237. End Rem
  1238. Function SetTransform( rotation:Double, scale_x:Double = 1:Double, scale_y:Float = 1:Double )
  1239. TMax2DGraphics.Current().SetTransform(Float(rotation), Float(scale_x), Float(scale_y))
  1240. End Function
  1241. Rem
  1242. bbdoc: Make the mouse pointer visible
  1243. End Rem
  1244. Rem
  1245. Function ShowMouse()
  1246. _max2dDriver.SetMouseVisible True
  1247. End Function
  1248. End Rem
  1249. Rem
  1250. bbdoc: Make the mouse pointer invisible
  1251. End Rem
  1252. Rem
  1253. Function HideMouse()
  1254. _max2dDriver.SetMouseVisible False
  1255. End Function
  1256. End Rem
  1257. Rem
  1258. bbdoc: Load an image font
  1259. returns: An image font object
  1260. about:
  1261. @style can be a combination of BOLDFONT, ITALICFONT and SMOOTHFONT
  1262. flags. Use the SMOOTHFONT flag for improved filtering if the font is to be rotated or
  1263. scaled.
  1264. End Rem
  1265. Function LoadImageFont:TImageFont( url:Object, size:Int, style:Int = SMOOTHFONT )
  1266. Return TImageFont.Load( url,size,style )
  1267. End Function
  1268. Rem
  1269. bbdoc: Set current image font
  1270. about:
  1271. In order to #DrawText in fonts other than the default system font use the #SetImageFont
  1272. command with a font handle returned by the #LoadImageFont command.
  1273. Use &{SetImageFont Null} to select the default, built-in font.
  1274. End Rem
  1275. Function SetImageFont( font:TImageFont )
  1276. TMax2DGraphics.Current().SetImageFont(font)
  1277. End Function
  1278. Rem
  1279. bbdoc: Get current image font.
  1280. returns: The current image font.
  1281. End Rem
  1282. Function GetImageFont:TImageFont()
  1283. Return TMax2DGraphics.Current().GetImageFont()
  1284. End Function
  1285. Rem
  1286. bbdoc: Get width of text
  1287. returns: the width, in pixels, of @text based on the current image font.
  1288. about:
  1289. This command is useful for calculating horizontal alignment of text when using
  1290. the #DrawText command.
  1291. End Rem
  1292. Function TextWidth:Int( text:String )
  1293. Return TMax2DGraphics.Current().TextWidth(text)
  1294. End Function
  1295. Rem
  1296. bbdoc: Get height of text
  1297. returns: the height, in pixels, of @text based on the current image font.
  1298. about:
  1299. This command is useful for calculating vertical alignment of text when using
  1300. the #DrawText command.
  1301. End Rem
  1302. Function TextHeight:Int( text:String )
  1303. Return TMax2DGraphics.Current().TextHeight(text)
  1304. End Function
  1305. Rem
  1306. bbdoc: Load an image
  1307. returns: A new image object
  1308. about:
  1309. @url can be either a string or an existing pixmap.
  1310. @flags can be 0, -1 or any combination of:
  1311. [ @{Flags value} | @{Effect}
  1312. * MASKEDIMAGE | The image is masked with the current mask color.
  1313. * FILTEREDIMAGE | The image is smoothed when scaled up to greater than its original
  1314. size, when rotated, or when drawn at fractional pixel coordinates.
  1315. * MIPMAPPEDIMAGE | The image is smoothed when scaled down to less than its original size.
  1316. * DYNAMICIMAGE | The image can be modified using #LockImage or #GrabImage.
  1317. ]
  1318. Note MIPMAPPEDIMAGE images consume extra video memory, so this flag should only be used
  1319. when really necessary.
  1320. If flags is -1, the auto image flags are used: See #AutoImageFlags.
  1321. To combine flags, use the | (boolean OR) operator.
  1322. End Rem
  1323. Function LoadImage:TImage( url:Object, flags:Int = -1 )
  1324. Return TMax2DGraphics.LoadImage(url, flags)
  1325. End Function
  1326. Rem
  1327. bbdoc: Load a multi-frame image
  1328. returns: An image object
  1329. about:
  1330. #LoadAnimImage extracts multiple image frames from a single, larger image. @url can be either a string or an
  1331. existing pixmap.
  1332. See #LoadImage for valid @flags values.
  1333. End Rem
  1334. Function LoadAnimImage:TImage( url:Object, cell_width:Int, cell_height:Int, first_cell:Int, cell_count:Int, flags:Int = -1 )
  1335. Return TMax2DGraphics.LoadAnimImage(url, cell_width, cell_height, first_cell, cell_count, flags)
  1336. End Function
  1337. Rem
  1338. bbdoc: Clear content of the passed image
  1339. about:
  1340. @image defines the image to clear.
  1341. @r, @g, @b define the red, green and blue components of the clear color. Range is 0 - 255.
  1342. @a defines the alpha value and is ranged 0.0 to 1.0.
  1343. @frameIndex defines an optional frame if the image is an animated image, -1 clears all existing frames
  1344. End Rem
  1345. Function ClearImage( image:Timage, r:UInt=0, g:UInt=0, b:UInt=0, a:Float=0.0, frameIndex:Int = -1 )
  1346. image.Clear( r, g, b, a, frameIndex )
  1347. End Function
  1348. Rem
  1349. bbdoc: Clear content of the passed image
  1350. about:
  1351. @image defines the image to clear.
  1352. @color defines the rgba components of the clear color.
  1353. @frameIndex defines an optional frame if the image is an animated image, -1 clears all existing frames
  1354. End Rem
  1355. Function ClearImage( image:Timage, color:SColor8, frameIndex:Int = -1 )
  1356. image.Clear( color.r, color.g, color.b, color.a/255.0, frameIndex )
  1357. End Function
  1358. Rem
  1359. bbdoc: Set an image's handle to an arbitrary point
  1360. about:
  1361. An image's handle is subtracted from the coordinates of #DrawImage before
  1362. rotation and scale are applied.
  1363. End Rem
  1364. Function SetImageHandle( image:TImage, x:Float, y:Float )
  1365. image.handle_x=x
  1366. image.handle_y=y
  1367. End Function
  1368. Rem
  1369. bbdoc: Set an image's handle to an arbitrary point
  1370. about:
  1371. An image's handle is subtracted from the coordinates of #DrawImage before
  1372. rotation and scale are applied.
  1373. End Rem
  1374. Function SetImageHandle( image:TImage,x:Double,y:Double )
  1375. SetImageHandle(image,Float(x),Float(y))
  1376. End Function
  1377. Rem
  1378. bbdoc: Enable or disable auto midhandle mode
  1379. about:
  1380. When auto midhandle mode is enabled, all images are automatically 'midhandled' (see #MidHandleImage)
  1381. when they are created. If auto midhandle mode is disabled, images are handled by their top left corner.
  1382. AutoMidHandle defaults to False after calling #Graphics.
  1383. End Rem
  1384. Function AutoMidHandle( enable:Int )
  1385. TMax2DGraphics.AutoMidHandle(enable)
  1386. End Function
  1387. Rem
  1388. bbdoc: Set auto image flags
  1389. about:
  1390. The auto image flags are used by #LoadImage and #CreateImage when no image
  1391. flags are specified. See #LoadImage for a full list of valid image flags.
  1392. AutoImageFlags defaults to MASKEDIMAGE | FILTEREDIMAGE.
  1393. End Rem
  1394. Function AutoImageFlags( flags:Int )
  1395. TMax2DGraphics.AutoImageFlags(flags)
  1396. End Function
  1397. Function GetAutoImageFlags:Int()
  1398. Return TMax2DGraphics.GetAutoImageFlags()
  1399. End Function
  1400. Rem
  1401. bbdoc: Set an image's handle to its center
  1402. End Rem
  1403. Function MidHandleImage( image:TImage )
  1404. image.handle_x = image.width * 0.5
  1405. image.handle_y = image.height * 0.5
  1406. End Function
  1407. Rem
  1408. bbdoc: Get width of an image
  1409. returns: The width, in pixels, of @image
  1410. End Rem
  1411. Function ImageWidth:Int( image:TImage )
  1412. Return image.width
  1413. End Function
  1414. Rem
  1415. bbdoc: Get height of an image
  1416. returns: The height, in pixels, of @image
  1417. End Rem
  1418. Function ImageHeight:Int( image:TImage )
  1419. Return image.height
  1420. End Function
  1421. Rem
  1422. bbdoc: Create an empty image
  1423. returns: A new image object
  1424. about:
  1425. #CreateImage creates an 'empty' image, which should be initialized using either #GrabImage or #LockImage
  1426. before being drawn.
  1427. Please refer to #LoadImage for valid @flags values. The @flags value is always combined with DYNAMICIMAGE.
  1428. End Rem
  1429. Function CreateImage:TImage( width:Int, height:Int, frames:Int = 1, flags:Int = -1 )
  1430. Return TMax2DGraphics.CreateImage(width, height, frames, flags)
  1431. End Function
  1432. Rem
  1433. bbdoc: Lock an image for direct access
  1434. returns: A pixmap representing the image contents
  1435. about:
  1436. Locking an image allows you to directly access an image's pixels.
  1437. Only images created with the DYNAMICIMAGE flag can be locked.
  1438. Locked images must eventually be unlocked with #UnlockImage before they can be drawn.
  1439. End Rem
  1440. Function LockImage:TPixmap( image:TImage, frame:Int = 0, read_lock:Int = True, write_lock:Int = True )
  1441. Return image.Lock( frame,read_lock,write_lock )
  1442. End Function
  1443. Rem
  1444. bbdoc: Unlock an image
  1445. about:
  1446. Unlocks an image previously locked with #LockImage.
  1447. end rem
  1448. Function UnlockImage( image:TImage, frame:Int = 0 )
  1449. End Function
  1450. Rem
  1451. bbdoc: Grab an image from the back buffer
  1452. about:
  1453. Copies pixels from the back buffer to an image frame.
  1454. Only images created with the DYNAMICIMAGE flag can be grabbed.
  1455. End Rem
  1456. Function GrabImage( image:TImage, x:Int, y:Int, frame:Int = 0 )
  1457. TMax2DGraphics.Current().GrabImage(image, x, y, frame)
  1458. End Function
  1459. Rem
  1460. bbdoc: Draw pixmap
  1461. end rem
  1462. Function DrawPixmap( pixmap:TPixmap,x:Int,y:Int )
  1463. _max2dDriver.DrawPixmap pixmap,x,y
  1464. End Function
  1465. Rem
  1466. bbdoc: Grab pixmap
  1467. end rem
  1468. Function GrabPixmap:TPixmap( x:Int,y:Int,width:Int,height:Int )
  1469. Return _max2dDriver.GrabPixmap( x,y,width,height )
  1470. End Function
  1471. Rem
  1472. bbdoc: Create a new render image
  1473. about:
  1474. @width, @height specify the dimensions of the render image.
  1475. @useLinearFlitering defines the image flag to filter images when scaling.
  1476. @max2DGraphics is an optional parameter to pass a custom Max2DGraphics context.
  1477. returns: #TRenderImage with the given dimension
  1478. End Rem
  1479. Function CreateRenderImage:TRenderImage(width:UInt, height:UInt, flags:Int=-1)
  1480. Return TMax2DGraphics.CreateRenderImage(width, height, flags)
  1481. End Function
  1482. Rem
  1483. bbdoc: Set a render image as currently active render target
  1484. about:
  1485. @renderImage defines the render image to use as target. Set to Null to render on the default graphics buffer again.
  1486. End Rem
  1487. Function SetRenderImage(renderImage:TRenderImage)
  1488. If Not renderImage
  1489. TMax2DGraphics.Current().SetRenderImageFrame(Null)
  1490. Else
  1491. Local frame:TImageFrame = renderImage.frames[0]
  1492. If Not frame
  1493. frame = renderImage.Frame(0)
  1494. EndIf
  1495. TMax2DGraphics.Current().SetRenderImageFrame(frame)
  1496. EndIf
  1497. End Function
  1498. Const COLLISION_LAYER_ALL:Int=0
  1499. Const COLLISION_LAYER_1:Int=$0001
  1500. Const COLLISION_LAYER_2:Int=$0002
  1501. Const COLLISION_LAYER_3:Int=$0004
  1502. Const COLLISION_LAYER_4:Int=$0008
  1503. Const COLLISION_LAYER_5:Int=$0010
  1504. Const COLLISION_LAYER_6:Int=$0020
  1505. Const COLLISION_LAYER_7:Int=$0040
  1506. Const COLLISION_LAYER_8:Int=$0080
  1507. Const COLLISION_LAYER_9:Int=$0100
  1508. Const COLLISION_LAYER_10:Int=$0200
  1509. Const COLLISION_LAYER_11:Int=$0400
  1510. Const COLLISION_LAYER_12:Int=$0800
  1511. Const COLLISION_LAYER_13:Int=$1000
  1512. Const COLLISION_LAYER_14:Int=$2000
  1513. Const COLLISION_LAYER_15:Int=$4000
  1514. Const COLLISION_LAYER_16:Int=$8000
  1515. Const COLLISION_LAYER_17:Int=$00010000
  1516. Const COLLISION_LAYER_18:Int=$00020000
  1517. Const COLLISION_LAYER_19:Int=$00040000
  1518. Const COLLISION_LAYER_20:Int=$00080000
  1519. Const COLLISION_LAYER_21:Int=$00100000
  1520. Const COLLISION_LAYER_22:Int=$00200000
  1521. Const COLLISION_LAYER_23:Int=$00400000
  1522. Const COLLISION_LAYER_24:Int=$00800000
  1523. Const COLLISION_LAYER_25:Int=$01000000
  1524. Const COLLISION_LAYER_26:Int=$02000000
  1525. Const COLLISION_LAYER_27:Int=$04000000
  1526. Const COLLISION_LAYER_28:Int=$08000000
  1527. Const COLLISION_LAYER_29:Int=$10000000
  1528. Const COLLISION_LAYER_30:Int=$20000000
  1529. Const COLLISION_LAYER_31:Int=$40000000
  1530. Const COLLISION_LAYER_32:Int=$80000000
  1531. Rem
  1532. bbdoc: Tests if two images collide
  1533. returns: True if any pixels of the two images specified at the given location overlap.
  1534. about:
  1535. #ImagesCollide uses the current Rotation and Scale factors from the most previous
  1536. call to #SetScale and #SetRotation to calculate at a pixel level if the two images collide.
  1537. End Rem
  1538. Function ImagesCollide:Int(image1:TImage,x1:Int,y1:Int,frame1:Int,image2:TImage,x2:Int,y2:Int,frame2:Int)
  1539. ResetCollisions COLLISION_LAYER_32
  1540. CollideImage image1,x1,y1,frame1,0,COLLISION_LAYER_32
  1541. If CollideImage(image2,x2,y2,frame2,COLLISION_LAYER_32,0) Return True
  1542. End Function
  1543. Rem
  1544. bbdoc: Tests if two images with arbitrary Rotation and Scales collide
  1545. returns: True if any pixels of the two images specified at the given location overlap.
  1546. about:
  1547. #ImagesCollide2 uses the specified Rotation and Scale paramteters
  1548. to calculate at a pixel level if the two images collide (overlap).
  1549. End Rem
  1550. Function ImagesCollide2:Int(image1:TImage,x1:Int,y1:Int,frame1:Int,rot1:Float,scalex1:Float,scaley1:Float,image2:TImage,x2:Int,y2:Int,frame2:Int,rot2:Float,scalex2:Float,scaley2:Float)
  1551. Local _scalex:Float,_scaley:Float,_rot:Float,res:Int
  1552. _rot=GetRotation()
  1553. GetScale _scalex,_scaley
  1554. ResetCollisions COLLISION_LAYER_32
  1555. SetRotation rot1
  1556. SetScale scalex1,scaley1
  1557. CollideImage image1,x1,y1,frame1,0,COLLISION_LAYER_32
  1558. SetRotation rot2
  1559. SetScale scalex2,scaley2
  1560. If CollideImage(image2,x2,y2,frame2,COLLISION_LAYER_32,0) res=True
  1561. SetRotation _rot
  1562. SetScale _scalex,_scaley
  1563. Return res
  1564. End Function
  1565. Rem
  1566. bbdoc: Clears collision layers specified by the value of @mask, mask=0 for all layers.
  1567. about:
  1568. The BlitzMax 2D collision system manages 32 layers, the @mask parameter can
  1569. be a combination of the following values or the special value COLLISION_LAYER_ALL in order
  1570. to perform collision operations on multiple layers.
  1571. Note: COLLISION_LAYER_32 is used by the #ImagesCollide and #ImagesCollide2 commands.
  1572. [ @Layer | @{Mask value}
  1573. * COLLISION_LAYER_ALL | 0
  1574. * COLLISION_LAYER_1 | $0001
  1575. * COLLISION_LAYER_2 | $0002
  1576. * COLLISION_LAYER_3 | $0004
  1577. * COLLISION_LAYER_4 | $0008
  1578. * COLLISION_LAYER_5 | $0010
  1579. * COLLISION_LAYER_6 | $0020
  1580. * COLLISION_LAYER_7 | $0040
  1581. * COLLISION_LAYER_8 | $0080
  1582. * COLLISION_LAYER_9 | $0100
  1583. * COLLISION_LAYER_10 | $0200
  1584. * COLLISION_LAYER_11 | $0400
  1585. * COLLISION_LAYER_12 | $0800
  1586. * COLLISION_LAYER_13 | $1000
  1587. * COLLISION_LAYER_14 | $2000
  1588. * COLLISION_LAYER_15 | $4000
  1589. * COLLISION_LAYER_16 | $8000
  1590. ]
  1591. EndRem
  1592. Function ResetCollisions(mask:Int=0)
  1593. Local i:Int,q:TQuad
  1594. For i=0 To 31
  1595. If mask=0 Or mask&(1 Shl i)
  1596. q=quadlayer[i]
  1597. If q
  1598. q.mask=Null
  1599. q.id=Null
  1600. While q.link
  1601. q=q.link
  1602. q.mask=Null
  1603. q.id=Null
  1604. Wend
  1605. q.link=freequads
  1606. q=quadlayer[i]
  1607. freequads=q
  1608. quadlayer[i]=Null
  1609. EndIf
  1610. EndIf
  1611. Next
  1612. End Function
  1613. Rem
  1614. bbdoc: Pixel accurate collision testing between transformed Images.
  1615. about:
  1616. The @collidemask specifies any layers to test for collision with.
  1617. The @writemask specifies which if any collision layers the @image is added to in it's currently transformed state.
  1618. The id specifies an object to be returned to future #CollideImage calls when collisions occur.
  1619. EndRem
  1620. Function CollideImage:Object[](image:TImage,x:Int,y:Int,frame:Int,collidemask:Int,writemask:Int,id:Object=Null)
  1621. Local q:TQuad
  1622. q=CreateQuad(image,frame,x,y,image.width,image.height,id)
  1623. Return CollideQuad(q,collidemask,writemask)
  1624. End Function
  1625. Rem
  1626. bbdoc: Pixel accurate collision testing between image layers
  1627. about:
  1628. The @collidemask specifies any layers to test for collision with.
  1629. The @writemask specifies which if any collision layers the @image is added to in it's currently transformed state.
  1630. The @id specifies an object to be returned to future #CollideImage calls when collisions occur.
  1631. EndRem
  1632. Function CollideRect:Object[](x:Int,y:Int,w:Int,h:Int,collidemask:Int,writemask:Int,id:Object=Null)
  1633. Local q:TQuad
  1634. q=CreateQuad(Null,0,x,y,w,h,id)
  1635. Return CollideQuad(q,collidemask,writemask)
  1636. End Function
  1637. Private
  1638. Global cix:Float,ciy:Float,cjx:Float,cjy:Float
  1639. Function SetCollisions2DTransform(ix:Float,iy:Float,jx:Float,jy:Float) 'callback from module Blitz2D
  1640. cix=ix
  1641. ciy=iy
  1642. cjx=jx
  1643. cjy=jy
  1644. End Function
  1645. Global TextureMaps:TPixmap[]
  1646. Global LineBuffer:Int[]
  1647. Global quadlayer:TQuad[32]
  1648. Global freequads:TQuad
  1649. Const POLYX:Int=0
  1650. Const POLYY:Int=1
  1651. Const POLYU:Int=2
  1652. Const POLYV:Int=3
  1653. Function DotProduct:Int(x0:Float,y0:Float,x1:Float,y1:Float,x2:Float,y2:Float)
  1654. Return (((x2-x1)*(y1-y0))-((x1-x0)*(y2-y1)))
  1655. End Function
  1656. Function ClockwisePoly(data:Float[],channels:Int) 'flips order if anticlockwise
  1657. Local count:Int,clk:Int,i:Int,j:Int
  1658. Local r0:Int,r1:Int,r2:Int
  1659. Local t:Float
  1660. count=Len(data)/channels
  1661. ' clock wise test
  1662. r0=0
  1663. r1=channels
  1664. clk=2
  1665. For i=2 To count-1
  1666. r2=r1+channels
  1667. If DotProduct(data[r0+POLYX],data[r0+POLYY],data[r1+POLYX],data[r1+POLYY],data[r2+POLYX],data[r2+POLYY])>=0 clk:+1
  1668. r1=r2
  1669. Next
  1670. If clk<count Return
  1671. ' flip order for anticockwise
  1672. r0=0
  1673. r1=(count-1)*channels
  1674. While r0<r1
  1675. For j=0 To channels-1
  1676. t=data[r0+j]
  1677. data[r0+j]=data[r1+j]
  1678. data[r1+j]=t
  1679. Next
  1680. r0:+channels
  1681. r1:-channels
  1682. Wend
  1683. End Function
  1684. Type rpoly
  1685. Field texture:TPixmap
  1686. Field data:Float[]
  1687. Field channels:Int,count:Int,size:Int
  1688. Field ldat:Float[],ladd:Float[]
  1689. Field rdat:Float[],radd:Float[]
  1690. Field Left:Int,Right:Int,top:Int
  1691. Field state:Int
  1692. End Type
  1693. Function RenderPolys:Int(vdata:Float[][],channels:Int[],textures:TPixmap[],renderspans:Int(polys:TList,count:Int,ypos:Int))
  1694. Local polys:rpoly[],p:rpoly,pcount:Int
  1695. Local active:TList
  1696. Local top:Int,bot:Int
  1697. Local n:Int,y:Int,h:Int,i:Int,j:Int,res:Int
  1698. Local data:Float[]
  1699. bot=$80000000
  1700. top=$7fffffff
  1701. n=Len(vdata)
  1702. ' create polys an array of poly renderers
  1703. polys=New rpoly[n]
  1704. For i=0 Until n
  1705. p=New rpoly
  1706. polys[i]=p
  1707. p.texture=textures[i]
  1708. p.data=vdata[i]
  1709. p.channels=channels[i]
  1710. p.count=Len(p.data)/p.channels
  1711. p.size=p.count*p.channels
  1712. ClockwisePoly(p.data,p.channels) 'flips order if anticlockwise
  1713. ' find top verticies
  1714. p.Left=0
  1715. j=0
  1716. p.top=$7fffffff
  1717. While j<p.size
  1718. y=p.data[j+POLYY] 'float to int conversion
  1719. If y<p.top p.top=y;p.Left=j
  1720. If y<top top=y
  1721. If y>bot bot=y
  1722. j:+p.channels
  1723. Wend
  1724. p.Right=p.Left
  1725. Next
  1726. active=New TList
  1727. pcount=0
  1728. ' draw top to bottom
  1729. For y=top To bot-1
  1730. ' get left gradient
  1731. For p=EachIn polys
  1732. If p.state=2 Continue
  1733. If p.state=0 And y<p.top Continue
  1734. data=p.data
  1735. If y>=Int(data[p.Left+POLYY])
  1736. j=p.Left
  1737. i=(p.Left-p.channels)
  1738. If i<0 i:+p.size
  1739. While i<>p.Left
  1740. If Int(data[i+POLYY])>y Exit
  1741. j=i
  1742. i=(i-p.channels)
  1743. If i<0 i:+p.size
  1744. Wend
  1745. h=Int(data[i+POLYY])-Int(data[j+POLYY])
  1746. If i=p.Left Or h<=0
  1747. active.remove p
  1748. ' p.remove
  1749. pcount:-1
  1750. p.state=2
  1751. Continue
  1752. EndIf
  1753. p.ldat=data[j..j+p.channels]
  1754. p.ladd=data[i..i+p.channels]
  1755. For j=0 To p.channels-1
  1756. p.ladd[j]=(p.ladd[j]-p.ldat[j])/h
  1757. p.ldat[j]:+p.ladd[j]*0.5
  1758. Next
  1759. p.Left=i
  1760. If p.state=0
  1761. p.state=1
  1762. active.AddLast p
  1763. pcount:+1
  1764. EndIf
  1765. EndIf
  1766. ' get right gradient
  1767. If y>=Int(data[p.Right+POLYY])
  1768. i=(p.Right+p.channels) Mod p.size
  1769. j=p.Right
  1770. While i<>p.Right
  1771. If Int(data[i+POLYY])>y Exit
  1772. j=i
  1773. i=(i+p.channels)Mod p.size
  1774. Wend
  1775. h=Int(data[i+POLYY])-Int(data[j+POLYY])
  1776. If i=p.Right Or h<=0
  1777. active.remove p
  1778. pcount:-1
  1779. p.state=2
  1780. Continue
  1781. EndIf
  1782. p.rdat=data[j..j+p.channels]
  1783. p.radd=data[i..i+p.channels]
  1784. For j=0 To p.channels-1
  1785. p.radd[j]=(p.radd[j]-p.rdat[j])/h
  1786. p.rdat[j]:+p.radd[j]*0.5
  1787. Next
  1788. p.Right=i
  1789. If p.state=0
  1790. p.state=1
  1791. active.AddLast p
  1792. pcount:+1
  1793. EndIf
  1794. EndIf
  1795. Next
  1796. ' call renderer
  1797. If pcount
  1798. res=renderspans(active,pcount,y)
  1799. If res<0 Return res
  1800. EndIf
  1801. ' increment spans
  1802. For p=EachIn active
  1803. For j=0 To p.channels-1
  1804. p.ldat[j]:+p.ladd[j]
  1805. p.rdat[j]:+p.radd[j]
  1806. Next
  1807. Next
  1808. Next
  1809. Return res
  1810. End Function
  1811. Function CollideSpans:Int(polys:TList,count:Int,y:Int)
  1812. Local p:rpoly
  1813. Local startx:Int,endx:Int
  1814. Local x0:Int,x1:Int,w:Int,x:Int
  1815. Local u:Float,v:Float,ui:Float,vi:Float
  1816. Local pix:Int Ptr
  1817. Local src:TPixmap
  1818. Local tw:Int,th:Int,tp:Int,argb:Int
  1819. Local width:Int,skip:Float
  1820. startx=$7fffffff
  1821. endx=$80000000
  1822. If count<2 Return 0
  1823. p=rpoly(polys.ValueAtIndex(0))
  1824. startx=p.ldat[POLYX]
  1825. endx=p.rdat[POLYX]
  1826. p=rpoly(polys.ValueAtIndex(1))
  1827. x0=p.ldat[POLYX]
  1828. x1=p.rdat[POLYX]
  1829. If x0>=endx Return 0
  1830. If x1<=startx Return 0
  1831. If x0>startx startx=x0
  1832. If x1<endx endx=x1
  1833. width=endx-startx
  1834. If width<=0 Return 0
  1835. If width>Len(LineBuffer) LineBuffer=New Int[width]
  1836. MemClear LineBuffer,Size_T(width*4)
  1837. For p=EachIn polys
  1838. src=p.texture
  1839. If src
  1840. x0=p.ldat[POLYX]
  1841. x1=p.rdat[POLYX]
  1842. w=x1-x0
  1843. If w<=0 Continue
  1844. u=p.ldat[POLYU]
  1845. v=p.ldat[POLYV]
  1846. ui=(p.rdat[POLYU]-u)/w
  1847. vi=(p.rdat[POLYV]-v)/w
  1848. skip=(startx-x0)+0.5
  1849. u=u+ui*skip
  1850. v=v+vi*skip
  1851. pix=Int Ptr(src.pixels)
  1852. tw=src.width
  1853. th=src.height
  1854. tp=src.pitch/4
  1855. For x=0 Until width
  1856. If u<0.0 u=0.0
  1857. If v<0.0 v=0.0
  1858. If u>1.0 u=1.0
  1859. If v>1.0 v=1.0
  1860. ?BigEndian
  1861. argb=$00000080 & pix[(Int(v*th))*tp+(Int(u*tw))]
  1862. ?LittleEndian
  1863. argb=$80000000 & pix[(Int(v*th))*tp+(Int(u*tw))]
  1864. ?
  1865. If (argb)
  1866. If LineBuffer[x] Return -1
  1867. LineBuffer[x]=argb
  1868. EndIf
  1869. u:+ui
  1870. v:+vi
  1871. Next
  1872. Else
  1873. For x=0 Until width
  1874. If LineBuffer[x] Return -1
  1875. LineBuffer[x]=-1
  1876. Next
  1877. EndIf
  1878. Next
  1879. Return 0
  1880. End Function
  1881. Type TQuad
  1882. Field link:TQuad
  1883. Field id:Object
  1884. Field mask:TPixmap
  1885. Field frame:Int
  1886. Field minx:Float,miny:Float,maxx:Float,maxy:Float
  1887. Field xyuv:Float[16]
  1888. Method SetCoords(tx0:Float,ty0:Float,tx1:Float,ty1:Float,tx2:Float,ty2:Float,tx3:Float,ty3:Float)
  1889. xyuv[0]=tx0
  1890. xyuv[1]=ty0
  1891. xyuv[2]=0.0
  1892. xyuv[3]=0.0
  1893. xyuv[4]=tx1
  1894. xyuv[5]=ty1
  1895. xyuv[6]=1.0
  1896. xyuv[7]=0.0
  1897. xyuv[8]=tx2
  1898. xyuv[9]=ty2
  1899. xyuv[10]=1.0
  1900. xyuv[11]=1.0
  1901. xyuv[12]=tx3
  1902. xyuv[13]=ty3
  1903. xyuv[14]=0.0
  1904. xyuv[15]=1.0
  1905. minx=Min(Min(Min(tx0,tx1),tx2),tx3)
  1906. miny=Min(Min(Min(ty0,ty1),ty2),ty3)
  1907. maxx=Max(Max(Max(tx0,tx1),tx2),tx3)
  1908. maxy=Max(Max(Max(ty0,ty1),ty2),ty3)
  1909. End Method
  1910. End Type
  1911. Function QuadsCollide:Int(p:TQuad,q:TQuad)
  1912. If p.maxx<q.minx Or p.maxy<q.miny Or p.minx>q.maxx Or p.miny>q.maxy Return False
  1913. Local vertlist:Float[][2]
  1914. Local textures:TPixmap[2]
  1915. Local channels:Int[2]
  1916. vertlist[0]=p.xyuv
  1917. vertlist[1]=q.xyuv
  1918. textures[0]=p.mask
  1919. textures[1]=q.mask
  1920. channels[0]=4
  1921. channels[1]=4
  1922. Return RenderPolys(vertlist,channels,textures,CollideSpans)
  1923. End Function
  1924. Function CreateQuad:TQuad(image:TImage,frame:Int,x:Float,y:Float,w:Float,h:Float,id:Object)
  1925. Local x0:Float,y0:Float,x1:Float,y1:Float,tx:Float,ty:Float
  1926. Local tx0:Float,ty0:Float,tx1:Float,ty1:Float,tx2:Float,ty2:Float,tx3:Float,ty3:Float
  1927. Local minx:Float,miny:Float,maxx:Float,maxy:Float
  1928. Local q:TQuad
  1929. Local pix:TPixmap
  1930. If image
  1931. x0=-image.handle_x
  1932. y0=-image.handle_y
  1933. EndIf
  1934. x1=x0+w
  1935. y1=y0+h
  1936. tx=x+TMax2DGraphics.Current().origin_x
  1937. ty=y+TMax2DGraphics.Current().origin_y
  1938. tx0=x0*cix+y0*ciy+tx
  1939. ty0=x0*cjx+y0*cjy+ty
  1940. tx1=x1*cix+y0*ciy+tx
  1941. ty1=x1*cjx+y0*cjy+ty
  1942. tx2=x1*cix+y1*ciy+tx
  1943. ty2=x1*cjx+y1*cjy+ty
  1944. tx3=x0*cix+y1*ciy+tx
  1945. ty3=x0*cjx+y1*cjy+ty
  1946. If freequads
  1947. q=freequads
  1948. freequads=q.link
  1949. q.link=Null
  1950. Else
  1951. q=New TQuad
  1952. EndIf
  1953. q.id=id
  1954. If image
  1955. pix=image.Lock( frame,True,False )
  1956. If AlphaBitsPerPixel[pix.format] q.mask=pix
  1957. EndIf
  1958. q.setcoords(tx0,ty0,tx1,ty1,tx2,ty2,tx3,ty3)
  1959. Return q
  1960. End Function
  1961. Function CollideQuad:Object[](pquad:TQuad,collidemask:Int,writemask:Int)
  1962. Local result:Object[]
  1963. Local p:TQuad,q:TQuad
  1964. Local i:Int,j:Int,count:Int
  1965. p=pquad 'CreateImageQuad(image,frame,x,y)
  1966. ' check for collisions
  1967. For i=0 To 31
  1968. If collidemask & (1 Shl i)
  1969. q=quadlayer[i]
  1970. While q
  1971. If QuadsCollide(p,q)
  1972. If count=Len(result) result=result[..((count+4)*1.2)]
  1973. result[count]=q.id
  1974. count:+1
  1975. EndIf
  1976. q=q.link
  1977. Wend
  1978. EndIf
  1979. Next
  1980. ' write to layers
  1981. For i=0 To 31
  1982. If writemask & (1 Shl i)
  1983. If freequads
  1984. q=freequads
  1985. freequads=q.link
  1986. Else
  1987. q=New TQuad
  1988. EndIf
  1989. q.id=p.id; 'TODO:optimize with memcpy?
  1990. q.mask=p.mask;
  1991. q.frame=p.frame
  1992. MemCopy q.xyuv,p.xyuv,64
  1993. q.minx=p.minx;q.miny=p.miny;q.maxx=p.maxx;q.maxy=p.maxy;
  1994. q.link=quadlayer[i]
  1995. quadlayer[i]=q
  1996. EndIf
  1997. Next
  1998. ' return result
  1999. If count Return result[..count]
  2000. End Function