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

GameMaker Studio 2 support #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
prettylight.gmx/Configs/
prettylight.gmx/help.rtf
prettylight.yyp/options
.DS_Store
.vscode
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@

A powerful lighting engine for use with GameMaker: Studio. It makes use of surfaces and shaders to give you a simple to use yet optimal solution to your problems in need of lighting up!

Now includes beta GMS2 support! If you encounter any issues using it with GMS2, please let us know.

<div style="display: flex;">
<img src="http://i.imgur.com/8lQR4pk.png" height="200px">
<img src="http://i.imgur.com/820eNjo.png" height="200px">
<img src="https://i.imgur.com/8lQR4pk.png" height="200px">
<img src="https://i.imgur.com/820eNjo.png" height="200px">
</div>

## Usage

### Prerequisites
### Prerequisites (GameMaker: Studio)

- [GameMaker: Studio 1](http://www.yoyogames.com/gamemaker)
- GameMaker: Studio 1
- The required [scripts](https://github.com/niksudan/prettylight/tree/master/prettylight.gmx/scripts) and [shaders](https://github.com/niksudan/prettylight/tree/master/prettylight.gmx/shaders) added to your project
- Light objects
- A view

### Prerequisites (Gamemaker Studio 2)

- [Gamemaker Studio 2](http://www.yoyogames.com/gamemaker)
- The required [scripts](INSERT LINK!!!) and [shaders](INSERT LINK!!!) added to your project (You can drag scripts in but you'll have to manually create the shaders with copy/paste)
- Light objects
- A view with a camera

### Registration

You should make use of a game control object to register settings and such for the framework.

- Initialise the system in the **create** event with `pl_init()`
- Process the system in the **step/begin step** event with `pl_update()`
- Render the lights in the **draw** event with `pl_draw()`
- Clear the system in the **room end/game end** event with `pl_end()`
- Clear the system in the **room end/game end/cleanup** event with `pl_end()`

### Adding Lights

Expand Down
9 changes: 9 additions & 0 deletions dist/GMS2/scripts/pl_add.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// @description Add light object(s) to a prettylight system
/// @function pl_add
/// @param obj1
/// @param obj2
/// @param ...

for (var i = 0; i < argument_count; i++) {
ds_list_add(_pl_lightObjects, argument[i]);
}
15 changes: 15 additions & 0 deletions dist/GMS2/scripts/pl_draw.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// @description Render lights in the draw event
/// @function pl_draw

var cam = view_get_camera(0);

// Draw vivid lightmap
if (_pl_vivid) {
gpu_set_blendmode(bm_add);
draw_surface_ext(_pl_lightmap, camera_get_view_x(cam), camera_get_view_y(cam), 1, 1, 0, c_white, 0.1);
}

// Draw standard lightmap
gpu_set_blendmode_ext(bm_dest_color, bm_src_color);
draw_surface(_pl_lightmap, camera_get_view_x(cam), camera_get_view_y(cam));
gpu_set_blendmode(bm_normal);
11 changes: 11 additions & 0 deletions dist/GMS2/scripts/pl_end.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// @description Clear a prettylight system
/// @function pl_end

surface_free(_pl_lightmap);
surface_free(_pl_blurmap);

for (var i = 0; i < ds_list_size(_pl_lightObjects); i++) {
with (ds_list_find_value(_pl_lightObjects , i)) {
pl_light_destroy();
}
}
25 changes: 25 additions & 0 deletions dist/GMS2/scripts/pl_init.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// @description Initialise the prettylight system in the create event
/// @function pl_init

var cam = view_get_camera(0);

// Setup light object list
_pl_lightObjects = ds_list_create();

// Setup the global light surface
_pl_lightmap = surface_create(camera_get_view_width(cam), camera_get_view_height(cam));
_pl_vivid = true;

// Setup blurring
_pl_blurmap = surface_create(camera_get_view_width(cam), camera_get_view_height(cam));
_pl_blurring = true;

// Set options
pl_option_set_ambience(make_color_rgb(1, 16, 32), 0.8);
pl_option_set_blur(5, 0.25);

// Initialise gaussian blur shader
_pl_uni_resolution_hoz = shader_get_uniform(pl_shd_gaussian_horizontal, "resolution");
_pl_uni_resolution_vert = shader_get_uniform(pl_shd_gaussian_vertical, "resolution");
_pl_var_resolution_x = camera_get_view_width(cam) / _pl_blurringAmount;
_pl_var_resolution_y = camera_get_view_height(cam) / _pl_blurringAmount;
4 changes: 4 additions & 0 deletions dist/GMS2/scripts/pl_light_destroy.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// @description Destroy the current light.
/// @function pl_light_destroy

surface_free(_pl_lightSurface);
7 changes: 7 additions & 0 deletions dist/GMS2/scripts/pl_light_draw.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// @description Draw the light [Needs clarification in docs]
/// @function pl_light_draw

var cam = view_get_camera(0);

// Draw light
draw_surface(_pl_lightSurface, x - _pl_baseRadius - camera_get_view_x(cam), y - _pl_baseRadius - camera_get_view_y(cam));
4 changes: 4 additions & 0 deletions dist/GMS2/scripts/pl_light_get_alpha.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// @description Get the transparency of the light.
/// @function pl_light_get_alpha

return _pl_alpha;
4 changes: 4 additions & 0 deletions dist/GMS2/scripts/pl_light_get_color.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// @description Get the colour of the light.
/// @function pl_light_get_color

return _pl_color;
4 changes: 4 additions & 0 deletions dist/GMS2/scripts/pl_light_get_radius.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// @description Get the radius of the light.
/// @function pl_light_get_radius

return _pl_radius;
14 changes: 14 additions & 0 deletions dist/GMS2/scripts/pl_light_init.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// @description Initialise a new light.
/// @function pl_light_init
/// @param radius Radius of the light
/// @param Colour Colour of the light
/// @param Alpha Transparency of the light

// Set light properties
_pl_radius = argument0;
_pl_baseRadius = argument0;
_pl_color = argument1;
_pl_alpha = argument2;

// Setup the individual light surface
_pl_lightSurface = surface_create(_pl_radius * 2, _pl_radius * 2);
5 changes: 5 additions & 0 deletions dist/GMS2/scripts/pl_light_set_alpha.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// @description Set the transparency of the light.
/// @function pl_light_set_alpha
/// @param value The new transparency

_pl_alpha = argument0;
5 changes: 5 additions & 0 deletions dist/GMS2/scripts/pl_light_set_color.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// @description Set the colour of the light.
/// @function pl_light_set_color
/// @param value The color to set

_pl_color = argument0;
5 changes: 5 additions & 0 deletions dist/GMS2/scripts/pl_light_set_radius.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// @description Set the radius of the light.
/// @function pl_light_set_radius
/// @param value Radius to set

_pl_radius = clamp(argument0, 1, _pl_baseRadius);
21 changes: 21 additions & 0 deletions dist/GMS2/scripts/pl_light_update.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// @description Update the light [Needs clarification in docs]
/// @function pl_light_update

// Prepare the light surface
if (!surface_exists(_pl_lightSurface)) {
_pl_lightSurface = surface_create(_pl_baseRadius * 2, _pl_baseRadius * 2);
}
surface_set_target(_pl_lightSurface);

draw_clear(c_black);

// Draw light
draw_set_alpha(_pl_alpha);
draw_circle_color(_pl_baseRadius, _pl_baseRadius, _pl_radius, _pl_color, c_black, false);

// Reset
draw_set_alpha(1);
draw_set_color(c_black);

// Reset
surface_reset_target();
7 changes: 7 additions & 0 deletions dist/GMS2/scripts/pl_option_set_ambience.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// @description Set the ambience colour and brightness.
/// @function pl_option_set_ambience
/// @param color
/// @param brightness

_pl_ambientColor = argument0;
_pl_ambientBrightness = argument1;
7 changes: 7 additions & 0 deletions dist/GMS2/scripts/pl_option_set_blur.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// @description Set the global blurring amount and transparency.
/// @function pl_option_set_blur
/// @param amount Amout to blur
/// @param alpha Blur transparency?

_pl_blurringAmount = argument0;
_pl_blurringAlpha = argument1;
5 changes: 5 additions & 0 deletions dist/GMS2/scripts/pl_set_blurring.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// @description Set whether blurring should be active.
/// @function pl_set_blurring
/// @param active? Whether to use blurring or not

_pl_blurring = argument0;
5 changes: 5 additions & 0 deletions dist/GMS2/scripts/pl_set_vivid.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// @description Set whether vivid lighting should be active globally.
/// @function pl_set_vivid
/// @param active? Should vivid lighting be active?

_pl_vivid = argument0;
73 changes: 73 additions & 0 deletions dist/GMS2/scripts/pl_update.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/// @description Update the prettylight system
/// @function pl_update

var cam = view_get_camera(0);

// Update lights
for (var i = 0; i < ds_list_size(_pl_lightObjects); i++) {
with (ds_list_find_value(_pl_lightObjects , i)) {
pl_light_update();
}
}

// Prepare lightmap
if (!surface_exists(_pl_lightmap)) {
_pl_lightmap = surface_create(camera_get_view_width(cam), camera_get_view_height(cam));
}

surface_set_target(_pl_lightmap);

// Draw ambience
draw_clear(c_black);
draw_set_alpha(_pl_ambientBrightness);
draw_set_color(_pl_ambientColor);
draw_rectangle(0, 0, surface_get_width(_pl_lightmap), surface_get_height(_pl_lightmap), false);

// Draw lights
draw_set_alpha(1);
gpu_set_blendmode(bm_add);
for (var i = 0; i < ds_list_size(_pl_lightObjects); i++) {
with (ds_list_find_value(_pl_lightObjects , i)) {
pl_light_draw();
}
}

// Reset
surface_reset_target();
draw_set_colour(c_white);
gpu_set_blendmode(bm_normal);

if (_pl_blurring) {

// Prepare blurmap
if (!surface_exists(_pl_blurmap)) {
_pl_blurmap = surface_create(camera_get_view_width(cam), camera_get_view_height(cam));
}

surface_set_target(_pl_blurmap);

// Draw blur
draw_clear(c_black);
gpu_set_blendmode(bm_add);

shader_set(pl_shd_gaussian_horizontal);
shader_set_uniform_f(_pl_uni_resolution_hoz, _pl_var_resolution_x, _pl_var_resolution_y);
draw_surface(_pl_lightmap, 0, 0);
shader_reset();

surface_reset_target();

surface_set_target(_pl_lightmap);

draw_clear(c_black);

shader_set(pl_shd_gaussian_vertical);
shader_set_uniform_f(_pl_uni_resolution_vert, _pl_var_resolution_x, _pl_var_resolution_y);
draw_surface(_pl_blurmap, 0, 0);
shader_reset();

// Reset
surface_reset_target();
draw_set_colour(c_white);
gpu_set_blendmode(bm_normal);
}
22 changes: 22 additions & 0 deletions dist/GMS2/shaders/pl_shd_gaussian_horizontal.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
varying vec2 v_texcoord;

uniform float time;
uniform vec2 mouse_pos;
uniform vec2 resolution;

void main()
{
float blurSize = 1.0/resolution.x;

vec4 sum = vec4(0.0);
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x - 4.0*blurSize, v_texcoord.y)) * 0.05;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x - 3.0*blurSize, v_texcoord.y)) * 0.09;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x - 2.0*blurSize, v_texcoord.y)) * 0.12;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x - blurSize, v_texcoord.y)) * 0.15;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y)) * 0.16;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x + blurSize, v_texcoord.y)) * 0.15;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x + 2.0*blurSize, v_texcoord.y)) * 0.12;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x + 3.0*blurSize, v_texcoord.y)) * 0.09;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x + 4.0*blurSize, v_texcoord.y)) * 0.05;
gl_FragColor = sum;
}
16 changes: 16 additions & 0 deletions dist/GMS2/shaders/pl_shd_gaussian_horizontal.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Horizontal gaussian blur shader
*
* @author xygthop3
*/

