Skip to content

Commit

Permalink
add antialiasing support on sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
Vortex2Oblivion committed Dec 15, 2024
1 parent e6d0a69 commit 96d690d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
27 changes: 26 additions & 1 deletion src/flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package flixel;

import raylib.TextureFilter;
import raylib.Color;
import raylib.Colors;
import raylib.Image;
Expand All @@ -8,12 +9,18 @@ import raylib.Vector2;
import raylib.Texture;

class FlxSprite extends FlxObject {
public static var defaultAntialiasing:Bool = false;

private var texture(default, set):Texture;

public var color:Color = Colors.WHITE;

public var alpha(default, set):Float = 1.0;

public var antialiasing(default, set):Bool = defaultAntialiasing;

public var antialiasingLevel(default, set):TextureFilter = TEXTURE_FILTER_BILINEAR;

public function new(x:Float = 0, y:Float = 0, ?graphic:String) {
super(x, y);
if (graphic != null) {
Expand All @@ -37,7 +44,7 @@ class FlxSprite extends FlxObject {

override public function draw() {
super.draw();
if (!visible || alpha < 0.001 || !camera.visible || !isOnScreen()){
if (!visible || alpha < 0.001 || !camera.visible || !isOnScreen()) {
return;
}
drawTexturePro(texture, Rectangle.create(0, 0, texture.width, texture.height),
Expand Down Expand Up @@ -72,4 +79,22 @@ class FlxSprite extends FlxObject {
inline function set_alpha(value:Float):Float {
return alpha = value = color.a = Std.int(value * 255);
}

@:noCompletion
function set_antialiasing(value:Bool):Bool {
if (value) {
setTextureFilter(texture, antialiasingLevel);
} else {
setTextureFilter(texture, TEXTURE_FILTER_POINT);
}
return antialiasing = value;
}

@:noCompletion
function set_antialiasingLevel(value:TextureFilter):TextureFilter {
if(antialiasing){
setTextureFilter(texture, value);
}
return antialiasingLevel = value;
}
}
29 changes: 27 additions & 2 deletions src/flixel/text/FlxText.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package flixel.text;

import raylib.TextureFilter;
import flixel.FlxSprite;
import raylib.Font;
import raylib.Vector2;
Expand Down Expand Up @@ -34,14 +35,38 @@ class FlxText extends FlxSprite {
@:noCompletion function set_font(font:String):String {
unloadFont(_font);
_font = loadFont(font);
// reapply antialiasing after generating a new font.
set_antialiasing(antialiasing);
set_antialiasingLevel(antialiasingLevel);
return this.font = font;
}

@:noCompletion override function get_width():Float {
@:noCompletion
override function get_width():Float {
return measureTextEx(_font, text, size, letterSpacing).x;
}

@:noCompletion override function get_height():Float {
@:noCompletion
override function get_height():Float {
return measureTextEx(_font, text, size, letterSpacing).y;
}

@:noCompletion
override function set_antialiasing(value:Bool):Bool {
if (value) {
setTextureFilter(_font.texture, antialiasingLevel);
} else {
setTextureFilter(_font.texture, TEXTURE_FILTER_POINT);
}
setTextureFilter(getFontDefault().texture, TEXTURE_FILTER_POINT); // reset default font
return antialiasing = value;
}

@:noCompletion
override function set_antialiasingLevel(value:TextureFilter):TextureFilter {
if(antialiasing){
setTextureFilter(_font.texture, value);
}
return antialiasingLevel = value;
}
}
2 changes: 1 addition & 1 deletion src/raylib/Font.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern class Font {
public var glyphCount:Int;
public var glyphPadding:Int;

public var texure:Texture2D;
public var texture:Texture2D;

public var recs:Star<Rectangle>;

Expand Down
3 changes: 3 additions & 0 deletions src/raylib/Raylib.hx
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,9 @@ extern class Raylib {
@:native("UnloadTexture")
public static function unloadTexture(texture:Texture2D):Void;

@:native("SetTextureFilter")
public static function setTextureFilter(texture:Texture2D, filter:TextureFilter):Void;

@:native("LoadRenderTexture")
public static function loadRenderTexture(width:Int, height:Int):RenderTexture2D;

Expand Down
10 changes: 10 additions & 0 deletions src/raylib/TextureFilter.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package raylib;

enum abstract TextureFilter(Int) to Int from Int {
var TEXTURE_FILTER_POINT = 0;
var TEXTURE_FILTER_BILINEAR;
var TEXTURE_FILTER_TRILINEAR;
var TEXTURE_FILTER_ANISOTROPIC_4X;
var TEXTURE_FILTER_ANISOTROPIC_8X;
var TEXTURE_FILTER_ANISOTROPIC_16X;
}
1 change: 1 addition & 0 deletions test/src/OtherState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class OtherState extends FlxState {
maurice = new FlxSprite();
maurice.loadGraphic("images/maurice.png");
maurice.screenCenter();
maurice.antialiasing = true;
add(maurice);
}
override public function update(elapsed:Float) {
Expand Down
3 changes: 1 addition & 2 deletions test/src/PlayState.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package;

import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxState;
import flixel.FlxObject;
import flixel.FlxSprite;
import raylib.RayMath;
import raylib.Raylib;
Expand All @@ -24,6 +22,7 @@ class PlayState extends FlxState {
scythe.loadGraphic("images/scythe.png");
scythe.width *= 0.25;
scythe.height *= 0.25;
scythe.antialiasing = true;
add(scythe);
}

Expand Down
6 changes: 6 additions & 0 deletions test/src/TextState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ class TextState extends FlxState {

text = new FlxText(0, 0, "This is FlxText!", 32);
text.screenCenter();
text.antialiasing = true;
text.font = "dumbnerd.ttf";
add(text);
}

override public function update(elapsed:Float) {
super.update(elapsed);
text.size = Math.sin(Raylib.getTime()) * 10 + 32;

if(Raylib.isKeyPressed(32)) {
text.antialiasingLevel = TEXTURE_FILTER_ANISOTROPIC_16X;
}
}
}

0 comments on commit 96d690d

Please sign in to comment.