1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

First commit

This commit is contained in:
Danilo 2024-03-18 15:41:46 -03:00
commit 99c8486c90
38 changed files with 2499 additions and 0 deletions

485
.gitignore vendored Normal file
View File

@ -0,0 +1,485 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from `dotnet new gitignore`
# dotenv files
.env
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
[Oo]ut/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET
project.lock.json
project.fragment.lock.json
artifacts/
# Tye
.tye/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
.idea
##
## Visual studio for Mac
##
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# Mac bundle stuff
*.dmg
*.app
# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# Vim temporary swap files
*.swp

15
CMakeLists.txt Normal file
View File

@ -0,0 +1,15 @@
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
project ("xna")
# Include sub-projects.
add_subdirectory ("framework")

101
CMakePresets.json Normal file
View File

@ -0,0 +1,101 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "linux-debug",
"displayName": "Linux Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
},
{
"name": "macos-debug",
"displayName": "macOS Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
}
]
}

13
framework/CMakeLists.txt Normal file
View File

@ -0,0 +1,13 @@
# CMakeList.txt : CMake project for xna, include source and define
# project specific logic here.
#
# Add source to this project's executable.
add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp")
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET xna PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add tests and install targets if needed.
target_link_libraries(${PROJECT_NAME} D3d11.lib dxgi.lib)

View File

@ -0,0 +1,41 @@
#ifndef XNA_COMMON_MATRIX_HPP
#define XNA_COMMON_MATRIX_HPP
namespace xna {
struct Matrix {
float M11{ 0 };
float M12{ 0 };
float M13{ 0 };
float M14{ 0 };
float M21{ 0 };
float M22{ 0 };
float M23{ 0 };
float M24{ 0 };
float M31{ 0 };
float M32{ 0 };
float M33{ 0 };
float M34{ 0 };
float M41{ 0 };
float M42{ 0 };
float M43{ 0 };
float M44{ 0 };
constexpr Matrix() = default;
constexpr Matrix(float M11, float M12, float M13, float M14, float M21, float M22, float M23, float M24, float M31, float M32, float M33, float M34, float M41, float M42, float M43, float M44)
:
M11(M11), M12(M12), M13(M13), M14(M14),
M21(M21), M22(M22), M23(M23), M24(M24),
M31(M31), M32(M32), M33(M33), M34(M34),
M41(M41), M42(M42), M43(M43), M44(M44) { }
constexpr bool operator==(const Matrix& other) const {
return M11 == other.M11 && M12 == other.M12 && M13 == other.M13 && M14 == other.M14
&& M21 == other.M21 && M22 == other.M22 && M23 == other.M23 && M24 == other.M24
&& M31 == other.M31 && M32 == other.M32 && M33 == other.M33 && M34 == other.M34
&& M41 == other.M41 && M42 == other.M42 && M43 == other.M43 && M44 == other.M44;
}
};
}
#endif

View File

@ -0,0 +1,22 @@
#ifndef XNA_COMMON_POINT_HPP
#define XNA_COMMON_POINT_HPP
#include "../types.hpp"
namespace xna {
struct Point {
Int X{0};
Int Y{ 0 };
constexpr Point() = default;
constexpr Point(const Int& X, const Int& Y)
: X(X), Y(Y) {}
constexpr bool operator==(const Point& other) const {
return X == other.X && Y == other.Y;
}
};
}
#endif

View File

@ -0,0 +1,22 @@
#ifndef XNA_COMMON_QUATERNION_HPP
#define XNA_COMMON_QUATERNION_HPP
namespace xna {
struct Quaternion {
float X{ 0 };
float Y{ 0 };
float Z{ 0 };
float W{ 0 };
constexpr Quaternion() = default;
constexpr Quaternion(float X, float Y, float Z, float W)
: X(X), Y(Y), Z(Z), W(W) { }
constexpr bool operator==(const Quaternion& other) const {
return X == other.X && Y == other.Y && Z == other.Z && W == other.W;
}
};
}
#endif

View File

@ -0,0 +1,20 @@
#ifndef XNA_COMMON_RECTANGLE_HPP
#define XNA_COMMON_RECTANGLE_HPP
#include "../types.hpp"
namespace xna {
struct Rectangle {
Int Height{ 0 };
Int Width{ 0 };
Int X{ 0 };
Int Y{ 0 };
constexpr Rectangle() = default;
constexpr Rectangle(const Int& Height, const Int& Width, const Int& X, const Int& Y)
: Height(Height), Width(Width), X(X), Y(Y) {}
};
}
#endif

View File

@ -0,0 +1,51 @@
#ifndef XNA_COMMON_VECTORS_HPP
#define XNA_COMMON_VECTORS_HPP
namespace xna {
struct Vector2 {
float X{ 0 };
float Y{ 0 };
constexpr Vector2() = default;
constexpr Vector2(float X, float Y)
: X(X), Y(Y) {}
constexpr bool operator==(const Vector2& other) const {
return X == other.X && Y == other.Y;
}
};
struct Vector3 {
float X{ 0 };
float Y{ 0 };
float Z{ 0 };
constexpr Vector3() = default;
constexpr Vector3(float X, float Y, float Z)
: X(X), Y(Y), Z(Z) { }
constexpr bool operator==(const Vector3& other) const {
return X == other.X && Y == other.Y && Z == other.Z;
}
};
struct Vector4 {
float X{ 0 };
float Y{ 0 };
float Z{ 0 };
float W{ 0 };
constexpr Vector4() = default;
constexpr Vector4(float X, float Y, float Z, float W)
: X(X), Y(Y), Z(Z), W(W) { }
constexpr bool operator==(const Vector4& other) const {
return X == other.X && Y == other.Y && Z == other.Z && W == other.W;
}
};
}
#endif

76
framework/enums.hpp Normal file
View File

@ -0,0 +1,76 @@
#ifndef XNA_ENUMS_HPP
#define XNA_ENUMS_HPP
namespace xna {
enum class Blend {
Zero,
One,
SourceColor,
InverseSourceColor,
SourceAlpha,
InverseSourceAlpha,
DestinationAlpha,
InverseDestinationAlpha,
DestinationColor,
InverseDestinationColor,
SourceAlphaSaturation,
BlendFactor,
InverseBlendFactor,
Source1Color,
InverseSource1Color,
Source1Alpha,
InverseSource1Alpha
};
enum class ColorWriteChannels {
Red,
Green,
Blue,
Alpha,
All
};
enum class BlendFunction {
Add = 0,
Subtract = 1,
ReverseSubtract = 2,
Min = 3,
Max = 4,
};
using BlendOperation = BlendFunction;
enum class DisplayOrientation {
Default = 0,
LandscapeLeft = 1,
LandscapeRight = 2,
Portrait = 4,
};
enum class SurfaceFormat {
Color = 0,
Bgr565 = 1,
Bgra5551 = 2,
Bgra4444 = 3,
Dxt1 = 4,
Dxt3 = 5,
Dxt5 = 6,
NormalizedByte2 = 7,
NormalizedByte4 = 8,
Rgba1010102 = 9,
Rg32 = 10,
Rgba64 = 11,
Alpha8 = 12,
Single = 13,
Vector2 = 14,
Vector4 = 15,
HalfSingle = 16,
HalfVector2 = 17,
HalfVector4 = 18,
HdrBlendable = 19,
};
constexpr int SURFACE_FORMAT_COUNT = 19;
}
#endif

