Skip to content
This repository has been archived by the owner on Jun 6, 2020. It is now read-only.

refactor(dpi): check for errors in SDL_GetDisplayDPI #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/basic.re
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ let run = () => {
);

let display = Sdl2.Window.getDisplay(primaryWindow);
let dpi = Sdl2.Display.getDPI(display);
Console.log("Display DPI: " ++ Sdl2.Display.Dpi.show(dpi));

switch (Sdl2.Display.getDPI(display)) {
| Error(msg) => Console.error("Error: " ++ msg)
| Ok(dpi) => Console.log("Display DPI: " ++ Sdl2.Display.Dpi.show(dpi));
};

let mode = Sdl2.Display.getDesktopMode(display);
Console.log("Display mode: " ++ Sdl2.Display.Mode.show(mode));
Expand Down
2 changes: 1 addition & 1 deletion src/sdl2.re
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module Display = {
let getDisplays = () =>
List.init(getNumberOfDisplays(), (i) => (Obj.magic(i: int): t));

external getDPI: t => Dpi.t = "resdl_SDL_GetDisplayDPI";
external getDPI: t => result(Dpi.t, string) = "resdl_SDL_GetDisplayDPI";
external getCurrentMode: t => Mode.t = "resdl_SDL_GetCurrentDisplayMode";
external getDesktopMode: t => Mode.t = "resdl_SDL_GetDesktopDisplayMode";
external getBounds: t => Rect.t = "resdl_SDL_GetDisplayBounds";
Expand Down
17 changes: 12 additions & 5 deletions src/sdl2_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,19 @@ CAMLprim value resdl_SDL_GetDisplayDPI(value vDisplay) {
int displayIndex = Int_val(vDisplay);

float ddpi, hdpi, vdpi;
SDL_GetDisplayDPI(displayIndex, &ddpi, &hdpi, &vdpi);
int errorCode = SDL_GetDisplayDPI(displayIndex, &ddpi, &hdpi, &vdpi);

if (errorCode != 0) {
ret = Val_error(caml_copy_string(SDL_GetError()));
} else {
ret = caml_alloc(3, 0);
Store_double_field(ret, 0, ddpi);
Store_double_field(ret, 1, hdpi);
Store_double_field(ret, 2, vdpi);

ret = Val_ok(ret);
}

ret = caml_alloc(3, 0);
Store_double_field(ret, 0, ddpi);
Store_double_field(ret, 1, hdpi);
Store_double_field(ret, 2, vdpi);
CAMLreturn(ret);
};

Expand Down