Skip to content

Commit

Permalink
Fixed a few bugs and added in .CopySurface
Browse files Browse the repository at this point in the history
  • Loading branch information
tabularelf committed Sep 18, 2022
1 parent b301200 commit 6b5b207
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 11 deletions.
1 change: 1 addition & 0 deletions Canvas.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Canvas v1.1.2
# Canvas v1.1.3
A surface that you can modify & keep the contents, even when the surface should've had been lost, for GameMaker Studio 2.3+!

Join my Discord for any questions! https://discord.gg/ThW5exp6r4
Expand Down
1 change: 0 additions & 1 deletion objects/obj_test/Draw_0.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
draw_surface(surf.GetSurfaceID(), 0, 0);

var _string = @"Cache Canvas: Space
Update Surface: Control
Resize Surface: W
Expand Down
8 changes: 8 additions & 0 deletions objects/obj_test_manual_write/Create_0.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
show_debug_overlay(true);
background = layer_background_get_id("Background");
surf = new Canvas(512, 512).WriteToCache(false);
surf.Start();
draw_rectangle_colour(32, 32,surf.GetWidth(),surf.GetHeight(), c_red, c_green, c_blue, c_yellow, false);
surf.Finish();
__prevStatus = 0;
myBuffer = -1;
9 changes: 9 additions & 0 deletions objects/obj_test_manual_write/Draw_0.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
draw_surface(surf.GetSurfaceID(), 0, 0);
var _string = @"Cache Canvas: Space
Update Surface: Control
Resize Surface: W
Get Surface Cache Contents: E (Space for Cached)
Set Surface Cache Contents: Q
Copy Application Surface: V"

draw_text(8,8, _string);
1 change: 1 addition & 0 deletions objects/obj_test_manual_write/Draw_64.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
draw_text(8,8, "Canvas Status: " + string(__prevStatus));
39 changes: 39 additions & 0 deletions objects/obj_test_manual_write/Step_0.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
if (keyboard_check_released(vk_space)) {
surf.Cache();
}

if (keyboard_check(vk_control)) {
surf.Start();
var _offset = (current_time / 1000);
draw_rectangle_colour(32,32,412,412, make_colour_hsv(_offset mod 256, 255, 255), make_colour_hsv(_offset * 20 mod 256, 255, 255), make_colour_hsv(_offset * 30 mod 256, 255, 255), make_colour_hsv(_offset * 40 mod 256, 255, 255), false);
}

if (keyboard_check_released(ord("E"))) {
if (keyboard_check(vk_space)) {
surf.Cache();
}
if (buffer_exists(myBuffer)) {
buffer_delete(myBuffer);
}
myBuffer = surf.GetBufferContents();
}

if (keyboard_check_released(ord("W"))) {
surf.Resize(1024, 1024);
surf.Start();
draw_rectangle_colour(32,32,surf.GetWidth(),surf.GetHeight(), c_red, c_green, c_blue, c_yellow, false);
surf.Finish();
}

if (keyboard_check_released(ord("Q"))) {
surf.SetBufferContents(myBuffer);
}

if (keyboard_check_released(ord("V"))) {
surf.CopySurface(application_surface, 0, 0, true);
}

__prevStatus = surf.GetStatus();
if (surf.GetStatus() == CanvasStatus.IN_USE) {
surf.Finish();
}
1 change: 1 addition & 0 deletions objects/obj_test_manual_write/Step_2.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layer_background_blend(background, make_color_hsv(current_time / 100, 255, 255));
38 changes: 38 additions & 0 deletions objects/obj_test_manual_write/obj_test_manual_write.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions rooms/rm_test/rm_test.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 33 additions & 5 deletions scripts/Canvas/Canvas.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#macro __CANVAS_CREDITS "@TabularElf - https://tabelf.link/"
#macro __CANVAS_VERSION "1.1.2"
#macro __CANVAS_VERSION "1.1.3"
show_debug_message("Canvas " + __CANVAS_VERSION + " initalized! Created by " + __CANVAS_CREDITS);

#macro __CANVAS_HEADER_SIZE 5
Expand Down Expand Up @@ -42,13 +42,14 @@ function Canvas(_width, _height) constructor {

static Finish = function() {
surface_reset_target();
if !(buffer_exists(__buffer)) {
__init();
}
__init();

if (__writeToCache) {
__UpdateCache();
}

__status = CanvasStatus.HAS_DATA;

return self;
}

Expand All @@ -63,6 +64,28 @@ function Canvas(_width, _height) constructor {
}
}

static CopySurface = function(_surfID, _x, _y, _updateCache = __writeToCache) {
if (!surface_exists(_surfID)) {
show_error("Canvas: Surface " + string(_surfID) + " doesn't exist!", true);
}

var _width = surface_get_width(_surfID);
var _height = surface_get_height(_surfID);

if (__width != _width) || (__height != _height) {
Resize(_width, _height);
}

__init();
CheckSurface();

surface_copy(__surface, _x, _y, _surfID);
if (_updateCache) {
__UpdateCache();
}
return self;
}

static Free = function() {
if (buffer_exists(__buffer)) {
buffer_delete(__buffer);
Expand Down Expand Up @@ -97,7 +120,10 @@ function Canvas(_width, _height) constructor {
}
}

static Resize = function(_width, _height) {
static Resize = function(_width, _height, _keepData = false) {

if (__width == _width) && (__height == _height) return self;

if (buffer_exists(__buffer)) {
buffer_delete(__buffer);
}
Expand All @@ -112,6 +138,7 @@ function Canvas(_width, _height) constructor {

__width = _width;
__height = _height;

__status = CanvasStatus.NO_DATA;

return self;
Expand Down Expand Up @@ -265,6 +292,7 @@ function Canvas(_width, _height) constructor {
__UpdateCache();
break;
case CanvasStatus.NO_DATA:
__init();
case CanvasStatus.HAS_DATA:
__UpdateCache();
break;
Expand Down

0 comments on commit 6b5b207

Please sign in to comment.