Форум » Вопросы о разработке игр » проблемма с GraphicsDevice в XNA » Ответить

проблемма с GraphicsDevice в XNA

Inferno: Всем добрый день. Помогите мне пожалуйста, при инициализации девайса у меня происходит ошибка:An unexpected error has occurred. Так как я чайник не могу понять в чем причина. И надеюсь на помощь более квалифицированных программистов. Вот исходный код(строка в которой происходит ошибка выделена красным): [quote] using System; using System.Collections.Generic; 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 Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace DogmaTank { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDevice device = null; PresentationParameters presentationParameters; public Game1() { Content.RootDirectory = "Content"; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here presentationParameters = new PresentationParameters(); presentationParameters.BackBufferWidth = 1024; presentationParameters.BackBufferHeight = 768; presentationParameters.BackBufferFormat = SurfaceFormat.Rgba32; presentationParameters.BackBufferCount = 1; presentationParameters.MultiSampleType = MultiSampleType.None; presentationParameters.MultiSampleQuality = 0; presentationParameters.SwapEffect = SwapEffect.Discard; presentationParameters.DeviceWindowHandle = this.Window.Handle; presentationParameters.IsFullScreen = true; presentationParameters.EnableAutoDepthStencil = true; presentationParameters.AutoDepthStencilFormat = DepthFormat.Depth24; presentationParameters.FullScreenRefreshRateInHz = 0; presentationParameters.PresentationInterval = PresentInterval.Immediate; device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Reference, this.Window.Handle, presentationParameters); base.Initialize(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // TODO: Add your update logic here base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { device.Clear(Color.Gold); // TODO: Add your drawing code here device.Present(); base.Draw(gameTime); } } } [/quote] Вроде все написал правильно, но все равно не работает. Помогите кто может.

Ответов - 4

JohnK: Так а зачем тебе reference девайс?

JohnK: Решается это с помощью использования эвента: public Game1() { graphics = new GraphicsDeviceManager(this); graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>( graphics_PreparingDeviceSettings); Content.RootDirectory = "Content"; } void graphics_PreparingDeviceSettings( object sender, PreparingDeviceSettingsEventArgs e) { } В методе graphics_PreparingDeviceSettings получаешь информацию о девайсе и устанавливаешь нужные настройки. Вот так можно установить тип девайса, а так же изменить presentation параметры: void graphics_PreparingDeviceSettings( object sender, PreparingDeviceSettingsEventArgs e) { if (e.GraphicsDeviceInformation.Adapter.IsDeviceTypeAvailable( DeviceType.Hardware) == false) { GraphicsDeviceInformation gi = e.GraphicsDeviceInformation; gi.DeviceType = DeviceType.Reference; gi.PresentationParameters.MultiSampleType = MultiSampleType.None; } }

Inferno: Так а зачем тебе reference девайс? Решил по старинке инициализировать DirectX. Как в старые добрые времена, когда нужно было создать девайс, заполнить структуру PresentationParameters от руки. А не то, что счас, на всем готовеньком.


Inferno: Решается это с помощью использования эвента: Я на шел более оригинальный способ: Forms1.cs: using System; using System.Windows.Forms; 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 Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace Test { public partial class Form1 : Form { GraphicsDevice device = null; public Form1() { this.ClientSize = new System.Drawing.Size(800, 600); InitializeComponent(); } public void Init_Video() { PresentationParameters present = new PresentationParameters(); present.IsFullScreen = false; present.SwapEffect = SwapEffect.Discard; device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, this.Handle, present); } public void Render() { device.Clear(Color.Gold); device.Present(); } protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) { if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) { this.Close(); } } } } Program.cs: using System; using System.Collections.Generic; using System.Windows.Forms; namespace Test { static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main() { using (Form1 frm = new Form1()) { frm.Init_Video(); frm.Show(); while (frm.Created) { frm.Render(); Application.DoEvents(); } } } } }



полная версия страницы