#
root/lang/legacy-actionscript/as2draw/PixelBuffer.as
@
3382

Revision 3382, 3.3 kB (checked in by gyuque, 8 years ago) |
---|

Line | |
---|---|

1 | class PixelBuffer |

2 | { |

3 | private var mStride:Number; |

4 | private var mStrideShift:Number; |

5 | |

6 | private var mWidth:Number; |

7 | private var mHeight:Number; |

8 | |

9 | private var mBuf:Array; |

10 | private var mOut:MovieClip; |

11 | |

12 | public function PixelBuffer(aW:Number, aH:Number, aOut:MovieClip) |

13 | { |

14 | if (aW == 0) throw "pixbuf width cannot be zero"; |

15 | mOut = aOut; |

16 | |

17 | mWidth = aW; |

18 | mHeight = aH; |

19 | |

20 | mBuf = []; |

21 | |

22 | var i:Number; |

23 | var w:Number = aW - 1; |

24 | for (i = 0;i < 16;i++) |

25 | { |

26 | if (0 == (w >> i)) break; |

27 | } |

28 | if (i == 0) throw "pixbuf width too big"; |

29 | |

30 | mStrideShift = i; |

31 | mStride = 1 << i; |

32 | } |

33 | |

34 | public function setOut(aMC:MovieClip):Void |

35 | { |

36 | mOut = aMC; |

37 | } |

38 | |

39 | public function setPixel(x:Number, y:Number, k:Number) |

40 | { |

41 | if (x<0 || x>=mWidth || y<0) return; |

42 | mBuf[ (y<<mStrideShift) + x ] = k; |

43 | } |

44 | |

45 | private var mSX; |

46 | private var mSY; |

47 | private var mDir; |

48 | private var mCount; |

49 | |

50 | public function begin(aX:Number, aY:Number, aDir:Number) |

51 | { |

52 | mSX = aX; |

53 | mSY = aY; |

54 | mDir = aDir; |

55 | mCount = 0; |

56 | } |

57 | |

58 | public function advance() |

59 | { |

60 | mCount++; |

61 | } |

62 | |

63 | public function commit(aData:Number, aOutColor:Number, aUseDebugRendering:Boolean) |

64 | { |

65 | if (mCount == 0) |

66 | return; |

67 | |

68 | var dx:Number; |

69 | var dy:Number; |

70 | |

71 | if (aUseDebugRendering) |

72 | mOut.lineStyle(1, aOutColor); |

73 | |

74 | switch(mDir) { |

75 | case 0: |

76 | dx = 0; dy = -1; |

77 | if (aUseDebugRendering) |

78 | { |

79 | mOut.moveTo(mSX, mSY-mCount); |

80 | mOut.lineTo(mSX, mSY); |

81 | } |

82 | break; |

83 | case 1: |

84 | dx = 1; dy = 0; |

85 | if (aUseDebugRendering) |

86 | { |

87 | mOut.moveTo(mSX , mSY); |

88 | mOut.lineTo(mSX+mCount, mSY); |

89 | } |

90 | break; |

91 | case 2: |

92 | dx = 0; dy = 1; |

93 | if (aUseDebugRendering) |

94 | { |

95 | mOut.moveTo(mSX, mSY); |

96 | mOut.lineTo(mSX, mSY+mCount); |

97 | } |

98 | break; |

99 | case 3: |

100 | dx = -1; dy = 0; |

101 | |

102 | if (aUseDebugRendering) |

103 | { |

104 | mOut.moveTo(mSX-mCount, mSY); |

105 | mOut.lineTo(mSX , mSY); |

106 | } |

107 | break; |

108 | } |

109 | |

110 | for (var i:Number = 0;i < mCount;i++) |

111 | { |

112 | setPixel(mSX, mSY, aData); |

113 | mSX += dx; |

114 | mSY += dy; |

115 | } |

116 | mCount = 0; |

117 | } |

118 | |

119 | |

120 | public function drawLine(x1:Number, y1:Number, x2:Number, y2:Number, c:Number, outc:Number, aUseDebugRendering:Boolean) |

121 | { |

122 | x1 = Math.floor(x1); |

123 | x2 = Math.floor(x2); |

124 | y1 = Math.floor(y1); |

125 | y2 = Math.floor(y2); |

126 | |

127 | var E:Number; |

128 | var dx:Number = Math.abs(x2 - x1); |

129 | var dy:Number = Math.abs(y2 - y1); |

130 | |

131 | var x:Number; |

132 | var y:Number; |

133 | var t:Number; |

134 | var step:Number; |

135 | |

136 | if(dx > dy) |

137 | { |

138 | E = dx/2; |

139 | if (x2 < x1) |

140 | { |

141 | t=x1;x1=x2;x2=t; |

142 | t=y1;y1=y2;y2=t; |

143 | } |

144 | step = (y1<y2) ? 1 : -1; |

145 | |

146 | x = x1; |

147 | y = y1; |

148 | begin(x, y, 1); |

149 | for (; x <= x2; x++) |

150 | { |

151 | advance(); |

152 | |

153 | E += dy; |

154 | if (E >= dx){ |

155 | E -= dx; |

156 | y += step; |

157 | commit(c, outc, aUseDebugRendering); |

158 | begin(x+1, y, 1); |

159 | } |

160 | } |

161 | commit(c, outc, aUseDebugRendering); |

162 | } |

163 | else |

164 | { |

165 | E = dy/2; |

166 | if (y2 < y1) |

167 | { |

168 | t=x1;x1=x2;x2=t; |

169 | t=y1;y1=y2;y2=t; |

170 | } |

171 | step = (x1<x2) ? 1 : -1; |

172 | |

173 | |

174 | x = x1; |

175 | y = y1; |

176 | begin(x, y, 2); |

177 | for (; y <= y2; y++) |

178 | { |

179 | advance(); |

180 | |

181 | E += dx; |

182 | if (E >= dy) |

183 | { |

184 | E -= dy; |

185 | x += step; |

186 | commit(c, outc, aUseDebugRendering); |

187 | begin(x, y+1, 2); |

188 | } |

189 | } |

190 | commit(c, outc, aUseDebugRendering); |

191 | } |

192 | } |

193 | } |

194 |

**Note:**See TracBrowser for help on using the browser.