Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions Src/Game.NET/Core/Engine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using Game.NET.Logic;
using System.Collections.Generic;
using Game.NET.Gfx;
using Game.NET.Logic;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace Game.NET.Core
{
Expand All @@ -10,6 +15,25 @@ public class Engine

public bool IsDone { get; private set; }

public IResourceManager ResourceManager
{
get
{
if (_resourceManager == null)
_resourceManager = new ResourceManager();
return _resourceManager;
}
}

private List<State> states = new List<State>();

private GameWindow window;
private Renderer renderer = new Renderer();
private IResourceManager _resourceManager;

private float fpsTime;
private int frames;

public Engine()
{
IsDone = true;
Expand All @@ -18,9 +42,12 @@ public Engine()
public bool Init(string[] argv = null, string config = "Config.cfg")
{
IsDone = false;

renderer.CreateBatch("default");

return true;
}

public void SwitchState(State state)
{
if (CurrentState != null) CurrentState.End();
Expand All @@ -30,9 +57,48 @@ public void SwitchState(State state)
if (CurrentState != null) CurrentState.Start();
}

public void ProcessInput()
{
if(CurrentState != null) CurrentState.ProcessInput();
}

public void Update(GameTime time)
{
time.RealTime += time.DeltaTime;
time.Time += time.Speed * time.DeltaTime;

if (fpsTime >= 1.0f)
{
time.Fps = frames / fpsTime;
frames = 0;
fpsTime = 0.0f;
}

if (CurrentState != null) CurrentState.Update(time);
}

public void Draw(GameTime time)
{
frames++;
fpsTime += time.DeltaTime;

GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

if (CurrentState != null) CurrentState.Draw(renderer, time);

renderer.Draw();
}

public void Exit()
{
IsDone = true;
}

public void CleanUp()
{
IsDone = true;
// free data
ResourceManager.CleanAll();
}
}
}
19 changes: 18 additions & 1 deletion Src/Game.NET/Core/ResourceManager/ResourceManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Game.NET
{
Expand Down Expand Up @@ -52,6 +53,7 @@ public void Remove<T>(T resource) where T : Resource
{
if (Contains<T>(resource.Name))
{
Delete(resource);
_resources.Remove(resource.Name);
}
}
Expand All @@ -60,13 +62,28 @@ public void Remove<T>(string name) where T : Resource
{
if (Contains<T>(name))
{
Delete(_resources[name]);
_resources.Remove(name);
}
}

public void CleanAll()
{
foreach (var resource in _resources)
{
Delete(resource.Value);
}

_resources.Clear();
}

private void Delete(Resource resource)
{
IDisposable disposable = resource as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
}
16 changes: 15 additions & 1 deletion Src/Game.NET/Game.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -58,12 +59,24 @@
<Compile Include="Core\Engine.cs" />
<Compile Include="Core\GameTime.cs" />
<Compile Include="Core\Input.cs" />
<Compile Include="Gfx\RenderBatch.cs" />
<Compile Include="Gfx\Renderer.cs" />
<Compile Include="Gfx\RenderTarget.cs" />
<Compile Include="Gfx\Sprite.cs" />
<Compile Include="Gfx\TransitionEffect.cs" />
<Compile Include="Logic\Entity.cs" />
<Compile Include="Logic\State.cs" />
<Compile Include="Logic\Transition.cs" />
<Compile Include="Parsing\Menu\MenuParsingService.cs" />
<Compile Include="Parsing\Menu\MenuParsingWorker.cs" />
<Compile Include="Resources\Material\Material.cs" />
<Compile Include="Resources\Material\Matrix4Uniform.cs" />
<Compile Include="Resources\MeshFactory.cs" />
<Compile Include="Resources\Mesh\VertexAttribute.cs" />
<Compile Include="Resources\Mesh\SubMesh.cs" />
<Compile Include="Resources\Material\Uniform.cs" />
<Compile Include="Resources\Mesh\Vertex.cs" />
<Compile Include="Resources\SpriteSheet.cs" />
<Compile Include="States\Menu\Menu.cs" />
<Compile Include="States\Menu\MenuEntry.cs" />
<Compile Include="Utils\Exception\GameException.cs" />
Expand All @@ -78,7 +91,7 @@
<Compile Include="Resources\ShaderFactory.cs" />
<Compile Include="Resources\Shader\ShaderFileInfo.cs" />
<Compile Include="Resources\Shader\ShaderProgram.cs" />
<Compile Include="Resources\Mesh\SubMesh.cs" />
<Compile Include="Resources\Mesh\ObjSubMesh.cs" />
<Compile Include="Utils\OpenTKExtensions\VectorExtension.cs" />
<Compile Include="Parsing\FileParser.cs" />
<Compile Include="Parsing\FileType.cs" />
Expand All @@ -94,6 +107,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Logic\Dialog\" />
<Folder Include="Parsing\State\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
28 changes: 28 additions & 0 deletions Src/Game.NET/Gfx/RenderBatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL;

namespace Game.NET.Gfx
{
public class RenderBatch
{
private List<Renderer.DrawMeshCommand> _commands = new List<Renderer.DrawMeshCommand>();

public void Add(Renderer.DrawMeshCommand cmd)
{
_commands.Add(cmd);
}

public void Submit()
{
foreach (var cmd in _commands)
{
cmd.material.Use();
cmd.mesh.Use();

//GL.DrawElements(cmd.mode, cmd.count, cmd.type, cmd.mesh.indicies);
}

_commands.Clear();
}
}
}
11 changes: 11 additions & 0 deletions Src/Game.NET/Gfx/RenderTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Game.NET.Gfx
{
public abstract class RenderTarget
{
public abstract void Use();

public abstract void Clear();

public abstract void Swap();
}
}
49 changes: 49 additions & 0 deletions Src/Game.NET/Gfx/Renderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using Game.NET.Resources;
using OpenTK.Graphics.OpenGL;

namespace Game.NET.Gfx
{
public class Renderer
{
private Dictionary<string, RenderBatch> batches = new Dictionary<string, RenderBatch>();
private RenderTarget _target;

public class DrawMeshCommand
{
public BaseSubMesh mesh;
public Material material;
}

public void SetRenderTarget(RenderTarget target)
{
_target = target;
}

public void CreateBatch(string name)
{
batches.Add(name, new RenderBatch());
}

public void DrawMesh(DrawMeshCommand cmd, string batch)
{
batches[batch].Add(cmd);
}

public void Draw()
{
if (_target == null)
return;

_target.Use();
_target.Clear();

foreach (var batch in batches.Values)
{
batch.Submit();
}

_target.Swap();
}
}
}
12 changes: 12 additions & 0 deletions Src/Game.NET/Gfx/Sprite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Game.NET.Gfx
{
class Sprite
{
}
}
3 changes: 3 additions & 0 deletions Src/Game.NET/Logic/State.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Game.NET.Core;
using Game.NET.Gfx;

namespace Game.NET.Logic
{
Expand All @@ -22,5 +23,7 @@ public State(Engine engine)
public abstract void ProcessInput();

public abstract void CleanUp();

public abstract void Draw(Renderer renderer, GameTime time);
}
}
7 changes: 4 additions & 3 deletions Src/Game.NET/Logic/Transition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Game.NET.Core;
using Game.NET.Gfx;