attribute vec3 in_Position;
attribute vec2 in_TextureCoord;

varying vec2 v_texcoord;

void main()
{
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
v_texcoord = in_TextureCoord;
}
22 changes: 22 additions & 0 deletions dist/GMS2/shaders/pl_shd_gaussian_vertical.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
varying vec2 v_texcoord;

uniform float time;
uniform vec2 mouse_pos;
uniform vec2 resolution;

void main()
{
float blurSize = 1.0/resolution.y;

vec4 sum = vec4(0.0);
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y- 4.0*blurSize)) * 0.05;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y- 3.0*blurSize)) * 0.09;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y - 2.0*blurSize)) * 0.12;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y - blurSize)) * 0.15;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y)) * 0.16;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y + blurSize)) * 0.15;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y + 2.0*blurSize)) * 0.12;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y + 3.0*blurSize)) * 0.09;
sum += texture2D(gm_BaseTexture, vec2(v_texcoord.x, v_texcoord.y + 4.0*blurSize)) * 0.05;
gl_FragColor = sum;
}
16 changes: 16 additions & 0 deletions dist/GMS2/shaders/pl_shd_gaussian_vertical.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Vertical gaussian blur shader
*
* @author xygthop3
*/

attribute vec3 in_Position;
attribute vec2 in_TextureCoord;

varying vec2 v_texcoord;

void main()
{
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
v_texcoord = in_TextureCoord;
}
Loading