Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411468 Posts in 69368 Topics- by 58422 Members - Latest Member: daffodil_dev

April 22, 2024, 11:14:15 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)MonoGame Code Snippets
Pages: [1]
Print
Author Topic: MonoGame Code Snippets  (Read 1825 times)
_glitch
Guest
« on: December 28, 2016, 01:26:05 AM »

I will post some code snippets which someone might find useful here from time to time. They can be used within MonoGame and maybe in other projects if they don't include XNA-specific code.

First I want to link my sprite animation tutorial: https://forums.tigsource.com/index.php?topic=58912.0.

Maybe you wondered how to center drawn strings on the game screen. I will tell you:

You first have to import a font (you have to create the font via the MonoGame Pipeline):
Code:
SpriteFont font = Content.Load<SpriteFont>("font");

Simply put this code in your class in the Draw() method (I will use the standard objects which are generated by MonoGame):
Code:
string str = "Text";

spriteBatch.DrawString(font, str, new Vector2((GraphicsDevice.Viewport.Width / 2) - (int)(font.MeasureString(str).X / 2), (GraphicsDevice.Viewport.Height / 2) - (int)(font.MeasureString(str).Y / 2), Color.Black);
« Last Edit: December 29, 2016, 01:10:10 AM by _glitch » Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #1 on: December 31, 2016, 07:42:42 AM »

Does anyone remember how to disable filtering when generating text? I remember doing this years ago but I've forgotten how.
Logged

_glitch
Guest
« Reply #2 on: December 31, 2016, 07:58:08 AM »

Does anyone remember how to disable filtering when generating text? I remember doing this years ago but I've forgotten how.

Try
Code:
Game1.GraphicsDevice.RenderState.MultiSampleAntiAlias = false;
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #3 on: December 31, 2016, 03:16:51 PM »

I did give that and various other alias setting a try but that didn't seem to do it. I think it's because the problem is the content processor is antialiasing the text when it renders it to a texture. Thanks for the advice though.

I'll try to dig up my old code and see what I did because I'm quite certain I solved this in the past.
Logged

_glitch
Guest
« Reply #4 on: December 31, 2016, 11:21:55 PM »

Maybe you have to open the MonoGame Content file (Content.mgcb in the directory Content) and set there the value of the variable that causes the antialising in your sprite font or texture to false. So it will build the font or texture without antialising.
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #5 on: January 01, 2017, 03:28:36 PM »

Doesnt seem like the application exposes a variable for scaling/sampling.

EDIT : If I open the stpritefont file with a text editor I can find more stuff. Upping the size helps a bit.

This thread seems to indicate other's have had problems.

http://stackoverflow.com/questions/19954412/font-rendering-in-xna-is-getting-low-quality

EDITEDIT : If I switch to a font and use a specific size or multiple of that size the issue is fine. Maybe it uses a different scaling filter depending on the situation.
« Last Edit: January 01, 2017, 07:35:33 PM by InfiniteStateMachine » Logged

_glitch
Guest
« Reply #6 on: January 13, 2017, 11:13:31 AM »

Play music and sounds

To make this working you have first to add a music file (I have called it "file.mp3") to your content folder via the MonoGame Pipeline. After you have done this, add following line to your class where you want to use the music/sounds:
Code:
using Microsoft.Xna.Framework.Media;

Then you have to load the song:
Add following:
To your fields/properties:
Code:
Song song;
and
Code:
bool IsPlaying = false;
To the LoadContent method:
Code:
 song = content.Load<Song>("file");
To specify if you want to loop the music, you have to add
Code:
MediaPlayer.IsRepeating = true;

Now you can start playing your music in the Update() or Draw() method with following code:
Code:
if (IsPlaying == false)
{
    MediaPlayer.Play(song);
    IsPlaying = true;
}

To pause the music add
Code:
MediaPlayer.Pause();
To resume add
Code:
MediaPlayer.Resume();
And to stop playing add
Code:
MediaPlayer.Stop();
Logged
_glitch
Guest
« Reply #7 on: January 28, 2017, 11:20:59 PM »

It's not only for MonoGame, but in my opinion it's useful sometimes:

Instead of this
Code:
bool _Bool = true;

if (_Bool == true)
    _Bool = false;
else
    _Bool = true;

you can say
Code:
_Bool ^= true
Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #8 on: January 29, 2017, 11:46:18 AM »

_Bool = !_Bool; is how I usually see that done, but I suppose that way works too.
Logged

_glitch
Guest
« Reply #9 on: January 29, 2017, 12:22:50 PM »

Yes, your way is probably more readable and available in more programming languages.
Logged
oahda
Level 10
*****



View Profile
« Reply #10 on: January 30, 2017, 01:49:38 AM »

Me too, but it can be a bit redundant and verbose if the name of the variable is long. Would be cool to have some kind of unary !! operator or something, like ++ and --. I've toyed with that for my language design which is never going to actually happen but is fun to play with. c;
« Last Edit: January 30, 2017, 02:21:12 AM by Prinsessa » Logged

_glitch
Guest
« Reply #11 on: January 30, 2017, 12:12:48 PM »

Or, if the name of the variable is strange. Not to long ago I had a variable called 'exa'. At some point I wrote '!exa', but i read 'lexa' and wondered why my variable has gone. Facepalm
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic