Example #1
0
 public Shader(string name, string vertexShaderPath, string pixelShaderPath)
 {
     Name             = name;
     VertexShaderPath = Assets.LoadPath(vertexShaderPath);
     PixelShaderPath  = Assets.LoadPath(pixelShaderPath);
 }
Example #2
0
        static void Main(string[] args)
        {
            if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO) < 0)
            {
                Console.WriteLine("Error initializing SDL");
                SDL.SDL_Quit();
                return;
            }

            // LUNA: Initialize with OpenGL 3.2, so we can debug graphics with RenderDoc
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 2);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);

            var window = SDL.SDL_CreateWindow(
                "Disaster Engine Again",
                SDL.SDL_WINDOWPOS_UNDEFINED,
                SDL.SDL_WINDOWPOS_UNDEFINED,
                ScreenController.windowWidth, ScreenController.windowHeight,
                SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL
                );

            if (window == IntPtr.Zero)
            {
                Console.WriteLine($"There was an issue creating the window. {SDL.SDL_GetError()}");
            }

            var renderer = SDL.SDL_CreateRenderer(
                window,
                -1,
                SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
                SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC
                );

            if (renderer == IntPtr.Zero)
            {
                Console.WriteLine($"There was an issue creating the renderer. {SDL.SDL_GetError()}");
            }

            SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "0");

            Console.WriteLine($"Welcome to disaster engine");

            var frameStart = DateTime.UtcNow.Ticks;

            // software renderer initialisation
            Draw.InitTexture(renderer, 320, 240);
            LoadConfig();
            Draw.LoadFont(Assets.LoadPath("fontsmall.png"));

            screen = new ScreenController(window);
            LoadingMessage("disaster engine 5.0");
            LoadingMessage("(c) jazz mickle ultramegacorp 2021");
            LoadingMessage("initialised screen");
            var js = new JS();

            double ms    = 0;
            int    frame = 0;

            LoadingMessage("building input collection");
            DisasterAPI.Input.keyState = new System.Collections.Generic.Dictionary <SDL.SDL_Keycode, (bool down, bool held, bool up)>();

            LoadingMessage("opening audio");
            SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 1024);

            LoadingMessage("complete");

            System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.LowLatency;

            long ticks;
            long t;

            Debug.enabled = false;

            while (running)
            {
                frame += 1;

                Debug.FrameStart();

                ticks      = DateTime.UtcNow.Ticks;
                t          = ticks - frameStart;
                ms         = t / 10000.0;
                frameStart = ticks;

                while (SDL.SDL_PollEvent(out SDL.SDL_Event e) == 1)
                {
                    switch (e.type)
                    {
                    case SDL.SDL_EventType.SDL_QUIT:
                        running = false;
                        break;

                    case SDL.SDL_EventType.SDL_KEYDOWN:
                        DisasterAPI.Input.keyState[e.key.keysym.sym] = (true, true, DisasterAPI.Input.GetKeyUp(e.key.keysym.sym));
                        break;

                    case SDL.SDL_EventType.SDL_KEYUP:
                        DisasterAPI.Input.keyState[e.key.keysym.sym] = (DisasterAPI.Input.GetKeyDown(e.key.keysym.sym), false, true);
                        break;
                    }
                }

                Debug.Label("sdl events");

                double delta = ms * .001 * timescale;
                js.Update(delta);

                Debug.Label("js update");
                Debug.DrawGraph();
                screen.Update();

                Debug.Label("render update");

                DisasterAPI.Input.Clear();

                SDL.SDL_SetWindowTitle(window, $"DISASTER ENGINE 5 -- MS: {Math.Floor(ms)}");

                if (ms > 20)
                {
                    //Console.WriteLine("HElo");
                    System.Diagnostics.Debugger.Log(0, "hitch", $"took {ms} this frame\n");
                }
                //if (frame % 30 == 0)

                if (GC.GetTotalMemory(false) > 10000000)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }

                Debug.Label("gc");

                Debug.FrameEnd();
                Debug.GetFrameMSData();
            }

            screen.Done();

            // Clean up the resources that were created.
            Assets.Dispose();

            SDL_mixer.Mix_CloseAudio();
            SDL.SDL_DestroyRenderer(renderer);
            SDL.SDL_DestroyTexture(Draw.drawTexture);
            SDL.SDL_DestroyWindow(window);
            SDL.SDL_Quit();
        }
Example #3
0
        //public unsafe static ObjectInstance Object(Model model)
        //{
        //var mesh = ((Mesh*)model.meshes)[0];
        //var output = JS.instance.engine.Object.Construct();
        //output.SetPropertyValue("position", Object(model.position), true);
        //output.SetPropertyValue("direction", Object(model.direction), true);
        //return output;
        //}

        public static Model Model(ObjectInstance input)
        {
            Mesh output = new Mesh();

            // necessary stuff
            var vertices = Vector3Array((ObjectInstance)input.GetPropertyValue("vertices"));

            output.vertexCount = vertices.Length;
            output.vertices    = ArrayPointer(vertices);

            // optionals
            if (input.HasProperty("indices"))
            {
                var indices = ShortArray((ObjectInstance)input.GetPropertyValue("indices"));
                output.triangleCount = indices.Length / 3;
                output.indices       = ArrayPointer(indices);
            }

            if (input.TryGetPropertyValue("uvs", out object uvs))
            {
                output.texcoords = ArrayPointer(Vector2Array((ObjectInstance)uvs));
            }
            if (input.TryGetPropertyValue("uv2s", out object uv2s))
            {
                output.texcoords2 = ArrayPointer(Vector2Array((ObjectInstance)uv2s));
            }
            if (input.TryGetPropertyValue("normals", out object normals))
            {
                output.normals = ArrayPointer(Vector3Array((ObjectInstance)normals));
            }
            if (input.TryGetPropertyValue("tangents", out object tangents))
            {
                output.tangents = ArrayPointer(Vector3Array((ObjectInstance)tangents));
            }
            if (input.TryGetPropertyValue("colors", out object colors))
            {
                output.colors = ArrayPointer(Color32Array((ObjectInstance)colors));
            }

            Raylib.UploadMesh(ref output, false);

            Model model = Raylib.LoadModelFromMesh(output);

            if (input.TryGetPropertyValue("texture", out object texturePath))
            {
                Texture2D texture;
                if (!Assets.LoadPath((string)texturePath, out _))
                {
                    texture = PixelBuffer.missing.texture;
                }
                else
                {
                    texture = Assets.PixelBuffer((string)texturePath).texture;
                }
                SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_DIFFUSE, ref texture);
            }
            else
            {
                var texture = PixelBuffer.missing.texture;
                SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_DIFFUSE, ref texture);
            }

            return(model);
        }
Example #4
0
        public ScreenController(IntPtr window)
        {
            this.window = window;

            var glcontext = SDL.SDL_GL_CreateContext(window);

            SDL.SDL_GL_SetSwapInterval(1);

            shader = new ShaderProgram(
                File.ReadAllText(Assets.LoadPath("outputvert.glsl")),
                File.ReadAllText(Assets.LoadPath("outputfrag.glsl"))
                );
            framebuffer = Gl.GenFramebuffer();
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);

            texture = Gl.GenTexture();
            Gl.UseProgram(shader);
            Gl.BindTexture(TextureTarget.Texture2D, texture);
            Gl.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, screenWidth, screenHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, IntPtr.Zero);
            Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureParameter.Nearest);
            Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureParameter.Nearest);

            var renderBuffer = Gl.GenRenderbuffer();

            Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderBuffer);

            Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent16, screenWidth, screenHeight);
            Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, renderBuffer);

            Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, texture, 0);
            Gl.DrawBuffer(DrawBufferMode.ColorAttachment0);

            var error = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);

            switch (error)
            {
            case FramebufferErrorCode.FramebufferComplete:
                break;

            default:
                Console.WriteLine($"fbo error: {error}");
                break;
            }

            vertices = new VBO <Vector3>(
                new Vector3[] {
                new Vector3(-1, -1, 0),
                new Vector3(1, -1, 0),
                new Vector3(1, 1, 0),
                new Vector3(-1, 1, 0),
            }
                );

            uvs = new VBO <Vector2>(
                new Vector2[] {
                new Vector2(0, 0),
                new Vector2(1, 0),
                new Vector2(1, 1),
                new Vector2(0, 1),
            }
                );

            triangles = new VBO <uint>(
                new uint[] {
                0, 1, 2, 0, 2, 3
            }, BufferTarget.ElementArrayBuffer
                );

            drawScreen = new DrawRenderer(
                new ShaderProgram(
                    File.ReadAllText(Assets.LoadPath("screenvert.glsl")),
                    File.ReadAllText(Assets.LoadPath("screenfrag.glsl"))
                    )
                );
        }
