About Monkey 2 › Forums › Monkey 2 Programming Help › Closing a Window
This topic contains 10 replies, has 5 voices, and was last updated by
Mark Sibly
2 years ago.
-
AuthorPosts
-
April 7, 2017 at 2:13 am #7750
Is there no way to close a Window without killing the AppInstance? There don’t seem to be any SDL_DestroyWindow calls anywhere that I can find, even.
Or maybe I’m just going about things the wrong way. How is everyone handling changing resolutions? At this point, the only thing I can think of would be to have a persistent “game state” object that you create in Main, then create an AppInstance and Window within some loop, and then call Terminate on that app and make a new one that uses the same state object if you even need to change resolution, switch windowed/fullscreen, etc. But… that seems like a pretty bizarre way to handle things, and I question whether that will be sanitary for things like std.time.Millisecs and so on.
Edit: Actually, that doesn’t even work, since Terminate calls libc.exit instead of continuing after the blocking App.Run call. Is this just impossible because Mark didn’t get around to writing SDL cleanup code? I guess none of this would work at all outside of desktop, but is noone trying to develop something commercial?
April 7, 2017 at 7:08 am #7755you could run in console mode and use createwindow function in mojo module just use global pointers to store the new window hwnd and then terminate the global appInstance and as its console it should not close the full app just the hwnd.
April 7, 2017 at 7:24 am #7756How is everyone handling changing resolutions?
You can change window size on the fly, but with some extra code right now (it’s easy to make it a part of Window class).
Code:
Monkey1234Funcrion ResizeWindow( wnd:Window,size:Vec2i )SDL_SetWindowSize( wnd.SDLWindow,size.x,size.y )wnd.SendWindowEvent( New WindowEvent( EventType.WindowResized,wnd ) )EndAdditional import is required:
Monkey12#Import "<sdl2>"Using sdl2..Usage:
Monkey1ResizeWindow( App.ActiveWindow,New Vec2i(800,600) )Also there is a SDL_HideWindow() method inside of sdl2 module.
Maybe it’s that you are searching for (instead of DestroyWindow).
April 7, 2017 at 8:14 pm #7763Running in console mode – wouldn’t that not get you access to SDL, though? Still would like to be using SDL for input and rendering and everything. Maybe I’m wrong here, but getting the WinAPI handle of a manually created window seems like the wrong approach.
SDL_HideWindow and SDL_DestroyWindow aren’t quite the same, no.
It looks like the sdl2 module exports SDL_SetWindowBordered and SDL_SetWindowFullscreen, among others, so maybe the solution is just to run SDL commands manually.
In that case I would have to hack mojo though – mojo.app.Window._sdlWindow is private. While I’ve posted quite a bit in the past about wanting more direct access to OpenGL/SDL directly, in this case for simple 2D I do plan on actually using mojo, is all.Oh, there is actually a getter for the SDL window, nevermind. I should’ve read your code more carefully.Is there a reason that more of this functionality isn’t already exposed by the Window class? This was all so easy by the time I started using monkey1, and I don’t think I was super late to the party. Maybe the worst part is that 90% of the methods on these classes are hidden. I get that Mark wants to wait to expose stuff that isn’t finalized, but it means I have to spend a lot of time looking through the actual source for all these modules in order to be able to actually do anything productive with them at all. It was all I could do to figure out how to get an actual timestamp for SeedRnd:
Monkey1SeedRnd(libc.tolong(libc.time(Null)))All of the time_t and tm_t structs, and all of libc, aren’t even documented – but from the looks of things it would’ve been really hard to get an actual timestamp from std.time.Time even after calling std.time.Time.Now to get the system time in the first place.
April 7, 2017 at 8:52 pm #7766[/crayon]Monkey1234567891011121314151617181920212223242526272829[crayon-5cba1573c3429959834040 inline="true" ]Namespace myConsoleApp#Import "<std>"#Import "<libc>"#Import "<reflection>"#import "<mojo>"Using std..Using libc..Using reflection..Using mojo..class appwindow Extends WindowMethod New()Super.New("Game", 640, 480)End MethodEnd Class'here is the main function which is called. and the simplest text "hello world" is outputted to the consoleFunction Main()Print "Hello World"Global pAppinstance1:= New AppInstanceGlobal pApp1:= New appwindowGlobal pAppinstance2:= New AppInstanceGlobal pApp2:= New appwindowApp.Run()Endas you can see you can create instances and multi windows from the console its the route im trying to pass instance to a externel lib. its a bit hackish but mx2 is still being developed.
April 7, 2017 at 10:13 pm #7767The fact that you have two AppInstance’s isn’t doing anything for you here, unless I’m crazy. When you call App.Run(), it’s executing on pAppinstance2, which starts an SDL event loop. Both windows work because they aren’t tied directly to the AppInstance; both pAppinstance1 and pAppinstance2 should have access to both windows when they call Window.VisibleWindows().
I don’t know what you’re trying to do here, but I have a lot of trouble believing that that could possibly help anything, and it certainly doesn’t give you any extra ability to close windows individually or to exit that SDL event loop and re-enter a new one (via App.Terminate or whatever).
I’m reasonably confident that using SDL commands directly is the only solution right now.
April 7, 2017 at 10:43 pm #7768i used app.run to show that you can create instance in console mode obv you would need to overide the window attributes you just create ur own app.run not the default.
the other way would be to create a cpp class that handles the SYS WINDOW directly so you just hook into the window exteranly.
could you not extend the app.cpp in mojo?
April 8, 2017 at 4:43 am #7779Is there a reason that more of this functionality isn’t already exposed by the Window class?
The main idea here is to extend base minimal functionality by real needs – to minimize overcoding.
Also don’t forget that mx2 is very newly language. Our real using and reature requesting make it better and better.
—————————————–
Not need to create many app instances, but create a few windows.
And create many windows isn’t a problem – it works.
The problem is terminating whole app when closing any window.
I looked into sources of Window class and found that (private method):
Monkey12345678910111213141516171819202122#rem monkeydoc Window event handler.Called when the window is sent a window event.#endMethod OnWindowEvent( event:WindowEvent ) VirtualSelect event.TypeCase EventType.WindowCloseApp.Terminate()Case EventType.WindowResizedApp.RequestRender() 'Should maybe do this regardless?Case EventType.WindowGainedFocusApp.RequestRender() 'Need to do this for KDE on linux?EndEndAs you can see, there is
Monkey123Case EventType.WindowCloseApp.Terminate()that doesn’t check windows count, and terminate app by closing any window.
I added that as issue: https://github.com/blitz-research/monkey2/issues/163
April 9, 2017 at 11:06 pm #7826Currently, mojo is designed very much around ‘one permanent window’ style game coding. Multiple windows are very untested (although the basic framework for support is in there and they will eventually happen) and yes, closing window=closing app, something that will also change in future.
handling changing resolutions?
The Window.BeginFullscreen() and Window.EndFullscreen() methods allow you to dynamically enter/leave fullscreen mode at runtime. With no params, BeginFullscreen enters ‘desktop mode’ fullscreen, or you can specify width,height,hertz to actually change display mode. Note you can also provide a virtual resolution via OnMeasure:Vec2i() so desktop mode fullscreen is often fine.
See bananas/spacehimps for a demo of Alt-Enter fullscreen toggling. See bananas/viewlayout for a demo of virtual resolution.
There’s still no way to enum display modes cleanly though (excluding SDL) – coming soon-ish.
To manually change position/size of a window (in ‘desktop’ coords) use the Window.Frame property.
In general, avoid using SDL_* calls if there’s a ‘mojo’ way to do something. SDL_Resize will probably work, but only because mojo currently polls window size for the sake of some platforms. I’d rather not expose this level of mesiness though (ie: In future it may not, or it may not on some platforms etc) so if there’s mojo way to do something, please use that as it should be ‘just work’. And if it doesn’t, or mojo doesn’t have some feature you’re having to resort to SDL for, let me know or better, post an issue at github.
April 10, 2017 at 1:43 am #7833There’s a bug in the Window.BeginFullscreen method. I posted an issue on Github…
When it’s fixed the following *rough* example should work. It gets your primary display’s supported modes and allows you to switch through them at run time:
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163[crayon-5cba1573d094b886798143 inline="true" ]#Import "<std>"#Import "<sdl2>"#Import "<mojo>"Using std..Using sdl2..Using mojo..Function Main()New AppInstanceNew MyWindowApp.Run()EndClass MyWindow Extends WindowField _curScreenMode:ScreenModeField _curScreenModeStr:StringMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )'In this example, assuming mojo is using SDL display id = 0.ScreenMode.GetAll(0)ScreenMode.DebugPrintAll()If ScreenMode.Stack.Length <= 0Print "ERROR: Could not find a valid screen mode."App.Terminate()EndSetScreenMode(0)EndMethod SetScreenMode:Void(idx:int)If Fullscreen Then EndFullscreen()If idx < 0 Or idx >= ScreenMode.Stack.Length Then idx = 0_curScreenMode = ScreenMode.Stack[idx]_curScreenModeStr = _curScreenMode.ToString()BeginFullscreen(_curScreenMode.Width, _curScreenMode.Height, _curScreenMode.Rate)App.RequestRender()EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText("[TAB] - toggle fullscreen, [LEFT] [RIGHT] change screen mode.", Width / 2, Height / 4, 0.5, 0.5)canvas.DrawText(_curScreenModeStr, Width / 2, Height / 2, 0.5, 0.5)canvas.Flush()EndMethod OnKeyEvent:Void(event:KeyEvent) OverrideIf event.Type = EventType.KeyDownSelect event.KeyCase Key.EscapeApp.Terminate()Case Key.TabIf Fullscreen Then EndFullscreen() Else SetScreenMode(_curScreenMode.ModeId)Case Key.LeftSetScreenMode(_curScreenMode.ModeId - 1)Case Key.RightSetScreenMode(_curScreenMode.ModeId + 1)EndEndifEndEndClass ScreenMode'TODO: DPI information for each display?PublicGlobal Stack:Stack<ScreenMode>Function GetAll:Void(displayId:Int = -1)Stack = New Stack<ScreenMode>Local numDisplays := SDL_GetNumVideoDisplays()For Local i:Int = 0 Until numDisplays'Only include modes matvhing the specified displayId'otherwise if < 0 then inlclude all modes for all detected displays.If displayId >= 0 And i <> displayId Then ContinueLocal displayName := String.FromCString(SDL_GetDisplayName(i))Local numModes := SDL_GetNumDisplayModes(i)For Local j := 0 Until numModesLocal displayMode:SDL_DisplayModeLocal displayModePtr:SDL_DisplayMode Ptr = Varptr displayModeSDL_GetDisplayMode(i, j, displayModePtr)Stack.Push(New ScreenMode(i, j, displayMode))NextNextEndFunction DebugPrintAll:Void()For Local mode := Eachin StackPrint mode.ToString()NextEnd'----------PrivateField _displayId:IntField _modeId:IntField _mode:SDL_DisplayModePublicMethod New(displayId:Int, modeId:Int, mode:SDL_DisplayMode)_displayId = displayId_modeId = modeId_mode = modeEndProperty DisplayId:Int()Return _displayIdEndProperty ModeId:Int()Return _modeIdEndProperty Width:Int()Return _mode.wEndProperty Height:Int()Return _mode.hEndProperty Rate:Int()Return _mode.refresh_rateEndMethod ToString:String()Local str := "(" + _displayId + ") " + String.FromCString(SDL_GetDisplayName(_displayId))str += " -- [" + _modeId + "] " + _mode.w + "x" + _mode.h + " @ " + _mode.refresh_rate + "hz"Return strEndEndApril 10, 2017 at 2:11 am #7834…thanks for that Impixi, fixing now.
And BeginFullscreen isn’t even docced yet! Fixing that too…in general, if you notice something @hidden in the docs that looks like it shouldn’t be, please let me know.
-
AuthorPosts
You must be logged in to reply to this topic.