Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I render font correctly? #77

Open
nickguimond opened this issue Aug 31, 2024 · 1 comment
Open

How do I render font correctly? #77

nickguimond opened this issue Aug 31, 2024 · 1 comment

Comments

@nickguimond
Copy link

import { 
    Color,
    EventType, 
    Rect, 
    WindowBuilder 
} from "jsr:@divy/sdl2";

const window = new WindowBuilder(
  "Hello World",
  1000,
  500,
).build();

const canvas = window.canvas();

const font = canvas.loadFont('./fonts/Roboto-Regular.ttf', 16);
const whiteForegroundColor = new Color(255, 255, 255, 255);
const fontTexture = font.renderBlended('Hello World', whiteForegroundColor);


const creator = canvas.textureCreator();
const textTexture = creator.createTextureFromSurface(fontTexture);

const {w, h} = textTexture.query();

const destRect = new Rect(20, 20, w, h);

const frame = () => {
    canvas.setDrawColor(255, 255, 255, 255); // Set background to white
    canvas.clear();
    canvas.copy(textTexture, undefined, destRect);
    canvas.present();
}

for await (const event of window.events()) {
  switch (event.type) {
    case EventType.Draw:
      frame();
      break;
    case EventType.Quit:
      Deno.exit(0);
      break;
    default:
      break;
  }
}

Given this minimal example ^ I get this as a result..
image

Where the text is super faint and I cant seem to figure out how to fix it. All the other 'game' showcase repos* don't use font and a couple of them have it commented out in the code. So I'm not sure if I'm really in the right direction or just trying to use a feature that's not really supported yet?

Running on Apple M2 FWIW
*https://github.com/dhairy-online/dino-deno
*https://github.com/dhairy-online/flappybird
*https://github.com/load1n9/caviar

@kaleidawave
Copy link

I believe there is some pointer issue or something happening. If you run it a few times you will note that the colors change / appear to be random.

Haven't looked into it but I think the renderBlended doesn't seem to observe the passed color and internally the color is read from another place in memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants