티스토리 뷰

Application/C#

XNA를 시작합시다!

알 수 없는 사용자 2011. 12. 31. 11:48

Microsoft XNA Game Studio Express를 설치하는 방법 등, 기초적인 환경 설정은 인터넷에도 많으니 여기서는 다루지 않습니다.

또한 reimer`s tutorial 01 원문에서는 간단하게 Game1.cs의 구조를 정리하고 있는데 이 또한 나중에 자세하게 다루기로 하고, 여기서는 다루지 않습니다.

일단 기본적인 환경은 모두 갖추어 졌다고 보고, 간단하게 Windows Game 프로젝트를 하나 생성해봅시다.

프로젝트를 처음 생성하고 디버그(F5)키로 실행시켜 보면 DirectX를 다뤄왔을 사람이라면 익숙하게 느껴질 파란 바탕의 윈도우가 하나 생성될 것 입니다.

우리가 이번 tutorial에서 할 일은 이 윈도우의 속성(크기, 전체 화면 등...)을 수정하는 일 입니다.

그러기 위해, 다시 코드로 돌아와 몇가지 코딩을 해 봅시다.

먼저 GraphicsDevice를 사용하기 쉽도록 전역 변수를 설정합니다.

 GraphicsDevice device;

전역 변수를 설정했다면, 윈도우의 속성을 정의할 우리만의 함수를 하나 만들어 봅시다. 이름은 SetupXNADevice로 하고 parameter는 없으며 return type은 void입니다. 물론 이 함수는 다른 클래스에서 접근할 일이 없으므로 private입니다.


 #region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace reimers_tutorial01_series1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        GraphicsDevice device; // GraphicsDevice를 설정할 전역 변수


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }


        /// <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

            base.Initialize();

            SetupXNADevice(); //추가
        }

        private void SetupXNADevice()
        {
            device = graphics.GraphicsDevice; // 전역 변수에 GraphicsDevice를 설정

            graphics.PreferredBackBufferWidth = 500; // 윈도우의 너비, 높이를 설정
            graphics.PreferredBackBufferHeight = 500;

            graphics.IsFullScreen = false; // 윈도우 모드

            graphics.ApplyChanges(); // 설정 적용

            Window.Title = "riemer`s XNA tutorial 01 - series 1"; // 윈도우 제목 설정
        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
            }

            // TODO: Load any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                // TODO: Unload any ResourceManagementMode.Automatic content
                content.Unload();
            }

            // TODO: Unload any ResourceManagementMode.Manual content
        }


        /// <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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // 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)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}



이제 다시 디버그(F5)키를 눌러 실행하면 윈도우의 모양이 우리가 설정한 대로 바뀌어 나옴을 확인할 수 있습니다.


Array

'Application > C#' 카테고리의 다른 글

Windows 10 IoT 장단점  (0) 2016.11.23
C# 다른 버튼 액션 갖다쓰기.  (0) 2014.05.10
Using Cleartype font in Windows Embedded CE  (0) 2010.11.02
컨트롤 드래그  (0) 2010.10.31
C# 메세지 처리  (0) 2010.07.16