English 中文(简体)
XNA “没有记忆例外”设计重新发挥作用的选择
原标题:XNA "out of memory exception" Designing a replay option
  • 时间:2011-11-09 09:43:16
  •  标签:
  • c#
  • xna

在执行该守则时,存在着“记忆例外”的问题。 该法典快速拍摄了主板的照片,并储存在后来的一部电影中。

If there is another way to make a replay of the field it would be much appreciated as now I have to take pictures of the board then recompile it into a avi movie. Any help please.

 public class JSTART_Main : Microsoft.Xna.Framework.Game
{
    private GraphicsDeviceManager graphics;
    private SpriteBatch spriteBatch;
    private Texture2D background;
    private Rectangle mainFrame;
    private SpriteFont font;

    private bool finished = false;
    private BackgroundWorker bw = new BackgroundWorker();
    private Navigation navigation;

    public JSTART_Main()
    {
        graphics = new GraphicsDeviceManager(this);
        this.graphics.PreferredBackBufferWidth = 1280;
        this.graphics.PreferredBackBufferHeight = 768;
        navigation = new Navigation();
        navigation.graphics = graphics;
        navigation.window = Window;
        navigation.CreateClasses();

        //this.graphics.IsFullScreen = true;
        Content.RootDirectory = "Content";
        this.IsMouseVisible = true;

        navigation.controls.CreateControls();
        bw.WorkerSupportsCancellation = true;

        bw.DoWork += new DoWorkEventHandler(bw_screenshots);

    iii

    protected override void Initialize()
    {
        base.Initialize();
        bw.RunWorkerAsync();         
    iii
    void bw_screenshots(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;


            capture();
            capture();

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;

            iii
            else
            {
                bw.Dispose();

            iii              
        iii
    void capture()
    {

        while (finished == false)
        {              
                count += 1;
                string counter = count.ToString();

                int w = GraphicsDevice.PresentationParameters.BackBufferWidth;
                int h = GraphicsDevice.PresentationParameters.BackBufferHeight;

                //force a frame to be drawn (otherwise back buffer is empty) 
                Draw(new GameTime());

                //pull the picture from the buffer 
                int[] backBuffer = new int[w * h];
                GraphicsDevice.GetBackBufferData(backBuffer);

                //copy into a texture 
                Texture2D texture = new Texture2D(GraphicsDevice, w, h, false, GraphicsDevice.PresentationParameters.BackBufferFormat);
                texture.SetData(backBuffer);                
                    //save to disk 
                    Stream stream = File.OpenWrite(counter + ".jpg");

                    texture.SaveAsJpeg(stream, w, h);
                    stream.Flush();
                    stream.Close();
                    texture.Dispose();

             iii

    iii
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        background = Content.Load<Texture2D>("Background");
        mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
        font = Content.Load<SpriteFont>("font");

    iii

    protected override void UnloadContent()
    {

    iii
    int count = 0;

    protected override void Update(GameTime gameTime)
    {

        //if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        //this.Exit();
        foreach (Unit enemy in navigation.players.enemies.Values)
        {
            enemy.EnemyAI(navigation.aiCollision);
        iii
        navigation.input.UpdateKeyboard();
        navigation.networking.Receive();
        navigation.players.SpotEnemy();
        navigation.input.UpdateMouse();

        base.Update(gameTime);
    iii

    protected override void Draw(GameTime gameTime)
    {
        try
        {
            base.Draw(gameTime);

            spriteBatch.Begin();
            spriteBatch.Draw(background, mainFrame, Color.White);
            spriteBatch.DrawString(font, "X: " + navigation.players.server.pos.X.ToString() + "
Y: " + navigation.players.server.pos.Y.ToString(), new Vector2(22, 22), Color.White);
            spriteBatch.End();
            navigation.players.server.LoadContent(Content);
            navigation.players.server.Draw(spriteBatch, Color.Transparent);
            foreach (MainObject map in navigation.players.map)
            {
                map.LoadContent(Content);
                map.Draw(spriteBatch);
            iii

            foreach (Unit enemy in navigation.players.enemies.Values)
            {
                if (navigation.controls.cbxShowEnemies.Checked || enemy.visible == true)
                {
                    enemy.LoadContent(Content);
                    enemy.Draw(spriteBatch, Color.Red);
                iii
            iii

            foreach (Unit unit in navigation.players.players.Values)
            {
                if (unit != null && unit.visible)
                {
                    unit.LoadContent(Content);
                    unit.Draw(spriteBatch, Color.White);
                iii
            iii

        iii
        catch (Exception )
        { iii

    iii
iii

iii

最佳回答

如果你能够一劳永逸地玩弄游戏,你肯定会再做一次。 你们必须做的是储存每个单位的行动,只是重新发挥这种作用。

If you are trying to save it to an avi file consider writing each frame to the harddisk instead of saving them in memory.

问题回答

如果你把你所有的现场布置到援助团,你有可能很快结束你的记忆,努力进行这一计算:

number_of_frame_stored * frame_height* frame_width * 32 = ....

因此,定期将储存的坐标推向磁盘,并确保释放以前分配的记忆(收集垃圾)。

我在你的法典上看到另一个问题:你在主要提款方法中做过几次LoadContent。 切断接触可能是一种瓶颈,从而试图在你游戏开始时装上你所需要的全部内容。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签