1
0
Fork 0

auts: use Texture class

This commit is contained in:
coaljoe 2020-08-21 10:32:46 +03:00
parent 830f305249
commit 61b22a8719
5 changed files with 84 additions and 9 deletions

View File

@ -1,6 +1,7 @@
module main
pub fn pp(v int) {
//pub fn pp(v any) {
println(v)
println("panic: pp")
@ -18,3 +19,10 @@ pub fn pp(v int) {
//panic("pp")
exit(v)
}
/*
pub fn pp(s string) {
println(s)
pp(2)
}
*/

View File

@ -46,8 +46,8 @@ fn (mut d Drawer) init(w, h int, renderer voidptr) {
d.font = C.TTF_OpenFont(d.font_name.str, d.font_size)
}
// Load image as tex
fn (mut d Drawer) load_image_tex(path string) voidptr {
// Load image as sdl tex
fn (mut d Drawer) load_image_sdl_tex(path string) voidptr {
sdl_img := img.load(path)
mut tex := voidptr(0)
@ -62,7 +62,7 @@ fn (mut d Drawer) draw_image() {
}
fn (mut d Drawer) draw_tex(tex voidptr, px, py int) {
fn (mut d Drawer) draw_sdl_tex(tex voidptr, x, y int) {
if isnil(tex) {
println("tex is nil")
pp(2)
@ -72,12 +72,16 @@ fn (mut d Drawer) draw_tex(tex voidptr, px, py int) {
texh := 0
C.SDL_QueryTexture(tex, 0, 0, &texw, &texh)
//dstrect := vsdl2.Rect { (d.w / 2) - (texw / 2), 20, texw, texh }
dstrect := vsdl2.Rect { px, py, texw, texh }
dstrect := vsdl2.Rect { x, y, texw, texh }
// Currently we can't seem to use vsdl2.render_copy when we need to pass a nil pointer (eg: srcrect to be NULL)
//vsdl2.render_copy(g.sdl.renderer, tv_logo, 0, &dstrect)
C.SDL_RenderCopy(d.renderer, tex, voidptr(0), voidptr(&dstrect))
}
fn (mut d Drawer) draw_tex(tex &Texture, x, y int) {
d.draw_sdl_tex(tex.tex, x, y)
}
fn (mut d Drawer) draw_text(text string, x, y int, c vsdl2.Color) {
//println("drawer draw_text() text: $text, x: $x, y: $y, c: $c")
@ -94,6 +98,20 @@ fn (mut d Drawer) draw_text(text string, x, y int, c vsdl2.Color) {
vsdl2.free_surface(tsurf)
}
fn (mut d Drawer) get_sdl_tex_size(tex voidptr) (int, int) {
if isnil(tex) {
println("tex is nil")
pp(2)
//return
}
texw := 0
texh := 0
C.SDL_QueryTexture(tex, 0, 0, &texw, &texh)
return texw, texh
}
fn (mut d Drawer) clear() {
C.SDL_RenderClear(d.renderer)
}

View File

@ -5,7 +5,7 @@ import os
struct FieldView {
mut:
m &Field
tiles map[string]voidptr
tiles map[string]&Texture
}
fn new_fieldview(m &Field) &FieldView {
@ -37,7 +37,9 @@ fn (mut v FieldView) load() {
}
println("name: $name")
tex := ctx.drawer.load_image_tex(f)
//tex := ctx.drawer.load_image_tex(f)
mut tex := new_texture()
tex.load(f)
v.tiles[name] = tex
@ -64,7 +66,7 @@ fn (mut v FieldView) draw() {
println("name: $name")
tex := v.tiles[name]
mut tex := v.tiles[name]
if isnil(tex) {
println("tex is nil")
@ -83,7 +85,8 @@ fn (mut v FieldView) draw() {
mut py := y * c_tile_h / 2
//v.m.ctx.drawer.draw_tex(px, py, tex)
ctx.drawer.draw_tex(tex, px, py)
//ctx.drawer.draw_tex(tex, px, py)
tex.draw_pos(px, py)
}
}
}

View File

@ -34,7 +34,7 @@ fn main() {
//d := new_drawer(mut ctx)
mut d := ctx.drawer
tex := d.load_image_tex("res/images/heightmap.png")
tex := d.load_image_sdl_tex("res/images/heightmap.png")
mut h := new_hud()

46
isotest/texture.v Normal file
View File

@ -0,0 +1,46 @@
module main
struct Texture {
mut:
w int
h int
// Sdl
tex voidptr
}
fn new_texture() &Texture {
println("new_texture()")
t := &Texture{
tex: voidptr(0),
}
return t
}
fn (mut t Texture) load(path string) {
println("texture load() path: $path")
t.tex = ctx.drawer.load_image_sdl_tex(path)
if isnil(t.tex) {
println("tex is nil")
pp(2)
}
t.w, t.h = ctx.drawer.get_sdl_tex_size(t.tex)
if t.w < 1 || t.h < 1 {
println("bad tex size")
pp(2)
}
}
// Draw at position
fn (mut t Texture) draw_pos(x, y int) {
//println("texture draw_pos() x: $x, y: $y")
//ctx.drawer.draw_sdl_tex(t.tex, x, y)
ctx.drawer.draw_tex(t, x, y)
}