Skip to content

add direct x12 support for c# #52763

@gabriel123495

Description

@gabriel123495

example:

dotnet new dx2d12 -n apiexample

code:

using System;
using DirectX12; 
using DirectX12.Graphics2D;

class PongGame : Game
{
    Paddle leftPaddle;
    Paddle rightPaddle;
    Ball ball;

    public override void Initialize()
    {
        leftPaddle = new Paddle(50, 200, 20, 100);
        rightPaddle = new Paddle(730, 200, 20, 100);
        ball = new Ball(400, 240, 10, 4, 4);
    }

    public override void Update(GameTime time)
    {
        ball.X += ball.DX;
        ball.Y += ball.DY;

        if (ball.Y < 0 || ball.Y > 480) ball.DY *= -1;

        if (ball.Intersects(leftPaddle) || ball.Intersects(rightPaddle))
            ball.DX *= -1;

        if (Keyboard.IsKeyDown(Keys.W)) leftPaddle.Y -= 5;
        if (Keyboard.IsKeyDown(Keys.S)) leftPaddle.Y += 5;
        if (Keyboard.IsKeyDown(Keys.Up)) rightPaddle.Y -= 5;
        if (Keyboard.IsKeyDown(Keys.Down)) rightPaddle.Y += 5;
    }

    public override void Draw(Graphics2D g)
    {
        g.Clear(Color.Black);

        g.FillRectangle(Color.White, leftPaddle.Bounds);
        g.FillRectangle(Color.White, rightPaddle.Bounds);
        g.FillCircle(Color.White, ball.X, ball.Y, ball.Radius);
    }
}

class Paddle {
    public float X, Y, Width, Height;
    public Paddle(float x, float y, float w, float h) { X=x; Y=y; Width=w; Height=h; }
    public RectangleF Bounds => new RectangleF(X, Y, Width, Height);
}

class Ball {
    public float X, Y, Radius, DX, DY;
    public Ball(float x, float y, float r, float dx, float dy) { X=x; Y=y; Radius=r; DX=dx; DY=dy; }
    public bool Intersects(Paddle p) =>
        X - Radius < p.X + p.Width && X + Radius > p.X &&
        Y > p.Y && Y < p.Y + p.Height;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    untriagedRequest triage from a team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions