how to change fontsize during the game in XNA? - xna

I need to use different fontsize of spritefont, Have to I create new spritefont for the each size?

Basically yes.
There is an overload of SpriteBatch.DrawString that gives you an option to scale your text.
However the major downside to this is that your text will become pixelated as you scale it up. If you start at a higher resolution and scale down you will start to get artefacts as you get to smaller sizes.
So if you have a fixed number of sizes, you should create multiple versions of your sprite font at the different sizes you require.
If you want continuously scalable text with sharp edges, you could perhaps look into vector fonts. The Nuclex Framework has some code to do that.

You can also make your font at the largest size you need and scale down from there.

Suppose the SpriteFont you are using is named x.spritefont.
Do the following to create new SpriteFont for each size.
Open the x.spritefont file from solution explorer.
Go to the tag and edit it to your desired font size.
To make multiple size font, duplicate the file and change the
tags accordingly. Rename the files with size appended at last for
easy remembering.
Now create multiple instances of SpriteFont and load them accordingly.
SpriteFont sf_s10;
SpriteFont sf_s14;
protected override void LoadContent()
{
sf_s10 = Content.Load<SpriteFont>("x_10");
sf_s14 = Content.Load<SpriteFont>("x_14");
//OTHER LOADS
}
to dynamically change fontSize, do the following:
SpriteFont current_font;
protected override void Update(GameTime gameTime)
{
if(/*SOME_CONDITION_TO_DECREASE_SIZE*/)
current_font=sf_s10;
if(/*SOME_CONDITION_TO_INCREASE_SIZE*/)
current_font=sf_s14;
}

Related

setting extent using setExtent in blackberry

I want to make use of the setExtent() method in blackberry.
Every time i use setExtent(), it allocates the space from the zero, zero coordinate to any custom field that i paint by extending the Field class.
I want when I draw any custom field from (100,100), then it should allocate space from (100,100) and not from (0,0).
my code looks like:
protected void layout(int width,int height)
{
setExtent(100,100);
}
Please help me out with any solutions.
What you appear to be trying to do actually doesn't sound like a very good idea. 100 pixels is a useful measure on one device, but 100 pixels on a 9300 is over 1/3 of the height of the screen, but on a 9800 is no where near that. I would strongly recommend that you do not use fixed pixel sizes for any Blackberry development.
That said, if you want to Field to start at position 100,100, you should use setPositionChild for that Field in the Field's Manager.
Before you start leaping off attempting this, I would recommend that you review the following:
http://supportforums.blackberry.com/t5/Java-Development/How-to-Extend-Manager/ta-p/446749
and
http://supportforums.blackberry.com/t5/Java-Development/Create-a-custom-layout-manager-for-a-screen/ta-p/442990
But perhaps the best options might be to explain what you are trying to achieve and we might be able to suggest an alternative.

Bulk creation + edit images

I currently use photoshop + datasets to automatically create CTA buttons for testing on a clients website. The dataset often contains text and other changes like underline or background colour, as well as alignment and font formatting.
Typically these can run into the thousands, which photoshop handles very well but the task is technical and not graphical. For me photoshop is overkill for the task.
Is anyone aware of a solution that is more code-friendly? I am currently playing around with canvas on HTML and fabric.js. This allows me to manipulate a template image, and I hoping I can pipe in code to create a number of .png output images.
Canvas would be well suited to your automation
First, create a base .png and then programmatically alter it in thousands of ways.
Save the results to appropriately named .png files.
You could even create an on-screen viewer that dynamically creates thousands of variations. The client(s) could browse the dynamically created images and select the variations that interest them. That would eliminate the need for thousands of saved files.
Here are just a start of canvas commands you can use to automate:
Apply text:
context.font("italic 14pt Verdana");
context.textAlign = textAlign; // left|right|center
context.fillText("anyText", x,y);
Underlines are just a bit trickier:
// use measureText to get the text width
var textWidth =context.measureText(text).width;
// 1-time only, pre-calculate all the “Y” underline offsets for each font you use
var ULOffset=lookupULOffset(font,fontSize);
context.moveTo(x,y+ULOffset);
context.lineTo(textWidth,y+ULOffset);
context.stroke();
Change background color or anything else that you've created a sub-layer for!
Like Photoshop Canvas has an excellent set of compositing operations.
Source-over
Source-in
Source-out
Source-atop
Lighter
Xor
Destination-over
Destination-in
Destination-out
Destination-atop
Darker (this has been deprecated for some reason??)
You also can apply an alpha when drawing a layer as well as an alpha while drawing any canvas shapes.
In addition, check online for many image filters that have been created for canvas.
And that's just be beginning...
Happy Automating!

How do I create a bitmap image as spritefont with multiple character regions?

How do I create a bitmap font image that contains characters from multiple regions and is correctly interpreted by XNA content pipeline?
I want to add some special characters to my bitmap font image, but I don't know how to do it correctly.
UPD: I think I'm getting closer to my answer. Sprite font texture content processor looks for non-magenta squares in the image and probably uses an xml settings file like with normal spritefonts to map each square to a corresponding symbol. I should probably edit that xml file for my custom texture, but I don't know where I can find it yet.
There is no XML file.
You have to create a custom content processor. Inherit that processor from FontTextureProcessor and override the GetCharacterForIndex method.
Have your method return the character for the specified index in your texture.
The default implementation simply returns FirstCharacter + index. Yours can use whatever logic it likes. (I guess you could even make it parse an XML file for the data.)
(Note that, for a single region, you can specify what FirstCharacter is in the properties for the "Sprite Font Texture" content processor, in the properties window (F4) for that content file.)

