현재 GDI +를 사용하는 2D 게임 엔진에서 작업 중입니다. 모든 것이 매우 원활하게 실행되지만 내 엔진 렌더링 그래픽을 더 빠르게 만들 수있는 몇 가지 사항이 있어야한다는 것을 알고 있습니다.GDI + 게임 엔진 (엔진을 빠르게 실행하려면 어떻게해야합니까?)
현재 800x600 픽셀의 빈 비트 맵을 화면에 렌더링하면 약 130fps가됩니다. 그러나 비트 맵에 800x600 픽셀의 이미지를 그리면 약 70fps가됩니다. 그건 전혀 문제가되지 않습니다. 저는 실제로 결과가 자랑 스럽습니다. 더 좋을 수 있다는 것을 압니다.
엔진을 더 빨리 작동시킬 수있는 방법에 대한 제안이 있다면 궁금한가요?
여기 내 창 클래스에 코드입니다 :
using System;
using System.Windows.Forms;
using GameEngine.Graphics;
using System.Threading;
using System.Drawing;
namespace GameEngine.Core
{
public class Game : Form
{
// Just a class that checks the fps and such
private GameTime gameTime;
private GraphicsEngine graphicsEngine;
private bool isLoaded = false;
private bool isRunning = false;
public Game(int width, int height, string title)
{
this.Width = width;
this.Height = height;
this.Text = title;
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.Paint += Game_Paint;
this.FormClosing += Game_FormClosing;
gameTime = new GameTime();
graphicsEngine = new GraphicsEngine(this);
Show();
}
public void Run()
{
Initialize();
Load();
isRunning = true;
while (isRunning && isLoaded)
{
// Yes I know, this is frowned upon
Application.DoEvents();
gameTime.Update();
// Only update if the form is focused and if the GameTime class says we're allowed
if (this.Focused && gameTime.Cycled)
{
BeginUpdate();
UpdateGame();
LateUpdate();
Render();
}
else
{
Thread.Sleep(1);
}
}
Exit();
}
public void Exit()
{
Unload();
this.Close();
// Using console application, so lets exit the console
Environment.Exit(0);
}
private void Initialize()
{
gameTime.Initialize();
}
private new void Load()
{
isLoaded = true;
}
private void Unload()
{
}
private void BeginUpdate()
{
}
private void UpdateGame()
{
this.Title = "FPS: " + gameTime.FPS;
}
private void LateUpdate()
{
}
Bitmap bmp = new Bitmap("Test.jpg");
private void Render()
{
// Draw the nice tree to the window
graphicsEngine.DrawBitmap(bmp, 0, 0);
// Cause the window to redraw it's self
this.Invalidate();
}
private void Game_Paint(object sender, PaintEventArgs e)
{
// Lets just make sure once again that we're allowed to draw
if (gameTime.Cycled)
{
// Render the graphics to the screen
graphicsEngine.Render(e.Graphics);
}
}
private void Game_FormClosing(object sender, FormClosingEventArgs e)
{
isLoaded = false;
}
public string Title
{
get { return Text; }
set { Text = value; }
}
}
}
여기에 그래픽 엔진 클래스입니다 :
using System.Drawing;
using GameEngine.Core;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace GameEngine.Graphics
{
public class GraphicsEngine
{
private Game game;
private System.Drawing.Graphics backBuffer;
private System.Drawing.Bitmap backBufferBitmap;
public GraphicsEngine(Game game)
{
this.game = game;
backBufferBitmap = new Bitmap(800, 600);
backBuffer = System.Drawing.Graphics.FromImage(backBufferBitmap);
backBuffer.CompositingMode = CompositingMode.SourceOver;
backBuffer.CompositingQuality = CompositingQuality.HighSpeed;
backBuffer.SmoothingMode = SmoothingMode.None;
backBuffer.InterpolationMode = InterpolationMode.Low;
backBuffer.TextRenderingHint = TextRenderingHint.SystemDefault;
backBuffer.PixelOffsetMode = PixelOffsetMode.Half;
}
public void Render(System.Drawing.Graphics g)
{
g.CompositingMode = CompositingMode.SourceCopy;
g.CompositingQuality = CompositingQuality.AssumeLinear;
g.SmoothingMode = SmoothingMode.None;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.TextRenderingHint = TextRenderingHint.SystemDefault;
g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
g.DrawImage(backBufferBitmap, new Rectangle(0, 0, game.Width, game.Height), new Rectangle(0, 0, game.Width, game.Height), GraphicsUnit.Pixel);
backBuffer.Clear(Color.Black);
}
public void DrawString(string text, int x, int y)
{
backBuffer.DrawString(text, SystemFonts.DefaultFont, Brushes.White, x, y);
}
public void DrawBitmap(Bitmap bitmap, int x, int y)
{
backBuffer.DrawImage(bitmap, x, y);
}
public void DrawBitmap(Bitmap bitmap, int x, int y, int width, int height)
{
backBuffer.DrawImageUnscaled(bitmap, x, y, width, height);
}
}
}
이 코드는 이미 작동중인 코드를 개선하는 데 중점을두고 있으므로 http://codereview.stackexchange.com/에 더 적합하다고 생각합니다. –