Autor Beitrag
Kasko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 126
Erhaltene Danke: 1

Win 10
C# C++ (VS 2017/19), (Java, PHP)
BeitragVerfasst: Sa 27.10.18 15:57 
Ich wollte eine Methode, welche jeden Frame aufgerufen wird. Umgesetzt habe ich das in der Application_Idle Methode (App-Klasse). Wenn ich jetzt aber probiere die Frame Time zu berechnen, werden dort viel zu kleine Werte berechnet (kleiner als 0.0003 Sekunden). Wisst ihr woran das liegt und wie ich den Fehler beheben kann, sodass korrekte Werte ausgeben werden.


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        App app = new App(new Console(13040), 13040"Program");
    }
}


ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
public class App {
    [StructLayout(LayoutKind.Sequential)]
    private struct Message {
        public IntPtr hWnd;
        public int msg;
        public IntPtr wParam;
        public IntPtr lParam;
        public uint time;
        public Point p;
    }

    [return: MarshalAs(UnmanagedType.Bool)]
    [SuppressUnmanagedCodeSecurity, DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);

    private Console console;
    private StateMachine stateMachine;

    public App(Console console, int width, int height, string title) {
        this.console = console;
        this.console.Text = title;

        stateMachine = new StateMachine();

        Run(width, height);
    }

    public void Run(int width, int height) {
        Application.Idle += Application_Idle;
        stateMachine.AddState(new SplashState(width, height), false);
        Application.Run(console);
    }

    private void Application_Idle(object sender, EventArgs e) {
        Message msg;
        Time.startTime = DateTime.Now;

        while(!PeekMessage(out msg, IntPtr.Zero, 000)) {
            float elapsed = (float)DateTime.Now.Subtract(Time.startTime).TotalSeconds;

            Time.deltaTime = elapsed;
            Time.totalElapsedTime += elapsed;
            Time.startTime = DateTime.Now;

            stateMachine.GetActiveState().Update();
            stateMachine.GetActiveState().Render(console);
        }
    }
}