Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightre committed Mar 5, 2024
1 parent 2702d79 commit 1237159
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 29 deletions.
4 changes: 2 additions & 2 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let rapid = new Rapid({
canvas: document.getElementById("game"),
backgroundColor: Color.fromHex("FFFFFF")
})
const cat = await rapid.texture.textureFromUrl("./cat.png")
const plane = await rapid.texture.textureFromUrl("./plane.png")
const cat = await rapid.textures.textureFromUrl("./cat.png")
const plane = await rapid.textures.textureFromUrl("./plane.png")

const spriteCountDom = document.getElementById("sprite-count")
const sprites = []
Expand Down
4 changes: 2 additions & 2 deletions demo/matrix_stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let rapid = new Rapid({
canvas: document.getElementById("game"),
backgroundColor: Color.fromHex("FFFFFF")
})
const cat = await rapid.texture.textureFromUrl("./cat.png")
const plane = await rapid.texture.textureFromUrl("./plane.png")
const cat = await rapid.textures.textureFromUrl("./cat.png")
const plane = await rapid.textures.textureFromUrl("./plane.png")

// R G B A
const red = new Color(255, 0, 0, 255)
Expand Down
28 changes: 20 additions & 8 deletions dist/rapid.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ class RenderRegion {
this.rapid = rapid;
this.gl = rapid.gl;
this.webglArrayBuffer = new WebglBufferArray(rapid.gl, Float32Array, rapid.gl.ARRAY_BUFFER);
this.TEXTURE_UNITS_ARRAY = Array.from({ length: rapid.MAX_TEXTURE_UNITS }, (_, index) => index);
this.TEXTURE_UNITS_ARRAY = Array.from({ length: rapid.maxTextureUnits }, (_, index) => index);
}
addVertex(x, y, ..._) {
const [tx, ty] = this.rapid.transformPoint(x, y);
Expand All @@ -456,7 +456,7 @@ class RenderRegion {
let textureUnit = this.usedTextures.indexOf(texture);
if (textureUnit === -1) {
// 新纹理
if (this.usedTextures.length >= this.rapid.MAX_TEXTURE_UNITS) {
if (this.usedTextures.length >= this.rapid.maxTextureUnits) {
this.render();
}
this.usedTextures.push(texture);
Expand Down Expand Up @@ -523,6 +523,19 @@ class GraphicRegion extends RenderRegion {
this.webglArrayBuffer.pushUint(color);
this.vertex += 1;
}
drawCircle(x, y, radius, color) {
const numSegments = 30; // Increase this for a smoother circle
const angleStep = (2 * Math.PI) / numSegments;
this.startRender();
this.addVertex(x, y, color); // Center point
for (let i = 0; i <= numSegments; i++) {
const angle = i * angleStep;
const dx = x + radius * Math.cos(angle);
const dy = y + radius * Math.sin(angle);
this.addVertex(dx, dy, color);
}
this.executeRender();
}
executeRender() {
super.executeRender();
const gl = this.gl;
Expand Down Expand Up @@ -566,7 +579,7 @@ class SpriteRegion extends RenderRegion {
{ name: "aColor", size: 4, type: gl.UNSIGNED_BYTE, stride, offset: 5 * Float32Array.BYTES_PER_ELEMENT, normalized: true },
]);
this.batchSprite = 0;
this.initDefaultShader(vertString, this.generateFragShader(fragString, rapid.MAX_TEXTURE_UNITS));
this.initDefaultShader(vertString, this.generateFragShader(fragString, rapid.maxTextureUnits));
this.MAX_BATCH = Math.floor(2 ** 16 / VERTEX_PER_SPRITE);
this.indexBuffer = new SpriteElementArray(gl, this.MAX_BATCH);
}
Expand Down Expand Up @@ -723,25 +736,24 @@ class Texture {
* @returns A new `Texture` instance created from the specified URL.
*/
static fromUrl(rapid, url) {
return rapid.texture.textureFromUrl(url);
return rapid.textures.textureFromUrl(url);
}
}

class Rapid {
constructor(options) {
this.projectionDirty = true;
this.matrixStack = new MatrixStack();
this.texture = new TextureCache(this);
this.textures = new TextureCache(this);
this.devicePixelRatio = window.devicePixelRatio || 1;
this.regions = new Map;
this.defaultColor = new Color(255, 255, 255, 255);
this.regions = new Map;
const gl = getContext(options.canvas);
this.gl = gl;
this.canvas = options.canvas;
this.MAX_TEXTURE_UNITS = gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS);
this.maxTextureUnits = gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS);
this.width = options.width || this.canvas.width;
this.height = options.width || this.canvas.height;
// this.projection = this.createOrthMatrix(0, this.canvas.width, this.canvas.height, 0)
this.backgroundColor = options.backgroundColor || new Color(255, 255, 255, 255);
this.registerBuildInRegion();
this.initWebgl(gl);
Expand Down
1 change: 1 addition & 0 deletions dist/regions/graphic_region.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare class GraphicRegion extends RenderRegion {
constructor(rapid: Rapid);
startRender(): void;
addVertex(x: number, y: number, color: number): void;
drawCircle(x: number, y: number, radius: number, color: number): void;
protected executeRender(): void;
}
export default GraphicRegion;
8 changes: 4 additions & 4 deletions dist/render.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ declare class Rapid {
projection: Float32Array;
projectionDirty: boolean;
matrixStack: MatrixStack;
texture: TextureCache;
textures: TextureCache;
width: number;
height: number;
backgroundColor: Color;
devicePixelRatio: number;
readonly devicePixelRatio: number;
readonly maxTextureUnits: number;
private readonly defaultColor;
private currentRegion?;
private currentRegionName?;
private regions;
readonly MAX_TEXTURE_UNITS: number;
private readonly defaultColor;
constructor(options: IRapiadOptions);
private initWebgl;
private registerBuildInRegion;
Expand Down
16 changes: 16 additions & 0 deletions src/regions/graphic_region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ class GraphicRegion extends RenderRegion {
this.webglArrayBuffer.pushUint(color)
this.vertex += 1
}
drawCircle(x: number, y: number, radius: number, color: number): void {
const numSegments = 30; // Increase this for a smoother circle
const angleStep = (2 * Math.PI) / numSegments;

this.startRender();
this.addVertex(x, y, color); // Center point

for (let i = 0; i <= numSegments; i++) {
const angle = i * angleStep;
const dx = x + radius * Math.cos(angle);
const dy = y + radius * Math.sin(angle);
this.addVertex(dx, dy, color);
}

this.executeRender();
}

protected override executeRender(): void {
super.executeRender()
Expand Down
4 changes: 2 additions & 2 deletions src/regions/region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RenderRegion {
this.rapid = rapid
this.gl = rapid.gl
this.webglArrayBuffer = new WebglBufferArray(rapid.gl, Float32Array, rapid.gl.ARRAY_BUFFER)
this.TEXTURE_UNITS_ARRAY = Array.from({ length: rapid.MAX_TEXTURE_UNITS },
this.TEXTURE_UNITS_ARRAY = Array.from({ length: rapid.maxTextureUnits },
(_, index) => index);
}
protected addVertex(x: number, y: number, ..._: unknown[]) {
Expand All @@ -31,7 +31,7 @@ class RenderRegion {
let textureUnit = this.usedTextures.indexOf(texture)
if (textureUnit === -1) {
// 新纹理
if (this.usedTextures.length >= this.rapid.MAX_TEXTURE_UNITS) {
if (this.usedTextures.length >= this.rapid.maxTextureUnits) {
this.render()
}
this.usedTextures.push(texture)
Expand Down
2 changes: 1 addition & 1 deletion src/regions/sprite_region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SpriteRegion extends RenderRegion {
])
this.initDefaultShader(
vertString,
this.generateFragShader(fragString, rapid.MAX_TEXTURE_UNITS)
this.generateFragShader(fragString, rapid.maxTextureUnits)
)
this.MAX_BATCH = Math.floor(2 ** 16 / VERTEX_PER_SPRITE)
this.indexBuffer = new SpriteElementArray(gl, this.MAX_BATCH)
Expand Down
17 changes: 8 additions & 9 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,27 @@ class Rapid {
projectionDirty: boolean = true

matrixStack = new MatrixStack()
texture = new TextureCache(this)
textures = new TextureCache(this)
width: number
height: number

backgroundColor: Color
devicePixelRatio = window.devicePixelRatio || 1
backgroundColor: Color
readonly devicePixelRatio = window.devicePixelRatio || 1
readonly maxTextureUnits: number
private readonly defaultColor = new Color(255, 255, 255, 255)

private currentRegion?: RenderRegion
private currentRegionName?: string
private regions: Map<string, RenderRegion> = new Map

readonly MAX_TEXTURE_UNITS: number
private readonly defaultColor = new Color(255, 255, 255, 255)

constructor(options: IRapiadOptions) {
const gl = getContext(options.canvas)
this.gl = gl
this.canvas = options.canvas
this.MAX_TEXTURE_UNITS = gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS);
this.maxTextureUnits = gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS);

this.width = options.width || this.canvas.width
this.height = options.width || this.canvas.height
// this.projection = this.createOrthMatrix(0, this.canvas.width, this.canvas.height, 0)


this.backgroundColor = options.backgroundColor || new Color(255, 255, 255, 255)
this.registerBuildInRegion()
Expand Down
2 changes: 1 addition & 1 deletion src/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Texture {
* @returns A new `Texture` instance created from the specified URL.
*/
static fromUrl(rapid: Rapid, url: string) {
return rapid.texture.textureFromUrl(url)
return rapid.textures.textureFromUrl(url)
}
}

Expand Down

0 comments on commit 1237159

Please sign in to comment.