Skip to content

Commit

Permalink
Fix window resizing, add FPS toggle and keyboard orbit
Browse files Browse the repository at this point in the history
  • Loading branch information
scarletcafe committed Feb 8, 2020
1 parent 98e3093 commit b1d59ce
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
48 changes: 30 additions & 18 deletions eldstar_server/src/controls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,34 @@ void orbiting_camera_control(eldstar::window& w, gl::perspective_camera& camera)
camera.target += move * pixels_to_world;
}

// Orbiting
float pixels_to_radians = -glm::radians(120.0f) / static_cast<float>(w.gl_window.get_height());
float keyboard_speed = (10.0f * multiplier) / pixels_to_radians;
glm::vec2 cursor = glm::vec2(0.0f, 0.0f);

// Right click orbiting
if (w.input_state->mouse[1].action < 2) {
float pixels_to_radians = -glm::radians(120.0f) / static_cast<float>(w.gl_window.get_height());
glm::vec2 cursor = w.input_state->cursor_delta;
glm::vec4 orbit_delta = glm::vec4(camera.position - camera.target, 1.0f);

float pitch = glm::atan(orbit_delta.y, glm::sqrt(glm::pow(orbit_delta.x, 2.0f) + glm::pow(orbit_delta.z, 2.0f)));
float pitch_delta = cursor.y * pixels_to_radians;

camera.position = camera.target + glm::vec3(glm::rotate(
glm::abs(pitch - pitch_delta) > (glm::pi<float>() / 2.0f) ? glm::mat4(1.0f) : glm::rotate(
glm::mat4(1.0f),
cursor.y * pixels_to_radians,
right
),
cursor.x * pixels_to_radians,
glm::vec3(0.0f, 1.0f, 0.0f)
) * orbit_delta);
}
if (w.input_state->mouse[1].action < 2) cursor += w.input_state->cursor_delta;
// Keyboard orbit support
if (w.input_state->keyboard[GLFW_KEY_I].action < 2) cursor.y -= keyboard_speed;
if (w.input_state->keyboard[GLFW_KEY_K].action < 2) cursor.y += keyboard_speed;
if (w.input_state->keyboard[GLFW_KEY_J].action < 2) cursor.x -= keyboard_speed;
if (w.input_state->keyboard[GLFW_KEY_L].action < 2) cursor.x += keyboard_speed;

glm::vec4 orbit_delta = glm::vec4(camera.position - camera.target, 1.0f);

float pitch = glm::atan(orbit_delta.y, glm::sqrt(glm::pow(orbit_delta.x, 2.0f) + glm::pow(orbit_delta.z, 2.0f)));
float pitch_delta = cursor.y * pixels_to_radians;

camera.position = camera.target + glm::vec3(glm::rotate(
glm::abs(pitch - pitch_delta) > (glm::pi<float>() / 2.0f) ? glm::mat4(1.0f) : glm::rotate(
glm::mat4(1.0f),
cursor.y * pixels_to_radians,
right
),
cursor.x * pixels_to_radians,
glm::vec3(0.0f, 1.0f, 0.0f)
) * orbit_delta);


// Reset with Numpad 0
if (w.input_state->keyboard[GLFW_KEY_KP_0].action == input::down) {
Expand Down Expand Up @@ -230,6 +239,9 @@ bool menu_control(eldstar::window& w, resource_manager& r, gl::perspective_camer
}
);
}),
menu_option("Toggle showing FPS", [&w](void* ptr) {
w.show_fps = !w.show_fps;
}),
menu_option("Exit", [&w](void* ptr) { w.close(); }),
},
[&w]() { w.active_menu.reset(); }
Expand Down
13 changes: 13 additions & 0 deletions eldstar_server/src/entrypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ int main(int argc, char** argv) {
// Render Eldstar version
glm::vec2 version_end = resources->opensans.render_utf8_bordered("Eldstar v1.0." + std::to_string(GIT_REV_COUNT) + "b", glm::vec2(20.0f, 45.0f));

if (window->show_fps) {
float delta = static_cast<float>(window->delta_time);
if (delta != 0.0f) {
float fps = 1.0f / delta;
std::stringstream buffer;
buffer.precision(4);
buffer << "Window FPS: " << fps;
resources->opensans.render_utf8_bordered(buffer.str(), glm::vec2(20.0f, 70.0f));
}
else
resources->opensans.render_utf8_bordered("Window FPS: inf", glm::vec2(20.0f, 70.0f));
}

if (window->recording) {
resources->text.set(color, glm::vec3(1.0f, 0.5f, 0.5f));
resources->opensans.render_utf8_bordered(" (recording)", version_end);
Expand Down
12 changes: 7 additions & 5 deletions eldstar_server/src/gl/glfw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class glfw {

class window {
public:
window(int width, int height, const char *title, bool make_context = false, GLFWmonitor* monitor = NULL, GLFWwindow* share = NULL) : width(width), height(height) {
window(int width, int height, const char *title, bool make_context = false, GLFWmonitor* monitor = NULL, GLFWwindow* share = NULL) {
ptr = glfwCreateWindow(width, height, title, monitor, share);

if (ptr == NULL) {
Expand All @@ -63,22 +63,25 @@ class window {
}

int get_width() const {
int width, height;
glfwGetWindowSize(ptr, &width, &height);
return width;
}

int get_height() const {
int width, height;
glfwGetWindowSize(ptr, &width, &height);
return height;
}

void set_size(int width, int height) {
this->width = width;
this->height = height;

glfwSetWindowSize(ptr, width, height);
glViewport(0, 0, width, height);
}

float aspect_ratio() const {
int width, height;
glfwGetWindowSize(ptr, &width, &height);
return static_cast<float>(width) / static_cast<float>(height);
}

Expand All @@ -96,7 +99,6 @@ class window {

private:
GLFWwindow* ptr;
int width, height;
};

} // gl
Expand Down
8 changes: 7 additions & 1 deletion eldstar_server/src/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class window {
window()
: gl_window(1184, 800, "Eldstar", true),
start_time(glfwGetTime()), last_time(start_time),
wireframe(false), mirror_game_camera(false), recording(false), recording_has_ui(false), color_mode(0)
wireframe(false), mirror_game_camera(false), recording(false), recording_has_ui(false), color_mode(0), show_fps(false)
{
// Enable depth-testing and set the blend function
glEnable(GL_DEPTH_TEST);
Expand All @@ -50,6 +50,11 @@ class window {
if (!global_state_manager.lock())
global_state_manager = input_state;

glfwSetFramebufferSizeCallback(gl_window.get(), [](GLFWwindow* w, int width, int height){
glfwSetWindowSize(w, width, height);
glViewport(0, 0, width, height);
});

glfwSetKeyCallback(gl_window.get(), [](GLFWwindow* w, int key, int scan_code, int action, int mods){
if (auto spt = global_state_manager.lock()) {
spt->keyboard_event(key, action, mods);
Expand Down Expand Up @@ -109,6 +114,7 @@ class window {
bool mirror_game_camera;
bool recording;
bool recording_has_ui;
bool show_fps;
int color_mode;

std::unique_ptr<menu> active_menu;
Expand Down

0 comments on commit b1d59ce

Please sign in to comment.