91
framework/forward.hpp Normal file
View File

@ -0,0 +1,91 @@
#ifndef XNA_FORWARD_HPP
#define XNA_FORWARD_HPP
#include "types.hpp"
namespace xna {
//Framework
class BoundingBox;
class BoundingFrustum;
class BoundinSphere;
struct Color;
class Curve;
class CurveContinuity;
class CurveKey;
class CurveKeyCollection;
struct Matrix;
class Plane;
struct Point;
class Quaternion;
class Ray;
struct Rectangle;
struct Vector2;
struct Vector3;
struct Vector4;
//Audio
//Graphics
class BlendState;
class DisplayMode;
class DisplayModeCollection;
class GraphicsAdapter;
class GraphicsDevice;
class RenderTarget2D;
class SwapChain;
class Texture;
class Texture2D;
struct Viewport;
//Game
class DrawableGameComponent;
class Game;
class GameComponent;
class GameComponentCollection;
class GameTime;
class GameWindow;
class IDrawable;
class IGameComponent;
class IUpdatable;
using PBoundingBox = std::shared_ptr<BoundingBox>;
using PBoundingFrustum = std::shared_ptr<BoundingFrustum>;
using PBoundinSphere = std::shared_ptr<BoundinSphere>;
using PColor = std::shared_ptr<Color>;
using PCurve = std::shared_ptr<Curve>;
using PCurveContinuity = std::shared_ptr<CurveContinuity>;
using PCurveKey = std::shared_ptr<CurveKey>;
using PCurveKeyCollection = std::shared_ptr<CurveKeyCollection>;
using PMatrix = std::shared_ptr<Matrix>;
using PPlane = std::shared_ptr<Plane>;
using PPoint = std::shared_ptr<Point>;
using PQuaternion = std::shared_ptr<Quaternion>;
using PRay = std::shared_ptr<Ray>;
using PRectangle = std::shared_ptr<Rectangle>;
using PVector2 = std::shared_ptr<Vector2>;
using PVector3 = std::shared_ptr<Vector3>;
using PVector4 = std::shared_ptr<Vector4>;
using PBlendState = std::shared_ptr<BlendState>;
using PDisplayMode = std::shared_ptr<DisplayMode>;
using PDisplayModeCollection = std::shared_ptr<DisplayModeCollection>;
using PGraphicsAdapter = std::shared_ptr<GraphicsAdapter>;
using PGraphicsDevice = std::shared_ptr<GraphicsDevice>;
using PRenderTarget2D = std::shared_ptr<RenderTarget2D>;
using PSwapChain = std::shared_ptr<SwapChain>;
using PTexture = std::shared_ptr<Texture>;
using PTexture2D = std::shared_ptr<Texture2D>;
using PViewport = std::shared_ptr<Viewport>;
using PDrawableGameComponent = std::shared_ptr<DrawableGameComponent>;
using PGame = std::shared_ptr<Game>;
using PGameComponent = std::shared_ptr<GameComponent>;
using PGameComponentCollection = std::shared_ptr<GameComponentCollection>;
using PGameTime = std::shared_ptr<GameTime>;
using PGameWindow = std::shared_ptr<GameWindow>;
using PIDrawable = std::shared_ptr<IDrawable>;
using PIGameComponent = std::shared_ptr<IGameComponent>;
using PIUpdatable = std::shared_ptr<IUpdatable>;
}
#endif

30
framework/game/window.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef XNA_GAME_WINDOW_HPP
#define XNA_GAME_WINDOW_HPP
#include "../enums.hpp"
#include "../common/rectangle.hpp"
namespace xna {
class GameWindow {
public:
PLATFORM_DEVELOPMENT
GameWindow();
PLATFORM_DEVELOPMENT
String Title() const;
PLATFORM_DEVELOPMENT
void Title(String const& title);
PLATFORM_DEVELOPMENT
Rectangle ClientBounds() const;
PLATFORM_DEVELOPMENT
intptr_t Handle() const;
private:
class InternalProperty;
friend class InternalProperty;
public:
sptr<InternalProperty> ip_GameWindow{ nullptr };
};
}
#endif

View File

@ -0,0 +1,71 @@
#ifndef XNA_GRAPHICS_ADAPTER_HPP
#define XNA_GRAPHICS_ADAPTER_HPP
#include "../enums.hpp"
#include "../types.hpp"
#include "../forward.hpp"
#include "displaymode.hpp"
namespace xna {
class GraphicsAdapter {
public:
PLATFORM_DEVELOPMENT
GraphicsAdapter();
static PGraphicsAdapter DefaultAdapter() {
if (_adaptersList.empty())
return nullptr;
if (_defaultAdapter >= _adaptersList.size())
return nullptr;
return _adaptersList[_defaultAdapter];
}
static constexpr void DefaultAdapter(size_t index) {
_defaultAdapter = index;
}
static constexpr std::vector<PGraphicsAdapter> Adapters() {
return _adaptersList;
}
PLATFORM_DEVELOPMENT
String Description() const;
PLATFORM_DEVELOPMENT
Uint DeviceId() const;
PLATFORM_DEVELOPMENT
String DeviceName() const;
bool IsDefaultAdapter() const {
auto def = DefaultAdapter();
return this == def.get();
}
PLATFORM_DEVELOPMENT
intptr_t MonitorHandle() const;
PLATFORM_DEVELOPMENT
Uint Revision() const;
PLATFORM_DEVELOPMENT
Uint SubSystemId() const;
PLATFORM_DEVELOPMENT
Uint VendorId() const;
PLATFORM_DEVELOPMENT
PDisplayModeCollection SupportedDisplayModes() const;
private:
inline static size_t _defaultAdapter = 0;
PLATFORM_DEVELOPMENT
static std::vector<PGraphicsAdapter> getAllAdapters();
inline static std::vector<PGraphicsAdapter> _adaptersList = getAllAdapters();
public:
class InternalProperty;
friend class InternalProperty;
sptr<InternalProperty> ip_GraphicsAdapter{ nullptr };
};
}
#endif

View File

@ -0,0 +1,76 @@
#ifndef XNA_GRAPHICS_BLENDSTATE_HPP
#define XNA_GRAPHICS_BLENDSTATE_HPP
#include "../types.hpp"
#include "../forward.hpp"
#include "../enums.hpp"
namespace xna {
class BlendState {
public:
PLATFORM_DEVELOPMENT
BlendState(GraphicsDevice* device);
static PBlendState Opaque() {
auto blendState = New<BlendState>(nullptr);
blendState->_source = Blend::SourceAlpha;
blendState->_sourceAlpha = Blend::SourceAlpha;
blendState->_destination = Blend::Zero;
blendState->_destinationAlpha = Blend::Zero;
return blendState;
}
static PBlendState AlphaBlend() {
auto blendState = New<BlendState>(nullptr);
blendState->_source = Blend::One;
blendState->_sourceAlpha = Blend::One;
blendState->_destination = Blend::InverseSourceAlpha;
blendState->_destinationAlpha = Blend::InverseSourceAlpha;
return blendState;
}
static PBlendState Additive() {
auto blendState = New<BlendState>(nullptr);
blendState->_source = Blend::SourceAlpha;
blendState->_sourceAlpha = Blend::SourceAlpha;
blendState->_destination = Blend::One;
blendState->_destinationAlpha = Blend::One;
return blendState;
}
static PBlendState NonPremultiplied() {
auto blendState = New<BlendState>(nullptr);
blendState->_source = Blend::SourceAlpha;
blendState->_sourceAlpha = Blend::SourceAlpha;
blendState->_destination = Blend::InverseSourceAlpha;
blendState->_destinationAlpha = Blend::InverseSourceAlpha;
return blendState;
}
PLATFORM_DEVELOPMENT
bool Apply(GraphicsDevice* device);
public:
bool _alphaToCoverage{ false };
bool _enabled{ true };
Blend _source{ Blend::SourceAlpha };
Blend _destination{ Blend::InverseSourceAlpha };
BlendOperation _operation{ BlendOperation::Add };
Blend _sourceAlpha{ Blend::One };
Blend _destinationAlpha{ Blend::Zero };
BlendOperation _operationAlpha{ BlendOperation::Add };
ColorWriteChannels _writeMask{ ColorWriteChannels::All };
GraphicsDevice* _device{nullptr};
public:
class InternalProperty;
friend class InternalProperty;
sptr<InternalProperty> ip_BlendState{ nullptr };
};
}
#endif

View File

@ -0,0 +1,66 @@
#ifndef XNA_GRAPHICS_DEVICE_HPP
#define XNA_GRAPHICS_DEVICE_HPP
#include "../enums.hpp"
#include "../forward.hpp"
#include "../game/window.hpp"
#include "../types.hpp"
#include "adapter.hpp"
#include "rendertarget.hpp"
#include "swapchain.hpp"
#include "viewport.hpp"
#include "blendstate.hpp"
namespace xna {
class GraphicsDevice : public std::enable_shared_from_this<GraphicsDevice> {
public:
PLATFORM_DEVELOPMENT
GraphicsDevice();
PLATFORM_DEVELOPMENT
void Clear();
PLATFORM_DEVELOPMENT
void Clear(Color const& color);
PLATFORM_DEVELOPMENT
bool Initialize(GameWindow& gameWindow);
PLATFORM_DEVELOPMENT
bool Present();
PGraphicsAdapter Adapter() const {
return _adapter;
}
void Adapter(PGraphicsAdapter const& adapter) {
_adapter = adapter;
}
constexpr xna::Viewport Viewport() const {
return _viewport;
}
constexpr void Viewport(xna::Viewport const& viewport) {
_viewport = viewport;
}
PSwapChain GetSwapChain() const {
return _swapChain;
}
private:
PGraphicsAdapter _adapter{ nullptr };
PSwapChain _swapChain{ nullptr };
PRenderTarget2D _renderTarget2D{ nullptr };
xna::Viewport _viewport{};
PBlendState _blendState{ nullptr };
public:
class InternalProperty;
friend class InternalProperty;
sptr<InternalProperty> ip_GraphicsDevice{ nullptr };
};
}
#endif

View File

@ -0,0 +1,80 @@
#ifndef XNA_GRAPHICS_DISPLAYMODE_HPP
#define XNA_GRAPHICS_DISPLAYMODE_HPP
#include "../enums.hpp"
#include "../types.hpp"
#include "../forward.hpp"
namespace xna {
class DisplayMode {
public:
constexpr DisplayMode() = default;
constexpr DisplayMode(Int width, Int height, SurfaceFormat format)
: _width(width), _height(height), _format(format) {}
constexpr float AspectRatio() const noexcept {
if (_height == 0 || _width == 0)
return 0;
return static_cast<float>(_width) / static_cast<float>(_height);
}
constexpr Int Width() const {
return _width;
}
constexpr Int Height() const {
return _height;
}
constexpr SurfaceFormat Format() const {
return _format;
}
public:
Int _width{ 0 };
Int _height{ 0 };
SurfaceFormat _format{ SurfaceFormat::Color };
};
class DisplayModeCollection {
public:
constexpr DisplayModeCollection() = default;
DisplayModeCollection(std::vector<DisplayMode> const& displayModes) :
_displayModes(displayModes) {}
std::vector<DisplayMode> At(SurfaceFormat format) {
std::vector<DisplayMode> modes;
At(format, modes);
return modes;
}
void At(SurfaceFormat format, std::vector<DisplayMode>& modes) {
size_t counter = 0;
for (size_t i = 0; i < _displayModes.size(); ++i) {
const auto& displayMode = _displayModes[i];
if (displayMode.Format() == format)
{
modes.push_back(displayMode);
++counter;
}
}
if (!modes.empty())
modes.resize(counter);
}
std::vector<DisplayMode> operator[](SurfaceFormat format) {
return At(format);
}
private:
std::vector<DisplayMode> _displayModes;
};
}
#endif

View File

@ -0,0 +1,25 @@
#ifndef XNA_GRAPHICS_RENDERTARGET_HPP
#define XNA_GRAPHICS_RENDERTARGET_HPP
#include "texture.hpp"
namespace xna {
class RenderTarget2D : public Texture2D {
public:
RenderTarget2D(GraphicsDevice* device);
PLATFORM_DEVELOPMENT
bool Apply();
private:
GraphicsDevice* _device;
public:
class InternalProperty;
friend class InternalProperty;
sptr<InternalProperty> ip_RenderTarget2D{ nullptr };
};
}
#endif

View File

@ -0,0 +1,31 @@
#ifndef XNA_GRAPHICS_SWAPCHAIN_HPP
#define XNA_GRAPHICS_SWAPCHAIN_HPP
#include "../types.hpp"
#include "../enums.hpp"
#include "../forward.hpp"
#include "../game/window.hpp"
namespace xna {
class SwapChain {
public:
PLATFORM_DEVELOPMENT
SwapChain(GraphicsDevice* device);
PLATFORM_DEVELOPMENT
bool Initialize(GameWindow const& gameWindow);
PLATFORM_DEVELOPMENT
bool Apply();
private:
GraphicsDevice* _device{ nullptr };
public:
class InternalProperty;
friend class InternalProperty;
sptr<InternalProperty> ip_SwapChain{ nullptr };
};
}
#endif

View File

@ -0,0 +1,24 @@
#ifndef XNA_GRAPHICS_TEXTURE_HPP
#define XNA_GRAPHICS_TEXTURE_HPP
#include "../forward.hpp"
#include "../types.hpp"
#include "../enums.hpp"
namespace xna {
class Texture {
};
class Texture2D : public Texture {
public:
PLATFORM_DEVELOPMENT
Texture2D();
public:
class InternalProperty;
friend class InternalProperty;
sptr<InternalProperty> ip_Texture2D{ nullptr };
};
}
#endif

View File

