Skip to content

Commit

Permalink
Add Canvas.loadFontRaw and Surface.fromRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Aug 31, 2024
1 parent 5e0ac1f commit 8f7d0bd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (
const selfPath = new URL(import.meta.url).pathname;
const worker = new Worker(`file://${selfPath}`, { type: "module" });
} else {
const ws = new WebSocket('ws://localhost:1234');
const ws = new WebSocket("ws://localhost:1234");
self.onmessage = (event) => {
ws.send(event.data);
};
Expand Down
1 change: 0 additions & 1 deletion examples/sprite/client.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

16 changes: 9 additions & 7 deletions examples/sprite/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function sleepSync(ms: number) {
}
}

const canvasSize = { width: 1000, height:800 };
const canvasSize = { width: 1000, height: 800 };
const window = new WindowBuilder(
"Hello, Deno!",
canvasSize.width,
Expand Down Expand Up @@ -131,12 +131,14 @@ function frame(e) {

// check for collision with tiles on map
for (const tile of tiles) {
if (deno.x < tile.x + tile.width &&
deno.x + deno.frames[0].width * deno.scale > tile.x &&
deno.y < tile.y + tile.height &&
deno.y + deno.frames[0].height * deno.scale > tile.y) {
deno.x -= deno.vx;
deno.y -= deno.vy;
if (
deno.x < tile.x + tile.width &&
deno.x + deno.frames[0].width * deno.scale > tile.x &&
deno.y < tile.y + tile.height &&
deno.y + deno.frames[0].height * deno.scale > tile.y
) {
deno.x -= deno.vx;
deno.y -= deno.vy;
}
}
}
Expand Down
42 changes: 39 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ const sdl2 = Deno.dlopen(getLibraryPath("SDL2"), {
"parameters": [],
"result": "i32",
},
"SDL_RWFromMem": {
"parameters": ["buffer", "i32"],
"result": "pointer",
},
});

const SDL2_Image_symbols = {
Expand All @@ -297,6 +301,10 @@ const SDL2_Image_symbols = {
"parameters": ["buffer"],
"result": "pointer",
},
"IMG_Load_RW": {
"parameters": ["pointer"],
"result": "pointer",
},
} as const;

const SDL2_TTF_symbols = {
Expand All @@ -308,6 +316,10 @@ const SDL2_TTF_symbols = {
"parameters": ["buffer", "i32"],
"result": "pointer",
},
"TTF_OpenFontRW": {
"parameters": ["pointer", "i32", "i32"],
"result": "pointer",
},
"TTF_RenderText_Solid": {
"parameters": ["pointer", "buffer", "pointer"],
"result": "pointer",
Expand Down Expand Up @@ -409,9 +421,7 @@ function init() {
}
}

if (typeof window !== "undefined") {
init();
}
init();

/**
* An enum that contains structures for the different event types.
Expand Down Expand Up @@ -691,6 +701,18 @@ export class Canvas {
const raw = sdl2Font.symbols.TTF_OpenFont(asCString(path), size);
return new Font(raw);
}

loadFontRaw(data: Uint8Array, size: number): Font {
const rwops = sdl2.symbols.SDL_RWFromMem(data, data.byteLength);
if (rwops === null) {
throwSDLError();
}
const raw = sdl2Font.symbols.TTF_OpenFontRW(rwops, 1, size);
if (raw === null) {
throwSDLError();
}
return new Font(raw);
}
}

/**
Expand Down Expand Up @@ -1027,6 +1049,20 @@ export class Surface {
}
return new Surface(raw);
}

static fromRaw(data: Uint8Array): Surface {
if (!sdl2Image) {
throw new Error("SDL2_image was not loaded");
}

const rwops = sdl2.symbols.SDL_RWFromMem(data, data.byteLength);
const raw = sdl2Image.symbols.IMG_Load_RW(rwops);
if (raw === null) {
throwSDLError();
}

return new Surface(raw);
}
}

const sizeOfEvent = 56; // type (u32) + event
Expand Down

0 comments on commit 8f7d0bd

Please sign in to comment.