Skip to content

Commit

Permalink
Converted renderer example.
Browse files Browse the repository at this point in the history
  • Loading branch information
smack0007 committed Sep 9, 2024
1 parent 9d05dd5 commit 343c37c
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 95 deletions.
32 changes: 8 additions & 24 deletions examples/renderer/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { join } from "@std/path";
const WINDOW_WIDTH = 1024;
const WINDOW_HEIGHT = 768;

function main(): number {
function main(): void {
SDL.Init(SDL.InitFlags.VIDEO, { functions: SDL_FUNCTIONS });

const [window, renderer] = SDL.CreateWindowAndRenderer(
Expand All @@ -17,12 +17,7 @@ function main(): number {

SDL.RenderSetLogicalSize(renderer, WINDOW_WIDTH, WINDOW_HEIGHT);

const rendererInfo = new SDL.RendererInfo();
if (SDL.GetRendererInfo(renderer, rendererInfo) != 0) {
console.error(`Failed to get renderer info: ${SDL.GetError()}`);
return 1;
}

const rendererInfo = SDL.GetRendererInfo(renderer);
console.info(rendererInfo.name);
console.info(rendererInfo.max_texture_width);
console.info(rendererInfo.max_texture_height);
Expand All @@ -38,23 +33,13 @@ function main(): number {

const denoSurface = SDL.LoadBMP(join(ASSETS_PATH, "jurassicDeno.bmp"));

if (denoSurface == null) {
console.error("Failed to load jurassicDeno.bmp.");
return 1;
}

const srcRect = new SDL.Rect(0, 0, denoSurface.w, denoSurface.h);
const destRect = new SDL.Rect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
const textureCenter = new SDL.Point(denoSurface.w / 2, denoSurface.h / 2);
let textureRotation = 0;
const texture = SDL.CreateTextureFromSurface(renderer, denoSurface);
SDL.FreeSurface(denoSurface);

if (texture == null) {
console.error(`Failed to create texture: ${SDL.GetError()}`);
return 1;
}

const points = new BoxArray<SDL.Point>(SDL.Point, 4);
points.at(0).x = 0;
points.at(0).y = 0;
Expand All @@ -65,8 +50,6 @@ function main(): number {
points.at(3).x = 0;
points.at(3).y = 1;

const numkeys = new Box<int>(Int);

const event = new SDL.Event();
let done = false;
while (!done) {
Expand All @@ -81,8 +64,11 @@ function main(): number {
break;
}

const state = SDL.GetKeyboardState(numkeys);
console.info(numkeys.value, Memory.readU8(state, SDL.Scancode.ESCAPE));
// TODO: Move this to some kind of keyboard example.
const state = SDL.GetKeyboardState();
if (state[SDL.Scancode.ESCAPE]) {
console.info("ESC is down.");
}

SDL.SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL.RenderClear(renderer);
Expand Down Expand Up @@ -117,12 +103,10 @@ function main(): number {
SDL.DestroyRenderer(renderer);
SDL.DestroyWindow(window);
SDL.Quit();

return 0;
}

try {
Deno.exit(main());
main();
} catch (error) {
console.error(error);
Deno.exit(1);
Expand Down
16 changes: 2 additions & 14 deletions examples/same-game/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,10 @@ function main(): number {
IMG.Init(IMG.InitFlags.PNG, { functions: IMG_FUNCTIONS });
TTF.Init({ functions: TTF_FUNCTIONS });

const windowBox = new Box<Pointer<SDL.Window>>(Pointer);
const rendererBox = new Box<Pointer<SDL.Renderer>>(Pointer);

SDL.CreateWindowAndRenderer(
const [window, renderer] = SDL.CreateWindowAndRenderer(
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL.WindowFlags.SHOWN,
windowBox,
rendererBox
);

const window = windowBox.unboxNotNull(
() => `Failed to create window: ${SDL.GetError()}`
);
const renderer = rendererBox.unboxNotNull(
() => `Failed to create renderer: ${SDL.GetError()}`
SDL.WindowFlags.SHOWN
);

SDL.SetWindowTitle(window, "Same Game");
Expand Down
27 changes: 16 additions & 11 deletions src/SDL/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PlatformPointer } from "../_types.ts";
import { Box } from "../boxes.ts";
import { SDLError } from "../error.ts";
import { Pointer, PointerLike } from "../pointers.ts";
import { f32, f64, i32, InitOptions, int, TypedArray, u16, u32, u64, u8 } from "../types.ts";
import { f32, f64, i32, InitOptions, Int, int, TypedArray, u16, u32, u64, u8 } from "../types.ts";

import { AudioCallback, EventFilter } from "./callbacks.ts";
import {
Expand Down Expand Up @@ -465,25 +465,30 @@ export function GetGrabbedWindow(): Window | null {
}
GetGrabbedWindow.symbolName = "SDL_GetGrabbedWindow";

export function GetKeyboardState(
numkeys: PointerLike<int> | null,
): Pointer<u8> {
const _result = Platform.fromPlatformPointer(_library.symbols.SDL_GetKeyboardState(
Platform.toPlatformPointer(Pointer.of(numkeys)),
) as PlatformPointer<u8>)!;
return _result;
export function GetKeyboardState(): Uint8Array {
const numkeys = new Box<int>(Int);
const _result = Platform.fromPlatformPointer(
_library.symbols.SDL_GetKeyboardState(
Platform.toPlatformPointer(Pointer.of(numkeys)),
) as PlatformPointer<u8>,
)!;
const dataView = new Platform.DataView(_result);
return new Uint8Array(dataView.getArrayBuffer(numkeys.value, 0));
}
GetKeyboardState.symbolName = "SDL_GetKeyboardState";

export function GetRendererInfo(
renderer: PointerLike<Renderer>,
info: PointerLike<RendererInfo>,
): i32 {
): RendererInfo {
const info = new RendererInfo();
const _result = _library.symbols.SDL_GetRendererInfo(
Platform.toPlatformPointer(Pointer.of(renderer)),
Platform.toPlatformPointer(Pointer.of(info)),
) as i32;
return _result;
if (_result < 0) {
throw new SDLError(GetError());
}
return info;
}
GetRendererInfo.symbolName = "SDL_GetRendererInfo";

Expand Down
2 changes: 1 addition & 1 deletion src/SDL_image/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PlatformPointer } from "../_types.ts";
import { Box } from "../boxes.ts";
import { SDLError } from "../error.ts";
import { Pointer, PointerLike } from "../pointers.ts";
import { f32, f64, i32, InitOptions, int, TypedArray, u16, u32, u64, u8 } from "../types.ts";
import { f32, f64, i32, InitOptions, Int, int, TypedArray, u16, u32, u64, u8 } from "../types.ts";

import {} from "./callbacks.ts";
import { InitFlags } from "./enums.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/SDL_ttf/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PlatformPointer } from "../_types.ts";
import { Box } from "../boxes.ts";
import { SDLError } from "../error.ts";
import { Pointer, PointerLike } from "../pointers.ts";
import { f32, f64, i32, InitOptions, int, TypedArray, u16, u32, u64, u8 } from "../types.ts";
import { f32, f64, i32, InitOptions, Int, int, TypedArray, u16, u32, u64, u8 } from "../types.ts";

import {} from "./callbacks.ts";
import {} from "./enums.ts";
Expand Down
1 change: 1 addition & 0 deletions src/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface PlatformDataView {
readonly byteOffset: number;

getArray(byteLength: number, byteOffset: number): Uint8Array;
getArrayBuffer(byteLength: number, byteOffset: number): ArrayBuffer;
getCallback<T extends Callback>(
byteOffset: number,
definition: DynamicCallbackDefinition<T>
Expand Down
9 changes: 8 additions & 1 deletion src/deno/_dataView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class DenoPlatformDataView {
}
}

// TODO: Maybe this should be renamed to getU8Array?
public getArray(byteLength: number, byteOffset: number): Uint8Array {
if (this._view instanceof globalThis.DataView) {
throw new Error("Not implemented.");
Expand All @@ -62,6 +61,14 @@ export class DenoPlatformDataView {
}
}

public getArrayBuffer(byteLength: number, byteOffset: number): ArrayBuffer {
if (this._view instanceof globalThis.DataView) {
throw new Error("Not implemented.");
} else {
return this._view.getArrayBuffer(byteLength, byteOffset);
}
}

public getCallback<T extends Callback>(
byteOffset: number,
definition: DynamicCallbackDefinition<T>
Expand Down
10 changes: 10 additions & 0 deletions tools/codegen/SDL/functionImplementations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const GET_KEYBOARD_STATE = `export function GetKeyboardState(): Uint8Array {
const numkeys = new Box<int>(Int);
const _result = Platform.fromPlatformPointer(
_library.symbols.SDL_GetKeyboardState(
Platform.toPlatformPointer(Pointer.of(numkeys))
) as PlatformPointer<u8>
)!;
const dataView = new Platform.DataView(_result);
return new Uint8Array(dataView.getArrayBuffer(numkeys.value, 0));
}`;
7 changes: 6 additions & 1 deletion tools/codegen/SDL/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CodeGenFunctions } from "../types.ts";
import { GET_KEYBOARD_STATE } from "./functionImplementations.ts";

export const functions: CodeGenFunctions = {
SDL_AddEventWatch: {
Expand Down Expand Up @@ -479,12 +480,14 @@ export const functions: CodeGenFunctions = {
parameters: {
numkeys: {
type: "int*",
isNullable: true,
isOutput: true,
},
},
result: {
type: "Uint8*",
},
resultIsOutput: true,
implementation: GET_KEYBOARD_STATE,
},
SDL_GetRendererInfo: {
parameters: {
Expand All @@ -493,11 +496,13 @@ export const functions: CodeGenFunctions = {
},
info: {
type: "SDL_RendererInfo*",
isOutput: true,
},
},
result: {
type: "int",
},
checkForError: true,
},
SDL_GetRevision: {
parameters: {},
Expand Down
Loading

0 comments on commit 343c37c

Please sign in to comment.