34 #if defined(_DEBUG) && defined(_WINDOWS)
35 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
43 template class Singleton<Video>;
45 Video * Video::create() {
50 const String appdata_path = fo.get_appdata_path();
52 const String user_normal = appdata_path +
"config/zenilib.xml";
53 const String user_backup = user_normal +
".bak";
54 const String local_normal =
"config/zenilib.xml";
55 const String local_backup = local_normal +
".bak";
57 static bool last_resort_taken =
false;
63 switch(Video::g_video_mode) {
65 #ifndef DISABLE_GL_SHADER
67 video =
new Video_GL_Shader();
72 video =
new Video_DX9();
75 #ifndef DISABLE_GL_FIXED
77 video =
new Video_GL_Fixed();
81 throw Video_Init_Failure();
85 catch(Video_Init_Failure &) {
89 if(fo.copy_file(user_backup, user_normal) && fo.delete_file(user_backup)) {
90 std::cerr <<
'\'' << user_normal <<
"' backup restored due to initialization failure.\n";
94 else if(fo.copy_file(local_backup, local_normal) && fo.delete_file(local_backup)) {
95 std::cerr <<
'\'' << local_normal <<
"' backup restored due to initialization failure.\n";
99 else if(!last_resort_taken) {
102 last_resort_taken =
true;
111 last_resort_taken =
false;
116 Singleton<Video>::Uninit Video::g_uninit;
117 Singleton<Video>::Reinit Video::g_reinit;
121 m_color(1.0
f, 1.0
f, 1.0
f, 1.0
f),
126 m_alpha_function(
Video::ZENI_ALWAYS),
130 static bool once =
false;
153 void Video::set_2d_view(
const std::pair<Point2f, Point2f> &camera2d,
const std::pair<Point2i, Point2i> &viewport,
const bool &fix_aspect_ratio) {
163 offset.
x *= (camera2d.second.x - camera2d.first.x) / (vp.second.x - vp.first.x);
164 offset.
y *= (camera2d.second.y - camera2d.first.y) / (vp.second.y - vp.first.y);
167 camera2d.second.x + offset.
x,
168 camera2d.second.y + offset.
y,
169 camera2d.first.y + offset.
y,
187 g_backface_culling = on;
191 g_vertical_sync = on;
204 const float &
value) {
206 m_alpha_function = test;
207 m_alpha_value =
value;
215 g_clear_color =
color;
223 g_ambient_lighting =
color;
231 m_projection = projection;
235 m_viewport = viewport;
242 std::pair<Point2i, Point2i>
Video::calculate_viewport(
const std::pair<Point2f, Point2f> &camera2d,
const std::pair<Point2i, Point2i> &viewport,
const bool &fix_aspect_ratio) {
244 return calculate_viewport((camera2d.second.x - camera2d.first.x) / (camera2d.second.y - camera2d.first.y), viewport);
250 std::pair<Point2i, Point2i> viewport = viewport_;
252 int width = viewport.second.x - viewport.first.x;
253 int height = viewport.second.y - viewport.first.y;
254 const float given_ratio = float(width) /
height;
256 if(given_ratio > aspect_ratio) {
257 const int new_width =
int(width * aspect_ratio / given_ratio);
258 const int cut_side = (width - new_width) / 2;
260 viewport.first.x += cut_side;
261 viewport.second.x -= cut_side;
263 else if(aspect_ratio > given_ratio) {
264 const int new_height =
int(height * given_ratio / aspect_ratio);
265 const int cut_side = (height - new_height) / 2;
267 viewport.first.y += cut_side;
268 viewport.second.y -= cut_side;
277 if(it != g_lights.end())
284 g_lights[number] =
light;
288 g_lights.erase(number);
292 return g_fog_enabled ? &g_fog : 0;
297 g_fog_enabled =
false;
301 g_fog_enabled =
false;
307 for(std::ifstream fin(filename.
c_str()); fin.get(c); )
310 std::cerr <<
"Compiling: " << filename << std::endl;
311 const char *
const in_ptr = in.
c_str();
317 std::cerr <<
"Result:" << std::endl << out;
329 g_multisampling = multisampling;
333 g_vertical_sync = vertical_sync;
352 #ifndef DISABLE_GL_SHADER
358 #ifndef DISABLE_GL_FIXED
396 const String user_normal = appdata_path +
"config/zenilib.xml";
397 const String user_backup = user_normal +
".bak";
398 const String local_normal =
"config/zenilib.xml";
399 const String local_backup = local_normal +
".bak";
405 file.
load(local_normal);
416 #ifndef DISABLE_GL_SHADER
422 #ifndef DISABLE_GL_FIXED
428 video[
"Multisampling"].
set_int(g_multisampling);
431 video[
"Vertical_Sync"].
set_bool(g_vertical_sync);
435 bool user_save =
false;
442 user_save = file.
try_save(user_normal);
451 fo.
copy_file(local_normal, local_backup);
462 const String user_normal = appdata_path +
"config/zenilib.xml";
463 const String user_backup = user_normal +
".bak";
464 const String local_normal =
"config/zenilib.xml";
465 const String local_backup = local_normal +
".bak";
467 bool reverted =
false;
478 std::cerr <<
'\'' << user_normal <<
"' backup restored due to initialization failure.\n";
489 std::cerr <<
'\'' << local_normal <<
"' backup restored due to initialization failure.\n";
500 std::cerr <<
'\'' << local_normal <<
"' backup restored due to initialization failure.\n";
516 #if !defined(DISABLE_GL_FIXED)
518 #elif !defined(DISABLE_DX9)
520 #elif !defined(DISABLE_GL_SHADER)
526 g_vertical_sync =
false;
530 #if !defined(DISABLE_GL) && !defined(REQUIRE_GL_ES)
537 bool Video::g_backface_culling =
false;
538 bool Video::g_lighting =
false;
542 bool Video::g_fog_enabled =
false;
544 bool Video::g_normal_interp =
false;
545 bool Video::g_vertical_sync =
false;
546 int Video::g_multisampling = 0;
547 bool Video::g_zwrite =
true;
548 bool Video::g_ztest =
true;
An Abstraction of a Light.
virtual void set_lighting(const bool &on=true)=0
Set lighting on/off.
String to_string() const
Get the contained string.
static const int & get_width()
Get the size of the window.
virtual void set_vertical_sync(const bool &on)=0
Set vertical_sync on/off.
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
COMPILER_EXPORT int ShCompile(const ShHandle handle, const char *const shaderStrings[], size_t numStrings, int compileOptions)
The Video Rendering Singleton.
static void change_resolution(const Point2i &resolution)
virtual void set_view_matrix(const Matrix4f &view)=0
Set the view Matrix4f.
virtual void set_clear_Color(const Color &color)=0
Set the blank background color.
const std::pair< Point2i, Point2i > & get_viewport() const
Get the viewport.
virtual void unset_Fog()=0
Unset Fog.
static bool revert()
Restore previously saved options; Returns true if successful.
GLenum GLsizei const GLuint GLboolean enabled
const Light * get_Light(const int &number) const
Get pointer to Light, or 0 if no such Light.
bool try_load(const String &filename)
virtual void set_Light(const int &number, const Light &light)=0
Set a particular Light.
static int get_anisotropic_filtering()
Check the current level of anisotropy.
static bool copy_file(const String &from, const String &to)
Copy a file from one filepath to another.
EGLSurface EGLint EGLint EGLint EGLint height
const char * c_str() const
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.
static void remove_post_reinit(Event::Handler *const &handler)
void lend_post_reinit(Event::Handler *const &handler)
void lend_pre_uninit(Event::Handler *const &handler)
static void completely_destroy()
void set_string(const String &s)
Set the contained string.
Matrix4f get_projection_matrix(const std::pair< Point2i, Point2i > &viewport) const
Equivalent to gluPerspective + tunnel_vision_factor.
static void set_failsafe_defaults()
Set failsafe default options.
A Featureful 3-Space Vector Class.
static void preinit_multisampling(const int &multisampling=0)
Set a multisampling value.
static void remove_pre_uninit(Event::Handler *const &handler)
virtual void set_ztest(const bool &enabled)=0
Enable or disable testing of the Z-Buffer.
virtual void set_ambient_lighting(const Color &color)=0
Set ambient lighting on/off.
EGLSurface EGLint EGLint EGLint width
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.
static bool create_directory(const String &directory_path)
Create a directory if it doesn't already exist; It is not considered an error if it already exists...
static const bool & is_full_screen()
Determine whether the window is windowed or full screen.
void set_bool(const bool &b)
Set the contained string as a boolean.
static Matrix4f Orthographic(const float &left, const float &right, const float &bottom, const float &top, const float &near, const float &far)
Similar to gluOrtho.
COMPILER_EXPORT int ShFinalize()
A class to open an XML file and manage the root node.
virtual void set_Color(const Color &color)=0
Set the current color.
void load(const String &filename)
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.
static void save(const bool &backup=true)
Save options.
static void preinit_vertical_sync(const bool &vertical_sync=false)
Set vertical_sync to true.
COMPILER_EXPORT void ShGetObjectCode(const ShHandle handle, char *objCode)
virtual void set_Fog(const Fog &fog)=0
Set Fog.
void alert_window_resized(const Point2i &resolution)
Tell Window that it has been resized.
Window & get_Window()
Get access to the singleton.
virtual void set_projection_matrix(const Matrix4f &projection)=0
Set the projection Matrix4f.
A node in an XML tree, possibly containing useful data.
virtual void set_viewport(const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get().get_render_target_size()))=0
Set the viewport.
String get_appdata_path()
Get the path that should be used for user-modifiable storage.
std::pair< Point2i, Point2i > calculate_viewport(const std::pair< Point2f, Point2f > &camera2d, const std::pair< Point2i, Point2i > &viewport, const bool &fix_aspect_ratio)
Calculate the viewport.
static void preinit_from_file(const String &filename)
Set rendering options from a file.
EGLSurface EGLint void ** value
COMPILER_EXPORT void ShGetInfo(const ShHandle handle, ShShaderInfo pname, size_t *params)
static const int & get_height()
Get the height of the window.
GLsizei const GLuint * textures
static Matrix4f Identity()
static void set_texturing_mode(const int &anisotropic_filtering_, const bool &bilinear_filtering_, const bool &mipmapping_)
Check to see if Textures is set to use lazy loading if possible.
const GLubyte * gluErrorString(GLenum error)
virtual void set_backface_culling(const bool &on)=0
Set backface culling on/off.
COMPILER_EXPORT int ShInitialize()
Video & get_Video()
Get access to the singleton.
void set_int(const int &i)
Set the contained string as an integer.
static void preinit_video_mode(const VIDEO_MODE &vm=ZENI_VIDEO_ANY)
Set which rendering engine to use.
static bool get_mipmapping()
Check if mipmapping is in use.
A Featureful 4-Space Matrix Class.
Matrix4f get_view_matrix() const
Equivalent to gluLookAt + tunnel_vision_factor.
virtual Point2f get_pixel_offset() const =0
Get the pixel offset in the 2d view.
static void set_failsafe_defaults()
Set failsafe default options.
virtual void set_alpha_test(const bool &enabled, const TEST &test=ZENI_ALWAYS, const float &value=0.0f)=0
Set the alpha test.
File_Ops & get_File_Ops()
Get access to the singleton.
virtual void unset_Light(const int &number)=0
Unset a particular Light.
static bool get_bilinear_filtering()
Check if bilinear filtering is in use.
A 2D Point represented with floats.
const Fog * get_Fog() const
Get pointer to current Fog, or 0 if no Fog.
static void preinit_from_xml(const XML_Element_c &video)
Set rendering options from an XML data structure.
The Window Management Singleton.
static bool delete_file(const String &file_path)
Delete a file.
static void print_errors()
Print any errors that may have occurred.
A 2D Point represented with integers.