1
0
Fork 0

auts: draw cursor test

This commit is contained in:
coaljoe 2020-08-22 14:39:53 +03:00
parent 98b1bdaa0f
commit a82dc6b068
7 changed files with 254 additions and 64 deletions

View File

@ -3,4 +3,7 @@ module main
const (
c_tile_w = 32
c_tile_h = 16
c_cell_w = 32
c_cell_h = 16
)

View File

@ -36,6 +36,14 @@ fn (mut f Field) generate(w, h int) {
f.generated = true
}
fn (f &Field) width_px() int {
return f.w * c_cell_w
}
fn (f &Field) height_px() int {
return f.h * c_cell_h
}
fn (mut f Field) spawn() {
f.view = new_fieldview(f)
f.view.spawn()

View File

@ -49,6 +49,46 @@ fn (mut v FieldView) load() {
//pp(2)
}
// Draw tile at position
[inline]
fn (mut v FieldView) draw_tile(tile_id int, px, py int) {
name := "tile_${tile_id}"
//println("name: $name")
mut tex := v.tiles[name]
if isnil(tex) {
println("tex is nil")
pp(2)
}
sx, sy := world_to_screen_pos(px, py)
//println("sx: $sx")
//println("sy: $sx")
//tex.draw_pos(sx, sy)
tex.draw_pos(px, py)
}
// Draw tile at cell position
[inline]
fn (mut v FieldView) draw_tile_cell_pos(tile_id int, cx, cy int) {
x := cx
y := cy
mut shx := 0
if y % 2 != 0 {
shx = c_tile_w / 2
}
mut px := x * c_tile_w + shx
mut py := y * c_tile_h / 2
v.draw_tile(tile_id, px, py)
}
fn (mut v FieldView) spawn() {
println("fiedview spawn()")
@ -61,39 +101,17 @@ fn (mut v FieldView) draw() {
for y in 0 .. v.m.h {
for x in 0 .. v.m.w {
c := v.m.cells[x][y]
name := "tile_${c.tile_id}"
println("name: $name")
mut tex := v.tiles[name]
if isnil(tex) {
println("tex is nil")
pp(2)
}
//mut px := c_tile_w * x
//mut py := c_tile_h * y
mut shx := 0
if y % 2 != 0 {
shx = c_tile_w / 2
}
mut px := shx + (x * c_tile_w)
mut py := y * c_tile_h / 2
sx, sy := world_to_screen_pos(px, py)
println("sx: $sx")
println("sy: $sx")
v.draw_tile_cell_pos(c.tile_id, x, y)
//pp(2)
//v.m.ctx.drawer.draw_tex(px, py, tex)
//ctx.drawer.draw_tex(tex, px, py)
//tex.draw_pos(px, py)
tex.draw_pos(sx, sy)
//tex.draw_pos(sx, sy)
}
}
}

View File

@ -1,5 +1,6 @@
module main
import math
// World pos to screen pos
fn world_to_screen_pos(x, y int) (int, int) {
@ -9,3 +10,167 @@ fn world_to_screen_pos(x, y int) (int, int) {
return sx, sy
}
fn cell_to_screen_pos(cx, cy int) (int, int) {
pan_x := 0
pan_y := 0
tile_size_z := 0
cz := 0
sx := pan_x + (c_tile_w * cx) + ((cy&1) * (c_tile_w / 2))
sy := pan_y + (c_tile_h * cy / 2) - tile_size_z * cz
return sx, sy
}
fn cell_to_screen_pos2(cx, cy int) (int, int) {
scale_ := f32(1.0)
width_ := f32(c_cell_w)
height_ := f32(c_cell_h)
//width_ := f32(game.field.w * c_cell_w)
//height_ := f32(game.field.h * c_cell_h)
sw := scale_ * width_/2.0
sh := scale_ * height_/2.0
rx, ry := (cx - cy + f32(int(cy)&1)/2.0) * sw,
(cx + cy + f32(int(cy)&1)/2.0) * sh
rxi, ryi := int(rx), int(ry)
return rxi, ryi
}
fn screen_to_cell_pos(sx, sy int) (int, int) {
pan_x := 0
pan_y := 0
tile_size_z := 0
mut cy := (2 * (sy - pan_y)) / c_tile_h
mut cx := (sx - pan_x - ((cy&1) * (c_tile_w / 2))) / c_tile_w
mut cz := 0
// Correction?
//mut xx, mut yy := cell_to_screen_pos(cx, cy, cz)
mut xx, mut yy := cell_to_screen_pos(cx, cy)
xx = sx - xx
mx0 := cx
yy = sy - yy
my0 := cy
if xx <= (c_tile_w / 2) {
if yy > xx * c_tile_h/c_tile_w {
cy++
if int(cy&1) != 0 {
cx--
}
}
} else {
if yy > (c_tile_w - xx) * c_tile_h/c_tile_w {
cy++
if int(cy&1) == 0 {
cx++
}
}
}
return cx, cy
}
// XXX bad
fn screen_to_cell_pos2(sx, sy int) (int, int) {
px := f32(sx)
py := f32(sy)
//width := game.field.w * c_cell_w
//height := game.field.h * c_cell_h
//width := game.field.w
//height := game.field.h
width := c_cell_w
height := c_cell_h
mx := int(math.floor(px / width))
my := int(math.floor(py / height) * 2)
return mx, my
}
// XXX
// x is accurate +/-
// y is not very
fn screen_to_cell_pos3(sx, sy int) (int, int) {
//y := f32(game.field.h) - 1.0 - f32(sy) * 2.0 / f32(c_cell_h)
//sy_ := ctx.vars.res_y - sy
//sy_ := game.field.height_px() - (f32(c_cell_h) * 1.5) - sy
//sy_ := sy
//sy_ := 1.0
//sy_ := f32(0.0)
//println("sy_: $sy_")
//y := f32(game.field.h) - f32(sy_) * 2.0 / f32(c_cell_h)
//y := f32(game.field.h) - 1.0 - f32(sy_) * 2.0 / f32(c_cell_h)
//mut y := f32(sy) / f32(c_cell_h)
//mut y := 0.0 - 1.0 - f32(sy_) * 2.0 / f32(c_cell_h)
mut y := f32(sy) * 2.0 / f32(c_cell_h) + 1.0
//mut y := f32(sy) * 2.0 / f32(c_cell_h)
mut x := f32(0.0)
y = (2 * (sy)) / c_tile_h
if int(y) % 2 == 0 {
x = f32(sx) / f32(c_cell_w)
} else {
//x = f32(sx) / f32(c_cell_w)
//x = (f32(sx) / f32(c_cell_w)) / 2.0
//x = (f32(sx) + f32(c_cell_w / 2)) / f32(c_cell_w)
x = ((f32(sx) / f32(c_cell_w))) - f32(0.5) // 0.5 is float rounding correction to the lower number
}
yi, xi := int(y), int(x)
return xi, yi
}
// XXX inaccurate
fn mouse_to_cell_pos(mx, my int) (int, int) {
// https://gamedev.stackexchange.com/questions/45103/staggered-isometric-map-calculate-map-coordinates-for-point-on-screen
px := f32(mx) - (c_tile_w / 2.0)
py := f32(my) - (c_tile_h / 2.0)
x := math.floor((px + (py - (c_tile_h / 2.0)) * 2.0) / c_tile_w)
y := math.floor((py - (px - (c_tile_w / 2.0)) * 0.5) / c_tile_h)
tx := math.floor((x - y) / 2.0) + 1.0 //+ this.camera.x;
ty := y + x + 2.0 //+ this.camera.y;
/*
tx := math.floor((x - y) / 2) + 1 + -(game.viewport.shx / c_tile_w)
ty := y + x + 2 + -(game.viewport.shy / c_tile_h)
*/
// XXX rounding errors?
mut txi := int(tx)
mut tyi := int(ty)
// XXX correct y by shift, fixme?
tyi = tyi - 1
/*
vp_shx_tiles := game.viewport.shx / c_tile_w
vp_shy_tiles := game.viewport.shy / c_tile_h
txi = txi + vp_shx_tiles
tyi = tyi + (vp_shy_tiles * 2)
*/
return txi, tyi
}

View File

@ -25,6 +25,23 @@ fn (mut h Hud) draw() {
s2 := "mcx: $h.mcx, mcy: $h.mcy"
ctx.drawer.draw_text(s2, 20, 100, ctx.drawer.color_white)
// Draw cursor
if h.mcx >= 0 && h.mcy >= 0 {
/*
x := h.mcx * c_tile_w
y := h.mcy * (c_tile_h / 2)
game.field.view.draw_tile(2, x, y)
*/
game.field.view.draw_tile_cell_pos(2, h.mcx, h.mcy)
game.field.view.draw_tile_cell_pos(2, 0, 0)
game.field.view.draw_tile_cell_pos(2, 1, 1)
//game.field.view.draw_tile_cell_pos(2, 2, 1)
}
}
fn (mut h Hud) update(dt f32) {
@ -33,7 +50,10 @@ fn (mut h Hud) update(dt f32) {
h.mx = mx
h.my = my
mcx, mcy := mouse_to_cell_pos(mx, my)
//mcx, mcy := mouse_to_cell_pos(mx, my)
//mcx, mcy := screen_to_cell_pos(mx, my)
//mcx, mcy := screen_to_cell_pos2(mx, my)
mcx, mcy := screen_to_cell_pos3(mx, my)
h.mcx = mcx
h.mcy = mcy

View File

@ -1,38 +1,2 @@
module main
import math
fn mouse_to_cell_pos(mx, my int) (int, int) {
// https://gamedev.stackexchange.com/questions/45103/staggered-isometric-map-calculate-map-coordinates-for-point-on-screen
px := f32(mx) - (c_tile_w / 2)
py := f32(my) - (c_tile_h / 2)
x := math.floor((px + (py - (c_tile_h / 2)) * 2) / c_tile_w)
y := math.floor((py - (px - (c_tile_w / 2)) * 0.5) / c_tile_h)
tx := math.floor((x - y) / 2) + 1 //+ this.camera.x;
ty := y + x + 2 //+ this.camera.y;
/*
tx := math.floor((x - y) / 2) + 1 + -(game.viewport.shx / c_tile_w)
ty := y + x + 2 + -(game.viewport.shy / c_tile_h)
*/
// XXX rounding errors?
mut txi := int(tx)
mut tyi := int(ty)
// XXX correct y by shift, fixme?
tyi = tyi - 1
vp_shx_tiles := game.viewport.shx / c_tile_w
vp_shy_tiles := game.viewport.shy / c_tile_h
txi = txi + vp_shx_tiles
tyi = tyi + (vp_shy_tiles * 2)
return txi, tyi
}

View File

@ -27,8 +27,9 @@ fn main() {
println("z 0 0: ${f.cells[0][0].z}")
println("z 1 1: ${f.cells[1][1].z}")
f.cells[0][2].tile_id = 1
f.cells[0][3].tile_id = 2
f.cells[0][1].tile_id = 1
//f.cells[0][2].tile_id = 1
//f.cells[0][3].tile_id = 2
//f.cells[1][1].tile_id = 1
//f.cells[2][2].tile_id = 2
@ -43,6 +44,10 @@ fn main() {
tex := d.load_image_sdl_tex("res/images/heightmap.png")
ctx.vars.scroll_speed = 0
for app.step() {
//app.step()
//h.update(1)
@ -53,12 +58,19 @@ fn main() {
d.clear()
//f.view.draw()
//d.draw_tex(tex, 0, 0)
//d.draw_text("test", 0, 0, d.color_white)
game.draw()
//d.draw_sdl_tex(tex, 0, 0)
//px, py := cell_to_screen_pos(1, 1)
px, py := cell_to_screen_pos2(1, 1)
//d.draw_sdl_tex(tex, px, py)
app.flip()
//time.sleep_ms(20)