GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2008-03-30 23:21:22

RoboBOT
Member
Registered: 2008-02-23
Posts: 6
Website

Drawing Sprites Centered Script

Here is a simple script to find the x and y values at which a sprite should be drawn so that it is centered in the room. This is good for title screens, etc.
One can input either the sprite name or the width (or height).
To find the x value:

Expand/*
**      sprite_centered_x(spr);
**
**      argument0 = either a sprite name or an integer referring
**                  to the width of the thing to be centered
**
**      Returns the x value at which to draw the sprite so that
**                  it is centered in the room
*/
var xx;
if (sprite_exists(argument0)) {
    xx=(room_width-sprite_get_width(argument0)) div 2+sprite_get_xoffset(argument0);
} else {
    xx=(room_width-argument0) div 2;
}
return xx;

To find the y value:

Expand/*
**      sprite_centered_y(spr);
**
**      argument0 = either a sprite name or an integer referring
**                  to the height of the thing to be centered
**
**      Returns the y value at which to draw the sprite so that
**                  it is centered in the room
*/
var yy;
if (sprite_exists(argument0)) {
    yy=(room_height-sprite_get_height(argument0)) div 2+sprite_get_yoffset(argument0);
} else {
    yy=(room_height-argument0) div 2;
}
return yy;

EDIT: After thinking a bit, I think having a second argument giving the width to be centered in would be useful. Then you could center it in the room, the view, or in any specific area by doing something like: x=room_width-235+sprite_centered_x(sprite0,235)  //center sprite0 in the area 235 pixels wide at the right
Thoughts?

Last edited by RoboBOT (2008-03-31 10:38:38)

Offline

#2 2008-03-31 13:03:26

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Drawing Sprites Centered Script

First Thought: why not a script to draw a sprite centered in the room?

Second Thought: center within an optionally(?) given rectangle.

Third Thought: two modes of operation; centering by sprite origin, or absolute centering by sprite dimensions.

Fourth Thought: if first thought not appealing, combine these scripts and have x or y returned based on an argument.


Abusing forum power since 1986.

Offline

#3 2008-03-31 19:07:36

RoboBOT
Member
Registered: 2008-02-23
Posts: 6
Website

Re: Drawing Sprites Centered Script

I thought that giving the x or y value would be more useful. It could be stored in a variable and used to draw multiple things (such as centering several equal-sized buttons).
I've taken your 3rd and 4th suggestions, though:

Expand/*
**      center_sprite(1,sprite0,0,room_width);
**
**      argument0 = true for horizontal (x value), false for vertical (y)
**      argument1 = sprite name
**      argument2 = true for centering the sprite origin, false for centering absolutely
**      argument3 = width or height to center in
**
**      Returns the x or y value at which to draw the sprite so that
**                  it is centered in the given distance
*/
var val;
if (argument0) {
    if (argument2) val=argument3 div 2 else val=(argument3-sprite_get_width(argument1)) div 2+sprite_get_xoffset(argument1);
} else {
    if (argument2) val=argument3 div 2 else val=(argument3-sprite_get_height(argument1)) div 2+sprite_get_yoffset(argument1);
}
return val;

EDIT: please pardon the forced line breaks biggrin

Last edited by RoboBOT (2008-04-27 12:52:02)

Offline

Board footer

Powered by FluxBB