How to get antialiasing with sdl2 ( SOLVED )

Hello everybody,

I’m trying to draw some simple figures in a window with sdl2 crate but i get stairlike lines:

jaggies

Here is my Cargo.toml

[package]
name = “draw-test”
version = “0.1.0”
authors = [“Guish”]
edition = “2018”

[dependencies]
rand = “0.6.5”

[dependencies.sdl2]
version = “0.32.2”
default-features = false
features = [“gfx”]

And here the code:

extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use std::{thread, time};

use sdl2::gfx::primitives::DrawRenderer;
use sdl2::render::WindowCanvas;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::EventPump;

fn init() -> (WindowCanvas, EventPump) {
    let sdl_context = sdl2::init().unwrap();
    let video_subsystem = sdl_context.video().unwrap();

    let window = video_subsystem
        .window("demo", 400, 400)
        .position_centered()
        .opengl()
        .build()
        .unwrap();

    let mut renderer = window.into_canvas().build().unwrap();

    let event_pump = sdl_context.event_pump().unwrap();

    renderer.set_draw_color(Color::RGB(255, 255, 255));
    renderer.clear();
    renderer.present();

    (renderer, event_pump)
}

fn main() {
    let (mut r, mut e) = init();
    let color = Color::RGB(255, 0, 0);
    r.set_draw_color(color);
    r.fill_rect(Rect::new(10, 10, 100, 100));
    let _ = r.circle(200, 100, 30, color);
    r.present();

    'running: loop {
        for event in e.poll_iter() {
            match event {
                Event::KeyDown {
                    keycode: Some(Keycode::Escape),
                    ..
                } => break 'running,
                _ => {}
            }
        }
        thread::sleep(time::Duration::from_millis(10));
    }
}

I was unable to find info about antialiasing on crate docs or other sources.

Could anyone give me advice about how add it to this example?

Also any other simple way to draw circles and arc on a window would be welcome.

Many Thanks!

You seem to look for DrawRenderer::aa_circle():

https://rust-sdl2.github.io/rust-sdl2/sdl2/gfx/primitives/trait.DrawRenderer.html#tymethod.aa_circle

fn aa_circle<C: ToColor>(&self, x: i16, y: i16, rad: i16, color: C) -> Result<(), String>

http://www.ferzkopp.net/Software/SDL2_gfx/Docs/html/_s_d_l2__gfx_primitives_8h.html#a46b037505b91133ae3bd18556092a632

SDL2_GFXPRIMITIVES_SCOPE int aacircleColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Uint32 color)

Draw anti-aliased circle with blending.

Thank you dkim!

I got lost on crate doc and couldn’t figure out that aa was antialiasing :persevere:

Docs for the library at ferzkopp.net are more descriptive, thanks for pointing it.

Cheers.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.