Example #5
0
        static void Main(string[] args)
        {
            Raylib.SetTraceLogLevel(TraceLogType.LOG_WARNING);
            Raylib.InitAudioDevice();

            Raylib.SetExitKey(KeyboardKey.KEY_F10);
            Console.WriteLine($"Welcome to disaster engine");

            // software renderer initialisation
            SoftwareCanvas.InitTexture(320, 240);
            LoadConfig();
            // TODO: bake in a default font! so you can't end up with no font at all
            if (Assets.LoadPath("lib/fontsmall.png", out string fontPath))
            {
                SoftwareCanvas.LoadFont(fontPath);
            }

            screen = new ScreenController();
            LoadingMessage("disaster engine 5.0");
            LoadingMessage("(c) jazz mickle ultramegacorp 2021");
            LoadingMessage("initialised screen");

            var js = new JS();

            Debug.enabled = false;

            while (!Raylib.WindowShouldClose())
            {
                Debug.FrameStart();
                js.Update(Raylib.GetFrameTime());
                Debug.Label("js update");
                Debug.DrawGraph();
                screen.Update();
                SoftwareCanvas.slowDraw = false;
                Debug.FrameEnd();
                Debug.GetFrameMSData();
            }

            screen.Done();
            Raylib.CloseAudioDevice();



            //double ms = 0;
            //int frame = 0;


            //System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.LowLatency;

            //long ticks;
            //long t;



            //while (running)
            //{
            //    frame += 1;

            //    Debug.FrameStart();

            //    ticks = DateTime.UtcNow.Ticks;
            //    t = ticks - frameStart;
            //    ms = t / 10000.0;
            //    frameStart = ticks;

            //    while (SDL.SDL_PollEvent(out SDL.SDL_Event e) == 1)
            //    {
            //        switch (e.type)
            //        {
            //            case SDL.SDL_EventType.SDL_QUIT:
            //                running = false;
            //                break;
            //            case SDL.SDL_EventType.SDL_TEXTINPUT:
            //                //DisasterAPI.Input._lastChar = e.text.text;
            //                break;
            //            case SDL.SDL_EventType.SDL_KEYDOWN:
            //                DisasterAPI.Input._anyKeyDown = true;
            //                //DisasterAPI.Input._lastChar = ((char)e.key.keysym.sym).ToString();
            //                DisasterAPI.Input.keyState[e.key.keysym.sym] = (true, true, DisasterAPI.Input.GetKeyUp(e.key.keysym.sym));
            //                break;
            //            case SDL.SDL_EventType.SDL_KEYUP:
            //                DisasterAPI.Input.keyState[e.key.keysym.sym] = (DisasterAPI.Input.GetKeyDown(e.key.keysym.sym), false, true);
            //                break;
            //        }
            //    }

            //    //Debug.Label("sdl events");

            //    double delta = ms * .001 * timescale;
            //    js.Update(delta);

            //    Debug.Label("js update");
            //    Debug.DrawGraph();
            //    screen.Update();

            //    //Debug.Label("render update");

            //    DisasterAPI.Input.Clear();

            //    SDL.SDL_SetWindowTitle(window, $"DISASTER ENGINE 5 -- MS: {Math.Floor(ms)}");

            //    if (ms > 20)
            //    {
            //        //Console.WriteLine("HElo");
            //        System.Diagnostics.Debugger.Log(0, "hitch", $"took {ms} this frame\n");
            //    }
            //    //if (frame % 30 == 0)

            //    //if (GC.GetTotalMemory(false) > 10000000)
            //    //{
            //    //    GC.Collect();
            //    //    GC.WaitForPendingFinalizers();
            //    //}

            //    //Debug.Label("gc");

            //    Debug.FrameEnd();
            //    Debug.GetFrameMSData();
            //}

            //screen.Done();

            //// Clean up the resources that were created.
            //Assets.Dispose();

            //SDL_mixer.Mix_CloseAudio();
            //SDL.SDL_DestroyRenderer(renderer);
            //SDL.SDL_DestroyTexture(SoftwareCanvas.drawTexture);
            //SDL.SDL_DestroyWindow(window);
            //SDL.SDL_Quit();
        }
Example #6
0
 public void ReloadShader()
 {
     drawScreen.shader = Assets.Shader("shaders/screen");
     postProcessShader = Assets.Shader("shaders/postprocess");
 }