1
0
Fork 0

auts: can draw some tex with vsdl

This commit is contained in:
coaljoe 2020-08-19 23:53:00 +03:00
parent b733d62b61
commit e4f0d0c988
7 changed files with 229 additions and 0 deletions

1
isotest/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
isotest

88
isotest/app.v Normal file
View File

@ -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) {
}

18
isotest/context.v Normal file
View File

@ -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
}

20
isotest/debug.v Normal file
View File

@ -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)
}

70
isotest/draw.v Normal file
View File

@ -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)
}

View File

@ -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")
}

BIN
isotest/res/images/heightmap.png (Stored with Git LFS) Normal file

Binary file not shown.