DeviceWidth/DeviceHeight

Monkey Forums/Monkey Programming/DeviceWidth/DeviceHeight

KawaCoder(Posted 2012) [#1]
Dear Monkey Coders,

Is there a way to fetch a value of DeviceWidth() without getting error message "bb_graphic_device = null"? The example is shown below.
[monkeycode]
Function Main:Int()
Print "Hello, Monkey~n"
Print "Console width is " + DeviceWidth()
Print "and height is " + DeviceHeight()
Return 0
End
[/monkeycode]
Thank you.


Xaron(Posted 2012) [#2]
Yes. Just don't do it in the Main function. At that time (within the main function) the device is not created yet, so you get an access violation.


KawaCoder(Posted 2012) [#3]
How do you create the device first? That is my real question.

Thank you.


Xaron(Posted 2012) [#4]
It is created automatically in OnCreate if your program derives from the App class.


vbnz(Posted 2012) [#5]
[monkeycode]
import mojo
Class YoursApp extends App
Method OnCreate:int()
Print "Hello, Monkey~n"
Print "Console width is " + DeviceWidth()
Print "and height is " + DeviceHeight()
SetUpdateRate 60
end Method
method OnUpdate:int()
end method
Method OnRender:int()
Cls
DrawText "Console width is " + DeviceWidth(),0,0
DrawText "and height is " + DeviceHeight(),0,20
end Method
end class
Function Main:int()
New YoursApp
End Function[/monkeycode]


Xaron(Posted 2012) [#6]
(bow) Danke vbnz.


KawaCoder(Posted 2012) [#7]
Sorry, I didn't make myself clear what I am after. Is it possible to create a device without using the class App first? I believe there is a way to do so but how?

Thank you.


Xaron(Posted 2012) [#8]
No there isn't. Well actually you could dig through the mojo code and do it by yourself, but why?


KawaCoder(Posted 2012) [#9]
"but why?"

I am developing a module named monkcon. I want my students to learn Monkey fundamentals without first introducing Monkey App class (OOP). The example:
[monkeycode]
import monkcon

Function Main:Int()
Print "Console width is " + ConsoleWidth()
Return 0
End
[/monkeycode]

The function ConsoleWidth() in the module should be able to create the device first and then return the value of DeviceWidth() I am not familiar with Monkey mojo. I am still new to it. Can't anyone help me to solve the problem?

Thank you.


muddy_shoes(Posted 2012) [#10]
Depends what you mean by "using it". You need to create one because it is the wrapper for the whole idea of there being a device with dimensions.

You can just create a base app and ignore it:

[monkeycode]
Import mojo

Function Main:Int()
Local a:App = New App()

Print DeviceWidth
End
[/monkeycode]

This would work on some targets:

[monkeycode]
Import mojo

Function Main:Int()
SetGraphicsContext(New GraphicsContext(New GraphicsDevice()))

Print DeviceWidth()
End
[/monkeycode]

But not HTML5 as the App class is the one aware of the canvas. Without it the canvas reference isn't set up.


muddy_shoes(Posted 2012) [#11]
Just read your last reply. If you want to hide the App from your students then you can do it in your function library. Just create the app yourself when they call ConsoleWidth (or any other function that requires the App).


vbnz(Posted 2012) [#12]
muddy_shoes,
[monkeycode]
Import mojo

Function Main:Int()
SetGraphicsContext(New GraphicsContext(New GraphicsDevice()))

Print DeviceWidth()
End

[/monkeycode]
This didn't work because GraphicsContext is private.

Xaron,Thanks!


KawaCoder(Posted 2012) [#13]
Your first solution might work for my case. I'll try it in my work.

Much appreciated.


muddy_shoes(Posted 2012) [#14]
My mojo isn't stock, so I probably removed the Private line. It's not what he really wants to do anyway.


smilertoo(Posted 2012) [#15]
you seem to be introducing a lot of problems, have you looked at blitzmax?


KawaCoder(Posted 2012) [#16]
I am not BlitzMax user but why? BlitzMax does not have mojo module, correct?
I think most of my problems lay in mojo per se.

BTW, I have solved the problem successfully. The next post will follow.


KawaCoder(Posted 2012) [#17]
Here is the final solution to my problem.

Part of the monkcon module:
[monkeycode]
Import mojo

Function ConsoleWidth:Int()
New App
Return DeviceWidth()
End

Function ConsoleHeight:Int()
New App
Return DeviceHeight()
End
[/monkeycode]

The main program:
[monkeycode]
Import monkcon

Function Main:Int()
Print "Hello, Monkey~n"
Print "Console width is " + ConsoleWidth()
Print "and height is " + ConsoleHeight()
Return 0
End
[/monkeycode]

Both HTML5 and Flash targets show the correct answers but not Android target. It shows zeros for the dimensions. Is it a known bug?


Samah(Posted 2012) [#18]
You're creating the App instance twice. This is bad. To be honest I don't know why you're trying to do Mojo-related stuff when you're creating a console application. Mojo is only necessary if you want to make a graphical application, or for targets that do not support a headless mode. Without Mojo you are limited to HTML5 and stdcpp, which should be good enough for teaching a "console" interface.


therevills(Posted 2012) [#19]
I want my students to learn Monkey fundamentals without first introducing Monkey App class (OOP).


The commands DeviceWidth and DeviceHeight are part of Mojo, so nothing to do with the Monkey fundamentals.

I agree with Samah, stay away from Mojo totally for the time being.


KawaCoder(Posted 2012) [#20]
@Samah

The example above was merely simplified in order to show how the solution works. A full version example is shown at post #9 in the thread. The results were rendered entirely on the console (aka, canvas) across some platforms: HTML5, Flash, and Android. (I haven't tested the other platforms.)

I used App class for the rendering at the end of the program. Those other two App instances should have been destroyed eventually upon the end of each function like Java does.

I hope it makes sense.

Thank you.


Samah(Posted 2012) [#21]
App isn't some magical class you can create and throw away. It encompasses your entire application, and it doesn't make sense to ever have more than one of them. In fact, it should really be a singleton. Some targets would probably break if you tried to instantiate it twice. I'm surprised Android works at all.


KawaCoder(Posted 2012) [#22]
I will set up a project named monkcon at Google Code site in a couple of days so you all could be able to view all source code written in Monkey. Please note that the project is still in a very early alpha stage.

I hope it would make all sense why monkcon uses App class for the rendering. The output is scrollable in the "console" unlike the output done by Print commands.

Note: The console can be shown on Android devices whereas the output by Print commands isn't. Thanks to the mighty mojo.


Gerry Quinn(Posted 2012) [#23]
I think the simplest solution is simply to make a shell app that calls a function which can be your students equivalent of a Main() function. Here, for example, they can calculate and print out anything they want just by typing it into MyProgram().

They can even use mojo functions to create (non-moving) graphic images.

The following only works if the Print command works, but you can easily add your own MyPrint() function that they can call instead during MyProgram().





Gerry Quinn(Posted 2012) [#24]
I think the simplest solution is simply to make a shell app that calls a function which can be your students equivalent of a Main() function. Here, for example, they can calculate and print out anything they want just by typing it into MyProgram().

They can even use mojo functions to create (non-moving) graphic images.

The following only works if the Print command works, but you can easily add your own MyPrint() function that they can call instead during MyProgram().





vbnz(Posted 2012) [#25]
muddy_shoes,ok!
What modifications in mojo you did?


muddy_shoes(Posted 2012) [#26]
@vbnz A few things. Mostly to do with off-screen rendering which may or may not be redundant now with the recent read/write pixel addition.

@KawaGeo When I suggested creating the App behind the scenes I meant just one instance. Samah is correct that creating multiples may cause problems as the App class triggers a bunch of global initialisation.

I think you're creating a bit of a rod for your own back by trying to hide all evidence of classes from your students. Overall I think Gerry's suggestion is a good way to go as it brings the "console script" within the standard App processes. The console function could be in a separate file if a simple "ignore the rest for now" instruction doesn't suffice.


ziggy(Posted 2012) [#27]
I would suggest you to use the stdcpp target for console purposes if you're just teaching some coding basics.
You can use this module to allow console input:
[monkeycode]#REM
header: This module is a very simple I/O layer for the stdcpp target.
It provides the basic functionality to send string data to the Standard Output Pipe, and to the Standard Error Pipe. It also provides an Input function to get data from the user, using the typical console prompt.
#END
#if TARGET="stdcpp" Or TARGET="glfw"
'Private
Extern Private

Class FILE
End

Global stdin:FILE
Global stdout:FILE
Global stderr:FILE

Function fputc(c, file:FILE)
Function fflush(file:FILE)
Function fgetc:Int(file:FILE)
'Const EOF
Public

'summary: Prompt for user input in the system console window and resturns a String
Function Input:String(prompt:String = ">")

Local c:Int, result:String

For Local i:Int = 0 Until prompt.Length
fputc(prompt[i], stdout)
End
fflush(stdout)

c = fgetc(stdin)

While c <> 10 And c <> 13
result += String.FromChar(c)
c = fgetc(stdin);
Wend

fflush(stdin)

Return result;
End

'summary: Sends text to the standard output pipe
Function Output:Void(value:String)
For Local i:Int = 0 To value.Length - 1
fputc(value[i], stdout)
Next
fflush(stdout)
End

'summary: Sends text to the standard error pipe
Function ErrOutput:Void(value:String)
For Local i:Int = 0 To value.Length - 1
fputc(value[i], stderr)
Next
fflush(stderr)
End
#Else
#Error "This module is only supported in C++ based targets such as stdcpp or glfw."
#End
[/monkeycode]


muddy_shoes(Posted 2012) [#28]
I think you (and others) are getting confused about what he means by "console". He's not creating a console app, he's trying to create a cross-platform console-like environment.


KawaCoder(Posted 2012) [#29]
I have been warned that multiple of App instances is a bad idea so I changed those functions which follow:
[monkeycode]
Function ConsoleWidth()
Return 640
End

Function ConsoleHeight()
Return 480
End
[/monkeycode]
After all, we are using only HTML5 target for our initial training.

I prefer to keep all OOP stuff behind the scene to avoid any confusings for new students.

Thank you for all help you guys have done.