Advertisement
Guest User

surface.min.lua

a guest
Jun 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.56 KB | None | 0 0
  1. local e={}
  2. do local t={}e.surf=t;local a,o,i=table.concat,math.floor,math.atan2;local n,s={},{}for q=0,15
  3. do n[2^q]=string.format("%01x",q)s[string.format("%01x",q)]=
  4. 2^q end;local h={}for q=0,255 do
  5. h[q]=string.char(q)end;local r={}for q=0,1023 do r[q]=tostring(q)end
  6. local d,l,u=20,{},{}for q=0,d-1 do
  7. l[q+1]=(1-math.sin(q/d*math.pi*2))/2
  8. u[q+1]=(1+math.cos(q/d*math.pi*2))/2 end;local c,m,f,w,y,p=16
  9. local function v(q,j,x)local z,_,E,T,A,O=0,0,0,0,j,x;for I=1,#
  10. q do z=z+q[I].ox;_=_+q[I].oy;E=E+q[I].x;T=T+q[I].y
  11. A=q[I].width;O=q[I].height end;return z,_,E,T,A,O end
  12. local function b(q,j,x,z,_,E,T,A)if q<_ then x=x+q-_;q=_ end;if j<E then z=z+j-E;j=E end
  13. if q+x>_+T then x=T+_-q end;if j+z>E+A then z=A+E-j end;return q,j,x,z end
  14. function e.create(q,j,x,z,_)local e=setmetatable({},{__index=e.surf})e.width=q
  15. e.height=j;e.buffer={}e.overwrite=false;e.stack={}
  16. e.ox,e.oy,e.cx,e.cy,e.cwidth,e.cheight=v(e.stack,q,j)local E=e.buffer;for T=1,q*j*3,3 do E[T]=x or false;E[T+1]=z or false
  17. E[T+2]=_ or false end;E[q*j*3+1]=false;if not x then for T=1,q*j*3,3 do
  18. E[T]=x end end
  19. if not z then for T=2,q*j*3,3 do E[T]=z end end;if not _ then for T=3,q*j*3,3 do E[T]=_ end end;return e end
  20. function e.getPlatformOutput(q)q=q or
  21. (term or gpu or(love and love.graphics)or io)
  22. if q.blit and q.setCursorPos then return
  23. "cc",q,q.getSize()elseif
  24. q.write and q.setCursorPos and q.setTextColor and q.setBackgroundColor then return"cc-old",q,q.getSize()elseif
  25. q.blitPixels then return"riko-4",q,320,200 elseif q.points and q.setColor then
  26. return"love2d",q,q.getWidth(),q.getHeight()elseif q.drawPixel then return"redirection",q,64,64 elseif
  27. q.setForeground and q.setBackground and q.set then return"oc",q,q.getResolution()elseif q.write then
  28. return"ansi",q, (os.getenv and
  29. (os.getenv("COLUMNS")))or 80, (os.getenv and
  30. (os.getenv("LINES")))or 43 else error("unsupported platform/output object")end end
  31. function t:output(q,j,x,z,_,E,T)local A,q,O,I=e.getPlatformOutput(q)j=j or 0;x=x or 0;z=z or 0
  32. _=_ or 0;E=E or self.width;T=T or self.height
  33. z,_,E,T=b(z,_,E,T,0,0,self.width,self.height)local N=self.buffer;local S=self.width;local H,R,D
  34. if A=="cc"then local L,U,C={},{},{}
  35. for M=0,T-1 do
  36. R=(M+_)*S+z
  37. for F=0,E-1 do H=(R+F)*3;D=F+1;L[D]=N[H+3]or" "
  38. U[D]=n[N[H+2]or 1]C[D]=n[N[H+1]or 32768]end;q.setCursorPos(j+1,x+M+1)q.blit(a(L),a(U),a(C))end elseif A=="cc-old"then local L,U,C,M,F={}
  39. for W=0,T-1 do q.setCursorPos(j+1,x+W+1)
  40. R=(W+_)*S+z
  41. for Y=0,E-1 do H=(R+Y)*3;M=N[H+1]or 32768;F=N[H+2]or 1
  42. if M~=U then if#L~=0 then
  43. q.write(a(L))L={}end;U=M;q.setBackgroundColor(U)end
  44. if F~=C then if#L~=0 then q.write(a(L))L={}end;C=F;q.setTextColor(C)end;L[#L+1]=N[H+3]or" "end;q.write(a(L))L={}end elseif A=="riko-4"then local L={}
  45. for U=0,T-1 do R=(U+_)*S+z;for C=0,E-1 do L[U*E+C+1]=
  46. N[(R+C)*3+1]or 0 end end;q.blitPixels(j,x,E,T,L)elseif A=="love2d"then local L,U,C,M,F,W,Y={}
  47. for P=0,T-1 do
  48. R=(P+_)*S+z
  49. for V=0,E-1 do H=(R+V)*3;F=N[H+1]W=N[H+2]Y=N[H+3]
  50. if F~=U or W~=C or Y~=M then if#L~=
  51. 0 then
  52. q.setColor((U or 0)*255,(C or 0)*255,(M or 0)*255,
  53. (U or C or M)and 255 or 0)q.points(L)end
  54. U,C,M=F,W,Y;L={}end;L[#L+1]=V+j+1;L[#L+1]=P+x+1 end end
  55. q.setColor((U or 0)*255,(C or 0)*255,(M or 0)*255,
  56. (U or C or M)and 255 or 0)q.points(L)elseif A=="redirection"then local L=q.drawPixel;for U=0,T-1 do for C=0,E-1 do
  57. L(j+C,x+U,N[
  58. ((U+_)*S+ (C+z))*3+1]or 0)end end elseif A=="oc"then
  59. local L,U,C,M,F,W={}
  60. for Y=0,T-1 do U=j;R=(Y+_)*S+z
  61. for P=0,E-1 do H=(R+P)*3;F=N[H+1]or 0x000000;W=
  62. N[H+2]or 0xFFFFFF;if F~=C then
  63. if#L~=0 then q.set(U+1,Y+x+1,a(L))U=P+j;L={}end;C=F;q.setBackground(C)end;if W~=M then if#
  64. L~=0 then q.set(U+1,Y+x+1,a(L))U=P+j;L={}end
  65. M=W;q.setForeground(M)end;L[#L+1]=N[
  66. H+3]or" "end;q.set(U+1,Y+x+1,a(L))L={}end elseif A=="ansi"then local L,U,C,M,F={}
  67. for W=0,T-1 do L[#L+1]="\x1b["..
  68. r[x+W+1]..";"..r[j+1].."H"R=(W+_)*S+z
  69. for Y=0,E-1 do
  70. H=(R+Y)*3;M=N[H+1]or 0;F=N[H+2]or 7
  71. if M~=U then U=M
  72. if U<8 then
  73. L[#L+1]="\x1b["..r[40+U].."m"elseif U<16 then L[#L+1]="\x1b["..r[92+U].."m"elseif U<232 then
  74. L[#L+1]="\x1b[48;2;"..r[o((U-
  75. 16)/36*85/2)]..";"..
  76.  
  77. r[o((U-16)/6%6*85/2)]..";"..r[o((U-16)%6*85/2)].."m"else local P=r[U*10-2312]L[#L+1]="\x1b[48;2;"..
  78. P..";"..P..";"..P.."m"end end
  79. if F~=C then C=F
  80. if C<8 then L[#L+1]="\x1b["..r[30+C].."m"elseif C<16 then L[#L+1]="\x1b["..r[
  81. 82+C].."m"elseif C<232 then
  82. L[#L+1]="\x1b[38;2;"..r[o(
  83. (C-16)/36*85/2)]..";"..
  84.  
  85. r[o((C-16)/6%6*85/2)]..";"..r[o((C-16)%6*85/2)].."m"else local P=r[C*10-2312]L[#L+1]="\x1b[38;2;"..
  86. P..";"..P..";"..P.."m"end end;L[#L+1]=N[H+3]or" "end end;q.write(a(L))end end
  87. function t:push(q,j,x,z,_)q,j=q+self.ox,j+self.oy
  88. local E,T=_ and self.ox or q,_ and self.oy or j
  89. q,j,x,z=b(q,j,x,z,self.cx,self.cy,self.cwidth,self.cheight)
  90. self.stack[#self.stack+1]={ox=E-self.ox,oy=T-self.oy,x=q-self.cx,y=j-
  91. self.cy,width=x,height=z}
  92. self.ox,self.oy,self.cx,self.cy,self.cwidth,self.cheight=v(self.stack,self.width,self.height)end
  93. function t:pop()
  94. if#self.stack==0 then error("no stencil to pop")end;self.stack[#self.stack]=nil
  95. self.ox,self.oy,self.cx,self.cy,self.cwidth,self.cheight=v(self.stack,self.width,self.height)end
  96. function t:copy()local e=setmetatable({},{__index=e.surf})for q,j in pairs(self)do
  97. e[q]=j end;e.buffer={}for q=1,self.width*self.height*3+1
  98. do e.buffer[q]=false end
  99. for q=1,
  100. self.width*self.height*3 do e.buffer[q]=self.buffer[q]end;e.stack={}
  101. for q=1,#self.stack do e.stack[q]=self.stack[q]end;return e end
  102. function t:clear(q,j,x)local z,_
  103. for E=0,self.cheight-1 do
  104. _=(E+self.cy)*self.width+self.cx;for T=0,self.cwidth-1 do z=(_+T)*3;self.buffer[z+1]=q
  105. self.buffer[z+2]=j;self.buffer[z+3]=x end end end
  106. function t:drawPixel(q,j,x,z,_)q,j=q+self.ox,j+self.oy;local E
  107. if q>=self.cx and q<
  108. self.cx+self.cwidth and j>=self.cy and j<self.cy+
  109. self.cheight then E=(
  110. j*self.width+q)*3;if x or self.overwrite then
  111. self.buffer[E+1]=x end
  112. if z or self.overwrite then self.buffer[E+2]=z end;if _ or self.overwrite then self.buffer[E+3]=_ end end end
  113. function t:drawString(q,j,x,z,_)j,x=j+self.ox,x+self.oy;local E=j;local T=x>=self.cy and x<
  114. self.cy+self.cheight;local A;local O=self.cx;local I=
  115. self.cx+self.cwidth;local N=z or self.overwrite
  116. local S=_ or self.overwrite
  117. for H=1,#q do local R=q:sub(H,H)
  118. if R=="\n"then j=E;x=x+1;if T then
  119. if x>=self.cy+self.cheight then return end else T=x>=self.cy end else A=
  120. (x*self.width+j)*3
  121. if j>=O and j<I and T then
  122. if N then self.buffer[A+1]=z end;if S then self.buffer[A+2]=_ end;self.buffer[A+3]=R end;j=j+1 end end end
  123. function e.load(q,j)local x=q
  124. if not j then local z=io.open(q,"rb")if not z then return end;local _={}
  125. local E=z:read(1)
  126. if type(E)=="number"then while E do _[#_+1]=h[E]E=z:read(1)end else while E do
  127. _[#_+1]=E;E=z:read(1)end end;z:close()x=a(_)end
  128. if x:sub(1,3)=="RIF"then local z,_=x:byte(4)*256+x:byte(5),x:byte(6)*256+
  129. x:byte(7)local t=e.create(z,_)
  130. local E=t.buffer;local T,A=8,false;local A=x:byte(index)for O=0,_-1 do
  131. for I=0,_-1 do if not T then
  132. E[(O*z+I)*3+1]=o(A/16)else E[(O*z+I)*3+1]=A%16;index=index+1
  133. x=x:byte(index)end;T=not T end end;return t elseif
  134. x:sub(1,2)=="BM"then
  135. local z=x:byte(0x13)+x:byte(0x14)*256;local _=x:byte(0x17)+x:byte(0x18)*256;if
  136.  
  137. x:byte(0xF)~=0x28 or x:byte(0x1B)~=1 or x:byte(0x1D)~=0x18 then
  138. error("unsupported bmp format, only uncompressed 24-bit rgb is supported.")end;local E,T=0x36,math.ceil((
  139. z*3)/4)*4;local t=e.create(z,_)
  140. local A=t.buffer
  141. for O=0,_-1 do
  142. for I=0,z-1 do A[(O*z+I)*3+1]=
  143. x:byte((_-O-1)*T+I*3+E+3)/255
  144. A[(O*z+I)*3+2]=x:byte(
  145. (_-O-1)*T+I*3+E+2)/255;A[(O*z+I)*3+3]=
  146. x:byte((_-O-1)*T+I*3+E+1)/255 end end;return t elseif x:find("\30")then local z,_,E=0,1,0
  147. for H=1,#x do
  148. if x:byte(H)==10 then _=_+1;if E>z then z=E end;E=0 elseif
  149. x:byte(H)==30 or x:byte(H)==31 then E=E-1 elseif x:byte(H)~=13 then E=E+1 end end;if x:byte(#x)==10 then _=_-1 end;local t=e.create(z,_)local T=t.buffer
  150. local A,O,I,N,S=1,0,0
  151. while A<=#x do
  152. if x:byte(A)==10 then O,I=0,I+1 elseif x:byte(A)==30 then A=A+1;N=s[x:sub(A,A)]elseif
  153. x:byte(A)==31 then A=A+1;S=s[x:sub(A,A)]elseif x:byte(A)~=13 then
  154. T[(I*z+O)*3+1]=N;T[(I*z+O)*3+2]=S
  155. if N or S then
  156. T[(I*z+O)*3+3]=x:sub(A,A)elseif x:sub(A,A)~=" "then T[(I*z+O)*3+3]=x:sub(A,A)end;O=O+1 end;A=A+1 end;return t else local z,_,E=0,1,0
  157. for I=1,#x do if x:byte(I)==10 then _=_+1;if E>z then z=E end;E=0 elseif x:byte(I)~=13 then
  158. E=E+1 end end;if x:byte(#x)==10 then _=_-1 end;local t=e.create(z,_)local T=t.buffer
  159. local A,O=0,0
  160. for I=1,#x do if x:byte(I)==10 then A,O=0,O+1 elseif x:byte(I)~=13 then
  161. T[(O*z+A)*3+1]=s[x:sub(I,I)]A=A+1 end end;return t end end
  162. function t:save(q,j)j=j or"nfp"local x={}
  163. if j=="nfp"then
  164. for z=0,self.height-1 do
  165. for _=0,self.width-1 do x[#x+1]=
  166. n[self.buffer[(
  167. z*self.width+_)*3+1]]or" "end;x[#x+1]="\n"end elseif j=="nft"then
  168. for z=0,self.height-1 do local _,E,T,A
  169. for O=0,self.width-1 do T=self.buffer[
  170. (z*self.width+O)*3+1]A=self.buffer[(
  171. z*self.width+O)*3+2]if
  172. T~=_ then x[#x+1]="\30".. (n[T]or" ")_=T end;if
  173. A~=E then x[#x+1]="\31".. (n[A]or" ")E=A end;x[
  174. #x+1]=
  175. self.buffer[(z*self.width+O)*3+3]or" "end;x[#x+1]="\n"end elseif j=="rif"then x[1]="RIF"
  176. x[2]=string.char(o(self.width/256),self.width%256)
  177. x[3]=string.char(o(self.height/256),self.height%256)local z,_,E=0,false
  178. for T=0,self.width-1 do for A=0,self.height-1 do
  179. E=self.buffer[
  180. (T*self.width+A)*3+1]or 0
  181. if not _ then z=E*16 else z=z+E;x[#x+1]=string.char(z)end;_=not _ end end;if _ then x[#x+1]=string.char(z)end elseif j=="bmp"then x[1]="BM"
  182. x[2]=string.char(0,0,0,0)x[3]=string.char(0,0,0,0,0x36,0,0,0,0x28,0,0,0)
  183. x[4]=string.char(
  184. self.width%256,o(self.width/256),0,0)
  185. x[5]=string.char(self.height%256,o(self.height/256),0,0)x[6]=string.char(1,0,0x18,0,0,0,0,0)
  186. x[7]=string.char(0,0,0,0)
  187. x[8]=string.char(0x13,0x0B,0,0,0x13,0x0B,0,0,0,0,0,0,0,0,0,0)local z=math.ceil((self.width*3)/4)*4-
  188. self.width*3
  189. for E=self.height-1,0,-1 do
  190. for T=0,self.width-1
  191. do
  192. x[#x+1]=string.char((
  193. self.buffer[(E*self.width+T)*3+1]or 0)*255)
  194. x[#x+1]=string.char((
  195. self.buffer[(E*self.width+T)*3+2]or 0)*255)
  196. x[#x+1]=string.char((
  197. self.buffer[(E*self.width+T)*3+3]or 0)*255)end;x[#x+1]=("\0"):rep(z)end;local _=#a(x)
  198. x[2]=string.char(_%256,o(_/256)%256,o(_/65536),0)_=_-54
  199. x[7]=string.char(_%256,o(_/256)%256,o(_/65536),0)else error("format not supported")end;x=a(x)if q then local z=io.open(q,"wb")
  200. for _=1,#x do z:write(x:byte(_))end;z:close()end;return x end
  201. function t:drawLine(q,j,x,z,_,E,T)
  202. if q==x then
  203. q,j,x,z=q+self.ox,j+self.oy,x+self.ox,z+self.oy
  204. if q<self.cx or q>=self.cx+self.cwidth then return end;if z<j then local A=j;j=z;z=A end;if j<self.cy then j=self.cy end
  205. if z>=self.cy+
  206. self.cheight then z=self.cy+self.cheight-1 end;if _ or self.overwrite then for A=j,z do
  207. self.buffer[(A*self.width+q)*3+1]=_ end end;if E or
  208. self.overwrite then for A=j,z do
  209. self.buffer[(A*self.width+q)*3+2]=E end end;if T or
  210. self.overwrite then for A=j,z do
  211. self.buffer[(A*self.width+q)*3+3]=T end end elseif j==z then q,j,x,z=q+
  212. self.ox,j+self.oy,x+self.ox,z+self.oy
  213. if j<self.cy or j>=
  214. self.cy+self.cheight then return end;if x<q then local A=q;q=x;x=A end;if q<self.cx then q=self.cx end
  215. if x>=self.cx+
  216. self.cwidth then x=self.cx+self.cwidth-1 end;if _ or self.overwrite then for A=q,x do
  217. self.buffer[(j*self.width+A)*3+1]=_ end end;if E or
  218. self.overwrite then for A=q,x do
  219. self.buffer[(j*self.width+A)*3+2]=E end end;if T or
  220. self.overwrite then for A=q,x do
  221. self.buffer[(j*self.width+A)*3+3]=T end end else
  222. local A=x-q;local O=A>0 and 1 or-1;A=2*math.abs(A)local I=z-j
  223. local N=I>0 and 1 or-1;I=2*math.abs(I)self:drawPixel(q,j,_,E,T)
  224. if A>=I then
  225. local S=I-A/2
  226. while q~=x do
  227. if(S>=0)and((S~=0)or(O>0))then S=S-A;j=j+N end;S=S+I;q=q+O;self:drawPixel(q,j,_,E,T)end else local S=A-I/2
  228. while j~=z do
  229. if(S>=0)and((S~=0)or(N>0))then S=S-I;q=q+O end;S=S+A;j=j+N;self:drawPixel(q,j,_,E,T)end end end end
  230. function t:drawRect(q,j,x,z,_,E,T)self:drawLine(q,j,q+x-1,j,_,E,T)
  231. self:drawLine(q,j,q,j+z-1,_,E,T)self:drawLine(q+x-1,j,q+x-1,j+z-1,_,E,T)self:drawLine(q,
  232. j+z-1,q+x-1,j+z-1,_,E,T)end
  233. function t:fillRect(q,j,x,z,_,E,T)
  234. q,j,x,z=b(q+self.ox,j+self.oy,x,z,self.cx,self.cy,self.cwidth,self.cheight)
  235. if _ or self.overwrite then for A=0,z-1 do
  236. for O=0,x-1 do self.buffer[
  237. ((A+j)*self.width+O+q)*3+1]=_ end end end
  238. if E or self.overwrite then for A=0,z-1 do
  239. for O=0,x-1 do self.buffer[
  240. ((A+j)*self.width+O+q)*3+2]=E end end end
  241. if T or self.overwrite then for A=0,z-1 do
  242. for O=0,x-1 do self.buffer[
  243. ((A+j)*self.width+O+q)*3+3]=T end end end end
  244. function t:drawTriangle(q,j,x,z,_,E,T,A,O)self:drawLine(q,j,x,z,T,A,O)
  245. self:drawLine(x,z,_,E,T,A,O)self:drawLine(_,E,q,j,T,A,O)end
  246. function t:fillTriangle(q,j,x,z,_,E,T,A,O)if j>z then local F,W=q,j;q,j=x,z;x,z=F,W end
  247. if j>E then local F,W=q,j;q,j=_,E;_,E=F,W end;if z>E then local F,W=x,z;x,z=_,E;_,E=F,W end
  248. if j==z and q>x then local F=q;q=x;x=F end;if z==E and x>_ then local F=x;x=_;_=F end;local I,N
  249. if q<=x then I=q+
  250. (z-j)/ (E-j)* (_-q)N=z;local F,W=x,z;x,z=I,N;I,N=F,W else I=q+
  251. (z-j)/ (E-j)* (_-q)N=z end;local S=(x-q)/ (z-j)local H=(I-q)/ (N-j)
  252. local R=(_-x)/ (E-z)local D=(_-I)/ (E-N)local L,U,C,M
  253. for F=math.ceil(j+0.5)-0.5,
  254. math.floor(E-0.5)+0.5,1 do if F<=z then L=q+S* (F-j)U=q+H* (F-j)else
  255. L=_-R* (E-F)U=_-D* (E-F)end;C,M=math.ceil(L-0.5),math.floor(
  256. U-0.5)if C<=M then
  257. self:drawLine(C,F-0.5,M,F-0.5,T,A,O)end end end
  258. function t:drawEllipse(q,j,x,z,_,E,T)for A=0,d-1 do
  259. self:drawLine(o(q+u[A+1]* (x-1)+0.5),o(j+l[A+1]*
  260. (z-1)+0.5),o(
  261. q+u[(A+1)%d+1]* (x-1)+0.5),o(
  262. j+l[(A+1)%d+1]* (z-1)+0.5),_,E,T)end end
  263. function t:fillEllipse(q,j,x,z,_,E,T)q,j=q+self.ox,j+self.oy;local A,O
  264. for I=0,z-1 do
  265. for N=0,x-1 do A,O=N+q,I+j
  266. if
  267.  
  268.  
  269. (
  270. (N+0.5)/x*2-1)^2+ ((I+0.5)/z*2-1)^2 <=1 and A>=self.cx and A<
  271. self.cx+self.cwidth and O>=self.cy and O<self.cy+self.cheight then if _ or self.overwrite then
  272. self.buffer[(O*self.width+A)*3+1]=_ end
  273. if E or self.overwrite then self.buffer[
  274. (O*self.width+A)*3+2]=E end;if T or self.overwrite then
  275. self.buffer[(O*self.width+A)*3+3]=T end end end end end
  276. function t:drawArc(q,j,x,z,_,E,T,A,O)if _>E then local I=_;_=E;I=E end
  277. _=o(_/math.pi/2*d+0.5)E=o(E/math.pi/2*d+0.5)-1
  278. for I=_,E do
  279. local N=I%d
  280. self:drawLine(o(q+u[N+1]* (x-1)+0.5),o(
  281. j+l[N+1]* (z-1)+0.5),o(q+u[(N+1)%d+1]* (x-1)+0.5),o(
  282. j+l[(N+1)%d+1]* (z-1)+0.5),T,A,O)end end
  283. function t:fillArc(q,j,x,z,_,E,T,A,O)q,j=q+self.ox,j+self.oy;if _>E then local L=_;_=E;L=E end
  284. local I=E-_;_=_% (math.pi*2)local N,S,H,R,D
  285. for L=0,z-1 do
  286. for U=0,x-1 do N,S=(U+0.5)/x*2-1,
  287. (L+0.5)/z*2-1;H,R=U+q,L+j
  288. D=i(-S,N)% (math.pi*2)
  289. if
  290.  
  291.  
  292. N^2+S^2 <=1 and((D>=_ and D-_<=I)or
  293. (D<= (_+I)% (math.pi*2)))and H>=self.cx and
  294. H<self.cx+self.cwidth and R>=self.cy and R<self.cy+self.cheight then if T or self.overwrite then
  295. self.buffer[(R*self.width+H)*3+1]=T end
  296. if A or self.overwrite then self.buffer[
  297. (R*self.width+H)*3+2]=A end;if O or self.overwrite then
  298. self.buffer[(R*self.width+H)*3+3]=O end end end end end
  299. function t:drawSurface(q,j,x,z,_,E,T,A,O)j,x,z,_,E,T,A,O=j+self.ox,x+self.oy,z or q.width,_ or q.height,E or 0,T or 0,A or q.width,O or
  300. q.height
  301. if z==A and _==O then local I,N
  302. I,N,z,_=b(j,x,z,_,self.cx,self.cy,self.cwidth,self.cheight)A,O=z,_;if I>j then E=E+I-j;j=I end;if N>x then T=T+N-x;x=N end
  303. I,N,A,O=b(E,T,A,O,0,0,q.width,q.height)z,_=A,O;if I>E then j=j+I-E;E=I end;if N>T then x=x+N-T;T=N end;local S,H,R
  304. for D=0,_-1 do
  305. for L=0,z-1 do
  306. S=q.buffer[((
  307. D+T)*q.width+L+E)*3+1]
  308. H=q.buffer[((D+T)*q.width+L+E)*3+2]
  309. R=q.buffer[((D+T)*q.width+L+E)*3+3]
  310. if S or self.overwrite then self.buffer[
  311. ((D+x)*self.width+L+j)*3+1]=S end
  312. if H or self.overwrite then self.buffer[
  313. ((D+x)*self.width+L+j)*3+2]=H end
  314. if R or self.overwrite then self.buffer[
  315. ((D+x)*self.width+L+j)*3+3]=R end end end else local I,N=false,false;if z<0 then I=true;j=j+z end;if _<0 then N=true;x=x+_ end;if A<0 then
  316. I=not I;E=E+A end;if O<0 then N=not N;T=T+O end
  317. z,_,A,O=math.abs(z),math.abs(_),math.abs(A),math.abs(O)local S,H,R,D,L,U,C,M,F=A/z,O/_
  318. for W=0,_-1 do
  319. for Y=0,z-1 do
  320. R,D=o((Y+0.5)*S),o((W+0.5)*H)if I then L=j+z-Y-1 else L=Y+j end;if N then U=x+_-W-1 else U=W+x end
  321. if
  322.  
  323.  
  324.  
  325. L>=self.cx and L<self.cx+self.cwidth and U>=self.cy and U<self.cy+self.cheight and
  326. R>=0 and R<q.width and D>=0 and D<q.height then C=q.buffer[(D*q.width+R)*3+1]M=q.buffer[(
  327. D*q.width+R)*3+2]F=q.buffer[(D*
  328. q.width+R)*3+3]if
  329. C or self.overwrite then
  330. self.buffer[(U*self.width+L)*3+1]=C end
  331. if M or self.overwrite then self.buffer[
  332. (U*self.width+L)*3+2]=M end;if F or self.overwrite then
  333. self.buffer[(U*self.width+L)*3+3]=F end end end end end end
  334. function t:drawSurfaceRotated(q,j,x,z,_,E)local T,A,O,I,N,S=math.sin(E),math.cos(E)
  335. for H=math.floor(-q.height*0.75),math.ceil(
  336. q.height*0.75)do
  337. for R=math.floor(-q.width*0.75),math.ceil(q.width*0.75)
  338. do
  339. O,I,N,S=j+R,x+H,o(A* (R+0.5)-T* (H+0.5)+z),o(T*
  340. (R+0.5)+A* (H+0.5)+_)
  341. if
  342.  
  343.  
  344. O>=self.cx and O<self.cx+self.cwidth and I>=self.cy and I<self.cy+self.cheight and N>=
  345. 0 and N<q.width and S>=0 and S<q.height then b=q.buffer[(S*q.width+N)*3+1]t=q.buffer[(
  346. S*q.width+N)*3+2]c=q.buffer[(S*
  347. q.width+N)*3+3]if
  348. b or self.overwrite then
  349. self.buffer[(I*self.width+O)*3+1]=b end
  350. if t or self.overwrite then self.buffer[
  351. (I*self.width+O)*3+2]=t end;if c or self.overwrite then
  352. self.buffer[(I*self.width+O)*3+3]=c end end end end end
  353. function t:drawSurfacesInterlaced(q,j,x,z)j,x,z=j+self.ox,x+self.oy,z or 0
  354. local _,E=q[1].width,q[1].height
  355. for D=2,#q do if q[D].width~=_ or q[D].height~=E then
  356. error("surfaces should be the same size")end end
  357. local T,A,O,I,N,S,H,R=b(j,x,_,E,self.cx,self.cy,self.cwidth,self.cheight)
  358. for D=A,A+I-1 do
  359. for L=T,T+O-1 do N=(L+D+z)%#q+1
  360. S=q[N].buffer[(
  361. (D-A)*q[N].width+L-T)*3+1]
  362. H=q[N].buffer[((D-A)*q[N].width+L-T)*3+2]
  363. R=q[N].buffer[((D-A)*q[N].width+L-T)*3+3]if S or self.overwrite then
  364. self.buffer[(D*self.width+L)*3+1]=S end
  365. if H or self.overwrite then self.buffer[
  366. (D*self.width+L)*3+2]=H end;if R or self.overwrite then
  367. self.buffer[(D*self.width+L)*3+3]=R end end end end
  368. function t:drawSurfaceSmall(q,j,x)j,x=j+self.ox,x+self.oy;if q.width%2 ~=0 or
  369. q.height%3 ~=0 then
  370. error("surface width must be a multiple of 2 and surface height a multiple of 3")end
  371. local z,_,E,T,A,O,I,N=32768
  372. for S=0,q.height/3-1 do
  373. for H=0,q.width/2-1 do
  374. if H+j>=self.cx and
  375. H+j<self.cx+self.cwidth and S+x>=self.cy and S+x<self.cy+
  376. self.cheight then
  377. _,E,T,A,O,I,N=0,q.buffer[((
  378. S*3)*q.width+H*2)*3+1],q.buffer[((
  379. S*3)*q.width+H*2+1)*3+1],q.buffer[((
  380. S*3+1)*q.width+H*2)*3+1],q.buffer[((
  381. S*3+1)*q.width+H*2+1)*3+1],q.buffer[((
  382. S*3+2)*q.width+H*2)*3+1],q.buffer[((
  383. S*3+2)*q.width+H*2+1)*3+1]if E~=N then z=E;_=1 end;if T~=N then z=T;_=_+2 end;if A~=N then z=A;_=_+4 end;if O~=N then z=O
  384. _=_+8 end;if I~=N then z=I;_=_+16 end
  385. self.buffer[(
  386. (S+x)*self.width+H+j)*3+1]=N
  387. self.buffer[((S+x)*self.width+H+j)*3+2]=z;self.buffer[((S+x)*self.width+H+j)*3+3]=h[
  388. 128+_]end end end end
  389. function t:flip(q,j)local x,z,_,E,T,A,O
  390. if q then
  391. for I=0,math.ceil(self.cwidth/2)-1 do
  392. for N=0,self.cheight-1
  393. do
  394. x,z,_,E=I+self.cx,N+self.cy,self.cx+self.cwidth-I-1,N+self.cy
  395. T=self.buffer[(z*self.width+x)*3+1]
  396. A=self.buffer[(z*self.width+x)*3+2]
  397. O=self.buffer[(z*self.width+x)*3+3]
  398. self.buffer[(z*self.width+x)*3+1]=self.buffer[(
  399. E*self.width+_)*3+1]
  400. self.buffer[(z*self.width+x)*3+2]=self.buffer[(
  401. E*self.width+_)*3+2]
  402. self.buffer[(z*self.width+x)*3+3]=self.buffer[(
  403. E*self.width+_)*3+3]
  404. self.buffer[(E*self.width+_)*3+1]=T
  405. self.buffer[(E*self.width+_)*3+2]=A
  406. self.buffer[(E*self.width+_)*3+3]=O end end end
  407. if j then
  408. for I=0,math.ceil(self.cheight/2)-1 do
  409. for N=0,self.cwidth-1 do
  410. x,z,_,E=N+self.cx,
  411. I+self.cy,N+self.cx,self.cy+self.cheight-I-1
  412. T=self.buffer[(z*self.width+x)*3+1]
  413. A=self.buffer[(z*self.width+x)*3+2]
  414. O=self.buffer[(z*self.width+x)*3+3]
  415. self.buffer[(z*self.width+x)*3+1]=self.buffer[(
  416. E*self.width+_)*3+1]
  417. self.buffer[(z*self.width+x)*3+2]=self.buffer[(
  418. E*self.width+_)*3+2]
  419. self.buffer[(z*self.width+x)*3+3]=self.buffer[(
  420. E*self.width+_)*3+3]
  421. self.buffer[(E*self.width+_)*3+1]=T
  422. self.buffer[(E*self.width+_)*3+2]=A
  423. self.buffer[(E*self.width+_)*3+3]=O end end end end
  424. function t:shift(q,j,x,z,_)local E,T=q<0,j<0
  425. local A,O=self.cx,self.cx+self.cwidth-1;local I,N=self.cy,self.cy+self.cheight-1;local S,H
  426. for R=T and I or N,
  427. T and N or I,T and 1 or-1 do
  428. for D=E and A or O,E and O or A,E and 1 or-1 do S,H=D-q,R-j
  429. if S>=0 and S<
  430. self.width and H>=0 and H<self.height then
  431. self.buffer[(
  432. R*self.width+D)*3+1]=self.buffer[(
  433. H*self.width+S)*3+1]
  434. self.buffer[(R*self.width+D)*3+2]=self.buffer[(
  435. H*self.width+S)*3+2]
  436. self.buffer[(R*self.width+D)*3+3]=self.buffer[(
  437. H*self.width+S)*3+3]else
  438. self.buffer[(R*self.width+D)*3+1]=x
  439. self.buffer[(R*self.width+D)*3+2]=z
  440. self.buffer[(R*self.width+D)*3+3]=_ end end end end
  441. function t:map(q)local j
  442. for x=self.cy,self.cy+self.cheight-1 do
  443. for z=self.cx,self.cx+self.cwidth-
  444. 1 do
  445. j=q[self.buffer[(x*self.width+z)*3+1]]if j or self.overwrite then
  446. self.buffer[(x*self.width+z)*3+1]=j end end end end;e.palette={}
  447. e.palette.cc={[1]="F0F0F0",[2]="F2B233",[4]="E57FD8",[8]="99B2F2",[16]="DEDE6C",[32]="7FCC19",[64]="F2B2CC",[128]="4C4C4C",[256]="999999",[512]="4C99B2",[1024]="B266E5",[2048]="3366CC",[4096]="7F664C",[8192]="57A64E",[16384]="CC4C4C",[32768]="191919"}
  448. e.palette.riko4={"181818","1D2B52","7E2553","008651","AB5136","5F564F","7D7F82","FF004C","FFA300","FFF023","00E755","29ADFF","82769C","FF77A9","FECCA9","ECECEC"}e.palette.redirection={[0]="040404",[1]="FFFFFF"}
  449. local function g(q)
  450. if q==m then return end;m=q;f,w,y,p={},{},{},{}local j={}
  451. for O,I in pairs(m)do
  452. if type(I)=="string"then w[O]=
  453. tonumber(I:sub(1,2),16)/255
  454. y[O]=tonumber(I:sub(3,4),16)/255;p[O]=tonumber(I:sub(5,6),16)/255 elseif
  455. type(I)=="number"then w[O]=math.floor(I/65536)/255;y[O]=(
  456. math.floor(I/256)%256)/255
  457. p[O]=(I%256)/255 end;j[#j+1]=O end;local x,z,_,E,T,A
  458. for O=0,c-1 do
  459. for I=0,c-1 do
  460. for N=0,c-1 do x=(O+0.5)/c;z=(I+0.5)/c;_=(N+0.5)/c
  461. E=1e10
  462. for S=1,#j do T=(x-w[j[S]])^2+ (z-y[j[S]])^2+
  463. (_-p[j[S]])^2;if T<E then E=T;A=S end end;f[O*c*c+I*c+N+1]=j[A]end end end end
  464. function t:toRGB(q)g(q)local j
  465. for x=0,self.height-1 do
  466. for z=0,self.width-1 do j=self.buffer[
  467. (x*self.width+z)*3+1]
  468. self.buffer[(
  469. x*self.width+z)*3+1]=w[j]
  470. self.buffer[(x*self.width+z)*3+2]=y[j]
  471. self.buffer[(x*self.width+z)*3+3]=p[j]end end end
  472. function t:toPalette(q,j)g(q)local x,z,_,E,T,A,O,I,N,S,H=c-1
  473. for R=0,self.height-1 do
  474. for D=0,self.width-1 do z=self.buffer[(
  475. R*self.width+D)*3+1]
  476. _=self.buffer[(
  477. R*self.width+D)*3+2]
  478. E=self.buffer[(R*self.width+D)*3+3]z=(z>1)and 1 or z;z=(z<0)and 0 or z
  479. _=(_>1)and 1 or _;_=(_<0)and 0 or _;E=(E>1)and 1 or E
  480. E=(E<0)and 0 or E;T=(z==1)and x or o(z*c)
  481. A=(_==1)and x or o(_*c)O=(E==1)and x or o(E*c)
  482. I=f[T*c*c+A*c+O+1]
  483. if j then N=(z-w[I])/16;S=(_-y[I])/16;H=(E-p[I])/16
  484. if D<
  485. self.width-1 then self.buffer[(R*self.width+D+1)*3+1]=
  486. self.buffer[(
  487. R*self.width+D+1)*3+1]+N*7;self.buffer[(R*
  488. self.width+D+1)*3+2]=
  489. self.buffer[(
  490. R*self.width+D+1)*3+2]+S*7;self.buffer[(R*
  491. self.width+D+1)*3+3]=
  492. self.buffer[(
  493. R*self.width+D+1)*3+3]+H*7 end
  494. if R<self.height-1 then
  495. if D>0 then
  496. self.buffer[
  497. ((R+1)*self.width+D-1)*3+1]=self.buffer[(
  498. (R+1)*self.width+D-1)*3+1]+N*3;self.buffer[((R+1)*self.width+D-1)*3+2]=
  499. self.buffer[((
  500. R+1)*self.width+D-1)*3+2]+S*3;self.buffer[((
  501. R+1)*self.width+D-1)*3+3]=
  502. self.buffer[((
  503. R+1)*self.width+D-1)*3+3]+H*3 end;self.buffer[((R+1)*self.width+D)*3+1]=
  504. self.buffer[((
  505. R+1)*self.width+D)*3+1]+N*5;self.buffer[(
  506. (R+1)*self.width+D)*3+2]=
  507. self.buffer[((
  508. R+1)*self.width+D)*3+2]+S*5;self.buffer[(
  509. (R+1)*self.width+D)*3+3]=
  510. self.buffer[((
  511. R+1)*self.width+D)*3+3]+H*5
  512. if D<
  513. self.width-1 then
  514. self.buffer[
  515. ((R+1)*self.width+D+1)*3+1]=self.buffer[
  516. ((R+1)*self.width+D+1)*3+1]+N*1;self.buffer[((R+1)*self.width+D+1)*3+2]=
  517. self.buffer[((
  518. R+1)*self.width+D+1)*3+2]+S*1;self.buffer[((
  519. R+1)*self.width+D+1)*3+3]=
  520. self.buffer[((
  521. R+1)*self.width+D+1)*3+3]+H*1 end end end
  522. self.buffer[(R*self.width+D)*3+1]=I
  523. self.buffer[(R*self.width+D)*3+2]=nil
  524. self.buffer[(R*self.width+D)*3+3]=nil end end end
  525. function e.loadFont(t)local q={width=t.width,height=t.height-1}q.buffer={}
  526. q.indices={0}q.widths={}
  527. local j,x,z=t.buffer[((t.height-1)*t.width)*3+1]for E=0,t.width-1 do z=t.buffer[
  528. ((t.height-1)*t.width+E)*3+1]
  529. if z~=j then x=z;break end end;for E=0,t.height-2 do
  530. for T=0,t.width-1 do q.buffer[E*
  531. q.width+T+1]=
  532. t.buffer[(E*t.width+T)*3+1]==x end end;local _=1
  533. for E=0,t.width-1 do if
  534. t.buffer[((
  535. t.height-1)*t.width+E)*3+1]==x then q.widths[_]=E-q.indices[_]_=_+1;q.indices[_]=
  536. E+1 end end;q.widths[_]=q.width-q.indices[_]return q end
  537. function e.getTextSize(q,j)local x,z,_=0,0,0;local E,T=x
  538. for A=1,#q do T=q:byte(A)-31
  539. if T+31 ==10 then x=E
  540. z=z+j.height+1 elseif j.indices[T]then x=x+j.widths[T]+1 else x=x+j.widths[1]end;if x>_ then _=x end end;return _-1,z+j.height end
  541. function t:drawText(q,j,z,_,E,T,A)local x,O=z+self.ox,_+self.oy;local I,N,S=x
  542. for H=1,#q do N=q:byte(H)-31
  543. if
  544. N+31 ==10 then x=I;O=O+j.height+1 elseif j.indices[N]then
  545. for H=0,j.widths[N]-1 do
  546. for R=0,j.height-1 do z,_=x+H,
  547. O+R
  548. if
  549. j.buffer[R*j.width+H+j.indices[N]+1]then
  550. if
  551. z>=self.cx and z<self.cx+self.cwidth and _>=self.cy and _<self.cy+self.cheight then S=(
  552. _*self.width+z)*3;if E or self.overwrite then self.buffer[
  553. S+1]=E end;if T or self.overwrite then
  554. self.buffer[S+2]=T end
  555. if A or self.overwrite then self.buffer[S+3]=A end end end end end;x=x+j.widths[N]+1 else x=x+j.widths[1]end end end;local k={}e.smap=k
  556. function e.loadSpriteMap(t,q,j,x)if t.width%q~=0 or t.height%j~=0 then
  557. error("sprite width/height does not match smap width/height")end
  558. local k=setmetatable({},{__index=e.smap})k.surf=t;k.spwidth=q;k.spheight=j;k.sprites=x or
  559. ((t.width/q)* (t.height/j))k.perline=t.width/q;return k end
  560. function k:pos(q,j)if q<0 or q>=self.sprites then
  561. error("sprite index out of bounds")end
  562. return
  563. (q%self.perline)*self.spwidth,math.floor(q/self.perline)*self.spheight end
  564. function k:sprite(q,j,x,z,_)local E,T=self:pos(q)return self.surf,j,x,z or self.spwidth,_ or self.spheight,E,T,
  565. self.spwidth,self.spheight end end;return e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement