January 16, 2021, 06:03:07 AM

Author Topic: [bb] OverlayImage by Mikle [ 1+ years ago ]  (Read 1565 times)

BlitzBot

• Jr. Member
• Posts: 1
[bb] OverlayImage by Mikle [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : OverlayImage
Author : Mikle
Posted : 1+ years ago

Description : Draws "repeating"/"cycling" image

Code :
Code: BlitzBasic
1. ; This function was made by Mikle's friend Dima (DiGlib)
2. ; with the help of MathLab. Thanks to MathLab developers!
3.
4. ; Draws "repeating"/"cycling" image
5.
6. Function OverlayImage(img, x, y, rect_x, rect_y, rect_w, rect_h, frame = 0)
7.         Local x_draw, y_draw
8.         Local cell_ix, cell_ix_start, cell_ix_end
9.         Local cell_iy, cell_iy_start, cell_iy_end
10.         Local cell_x, cell_y
11.         Local wDraw_x, wDraw_y, wGet_x, wGet_y, wGet_w, wGet_h
12.         Local img_w, img_h
13.         img_w = ImageWidth(img)
14.         img_h = ImageHeight(img)
15.         x_draw = x - ImageXHandle(img) - rect_x
16.         y_draw = y - ImageYHandle(img) - rect_y
17.         cell_ix_start = Floor(Float(x_draw)/Float(rect_w))
18.         cell_ix_end = Floor(Float(x_draw + img_w)/Float(rect_w)) - cell_ix_start
19.         cell_iy_start = Floor(Float(y_draw)/Float(rect_h))
20.         cell_iy_end = Floor(Float(y_draw + img_h)/Float(rect_h)) - cell_iy_start
21.         x_draw = x_draw - cell_ix_start*rect_w
22.         y_draw = y_draw - cell_iy_start*rect_h
23.         For cell_iy = 0 To cell_iy_end
24.                 cell_y = cell_iy*rect_h
25.                 For cell_ix = 0 To cell_ix_end
26.                         cell_x = cell_ix*rect_w
27.                         wGet_x = cell_x - x_draw
28.                         wGet_y = cell_y - y_draw
29.                         wGet_w = rect_w
30.                         wGet_h = rect_h
31.                         If (wGet_x+rect_w > img_w) Then wGet_w = img_w-wGet_x
32.                         If (wGet_y+rect_h > img_h) Then wGet_h = img_h-wGet_y
33.                         If (x_draw > cell_x) Then wGet_w = rect_w - x_draw
34.                         If (y_draw > cell_y) Then wGet_h = rect_h - y_draw
35.                         If (wGet_x < 0) Then wGet_x = 0
36.                         If (wGet_y < 0) Then wGet_y = 0
37.                         wDraw_x = wGet_x - cell_x + x_draw
38.                         wDraw_y = wGet_y - cell_y + y_draw
39.                         If (wDraw_x < 0) Then wDraw_x = 0
40.                         If (wDraw_y < 0) Then wDraw_y = 0
41.                         wDraw_x = rect_x + wDraw_x + ImageXHandle(img)
42.                         wDraw_y = rect_y + wDraw_y + ImageYHandle(img)
43.                         DrawImageRect img, wDraw_x, wDraw_y, wGet_x, wGet_y, wGet_w, wGet_h, frame
44.                 Next
45.         Next
46. End Function
47.
48. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49.
50. ; TEST
51.
52. Global GW = 640
53. Global GH = 480
54.
55. Graphics GW, GH, 16, 2
56. SetBuffer BackBuffer()
57.
58. ;HidePointer
59.
60. Font = LoadFont("Courier", 16, True, False, False)
61. SetFont Font
62.
63. w = 128
64. h = 128
65. img = CreateImage(w, h)
66. SetBuffer ImageBuffer(img)
67. Color 255, 0, 0
68. Oval 0, 0, w, h, False
69. Color 0, 255, 0
70. Oval 0, h*0.25, w, h*0.5, False
71. Color 0, 0, 255
72. Oval w*0.25, 0, w*0.5, h, False
73. SetBuffer BackBuffer()
74. MidHandle img
75.
76. rect_w# = 200
77. rect_h# = 200
78. rect_y# = GH*0.5-rect_h*0.5
79. rect_x# = GW*0.5+rect_w*0.5
80.
81. ClsColor 255, 255, 255
82.
83. While Not KeyHit(1)
84.         Cls
85.
86.         rect_h = rect_h+2*(KeyDown(200) - KeyDown(208))
87.         rect_w = rect_w+2*(KeyDown(203) - KeyDown(205))
88.         rect_y = GH*0.5-rect_h*0.5
89.         rect_x = GW*0.5-rect_w*0.5
90.
91.         OverlayImage(img, MouseX(), MouseY(), rect_x, rect_y, rect_w, rect_h)
92.
93.         Color 0, 0, 0
94.         Rect rect_x, rect_y, rect_w, rect_h, False
95.
96.         ; Output results
97.         Color 0, 0, 0
98.         txt_x = 0
99.         txt_y = 0
100.         Text txt_x, txt_y, "Press arrow keys to scale viewport."
101.         txt_y = txt_y + FontHeight()
102.         Text txt_x, txt_y, "Width = "+rect_w
103.         txt_y = txt_y + FontHeight()
104.         Text txt_x, txt_y, "Height = "+rect_h
105.         txt_y = txt_y + FontHeight()
106.
107.         Flip
108. Wend
109.
110. FreeFont Font
111.
112. EndGraphics
113.
114. End