126 lines
2.1 KiB
V
126 lines
2.1 KiB
V
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() bool {
|
|
println("app step()")
|
|
|
|
a.dt = f32(20.0)
|
|
|
|
mut should_close := false
|
|
//for {
|
|
//evt := SDL_Event{}
|
|
evt := vsdl2.Event{}
|
|
for 0 < vsdl2.poll_event(&evt) {
|
|
match int(evt.@type) {
|
|
C.SDL_QUIT { should_close = true }
|
|
|
|
C.SDL_KEYDOWN {
|
|
key := evt.key.keysym.sym
|
|
if key == C.SDLK_ESCAPE || key == C.SDLK_q {
|
|
should_close = true
|
|
break
|
|
}
|
|
}
|
|
|
|
else {}
|
|
}
|
|
|
|
}
|
|
//}
|
|
|
|
if should_close {
|
|
//pp(2)
|
|
return false
|
|
}
|
|
|
|
//a.render()
|
|
|
|
a.update(a.dt)
|
|
|
|
return true
|
|
}
|
|
|
|
fn (mut a App) flip() {
|
|
println("app flip()")
|
|
|
|
C.SDL_RenderPresent(a.renderer)
|
|
}
|
|
|
|
fn (mut a App) quit() {
|
|
println("app quit()")
|
|
|
|
//C.SDL_DestroyRenderer(a.renderer)
|
|
C.SDL_DestroyWindow(a.window)
|
|
C.SDL_Quit()
|
|
}
|
|
|
|
fn (mut a App) update(dt f32) {
|
|
|
|
}
|