Diddy Tutorials - Screen Based Framework

Monkey Archive Forums/Monkey Tutorials/Diddy Tutorials - Screen Based Framework

therevills(Posted 2014) [#1]
First thing to do is to grab the latest version of Diddy either using Mercurial (Hg) or the Zip from Google Code:
https://code.google.com/p/diddy/
http://diddy.googlecode.com/archive/default.zip

Once extracted or downloaded you should have a folder structure like this:
diddy\
  data\
  examples\
  src\
  utils\


Go into the src folder and copy the diddy folder into your MonkeyX/modules folder or add diddy/src to the Monkey MODPATH.
c:\MonkeyX\modules\


Now Diddy is ready to be used :)

Diddy's framework already extends Mojo's App so all you have to do is extend DiddyApp:
Strict 'enable strict mode - recommended

Import diddy 'imports the main diddy file

'MonkeyX entry point
Function Main:Int()
	New MyGame() ' instantiate MyGame
	Return True
End

' MyGame class
Class MyGame Extends DiddyApp
	' DiddyApp will call Create on instantiation
	Method Create:Void()
	End
End


So now let's create a couple of screens like a title screen and a main game screen. There are a few ways to do this, mainly either using Globals or a Singleton pattern.
Using Globals:
Strict

Import diddy

' declare the screens
Global titleScreen:TitleScreen
Global gameScreen:GameScreen

Function Main:Int()
	New MyGame()
	Return True
End

Class MyGame Extends DiddyApp
	Method Create:Void()
		'instantiate the screens
		titleScreen = New TitleScreen
		gameScreen = New GameScreen
		'start the titlescreen
		Start(titleScreen)
	End
End

' all screens must extend Screen
Class TitleScreen Extends Screen
	Method New()
		name = "Title"
	End
	
	' use Start to set varibles etc
	Method Start:Void()
	End

	' the Render method draws onto the screen
	Method Render:Void()
		Cls
		DrawText "TITLE SCREEN!", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0.5
		DrawText "Space to Play!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 20, 0.5, 0.5
		DrawText "Escape to Quit!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 40, 0.5, 0.5
	End

	' the Update method wraps the OnUpdate of Mojos App
	Method Update:Void()
		If KeyHit(KEY_ESCAPE)
			'diddyGame.exitScreen is automatically created for you
			FadeToScreen(diddyGame.exitScreen)
		End
		If KeyHit(KEY_SPACE)
			' Fade to the game screen
			FadeToScreen(gameScreen)
		End
	End
End

Class GameScreen Extends Screen
	Method New()
		name = "GameScreen"
	End
	
	Method Start:Void()
	End

	Method Render:Void()
		Cls
		DrawText "GAME SCREEN!", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0.5
		DrawText "Escape to go back to the Title!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 40, 0.5, 0.5
	End

	Method Update:Void()
		If KeyHit(KEY_ESCAPE)
			FadeToScreen(titleScreen)
		End
	End
End


Using a Singleton like pattern:
Strict

Import diddy

Function Main:Int()
	New MyGame()
	Return True
End

Class MyGame Extends DiddyApp
	Method Create:Void()
		Start(TitleScreen.GetInstance())
	End
End

Class TitleScreen Extends Screen
	Global instance:TitleScreen = Null
	
	Method New()
		name = "Title"
	End
	
	Function GetInstance:TitleScreen()
		If instance = Null
			instance = New TitleScreen()
		End
		Return instance
	End
	
	Method Start:Void()
	End

	Method Render:Void()
		Cls
		DrawText "TITLE SCREEN!", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0.5
		DrawText "Space to Play!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 20, 0.5, 0.5
		DrawText "Escape to Quit!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 40, 0.5, 0.5
	End

	Method Update:Void()
		If KeyHit(KEY_ESCAPE)
			FadeToScreen(diddyGame.exitScreen)
		End
		If KeyHit(KEY_SPACE)
			FadeToScreen(GameScreen.GetInstance())
		End
	End
End

Class GameScreen Extends Screen
	Global instance:GameScreen = Null
	
	Method New()
		name = "GameScreen"
	End
	
	Function GetInstance:GameScreen()
		If instance = Null
			instance = New GameScreen()
		End
		Return instance
	End
	
	Method Start:Void()
	End

	Method Render:Void()
		Cls
		DrawText "GAME SCREEN!", SCREEN_WIDTH2, SCREEN_HEIGHT2, 0.5, 0.5
		DrawText "Escape to go back to the Title!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 40, 0.5, 0.5
	End

	Method Update:Void()
		If KeyHit(KEY_ESCAPE)
			FadeToScreen(TitleScreen.GetInstance())
		End
	End
End


The Singleton way is a more correct Object Oriented way.

Done! We have now created two screens which we can go back forward between :)


Sammy(Posted 2014) [#2]
A clear and useful example, thank you! :)