Exemple #1
0
        public void Initialize()
        {
            string vertShader =
                @"attribute vec3 vertPosition;
attribute vec4 vertColor;
attribute vec2 vertTexCoord;

varying vec4 fragColor;
varying vec2 fragTexCoord;

uniform mat4 vertTransform; 

void main()
{
    gl_Position = vertTransform * vec4(vertPosition, 1.0);
    fragColor = vertColor;
    fragTexCoord = vertTexCoord;
}";

            string fragShader =
                @"precision mediump float;

varying vec4 fragColor;
varying vec2 fragTexCoord;

uniform sampler2D fragTexture;

void main()
{
    gl_FragColor = texture2D(fragTexture, fragTexCoord) * fragColor;
}";

            uint vertexShader   = GLUtils.CompileShader(vertShader, GL_VERTEX_SHADER);
            uint fragmentShader = GLUtils.CompileShader(fragShader, GL_FRAGMENT_SHADER);

            _program = glCreateProgram();
            if (_program == 0)
            {
                throw new InvalidOperationException("Failed to create program.");
            }

            glAttachShader(_program, vertexShader);
            glAttachShader(_program, fragmentShader);

            glBindAttribLocation(_program, 0, "vertPosition");
            glBindAttribLocation(_program, 1, "vertColor");
            glBindAttribLocation(_program, 2, "vertTexCoord");
            GLUtils.LinkProgram(_program);

            _vertTransformLocation = glGetUniformLocation(_program, "vertTransform");
            _fragTextureLocation   = glGetUniformLocation(_program, "fragTexture");

            glEnable(GL_BLEND);
            glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
            glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
        }
Exemple #2
0
        public Game()
        {
            var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location ?? "");

            Directory.SetCurrentDirectory(basePath);

            if (glfwInit() == 0)
            {
                throw new InvalidOperationException("Failed to initialize GLFW.");
            }

            glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

            _window = glfwCreateWindow(1024, 768, "SameGame", IntPtr.Zero, IntPtr.Zero);

            if (_window == IntPtr.Zero)
            {
                throw new InvalidOperationException("Failed to create window.");
            }

            _windowSizeCallback = (a, b, c) => OnWindowSize(a, b, c);
            glfwSetWindowSizeCallback(_window, _windowSizeCallback);

            _keyboardCallback = (a, b, c, d, e) => OnKeyboard(a, b, c, d, e);
            glfwSetKeyCallback(_window, _keyboardCallback);

            _mouseMoveCallback = (a, b, c) => OnMouseMove(a, b, c);
            glfwSetCursorPosCallback(_window, _mouseMoveCallback);

            _mouseButtonCallback = (a, b, c, d) => OnMouseButton(a, b, c, d);
            glfwSetMouseButtonCallback(_window, _mouseButtonCallback);

            GLUtils.CreateContext(_window, out _display, out _surface);

            Graphics = new Graphics(this);

            BoardRenderer = new BoardRenderer(Graphics);

            Board = new Board(new RNG(100));
        }