@ -0,0 +1,29 @@
#ifndef XNA_GRAPHICS_VIEWPORT
#define XNA_GRAPHICS_VIEWPORT
namespace xna {
struct Viewport {
float X{ 0 };
float Y{ 0 };
float Width{ 0 };
float Height{ 0 };
float MinDetph{ 0 };
float MaxDepth{ 1.0F };
constexpr Viewport() = default;
constexpr Viewport(float X, float Y, float Width, float Height, float MinDetph, float MaxDepth)
: X(X), Y(Y), Width(Width), Height(Height), MinDetph(MinDetph), MaxDepth(MaxDepth) {}
constexpr bool operator==(const Viewport& other) const {
return X == other.X
&& Y == other.Y
&& Width == other.Width
&& Height == other.Height
&& MinDetph == other.MinDetph
&& MaxDepth == other.MaxDepth;
}
};
}
#endif

26
framework/helpers.hpp Normal file
View File

@ -0,0 +1,26 @@
#ifndef XNA_HELPERS_HPP
#define XNA_HELPERS_HPP
#include <string>
namespace xna {
inline std::wstring StringToWString(const std::string& str)
{
std::wstring wstr;
size_t size;
wstr.resize(str.length());
mbstowcs_s(&size, &wstr[0], wstr.size() + 1, str.c_str(), str.size());
return wstr;
}
inline std::string WStringToString(const std::wstring& wstr)
{
std::string str;
size_t size;
str.resize(wstr.length());
wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size());
return str;
}
}
#endif

View File

@ -0,0 +1,154 @@
#include "adapter-dx.hpp"
#include "../helpers.hpp"
namespace xna {
GraphicsAdapter::GraphicsAdapter() {
ip_GraphicsAdapter = New<InternalProperty>();
}
String GraphicsAdapter::Description() const {
auto& p = ip_GraphicsAdapter;
DXGI_ADAPTER_DESC1 desc;
p->_adapter->GetDesc1(&desc);
String description = WStringToString(desc.Description);
return description;
}
Uint GraphicsAdapter::DeviceId() const {
auto& p = ip_GraphicsAdapter;
DXGI_ADAPTER_DESC1 desc;
p->_adapter->GetDesc1(&desc);
return static_cast<Uint>(desc.DeviceId);
}
String GraphicsAdapter::DeviceName() const {
auto& p = ip_GraphicsAdapter;
IDXGIOutput* pOutput = nullptr;
DXGI_OUTPUT_DESC outputDesc;
if (p->_adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
pOutput->GetDesc(&outputDesc);
String deviceName = WStringToString(outputDesc.DeviceName);
pOutput->Release();
pOutput = nullptr;
return deviceName;
}
return String();
}
intptr_t GraphicsAdapter::MonitorHandle() const {
auto& p = ip_GraphicsAdapter;
IDXGIOutput* pOutput = nullptr;
DXGI_OUTPUT_DESC outputDesc;
if (p->_adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
pOutput->GetDesc(&outputDesc);
pOutput->Release();
pOutput = nullptr;
return reinterpret_cast<intptr_t>(outputDesc.Monitor);
}
return 0;
}
Uint GraphicsAdapter::Revision() const {
auto& p = ip_GraphicsAdapter;
DXGI_ADAPTER_DESC1 desc;
p->_adapter->GetDesc1(&desc);
return static_cast<Uint>(desc.Revision);
}
Uint GraphicsAdapter::SubSystemId() const {
auto& p = ip_GraphicsAdapter;
DXGI_ADAPTER_DESC1 desc;
p->_adapter->GetDesc1(&desc);
return static_cast<Uint>(desc.SubSysId);
}
Uint GraphicsAdapter::VendorId() const {
auto& p = ip_GraphicsAdapter;
DXGI_ADAPTER_DESC1 desc;
p->_adapter->GetDesc1(&desc);
return static_cast<Uint>(desc.VendorId);
}
PDisplayModeCollection GraphicsAdapter::SupportedDisplayModes() const {
auto& p = ip_GraphicsAdapter;
IDXGIOutput* pOutput = nullptr;
UINT numModes = 0;
UINT totalModes = 0;
std::vector<DXGI_MODE_DESC> buffer(250);
if (p->_adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) {
const auto currentSurface = static_cast<SurfaceFormat>(f);
DXGI_FORMAT format = SurfaceFormatMapper::ParseToDXGI(currentSurface);
pOutput->GetDisplayModeList(format, 0, &numModes, nullptr);
if (numModes == 0)
continue;
if (buffer.size() < totalModes + numModes)
buffer.resize(totalModes * 2);
pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data() + totalModes);
totalModes += numModes;
}
pOutput->Release();
buffer.resize(totalModes);
std::vector<DisplayMode> dmodes;
for (size_t i = 0; i < buffer.size(); ++i) {
const auto& modedesc = buffer[i];
const auto surface = SurfaceFormatMapper::ParseToSurface(modedesc.Format);
dmodes[0] = DisplayMode(modedesc.Width, modedesc.Height, surface);
}
dmodes.resize(buffer.size());
return New<DisplayModeCollection>(dmodes);
}
return New<DisplayModeCollection>();
}
std::vector<PGraphicsAdapter> GraphicsAdapter::getAllAdapters() {
IDXGIFactory1* pFactory = nullptr;
std::vector<PGraphicsAdapter> adapters;
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) {
return adapters;
}
IDXGIAdapter1* pAdapter = nullptr;
UINT count = 0;
for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) {
auto adp = New<GraphicsAdapter>();
const auto& p = adp->ip_GraphicsAdapter;
p->_adapter = pAdapter;
adapters.push_back(adp);
}
if (!adapters.empty())
adapters.resize(count);
return adapters;
}
}

View File

