Display a region of a sprite
by Kostiantyn Teterin · in Torque Game Builder · 07/17/2007 (9:19 am) · 18 replies
I want to make something like a progress bar. So I have a long sprite and I to show a progress, I am going to display a part of the sprite, and I would increase the width of the displayed region.
Is it possible to do that in Torque? The scaling of sprite is not a way to solve this problem because the progress bar sprite has an ornament on it.
Is it possible to do that in Torque? The scaling of sprite is not a way to solve this problem because the progress bar sprite has an ornament on it.
About the author
#2
07/17/2007 (9:41 am)
Is this the only way? I mean, is there any way to display a portion of the sprite?
#3
There are other ways to achieve a similar effect though--but not with a static sprite. For instance, you could put the progress bar into a tile layer with a single tile. Then you could gradually resize the tile layer (this can hide part of the tile layer without stretching the image).
07/17/2007 (9:54 am)
I'm pretty sure that's not part of a static sprite's functionality.There are other ways to achieve a similar effect though--but not with a static sprite. For instance, you could put the progress bar into a tile layer with a single tile. Then you could gradually resize the tile layer (this can hide part of the tile layer without stretching the image).
#4
07/17/2007 (12:17 pm)
This is what i do for my progress bars. I make a static sprite, then make a scenewindow and use the staticsprites attachgui function to put that scenewindow on top of the sprite.
#5
I tried to use tiles. But these 2 lines of code:
%lineTile = new t2dTileLayer() { scenegraph = sceneWindow2D.getSceneGraph(); };
%lineTile.createLayer(11, 1, 10, 8);
Make TGB throw a critical windows error :(
11x1 is the number of tiles, and 10x8 is the size of a single tile.
I am totally sad :( I spent 4 hours trying to find out how to solve this problem.
07/17/2007 (3:50 pm)
Joe: Thanks, but this solution is not working in my case, because I don't really make a progress bar (I took it as a simple example of what I am doing). I have a picture below the line, and I need to draw a line above the picture.I tried to use tiles. But these 2 lines of code:
%lineTile = new t2dTileLayer() { scenegraph = sceneWindow2D.getSceneGraph(); };
%lineTile.createLayer(11, 1, 10, 8);
Make TGB throw a critical windows error :(
11x1 is the number of tiles, and 10x8 is the size of a single tile.
I am totally sad :( I spent 4 hours trying to find out how to solve this problem.
#6
tdn.garagegames.com/wiki/Torque_2D/Reference_Guide#t2dTileLayer
Hope these help. Post if you need specifics or examples. :)
07/17/2007 (4:16 pm)
Heh, I don't think there's a createLayer() method available for tile layers. Are you trying to set its size and position? Here is the list of methods you can call on your tile layers:tdn.garagegames.com/wiki/Torque_2D/Reference_Guide#t2dTileLayer
Hope these help. Post if you need specifics or examples. :)
#7
I saw both lists in TGB documentation and also on TDN. What I don't understand, is how to set up layers and to use them - I can't see any examples neither on TDN, nor in the documentation that is distributed along with the engine :( Truly said, I never felt myself so helpless... Right now I am checking TGB code for a possible inclusion of some features that I need. But I don't really have too much time to understand how it works.
I'll be very grateful for the examples! By the way, how did you learn TorqueScript? Are there some sources of good documentation that I've missed? - I just don't want to spam this forum with my questions, and it seems that I have a new one every day.
ps. What I need to know is how to break a single sprite on tiles and how to display them one by one. And if you will need some help with the ideas for your games - feel free to use me :)
07/17/2007 (4:45 pm)
Melissa:I saw both lists in TGB documentation and also on TDN. What I don't understand, is how to set up layers and to use them - I can't see any examples neither on TDN, nor in the documentation that is distributed along with the engine :( Truly said, I never felt myself so helpless... Right now I am checking TGB code for a possible inclusion of some features that I need. But I don't really have too much time to understand how it works.
I'll be very grateful for the examples! By the way, how did you learn TorqueScript? Are there some sources of good documentation that I've missed? - I just don't want to spam this forum with my questions, and it seems that I have a new one every day.
ps. What I need to know is how to break a single sprite on tiles and how to display them one by one. And if you will need some help with the ideas for your games - feel free to use me :)
#8
07/17/2007 (6:20 pm)
Maybe using a scroller could also be a solution.
#9
07/17/2007 (6:41 pm)
Remember that tile layers don't exist by themselves--tile layers are "contained" by a tile map. A little counter-intuitive for the relatively simply task you want to accomplish, but that is probably why it's not working as you'd expect.
#10
Well, what I am trying to do now is to add a simple command to torque script that will control the width of displayed sprite. And I have another problem - when I recompile the engine using Visual Studio 2005, won't load behaviors anymore (added the error message to console: Missing Behavior). That is pretty strange. I am currently digging into the behavior loader to find the source of the problem.
ps. I am going to modify the sources because I know C++ much better than TorqueScript, and I can't find any good documentation on TorqueScript, especially because most of tutorials are about the level editor and not about the scripting. It will be great if you'll add more articles about scripting to TDN!
pps. As for now, I've found that when I recompile the code, it doesn't compile behaviors anymore :) Hey, are these sources the same that you've used to compile the executable? :)
07/17/2007 (6:50 pm)
Stephen:Well, what I am trying to do now is to add a simple command to torque script that will control the width of displayed sprite. And I have another problem - when I recompile the engine using Visual Studio 2005, won't load behaviors anymore (added the error message to console: Missing Behavior). That is pretty strange. I am currently digging into the behavior loader to find the source of the problem.
ps. I am going to modify the sources because I know C++ much better than TorqueScript, and I can't find any good documentation on TorqueScript, especially because most of tutorials are about the level editor and not about the scripting. It will be great if you'll add more articles about scripting to TDN!
pps. As for now, I've found that when I recompile the code, it doesn't compile behaviors anymore :) Hey, are these sources the same that you've used to compile the executable? :)
#11
07/17/2007 (7:24 pm)
You said above "scaling won't work"--can't you simply use the size parameter in the Y only direction? It would depend on your exact image, but you can certainly control the size of the sprite in either axis via script.
#12
This is how I tried to do that:
function drawRedLine(%p) {
%p.current_x += 10;
if(%p.current_x >= %p.x_size) {
%p.current_x = %p.x_size;
}
%p.size = %p.current_x SPC %p.y_size;
if(%p.current_x < %p.x_size) schedule(30, 0, drawRedLine, %p);
}
What I had is a sprite that was scaled to both sides from center point along the X axis.
07/17/2007 (7:39 pm)
Stephen:This is how I tried to do that:
function drawRedLine(%p) {
%p.current_x += 10;
if(%p.current_x >= %p.x_size) {
%p.current_x = %p.x_size;
}
%p.size = %p.current_x SPC %p.y_size;
if(%p.current_x < %p.x_size) schedule(30, 0, drawRedLine, %p);
}
What I had is a sprite that was scaled to both sides from center point along the X axis.
#13
07/17/2007 (8:04 pm)
Yes, that would cause that...have you tried both changing the size and moving the center of the shape?
#14
Probably the best way to hande this problem is to add a new command to the source code.
07/17/2007 (8:07 pm)
Well, the problem is that I don't need any scaling effect at all. I need it to be looks like someone just draw a noisy line using a pen.Probably the best way to hande this problem is to add a new command to the source code.
#15
It would take some work, but it's possible that you may simply want to develop a new class--something like a "T2DDynamicSprite" which has an imageMap built from a texture that can be modified procedurally. I want to stress that this isn't trivial (TGB has no built in render to texture capability), but with enough exploration it shouldn't be an impossible task either.
07/17/2007 (8:16 pm)
Yeah, I think we may be mis-reading your actual intended result--hard to picture it in my case anyway :)It would take some work, but it's possible that you may simply want to develop a new class--something like a "T2DDynamicSprite" which has an imageMap built from a texture that can be modified procedurally. I want to stress that this isn't trivial (TGB has no built in render to texture capability), but with enough exploration it shouldn't be an impossible task either.
#16
OpenGL lets you render a texture and then clip it. It's done all the time since irregular shapes have the texture end at the edges rather than render off the edge.
It's impossible in script.
07/18/2007 (10:10 am)
You can partially render a sprite if you modify the source. You can render a texture and limit it's boundries with dynamic fields. I've seen it done. You could add it to the t2dStaticSprite rendering code so that every object has that capability.OpenGL lets you render a texture and then clip it. It's done all the time since irregular shapes have the texture end at the edges rather than render off the edge.
It's impossible in script.
#17
That is exactly how I did it today. I've added a new medhot to tdStaticSprite class, and the purpose of this method is to define the region within the sprite that will be rendered.
Well, that was a LOT easier than to understand how those tiles/layers works :)
07/18/2007 (11:00 am)
Nikos:That is exactly how I did it today. I've added a new medhot to tdStaticSprite class, and the purpose of this method is to define the region within the sprite that will be rendered.
Well, that was a LOT easier than to understand how those tiles/layers works :)
#18
07/20/2007 (9:45 am)
Very nice Kostya. Glad it worked out.
Torque Owner Melissa Niiya