namespace Game.NET.Logic
{
Expand Down Expand Up @@ -58,13 +59,13 @@ public override void Update(GameTime gameTime)
_engine.SwitchState(TargetState);
}

/*public override void Draw()
public override void Draw(Renderer renderer, GameTime time)
{
if (InitialState != null && Out != null && !Out.IsFinished)
Out.Draw(InitialState);
else if (TargetState != null && In != null)
In.Draw(TargetState);
}*/
}

public override void ProcessInput()
{
Expand All @@ -75,7 +76,7 @@ public override void CleanUp()
{

}

#endregion
}
}
5 changes: 3 additions & 2 deletions Src/Game.NET/Parsing/FileParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using Game.NET.Logic;
using Game.NET.Parsing.IO;
using Game.NET.Parsing.Obj;

Expand All @@ -24,10 +25,10 @@ private static IDictionary<FileType, IParsingService> GetDefaultServices()
{
return new Dictionary<FileType, IParsingService>()
{
[FileType.Obj] = new ObjParsingService(),
[FileType.Obj] = new ObjParsingService()
};
}

public Mesh LoadMesh(string path, FileType fileType)
{
string fileName = Path.GetFileName(path);
Expand Down
4 changes: 3 additions & 1 deletion Src/Game.NET/Parsing/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public enum FileType : byte
{
Obj = 0
Obj = 0,
State = 1,
Menu = 2
}
}
Loading