@ -0,0 +1,123 @@
#ifndef XNA_PLATFORM_ADAPTER_DX_HPP
#define XNA_PLATFORM_ADAPTER_DX_HPP
#include "../graphics/adapter.hpp"
#include "dxgi.h"
#include "d3d11.h"
namespace xna {
class GraphicsAdapter::InternalProperty {
friend class GraphicsAdapter;
public:
InternalProperty() {
}
~InternalProperty() {
if (_adapter) {
_adapter->Release();
_adapter = nullptr;
}
}
public:
IDXGIAdapter1* _adapter{ nullptr };
};
struct SurfaceFormatMapper {
static constexpr DXGI_FORMAT ParseToDXGI(SurfaceFormat format)
{
switch (format)
{
case SurfaceFormat::Color://21
return DXGI_FORMAT_B8G8R8A8_UNORM;
case SurfaceFormat::Bgr565: //23
return DXGI_FORMAT_B5G6R5_UNORM;
case SurfaceFormat::Bgra5551://25
return DXGI_FORMAT_B5G5R5A1_UNORM;
case SurfaceFormat::Bgra4444://26
return DXGI_FORMAT_B4G4R4A4_UNORM;
case SurfaceFormat::Dxt1://827611204
return DXGI_FORMAT_BC1_UNORM;
case SurfaceFormat::Dxt3://861165636
return DXGI_FORMAT_BC2_UNORM;
case SurfaceFormat::Dxt5://894720068
return DXGI_FORMAT_BC3_UNORM;
case SurfaceFormat::NormalizedByte2://60
return DXGI_FORMAT_R8G8_SNORM;
case SurfaceFormat::NormalizedByte4://63
return DXGI_FORMAT_R8G8B8A8_SNORM;
case SurfaceFormat::Rgba1010102://31
return DXGI_FORMAT_R10G10B10A2_UNORM;
case SurfaceFormat::Rg32://34
return DXGI_FORMAT_R16G16_UNORM;
case SurfaceFormat::Rgba64://36
return DXGI_FORMAT_R16G16B16A16_UNORM;
case SurfaceFormat::Alpha8://28
return DXGI_FORMAT_A8_UNORM;
case SurfaceFormat::Single://114
return DXGI_FORMAT_R32_FLOAT;
case SurfaceFormat::Vector2://115
return DXGI_FORMAT_R32G32_FLOAT;
case SurfaceFormat::Vector4://116
return DXGI_FORMAT_R32G32B32A32_FLOAT;
case SurfaceFormat::HalfSingle://111
return DXGI_FORMAT_R16_FLOAT;
case SurfaceFormat::HalfVector2://112
return DXGI_FORMAT_R16G16_FLOAT;
case SurfaceFormat::HalfVector4://113
return DXGI_FORMAT_R16G16B16A16_FLOAT;
case SurfaceFormat::HdrBlendable://113
return DXGI_FORMAT_R16G16B16A16_FLOAT;
default://0
return DXGI_FORMAT_UNKNOWN;
}
}
static constexpr SurfaceFormat ParseToSurface(DXGI_FORMAT format) {
switch (format)
{
case DXGI_FORMAT_B8G8R8A8_UNORM:
return SurfaceFormat::Color;
case DXGI_FORMAT_B5G6R5_UNORM:
return SurfaceFormat::Bgr565;
case DXGI_FORMAT_B5G5R5A1_UNORM:
return SurfaceFormat::Bgra5551;
case DXGI_FORMAT_B4G4R4A4_UNORM:
return SurfaceFormat::Bgra4444;
case DXGI_FORMAT_BC2_UNORM:
return SurfaceFormat::Dxt3;
case DXGI_FORMAT_BC3_UNORM:
return SurfaceFormat::Dxt5;
case DXGI_FORMAT_R8G8_SNORM:
return SurfaceFormat::NormalizedByte2;
case DXGI_FORMAT_R8G8B8A8_SNORM:
return SurfaceFormat::NormalizedByte4;
case DXGI_FORMAT_R10G10B10A2_UNORM:
return SurfaceFormat::Rgba1010102;
case DXGI_FORMAT_R16G16_UNORM:
return SurfaceFormat::Rg32;
case DXGI_FORMAT_R16G16B16A16_UNORM:
return SurfaceFormat::Rgba64;
case DXGI_FORMAT_A8_UNORM:
return SurfaceFormat::Alpha8;
case DXGI_FORMAT_R32_FLOAT:
return SurfaceFormat::Single;
case DXGI_FORMAT_R32G32_FLOAT:
return SurfaceFormat::Vector2;
case DXGI_FORMAT_R32G32B32A32_FLOAT:
return SurfaceFormat::Vector4;
case DXGI_FORMAT_R16_FLOAT:
return SurfaceFormat::HalfSingle;
case DXGI_FORMAT_R16G16_FLOAT:
return SurfaceFormat::HalfVector2;
case DXGI_FORMAT_R16G16B16A16_FLOAT:
return SurfaceFormat::HalfVector4;
default:
return SurfaceFormat::Color;
}
}
};
}
#endif

View File

@ -0,0 +1,34 @@
#include "blendstate-dx.hpp"
#include "device-dx.hpp"
namespace xna {
BlendState::BlendState(GraphicsDevice* device) : _device(device) {
ip_BlendState = New<InternalProperty>();
}
bool BlendState::Apply(GraphicsDevice* device) {
D3D11_BLEND_DESC blendDesc{};
blendDesc.AlphaToCoverageEnable = _alphaToCoverage;
blendDesc.IndependentBlendEnable = false;
blendDesc.RenderTarget[0].BlendEnable = _enabled;
blendDesc.RenderTarget[0].SrcBlend = BlendMapper::ConvertBlend(_source);
blendDesc.RenderTarget[0].DestBlend = BlendMapper::ConvertBlend(_destination);
blendDesc.RenderTarget[0].BlendOp = BlendMapper::ConvertOperation(_operation);
blendDesc.RenderTarget[0].SrcBlendAlpha = BlendMapper::ConvertBlend(_sourceAlpha);
blendDesc.RenderTarget[0].DestBlendAlpha = BlendMapper::ConvertBlend(_destinationAlpha);
blendDesc.RenderTarget[0].BlendOpAlpha = BlendMapper::ConvertOperation(_operationAlpha);
blendDesc.RenderTarget[0].RenderTargetWriteMask = BlendMapper::ConvertColorWrite(_writeMask);
if (_device == nullptr || _device != device)
_device = device;
auto& p = _device->ip_GraphicsDevice;
if FAILED(p->_device->CreateBlendState(&blendDesc, &ip_BlendState->_blendState))
return false;
p->_context->OMSetBlendState(ip_BlendState->_blendState, nullptr, 0xffffffff);
return true;
}
}

View File

@ -0,0 +1,105 @@
#ifndef XNA_PLATFORM_BLENDSTATE_HPP
#define XNA_PLATFORM_BLENDSTATE_HPP
#include "../graphics/blendstate.hpp"
#include "dxgi.h"
#include "d3d11.h"
namespace xna {
class BlendState::InternalProperty {
friend class BlendState;
friend class GraphicsDevice;
public:
~InternalProperty() {
if (_blendState) {
_blendState->Release();
_blendState = nullptr;
}
}
private:
ID3D11BlendState* _blendState{ nullptr };
};
struct BlendMapper {
static constexpr D3D11_BLEND ConvertBlend(Blend blend) {
switch (blend)
{
case xna::Blend::Zero:
return D3D11_BLEND_ZERO;
case xna::Blend::One:
return D3D11_BLEND_ONE;
case xna::Blend::SourceColor:
return D3D11_BLEND_SRC_COLOR;
case xna::Blend::InverseSourceColor:
return D3D11_BLEND_INV_SRC_COLOR;
case xna::Blend::SourceAlpha:
return D3D11_BLEND_SRC_ALPHA;
case xna::Blend::InverseSourceAlpha:
return D3D11_BLEND_INV_SRC_ALPHA;
case xna::Blend::DestinationAlpha:
return D3D11_BLEND_DEST_ALPHA;
case xna::Blend::InverseDestinationAlpha:
return D3D11_BLEND_INV_DEST_ALPHA;
case xna::Blend::DestinationColor:
return D3D11_BLEND_DEST_COLOR;
case xna::Blend::InverseDestinationColor:
return D3D11_BLEND_INV_DEST_COLOR;
case xna::Blend::SourceAlphaSaturation:
return D3D11_BLEND_SRC_ALPHA_SAT;
case xna::Blend::BlendFactor:
return D3D11_BLEND_BLEND_FACTOR;
case xna::Blend::InverseBlendFactor:
return D3D11_BLEND_INV_BLEND_FACTOR;
case xna::Blend::Source1Color:
return D3D11_BLEND_SRC1_COLOR;
case xna::Blend::InverseSource1Color:
return D3D11_BLEND_INV_SRC1_COLOR;
case xna::Blend::Source1Alpha:
return D3D11_BLEND_SRC1_ALPHA;
case xna::Blend::InverseSource1Alpha:
return D3D11_BLEND_INV_SRC1_ALPHA;
default:
return D3D11_BLEND_ZERO;
}
}
static constexpr D3D11_BLEND_OP ConvertOperation(BlendOperation op) {
switch (op)
{
case BlendOperation::Add:
return D3D11_BLEND_OP_ADD;
case BlendOperation::Subtract:
return D3D11_BLEND_OP_SUBTRACT;
case BlendOperation::ReverseSubtract:
return D3D11_BLEND_OP_REV_SUBTRACT;
case BlendOperation::Min:
return D3D11_BLEND_OP_MIN;
case BlendOperation::Max:
return D3D11_BLEND_OP_MAX;
default:
return D3D11_BLEND_OP_ADD;
}
}
static constexpr D3D11_COLOR_WRITE_ENABLE ConvertColorWrite(ColorWriteChannels colorWrite) {
switch (colorWrite)
{
case xna::ColorWriteChannels::Red:
return D3D11_COLOR_WRITE_ENABLE_RED;
case xna::ColorWriteChannels::Green:
return D3D11_COLOR_WRITE_ENABLE_GREEN;
case xna::ColorWriteChannels::Blue:
return D3D11_COLOR_WRITE_ENABLE_BLUE;
case xna::ColorWriteChannels::Alpha:
return D3D11_COLOR_WRITE_ENABLE_ALPHA;
case xna::ColorWriteChannels::All:
return D3D11_COLOR_WRITE_ENABLE_ALL;
default:
return D3D11_COLOR_WRITE_ENABLE_ALL;
}
}
};
}
#endif

