Implemented relative mouse mode

This commit is contained in:
CobaltXII
2018-12-30 15:35:24 -05:00
parent 17e82e417a
commit a19c3d8be7

View File

@@ -123,7 +123,7 @@ int main(int argc, char** argv)
// Allocate a new world*.
world* the_world = allocate_world(32, 32, 32);
world* the_world = allocate_world(128, 128, 128);
for (int x = 0; x < the_world->x_res; x++)
for (int y = 0; y < the_world->y_res; y++)
@@ -168,8 +168,8 @@ int main(int argc, char** argv)
const float friction = 0.9f;
// Create variables to store the position of the mouse pointer and the
// state of the mouse buttons.
// Create variables to store the position of the mouse pointer, the state
// of the mouse buttons, and the relative mouse mode.
int sdl_mouse_x = 0;
int sdl_mouse_y = 0;
@@ -177,6 +177,8 @@ int main(int argc, char** argv)
bool sdl_mouse_l = false;
bool sdl_mouse_r = false;
bool sdl_mouse_relative = false;
// The sdl_iteration counter is incremented every frame.
unsigned long long sdl_iteration = 0;
@@ -208,8 +210,25 @@ int main(int argc, char** argv)
{
// The mouse moved.
sdl_mouse_x = e.motion.x;
sdl_mouse_y = e.motion.y;
if (sdl_mouse_relative)
{
sdl_mouse_x += e.motion.xrel;
sdl_mouse_y += e.motion.yrel;
if (sdl_mouse_y > sdl_y_res - 1)
{
sdl_mouse_y = sdl_y_res - 1;
}
else if (sdl_mouse_y < 0)
{
sdl_mouse_y = 0;
}
}
else
{
sdl_mouse_x = e.motion.x;
sdl_mouse_y = e.motion.y;
}
}
else if (e.type == SDL_MOUSEBUTTONDOWN)
{
@@ -250,6 +269,16 @@ int main(int argc, char** argv)
}
}
// Enable relative mouse mode when the left mouse button is down and
// relative mouse mode is currently off.
if (sdl_mouse_relative == false && sdl_mouse_l == true)
{
SDL_SetRelativeMouseMode(SDL_TRUE);
sdl_mouse_relative = SDL_GetRelativeMouseMode();
}
// Calculate the looking direction of the camera.
float rot_x_deg_want = (float(sdl_mouse_y) - (float(sdl_y_res) / 2.0f)) / float(sdl_y_res) * 180.0f;