Показать сообщение отдельно
Старый 06.12.2012, 17:49   #200
wppt
Нуждающийся
 
Регистрация: 25.11.2012
Сообщений: 83
Написано 2 полезных сообщений
(для 2 пользователей)
Ответ: Вопросы по XNA.

вот ты ухватился за этот код! Сейчас будет все


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.IO;
using System.Threading.Tasks;
using System.Threading;

namespace WindowsGame4
{
    public class mygam : Microsoft.Xna.Framework.Game
    {
        int overload;
        float camscale = 1.5f;
        RenderTarget2D mapr;
        GraphicsDevice graphdev;
        GraphicsDeviceManager myGraphics;
        GraphicsProfile graphprof;
        KeyboardState myKeyboardState;
        MouseState mymouse;
        ContentManager Content;
        
        SpriteBatch batch1;
        SpriteBatch batch2;
        
        static SpriteFont main;
        Random rand;

        TimeSpan eltime = TimeSpan.Zero;
        
        Color[] dat;
        Color[] mapTextData;
        
        Drawing Map;
        
        Camera cam;

        Matrix Camp;
        Rectangle mapRect;
        Vector2 vel;
        
        int FPC = 0;
        int FPS = 0;
        
        World world = new World();
        
        public mygam()
        {

            Window.Title = "myGam";
            Window.AllowUserResizing = true;
            
            myGraphics = new GraphicsDeviceManager(this); 
            myGraphics.PreferMultiSampling = true;
            myGraphics.SynchronizeWithVerticalRetrace = false;
            myGraphics.PreferredBackBufferWidth = 800;
            myGraphics.PreferredBackBufferHeight = 600;
         
            Content  = new ContentManager(Services);
            graphprof = new GraphicsProfile();
            rand = new Random();
            cam = new Camera(new Vector2(0,0));
            dat = new Color[1] { new Color(0,0,0,0)};
            
            Map = new Drawing(10, 10, false, new Vector2(0, 0));
         
        }

        protected override void Initialize()
        {
            base.Initialize();
            world.Init();
        }
  
        protected override void LoadContent()
        { 
            var parameters = myGraphics.GraphicsDevice.PresentationParameters;

            graphdev = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, graphprof, parameters);

            mapr = new RenderTarget2D(GraphicsDevice, 800, 600, false, SurfaceFormat.Color, DepthFormat.Depth24);

            Content.RootDirectory = "Content";

            batch1 = new SpriteBatch(GraphicsDevice);
            batch2 = new SpriteBatch(GraphicsDevice);

            Map.loading(Content, @"NewFolder1\map");

            mainf = Content.Load<SpriteFont>("mainf");

            mapTextData = new Color[800 * 600];
            mapr.GetData<Color>(mapTextData);
        }

        protected override void UnloadContent()
        {
        }
      
        protected override void Update(GameTime gameTime)
        {
            Camp = cam.Targeted(cam.position, vel,0,camscale);

            eltime += gameTime.ElapsedGameTime;

            if (eltime > TimeSpan.FromSeconds(1))
            {
                eltime -= TimeSpan.FromSeconds(1);
                FPS = FPC;
                FPC = 0;
            }

            world.UpdateWorld();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.SetRenderTarget(mapr);
            world.UpdateWorld();
            FPC++;

            string FPSS = string.Format("fps: {0}", FPS);
            
            batch1.Begin(SpriteSortMode.Deferred,BlendState.AlphaBlend,null,null,null,null,Camp);
                        
            Map.draw(batch1);

            batch1.End();

            GraphicsDevice.SetRenderTarget(null);
            GraphicsDevice.Clear(Color.CornflowerBlue);
            
            batch2.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

                batch2.Draw(mapr, Vector2.Zero, Color.White);

                if (mymouse.LeftButton == ButtonState.Pressed && overload == 0)
                {
                    try
                    {
                        mapr.GetData<Color>(0, new Rectangle(mymouse.X, mymouse.Y, 1, 1), dat, 0, 1);
                        overload = 60;
                    }
                    catch
                    { }
                }

                if (mymouse.RightButton == ButtonState.Pressed)
                {
                   fill(mymouse.X, mymouse.Y, new Color(254, 230, 158, 255), dat[0], mapr); 
                }

                Color a = dat[0];
                string aa = string.Format("{0}", a);
                batch2.DrawString(mainf, aa, new Vector2(33, 400), Color.Black);

                if (overload > 0)
                    overload--;
            }

            batch2.End();
 
            base.Draw(gameTime);
        }

        fill(int x, int y, Color newC, Color oldC,RenderTarget2D rend)
        {
            Color[] colors = new Color[1];
            rend.GetData(0, new Rectangle(x, y, 1, 1), colors, 0, 1);
            if (x >= 0 && y >= 0 && x < rend.Width && y < rend.Height && colors[0] == oldC && colors[0] != newC)
            {
                colors [0] = newC;
                mapTextData[y * rend.Width + x] = colors[0];
                rend.SetData<Color>(mapTextData);
                fill(x - 1, y, newC, oldC, rend);
                fill(x + 1, y, newC, oldC, rend);
                fill(x, y - 1, newC, oldC, rend);
                fill(x, y + 1, newC, oldC, rend);
            }
            else return;
        }
    }
}

Последний раз редактировалось wppt, 06.12.2012 в 20:22.
(Offline)
 
Ответить с цитированием