gfx.l 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. (
  2. (struct surface
  3. width 0
  4. height 0
  5. shift 1
  6. pitch 0
  7. x 0
  8. y 0
  9. pixels [])
  10. (struct font
  11. surface (surface)
  12. rune-w 16
  13. rune-h 16
  14. rune-mod 256 ;; ?
  15. spacing 8) ;; todo kerning table
  16. (def fb (surface))
  17. (sput fb pixels (mmap "/framebuffer"))
  18. (sput fb width (load "/framebuffer/width"))
  19. (sput fb height (load "/framebuffer/height"))
  20. (sput fb shift (- (load "/framebuffer/depth") 1))
  21. (sput fb pitch (shl (sget fb width) (sget fb shift)))
  22. ;(print (list "[gfx] fb " fb))
  23. (def screen (open "/framebuffer"))
  24. (def set-pixel (fn (surf surface) x y c (do
  25. (let pixels (sget surf pixels))
  26. (let ofs (+ (* y (sget surf pitch)) (shl x (sget surf shift))))
  27. (put16 pixels ofs c)
  28. c
  29. )))
  30. (def pt list)
  31. (def line (fn (s surface) a b color (do
  32. (let xa (car a))
  33. (let ya (car (cdr a)))
  34. (let xb (car b))
  35. (let yb (car (cdr b)))
  36. (let dx (abs (- xb xa)))
  37. (let dy (abs (- yb ya)))
  38. (let sx (if (lt xa xb) 1 -1))
  39. (let sy (if (lt ya yb) 1 -1))
  40. (let err (if (gt dx dy) dx (- 0 dy)))
  41. (let err (/ err 2))
  42. (let e2 0)
  43. (while (not (and (eq xa xb) (eq ya yb))) (do
  44. (set-pixel s xa ya color)
  45. (let e2 err)
  46. (if (gt e2 (- 0 dx)) (do (let err (- err dy)) (let xa (+ xa sx))) 0)
  47. (if (lt e2 dy) (do (let err (+ err dx)) (let ya (+ ya sy))) 0)
  48. ))
  49. 0
  50. )))
  51. (def unifont-surf (surface))
  52. (sput unifont-surf pixels (load "/sd/unifont.bin"))
  53. (sput unifont-surf width 4096)
  54. (sput unifont-surf height 4096)
  55. (sput unifont-surf pitch 4096)
  56. (sput unifont-surf shift 0)
  57. (def unifont (font))
  58. (sput unifont surface unifont-surf)
  59. (sput unifont rune-w 16)
  60. (sput unifont rune-h 16)
  61. (sput unifont rune-mod 256) ;; 256 chars per row
  62. (sput unifont spacing 8)
  63. (def blit-char16 (fn (surf surface) (font font) rune x y (do
  64. (let sx 0)
  65. (let sy 0)
  66. (let so 0)
  67. (let ddo 0)
  68. (let iy 0)
  69. (let rune-ww 0)
  70. (let c 0)
  71. (let d 0)
  72. (let rune-mod (sget font rune-mod))
  73. (let font-surf (sget font surface))
  74. (let font-pitch (sget font-surf pitch))
  75. (let screen-pitch (sget surf pitch))
  76. (let rune-spacing (sget font spacing))
  77. (let rune-w (sget font rune-w))
  78. (let rune-h (sget font rune-h))
  79. (let pixels (sget surf pixels))
  80. (let font-pixels (sget font-surf pixels))
  81. (let sx (* rune-w (% rune rune-mod)))
  82. (let sy (* rune-h (/ rune rune-mod)))
  83. (let so (+ sx (* sy font-pitch)))
  84. (let ddo (+ (shl x 1) (* y screen-pitch)))
  85. (let rune-ww (shl rune-spacing 1))
  86. (while (lt iy rune-h) (do
  87. (let ix 0)
  88. (while (lt ix rune-ww) (do
  89. (let c (get8 font-pixels (+ so (shr ix 1))))
  90. (put16 pixels (+ ddo ix) (bitor c (shl c 8)))
  91. (let ix (+ ix 2))
  92. ))
  93. (let so (+ so font-pitch))
  94. (let ddo (+ ddo screen-pitch))
  95. (let iy (+ iy 1))
  96. ))
  97. 0
  98. )))
  99. ; (def blit-char (fn rune x y (do
  100. ; (let sx 0)
  101. ; (let sy 0)
  102. ; (let so 0)
  103. ; (let do 0)
  104. ; (let iy 0)
  105. ; (let rune-ww 0)
  106. ; (let c 0)
  107. ; (let d 0)
  108. ; (let sx (* rune-w (% rune rune-mod)))
  109. ; (let sy (* rune-h (/ rune rune-mod)))
  110. ; (let so (+ sx (* sy font-pitch)))
  111. ; (let do (+ (* x 2) (* y screen-pitch)))
  112. ; (let rune-ww rune-spacing)
  113. ; (while (lt iy rune-h) (do
  114. ; (let ix 0)
  115. ; (let dx 0)
  116. ; (while (lt ix rune-ww) (do
  117. ; (let c (get font (+ so ix)))
  118. ; (let dx (+ do (shl ix 1)))
  119. ; (put fb dx c)
  120. ; (put fb (+ dx 1) c)
  121. ; (let ix (+ ix 1))
  122. ; ))
  123. ; (let so (+ so font-pitch))
  124. ; (let do (+ do screen-pitch))
  125. ; (let iy (+ iy 1))
  126. ; ))
  127. ; 0
  128. ; )))
  129. ; (def blit-char8 (fn rune x y (do
  130. ; (let sx 0)
  131. ; (let sy 0)
  132. ; (let so 0)
  133. ; (let do 0)
  134. ; (let iy 0)
  135. ; (let rune-ww 0)
  136. ; (let c 0)
  137. ; (let d 0)
  138. ; (let sx (* rune-w (% rune rune-mod)))
  139. ; (let sy (* rune-h (/ rune rune-mod)))
  140. ; (let so (+ sx (* sy font-pitch)))
  141. ; (let do (+ x (* y screen-pitch)))
  142. ; (let rune-ww rune-spacing)
  143. ; (while (lt iy rune-h) (do
  144. ; (let ix 0)
  145. ; (let dx 0)
  146. ; (while (lt ix rune-ww) (do
  147. ; (let c (get font (+ so ix)))
  148. ; (let dx (+ do ix))
  149. ; (put fb dx c)
  150. ; (let ix (+ ix 1))
  151. ; ))
  152. ; (let so (+ so font-pitch))
  153. ; (let do (+ do screen-pitch))
  154. ; (let iy (+ iy 1))
  155. ; ))
  156. ; 0
  157. ; )))
  158. ; (if (eq screen-bpp 1) (def blit-char blit-char8) 0)
  159. ; (def grab-from fb)
  160. ; (def grab-pitch screen-pitch)
  161. ; (def grab (fn x y w h (do
  162. ; (let xx 0)
  163. ; (let yy 0)
  164. ; (let di 0)
  165. ; (let yy (+ y 0))
  166. ; (let xw (+ x w))
  167. ; (let yh (+ y h))
  168. ; (let res (alloc (* (shl w 1) h)))
  169. ; (let from grab-from)
  170. ; (let pitch grab-pitch)
  171. ; (while (lt yy yh) (do
  172. ; (let xx (+ x 0))
  173. ; (while (lt xx xw) (do
  174. ; (put res di (get from (+ xx (* pitch yy))))
  175. ; (let di (+ di 1))
  176. ; (put res di (get from (+ (+ xx (* pitch yy)) 1)))
  177. ; (let di (+ di 1))
  178. ; (let xx (+ xx 1))
  179. ; ))
  180. ; (let yy (+ yy 1))
  181. ; ))
  182. ; res
  183. ; )))
  184. (def boxfill (fn (surf surface) x y w h color (do
  185. (if (lt x 0) (let x 0) 0)
  186. (if (lt y 0) (let y 0) 0)
  187. (let dw (sget surf width))
  188. (let dh (sget surf height))
  189. (if (gt (+ x w) (- dw 1)) (let w (- dw x)) 0)
  190. (if (gt (+ y h) (- dh 1)) (let h (- dh y)) 0)
  191. (let ofs 0)
  192. (let xi 0)
  193. (let yi 0)
  194. (let xi (+ x 0)) ; TODO get rid of this
  195. (let yi (+ y 0))
  196. (let xx (+ x w))
  197. (let yy (+ y h))
  198. (let ww (shl w 1))
  199. (let pitch (sget surf pitch))
  200. (let ofs (+ (* y pitch) (shl x 1)))
  201. (let pixels (sget surf pixels))
  202. (while (lt yi yy) (do
  203. (let xi (+ x 0))
  204. (while (lt xi xx) (do
  205. (put16 pixels ofs color)
  206. (let xi (+ xi 1))
  207. (let ofs (+ ofs 2))
  208. ))
  209. (let ofs (- (+ ofs pitch) ww))
  210. (let yi (+ yi 1))
  211. ))
  212. 0 ; FIXME crashes x64 if this is not here
  213. )))
  214. (def blit (fn (dest surface) (from surface) (do
  215. (let xx 0)
  216. (let yy 0)
  217. (let di 0)
  218. (let si 0)
  219. (let yy (+ (sget from y) 0))
  220. (let xw (+ (sget from x) (sget from width)))
  221. (let yh (+ (sget from y) (sget from height)))
  222. (let to (sget dest pixels))
  223. (let shift (sget dest shift))
  224. (let w (sget from width))
  225. (let c 0)
  226. (let pixels (sget from pixels))
  227. ;; clipping
  228. (let x (sget from x))
  229. (let xskip 0)
  230. (if (lt yy 0) (do
  231. (let si (+ si (* (abs yy) (shl w 1))))
  232. (let yy 0)
  233. ) 0)
  234. (if (gt yh (- (sget dest height) 1)) (do
  235. (let yh (- (sget dest height) 1))
  236. ) 0)
  237. (if (lt x 0) (do
  238. (let xskip (abs x))
  239. (let w (- w xskip))
  240. (let x (+ x xskip))
  241. ) 0)
  242. (if (gt xw (- (sget dest width) 1)) (do
  243. (let xskip (- xw (sget dest width)))
  244. (let si (- si (shl xskip 1)))
  245. (let xw (- xw xskip))
  246. (let w (- w xskip))
  247. ) 0)
  248. (let pitch (- (sget dest pitch) (shl w shift)))
  249. (let di (+ (shl x shift) (* yy (sget dest pitch))))
  250. (while (lt yy yh) (do
  251. (let xx (+ x 0))
  252. (let si (+ si (shl xskip 1)))
  253. (while (lt xx xw) (do
  254. (let c (get16 pixels si))
  255. (put16 to di c)
  256. (let si (+ si 2))
  257. (let di (+ di 2))
  258. (let xx (+ xx 1))
  259. ))
  260. (let yy (+ yy 1))
  261. (let di (+ di pitch))
  262. ))
  263. 1
  264. )))
  265. (def blit-str (fn (surf surface) (font font) str x y (do
  266. (let i 0)
  267. (let xx 0)
  268. (let yy 0)
  269. (let xx (+ x 0))
  270. (let yy (+ y 0))
  271. (let sz (+ (size str) 0))
  272. (let c 0)
  273. (let maxx (sget surf width))
  274. (let maxy (sget surf height))
  275. (let rune-spacing (sget font spacing))
  276. (let rune-h (sget font rune-h))
  277. (while (lt i sz) (do
  278. (let c (get8 str i))
  279. (blit-char16 surf font c xx yy)
  280. (let xx (+ xx rune-spacing))
  281. ; newline
  282. (if (or (eq c 10) (gt xx maxx)) (do
  283. (let xx x)
  284. (let yy (+ yy rune-h))
  285. (if (gt yy maxy) (do
  286. (let yy y)) 0)
  287. ) 0)
  288. (let i (+ i 1))
  289. (if (get8 str i) 0 (let i sz)) ; stop at 0
  290. ))
  291. yy
  292. )))
  293. (def triangle (fn (surf surface) a b c color (do
  294. (line surf a b color)
  295. (line surf b c color)
  296. (line surf a c color)
  297. )))
  298. (def box (fn (surf surface) x y x2 y2 color (do
  299. (let tl (list x y))
  300. (let tr (list x2 y))
  301. (let bl (list x y2))
  302. (let br (list x2 y2))
  303. (line surf tl tr color)
  304. (line surf bl br color)
  305. (line surf tr br color)
  306. (line surf tl bl color)
  307. 0
  308. )))
  309. (def circle (fn (surf surface) cx cy r color (do
  310. (let x 0)
  311. (while (lt x 359) (do
  312. (set-pixel surf (+ cx (* (sin x) r)) (+ cy (* (cos x) r)) color)
  313. (let x (+ x 1))
  314. ))
  315. x
  316. )))
  317. (def clear (fn (boxfill fb 0 0 (- (sget fb width) 1) (- (sget fb height) 1) 0xffff)))
  318. )