31 #if defined(_DEBUG) && defined(_WINDOWS)
32 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
38 Video_DX9::Video_DX9()
39 : m_d3d_capabilities(0),
44 m_begun_render(
false),
48 m_render_to_surface(0),
51 m_d3d9 = LoadLibrary(
"d3d9.dll");
53 std::cerr <<
"Loading d3d9.dll failed." << std::endl;
55 throw Video_Init_Failure();
58 g_Direct3DCreate9 = (Direct3DCreate9_fcn)GetProcAddress(m_d3d9,
"Direct3DCreate9");
59 if(!g_Direct3DCreate9) {
60 std::cerr <<
"Loading d3d9.dll failed." << std::endl;
66 throw Video_Init_Failure();
69 m_d3dx9 = LoadLibrary(
"D3DX9_43.dll");
70 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"D3DX9_42.dll");
71 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"D3DX9_41.dll");
72 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"D3DX9_40.dll");
73 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"D3DX9_39.dll");
74 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"D3DX9_38.dll");
75 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"D3DX9_37.dll");
76 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_36.dll");
77 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_35.dll");
78 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_34.dll");
79 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_33.dll");
80 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_32.dll");
81 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_31.dll");
82 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_30.dll");
83 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_29.dll");
84 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_28.dll");
85 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_27.dll");
86 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_26.dll");
87 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_25.dll");
88 if(!m_d3dx9) m_d3dx9 = LoadLibrary(
"d3dx9_24.dll");
90 std::cerr <<
"Loading d3dx9.dll failed." << std::endl;
96 throw Video_Init_Failure();
99 g_D3DXCreateRenderToSurface = (D3DXCreateRenderToSurface_fcn)GetProcAddress(m_d3dx9,
"D3DXCreateRenderToSurface");
100 g_D3DXCreateTexture = (D3DXCreateTexture_fcn)GetProcAddress(m_d3dx9,
"D3DXCreateTexture");
101 g_D3DXFilterTexture = (D3DXFilterTexture_fcn)GetProcAddress(m_d3dx9,
"D3DXFilterTexture");
102 g_D3DXCreateMatrixStack = (D3DXCreateMatrixStack_fcn)GetProcAddress(m_d3dx9,
"D3DXCreateMatrixStack");
103 g_D3DXCompileShader = (D3DXCompileShader_fcn)GetProcAddress(m_d3dx9,
"D3DXCompileShader");
104 if(!g_D3DXCreateRenderToSurface || !g_D3DXCreateTexture ||
105 !g_D3DXFilterTexture || !g_D3DXCreateMatrixStack ||
106 !g_D3DXCompileShader)
108 std::cerr <<
"Loading d3dx9.dll failed." << std::endl;
110 FreeLibrary(m_d3dx9);
115 throw Video_Init_Failure();
120 FreeLibrary(m_d3dx9);
125 throw Video_Init_Failure();
132 delete m_d3d_parameters;
133 delete m_d3d_capabilities;
135 FreeLibrary(m_d3dx9);
140 throw Video_Init_Failure();
144 Video_DX9::~Video_DX9() {
152 delete m_d3d_parameters;
153 delete m_d3d_capabilities;
155 FreeLibrary(m_d3dx9);
164 assert(!m_render_target && !m_render_to_surface);
176 m_back_buffer->Release();
180 if(FAILED(m_d3d_device->Reset(m_d3d_parameters)))
185 m_d3d_device->GetRenderTarget(0, &m_back_buffer);
198 assert(!m_render_target && !m_render_to_surface);
200 HRESULT result = m_begun_render ? m_d3d_device->Present(0, 0, 0, 0) : S_OK;
203 m_begun_render =
true;
206 m_d3d_device->SetViewport(&vp);
208 m_d3d_device->BeginScene();
224 m_d3d_device->EndScene();
228 class PrePostRenderActor {
229 PrePostRenderActor & operator=(
const PrePostRenderActor &) {
return *
this;}
232 PrePostRenderActor(
const Renderable &renderable_)
233 : renderable(renderable_)
238 ~PrePostRenderActor() {
262 void Video_DX9::set_2d_view(
const std::pair<Point2f, Point2f> &camera2d,
const std::pair<Point2i, Point2i> &viewport,
const bool &fix_aspect_ratio) {
269 m_d3d_device->SetTransform(
D3DTS_WORLD, world_ptr);
270 m_matrix_stack->LoadMatrix(world_ptr);
279 m_d3d_device->SetTransform(
D3DTS_WORLD, world_ptr);
280 m_matrix_stack->LoadMatrix(world_ptr);
338 int ref =
static_cast<int>(255.0f * value + 0.5f);
374 m_d3d_device->SetTexture(0, 0);
398 if(number < 0 || 7 < number)
407 if(number < 0 || 7 < number)
412 m_d3d_device->LightEnable(
DWORD(number),
FALSE);
420 material.
unset(*
this);
447 m_d3d_device->SetVertexShader(0);
448 m_d3d_device->SetPixelShader(0);
459 if(FAILED(tdx.m_texture->GetSurfaceLevel(0, &render_surface)))
464 if(!m_render_to_surface || FAILED(m_d3d_device->SetRenderTarget(0, render_surface)))
467 if(FAILED(m_d3d_device->BeginScene())) {
468 m_d3d_device->SetRenderTarget(0, m_back_buffer);
472 m_render_target = &tdx;
476 if(!m_render_target || !m_render_to_surface)
481 m_d3d_device->EndScene();
482 m_render_to_surface = 0;
484 if(FAILED(m_d3d_device->SetRenderTarget(0, m_back_buffer)))
507 m_d3d_device->SetTransform(
D3DTS_WORLD, m_matrix_stack->GetTop());
511 m_matrix_stack->TranslateLocal(direction.
i, direction.
j, direction.
k);
512 m_d3d_device->SetTransform(
D3DTS_WORLD, m_matrix_stack->GetTop());
516 m_matrix_stack->RotateAxisLocal(reinterpret_cast<const D3DXVECTOR3 *>(&about), radians);
517 m_d3d_device->SetTransform(
D3DTS_WORLD, m_matrix_stack->GetTop());
521 m_matrix_stack->ScaleLocal(factor.
i, factor.
j, factor.
k);
522 m_d3d_device->SetTransform(
D3DTS_WORLD, m_matrix_stack->GetTop());
526 m_matrix_stack->MultMatrixLocal(reinterpret_cast<const D3DXMATRIX * const>(&transformation));
527 m_d3d_device->SetTransform(
D3DTS_WORLD, m_matrix_stack->GetTop());
537 m_d3d_device->SetTransform(
D3DTS_VIEW, reinterpret_cast<D3DXMATRIX *>(const_cast<Matrix4f *>(&view)));
543 m_d3d_device->SetTransform(
D3DTS_PROJECTION, reinterpret_cast<D3DXMATRIX *>(const_cast<Matrix4f *>(&projection)));
549 D3DVIEWPORT9 vp = {
DWORD(viewport.first.x),
DWORD(viewport.first.y),
DWORD(viewport.second.x - viewport.first.x),
DWORD(viewport.second.y - viewport.first.y), 0
u, 1
u};
550 m_d3d_device->SetViewport(&vp);
565 while(corrected.
x > max.
x)
567 while(corrected.
y > max.
y)
574 return new Font_FT(filename, glyph_height, virtual_screen_height);
594 return *m_d3d_capabilities;
598 return *m_d3d_parameters;
604 #if SDL_VERSION_ATLEAST(2,0,0)
606 HWND hWnd = wmInfo.
info.win.window;
608 SDL_GetWMInfo(&wmInfo);
609 HWND hWnd = wmInfo.window;
612 std::cerr <<
"Initializing DirectX 9" << std::endl;
624 m_dpi = GetDeviceCaps(GetDC(hWnd), LOGPIXELSY);
626 ZeroMemory(m_d3d_parameters,
sizeof(m_d3d_parameters));
639 m_d3d_parameters->
Flags = 0;
676 m_d3d_device->GetRenderTarget(0, &m_back_buffer);
680 DWORD num_quality_levels;
717 m_matrix_stack->LoadIdentity();
768 for(
int i = 0;
i != 8; ++
i)
783 m_matrix_stack->Release();
788 m_back_buffer->Release();
793 m_d3d_device->Release();
798 void Video_DX9::zero_handles() {
799 g_Direct3DCreate9 = 0;
801 g_D3DXCreateRenderToSurface = 0;
802 g_D3DXCreateTexture = 0;
803 g_D3DXFilterTexture = 0;
804 g_D3DXCreateMatrixStack = 0;
805 g_D3DXCompileShader = 0;
808 void Video_DX9::set_fvf(
const bool &is_3d) {
814 void Video_DX9::set_fvf() {
825 m_d3d_device->SetFVF(fvf);
D3DFORMAT AutoDepthStencilFormat
Vertex_Buffer_Renderer * create_Vertex_Buffer_Renderer(Vertex_Buffer &vertex_buffer)
Function for creating a Vertex_Buffer_Renderer.
void set_projection_matrix(const Matrix4f &projection)
Set the projection Matrix4f.
DECLSPEC SDL_bool SDLCALL SDL_GetWindowWMInfo(SDL_Window *window, SDL_SysWMinfo *info)
This function allows access to driver-dependent window information.
An Abstraction of a Light.
virtual void set_lighting(const bool &on=true)=0
Set lighting on/off.
#define D3DERR_DEVICELOST
const Color & get_clear_Color() const
Get the blank background color.
HRESULT(WINAPI * D3DXCompileShader_fcn)(LPCSTR pSrcData, UINT srcDataLen, const D3DXMACRO *pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER *ppShader, LPD3DXBUFFER *ppErrorMsgs, LPD3DXCONSTANTTABLE *ppConstantTable)
Texture * load_Texture(const String &filename, const bool &repeat, const bool &lazy_loading=false)
Function for loading a Texture; used internally by Textures.
unsigned char a_ub() const
Get the alpha channel [0x00, 0xFF].
virtual void set_vertical_sync(const bool &on)=0
Set vertical_sync on/off.
D3DFORMAT BackBufferFormat
const GLubyte GLuint GLuint GLuint GLuint alpha GLboolean GLboolean GLboolean GLboolean alpha GLint GLint GLsizei GLsizei GLenum type GLenum GLint GLenum GLint GLint GLsizei GLsizei GLint border GLenum GLint GLint GLint GLint GLint GLsizei GLsizei height GLsizei GLsizei GLenum GLenum const GLvoid *pixels GLenum GLint GLint GLint GLint j2 GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum light
static bool g_video_dx9_reset
virtual void post_render() const
virtual void render_to(Video_GL_Fixed &screen) const =0
Overridden for OpenGL rendering.
enum _D3DCMPFUNC D3DCMPFUNC
Font * create_Font(const String &filename, const float &glyph_height, const float &virtual_screen_height)
Function for creating a Font; used internally by Fonts.
IDirect3D9 *WINAPI Direct3DCreate9(UINT SDKVersion)
typedef HRESULT(WINAPI *LPD3DXIMTSIGNALCALLBACK)(CONST D3DXVECTOR2 *uv
LPDIRECT3DVERTEXSHADER9 get_vertex_shader() const
virtual void set_view_matrix(const Matrix4f &view)=0
Set the view Matrix4f.
LPDIRECT3D9(WINAPI * Direct3DCreate9_fcn)(UINT SDKVersion)
virtual void unset_Fog()=0
Unset Fog.
void unset_program()
Disable a program.
#define D3DCOLOR_ARGB(a, r, g, b)
GLenum GLsizei const GLuint GLboolean enabled
void set_2d_view(const std::pair< Point2f, Point2f > &camera2d, const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get_Video().get_render_target_size()), const bool &fix_aspect_ratio=false)
Set a 2D view for a viewport.
int MaxVertexUniformVectors
static bool get_backface_culling()
Determine whether backface culling is enabled.
const Light * get_Light(const int &number) const
Get pointer to Light, or 0 if no such Light.
static bool get_lighting()
Determine whether dynamic lighting is enabled.
COMPILER_EXPORT ShHandle ShConstructCompiler(ShShaderType type, ShShaderSpec spec, ShShaderOutput output, const ShBuiltInResources *resources)
virtual void set_Light(const int &number, const Light &light)=0
Set a particular Light.
int MaxFragmentUniformVectors
LPDIRECT3DPIXELSHADER9 get_pixel_shader() const
static void init(struct bs2b *bs2b)
#define D3DCREATE_MIXED_VERTEXPROCESSING
int MaxVertexTextureImageUnits
An Abstraction of a Material.
void set_ambient_lighting(const Color &color)
Set ambient lighting on/off.
void set_Fog(const Fog &fog)
Set Fog.
static D3DXCreateMatrixStack_fcn D3DXCreateMatrixStack()
void set_ztest(const bool &enabled)
Enable or disable testing of the Z-Buffer.
const Shader_DX9 * get_fragment_shader() const
bool begin_render()
Must be called before all rendering functions; Returns true if rendering can proceed.
Shader * create_Fragment_Shader(const String &filename)
Create a Fragment_Shader from a file.
bool is_alpha_test_enabled() const
Determine whether alpha testing is enabled.
void set(const GLenum &number, Video_GL_Fixed &screen) const
TEST get_alpha_test_function() const
Determine which alpha test is in use.
virtual void set_zwrite(const bool &enabled)=0
Enable or disable writing to the Z-Buffer.
String compile_glsles_shader(const String &filename, const ShHandle &compiler)
Compile an OpenGL ES shader to GLSL/HLSL.
typedef UINT(WINAPI *PFNWGLGETCONTEXTGPUIDAMDPROC)(HGLRC hglrc)
bool begin_prerender()
Must be called before begin_render.
void set_program(Program &program)
Enable a program.
BOOL EnableAutoDepthStencil
#define D3DCREATE_SOFTWARE_VERTEXPROCESSING
ShHandle m_vertex_compiler
bool is_3d() const
Determine whether currently rendering in 3D.
virtual bool is_3d() const =0
Tell the rendering system if we're using 3D coordinates.
void set(Video_GL_Fixed &screen) const
Point2f get_pixel_offset() const
Get the pixel offset in the 2d view.
void set_zwrite(const bool &enabled)
Enable or disable writing to the Z-Buffer.
void clear_render_target(const Color &color=Color(0.0f, 0.0f, 0.0f, 0.0f))
Clear the viewport.
#define D3DADAPTER_DEFAULT
const Point2i & get_render_target_size() const
Get the dimensions of the render target.
void set_3d_view(const Camera &camera, const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get_Video().get_render_target_size()))
Set a 3D view for a viewport.
unsigned char r_ub() const
Get the red channel [0x00, 0xFF].
#define SDL_VERSION(x)
Macro to determine SDL version program was compiled against.
bool is_zwrite_enabled() const
Determine whether writing to Z-Buffer is enabled.
void set_Material(const Material &material)
Set a Material.
void transform_scene(const Matrix4f &transformation)
Transform the scene.
void scale_scene(const Vector3f &factor)
Scale the scene.
An Abstraction of a Texture.
A Featureful 3-Space Vector Class.
void end_render()
Must be called after all rendering functions.
static void preinit_multisampling(const int &multisampling=0)
Set a multisampling value.
void push_world_stack()
Push a model view matrix onto the stack.
virtual void set_ztest(const bool &enabled)=0
Enable or disable testing of the Z-Buffer.
UINT PresentationInterval
UINT FullScreen_RefreshRateInHz
void rotate_scene(const Vector3f &about, const float &radians)
Rotate the scene.
void set_alpha_test(const bool &enabled, const TEST &test, const float &value)
Set the alpha test.
unsigned char b_ub() const
Get the blue channel [0x00, 0xFF].
virtual void set_ambient_lighting(const Color &color)=0
Set ambient lighting on/off.
void set_render_target(Texture &texture)
Set a render target.
void unset(Video_GL_Fixed &screen) const
void unapply_Texture()
Unapply a texture.
int get_maximum_anisotropy() const
Get the current level of anisotrophy.
#define D3DERR_DRIVERINTERNALERROR
void unset_render_target()
Unset a render target.
static const Color & get_ambient_lighting()
Get the current ambient lighting Color.
virtual void set_3d_view(const Camera &camera, const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get().get_render_target_size()))=0
Set a 3D view for a viewport.
COMPILER_EXPORT void ShDestruct(ShHandle handle)
COMPILER_EXPORT void ShInitBuiltInResources(ShBuiltInResources *resources)
#define D3DCOLOR_XRGB(r, g, b)
void set_2d()
Set the default 2D view filling the entire display area.
void set_fvf_3d(const bool &on)
static int get_multisampling()
Get the current level of multisampling.
Shader * create_Vertex_Shader(const String &filename)
Create a Vertex_Shader from a file.
virtual void set_Color(const Color &color)=0
Set the current color.
void set_backface_culling(const bool &on)
Set backface culling on/off.
#define D3DPRESENT_INTERVAL_ONE
void set_lighting(const bool &on=true)
Set lighting on/off.
virtual void set_2d_view(const std::pair< Point2f, Point2f > &camera2d, const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get().get_render_target_size()), const bool &fix_aspect_ratio=false)=0
Set a 2D view for a viewport.
void unset_Material(const Material &material)
Unset a Material.
GLbitfield GLuint program
union SDL_SysWMinfo::@78 info
A Vertex_Buffer that accepts Triangle and Quadrilaterals.
const Shader_DX9 * get_vertex_shader() const
void set_Color(const Color &)
Set the current color.
void set_clear_Color(const Color &color)
Set the blank background color.
static Matrix4f Translate(const Vector3f &translation_factor)
void set(Video_GL_Fixed &screen) const
virtual void set_Fog(const Fog &fog)=0
Set Fog.
Textures & get_Textures()
Get access to the singleton.
Window & get_Window()
Get access to the singleton.
virtual void apply_Texture() const =0
Apply a Texture to upcoming polygons.
void clear_depth_buffer()
Can reset the depth buffer at any time if necessary.
const D3DPRESENT_PARAMETERS & get_d3d_parameters()
See DirectX Documentation for details.
void apply_Texture(const String &name)
Apply a texture for upcoming polygons (Called by Video::apply_Texture)
virtual void set_projection_matrix(const Matrix4f &projection)=0
Set the projection Matrix4f.
int MaxCombinedTextureImageUnits
void set_Light(const int &number, const Light &light)
Set a particular Light.
virtual void set_viewport(const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get().get_render_target_size()))=0
Set the viewport.
GLenum GLenum GLuint texture
void pop_world_stack()
Pop a model view matrix off the stack.
#define D3DDEVCAPS_HWTRANSFORMANDLIGHT
void unlose_resources()
If resources have been lost, then reload them.
EGLSurface EGLint void ** value
A base class for Vertex_Shader and Fragment_Shader.
DWORD VertexProcessingCaps
#define D3DCREATE_HARDWARE_VERTEXPROCESSING
LPD3DXMATRIXSTACK & get_matrix_stack()
Get access to a matrix stack stored by this class.
Texture * create_Texture(const Image &image)
Function for creating a Texture from an Image.
#define D3DPRESENT_RATE_DEFAULT
HRESULT(WINAPI * D3DXCreateTexture_fcn)(LPDIRECT3DDEVICE9 pDevice, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, LPDIRECT3DTEXTURE9 *ppTexture)
D3DMULTISAMPLE_TYPE MultiSampleType
bool has_vertex_buffers() const
Determine whether Vertex_Buffers are supported.
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
const D3DCAPS9 & get_d3d_capabilities()
See DirectX Documentation for details.
void * this_pointer_is_dead_beef
unsigned char g_ub() const
Get the green channel [0x00, 0xFF].
ID3DXRenderToSurface * render_to_surface() const
static Matrix4f Identity()
void set_viewport(const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get_Video().get_render_target_size()))
Set the viewport.
HRESULT(WINAPI * D3DXCreateMatrixStack_fcn)(DWORD Flags, LPD3DXMATRIXSTACK *ppStack)
static bool get_vertical_sync()
Determine whether vertical sync is enabled.
virtual void set_backface_culling(const bool &on)=0
Set backface culling on/off.
bool is_ztest_enabled() const
Determine whether testing the Z-Buffer is enabled.
void unset_Light(const int &number)
Unset a particular Light.
#define D3DERR_DEVICENOTRESET
Program * create_Program()
Create a Program from a file.
static const Point2i & get_size()
struct _D3DPRESENT_PARAMETERS_ D3DPRESENT_PARAMETERS
Fonts & get_Fonts()
Get access to the singleton.
void unset_Fog()
Unset Fog.
const Color & get_Color() const
Get the current color.
HRESULT(WINAPI * D3DXCreateRenderToSurface_fcn)(LPDIRECT3DDEVICE9 pDevice, UINT Width, UINT Height, D3DFORMAT Format, BOOL DepthStencil, D3DFORMAT DepthStencilFormat, LPD3DXRENDERTOSURFACE *ppRenderToSurface)
virtual void pre_render() const
const Point2i & get_size() const
Get the resolution of the Texture on the GPU.
A Featureful 4-Space Matrix Class.
void set_vertical_sync(const bool &on)
Set vertical_sync on/off.
HRESULT(WINAPI * D3DXFilterTexture_fcn)(LPDIRECT3DBASETEXTURE9 pBaseTexture, CONST PALETTEENTRY *pPalette, UINT SrcLevel, DWORD Filter)
#define D3DPRESENT_INTERVAL_IMMEDIATE
void set_fvf(const bool &is_3d)
struct _D3DCAPS9 D3DCAPS9
virtual void set_alpha_test(const bool &enabled, const TEST &test=ZENI_ALWAYS, const float &value=0.0f)=0
Set the alpha test.
virtual void unset_Light(const int &number)=0
Unset a particular Light.
int OES_EGL_image_external
void render(const Renderable &renderable)
Render a Renderable.
Uint32 get_argb() const
Get a Uint32 representation of 0xAARRGGBB.
#define D3DERR_DEVICEREMOVED
void translate_scene(const Vector3f &direction)
Translate the scene.
struct IDirect3DSurface9 * LPDIRECT3DSURFACE9
ShHandle m_fragment_compiler
A 2D Point represented with floats.
const Fog * get_Fog() const
Get pointer to current Fog, or 0 if no Fog.
void apply_Texture(const String &name)
Apply a texture by name.
static Matrix4f Scale(const Vector3f &scaling_factor)
int OES_standard_derivatives
float get_alpha_test_value() const
Determine what value the alpha test is comparing against.
void set_view_matrix(const Matrix4f &view)
Set the view Matrix4f.
A 2D Point represented with integers.