Resize actionscript stage dynamically?

i want to build a project base on actionscript3.and it needs to re-size its stage area to fit the the customer's web browser's viewing size or fit it when customer change the web browser's size.
There is no fixed aspect ratio but have a minimal(for example 640x480) size and a maximum size(1600x1000). when the customer's web window's length is less than the minimal size , a scroll bar would appear in that dimension ,so the user can see the whole stage using scroll bar .when customer's web window's size is larger than maximum size ,the flash application would appear at the top-center.and it needs to be working on most of the web browsers including IE 6.
when the stage size is changed ,the content on the stage wouldn't scale.in my case,the project has dozens of view,every view needs to handle size change and rearrange their elements, i thought this would be a huge work,so need any suggestion to build a project like this.
Firstly, the resizing will be managed by JavaScript / CSS, not ActionScript. That said, there are some things that you need to do in ActionScript to prepare for this behaviour.
Firstly, you mentioned that you don't want all of the content in the SWF to scale with the web page. To counter this default behaviour, you need to change the scale mode that the stage will use:
stage.scaleMode = StageScaleMode.NO_SCALE;
Another thing that I find makes scalable websites easier to work with is to set the top-left corner of the SWF to 0,0. By default if you scale up a SWF, 0,0 will remain in the same place with some 'padding' representing the new size. By that I mean, if you scale up the SWF by 200 pixels across and it was originally 400 pixels across, 0,0 will fall 100 pixels into the SWF from the left side rather than representing the top-left corner. Here's what you can do to correct that:
stage.align = StageAlign.TOP_LEFT;
These two things will help your embedded SWF act in a more expected manner which will likely help you throughout your project.
Finally, you mentioned that you have many viewable objects on the screen that need to react to the stage / browser window being resized. This is quite easy to deal with using Event.RESIZE. You can attach a listener to the stage to deal with this event and pass information across to each of your viewable objects reflecting the changes.
You can create a class that has a handleResize() method, which will be a base class for your view objects:
class ViewComponent extends Sprite
{
public function handleResize(width:int, height:int):void
{
trace(width, height);
}
}
Each of these objects can be listed within another class that manages the overall view, like this:
class ViewManager extends Sprite
{
public var viewComponents:Vector.<ViewComponent>;
public function ViewManager(stage:Stage)
{
viewComponents = new Vector.<ViewComponent>();
stage.addEventListener(Event.RESIZE, _manageResize);
}
private function _manageResize(e:Event):void
{
for each(var i:ViewComponent in viewComponents)
{
i.handleResize(e.target.stageWidth, e.target.stageHeight);
}
}
}
This way you can simply list your ViewComponents within the ViewManager and deal with each appropriately in it's own class by overriding handleResize().

QTMovieCurrentSizeAttribute and QTMovieSizeDidChangeNotification replacements

Does anyone know the correct way to replace old QTMovieCurrentSizeAttribute and QTMovieSizeDidChangeNotification tasks? I'm trying to clean out old deprecated code.
I've found that QTMovieNaturalSizeDidChangeNotification is not a replacement for QTMovieSizeDidChangeNotification. Likewise QTMovieNaturalSizeAttribute is not a replacement for QTMovieCurrentSizeAttribute. Natural Size refers to the QTMovie's native resolution, while Current Size refer to the resolution at which a QTMovie is being displayed (this may also be the resolution to which the movie is being decoded, which can resize from native). For example, if the source was anamorphic or had non-square pixels, then Natural and Current Sizes will not be the same. The difference is easily seen in the Movie Inspector Window of the QuickTime 7 Player.
As near as I can tell, QuickTime X allows multiple views into the same QTMovie, so the notion of Current Size needed to be replaced by something new. (Perhaps the Current Size functionality was moved into QTMovieView? Or a decoder query?) Can anyone refer me to documentation or sample code for the new way?
Any sample code of a Movie Inspector Window that has been updated to show Natural and Current ('Actual') Sizes, without using deprecated code, would be ideal. This has been very confusing to tackle, so far.
Is this useful? http://opensource.apple.com/source/WebCore/WebCore-955.66/platform/graphics/mac/MediaPlayerPrivateQTKit.mm
IntSize MediaPlayerPrivate::naturalSize() const
{
if (!metaDataAvailable())
return IntSize();
// In spite of the name of this method, return QTMovieNaturalSizeAttribute transformed by the
// initial movie scale because the spec says intrinsic size is:
//
// ... the dimensions of the resource in CSS pixels after taking into account the resource's
// dimensions, aspect ratio, clean aperture, resolution, and so forth, as defined for the
// format used by the resource
NSSize naturalSize = [[m_qtMovie.get() attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
return IntSize(naturalSize.width * m_scaleFactor.width(), naturalSize.height * m_scaleFactor.height());
}

Resources