Skip to content

Commit

Permalink
Add uniform image::Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
skejeton committed Aug 25, 2024
1 parent 1676de5 commit 453a5d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
55 changes: 29 additions & 26 deletions src/staembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@ const char *th_em_modulesrc[] = {
"\t\"th.um\"\n"
")\n"
"\n"
"//~~enum Filter\n"
"// Filtering mode for images, render targets, and fonts.\n"
"type Filter* = enum {\n"
"\tnearest = 0\n"
"\tlinear = 1\n"
"}\n"
"//~~\n"
"\n"
"//~~opaque Image\n"
"// Represents a drawable image. It is an opaque structure.\n"
"// Images support a color filter. It is applied by multiplying the color\n"
Expand All @@ -502,12 +510,12 @@ const char *th_em_modulesrc[] = {
"type RenderTarget* = struct { _: ^struct{} }\n"
"//~~\n"
"\n"
"fn umth_image_create_render_target(ret: ^RenderTarget, width: int, height: int, filter: int): th::ErrCode\n"
"fn umth_image_create_render_target(ret: ^RenderTarget, width: int, height: int, filter: Filter): th::ErrCode\n"
"//~~fn createRenderTarget\n"
"// Creates a render target you can draw to, like to a window.\n"
"// Filter specifies specfifies filtering for resulting image.\n"
"// Image can be retrieved via `toImage`.\n"
"fn createRenderTarget*(size: th::Vf2, filter: int): (RenderTarget, std::Err) {\n"
"fn createRenderTarget*(size: th::Vf2, filter: Filter): (RenderTarget, std::Err) {\n"
"//~~\n"
"\trt := RenderTarget{}\n"
"\tec := umth_image_create_render_target(&rt, trunc(size.x), trunc(size.y), filter)\n"
Expand Down Expand Up @@ -735,14 +743,14 @@ const char *th_em_modulesrc[] = {
"\treturn img, th::__errFromCode(ec)\n"
"}\n"
"\n"
"fn umth_image_set_filter(data: Image, filter: int): th::ErrCode\n"
"fn umth_image_set_filter(data: Image, filter: Filter): th::ErrCode\n"
"//~~fn Image.setfilter\n"
"// Sets a mag/min filter. 0 is nearest, others are linear.\n"
"// This function will regenerate the texture. This means it shouldn\'t be\n"
"// used in a loop.\n"
"// https://learnopengl.com/img/getting-started/texture_filtering.png\n"
"// left is nearest, right is linear.\n"
"fn (i: ^Image) setfilter*(filter: int): std::Err {\n"
"fn (i: ^Image) setfilter*(filter: Filter): std::Err {\n"
"//~~\n"
"\tif !i.validate() {\n"
"\t\tth::__error(\"image is invalid\")\n"
Expand Down Expand Up @@ -2079,19 +2087,13 @@ const char *th_em_modulesrc[] = {
"import (\n"
"\t\"th.um\"\n"
"\t\"std.um\"\n"
"\t\"image.um\"\n"
")\n"
"\n"
"//~~Filtering constants\n"
"type Filter* = enum {\n"
"\tnearest = 0\n"
"\tlinear = 1\n"
"}\n"
"//~~\n"
"\n"
"// TODO: should this be exported and documented?\n"
"type TrueType* = struct { _: ^struct{} }\n"
"\n"
"fn umth_ttf_font_load(f: ^TrueType, path: str, size: th::fu, filter: Filter): th::ErrCode\n"
"fn umth_ttf_font_load(f: ^TrueType, path: str, size: th::fu, filter: image::Filter): th::ErrCode\n"
"fn umth_ttf_font_draw(font: TrueType, s: str, x: th::fu, y: th::fu, color: uint32, scale: th::fu)\n"
"fn umth_ttf_font_measure(font: TrueType, s: str): th::Vf2\n"
"\n"
Expand Down Expand Up @@ -2130,7 +2132,7 @@ const char *th_em_modulesrc[] = {
"\n"
"//~~fn load\n"
"// Loads a font from a path and returns it.\n"
"fn load*(path: str, size: th::fu, filter: Filter = Filter.linear): (Font, std::Err) {\n"
"fn load*(path: str, size: th::fu, filter: image::Filter = .linear): (Font, std::Err) {\n"
"//~~\n"
"\tvar f: TrueType\n"
"\tec := umth_ttf_font_load(&f, path, size, filter)\n"
Expand Down Expand Up @@ -4389,6 +4391,17 @@ const char *th_em_moduledocs[] = {
"---------\n"
"\n"
"",
"enum Filter\n"
"\n"
"type Filter* = enum {\n"
"\tnearest = 0\n"
"\tlinear = 1\n"
"}\n"
"\n"
"Filtering mode for images, render targets, and fonts.\n"
"\n"
"---------\n"
"\n"
"opaque Image\n"
"\n"
"type Image* = struct{ _: ^struct{} }\n"
Expand All @@ -4409,7 +4422,7 @@ const char *th_em_moduledocs[] = {
"\n"
"fn createRenderTarget\n"
"\n"
"[3mfn createRenderTarget*(size: th::Vf2, filter: int): (RenderTarget, std::Err) {\n"
"[3mfn createRenderTarget*(size: th::Vf2, filter: Filter): (RenderTarget, std::Err) {\n"
"\n"
"Creates a render target you can draw to, like to a window.\n"
"Filter specifies specfifies filtering for resulting image.\n"
Expand Down Expand Up @@ -4582,7 +4595,7 @@ const char *th_em_moduledocs[] = {
"\n"
"fn Image.setfilter\n"
"\n"
"[3mfn (i: ^Image) setfilter*(filter: int): std::Err {\n"
"[3mfn (i: ^Image) setfilter*(filter: Filter): std::Err {\n"
"\n"
"Sets a mag/min filter. 0 is nearest, others are linear.\n"
"This function will regenerate the texture. This means it shouldn\'t be\n"
Expand Down Expand Up @@ -5629,16 +5642,6 @@ const char *th_em_moduledocs[] = {
"\n"
"---------\n"
"\n"
"Filtering constants\n"
"\n"
"type Filter* = enum {\n"
"\tnearest = 0\n"
"\tlinear = 1\n"
"}\n"
"\n"
"\n"
"---------\n"
"\n"
"interface Font\n"
"\n"
"type Font* = interface {\n"
Expand All @@ -5657,7 +5660,7 @@ const char *th_em_moduledocs[] = {
"\n"
"fn load\n"
"\n"
"[3mfn load*(path: str, size: th::fu, filter: Filter = Filter.linear): (Font, std::Err) {\n"
"[3mfn load*(path: str, size: th::fu, filter: image::Filter = .linear): (Font, std::Err) {\n"
"\n"
"Loads a font from a path and returns it.\n"
"\n"
Expand Down
12 changes: 3 additions & 9 deletions umka/font.um
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
import (
"th.um"
"std.um"
"image.um"
)

//~~Filtering constants
type Filter* = enum {
nearest = 0
linear = 1
}
//~~

// TODO: should this be exported and documented?
type TrueType* = struct { _: ^struct{} }

fn umth_ttf_font_load(f: ^TrueType, path: str, size: th::fu, filter: Filter): th::ErrCode
fn umth_ttf_font_load(f: ^TrueType, path: str, size: th::fu, filter: image::Filter): th::ErrCode
fn umth_ttf_font_draw(font: TrueType, s: str, x: th::fu, y: th::fu, color: uint32, scale: th::fu)
fn umth_ttf_font_measure(font: TrueType, s: str): th::Vf2

Expand Down Expand Up @@ -56,7 +50,7 @@ type Font* = interface {

//~~fn load
// Loads a font from a path and returns it.
fn load*(path: str, size: th::fu, filter: Filter = Filter.linear): (Font, std::Err) {
fn load*(path: str, size: th::fu, filter: image::Filter = .linear): (Font, std::Err) {
//~~
var f: TrueType
ec := umth_ttf_font_load(&f, path, size, filter)
Expand Down
16 changes: 12 additions & 4 deletions umka/image.um
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import (
"th.um"
)

//~~enum Filter
// Filtering mode for images, render targets, and fonts.
type Filter* = enum {
nearest = 0
linear = 1
}
//~~

//~~opaque Image
// Represents a drawable image. It is an opaque structure.
// Images support a color filter. It is applied by multiplying the color
Expand All @@ -16,12 +24,12 @@ type Image* = struct{ _: ^struct{} }
type RenderTarget* = struct { _: ^struct{} }
//~~

fn umth_image_create_render_target(ret: ^RenderTarget, width: int, height: int, filter: int): th::ErrCode
fn umth_image_create_render_target(ret: ^RenderTarget, width: int, height: int, filter: Filter): th::ErrCode
//~~fn createRenderTarget
// Creates a render target you can draw to, like to a window.
// Filter specifies specfifies filtering for resulting image.
// Image can be retrieved via `toImage`.
fn createRenderTarget*(size: th::Vf2, filter: int): (RenderTarget, std::Err) {
fn createRenderTarget*(size: th::Vf2, filter: Filter): (RenderTarget, std::Err) {
//~~
rt := RenderTarget{}
ec := umth_image_create_render_target(&rt, trunc(size.x), trunc(size.y), filter)
Expand Down Expand Up @@ -249,14 +257,14 @@ fn (i: ^Image) copy*(): (Image, std::Err) {
return img, th::__errFromCode(ec)
}

fn umth_image_set_filter(data: Image, filter: int): th::ErrCode
fn umth_image_set_filter(data: Image, filter: Filter): th::ErrCode
//~~fn Image.setfilter
// Sets a mag/min filter. 0 is nearest, others are linear.
// This function will regenerate the texture. This means it shouldn't be
// used in a loop.
// https://learnopengl.com/img/getting-started/texture_filtering.png
// left is nearest, right is linear.
fn (i: ^Image) setfilter*(filter: int): std::Err {
fn (i: ^Image) setfilter*(filter: Filter): std::Err {
//~~
if !i.validate() {
th::__error("image is invalid")
Expand Down

0 comments on commit 453a5d9

Please sign in to comment.