Skip to content

Commit cebb15f

Browse files
authored
Update camera's screen size on update() instead of previous draw() (#3175)
These changes ensure the screen size used in Camera::update() will match that used by the next ::draw(), instead of sometimes being one frame behind when the SuperTux window is resized. They also may ensure that when the scale is being changed over time, the scale-divided screen size observed by Squirrel will update more frequently. By reducing the number of public functions which can modify the Camera state, they should make Camera very slightly easier to refactor.
1 parent 35e92ba commit cebb15f

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

src/editor/editor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@ Editor::draw(Compositor& compositor)
173173
camera.move((m_mouse_pos - Vector(static_cast<float>(SCREEN_WIDTH - 128),
174174
static_cast<float>(SCREEN_HEIGHT - 32)) / 2.f) / CAMERA_ZOOM_FOCUS_PROGRESSION);
175175

176-
// Update the camera's screen size variable, so it can properly be kept in sector bounds.
177-
camera.draw(context);
178176
keep_camera_in_bounds();
179177
}
180178
m_new_scale = 0.f;

src/object/camera.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,21 +298,15 @@ Camera::scroll_to(const Vector& goal, float scrolltime)
298298
m_mode = Mode::SCROLLTO;
299299
}
300300

301-
void
302-
Camera::draw(DrawingContext& context)
303-
{
304-
context.push_transform();
305-
context.transform().scale = get_current_scale();
306-
307-
m_screen_size = Sizef(context.get_width(),
308-
context.get_height());
309-
310-
context.pop_transform();
311-
}
312-
313301
void
314302
Camera::update(float dt_sec)
315303
{
304+
// Fetch the current screen size from the VideoSystem. The main loop always
305+
// processes input and window events, updates game logic, and then draws zero
306+
// or more frames, in that order, so this screen size will be used by the next
307+
// draw() operation.
308+
m_screen_size = Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)) / get_current_scale();
309+
316310
// Minimum scale should be set during the update sequence; else, reset it.
317311
m_enfore_minimum_scale = false;
318312

@@ -348,15 +342,17 @@ Camera::update(float dt_sec)
348342
void
349343
Camera::keep_in_bounds(const Rectf& bounds)
350344
{
345+
Sizef screen_size = Sizef(static_cast<float>(SCREEN_WIDTH), static_cast<float>(SCREEN_HEIGHT)) / get_current_scale();
346+
351347
// Determines the difference between normal and scaled translation.
352-
const Vector scale_factor = (m_screen_size.as_vector() * (get_current_scale() - 1.f)) / 2.f;
348+
const Vector scale_factor = (screen_size.as_vector() * (get_current_scale() - 1.f)) / 2.f;
353349

354350
// Keep the translation's scaled position in provided bounds.
355-
m_translation.x = (bounds.get_width() > m_screen_size.width ?
356-
math::clamp(m_translation.x + scale_factor.x, bounds.get_left(), bounds.get_right() - m_screen_size.width) :
351+
m_translation.x = (bounds.get_width() > screen_size.width ?
352+
math::clamp(m_translation.x + scale_factor.x, bounds.get_left(), bounds.get_right() - screen_size.width) :
357353
bounds.get_left());
358-
m_translation.y = (bounds.get_height() > m_screen_size.height ?
359-
math::clamp(m_translation.y + scale_factor.y, bounds.get_top(), bounds.get_bottom() - m_screen_size.height) :
354+
m_translation.y = (bounds.get_height() > screen_size.height ?
355+
math::clamp(m_translation.y + scale_factor.y, bounds.get_top(), bounds.get_bottom() - screen_size.height) :
360356
bounds.get_top());
361357

362358
// Remove any scale factor we may have added in the checks above.

src/object/camera.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Camera final : public LayerObject,
5858
/** \addtogroup GameObject
5959
@{ */
6060
virtual void update(float dt_sec) override;
61-
virtual void draw(DrawingContext& ) override;
61+
virtual void draw(DrawingContext& ) override {}
6262

6363
virtual bool is_singleton() const override { return true; }
6464
virtual bool is_saveable() const override;

0 commit comments

Comments
 (0)