View File

@ -0,0 +1,127 @@
#include "device-dx.hpp"
#include "window-dx.hpp"
#include "swapchain-dx.hpp"
#include "rendertarget-dx.hpp"
#include "adapter-dx.hpp"
namespace xna {
GraphicsDevice::GraphicsDevice() {
_adapter = New<GraphicsAdapter>();
_blendState = BlendState::NonPremultiplied();
_adapter = GraphicsAdapter::DefaultAdapter();
ip_GraphicsDevice = New<InternalProperty>();
}
bool GraphicsDevice::Initialize(GameWindow& gameWindow) {
auto& p = ip_GraphicsDevice;
p->_createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
if (p->_device) {
p->_device->Release();
p->_device = nullptr;
}
if (p->_context) {
p->_context->Release();
p->_context = nullptr;
}
const auto bounds = gameWindow.ClientBounds();
_viewport = xna::Viewport(0.0F, 0.0F,
static_cast<float>(bounds.Width),
static_cast<float>(bounds.Height),
0.0F, 1.F);
if FAILED(
D3D11CreateDevice(
_adapter->ip_GraphicsAdapter->_adapter, // adaptador de vídeo (NULL = adaptador padrão)
D3D_DRIVER_TYPE_HARDWARE, // tipo de driver D3D (Hardware, Reference ou Software)
NULL, // ponteiro para rasterizador em software
p->_createDeviceFlags, // modo de depuração ou modo normal
NULL, // featureLevels do Direct3D (NULL = maior suportada)
0, // tamanho do vetor featureLevels
D3D11_SDK_VERSION, // versão do SDK do Direct3D
&p->_device, // guarda o dispositivo D3D criado
&p->_featureLevel, // versão do Direct3D utilizada
&p->_context)) // contexto do dispositivo D3D
{
// sistema não suporta dispositivo D3D11
// fazendo a criação de um WARP Device que
// implementa um rasterizador em software
if FAILED(D3D11CreateDevice(
NULL,
D3D_DRIVER_TYPE_WARP,
NULL,
p->_createDeviceFlags,
NULL,
0,
D3D11_SDK_VERSION,
&p->_device,
&p->_featureLevel,
&p->_context))
return false;
OutputDebugString("---> Usando Adaptador WARP: não há suporte ao D3D11\n");
}
COLORREF color = gameWindow.ip_GameWindow->Color();
p->_backgroundColor[0] = GetRValue(color) / 255.0f;
p->_backgroundColor[1] = GetGValue(color) / 255.0f;
p->_backgroundColor[2] = GetBValue(color) / 255.0f;
p->_backgroundColor[3] = 1.0f;
if (!_swapChain)
_swapChain = New<xna::SwapChain>(this);
_swapChain->Initialize(gameWindow);
if (!_swapChain->Apply())
return false;
IDXGIFactory1* dxgiFactory = nullptr;
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&dxgiFactory))
return false;
if FAILED(dxgiFactory->MakeWindowAssociation(gameWindow.ip_GameWindow->WindowHandle(), DXGI_MWA_NO_ALT_ENTER))
return false;
if (!_renderTarget2D) {
_renderTarget2D = New<RenderTarget2D>(this);
}
if (!_renderTarget2D->Apply())
return false;
D3D11_VIEWPORT view{};
view.TopLeftX = _viewport.X;
view.TopLeftY = _viewport.Y;
view.Width = _viewport.Width;
view.Height = _viewport.Height;
view.MinDepth = _viewport.MinDetph;
view.MaxDepth = _viewport.MaxDepth;
p->_context->RSSetViewports(1, &view);
_blendState->Apply(this);
return true;
}
bool GraphicsDevice::Present() {
_swapChain->ip_SwapChain->_swapChain->Present(false, NULL);
auto& p = ip_GraphicsDevice;
auto& pr = _renderTarget2D->ip_RenderTarget2D;
p->_context->OMSetRenderTargets(1, &pr->_renderTargetView, nullptr);
return true;
}
void GraphicsDevice::Clear() {
auto& p = ip_GraphicsDevice;
auto& pr = _renderTarget2D->ip_RenderTarget2D;
p->_context->ClearRenderTargetView(pr->_renderTargetView, p->_backgroundColor);
}
}

View File

@ -0,0 +1,46 @@
#ifndef XNA_PLATFORM_DEVICE_DX_HPP
#define XNA_PLATFORM_DEVICE_DX_HPP
#include "../graphics/device.hpp"
#include "dxgi.h"
#include "d3d11.h"
namespace xna {
class GraphicsDevice::InternalProperty {
friend class GraphicsDevice;
public:
InternalProperty() {}
~InternalProperty() {
if (_device) {
_device->Release();
_device = nullptr;
}
if (_context) {
_context->Release();
_context = nullptr;
}
}
constexpr void SetCreateFlags(D3D11_CREATE_DEVICE_FLAG flags) {
_createDeviceFlags |= flags;
}
constexpr void ClearCreateFlags() {
_createDeviceFlags = 0;
}
public:
ID3D11Device* _device{ nullptr };
ID3D11DeviceContext* _context{ nullptr };
private:
unsigned int _createDeviceFlags{ 0 };
D3D_FEATURE_LEVEL _featureLevel{ D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0 };
float _backgroundColor[4] = { 0, 0, 0, 0 };
};
}
#endif

View File

