Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics

Today's post will be a technical one: fixing the mouse issue.

Or more specifically, the issue of disabling the game when you click outside of the game-window. It is almost bound to happen when playing in windowed mode (especially for action-heavy games), but can also happen even in full-screen if there is more than one monitor.

This is an especially common feedback I've received from my game's demo. It is very disruptive when it happens in the middle of the action. Definitely can get annoying real quick, so I fixed it!

Crosshair can no longer go beyond the game-window. Pause and you'll regain control of the mouse: 

The following is what I came up with. (Disclaimer: I'm not entirely sure if this is a good solution so if anyone has any better implementation please enlighten me!)

*language is in gml. engine is gamemaker studio(1.4)
var cursor_x = window_mouse_get_x() - window_get_width()/2;
var cursor_y = window_mouse_get_y() - window_get_height()/2;
//Keep mouse to Screen's center

//Mouse.x relative to camera
view_x += cursor_x / global.mouse_offset;
x = view_xview + view_x;
//Limit x to screen
if (x < view_xview) {
    x = view_xview;
    //Cut back view_x's value
    view_x = 0;
if (x > view_xview + view_wview[0]) {
    x = view_xview + view_wview[0];
    //Clamp view_x's value
    view_x = view_wview[0];

//Mouse.y relative to camera
view_y += cursor_y / global.mouse_offset;
y = view_yview + view_y;
//Limit y to screen
if (y < view_yview) {
    y = view_yview;
    //Cut back view_y's value
    view_y = 0;
if (y > view_yview + view_hview[0]) {
    y = view_yview + view_hview[0];
    //Clamp view_x's value
    view_y = view_hview[0];

So basically what happens is that the actual mouse will be locked to the center of the screen with:

Any movement of the mouse will then be calculated and the mouse object will be moved accordingly instead of the actual mouse. The x and y position of the mouse object is also relative to the camera so it'd work even with a moving camera. Because the game's base resolution is 640x360 and scaled up approximately to the monitor's resolution, global.mouse_offset is used for calculating the actual movement of the mouse. If there is no offset, moving 100 pixels to the left would translate to moving 200 pixels if the monitor's resolution is at 1280x720 (twice the base resolution).

Hope someone finds this useful!