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

Add fill_rect() method to Image class #3259

Closed
fire-forge opened this issue Sep 5, 2021 · 2 comments · Fixed by godotengine/godot#52456
Closed

Add fill_rect() method to Image class #3259

fire-forge opened this issue Sep 5, 2021 · 2 comments · Fixed by godotengine/godot#52456
Milestone

Comments

@fire-forge
Copy link

Describe the project you are working on

A 2d platformer game with procedurally generated levels.

Describe the problem or limitation you are having in your project

There is not a function to fill a rectangular section of an Image with a color. However, there is a function to fill the entire image with a color - Image.fill(color: Color).

Describe the feature / enhancement and how it helps to overcome the problem or limitation

This function would fill a section of an image, defined by a Rect2, with a single color.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

It would look like this: Image.fill_rect(rect: Rect2, color: Color) I know nothing about C++, but I would assume this would be relatively easy to add.

If this enhancement will not be used often, can it be worked around with a few lines of script?

I think it would be used often enough to justify adding it to the Godot API, but there are two ways in which this could be done now:

  1. Create an Image, set its size to the size of your fill Rect2, fill the image with a color, and use blit_rect on the original image with the new image.
func fill_rect(image: Image, rect: Rect2, color: Color) -> void:
	var fill_image: = Image.new()
	fill_image.create(rect.size.x, rect.size.y, false, Image.FORMAT_RGBA8)
	fill_image.lock()
	fill_image.fill(color)
	image.blit_rect(fill_image, rect, rect.position)
  1. Use for loops to set each individual pixel.
func fill_rect(image: Image, rect: Rect2, color: Color) -> void:
	var rect_position: = rect.position
	for x in rect.size.x:
		for y in rect.size.y:
			image.set_pixel(x + rect_position.x, y + rect_position.y, color)

Is there a reason why this should be core and not an add-on in the asset library?

It is a part of the built-in Image class, which can't be modified by add-ons.

@kleonc
Copy link
Member

kleonc commented Sep 7, 2021

Besides the usability it'll be more performant. I've opened a PR.

@Xrayez
Copy link
Contributor

Xrayez commented Sep 7, 2021

A 2d platformer game with procedurally generated levels.

Do you use BitMap.opaque_to_polygons() by any chance? What you find this kind of functionality useful for exactly?

If for some reason godotengine/godot#52456 won't be merged, then feel free to contribute to Goost, if you wish: goostengine/goost#7.

@Calinou Calinou changed the title Add fill_rect method to Image class Add fill_rect() method to Image class Sep 7, 2021
@akien-mga akien-mga added this to the 4.0 milestone Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants