Permalink
Browse files

Add dead, ui, pass

  • Loading branch information...
Amos Wenger
Amos Wenger committed Aug 22, 2012
1 parent 3375d1d commit c7daf4f39c246aeedadbb88d74258e358f22c09e
Showing with 271 additions and 0 deletions.
  1. +22 −0 source/ldkit/Dead.ooc
  2. +39 −0 source/ldkit/Engine.ooc
  3. +59 −0 source/ldkit/Pass.ooc
  4. +151 −0 source/ldkit/UI.ooc
View
@@ -0,0 +1,22 @@
+
+use deadlogger
+import deadlogger/[Log, Handler, Formatter, Filter, Logger]
+
+Dead: class {
+
+ initialized := static false
+ console: static StdoutHandler
+
+ logger: static func (name: String) -> Logger {
+ if (!initialized) {
+ console = StdoutHandler new()
+ console setFormatter(ColoredFormatter new(NiceFormatter new()))
+ Log root attachHandler(console)
+ initialized = true
+ }
+
+ Log getLogger("main")
+ }
+
+}
+
View
@@ -0,0 +1,39 @@
+
+import UI, Timing
+
+import structs/[ArrayList]
+
+use zombieconfig
+import zombieconfig
+
+Engine: class {
+
+ ui: UI
+
+ FPS := 30.0 // let's target 30FPS
+
+ init: func(config: ZombieConfig) {
+ ui = UI new(this, config)
+ }
+
+ run: func {
+ ticks: Int
+ delta := 1000.0 / 30.0 // try 30FPS
+
+ // main loop
+ while (true) {
+ ticks = LTime getTicks()
+
+ ui update()
+
+ // teleport ourselves in the future when the next frame is due
+ roadToFuture := ticks + delta - LTime getTicks()
+ if(roadToFuture > 0) {
+ LTime delay(roadToFuture)
+ }
+ }
+ }
+
+}
+
+
View
@@ -0,0 +1,59 @@
+
+// libs deps
+import Sprites, Display, UI
+import deadlogger/Log
+import structs/[ArrayList]
+
+Pass: class {
+
+ ui: UI
+ name: String // for debug
+
+ parent: This // can be null (for root pass)
+
+ passes := ArrayList<This> new()
+ sprites := ArrayList<Sprite> new()
+
+ logger := static Log getLogger(This name)
+
+ /*
+ * Constructor
+ */
+ init: func (=ui, =name) {
+ // not much here
+ }
+
+ reset: func {
+ sprites each(|s| s free())
+ sprites clear()
+ passes clear()
+ }
+
+ addPass: func (pass: This) {
+ passes add(pass)
+ pass parent = this
+ }
+
+ addSprite: func (sprite: Sprite) {
+ sprites add(sprite)
+ }
+
+ removeSprite: func (sprite: Sprite) {
+ sprites remove(sprite)
+ }
+
+ draw: func {
+ sprites each(|s| s draw(ui display))
+ passes each(|p| p draw())
+ }
+
+ toString: func -> String { if (parent) {
+ "%s / %s" format(parent toString(), name)
+ } else {
+ name
+ }
+ }
+
+}
+
+
View
@@ -0,0 +1,151 @@
+
+use gobject, cairo, sdl, deadlogger, ldkit
+
+// game deps
+import Display, Input, Math, Sprites, Sound, Engine
+import Pass
+
+// libs deps
+import deadlogger/Log
+import zombieconfig
+import os/Time
+
+UI: class {
+
+ // note to viewers: 'This' refers to the current class in ooc.
+ logger := static Log getLogger(This name)
+
+ // something we can draw on using Cairo
+ display: Display
+
+ // something we can read events from
+ input: Input
+
+ // something we can make noise with
+ boombox: Boombox
+
+ // something we can control level loading with
+ engine: Engine
+
+ /*
+ * Constructors
+ */
+ init: func (=engine, config: ZombieConfig) {
+ // note: all config entries are String, so we just have to cheat a bit ;)
+ width := config["screenWidth"] toInt()
+ height := config["screenHeight"] toInt()
+ fullScreen := (config["fullScreen"] == "true")
+ title := config["title"]
+
+ display = Display new(width, height, fullScreen, title)
+ display hideCursor()
+
+ input = Input new()
+
+ initSound()
+ initPasses()
+ initEvents()
+ }
+
+ bgMusic: Sample
+
+ initSound: func {
+ logger info("Initializing sound system")
+ boombox = Boombox new()
+
+ // bgMusic = boombox load("assets/music/quiet-dignity.ogg")
+ // boombox loop(bgMusic)
+
+ // criekSound = boombox load("assets/sounds/criek.ogg")
+ }
+
+ // different UI passes
+ rootPass := Pass new(this, "root")
+
+ // name passes for later profiling
+ bgPass := Pass new(this, "bg") // clear
+ levelPass := Pass new(this, "level") // level terrain etc.
+ hudPass := Pass new(this, "hud") // human interface (windows/dialogs etc.)
+
+ // status pass
+ statusPass := Pass new(this, "status")
+
+ // mouse pass (cursor)
+ mousePass := Pass new(this, "mouse")
+ cursor: GroupSprite
+
+ // flashMessages: FlashMessages
+
+ initPasses: func {
+ // flashMessages = FlashMessages new(this)
+
+ // temp code, no real art :'(
+ // bgPos := vec2(display getWidth() - 1920, display getHeight() - 1080)
+ // bgPass addSprite(ImageSprite new(bgPos, "assets/png/background-placeholder.png"))
+
+ // offset to make the hand correspond with the actual mouse
+ cursorImage := ImageSprite new(vec2(-12, -10), "assets/png/cursor.png")
+ cursor = GroupSprite new()
+ cursor add(cursorImage)
+
+ mousePass addSprite(cursor)
+
+ input onMouseMove(||
+ cursor pos set!(input mousepos)
+ )
+
+ reset()
+ }
+
+ reset: func {
+ // flashMessages reset()
+
+ rootPass reset()
+
+ // nothing to reset
+ rootPass addPass(bgPass)
+
+ // everything will be re-created when loading the level
+ levelPass reset()
+ rootPass addPass(levelPass)
+
+ // close all windows
+ hudPass reset()
+ rootPass addPass(hudPass)
+
+ // status is just a few text fields, no need to recreate
+ rootPass addPass(statusPass)
+
+ // no need to recreate either
+ rootPass addPass(mousePass)
+ }
+
+ flash: func (msg: String) {
+ // flashMessages push(msg)
+ }
+
+ update: func {
+ boombox update()
+ // flashMessages update()
+
+ display clear()
+ rootPass draw()
+ display blit()
+
+ input _poll()
+ }
+
+ initEvents: func {
+ // it's a better practice to turn on debug locally
+ //input debug = true
+
+ input onExit(||
+ // just exit bluntly. Let's rely on the OS to free all the resources :D
+ exit(0)
+ )
+ }
+
+}
+
+
+

0 comments on commit c7daf4f

Please sign in to comment.