PageRenderTime 291ms CodeModel.GetById 197ms RepoModel.GetById 1ms app.codeStats 0ms

/v0.8.0/Red2D_V0.8/src/red2D/system/Red2D.as

http://red2d-agal2d.googlecode.com/
ActionScript | 408 lines | 173 code | 62 blank | 173 comment | 10 complexity | b5f95102c887bcd3733ecac9b46d8ffd MD5 | raw file
  1. package red2D.system
  2. {
  3. import flash.display.Sprite;
  4. import flash.display.Stage;
  5. import flash.display.Stage3D;
  6. import flash.display.StageAlign;
  7. import flash.display.StageScaleMode;
  8. import flash.display3D.Context3D;
  9. import flash.display3D.Context3DCompareMode;
  10. import flash.display3D.Context3DRenderMode;
  11. import flash.display3D.Context3DTriangleFace;
  12. import flash.events.ErrorEvent;
  13. import flash.events.Event;
  14. import flash.events.MouseEvent;
  15. import flash.events.TimerEvent;
  16. import flash.system.System;
  17. import flash.utils.Timer;
  18. import flash.utils.getTimer;
  19. import flash.utils.setInterval;
  20. import red2D.projectInfomation.InfoLogger;
  21. import red2D.util.CheckEnableStage;
  22. import red2D.util.RGB_separator;
  23. /**
  24. * <p>Red2D? ?????? ?? ??? ??? ???? Class</p>
  25. * Last update - Sep 10, 2011
  26. * @author ???(seon ki, paik) http://redcamel-studio.tistory.com
  27. * @langversion ActionScript 3.0
  28. * @playerversion Flash 11
  29. * @playerversion AIR 3.0
  30. */
  31. public class Red2D extends Sprite
  32. {
  33. /********************************************************************************/
  34. /** static ??? **/
  35. /********************************************************************************/
  36. /**
  37. * ??? ?? ?? context3D (??????? ?? ????)
  38. */
  39. public static var targetContext3d:Context3D;
  40. /**
  41. * ??? ?? ?? Stage3D (??????? ?? ????)
  42. */
  43. public static var targetStage3d:Stage3D;
  44. /**
  45. * ??? ????? ???? Scene??
  46. */
  47. public static var targetScene:Red_Scene;
  48. /**
  49. * Red2d????? stage? static?? ???? ?? / Red_Scene???? stage??? ????
  50. */
  51. public static var targetStage:Stage;
  52. /**
  53. * shader ???? ?? ??? (??????? ?? ????)
  54. */
  55. public static var targetProgramData:Red_ProgramData;
  56. /**
  57. * ???? ?? ?? ?????? ????? (??????? ?? ????)
  58. */
  59. public static var targetBufferData:Red_BufferData;
  60. /********************************************************************************/
  61. /** ?? ????? ??? **/
  62. /********************************************************************************/
  63. /**
  64. * ???? ??? ??
  65. */
  66. private var _checkStage:CheckEnableStage;
  67. /**
  68. * context3D ?????
  69. */
  70. private var _enableRed2D:Boolean=false;
  71. /********************************************************************************/
  72. /** ????? ???
  73. * Red2D? ?????? ??? ????? ?????.
  74. * ???? ??? ???? ??? ?????? ??? ??? ?????? ????? ?????? ????????.
  75. * ?? ???????? ???? ??, ??????? ?? ???? ?? ??? ????????.
  76. **/
  77. /********************************************************************************/
  78. /**
  79. * ????? - ???? ???? ???? ???? ??
  80. */
  81. private var _timer:Timer;
  82. /**
  83. * ?? ????? ???? MouseDown????? ?????? ?? (??????? ?? ????)
  84. */
  85. public static var currentMouseDown:Boolean;
  86. public static var checkTime:int;
  87. private var _prevNum:int;
  88. /********************************************************************************/
  89. /** ???? ?? ??? **/
  90. /********************************************************************************/
  91. /**
  92. * ??????
  93. */
  94. private static var _backgroundColor:int;
  95. /**
  96. * ????? RGB? ???? ??
  97. */
  98. private static var _backgroundColor_Data:Array;
  99. /********************************************************************************/
  100. /** ??? ??? **/
  101. /********************************************************************************/
  102. /**
  103. * ???
  104. *
  105. */
  106. public function Red2D()
  107. {
  108. InfoLogger.traceLogger()
  109. addEventListener(Event.ADDED_TO_STAGE, init)
  110. backgroundColor=0x000000
  111. }
  112. /**
  113. * red2D? ??? 1?? - stage??? ??
  114. * @param $event
  115. *
  116. */
  117. private function init( $event:Event ):void{
  118. stage.doubleClickEnabled = false
  119. stage.stageFocusRect = false
  120. //stage.quality = StageQuality.LOW
  121. stage.scaleMode = StageScaleMode.NO_SCALE
  122. stage.align = StageAlign.TOP_LEFT
  123. // stage?????
  124. _checkStage = new CheckEnableStage()
  125. addChild(_checkStage)
  126. _checkStage.addEventListener(CheckEnableStage.ENABLE_STAGE, check_contex3D)
  127. }
  128. /**
  129. * red2D? ??? 2??-1 - context3D ??? ??
  130. * @param event
  131. *
  132. */
  133. private function check_contex3D( $event:Event ):void
  134. {
  135. // context3D ????
  136. targetStage = stage
  137. targetStage3d = targetStage.stage3Ds[0]
  138. targetStage3d.addEventListener(Event.CONTEXT3D_CREATE, setContext3D)
  139. targetStage3d.addEventListener(ErrorEvent.ERROR, HD_context3D_Error)
  140. targetStage3d.requestContext3D(Context3DRenderMode.AUTO)
  141. }
  142. /**
  143. * red2D? ??? 2??-2 - context3D ??? ???? ??
  144. * @param event
  145. *
  146. */
  147. private function setContext3D( $event:Event ):void
  148. {
  149. trace(":::::::::: Context3D Test : Pass!!")
  150. _enableRed2D = true
  151. targetContext3d = targetStage.stage3Ds[0].context3D
  152. // targetContext3d???? ??
  153. resetContext3D_property()
  154. // ?? - ????? ??? ??? ????
  155. setBufferData()
  156. // ?? - program3D???? : ???? ProgramData ???? ???? ???? ???? program? ???? setProgram???? ?????.
  157. setProgramData()
  158. // ???? ???? ??? ??? ?? ?????.
  159. targetStage.addEventListener(Event.RESIZE, HD_Resize)
  160. trace(":::::::::: Start Molehill!! With red2D ::::::::::")
  161. }
  162. /**
  163. * red2D? ??? 3?? context3D? ?????.
  164. *
  165. */
  166. private function resetContext3D_property():void
  167. {
  168. targetContext3d.enableErrorChecking = false
  169. targetContext3d.configureBackBuffer( targetStage.stageWidth , targetStage.stageHeight ,2, false)
  170. targetContext3d.setCulling(Context3DTriangleFace.BACK);
  171. targetContext3d.setDepthTest(false, Context3DCompareMode.NEVER)
  172. // ??? ???? ??
  173. targetStage.addEventListener(MouseEvent.MOUSE_DOWN, HD_MouseDownCheck)
  174. targetStage.addEventListener(MouseEvent.MOUSE_UP, HD_MouseUpCheck)
  175. }
  176. /********************************************************************************/
  177. /** ??? **/
  178. /********************************************************************************/
  179. /**
  180. * ???? ?? ?????? ????.(?????)
  181. *
  182. */
  183. private function setBufferData():void{
  184. targetBufferData = new Red_BufferData( targetContext3d )
  185. }
  186. /**
  187. * program3d? ?????.(?????)
  188. *
  189. */
  190. private function setProgramData():void
  191. {
  192. targetProgramData = new Red_ProgramData( targetContext3d )
  193. }
  194. /**
  195. * Red_Scene ??
  196. * @param $scene
  197. *
  198. */
  199. public function setScene( $scene:Red_Scene ):void{
  200. if(hasEventListener(Event.ENTER_FRAME)){
  201. removeEventListener(Event.ENTER_FRAME, HD_TimeRender);
  202. }
  203. if(targetScene != null){
  204. targetScene.removeChildAll()
  205. }
  206. targetScene = null
  207. System.gc()
  208. targetScene = $scene
  209. checkEnableContext3D()
  210. }
  211. /**
  212. * Scene? ???? ???? _targetContext3D ??? ????? ????.
  213. *
  214. */
  215. private function checkEnableContext3D():void {
  216. stage.addEventListener(Event.ENTER_FRAME, tempLoop );
  217. }
  218. /**
  219. * _targetContext3D ????? ???? ????.
  220. * @param event
  221. *
  222. */
  223. private function tempLoop( $event:Event ):void {
  224. if ( _enableRed2D ) {
  225. if ( targetStage.stageWidth > 0 && targetStage.stageHeight > 0 ) {
  226. targetStage.removeEventListener( Event.ENTER_FRAME, tempLoop );
  227. setRender()
  228. }
  229. }
  230. }
  231. /**
  232. * targetScene? ??????.
  233. *
  234. */
  235. private function setRender():void
  236. {
  237. trace(":::::::::: Set New Scene ::::::::::")
  238. if(targetScene==null){
  239. throw new Error("target Scene? ??????")
  240. }
  241. targetScene.context3D = targetContext3d
  242. targetScene.stage = targetStage
  243. targetScene.setDesign()
  244. HD_Resize()
  245. addEventListener(Event.ENTER_FRAME, HD_TimeRender);
  246. // ??????? ???? ??????.
  247. // setTimer()
  248. }
  249. /**
  250. * ?????? ??? ????
  251. * ??? ??????
  252. */
  253. private function setTimer():void
  254. {
  255. if(_timer!=null){
  256. _timer.removeEventListener(TimerEvent.TIMER, HD_TimeRender)
  257. _timer=null
  258. }
  259. /*_timer = new Timer(1000/60)
  260. //TODO 1000? ?? 1100? 12000? 1300? 1400? 1500? 1600? 1700? ????....???? ??? ??? ?????.
  261. _timer.start()
  262. _timer.addEventListener(TimerEvent.TIMER, HD_TimeRender)*/
  263. setInterval(HD_TimeRender,1000/60)
  264. }
  265. /********************************************************************************/
  266. /** ??? ??? **/
  267. /********************************************************************************/
  268. private function HD_TimeRender($e:Event=null):void
  269. {
  270. $e.stopImmediatePropagation();
  271. checkTime = int((getTimer())/10%100)
  272. if(_prevNum != checkTime){
  273. // * Context3d? ??
  274. targetContext3d.clear(_backgroundColor_Data[0],_backgroundColor_Data[1],_backgroundColor_Data[2])
  275. // * Context3D? ??? ????? ??? ??? ?? ?? ???? ???.
  276. // * ??? scene? setRenderList??? ???
  277. targetScene.setRenderList()
  278. // * context3d? ???
  279. _prevNum = checkTime
  280. targetContext3d.present()
  281. }
  282. }
  283. /**
  284. * red2D? ??? 2?? - context3D ???? ?????? ???
  285. * @param event
  286. *
  287. */
  288. private function HD_context3D_Error(event:Event):void
  289. {
  290. trace(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
  291. trace(":::::::::: Context3D Test : Fail!! - Check Your wmode, set wmode=direct or gpu")
  292. trace(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
  293. }
  294. /********************************************************************************/
  295. /** ??? ??? **/
  296. /********************************************************************************/
  297. /**
  298. * ????
  299. * @param event
  300. *
  301. */
  302. private function HD_Resize( $event:Event=null ):void
  303. {
  304. trace(":::::::::: Stage Resize!! reset configureBackBuffer ::::::::::")
  305. resetContext3D_property()
  306. // ?????? ?????? ??????.
  307. targetScene.resetVcsAndWcs()
  308. }
  309. /**
  310. * ???? MouseDown????? ??
  311. * @param event
  312. *
  313. */
  314. private function HD_MouseDownCheck(event:MouseEvent):void
  315. {
  316. currentMouseDown = true
  317. }
  318. /**
  319. * ???? MouseUp????? ??
  320. * @param event
  321. *
  322. */
  323. private function HD_MouseUpCheck(event:MouseEvent):void
  324. {
  325. currentMouseDown = false
  326. }
  327. /********************************************************************************/
  328. /** get / set **/
  329. /********************************************************************************/
  330. /**
  331. * ?????? ??/?????.
  332. */
  333. public static function get backgroundColor():int
  334. {
  335. return _backgroundColor;
  336. }
  337. /**
  338. * ?????? ??/?????.
  339. */
  340. public static function set backgroundColor( $color:int):void
  341. {
  342. _backgroundColor_Data = RGB_separator.divide($color)
  343. _backgroundColor = $color;
  344. }
  345. }
  346. }