Skip to content

Commit 36f6e78

Browse files
committed
add interpolation between mouse movement points for smooth drawing
1 parent 03f1de6 commit 36f6e78

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

Utils.jl

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Utils
2+
3+
4+
5+
export applyBetween
6+
7+
# for now assume p1 and p2 are 2d [x,y]
8+
9+
10+
function applyBetween(p1::Vector, p2::Vector, f)
11+
if (p1 == p2)
12+
f(p1)
13+
return
14+
end
15+
x1, y1 = p1
16+
x2, y2 = p2
17+
18+
dx = x2 - x1
19+
dy = y2 - y1
20+
dxIsLarger = abs(dx) > abs(dy)
21+
22+
xModifier = dx < 0 ? -1 : 1
23+
yModifier = dy < 0 ? -1 : 1
24+
25+
longerSideLength = dxIsLarger ? abs(dx) : abs(dy)
26+
shorterSideLength = dxIsLarger ? abs(dy) : abs(dx)
27+
slope = shorterSideLength == 0 || longerSideLength == 0 ? 0 : shorterSideLength / longerSideLength
28+
29+
for i in 0:longerSideLength
30+
if (dxIsLarger)
31+
f([x1 + i * xModifier, y1 + round(Int, i * slope) * yModifier])
32+
else
33+
f([x1 + round(Int, i * slope) * xModifier, y1 + i * yModifier])
34+
end
35+
end
36+
end
37+
38+
39+
# function getVoxelColor(type)
40+
# type == Voxel.AIR ? Voxel.airColor :
41+
# type == Voxel.STONE ? Voxel.stoneColor :
42+
# type == Voxel.SAND ? Voxel.sandColor :
43+
# _ => sfColor_fromRGBA(0, 0, 0, 255)
44+
45+
# end
46+
47+
end

constants.jl

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module
2+
3+
end

main.jl

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using CSFML
22
using CSFML.LibCSFML
33

4+
include("Utils.jl")
45
include("Voxel.jl")
56
using .Voxel: newVoxel
7+
using .Utils: applyBetween
68

79
# enums of particle types, sand, stone, water, etc
810
const AIR = 0
@@ -14,6 +16,7 @@ selectedBlock = SAND
1416
windowWidth = 640
1517
windowHeight = 480
1618
leftPressed = false
19+
prevPosition = nothing;
1720

1821
# TODO figure out how I want to handle resizing and map size in general
1922

@@ -68,24 +71,38 @@ function handleResize()
6871
return true
6972
end
7073

71-
function updateClickedPixel()
74+
function updateClickedPixel(prevPosition)
7275
mousePos = sfMouse_getPositionRenderWindow(window)
7376
if mousePos.x < 0 || mousePos.x >= windowWidth || mousePos.y < 0 || mousePos.y >= windowHeight
7477
return
7578
end
7679

77-
# set boards voxel to new color
78-
board[mousePos.y+1][mousePos.x+1] = newVoxel(selectedBlock)
79-
if (selectedBlock == SAND)
80-
moveableMap[[mousePos.y + 1, mousePos.x + 1]] = SAND
80+
if prevPosition === nothing
81+
global prevPosition = mousePos
82+
else
83+
84+
end
85+
86+
function setPixel(point)
87+
x = point[1]
88+
y = point[2]
89+
board[y+1][x+1] = newVoxel(selectedBlock)
90+
if (selectedBlock == SAND)
91+
moveableMap[[y + 1, x + 1]] = SAND
92+
end
8193
end
94+
95+
applyBetween([prevPosition.x, prevPosition.y], [mousePos.x, mousePos.y], setPixel)
96+
97+
global prevPosition = mousePos
8298
end
8399

84100
function physicsTick(board)
85101
# iterate over moveableMap and move particles down if air below them
86102
for (i, j) in keys(moveableMap)
87103
if i < windowHeight
88104
down1 = board[i+1][j]
105+
# TODO check against swappable blocks instead of air
89106
if down1.type == AIR
90107
board[i+1][j] = board[i][j]
91108
board[i][j] = down1
@@ -110,16 +127,17 @@ function mouseHandler(event::sfEvent)
110127
isLeft = sfMouseLeft == event.mouseButton.button
111128
# rightPress = sfMouseRight == event.mouseButton.button
112129
if event.type == sfEvtMouseButtonPressed && isLeft
113-
updateClickedPixel()
130+
updateClickedPixel(prevPosition)
114131
global leftPressed = true
115132
end
116133

117134
if event.type == sfEvtMouseMoved && leftPressed
118-
updateClickedPixel()
135+
updateClickedPixel(prevPosition)
119136
end
120137

121138
if event.type == sfEvtMouseButtonReleased && isLeft
122139
global leftPressed = false
140+
global prevPosition = nothing
123141
end
124142
end
125143

0 commit comments

Comments
 (0)