Commits

Sepehr Taghdisian committed 5162b2e

Added App.cs,
app decoupled from engine,
updated 'Examples'

  • Participants
  • Parent commits 51befbe
  • Branches appdecouple

Comments (0)

Files changed (12)

BehaviorEx/OrbitCam.cs

 
         protected override void Update(float dt)
         {
-            Vec2 mpos = Input.MousePosition;
-            if (mpos.X <= 0 || mpos.X >= Engine.ScreenSize.X || mpos.Y <= 0 || mpos.Y >= Engine.ScreenSize.Y)
-                return;
+            if (Input.IsMouseDown(MouseKey.Left)) {
+                Vec2 mpos = Input.MousePosition;
 
-            if (Input.IsMouseOnce(MouseKey.Left) || Input.IsMouseOnce(MouseKey.Right)) {
-                _lockPos = Input.MousePosition;
-            } else if (Input.IsMouseDown(MouseKey.Left)) {
-                Engine.LockCursor(_lockPos);
+                if (!_leftButton)  {
+                    _leftButton = true;
+                    Input.LockCursor();
+                    _lockPos = mpos;
+                }
+
                 Vec2 delta_pos = (_lockPos - Input.MousePosition) * Sensitivity;
-
                 _x += delta_pos.X;
                 _y += delta_pos.Y;
-            } else if (Input.IsMouseDown(MouseKey.Right)) {
-                Engine.LockCursor(_lockPos);
-                Vec2 delta_pos = (_lockPos - Input.MousePosition) * Sensitivity;
-
-                Distance += delta_pos.Y;
+                _lockPos = mpos;
+            }   else    {
+                _leftButton = false;
             }
 
-            Matrix4 rot = Matrix4.CreateRotationY(_x * MathUtil.Deg2Rad) * Matrix4.CreateRotationX(_y * MathUtil.Deg2Rad);
+            if (Input.IsMouseDown(MouseKey.Right)) {
+                Vec2 mpos = Input.MousePosition;
+
+                if (!_rightButton)  {
+                    Input.LockCursor();
+                    _rightButton = true;
+                    _lockPos = mpos;
+                }
+
+                Vec2 delta_pos = (_lockPos - Input.MousePosition) * Sensitivity;
+                Distance += delta_pos.Y;
+                _lockPos = mpos;
+            }   else    {
+                _rightButton = false;
+            }
+
+            if (!_rightButton && !_leftButton)
+                Input.UnlockCursor();
+
+            Matrix4 rot = Matrix4.CreateRotationY(_x * MathUtil.Deg2Rad) * 
+                Matrix4.CreateRotationX(_y * MathUtil.Deg2Rad);
             Quat rot2 = Quat.FromMatrix(new Matrix3(rot));
 
             _transform.Rotation = rot2;
-            _transform.Position = Vec3.TransformPosition((new Vec3(0, 0, -Distance)), Matrix4.CreateFromQuat(rot2)) + Target;
-
+            _transform.Position = Vec3.TransformPosition((new Vec3(0, 0, -Distance)), 
+                Matrix4.CreateFromQuat(rot2)) + Target;
         }
         #endregion
 
         private float     _y = 0;
         private Vec2      _lockPos = Vec2.Zero;
         private Transfrom _transform;
+        private bool _leftButton = false;
+        private bool _rightButton = false;
         #endregion
     }
 }

DarkMotion/Forms/MainForm.cs

         private void hmrview_reset()
         {
             hmrView.ResetEngine();
-            Engine.AddVdir(_animController.RootPath, true);
+            Engine.AddVirtualPath(_animController.RootPath, true);
             var h3da = _animController.ReelPath;
             var h3dm = h3da.Substring(0, h3da.Length - 1) + "m";
             if (!File.Exists(h3dm)) {

Examples/Program.cs

 
     class Program
     {
-        static float f;
-
         private static void Main(string[] args)
         {
             // initialize dark-hammer core system
                 cfg = Config.Default;
 
             // create an engine instance
+            App.Init("Hello world!", cfg);
             Engine.Init("Hello World!", cfg);
+            App.ShowWindow();
 
             // adding media directory
             string vdir = Path.GetFullPath("../../Media");
-            Engine.AddVdir(vdir, true);
+            Engine.AddVirtualPath(vdir, true);
 
             // create an empty scene
             Scene scene1 = Engine.CreateScene("test-scene");
             cam.GetBehavior<OrbitCam>().Target = new Vec3(0, .4f, 0);
 
             // some global code
+            /*
             Engine.Update += (dt) => {
                 f += 0.003f;
                 var rmx = Matrix4.CreateFromAxisAngle(Vec3.UnitY, f);
                 Vec3.TransformVector(ref dir, ref rmx, out wdir);
                 scene1.SunDir = wdir;
             };
+             * */
 
-            // run engine
-            Engine.Run();
+            App.Update += () =>
+                {
+                    Engine.Update();
+                    App.SwapBuffers();
+                };
+
+            App.Keypress += (charcode, vkey) =>
+                {
+                    Engine.SendKeys(charcode, vkey);
+                };
+
+            App.Resize += (width, height) =>
+                {
+                    App.ResizeView(width, height);
+                    Engine.ResizeView(width, height);
+                };
+
+            // run message loop
+            App.Run();
 
             cfg.Dispose();
             Engine.Release();
-
+            App.Release();
             Core.Release();
         }
     }
     {
         Transfrom _transform;
         Animator  _animator;
-        float _angle = 0;
 
         protected override void Init()
         {
 
         protected override void Update(float dt)
         {
-            // _angle -= dt;
-            // _transform.Rotation = Quat.FromAxisAngle(Vec3.UnitY, _angle);
-
             float f = _animator.GetFloat("MoveDir");
             if (Input.IsKeyDown(Key.Up))
                 f += .005f;
         }
         
     }
-
-
-
 }

SharpHammer/Animator.cs

         #region Methods
         public void SetBool(string name, bool value)
         {
-            NativeCalls.cmp_animchar_setparamb(_handle, name, value ? 1u : 0u);
+            NativeCalls.cmp_animchar_setparamb(_handle, name, value ? 1 : 0);
         }
 
         public bool GetBool(string name)

SharpHammer/App.cs

+/*****************************************************************************
+ * The MIT License (MIT)
+ * Copyright (c) 2014 - mohamad amin valinejad.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ * 
+ * **************************************************************************/
+
+/* Author: Sepehr taghdisian
+ * Decoupled App and Engine for the purpose of cleaner design 
+ */
+
+using System;
+using System.Windows.Forms;
+
+namespace SharpHammer
+{
+    public static class App
+    {
+        static bool _init = false;
+
+        public delegate void UpdateHandler();
+        public static event UpdateHandler Update;
+
+        public delegate void ResizeHandler(uint width, uint height);
+        public static event ResizeHandler Resize;
+
+        public delegate void KeypressHandler(byte charcode, uint vkeycode);
+        public static event KeypressHandler Keypress;
+
+        public delegate void ActivateHandler(int active);
+        public static event ActivateHandler Activate;
+
+        #region Methods
+        public static void Init(string title, Config cfg)
+        {
+            int r = NativeCalls.app_init(title, cfg._params);
+            if (NativeCalls.IS_FAIL(r))
+                throw new Exception(NativeCalls.err_getstring());
+
+            Input.Init();
+            ShowWindow();
+
+            NativeCalls.app_window_setupdatefn(OnUpdate);
+            NativeCalls.app_window_setactivefn(OnActivate);
+            NativeCalls.app_window_setresizefn(OnResize);
+            NativeCalls.app_window_setkeypressfn(OnKeypress);
+
+            _init = true;
+        }
+
+        public static void InitD3D(string title, Config cfg, Control window)
+        {
+            IntPtr hwnd = window.Handle;
+            int r = NativeCalls.app_d3d_initdev(hwnd, title, cfg._params);
+            if (NativeCalls.IS_FAIL(r))
+                throw new Exception(NativeCalls.err_getstring());
+        }
+
+        public static void Release()
+        {
+            NativeCalls.app_release();
+            _init = false;
+        }
+
+        public static void SwapBuffers()
+        {
+            NativeCalls.app_window_swapbuffers();
+        }
+
+        public static void ShowWindow()
+        {
+            if (_init)
+                NativeCalls.app_window_show();
+        }
+
+        public static void HideWindow()
+        {
+            if (_init)
+                NativeCalls.app_window_hide();
+        }
+
+        public static void ResizeView(uint width, uint height)
+        {
+            NativeCalls.app_window_resize(width, height);
+        }
+
+        public static void Run()
+        {
+            if (_init)
+                NativeCalls.app_window_run();
+        }
+
+        private static NativeCalls.pfn_app_update OnUpdate = () =>
+            {
+                if (Update != null)
+                    Update();
+            };
+
+        private static NativeCalls.pfn_app_resize OnResize = (width, height) =>
+            {
+                if (Resize != null)
+                    Resize(width, height);
+            };
+
+        private static NativeCalls.pfn_app_keypress OnKeypress = (charcode, vkey) =>
+            {
+                if (Keypress != null)
+                    Keypress(charcode, vkey);
+            };
+
+        private static NativeCalls.pfn_app_active OnActivate = (active) =>
+            {
+                if (Activate != null)
+                    Activate(active);
+            };
+
+        #endregion
+    }
+}

SharpHammer/Config.cs

                 return null;
             }
             Config cfg = new Config();
-            cfg._params = NativeCalls.app_load_config(path);
+            cfg._params = NativeCalls.app_config_load(path);
             if (cfg._params == IntPtr.Zero) {
                 Log.Print(LogType.Warning, "Invalid file format. | " + path);
                 return null;
         protected override void OnDisposeUnmanaged()
         {
             if (_params != IntPtr.Zero)
-                NativeCalls.app_unload_config(_params);
+                NativeCalls.app_config_unload(_params);
             if (_tmpStr != IntPtr.Zero)
                 Marshal.FreeHGlobal(_tmpStr);
         }
         internal static void init()
         {
             _default_cfg = new Config() {
-                _params = NativeCalls.app_defaultconfig()
+                _params = NativeCalls.app_config_default()
             };
         }
 

SharpHammer/Input.cs

 {
     public static class Input
     {
+        static bool _init = false;
+
         #region Methods
         public static bool IsKeyDown(Key key)
         {
-            return (NativeCalls.input_kb_getkey(key, 0) != 0);
+            if (_init)
+                return (NativeCalls.input_kb_getkey(key, 0) != 0);
+            else
+                return false;
         }
 
         public static bool IsKeyOnce(Key key)
         {
-            return !_mPrvKeybuf[(int)key] && _mKeybuf[(int)key];
-            //return (NativeCalls.input_kb_getkey(key, 1) != 0);
+            if (_init)
+                return (NativeCalls.input_kb_getkey(key, 1) != 0);
+            else
+                return false;
         }
 
         public static bool IsMouseDown(MouseKey key)
         {
-            return _mKeybuf[(int)key];
-            //return (NativeCalls.input_mouse_getkey(key, 0) != 0);
+            if (_init)
+                return (NativeCalls.input_mouse_getkey(key, 0) != 0);
+            else
+                return false;
         }
 
         public static bool IsMouseOnce(MouseKey key)
         {
-            return (NativeCalls.input_mouse_getkey(key, 1) != 0);
+            if (_init)
+                return (NativeCalls.input_mouse_getkey(key, 1) != 0);
+            else
+                return false;
+        }
+
+        public static void LockCursor()
+        {
+            Vec2 mpos = MousePosition;
+            NativeCalls.input_mouse_lockcursor((int)mpos.X, (int)mpos.Y);
+        }
+
+        public static void UnlockCursor()
+        {
+            NativeCalls.input_mouse_unlockcursor();
         }
 
         internal static void Init()
         {
-            _mKeyMap.Add(MouseButtons.Left, MouseKey.Left);
-            _mKeyMap.Add(MouseButtons.Middle, MouseKey.Middle);
-            _mKeyMap.Add(MouseButtons.Right, MouseKey.Right);
-
-            Engine.MainWnd.MouseDown += (s, e) => {
-                MouseKey value;
-                if (_mKeyMap.TryGetValue(e.Button, out value)) {
-                    _mKeybuf[(int)value] = true;
-                }
-            };
-
-            Engine.MainWnd.MouseUp += (s, e) => {
-                MouseKey value;
-                if (_mKeyMap.TryGetValue(e.Button, out value)) {
-                    _mKeybuf[(int)value] = false;
-                }
-            };
-        }
-
-        internal static void PostUpdate()
-        {
-            for (int i = 0; i < _mKeybuf.Length; i++)
-                _mPrvKeybuf[i] = _mKeybuf[i];
+            _init = true;
         }
 
         internal static void Update()
         {
-            NativeCalls.input_update();
+            if (_init)
+                NativeCalls.input_update();
         }
+
         #endregion
 
         #region Properties
             }
         }
         #endregion
-
-        #region Fields
-        static bool[] _mKeybuf = new bool[3];
-        static Dictionary<MouseButtons, MouseKey> _mKeyMap = new Dictionary<MouseButtons, MouseKey>();
-        static bool[] _mPrvKeybuf = new bool[3];
-        #endregion
     }
 
     public enum Key : int

SharpHammer/Log.cs

         public static bool ConsoleOutput
         {
             get { return NativeCalls.log_isconsole() != 0u; }
-            set { NativeCalls.log_outputconsole(value ? 1u : 0u); }
+            set { NativeCalls.log_outputconsole(value ? 1 : 0); }
         }
 
         public static void SetFileOutput(string path)
         {
             if (path == null)
-                NativeCalls.log_outputfile(0u, "");
+                NativeCalls.log_outputfile(0, "");
             else
-                NativeCalls.log_outputfile(1u, path);
+                NativeCalls.log_outputfile(1, path);
         }
 
         public static bool GetFileOutput()

SharpHammer/NativeCalls.cs

 #if DEBUG
         const string _coreDll   = "dhcore-dbg.dll";
         const string _engineDll = "dheng-dbg.dll";
+        const string _appDll = "dhapp-dbg.dll";
 #else
 		const string _coreDll = "dhcore.dll";
 		const string _engineDll = "dheng.dll";
+        const string _appDll = "dhapp.dll"
 #endif
         internal static bool IS_FAIL(int r) { return r <= 0; }
 
 
         #region core
         [DllImport(_coreDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int core_init(int trace_mem);
+        public static extern int core_init(uint flags);
         [DllImport(_coreDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern void core_release(int report_leaks);
         #endregion
 
         #region io
         [DllImport(_coreDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void fio_addvdir([In] [MarshalAs(UnmanagedType.LPStr)] string directory, uint monitor);
+        public static extern void fio_addvdir([In] [MarshalAs(UnmanagedType.LPStr)] string directory, int monitor);
         #endregion
 
         #region timer
         [DllImport(_coreDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern IntPtr timer_create(uint start);
+        public static extern IntPtr timer_create(int start);
         [DllImport(_coreDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern void timer_destroy(IntPtr tm);
         #endregion
         #region dh_engine
 
         #region delegates
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         public delegate void pfn_app_update();
-        public delegate void pfn_app_keypress([In] [MarshalAs(UnmanagedType.LPStr)] string wnd_name, byte charcode, uint vkeycode);
-        public delegate void pfn_app_active([In] [MarshalAs(UnmanagedType.LPStr)] string wnd_name, uint active);
-        public delegate void pfn_app_resize([In] [MarshalAs(UnmanagedType.LPStr)] string wnd_name, uint width, uint height);
-        #endregion
 
-        #region app
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern IntPtr app_load_config([In] [MarshalAs(UnmanagedType.LPStr)] string cfg_jsonfile);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        public delegate void pfn_app_keypress(byte charcode, uint vkeycode);
 
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern IntPtr app_defaultconfig();
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        public delegate void pfn_app_active(int active);
 
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_unload_config(IntPtr cfg);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int app_init([In] [MarshalAs(UnmanagedType.LPStr)] string name, [In] IntPtr @params, IntPtr wnd_override);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_release();
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_set_resizefunc(pfn_app_resize fn);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_set_activefunc(pfn_app_active fn);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_set_keypressfunc(pfn_app_keypress fn);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_set_updatefunc(pfn_app_update fn);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_show_window(IntPtr wnd_name);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void app_update();
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int app_resize_window(IntPtr name, uint width, uint height);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern uint app_get_wndwidth();
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern uint app_get_wndheight();
-
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        public delegate void pfn_app_resize(uint width, uint height);
         #endregion
 
         #region eng
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern void eng_send_guimsgs(byte c, uint vkey);
 
+        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl, 
+            EntryPoint = "eng_get_sharedir")]
+        private static extern IntPtr _eng_get_sharedir();
+
+        public static string eng_get_sharedir()
+        {
+            return Marshal.PtrToStringAnsi(_eng_get_sharedir());
+        }
+        #endregion
+
+        #region gfx
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void gfx_set_gridcallback(uint enable);
+        public static extern void gfx_set_gridcallback(int enable);
+
+        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void gfx_resize(uint width, uint height);
+
         #endregion
 
         #region scene
 
         #region camera
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void cam_init(IntPtr cam, [In] ref Vec4 pos, [In] ref Vec4 lookat, float fnear, float ffar, float fov);
+        public static extern void cam_init(IntPtr cam, [In] ref Vec4 pos, [In] ref Vec4 lookat, 
+            float fnear, float ffar, float fov);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern IntPtr cam_create([In] ref Vec4 pos, [In] ref Vec4 lookat, float fnear, float ffar, float fov);
+        public static extern IntPtr cam_create([In] ref Vec4 pos, [In] ref Vec4 lookat, 
+            float fnear, float ffar, float fov);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern void cam_destroy(IntPtr cam);
         { return Marshal.AllocHGlobal(208); }
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void cam_fps_init(IntPtr cfps, ref Vec4 pos, ref Vec4 lookat, float fnear, float ffar, float fov);
+        public static extern void cam_fps_init(IntPtr cfps, ref Vec4 pos, ref Vec4 lookat, 
+            float fnear, float ffar, float fov);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern void cam_set_viewsize(IntPtr cam, float width, float height);
         #endregion
 
-        #region input
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void input_update();
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern IntPtr input_mouse_getpos(out vec2i pos);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern uint input_kb_getkey(Key key, uint once);
-
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern uint input_mouse_getkey(MouseKey mkey, uint once);
-
-        #endregion
-
         #region component
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern IntPtr cmp_findname([In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void cmp_animchar_setparamb(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, uint value);
+        public static extern void cmp_animchar_setparamb(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, int value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void cmp_animchar_setparami(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, int value);
+        public static extern void cmp_animchar_setparami(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, int value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern void cmp_animchar_setparamf(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, float value);
+        public static extern void cmp_animchar_setparamf(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, float value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern uint cmp_animchar_getparamb(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_animchar_getparamb(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_animchar_getparami(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_animchar_getparami(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern float cmp_animchar_getparamf(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern float cmp_animchar_getparamf(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern IntPtr cmp_findtype(UInt16 type);
         public static extern UInt16 cmp_gettype(IntPtr c);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern ulong cmp_create_instance(IntPtr c, IntPtr obj, uint flags, ulong parent_hdl, uint offset_in_parent);
+        public static extern ulong cmp_create_instance(IntPtr c, IntPtr obj, uint flags, 
+            ulong parent_hdl, uint offset_in_parent);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
         public static extern ulong cmp_findinstance(IntPtr chain, UInt16 type);
         public static extern ulong cmp_findinstance_bytype_inobj(IntPtr obj, UInt16 type);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_sets(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, [In] [MarshalAs(UnmanagedType.LPStr)] string value);
+        public static extern int cmp_value_sets(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "cmp_value_gets")]
-        public static extern int _cmp_value_gets(IntPtr rs, uint rs_sz, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int _cmp_value_gets(IntPtr rs, uint rs_sz, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         public static string cmp_value_gets(ulong hdl, string name)
         {
         }
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_setf(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, float value);
+        public static extern int cmp_value_setf(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, float value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_getf(out float rf, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_value_getf(out float rf, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_seti(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, int value);
+        public static extern int cmp_value_seti(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, int value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_geti(out int rn, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_value_geti(out int rn, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_setui(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, uint value);
+        public static extern int cmp_value_setui(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, uint value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_getui(out uint rn, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_value_getui(out uint rn, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_set4f(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Vec4 value);
+        public static extern int cmp_value_set4f(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Vec4 value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_get4f(out Vec4 rfv, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_value_get4f(out Vec4 rfv, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_set3f(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Vec3 value);
+        public static extern int cmp_value_set3f(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Vec3 value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_get3f(out Vec3 rfv, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_value_get3f(out Vec3 rfv, ulong hdl,
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_set2f(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Vec2 value);
+        public static extern int cmp_value_set2f(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Vec2 value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_get2f(out Vec3 rfv, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_value_get2f(out Vec3 rfv, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "cmp_value_setb")]
-        private static extern int _cmp_value_setb(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, uint value);
+        private static extern int _cmp_value_setb(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, int value);
 
         public static int cmp_value_setb(ulong hdl, string name, bool value)
-        { return _cmp_value_setb(hdl, name, value ? 1u : 0u); }
+        { return _cmp_value_setb(hdl, name, value ? 1 : 0); }
 
-        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "cmp_modifyb")]
-        private static extern int _cmp_value_getb(out uint rb, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "cmp_value_getb")]
+        private static extern int _cmp_value_getb(out uint rb, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         public static bool cmp_value_getb(ulong hdl, string name)
         {
         }
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_set3m(ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Matrix4 value);
+        public static extern int cmp_value_set3m(ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, ref Matrix4 value);
 
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
-        public static extern int cmp_value_get3m(out Matrix4 rm, ulong hdl, [In] [MarshalAs(UnmanagedType.LPStr)] string name);
+        public static extern int cmp_value_get3m(out Matrix4 rm, ulong hdl, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name);
 
         #region animation
         [DllImport(_engineDll, CallingConvention = CallingConvention.Cdecl)]
 
         #endregion
 
+        #region dh_app
+        #region app
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern IntPtr app_config_load(
+            [In] [MarshalAs(UnmanagedType.LPStr)] string cfg_jsonfile);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern IntPtr app_config_default();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_config_unload(IntPtr cfg);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern int app_init([In] [MarshalAs(UnmanagedType.LPStr)] string name, 
+            [In] IntPtr @params);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern int app_d3d_initdev([In] IntPtr hwnd, 
+            [In] [MarshalAs(UnmanagedType.LPStr)] string name, [In] IntPtr @params);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_release();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_setresizefn(pfn_app_resize fn);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_setactivefn(pfn_app_active fn);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_setkeypressfn(pfn_app_keypress fn);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_setupdatefn(pfn_app_update fn);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_show();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_hide();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_update();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern int app_window_resize(uint width, uint height);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern uint app_window_getwidth();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern uint app_window_getheight();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_swapbuffers();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void app_window_run();
+        #endregion
+
+        #region input
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void input_update();
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern IntPtr input_mouse_getpos(out vec2i pos);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern int input_kb_getkey(Key key, int once);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern int input_mouse_getkey(MouseKey mkey, int once);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void input_mouse_lockcursor(int x, int y);
+
+        [DllImport(_appDll, CallingConvention = CallingConvention.Cdecl)]
+        public static extern void input_mouse_unlockcursor();
+
+        #endregion
+        
+        #endregion
     }
 
     internal enum variant_type

SharpHammer/SharpHammer.csproj

   <ItemGroup>
     <Compile Include="Animation.cs" />
     <Compile Include="Animator.cs" />
+    <Compile Include="App.cs" />
     <Compile Include="Behavior.cs" />
     <Compile Include="Bounds.cs" />
-    <Compile Include="DepCamera.cs" />
     <Compile Include="Camera.cs" />
     <Compile Include="Color4.cs" />
     <Compile Include="Component.cs" />

SharpHammer/core.cs

         public static void Release(bool reportLeaks = true)
         {
             Config.release();
-            NativeCalls.core_release(reportLeaks ? 1u : 0u);
+            NativeCalls.core_release(reportLeaks ? 1 : 0);
         }
     }
 

SharpHammer/engine.cs

             _name = "global"
         };
 
-        public static event UpdateHandler Update;
-        //public event ActivateHandler Activate;
-        public static event ResizeHandler Resize;
-
         #region Methods
         public static void Init(string title, Config cfg)
         {
-            Init(title, cfg, null);
-        }
-
-        public static void Init(string title, Config cfg, Control control)
-        {
             int r;
-            IntPtr hwnd;
-            if (control == null) {
-                _wnd = new RenderForm();
-                hwnd = _wnd.Handle;
-            } else {
-                _wnd = control;
-                hwnd = control.Handle;
-            }
-            r = NativeCalls.app_init(title, cfg._params, hwnd);
-
-            if (NativeCalls.IS_FAIL(r))
-                throw new Exception("Failed to initialize app. error code : " + r);
             r = NativeCalls.eng_init(cfg._params);
             if (NativeCalls.IS_FAIL(r))
                 throw new Exception("Failed to initialize engine. error code : " + r);
             NativeCalls.gfx_set_gridcallback(1);
-            var _frm = _wnd as RenderForm;
-            if (_frm != null) {
-                _frm.Text = title;
-                int w = (int)NativeCalls.app_get_wndwidth();
-                int h = (int)NativeCalls.app_get_wndheight();
-                _frm.ClientSize = new System.Drawing.Size(w, h);
-                _frm.StartPosition = FormStartPosition.CenterScreen;
-            }
-            Input.Init();
-            NativeCalls.input_update();
             Scene.SetupVars();
 
             _init = true;
         }
 
-        public static void Run()
-        {
-            var _frm = _wnd as RenderForm;
-            if (_frm != null) {
-                _frm.ResizeWindow = OnResize;
-                _frm.UpdateFrame = OnUpdate;
-                System.Windows.Forms.Application.Run(_frm);
-            } else {
-                Application.Idle += Application_Idle;
-            }
-        }
-
         public static void Release()
         {
             _init = false;
-            if (_wnd != null) {
-                var _frm = _wnd as RenderForm;
-                if (_frm != null)
-                    _frm.Close();
-                _wnd.Dispose();
-            }
-
             if (_active_scene != null)
                 _active_scene.Dispose();
             NativeCalls.eng_release();
-            NativeCalls.app_release();
         }
 
-        static void Application_Idle(object sender, EventArgs e)
-        {
-            while (AppStillIdle) {
-                OnUpdate();
-            }
-        }
-
-        public static void OnUpdate()
+        public static void Update()
         {
             if (!_init)
                 return;
                 }
             }
 
-            if (Update != null)
-                Update(dt);
-
             NativeCalls.eng_update();
-            Input.PostUpdate();
         }
 
-        static void OnResize(int width, int height)
+        public static void ResizeView(uint w, uint h)
         {
-            if (_wnd != null) {
-                SetViewSize(_wnd.ClientSize.Width, _wnd.ClientSize.Height);
-                if (Resize != null)
-                    Resize(width, height);
-            }
-        }
-
-        public static void SetViewSize(int w, int h)
-        {
-            //	if (ActiveCamera != null)
-            //		NativeCalls.cam_set_viewsize(ActiveCamera._cam, (float)w, (float)h);
-
-            NativeCalls.app_resize_window(IntPtr.Zero, (UInt32)w, (UInt32)h);
+            if (_init)
+                NativeCalls.gfx_resize(w, h);
         }
 
         public static Scene CreateScene(string name)
             return new Scene(name);
         }
 
-        public static void AddVdir(string path, bool monitor)
+        public static void AddVirtualPath(string path, bool monitor = false)
         {
-            NativeCalls.fio_addvdir(path, monitor ? 1u : 0u);
+            NativeCalls.fio_addvdir(path, monitor ? 1 : 0);
         }
 
-        public static void LockCursor(Vec2 pos)
+        public static void SendKeys(byte charcode, uint vkey)
         {
-            if (_wnd.Focused)
-                Cursor.Position = _wnd.PointToScreen(new System.Drawing.Point((int)pos.X, (int)pos.Y));
+            NativeCalls.eng_send_guimsgs(charcode, vkey);
         }
+
         #endregion
 
         #region Properties
             get { return _wnd.Focused; }
         }
 
+        public static string ShareDir
+        {
+            get { return NativeCalls.eng_get_sharedir(); }
+        }
+
         public static Scene GlobalScene
         {
             get { return _global_scene; }
                     NativeCalls.eng_resume();
             }
         }
-
-        public static Vec2 ScreenSize
-        {
-            get { return new Vec2((float)_wnd.ClientSize.Width, (float)_wnd.ClientSize.Height); }
-        }
-
-        internal static Control MainWnd
-        {
-            get { return _wnd; }
-        }
-        #endregion
-
-        #region FastLoop
-        static bool AppStillIdle
-        {
-            get
-            {
-                Message msg;
-                return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
-            }
-        }
-
-        [System.Security.SuppressUnmanagedCodeSecurity]
-        [DllImport("user32.dll", CharSet = CharSet.Auto)]
-        [return: MarshalAs(UnmanagedType.Bool)]
-        private static extern bool PeekMessage(out Message msg, IntPtr hWnd,
-            uint messageFilterMin, uint messageFilterMax, uint flags);
-
-        [StructLayout(LayoutKind.Sequential)]
-        struct Message
-        {
-            public IntPtr hWnd;
-            public uint msg;
-            public IntPtr wParam;
-            public IntPtr lParam;
-            public uint time;
-            public System.Drawing.Point p;
-        }
         #endregion
 
         #region RenderForm
 
             void Application_Idle(object sender, EventArgs e)
             {
-                while (AppStillIdle) {
+                while (Idle) {
                     if (UpdateFrame != null)
                         UpdateFrame();
                 }
             protected override void OnPaintBackground(PaintEventArgs e)
             { }
 
+            #region FastLoop
+            static bool Idle
+            {
+                get
+                {
+                    Message msg;
+                    return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
+                }
+            }
+
+            [System.Security.SuppressUnmanagedCodeSecurity]
+            [DllImport("user32.dll", CharSet = CharSet.Auto)]
+            [return: MarshalAs(UnmanagedType.Bool)]
+            private static extern bool PeekMessage(out Message msg, IntPtr hWnd,
+                uint messageFilterMin, uint messageFilterMax, uint flags);
+
+            [StructLayout(LayoutKind.Sequential)]
+            struct Message
+            {
+                public IntPtr hWnd;
+                public uint msg;
+                public IntPtr wParam;
+                public IntPtr lParam;
+                public uint time;
+                public System.Drawing.Point p;
+            }
+            #endregion
+
         }
         #endregion
     }
 
     #region Delegates
-    public delegate void UpdateHandler(float dt);
     public delegate void ResizeHandler(int width, int height);
     public delegate void ActivateHandler(bool active);
     #endregion