@ -0,0 +1,34 @@
#include "rendertarget-dx.hpp"
#include "../graphics/device.hpp"
#include "swapchain-dx.hpp"
#include "device-dx.hpp"
namespace xna {
RenderTarget2D::RenderTarget2D(GraphicsDevice* device) : _device(device) {
this->ip_RenderTarget2D = New<InternalProperty>();
}
bool RenderTarget2D::Apply() {
auto& p1 = this->ip_Texture2D;
auto swapChain = _device->GetSwapChain();
if (p1->_texture2D) {
p1->_texture2D->Release();
p1->_texture2D = nullptr;
}
if (!swapChain->ip_SwapChain->GetBackBuffer(p1->_texture2D))
return false;
auto& device = _device->ip_GraphicsDevice->_device;
auto& p2 = this->ip_RenderTarget2D;
if FAILED(device->CreateRenderTargetView(p1->_texture2D, NULL, &p2->_renderTargetView))
return false;
auto& context = _device->ip_GraphicsDevice->_context;
context->OMSetRenderTargets(1, &p2->_renderTargetView, nullptr);
return true;
}
}

View File

@ -0,0 +1,20 @@
#ifndef XNA_PLATFORM_RENDERTARGET_DX_HPP
#define XNA_PLATFORM_RENDERTARGET_DX_HPP
#include "../graphics/rendertarget.hpp"
#include "texture-dx.hpp"
#include "dxgi.h"
#include "d3d11.h"
namespace xna {
class RenderTarget2D::InternalProperty {
friend class RenderTarget2D;
friend class GraphicsDevice;
public:
private:
ID3D11RenderTargetView* _renderTargetView;
};
}
#endif

View File

@ -0,0 +1,54 @@
#include "swapchain-dx.hpp"
#include "../graphics/device.hpp"
#include "adapter-dx.hpp"
#include "device-dx.hpp"
namespace xna {
SwapChain::SwapChain(GraphicsDevice* device) :
_device(device) {
ip_SwapChain = New<InternalProperty>();
}
bool SwapChain::Initialize(GameWindow const& gameWindow) {
auto& p = ip_SwapChain;
const auto bounds = gameWindow.ClientBounds();
p->_swapDescription.BufferDesc.Width = static_cast<UINT>(bounds.Width);
p->_swapDescription.BufferDesc.Height = static_cast<UINT>(bounds.Height);
p->_swapDescription.BufferDesc.RefreshRate.Numerator = 60;
p->_swapDescription.BufferDesc.RefreshRate.Denominator = 1;
p->_swapDescription.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
p->_swapDescription.SampleDesc.Count = 1;
p->_swapDescription.SampleDesc.Quality = 0;
p->_swapDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
p->_swapDescription.BufferCount = 2;
p->_swapDescription.OutputWindow = gameWindow.ip_GameWindow->WindowHandle();
p->_swapDescription.Windowed = gameWindow.ip_GameWindow->Mode() != GameWindowMode::Fullscreen;
p->_swapDescription.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
p->_swapDescription.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
return true;
}
bool SwapChain::Apply() {
auto& p = ip_SwapChain;
auto adapter = _device->Adapter();
auto dxAdapter = adapter->ip_GraphicsAdapter->_adapter;
IDXGIFactory* dxFactory = nullptr;
if FAILED(dxAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxFactory))
return false;
auto dxdevice = _device->ip_GraphicsDevice->_device;
if FAILED(dxFactory->CreateSwapChain(dxdevice, &p->_swapDescription, &p->_swapChain))
return false;
dxFactory->Release();
dxFactory = nullptr;
return true;
}
}

View File

@ -0,0 +1,36 @@
#ifndef XNA_PLATFORM_SWAPCHAIN_DX_HPP
#define XNA_PLATFORM_SWAPCHAIN_DX_HPP
#include "../graphics/swapchain.hpp"
#include "window-dx.hpp"
#include "dxgi.h"
#include "d3d11.h"
namespace xna {
class SwapChain::InternalProperty {
friend class SwapChain;
friend class GraphicsDevice;
public:
InternalProperty(){}
~InternalProperty() {
if (_swapChain) {
_swapChain->Release();
_swapChain = nullptr;
}
}
bool GetBackBuffer(ID3D11Texture2D*& texture2D) {
if FAILED(_swapChain->GetBuffer(0, __uuidof(texture2D), (void**)(&texture2D)))
return false;
return true;
}
private:
IDXGISwapChain* _swapChain{nullptr};
DXGI_SWAP_CHAIN_DESC _swapDescription{};
};
}
#endif

View File

@ -0,0 +1,7 @@
#include "texture-dx.hpp"
namespace xna {
Texture2D::Texture2D() {
ip_Texture2D = New<InternalProperty>();
}
}

View File

@ -0,0 +1,20 @@
#ifndef XNA_PLATFORM_TEXTURE_DX_HPP
#define XNA_PLATFORM_TEXTURE_DX_HPP
#include "../graphics/texture.hpp"
#include "dxgi.h"
#include "d3d11.h"
namespace xna {
class Texture2D::InternalProperty {
friend class Texture2D;
friend class GraphicsDevice;
friend class RenderTarget2D;
public:
private:
ID3D11Texture2D* _texture2D{nullptr};
};
}
#endif

View File

@ -0,0 +1,108 @@
#include "window-dx.hpp"
namespace xna {
GameWindow::GameWindow() {
ip_GameWindow = New<InternalProperty>(this);
auto& p = ip_GameWindow;
p->_hInstance = GetModuleHandle(NULL);
p->_windowIcon = LoadIcon(NULL, IDI_APPLICATION);
p->_windowCursor = LoadCursor(NULL, IDC_ARROW);
p->_windowStyle = static_cast<int>(GameWindowMode::Windowed);
p->_windowCenterX = p->_windowWidth / 2.0F;
p->_windowCenterY = p->_windowHeight / 2.0F;
}
void GameWindow::Title(String const& title) {
ip_GameWindow->_windowTitle = title;
}
void GameWindow::InternalProperty::Size(int width, int height) {
_windowWidth = width;
_windowHeight = height;
setCenter();
setPosition();
}
bool GameWindow::InternalProperty::Create() {
WNDCLASSEX wndClass{};
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WinProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = _hInstance;
wndClass.hIcon = _windowIcon;
wndClass.hCursor = _windowCursor;
wndClass.hbrBackground = (HBRUSH)CreateSolidBrush(_windowColor);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = "XnaGameWindow";
wndClass.hIconSm = _windowIcon;
if (!RegisterClassEx(&wndClass))
return false;
_windowHandle = CreateWindowEx(
NULL,
"XnaGameWindow",
_windowTitle.c_str(),
_windowStyle,
_windowPosX, _windowPosY,
_windowWidth, _windowHeight,
NULL,
NULL,
_hInstance,
NULL);
if (_windowStyle == static_cast<int>(GameWindowMode::Windowed)) {
RECT winRect = { 0, 0, _windowWidth, _windowHeight };
AdjustWindowRectEx(&winRect,
GetWindowStyle(_windowHandle),
GetMenu(_windowHandle) != NULL,
GetWindowExStyle(_windowHandle));
_windowPosX = GetSystemMetrics(SM_CXSCREEN) / 2 - (winRect.right - winRect.left) / 2;
_windowPosY = GetSystemMetrics(SM_CYSCREEN) / 2 - (winRect.bottom - winRect.top) / 2;
MoveWindow(
_windowHandle,
_windowPosX,
_windowPosY,
winRect.right - winRect.left,
winRect.bottom - winRect.top,
TRUE);
return _windowHandle ? true : false;
}
}
String GameWindow::Title() const {
return ip_GameWindow->_windowTitle;
}
Rectangle GameWindow::ClientBounds() const {
return Rectangle(
ip_GameWindow->_windowPosX,
ip_GameWindow->_windowPosY,
ip_GameWindow->_windowWidth,
ip_GameWindow->_windowHeight
);
}
intptr_t GameWindow::Handle() const {
return reinterpret_cast<intptr_t>(ip_GameWindow->_windowHandle);
}
LRESULT GameWindow::InternalProperty::WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}

