I created a new MonoGame iOS solution in Xamarin Studio. After that, I added to following code to Game1.cs:
bool IsiOS = false;
if (Device.RuntimePlatform == Device.iOS)
IsiOS = true;
But I get an error message when I build the project.
if (Device.RuntimePlatform == Device.iOS)
System.InvalidOperationException You MUST call Xamarin.Forms.Init();
prior to using it.
What is wrong with my code? I don't know what I should add or change.
code(Game1.cs):
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Xamarin.Forms;
namespace NewTest.iOS
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
bool IsiOS = false;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
if (Device.RuntimePlatform == Device.iOS)
IsiOS = true;
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
code(Program.cs):
using System;
using Foundation;
using UIKit;
namespace NewTest.iOS
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
private static Game1 game;
internal static void RunGame()
{
game = new Game1();
game.Run();
}
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
public override void FinishedLaunching(UIApplication app)
{
RunGame();
}
}
}
The Device class is part of Xamarin Forms. If you are using Xamarin Forms, then you must initialize it first. That is exactly what the error message is telling you.
Related
When you instantiate a new GameWindow, its background color is white, I would like to change it to black for instance.
The problem is further visible if you have a long initialization sequence happening.
The closest I could get is to set clear color in constructor then swap but it ain't perfect.
using System.Drawing;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
internal static class Program
{
private static void Main(string[] args)
{
var gws = new GameWindowSettings();
var nws = new NativeWindowSettings
{
Size = new Vector2i(320, 240)
};
using (var game = new Game(gws, nws))
{
game.Run();
}
}
}
internal class Game : GameWindow
{
public Game(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
{
// close but still shows a white background at startup for some time
GL.ClearColor(Color.Red);
GL.Clear(ClearBufferMask.ColorBufferBit);
Context.SwapBuffers();
}
protected override void OnLoad()
{
base.OnLoad();
GL.ClearColor(Color.Green);
}
protected override void OnRenderFrame(FrameEventArgs args)
{
base.OnRenderFrame(args);
GL.Clear(ClearBufferMask.ColorBufferBit);
Context.SwapBuffers();
}
protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, e.Width, e.Height);
}
}
Question:
How to get GameWindow to use another color than white when it's first shown?
I want to use this wrapper in MonoGame but I get an error message if I use the code from the wrapper website.
Wrapper StatsIO
I downloaded the wrapper project and added the project to my solution in Visual Studio. After that, I created a reference from my project to the wrapper project.
Then, I copied this code to my project:
var client = new StatsIOClient("Client-ID", "Client-Secret");
await client.Statistics.CreateAsync("Stats-Id", "Username");
But something went wrong, because I get these error message:
Error CS1501: No overload for method 'CreateAsync' takes 2 arguments
(CS1501)
What is wrong with the code? How can I use the wrapper project in my Visual Studio project to send and receive leaderboard scores?
Dropbox link to my Visual Studio project: my project
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StatsIO.Objects;
using StatsIO;
namespace LeaderboardTest
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public async void NewClient()
{
var client = new StatsIOClient("Client-ID", "Client-Secret");
await client.Statistics.CreateAsync("Stats-Id", "Username");
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
NewClient();
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
I'm working on a game engine. Now assume we have a Game class:
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using static OpenTK.Graphics.OpenGL.GL;
namespace Test2
{
class Game : GameWindow
{
public Game(int width, int height)
:base(width, height)
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
}
}
}
And we have a medium in which to call a method for every frame.
Now assume the person creating a game, creates a file called ball_behaviour script:
using System;
using System.Drawing;
namespace Test2.Project.Behaviour
{
class Ball
{
public void Update()
{
}
public void init()
{
A2D.Background = Color.CornflowerBlue;
}
public void preInit()
{
}
}
}
And assume there are other files like it, which have the methods Update, and init. How can I call these methods from protected override void OnRenderFrame for example, in every class that's registered under Test2.Project.Behaiviour namespace? Thank you so much in advanced.
EDIT:The GameWindow class comes from OpenTK, which is the API I'm using to interface c# with OpenGL.
EDIT #2: Kind of like the way Unity does it.
you could use a virtual void that you than override which would be easier and faster to implement,have done this thing yesterday
I have the user select a file using an OpenFileDialog. After the file is selected, and the dialog is closed, the main window is the last application when alt-tabbing. I want to have the main window regain focus and be on the foreground without having to alt+tab to it after closing the dialog window. Is this possible and if so how?
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Class1 c1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
c1 = c1.Instance;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
c1.Load(Content);
c1.GetFile();
}
}
public sealed class Class1
{
private static Class1 _instance = null;
private static readonly object _padlock = new object();
private Class1()
{
}
public static Class1 Instance
{
get
{
lock (_padlock)
{
if (_instance == null)
{
_instance = new Class1();
}
return _instance;
}
}
}
public void GetFile()
{
string path;
OpenFileDialog ofdSelectLayout = new OpenFileDialog();
if(ofdSelectLayout.ShowDialog() == DialogResult.OK)
{
path = ofdSelectLayout.FileName;
}
//some code
}
public void Load(ContentManager content)
{
//Loading some textures
}
}
I thought there would be an simple method for it. (like Game.IsActive, which only checks if the game is out of focus)
After a while, I've found this one around, you can place it in the method where the FileDialog is closing:
using System.Windows.Forms;
Form myForm = (Form)Form.FromHandle(this.Window.Handle);
myForm.Activate();
This however does use System.Windows.Forms, and that means that your game have to be Windows-exclusive for it. But if you're working in Windows Forms, you can see it as a workaround for now.
I've looked around a lot.. and I haven't found anything to help me out.
This is the outcome I am looking for:
I am using XNA Game Studio 4.0, and I want to create my own library to use when making my game, so I don't have to rewrite code that I have previously used over and over every time I want to make a new game. So, I want to call in a library as a reference. My Library:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace iRaaptorLibraryXNA
{
public class XNALibrary
{
public static void consolePrint(string text)
{
var timeStamp = System.DateTime.Now.ToString(#"hh:mm:ss tt");
Console.WriteLine("[" + timeStamp + "] CONSOLE: " + text);
}
}
}
I use the consolePrint function a lot while debugging.
For example, if I wanted to print a line to show success, I'd like to be able to type
consolePrint("SUCCESS!")
and that prints it to console. I DONT want to have to repeatedly type in:
XNALibraryXNA.consolePrint("SUCCESS!");
I want to shorten it to just
consolePrint("SUCCESS!");
Here is my game code:
using iRaaptorLibraryXNA;
namespace xnaFun
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize()
XNALibrary.consolePrint("Initialized!");
}
protected override void LoadContent()
{
iRaaptorLibraryXNA.consolePrint("Loading content!"); // DOES PRINT TO CONSOLE
spriteBatch = new SpriteBatch(GraphicsDevice);
}
etc......
I hope I included enough information. ~iRaaptor
TL;DR I want to shorten
XMALibrary.consolePrint("text");
to
consolePrint("text");
Add this class to your library:
public class BaseGame : Microsoft.Xna.Framework.Game
{
protected void consolePrint(string message)
{
XNALibrary.consolePrint("Initialized!");
}
}
Your game class can then inherit from the basegame and use the consolePrint method:
public class MyGame : BaseGame
{
protected override void Initialize()
{
base.Initialize()
consolePrint("Initialized!");
}
}