From e4f0d0c988c5007103b0b0ff414693f0e3298bf1 Mon Sep 17 00:00:00 2001 From: coaljoe Date: Wed, 19 Aug 2020 23:53:00 +0300 Subject: [PATCH] auts: can draw some tex with vsdl --- isotest/.gitignore | 1 + isotest/app.v | 88 ++++++++++++++++++++++++++++++++ isotest/context.v | 18 +++++++ isotest/debug.v | 20 ++++++++ isotest/draw.v | 70 +++++++++++++++++++++++++ isotest/main.v | 29 +++++++++++ isotest/res/images/heightmap.png | 3 ++ 7 files changed, 229 insertions(+) create mode 100644 isotest/.gitignore create mode 100644 isotest/app.v create mode 100644 isotest/context.v create mode 100644 isotest/debug.v create mode 100644 isotest/draw.v create mode 100644 isotest/res/images/heightmap.png diff --git a/isotest/.gitignore b/isotest/.gitignore new file mode 100644 index 0000000..86a0a0a --- /dev/null +++ b/isotest/.gitignore @@ -0,0 +1 @@ +isotest \ No newline at end of file diff --git a/isotest/app.v b/isotest/app.v new file mode 100644 index 0000000..a9e633c --- /dev/null +++ b/isotest/app.v @@ -0,0 +1,88 @@ +module main + +import nsauzede.vsdl2 +import nsauzede.vsdl2.image as img +import time +type Atexit_func_t fn () +fn C.atexit(Atexit_func_t) + +struct App { +mut: + w int + h int + dt f32 + + ctx &Context + + // Sdl (fixme?) + window voidptr + screen &vsdl2.Surface + renderer voidptr +} + +fn new_app(mut ctx Context) &App { + println("new_app()") + + a := &App{ + dt: 0.0, + ctx: ctx, + } + + // Update ctx + ctx.app = a + + return a +} + +fn (mut a App) init(w, h int, title string) { + println("app init() w: $w, h: $h") + + // Init sdl + println("init sdl...") + + C.SDL_Init(C.SDL_INIT_VIDEO) + C.atexit(C.SDL_Quit) + C.TTF_Init() + C.atexit(C.TTF_Quit) + bpp := 32 + vsdl2.create_window_and_renderer(w, h, 0, &a.window, &a.renderer) + C.SDL_SetWindowTitle(a.window, title.str) + a.w = w + a.h = h + a.screen = vsdl2.create_rgb_surface(0, w, h, bpp, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000) + //a.texture = C.SDL_CreateTexture(sdl.renderer, C.SDL_PIXELFORMAT_ARGB8888, C.SDL_TEXTUREACCESS_STREAMING, w, h) + + flags := C.IMG_INIT_PNG + imgres := img.img_init(flags) + if (imgres & flags) != flags { + println('error initializing image library.') + } + + // Init drawer + println("init drawer...") + + mut d := new_drawer(mut a.ctx) + d.init(w, h, a.renderer) + + println("app init done") +} + +fn (mut a App) step() { + println("app step()") + + a.dt = f32(20.0) + + //a.render() + + a.update(a.dt) +} + +fn (mut a App) flip() { + println("app flip()") + + C.SDL_RenderPresent(a.renderer) +} + +fn (mut a App) update(dt f32) { + +} diff --git a/isotest/context.v b/isotest/context.v new file mode 100644 index 0000000..bd1ac20 --- /dev/null +++ b/isotest/context.v @@ -0,0 +1,18 @@ +module main + +import nsauzede.vsdl2 +import nsauzede.vsdl2.image as img + +struct Context { +pub mut: + app &App + drawer &Drawer +} + +fn new_context() &Context { + println("new_context()") + + c := &Context{} + + return c +} diff --git a/isotest/debug.v b/isotest/debug.v new file mode 100644 index 0000000..f3e8f6e --- /dev/null +++ b/isotest/debug.v @@ -0,0 +1,20 @@ +module main + +pub fn pp(v int) { + + println(v) + println("panic: pp") + println("") + + //println("bt:") + println("Backtrace:") + print_backtrace() + //println("bt2:") + //print_backtrace_skipping_top_frames(3) + //println(v) + + //println("panic $v") + + //panic("pp") + exit(v) +} diff --git a/isotest/draw.v b/isotest/draw.v new file mode 100644 index 0000000..957538f --- /dev/null +++ b/isotest/draw.v @@ -0,0 +1,70 @@ +module main + +import nsauzede.vsdl2 +import nsauzede.vsdl2.image as img + +struct Drawer { +mut: + w int + h int + + ctx &Context + + // Sdl + renderer voidptr +} + +fn new_drawer(mut ctx Context) &Drawer { + println("new_drawer()") + + d := &Drawer{ + ctx: ctx + } + + // Update ctx + ctx.drawer = d + + return d +} + +fn (mut d Drawer) init(w, h int, renderer voidptr) { + // XXX fixme? + //d.renderer = d.ctx.app.renderer + d.w = w + d.h = h + d.renderer = renderer +} + +// Load image as tex +fn (mut d Drawer) load_image_tex(path string) voidptr { + sdl_img := img.load(path) + mut tex := voidptr(0) + + if !isnil(sdl_img) { + tex = vsdl2.create_texture_from_surface(d.renderer, sdl_img) + } + + return tex +} + +fn (mut d Drawer) draw_image() { + +} + +fn (mut d Drawer) draw_tex(tex voidptr, px, py int) { + if isnil(tex) { + return + } + texw := 0 + 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 } + // 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) clear() { + C.SDL_RenderClear(d.renderer) +} diff --git a/isotest/main.v b/isotest/main.v index 1a6d5ad..e5892c6 100644 --- a/isotest/main.v +++ b/isotest/main.v @@ -1,5 +1,7 @@ module main +import time + fn main() { println("main()") @@ -11,5 +13,32 @@ fn main() { println("z 0 0: ${f.cells[0][0].z}") println("z 1 1: ${f.cells[1][1].z}") + + mut ctx := new_context() + + + mut app := new_app(mut ctx) + + app.init(800, 600, "auts") + + //d := new_drawer(mut ctx) + mut d := ctx.drawer + + tex := d.load_image_tex("res/images/heightmap.png") + + for { + app.step() + + d.clear() + + d.draw_tex(tex, 0, 0) + + app.flip() + + //time.sleep_ms(20) + time.sleep_ms(200) + //pp(2) + } + println("done") } diff --git a/isotest/res/images/heightmap.png b/isotest/res/images/heightmap.png new file mode 100644 index 0000000..aab6f67 --- /dev/null +++ b/isotest/res/images/heightmap.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01d025bdd71d8986f2254752264d2259ab42f2f597cfb2d179dcda4218c1d397 +size 10920