View File

@ -0,0 +1,125 @@
#ifndef XNA_PLATFORM_WINDOW_DX_HPP
#define XNA_PLATFORM_WINDOW_DX_HPP
#include "../game/window.hpp"
#include <Windows.h>
#include <windowsx.h>
namespace xna {
enum class GameWindowMode : UINT {
Fullscreen = WS_POPUP | WS_VISIBLE,
Windowed = WS_OVERLAPPED | WS_SYSMENU | WS_VISIBLE,
Borderless = WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
};
class GameWindow::InternalProperty {
friend class GameWindow;
public:
InternalProperty(GameWindow* gamewindow) :
_gamewindow(gamewindow){}
constexpr void Mode(GameWindowMode mode) {
_windowStyle = static_cast<int>(mode);
}
constexpr GameWindowMode Mode() const {
return static_cast<GameWindowMode>(_windowStyle);
}
void Size(int width, int height);
inline HINSTANCE HInstance() const {
return _hInstance;
}
inline HWND WindowHandle() const {
return _windowHandle;
}
inline int Width() const {
return _windowWidth;
}
inline int Height() const {
return _windowHeight;
}
inline void Icon(unsigned int icon) {
_windowIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(icon));
}
inline void Icon(HICON icon) {
_windowIcon = icon;
}
inline void Cursor(unsigned int cursor) {
_windowCursor = LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(cursor));
}
inline void Cursor(HCURSOR cursor) {
_windowCursor = cursor;
}
inline float CenterX() const {
return _windowCenterX;
}
inline float CenterY() const {
return _windowCenterY;
}
inline void CursorVisibility(bool visible) const {
ShowCursor(visible);
}
inline void Close() {
PostMessage(_windowHandle, WM_DESTROY, 0, 0);
}
inline COLORREF Color() const {
return _windowColor;
}
inline void Color(COLORREF color) {
_windowColor = color;
}
inline void Color(BYTE r, BYTE g, BYTE b) {
_windowColor = RGB(r, g, b);
}
bool Create();
static LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
private:
HINSTANCE _hInstance{ nullptr };
HWND _windowHandle{ nullptr };
int _windowWidth{ 800 };
int _windowHeight{ 600 };
HICON _windowIcon{ nullptr };
HCURSOR _windowCursor{ nullptr };
COLORREF _windowColor{ RGB(0,0,0) };
String _windowTitle{ "Xna++ Game Development" };
DWORD _windowStyle{ 0 };
int _windowPosX{ 0 };
int _windowPosY{ 0 };
float _windowCenterX{ 0 };
float _windowCenterY{ 0 };
private:
GameWindow* _gamewindow = nullptr;
inline void setPosition() {
_windowPosX = GetSystemMetrics(SM_CXSCREEN) / 2 - _windowWidth / 2;
_windowPosY = GetSystemMetrics(SM_CYSCREEN) / 2 - _windowHeight / 2;
}
inline void setCenter() {
_windowCenterX = _windowWidth / 2.0f;
_windowCenterY = _windowHeight / 2.0f;
}
};
}
#endif

58
framework/types.hpp Normal file
View File

@ -0,0 +1,58 @@
#ifndef XNA_TYPES_HPP
#define XNA_TYPES_HPP
#include <cstdint>
#include <limits>
#include <vector>
#include <string>
#include <memory>
#include <utility>
namespace xna {
using Sbyte = int8_t;
using Byte = uint8_t;
using Short = int16_t;
using Ushort = uint16_t;
using Int = int32_t;
using Uint = uint32_t;
using Long = int64_t;
using Ulong = uint64_t;
using Char = char16_t;
constexpr Sbyte SbyteMaxValue = std::numeric_limits<Sbyte>::max();
constexpr Sbyte SbyteMinValue = std::numeric_limits<Sbyte>::min();
constexpr Byte ByteMaxValue = std::numeric_limits<Byte>::max();
constexpr Byte ByteMinValue = std::numeric_limits<Byte>::min();
constexpr Short ShortMaxValue = std::numeric_limits<Short>::max();
constexpr Short ShortMinValue = std::numeric_limits<Short>::min();
constexpr Ushort UshortMaxValue = std::numeric_limits<Ushort>::max();
constexpr Ushort UshortMinValue = std::numeric_limits<Ushort>::min();
constexpr Int IntMaxValue = std::numeric_limits<Int>::max();
constexpr Int IntMinValue = std::numeric_limits<Int>::min();
constexpr Uint UintMaxValue = std::numeric_limits<Uint>::max();
constexpr Uint UintMinValue = std::numeric_limits<Uint>::min();
constexpr Long LongMaxValue = std::numeric_limits<Long>::max();
constexpr Long LongMinValue = std::numeric_limits<Long>::min();
constexpr Ulong UlongMaxValue = std::numeric_limits<Ulong>::max();
constexpr Ulong UlongMinValue = std::numeric_limits<Ulong>::min();
constexpr Char CharMaxValue = std::numeric_limits<Char>::max();
constexpr Char CharMinValue = std::numeric_limits<Char>::min();
constexpr float FloatMaxValue = std::numeric_limits<float>::max();
constexpr float FloatMinValue = std::numeric_limits<float>::min();
constexpr double DoubleMaxValue = std::numeric_limits<double>::max();
constexpr double DoubleMinValue = std::numeric_limits<double>::min();
using String = std::string;
template <typename T>
using sptr = std::shared_ptr<T>;
template <class _Ty, class... _Types>
inline std::shared_ptr<_Ty> New(_Types&&... _Args) {
return std::make_shared<_Ty>(std::forward<_Types>(_Args)...);
}
#define PLATFORM_DEVELOPMENT
}
#endif

40
framework/xna.cpp Normal file
View File

@ -0,0 +1,40 @@
// xna.cpp : Defines the entry point for the application.
//
#include "xna.h"
using namespace std;
using namespace xna;
//int main()
//{
// cout << "Hello CMake." << endl;
// return 0;
//}
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
GameWindow gw;
MSG msg = { 0 };
gw.ip_GameWindow->Color(255, 155, 55);
gw.Title("Teste de título");
gw.ip_GameWindow->Create();
GraphicsDevice device;
device.Initialize(gw);
do {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
device.Clear();
device.Present();
}
} while (msg.message != WM_QUIT);
return 0;
}

13
framework/xna.h Normal file
View File

@ -0,0 +1,13 @@
// xna.h : Include file for standard system include files,
// or project specific include files.
#pragma once
#define NOMINMAX
#include <iostream>
#include "Windows.h"
#include "game/window.hpp"
#include "platform/window-dx.hpp"
#include "graphics/device.hpp"
// TODO: Reference additional headers your program requires here.