Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,257 @@
package kha.graphics2;
import kha.Color;
import kha.FastFloat;
import kha.Font;
import kha.graphics4.PipelineState;
import kha.Image;
import kha.math.FastMatrix3;
class Graphics {
public function begin(clear: Bool = true, clearColor: Color = null): Void {}
public function end(): Void {}
public function flush(): Void {}
// scale-filtering
// draw/fillPolygon
public function clear(color: Color = null): Void {}
public function drawImage(img: Image, x: FastFloat, y: FastFloat): Void {
drawSubImage(img, x, y, 0, 0, img.width, img.height);
}
/**
* `sx, sy, sw, sh` arguments is the sub-rectangle of the source `img` image
*/
public function drawSubImage(img: Image, x: FastFloat, y: FastFloat, sx: FastFloat, sy: FastFloat, sw: FastFloat, sh: FastFloat): Void {
drawScaledSubImage(img, sx, sy, sw, sh, x, y, sw, sh);
}
/**
* `dx, dy, dw, dh` arguments is the rectangle to draw into the destination context
*/
public function drawScaledImage(img: Image, dx: FastFloat, dy: FastFloat, dw: FastFloat, dh: FastFloat): Void {
drawScaledSubImage(img, 0, 0, img.width, img.height, dx, dy, dw, dh);
}
/**
* `sx, sy, sw, sh` arguments is the sub-rectangle of the source `img` image
* `dx, dy, dw, dh` arguments is the rectangle to draw into the destination context
*/
public function drawScaledSubImage(img: Image, sx: FastFloat, sy: FastFloat, sw: FastFloat, sh: FastFloat, dx: FastFloat, dy: FastFloat, dw: FastFloat,
dh: FastFloat): Void {}
public function drawRect(x: Float, y: Float, width: Float, height: Float, strength: Float = 1.0): Void {}
public function fillRect(x: Float, y: Float, width: Float, height: Float): Void {}
/**
* Draw a single line of text with the current `color`, `font` and `fontSize` properties.
*
* When drawing into rendertargets, you might have to use a different shader than the default one
* - use the default shader when drawing into a transparent section of your rendertarget
* - use a shader with `alphaBlendSource = BlendOne` when drawing into a non-transparent section of your rendertarget
*/
public function drawString(text: String, x: Float, y: Float): Void {}
/**
* Draw a single line of characters with the current `color`, `font` and `fontSize` properties.
*
* When drawing into rendertargets, you might have to use a different shader than the default one
* - use the default shader when drawing into a transparent section of your rendertarget
* - use a shader with `alphaBlendSource = BlendOne` when drawing into a non-transparent section of your rendertarget
*/
public function drawCharacters(text: Array<Int>, start: Int, length: Int, x: Float, y: Float): Void {}
public function drawLine(x1: Float, y1: Float, x2: Float, y2: Float, strength: Float = 1.0): Void {}
public function drawVideo(video: Video, x: Float, y: Float, width: Float, height: Float): Void {}
public function fillTriangle(x1: Float, y1: Float, x2: Float, y2: Float, x3: Float, y3: Float): Void {}
public var imageScaleQuality(get, set): ImageScaleQuality;
public var mipmapScaleQuality(get, set): ImageScaleQuality;
function get_imageScaleQuality(): ImageScaleQuality {
return ImageScaleQuality.Low;
}
function set_imageScaleQuality(value: ImageScaleQuality): ImageScaleQuality {
return ImageScaleQuality.High;
}
function get_mipmapScaleQuality(): ImageScaleQuality {
return ImageScaleQuality.Low;
}
function set_mipmapScaleQuality(value: ImageScaleQuality): ImageScaleQuality {
return ImageScaleQuality.High;
}
/**
The color value is used for geometric primitives, images, and text. Remember to set it back to white to draw images unaltered.
*/
public var color(get, set): Color;
function get_color(): Color {
return Color.Black;
}
function set_color(color: Color): Color {
return Color.Black;
}
public var font(get, set): Font;
function get_font(): Font {
return null;
}
function set_font(font: Font): Font {
return null;
}
public var fontSize(get, set): Int;
function get_fontSize(): Int {
return myFontSize;
}
function set_fontSize(value: Int): Int {
return myFontSize = value;
}
public static var fontGlyphs: Array<Int> = [for (i in 32...256) i];
// works on the top of the transformation stack
public var transformation(get, set): FastMatrix3;
inline function get_transformation(): FastMatrix3 {
return transformations[transformationIndex];
}
inline function set_transformation(transformation: FastMatrix3): FastMatrix3 {
setTransformation(transformation);
transformations[transformationIndex].setFrom(transformation);
return transformation;
}
public inline function pushTransformation(trans: FastMatrix3): Void {
transformationIndex++;
if (transformationIndex == transformations.length) {
transformations.push(FastMatrix3.identity());
}
transformations[transformationIndex].setFrom(trans);
setTransformation(get_transformation());
}
public function popTransformation(): FastMatrix3 {
transformationIndex--;
if (transformationIndex == -1)
throw "There is no transformation matrix to remove, check your push/popTransformation code";
setTransformation(get_transformation());
return transformations[transformationIndex + 1];
}
public function scale(x: FastFloat, y: FastFloat): Void {
transformation.setFrom(kha.math.FastMatrix3.scale(x, y).multmat(transformation));
}
public function pushScale(x: FastFloat, y: FastFloat): Void {
final mat = FastMatrix3.scale(x, y).multmat(transformation);
pushTransformation(mat);
}
inline function translation(tx: FastFloat, ty: FastFloat): FastMatrix3 {
return FastMatrix3.translation(tx, ty).multmat(transformation);
}
public function translate(tx: FastFloat, ty: FastFloat): Void {
transformation.setFrom(translation(tx, ty));
}
public function pushTranslation(tx: FastFloat, ty: FastFloat): Void {
pushTransformation(translation(tx, ty));
}
inline function rotation(angle: FastFloat, centerx: FastFloat, centery: FastFloat): FastMatrix3 {
return FastMatrix3.translation(centerx, centery)
.multmat(FastMatrix3.rotation(angle))
.multmat(FastMatrix3.translation(-centerx, -centery))
.multmat(transformation);
}
public function rotate(angle: FastFloat, centerx: FastFloat, centery: FastFloat): Void {
transformation.setFrom(rotation(angle, centerx, centery));
}
public function pushRotation(angle: FastFloat, centerx: FastFloat, centery: FastFloat): Void {
pushTransformation(rotation(angle, centerx, centery));
}
public var opacity(get, set): Float; // works on the top of the opacity stack
public function pushOpacity(opacity: Float): Void {
setOpacity(opacity);
opacities.push(opacity);
}
public function popOpacity(): Float {
var ret = opacities.pop();
setOpacity(get_opacity());
return ret;
}
function get_opacity(): Float {
return opacities[opacities.length - 1];
}
function set_opacity(opacity: Float): Float {
setOpacity(opacity);
return opacities[opacities.length - 1] = opacity;
}
public function scissor(x: Int, y: Int, width: Int, height: Int): Void {}
public function disableScissor(): Void {}
#if sys_g4
private var pipe: PipelineState;
public var pipeline(get, set): PipelineState;
private function get_pipeline(): PipelineState {
return pipe;
}
private function set_pipeline(pipeline: PipelineState): PipelineState {
setPipeline(pipeline);
return pipe = pipeline;
}
#end
var transformations: Array<FastMatrix3>;
var transformationIndex: Int;
var opacities: Array<Float>;
var myFontSize: Int;
public function new() {
transformations = [FastMatrix3.identity()];
transformationIndex = 0;
opacities = [1];
myFontSize = 12;
#if sys_g4
pipe = null;
#end
}
function setTransformation(transformation: FastMatrix3): Void {}
function setOpacity(opacity: Float): Void {}
function setPipeline(pipeline: PipelineState): Void {}
}

View File

@ -0,0 +1,40 @@
package kha.graphics2;
import haxe.io.Bytes;
import kha.Canvas;
import kha.Color;
import kha.graphics4.TextureFormat;
import kha.graphics4.Usage;
import kha.Image;
class Graphics1 implements kha.graphics1.Graphics {
var canvas: Canvas;
var texture: Image;
var pixels: Bytes;
public function new(canvas: Canvas) {
this.canvas = canvas;
}
public function begin(): Void {
if (texture == null || (texture.realWidth != canvas.width || texture.realHeight != canvas.height)) {
texture = Image.create(canvas.width, canvas.height, TextureFormat.RGBA32, Usage.ReadableUsage);
}
pixels = texture.lock();
}
public function end(): Void {
texture.unlock();
canvas.g2.begin(false);
canvas.g2.drawImage(texture, 0, 0);
canvas.g2.end();
}
public function setPixel(x: Int, y: Int, color: Color): Void {
#if (kha_html5 || kha_krom)
pixels.setInt32(y * texture.stride + x * 4, Color.fromBytes(color.Bb, color.Gb, color.Rb, color.Ab));
#else
pixels.setInt32(y * texture.stride + x * 4, color);
#end
}
}

View File

@ -0,0 +1,370 @@
package kha.graphics2;
import kha.math.Vector2;
import kha.math.FastVector2;
import kha.graphics2.Graphics;
import kha.graphics2.VerTextAlignment;
import kha.graphics2.HorTextAlignment;
/**
* Static extension functions for Graphics2.
* Usage: "using kha.graphics2.GraphicsExtension;"
*/
class GraphicsExtension {
/**
* Draws a arc.
* @param ccw (optional) Specifies whether the drawing should be counterclockwise.
* @param segments (optional) The amount of lines that should be used to draw the arc.
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function drawArc(g2: Graphics, cx: Float, cy: Float, radius: Float, sAngle: Float, eAngle: Float, strength: Float = 1, ccw: Bool = false,
segments: Int = 0): Void {
#if kha_html5
if (kha.SystemImpl.gl == null) {
var g: kha.js.CanvasGraphics = cast g2;
radius -= strength / 2; // reduce radius to fit the line thickness within image width/height
g.drawArc(cx, cy, radius, sAngle, eAngle, strength, ccw);
return;
}
#end
sAngle = sAngle % (Math.PI * 2);
eAngle = eAngle % (Math.PI * 2);
if (ccw) {
if (eAngle > sAngle)
eAngle -= Math.PI * 2;
}
else if (eAngle < sAngle)
eAngle += Math.PI * 2;
radius += strength / 2;
if (segments <= 0)
segments = Math.floor(10 * Math.sqrt(radius));
var theta = (eAngle - sAngle) / segments;
var c = Math.cos(theta);
var s = Math.sin(theta);
var x = Math.cos(sAngle) * radius;
var y = Math.sin(sAngle) * radius;
for (n in 0...segments) {
var px = x + cx;
var py = y + cy;
var t = x;
x = c * x - s * y;
y = c * y + s * t;
drawInnerLine(g2, x + cx, y + cy, px, py, strength);
}
}
/**
* Draws a filled arc.
* @param ccw (optional) Specifies whether the drawing should be counterclockwise.
* @param segments (optional) The amount of lines that should be used to draw the arc.
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function fillArc(g2: Graphics, cx: Float, cy: Float, radius: Float, sAngle: Float, eAngle: Float, ccw: Bool = false,
segments: Int = 0): Void {
#if kha_html5
if (kha.SystemImpl.gl == null) {
var g: kha.js.CanvasGraphics = cast g2;
g.fillArc(cx, cy, radius, sAngle, eAngle, ccw);
return;
}
#end
sAngle = sAngle % (Math.PI * 2);
eAngle = eAngle % (Math.PI * 2);
if (ccw) {
if (eAngle > sAngle)
eAngle -= Math.PI * 2;
}
else if (eAngle < sAngle)
eAngle += Math.PI * 2;
if (segments <= 0)
segments = Math.floor(10 * Math.sqrt(radius));
var theta = (eAngle - sAngle) / segments;
var c = Math.cos(theta);
var s = Math.sin(theta);
var x = Math.cos(sAngle) * radius;
var y = Math.sin(sAngle) * radius;
var sx = x + cx;
var sy = y + cy;
for (n in 0...segments) {
var px = x + cx;
var py = y + cy;
var t = x;
x = c * x - s * y;
y = c * y + s * t;
g2.fillTriangle(px, py, x + cx, y + cy, sx, sy);
}
}
/**
* Draws a circle.
* @param segments (optional) The amount of lines that should be used to draw the circle.
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function drawCircle(g2: Graphics, cx: Float, cy: Float, radius: Float, strength: Float = 1, segments: Int = 0): Void {
#if kha_html5
if (kha.SystemImpl.gl == null) {
var g: kha.js.CanvasGraphics = cast g2;
radius -= strength / 2; // reduce radius to fit the line thickness within image width/height
g.drawCircle(cx, cy, radius, strength);
return;
}
#end
radius += strength / 2;
if (segments <= 0)
segments = Math.floor(10 * Math.sqrt(radius));
var theta = 2 * Math.PI / segments;
var c = Math.cos(theta);
var s = Math.sin(theta);
var x = radius;
var y = 0.0;
for (n in 0...segments) {
var px = x + cx;
var py = y + cy;
var t = x;
x = c * x - s * y;
y = c * y + s * t;
drawInnerLine(g2, x + cx, y + cy, px, py, strength);
}
}
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
static function drawInnerLine(g2: Graphics, x1: Float, y1: Float, x2: Float, y2: Float, strength: Float): Void {
var side = y2 > y1 ? 1 : 0;
if (y2 == y1)
side = x2 - x1 > 0 ? 1 : 0;
var vec = new FastVector2();
if (y2 == y1)
vec.setFrom(new FastVector2(0, -1));
else
vec.setFrom(new FastVector2(1, -(x2 - x1) / (y2 - y1)));
vec.length = strength;
var p1 = new FastVector2(x1 + side * vec.x, y1 + side * vec.y);
var p2 = new FastVector2(x2 + side * vec.x, y2 + side * vec.y);
var p3 = p1.sub(vec);
var p4 = p2.sub(vec);
g2.fillTriangle(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
g2.fillTriangle(p3.x, p3.y, p2.x, p2.y, p4.x, p4.y);
}
/**
* Draws a filled circle.
* @param segments (optional) The amount of lines that should be used to draw the circle.
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function fillCircle(g2: Graphics, cx: Float, cy: Float, radius: Float, segments: Int = 0): Void {
#if kha_html5
if (kha.SystemImpl.gl == null) {
var g: kha.js.CanvasGraphics = cast g2;
g.fillCircle(cx, cy, radius);
return;
}
#end
if (segments <= 0) {
segments = Math.floor(10 * Math.sqrt(radius));
}
var theta = 2 * Math.PI / segments;
var c = Math.cos(theta);
var s = Math.sin(theta);
var x = radius;
var y = 0.0;
for (n in 0...segments) {
var px = x + cx;
var py = y + cy;
var t = x;
x = c * x - s * y;
y = c * y + s * t;
g2.fillTriangle(px, py, x + cx, y + cy, cx, cy);
}
}
/**
* Draws a convex polygon.
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function drawPolygon(g2: Graphics, x: Float, y: Float, vertices: Array<Vector2>, strength: Float = 1) {
var iterator = vertices.iterator();
var v0 = iterator.next();
var v1 = v0;
while (iterator.hasNext()) {
var v2 = iterator.next();
g2.drawLine(v1.x + x, v1.y + y, v2.x + x, v2.y + y, strength);
v1 = v2;
}
g2.drawLine(v1.x + x, v1.y + y, v0.x + x, v0.y + y, strength);
}
/**
* Draws a filled convex polygon.
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function fillPolygon(g2: Graphics, x: Float, y: Float, vertices: Array<Vector2>) {
var iterator = vertices.iterator();
if (!iterator.hasNext())
return;
var v0 = iterator.next();
if (!iterator.hasNext())
return;
var v1 = iterator.next();
while (iterator.hasNext()) {
var v2 = iterator.next();
g2.fillTriangle(v0.x + x, v0.y + y, v1.x + x, v1.y + y, v2.x + x, v2.y + y);
v1 = v2;
}
}
/**
* Draws a cubic bezier using 4 pairs of points. If the x and y arrays have a length bigger then 4, the additional
* points will be ignored. With a length smaller of 4 a error will occur, there is no check for this.
* You can construct the curves visually in Inkscape with a path using default nodes.
* Provide x and y in the following order: startPoint, controlPoint1, controlPoint2, endPoint
* Reference: http://devmag.org.za/2011/04/05/bzier-curves-a-tutorial/
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function drawCubicBezier(g2: Graphics, x: Array<Float>, y: Array<Float>, segments: Int = 20, strength: Float = 1.0): Void {
var t: Float;
var q0 = calculateCubicBezierPoint(0, x, y);
var q1: Array<Float>;
for (i in 1...(segments + 1)) {
t = i / segments;
q1 = calculateCubicBezierPoint(t, x, y);
g2.drawLine(q0[0], q0[1], q1[0], q1[1], strength);
q0 = q1;
}
}
/**
* Draws multiple cubic beziers joined by the end point. The minimum size is 4 pairs of points (a single curve).
*/
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
public static function drawCubicBezierPath(g2: Graphics, x: Array<Float>, y: Array<Float>, segments: Int = 20, strength: Float = 1.0): Void {
var i = 0;
var t: Float;
var q0: Array<Float> = null;
var q1: Array<Float> = null;
while (i < x.length - 3) {
if (i == 0)
q0 = calculateCubicBezierPoint(0, [x[i], x[i + 1], x[i + 2], x[i + 3]], [y[i], y[i + 1], y[i + 2], y[i + 3]]);
for (j in 1...(segments + 1)) {
t = j / segments;
q1 = calculateCubicBezierPoint(t, [x[i], x[i + 1], x[i + 2], x[i + 3]], [y[i], y[i + 1], y[i + 2], y[i + 3]]);
g2.drawLine(q0[0], q0[1], q1[0], q1[1], strength);
q0 = q1;
}
i += 3;
}
}
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
static function calculateCubicBezierPoint(t: Float, x: Array<Float>, y: Array<Float>): Array<Float> {
var u: Float = 1 - t;
var tt: Float = t * t;
var uu: Float = u * u;
var uuu: Float = uu * u;
var ttt: Float = tt * t;
// first term
var p: Array<Float> = [uuu * x[0], uuu * y[0]];
// second term
p[0] += 3 * uu * t * x[1];
p[1] += 3 * uu * t * y[1];
// third term
p[0] += 3 * u * tt * x[2];
p[1] += 3 * u * tt * y[2];
// fourth term
p[0] += ttt * x[3];
p[1] += ttt * y[3];
return p;
}
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
static public function drawAlignedString(g2: Graphics, text: String, x: Float, y: Float, horAlign: HorTextAlignment, verAlign: VerTextAlignment): Void {
var xoffset = 0.0;
if (horAlign == TextCenter || horAlign == TextRight) {
var width = g2.font.width(g2.fontSize, text);
if (horAlign == TextCenter) {
xoffset = -width * 0.5;
}
else {
xoffset = -width;
}
}
var yoffset = 0.0;
if (verAlign == TextMiddle || verAlign == TextBottom) {
var height = g2.font.height(g2.fontSize);
if (verAlign == TextMiddle) {
yoffset = -height * 0.5;
}
else {
yoffset = -height;
}
}
g2.drawString(text, x + xoffset, y + yoffset);
}
@:deprecated("GraphicsExtension will be removed. If you want to use it, simply copy it into your own project and then remove this message.")
static public function drawAlignedCharacters(g2: Graphics, text: Array<Int>, start: Int, length: Int, x: Float, y: Float, horAlign: HorTextAlignment,
verAlign: VerTextAlignment): Void {
var xoffset = 0.0;
if (horAlign == TextCenter || horAlign == TextRight) {
var width = g2.font.widthOfCharacters(g2.fontSize, text, start, length);
if (horAlign == TextCenter) {
xoffset = -width * 0.5;
}
else {
xoffset = -width;
}
}
var yoffset = 0.0;
if (verAlign == TextMiddle || verAlign == TextBottom) {
var height = g2.font.height(g2.fontSize);
if (verAlign == TextMiddle) {
yoffset = -height * 0.5;
}
else {
yoffset = -height;
}
}
g2.drawCharacters(text, start, length, x + xoffset, y + yoffset);
}
}

View File

@ -0,0 +1,7 @@
package kha.graphics2;
enum abstract HorTextAlignment(Int) {
var TextLeft;
var TextCenter;
var TextRight;
}

View File

@ -0,0 +1,6 @@
package kha.graphics2;
enum abstract ImageScaleQuality(Int) {
var Low; // usually point filter
var High; // usually bilinear filter
}

View File

@ -0,0 +1,7 @@
package kha.graphics2;
enum abstract VerTextAlignment(Int) {
var TextTop;
var TextMiddle;
var TextBottom;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff