Console Module v.0.53

Miscellaneous Forums/Blitz Showcase/Console Module v.0.53

To learn a bit about OOP and the usage in BMAX I have created
a little Module which creates and manage a Console.

Currently available commands(0.53):
Type Console:
Create(width,height,alpha,maxChars)
update()
SetStatus(st:int) ; 1 = Visible 0 = Hidden
Set_alpha(_al:Float) ; 0 - 1 Sets a new Alpha value
Return_Alpha():Float ; returns the current Alpha value
Return Status():Int ; returns the current Status
Return_last():String ; Returns the last entered String
SetFPS(F_ST:Byte) ; enables/disables FPS in the topright of the console
Send_Text(txt:String) ; Send a string directly to the Console (maybe good for debugging)
Set_CursorSpeed(_blink:int) ; sets the blink time of the cursor.

[Edit:]
Edited the Cursor to be independent from the framerate and added new command for the Cursor Blinktime.
The Code is not well written but doesn't use much CPU.
Also fixed a bug which appears when you edit a String with the Cursor keys.

the Update method have to be at the end of your main loop(before the flip command).


Module Pub.Console
ModuleInfo "Name: A Console Module for BMAX"
ModuleInfo "License : None(Credits would be nice)"
ModuleInfo "Copyright 2005 Benjamin 'Klepto2' Rössig"
ModuleInfo "Simple Console like in some Shooters or similar"
ModuleInfo "Version: 0.5"

Import BRL.GLMax2d
Import BRL.Max2D
Import BRL.Keycodes
Import BRL.Retro
Import BRL.LinkedList

Global CText_List:Tlist


Type Console
	
	
	Field length:Int
	Field height:Int
	Field index:Int = 0
	Field Alpha:Float
	Field Status:Int
	Field tc:Int
	Field temp_Text:String
	Field Return_Text:String = ""
	Field Show_FPS:Byte = True
	Field fc,ms,mn,fr
	Field MAX_Chars:int
	Field c_timer1:Int = 0
	Field c_timer2:int = 0
	Field c_ms:Int
	Field c_pos:int = 0
	Field list_index:int = 0
	Field c_blink:Int = 500
	
	Function Create:Console(_length:Int,_height:int,_alpha:Float,_max_chars:Int)
		local c:Console = New Console
		c.length = _length
		c.height = _height
		c.Alpha = _Alpha
		c.MAX_Chars = _max_chars
		Return c
		
	End Function
	
	Method Update()
		if status = 1 then
			if Background = Null then
				SetBlend Alphablend
				SetAlpha alpha
		 		SetColor 25,25,25
				Drawrect(0,0,length,height)
				SetColor 255,255,255
				Drawline 0,height-25,length,height-25
				if Show_FPS = True then
					if fc = 0 then ms = Millisecs()
					mn = Millisecs() - ms
					fc = fc + 1
						if mn > 1000 then
						fr = fc
						fc = 0
						endif
					SetColor 25,25,25	
					Drawrect(length,0,30,15)
					SetColor 255,255,255
					DrawText fr,length,0
				end if
				local t_index:Int = 0
				if Ctext_List <> Null then
					For t_Text:CText = eachin Ctext_list
						SetColor 255,255,255
						Drawtext(t_Text.Text,0,height-(40+(t_index*20)))
						t_index = t_index + 1
					next
				Endif
				
				
			endif
			
			tc = Getchar()
			SetColor 255,255,255
			SetAlpha alpha*2
		
			if c_pos = temp_text.Length then	
				if tc <> 0 and temp_text.Length < max_chars and tc <> key_backspace and not keydown(key_alt) and tc <> key_enter then
					Temp_Text = Temp_text + CHR(tc)
					c_pos = c_pos + 1
				Endif
				if tc = key_backspace then
					Temp_text = left(Temp_Text,Temp_text.length - 1)
				if c_pos > 0 then c_pos = c_pos - 1
				Endif
			else
			    if tc <> 0 and temp_text.Length < max_chars and tc <> key_backspace and tc <> key_enter then
					t_text1:String = left(temp_text,c_pos)
					t_text2:String = right(temp_text,temp_text.length-c_pos)
					
					T_Text2 = CHR(tc) + T_Text2
					Temp_text = T_text1 + T_text2
					Temp_Text = left(temp_text,temp_text.length)
					Debuglog t_text1.Length + " : " + t_text2.length + " : " + temp_text.Length
					c_pos = c_pos + 1
				Endif
				if tc = key_backspace then
				    t_text1:String = left(temp_text,c_pos-1)
					t_text2:String = right(temp_text,temp_text.length-c_pos)
					Temp_text = t_text1 + t_text2
					Temp_Text = left(temp_text,temp_text.length)
					Debuglog t_text1.Length + " : " + t_text2.length + " : " + temp_text.Length
					if c_pos > 0 then c_pos = c_pos - 1
				Endif
			Endif
			if keyhit(key_right) then 
				if c_pos < temp_text.length then 
					c_pos = c_pos + 1
				Endif
			endif
			if keyhit(key_left) then 
				if c_pos > 0 then 
					c_pos = c_pos - 1
				Endif
			endif	
			if keyhit(key_up) then
			if Ctext_List <> Null then
			if list_index <= Ctext_list.Count() then list_index = list_index + 1
				if list_index > Ctext_list.Count() then List_index = 1
				local n = 0
				
					For t_Text:CText = eachin Ctext_list
						temp_text = t_text.Text
						n = n + 1
						if n = list_index then 
							n = 0
							exit
						endif
					Next
				Endif
			 c_pos = temp_text.Length
			endif
			
			if keyhit(key_Down) then
			
			if Ctext_List <> Null then
				if list_index >= 0 then list_index = list_index - 1
				if list_index < 0 then List_index = Ctext_list.Count()-1
					For t_Text:CText = eachin Ctext_list
						temp_text = t_text.Text
						n = n + 1
						if n = list_index then 
							n = 0
							exit
						endif
					Next
				Endif
			 c_pos = temp_text.Length
			endif
			if tc = key_Enter then
					'Temp_Text = left(temp_text,temp_text.length-1)
					CText.Create(Temp_Text.Trim())
					Return_Text = Temp_Text
					Temp_Text = ""
					c_pos = 0
					list_index = 0
			end if
			DrawText(Temp_text,0,height-20) 
			
			'Draw Cursor
			if c_timer1 = 0 then c_timer1 = millisecs()
			if c_ms - c_timer1 > c_blink then
				DrawText "_",c_pos*8,height-18
			    if c_timer2 = 0 then c_timer2 = millisecs()
				if c_ms - c_timer2 > c_blink then
					c_timer1 = 0
					c_timer2 = 0
				endif
			endif
			c_ms = millisecs()
		'	Debuglog temp_text.length
			'End Cursor
		endif
		
	End Method
	Method SetStatus(st:int)
		Status = st
		Flushkeys()
	End Method
	
	Method Set_Alpha(_al:Float)
		Alpha = _al
	End Method
	
	Method Return_Alpha:Float()
		Return Alpha
	End Method
	
	
	Method Return_Status:Int()
		Return Status
	End Method
	
	Method Return_last:String()
		Return Return_text
	End Method
	
	Method SetFPS(f_ST:Byte)
		Show_FPS = f_ST	
	End Method
	
	Method Send_Text(txt:String)
		CText.Create(txt)
	End Method
	
	Method Set_CursorSpeed(_blink:int)
		c_blink = _blink
	End Method
	
