Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to transform points to screenspace when window resizes #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion examples/framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ int main() {
framebuffer_2.Draw(sprite);

// Circle under user's mouse.
circle.SetPosition(window.input().cursor());
glm::vec2 mouse = window.ToScreenSpace(window.input().cursor());
circle.SetPosition(mouse);
framebuffer_2.Draw(circle);

// -------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions include/smk/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Window : public RenderTarget {
// Returns true when the user wants to close the window.
bool ShouldClose();

glm::vec2 ToScreenSpace(const glm::vec2& world);

// Move-only ressource.
Window(Window&&);
Window(const Window&) = delete;
Expand All @@ -63,6 +65,7 @@ class Window : public RenderTarget {

private:
GLFWwindow* window_ = nullptr;
glm::vec2 initialSize_{}; // of the window

// Time:
float time_ = 0.f;
Expand Down
7 changes: 7 additions & 0 deletions src/smk/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void GLFWCharCallback(GLFWwindow* glfw_window, unsigned int codepoint) {

} // namespace

glm::vec2 smk::Window::ToScreenSpace(const glm::vec2& world) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is a bit confusing, I would have thought we went from screen space to world coordinate.
What about MapPixelToWorldCoordinates?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the input is the world position and the output is the screenspace. This is pretty standard convention. Unsure how to make this more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MapPixelToWorldCoordinates would be appropriate the more I think about it. SFML uses this name too for that purpose I assume

glm::vec2 factor = glm::vec2(initialSize_.x/width_, initialSize_.y/height_);
return glm::vec2(world.x*factor.x, world.y*factor.y);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The view can also be rotated / skewed.

void RenderTarget::SetView(const View& view) {

  • I think it would be better to use the RenderTarget::project_matrix for this.
  • I don't think we need to keep track of the initial size.
  • I think this can be moved to RenderTarget.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yeah the view could be accounted for. That would make sense and become very useful. My original intention was to match the window size even after resizing. I originally had this event handled by Input and used the resize event for glfw to feed it, but I thought maybe it was more appropriate for the window.

}

/// @brief A null window.
Window::Window() {
id_ = ++id;
Expand All @@ -123,6 +128,7 @@ Window::Window(int width, int height, const std::string& title) {
window_by_id[id_] = this;
width_ = width;
height_ = height;
initialSize_ = {width, height};

glfwSetErrorCallback(GLFWErrorCallback);
// initialize the GLFW library
Expand Down Expand Up @@ -219,6 +225,7 @@ void Window::operator=(Window&& other) {
std::swap(input_, other.input_);
std::swap(id_, other.id_);
std::swap(module_canvas_selector_, other.module_canvas_selector_);
std::swap(initialSize_, other.initialSize_);
window_by_id[id_] = this;
window_by_glfw_window[window_] = this;
}
Expand Down