Skip to content

Commit

Permalink
Make sure to properly handle High DPI displays when calculating the p…
Browse files Browse the repository at this point in the history
…rojection

Fixes #7
  • Loading branch information
britzl committed Aug 17, 2018
1 parent 9e58906 commit cb7ab67
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
54 changes: 54 additions & 0 deletions example/example.collection
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,60 @@ embedded_instances {
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"position\"\n"
" type: \"label\"\n"
" data: \"size {\\n"
" x: 128.0\\n"
" y: 32.0\\n"
" z: 0.0\\n"
" w: 0.0\\n"
"}\\n"
"scale {\\n"
" x: 1.0\\n"
" y: 1.0\\n"
" z: 1.0\\n"
" w: 0.0\\n"
"}\\n"
"color {\\n"
" x: 1.0\\n"
" y: 1.0\\n"
" z: 1.0\\n"
" w: 1.0\\n"
"}\\n"
"outline {\\n"
" x: 0.0\\n"
" y: 0.0\\n"
" z: 0.0\\n"
" w: 1.0\\n"
"}\\n"
"shadow {\\n"
" x: 0.0\\n"
" y: 0.0\\n"
" z: 0.0\\n"
" w: 1.0\\n"
"}\\n"
"leading: 1.0\\n"
"tracking: 0.0\\n"
"pivot: PIVOT_CENTER\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"line_break: false\\n"
"text: \\\"\\\"\\n"
"font: \\\"/example/assets/fonts/silkscreen.font\\\"\\n"
"material: \\\"/builtins/fonts/label.material\\\"\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: -34.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
Expand Down
4 changes: 3 additions & 1 deletion example/player.script
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ end
function update(self, dt)
-- update crosshair based on mouse and camera position
local cursor_pos = camera.screen_to_world(go.get_id("camera"), self.crosshair)
go.set_position(vmath.lerp(0.5, go.get_position("crosshair"), cursor_pos), "crosshair")
cursor_pos = vmath.lerp(0.5, go.get_position("crosshair"), cursor_pos)
go.set_position(cursor_pos, "crosshair")
label.set_text("crosshair#position", ("%.2f x %.2f"):format(cursor_pos.x, cursor_pos.y))

-- rotate towards cursor
local player_world = go.get_world_position()
Expand Down
7 changes: 6 additions & 1 deletion game.project
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
title = Orthographic
version = 0.9
dependencies =
dependencies =

[bootstrap]
main_collection = /example/example.collectionc
Expand All @@ -13,6 +13,7 @@ game_binding = /input/game.input_bindingc
[display]
width = 1280
height = 720
high_dpi = 1

[physics]
scale = 0.02
Expand All @@ -27,3 +28,7 @@ include_dirs = orthographic

[graphics]
max_debug_vertices = 50000

[label]
subpixels = 0

20 changes: 16 additions & 4 deletions orthographic/render/orthographic.render_script
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ if sys.get_config("script.shared_state") ~= "1" then
error("ERROR - camera - 'shared_state' setting in game.project must be enabled for camera to work.")
end

local high_dpi = sys.get_config("display.high_dpi", "0") == "1"

local CLEAR_COLOR = hash("clear_color")
local SET_VIEW_PROJECTION = hash("set_view_projection")
local SET_CAMERA_OFFSET = hash("set_camera_offset")
Expand All @@ -31,15 +33,25 @@ function init(self)
self.world_projection = vmath.matrix4()

self.screen_view = vmath.matrix4()
end

self.window_width = nil
self.window_height = nil
end

function update(self)
local window_width = render.get_window_width()
local window_height = render.get_window_height()

-- update window width/height for camera (used by the projections)
camera.set_window_size(window_width, window_height)
if self.window_width ~= window_width or self.window_height ~= window_height then
self.window_width = window_width
self.window_height = window_height

-- update window width/height for camera (used by the projections)
if high_dpi then
camera.set_window_size(window_width / 2, window_height /2)
else
camera.set_window_size(window_width, window_height)
end
end


-- clear color
Expand Down

0 comments on commit cb7ab67

Please sign in to comment.