End Type

Type CText
	Field Text:String
	
	Function Create(_Text:String)
		if CText_List = Null then CText_list = New TList
		t_Text:CText = New CText
		t_Text.Text = _Text
		CText_List.Addfirst(t_Text)
	End Function
	
End type

	



And here a working sample :


Framework pub.console


Graphics(640,480,0,-1)

c:console = console.Create(300,100,.5,20)
c.setFPS(False)
c.setstatus(1)
While Not KeyHit(key_escape)
Cls

Global A_st:Float = c.Return_Alpha()

If KeyHit(key_space) And KeyDown(key_alt) Then 
	_st = 1 - _st
	c.setstatus(_st)
	End If
If KeyHit(key_F) And KeyDown(key_alt) Then 
	F_st = 1 - f_st
	c.setFPS(F_st)
End If 
If KeyDown(key_A) And KeyDown(key_alt) Then 
	A_st:Float = A_St + .001
	End If
If KeyDown(key_Y) And KeyDown(key_alt) Then 
	A_st:Float = a_st - .001 
End If 
If KeyHit(KEY_1) And KeyDown(key_alt) Then
	c.send_Text("Test")
EndIf
if keyhit(KEY_2) and keydown(key_alt) then
	c.set_CursorSpeed(500)
endif
if keyhit(KEY_3) and keydown(key_alt) then
	c.set_CursorSpeed(50)
endif

DrawText(c.Return_text,320,240)
c.set_Alpha(A_st)
c.Update()
FlushMem()
Flip

Wend



and now enjoy.

BTW: Critic and Suggestions are welcome :)

Nice one, and works !

:)

Sergio.

New Version available. Version 0.6 has some new things added and can be download now.

http://klepto2.kl.funpic.de/DL/

(ConsoleMod.Rar)

Features added:

change of the Object name (Console --> CConsole) in case of Confusion
Color setting
Adding of a Watchsystem (for Debugging and BetaTesting)
already compiled

more about in the Readme.txt and in the Sample

Please post your Opinions and your Suggestions because I want to know what I could make better (its my first Module).

BTW : THX Semar

New Version available:
Version 0.7

Added support for static and animated Images
Added support for Fonts
Added optional frame for the Console Window



Here is my updated Console Module for Bmax

Console Module Version 1.0
--------------------------

Features:

Full OOP Design (IMO)
Uses Max2d (so I think it may compatible with later 3DModule by Mark)
COnfigureable size
FPS Counter
Watch Functions
Sending Text to the Console and receiving Input from the Console
Alpha support
Easy to use
Different Fonts usable
static and animated Images as background supported
Frame supported
Frame could be changed via runtime
Roll in and out feature
Caption support

Complete Functions and Method list:

type cconsole
	function create:cconsole(_length:int,_height:float,_alpha:float,_max_chars:int,_fading:byte = false,_frame:byte = true,_border:int = 5,_img:string = "",_anim:byte = false,_aw:int = 0,_ah:int= 0,_af:int = 0,_am:int = 0,_fps:int = 25)
	method update()
	method set_font(_font:string,_size:int = 12)
	method setstatus(st:int)
	method set_statusbar(b_st:byte)
	method set_alpha(_al:float)
	method return_alpha:float()
	method return_status:int()
	method return_last:string()
	method setfps(f_st:byte,d_st:byte = true)
	method get_fps:int()
	method set_cursorspeed(_blink:int)
	method set_backcolor(_r:int,_b:int,_g:int)
	method set_textcolor(_r:int,_b:int,_g:int)
	method set_framecolor(_r:int,_b:int,_g:int)
	method set_frame(_frame:byte)
	method set_border(_border:int)
	method add_intwatch(name:string,_ptr:int ptr)
	method add_stringwatch(name:string,_ptr:string ptr)
	method add_floatwatch(name:string,_ptr:float ptr)
	method add_doublewatch(name:string,_ptr:double ptr)
	method add_longwatch(name:string,_ptr:long ptr)
	method add_bytewatch(name:string,_ptr:byte ptr)
	method set_caption(_caption:string)
end type



Download at : http://www.brsoftware.de.vu/DL/

Most bugfixes are done, but if you find something let me know. Also some Feedback would be nice.

Awww man! I get Blitz telling me there's too many function parameters! Any clues? I tried rebuilding all the modules too. No love. Using BlitzMax 1.09. :(

Is this when you call the CConsole.Create function.

When yes could you tell me the line in which you create it?

Well when I dont make it a module, it works fine. :) Who knows. I always seem to have lame trouble with modules. Suck! BUT ANYWAYS, it works awesome! I had your version 06 one and was very impressed! This one is even better. Keep it up!

Thx Booticus.

Maybe the new Version does work better.

Version 1.01 :

Added Method : Return_TrimText(_trim:int)
Added multiline support for sended text.

http://www.brsoftware.de.vu/DL/

File : ConsoleMODV101.rar

I have uploaded a hotfix for Bmax 1.10
It fixes Import things with glmax2d and d3d7max2d
and it fix some changes which had come with the Update of Bmax.

File: consolehotfix.rar

New Update available : Version 1.02

file: ConsoleMODV102.rar (hotfix included)
Download at : http://www.brsoftware.de.vu/DL/

fully compatible to Bmax 1.10
Added Method Return_TrimText2(_trim:String)

What does it ?
Trimming a return Text to different parts :
i.e : the Return_text 'Load Map MyMap.dat' will result in

text[0] = 'Load'
text[1] = 'Map'
text[2] = 'MyMap.dat'

by using following code :

local text:String[]
text = c.Return_TrimText[32] ' The '32' stays for the ASCII Code of
' " " in this case

After that you can get the different parts of text by using a For next loop.
For further details look in the sample.
Added another method quite the same as Return_TrimText but this is called
Return_Trim_text2(_Trim:String)

Instead of giving an int to the Method like in the first method you
can enter a String like " :" or "ABCDEF"

The difference between both methods is the following :
In some cases you have entered a text like "Load Map:MyMap.dat"

With Method 1 (Return_TrimText(32)):

text[0] = 'Load'
text[1] = 'Map:MyMap.dat'

With Method 2 (Return_TrimText2(" :")):

text[0] = 'Load'
text[1] = 'Map'
text[2] = 'MyMap.dat'

I hope you see the advantage of the two methods.