video.h

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00029 #ifndef __VIDEO_HEADER__
00030 #define __VIDEO_HEADER__
00031 
00032 #include "defs.h"
00033 #include "utils.h"
00034 
00035 // OpenGL includes
00036 #ifdef __APPLE__
00037   #include <OpenGL/gl.h>
00038   #include <OpenGL/glu.h>
00039 #else
00040   #include <GL/gl.h>
00041   #include <GL/glu.h>
00042 #endif
00043 
00044 // libpng and libjpeg image loader includes
00045 #include <png.h>
00046 extern "C" {
00047   #include <jpeglib.h>
00048 }
00049 
00050 // SDL_ttf includes
00051 #ifdef __APPLE__
00052   #include <SDL_ttf/SDL_ttf.h>
00053 #else
00054   #include <SDL/SDL_ttf.h>
00055 #endif
00056 
00057 // Various other headers of the video engine
00058 #include "context.h"
00059 #include "color.h"
00060 #include "coord_sys.h"
00061 #include "fade.h"
00062 #include "image.h"
00063 #include "interpolator.h"
00064 #include "shake.h"
00065 #include "screen_rect.h"
00066 #include "tex_mgmt.h"
00067 #include "text.h"
00068 #include "particle_manager.h"
00069 #include "particle_effect.h"
00070 #include "gui.h"
00071 #include "menu_window.h"
00072 #include "option.h"
00073 #include "textbox.h"
00074 
00076 namespace hoa_video {
00077 
00079 extern GameVideo* VideoManager;
00080 
00082 extern bool VIDEO_DEBUG;
00083 
00089 float RandomFloat(float a, float b);
00090 
00091 
00097 void RotatePoint(float &x, float &y, float angle);
00098 
00099 
00107 float Lerp(float alpha, float initial, float final);
00108 
00109 
00110 
00111 
00112 
00114 const int32 VIDEO_ANIMATION_FRAME_PERIOD = 10;
00115 
00117 enum VIDEO_DRAW_FLAGS {
00118   VIDEO_DRAW_FLAGS_INVALID = -1,
00119 
00121 
00122   VIDEO_X_LEFT = 1, VIDEO_X_CENTER = 2, VIDEO_X_RIGHT = 3,
00124 
00126 
00127   VIDEO_Y_TOP = 4, VIDEO_Y_CENTER = 5, VIDEO_Y_BOTTOM = 6,
00129 
00131 
00132   VIDEO_X_FLIP = 7, VIDEO_X_NOFLIP = 8,
00134 
00136 
00137   VIDEO_Y_FLIP = 9, VIDEO_Y_NOFLIP = 10,
00139 
00141 
00142   VIDEO_NO_BLEND = 11, VIDEO_BLEND = 12, VIDEO_BLEND_ADD = 13,
00144 
00145   VIDEO_DRAW_FLAGS_TOTAL = 14
00146 };
00147 
00148 
00152 enum VIDEO_TARGET {
00153   VIDEO_TARGET_INVALID = -1,
00154 
00156   VIDEO_TARGET_SDL_WINDOW = 0,
00157 
00159   VIDEO_TARGET_QT_WIDGET  = 1,
00160 
00161   VIDEO_TARGET_TOTAL = 2
00162 };
00163 
00164 
00166 enum VIDEO_STENCIL_OP {
00167   VIDEO_STENCIL_OP_INVALID = -1,
00168 
00170   VIDEO_STENCIL_OP_ONE      = 0,
00171 
00173   VIDEO_STENCIL_OP_ZERO     = 1,
00174 
00176   VIDEO_STENCIL_OP_INCREASE = 2,
00177 
00179   VIDEO_STENCIL_OP_DECREASE = 3,
00180 
00181   VIDEO_STENCIL_OP_TOTAL = 4
00182 };
00183 
00185 enum {
00186   VIDEO_STANDARD_RES_WIDTH  = 1024,
00187   VIDEO_STANDARD_RES_HEIGHT = 768
00188 };
00189 
00197 class GameVideo : public hoa_utils::Singleton<GameVideo> {
00198   friend class hoa_utils::Singleton<GameVideo>;
00199   friend class TextBox;
00200   friend class OptionBox;
00201   friend class MenuWindow;
00202   friend class private_video::GUIElement;
00203   friend class private_video::GUISupervisor;
00204   friend class private_video::FixedTexMemMgr;
00205   friend class private_video::VariableTexMemMgr;
00206   friend class private_video::TexSheet;
00207   friend class private_video::ParticleSystem;
00208   friend class StillImage;
00209   friend class RenderedString;
00210 
00211 public:
00212   ~GameVideo();
00213 
00214   bool SingletonInitialize();
00215 
00216 
00217   //-- General --------------------------------------------------------------
00218 
00225   bool Clear();
00226 
00231   bool Clear(const Color &c);
00232 
00237   bool Display(int32 frame_time);
00238 
00239 
00250   bool SetTarget(VIDEO_TARGET target);
00251 
00252   //-- Video settings -------------------------------------------------------
00253 
00256 
00265   bool SetResolution(int32 width, int32 height);
00266 
00270   int32 GetWidth() const { return _width; }
00271 
00275   int32 GetHeight() const { return _height; }
00276 
00280   const CoordSys& GetCoordSys() const { return _coord_sys; }
00281 
00285   bool IsFullscreen();
00286 
00292   void GetPixelSize(double &x, double &y)
00293   {
00294     x = (_coord_sys.GetRight() - _coord_sys.GetLeft()) / _width;
00295     y = (_coord_sys.GetTop() - _coord_sys.GetBottom()) / _height;
00296   }
00297 
00304   bool SetFullscreen(bool fullscreen);
00305 
00310   bool ToggleFullscreen();
00311 
00318   bool ApplySettings();
00319 
00320   //-- Coordinate system / viewport  ------------------------------------------
00321 
00331   void SetViewport(float left, float right, float bottom, float top);
00332 
00339   void SetCoordSys(float left, float right, float bottom, float top);
00340 
00344   void SetCoordSys(const CoordSys &coordinate_system);
00345 
00353   void EnableScissoring(bool enable);
00354 
00358   bool IsScissoringEnabled()
00359     { return _scissor_enabled; }
00360 
00369   void SetScissorRect(float left, float right, float bottom, float top);
00370 
00378   void SetScissorRect(const ScreenRect &rect);
00379 
00383   ScreenRect GetScissorRect()
00384     { return _scissor_rect; }
00385 
00393   ScreenRect CalculateScreenRect(float left, float right, float bottom, float top);
00394 
00395   //-- Transformations ------------------------------------------------------
00396 
00405   void PushState();
00406 
00410   void PopState ();
00411 
00412 
00416   void PushMatrix();
00417 
00420   void PopMatrix();
00421 
00426   void Move(float x, float y);
00427 
00432   void MoveRelative(float dx, float dy);
00433 
00438   void GetDrawPosition(float &x, float &y);
00439 
00445   void Rotate (float angle);
00446 
00458   void Scale(float x, float y);
00459 
00463   void SetTransform(float m[16]);
00464 
00465   //-- Text -----------------------------------------------------------------
00466 
00473   bool LoadFont(const std::string &TTF_filename, const std::string &name, uint32 size);
00474 
00479   bool IsValidFont(const std::string &name)
00480     { return (_font_map.find(name) != _font_map.end()); }
00481 
00486   FontProperties* GetFontProperties(const std::string &font_name);
00487 
00495   int32 CalculateTextWidth(const std::string &font_name, const hoa_utils::ustring &text);
00496 
00497 
00505   int32 CalculateTextWidth(const std::string &font_name, const std::string  &text);
00506 
00511   bool SetFont(const std::string &name);
00512 
00516   void EnableTextShadow(bool enable);
00517 
00522   void SetTextColor(const Color &color)
00523     { _current_text_color = color; }
00524 
00534   bool SetFontShadowXOffset(const std::string &font_name, int32 x);
00535 
00545   bool SetFontShadowYOffset(const std::string &font_name, int32 y);
00546 
00553   bool SetFontShadowStyle(const std::string &font_name, TEXT_SHADOW_STYLE style);
00554 
00558   std::string GetFont      () const;
00559 
00564   Color       GetTextColor () const;
00565 
00572   bool DrawText(const std::string &text);
00573 
00581   bool DrawText(const hoa_utils::ustring &uText);
00582 
00583   //-- Particle effects -----------------------------------------------------------
00584 
00596   ParticleEffectID AddParticleEffect(const std::string &particle_effect_filename, float x, float y, bool reload=false);
00597 
00601   bool DrawParticleEffects();
00602 
00608   void StopAllParticleEffects(bool kill_immediate = false);
00609 
00613   ParticleEffect *GetParticleEffect(ParticleEffectID id);
00614 
00618   int32 GetNumParticles();
00619 
00620   //-- Images / Animation ---------------------------------------------------------
00621 
00629   bool GetImageInfo (const std::string& file_name, uint32 &rows, uint32& cols, uint32& bpp);
00630 
00639   bool LoadImage(ImageDescriptor &id);
00640 
00650   bool LoadImageGrayScale(ImageDescriptor &id);
00651 
00661   bool LoadMultiImageFromNumberElements(std::vector<StillImage> &images, const std::string &filename, const uint32 rows, const uint32 cols);
00662 
00672   bool LoadMultiImageFromElementsSize(std::vector<StillImage> &images, const std::string &filename, const uint32 width, const uint32 height);
00673 
00683   bool LoadAnimatedImageFromNumberElements(AnimatedImage &image, const std::string &file_name, const uint32 rows, const uint32 cols);
00684 
00694   bool LoadAnimatedImageFromElementsSize(AnimatedImage &image, const std::string &file_name, const uint32 rows, const uint32 cols);
00695 
00697   /*  This function stores a vector of images as a single image. This is useful for creating multiimage 
00698    *  images. The image can be stored in JPEG or PNG, which is decided by the filename.
00699    *  \param image Vector of images to save
00700    *  \param file_name Name of the file.
00701    *  \param rows Number of rows of sub-images in the MultiImage.
00702    *  \param cols Number of columns of sub-images in the MultiImage.
00703    *  \return success/failure
00704    */
00705   bool SaveImage (const std::string &file_name, const std::vector<StillImage*> &image, const uint32 rows, const uint32 columns) const;
00706 
00708   /*  The image can be stored in JPEG or PNG, which is decided by the filename.
00709    *  \param image StillImage to store.
00710    *  \param file_name Name of the file.
00711    *  \return success/failure
00712    */
00713   bool SaveImage (const std::string &file_name, const StillImage &image) const;
00714 
00716   /*  The image can be stored in JPEG or PNG, which is decided by the filename.
00717    *  It will be stored as a multiimage, with each frame aligned in one row and as many columns as frames.
00718    *  \param image AnimatedImage to store.
00719    *  \param file_name Name of the file.
00720    *  \return success/failure
00721    */
00722   bool SaveImage (const std::string &file_name, const AnimatedImage &image) const;
00723 
00730   bool CaptureScreen(StillImage &id);
00731 
00737   bool DeleteImage(ImageDescriptor &id);
00738 
00743   bool UnloadTextures();
00744 
00749   bool ReloadTextures();
00750 
00758   void SetDrawFlags(int32 first_flag, ...);
00759 
00765   bool DrawImage(const ImageDescriptor &id);
00766 
00773   bool DrawImage(const ImageDescriptor &id, const Color &color);
00774 
00782   StillImage TilesToObject(std::vector<StillImage> &tiles, std::vector< std::vector<uint32> > indices);
00783 
00789   int32 GetFrameChange() { return _current_frame_diff; }
00790 
00793   void DEBUG_NextTexSheet();
00794   void DEBUG_PrevTexSheet();
00795 
00796   //-- Menus -----------------------------------------------------------------
00797 
00820 
00821   bool LoadMenuSkin(std::string skin_name, std::string border_image, std::string background_image, bool make_default = false);
00822 
00824   bool LoadMenuSkin(std::string skin_name, std::string border_image, Color background_color, bool make_default = false);
00825 
00827   bool LoadMenuSkin(std::string skin_name, std::string border_image, Color top_left, Color top_right,
00828     Color bottom_left, Color bottom_right, bool make_default = false);
00829 
00831   bool LoadMenuSkin(std::string skin_name, std::string border_image, std::string background_image,
00832     Color background_color, bool make_default = false);
00833 
00835   bool LoadMenuSkin(std::string skin_name, std::string border_image, std::string background_image,
00836     Color top_left, Color top_right, Color bottom_left, Color bottom_right, bool make_default = false);
00838 
00840   bool IsMenuSkinAvailable(std::string& skin_name) const
00841     { if (private_video::GUIManager->GetMenuSkin(skin_name) == NULL) return false; else return true; }
00842 
00850   void SetDefaultMenuSkin(std::string& skin_name)
00851     { private_video::GUIManager->SetDefaultMenuSkin(skin_name); }
00852 
00861   void DeleteMenuSkin(std::string& skin_name)
00862     { private_video::GUIManager->DeleteMenuSkin(skin_name); }
00863 
00864   //-- Lighting and fog -----------------------------------------------------
00865 
00870   bool EnableSceneLighting(const Color &color);
00871 
00874   void DisableSceneLighting();
00875 
00879   Color &GetSceneLightingColor();
00880 
00887   bool EnableFog(const Color &color, float intensity);
00888 
00891   void DisableFog();
00892 
00901   bool DrawHalo(const StillImage &id, float x, float y, const Color &color = Color(1.0f, 1.0f, 1.0f, 1.0f));
00902 
00911   bool DrawLight(const StillImage &id, float x, float y, const Color &color = Color(1.0f, 1.0f, 1.0f, 1.0f));
00912 
00916   bool EnablePointLights();
00917 
00921   void DisablePointLights();
00922 
00927   //bool AccumulateLights();
00928 
00934   bool ApplyLightingOverlay();
00935 
00936   //-- Overlays / lightning -------------------------------------------------------
00937 
00942   bool DrawFullscreenOverlay(const Color &color);
00943 
00949   bool MakeLightning(const std::string &lit_file);
00950 
00956   bool DrawLightning();
00957 
00958   //-- Fading ---------------------------------------------------------------
00959 
00965   void FadeScreen(const Color &color, float fade_time);
00966 
00970   bool IsFading();
00971 
00972   //-- Screen shaking -------------------------------------------------------
00973 
00983   bool ShakeScreen(float force, float falloff_time, ShakeFalloff falloff = VIDEO_FALLOFF_NONE);
00984 
00988   bool StopShaking();
00989 
00993   bool IsShaking();
00994 
00995   //-- Miscellaneous --------------------------------------------------------
00996 
01001   void SetGamma(float value);
01002 
01006   float GetGamma();
01007 
01011   void DrawFPS(uint32 frame_time);
01012 
01015   void ToggleFPS();
01016 
01031   void DrawGrid(float x, float y, float x_step, float y_step, const Color &c);
01032 
01041   void DrawRectangle(const float width, const float height, const Color &color);
01042 
01047   bool MakeScreenshot();
01048 
01053   bool ToggleAdvancedDisplay();
01054 
01058   bool SetDefaultCursor(const std::string &cursor_image_filename);
01059 
01063   StillImage *GetDefaultCursor();
01064 
01068   bool Draw(const RenderedString &string);
01069 
01074   bool Draw(const RenderedLine &line, int32 tex_index);
01075 
01079   RenderedString *RenderText(const hoa_utils::ustring &txt);
01080 
01081 private:
01082   GameVideo();
01083 
01084   //-- Private variables ----------------------------------------------------
01085 
01087   private_video::ParticleManager _particle_manager;
01088 
01090   VIDEO_TARGET _target;
01091 
01092   // draw flags
01093 
01095   int8 _blend;
01096 
01098   int8 _x_align;
01099 
01101   int8 _y_align;
01102 
01104   int8 _x_flip;
01105 
01107   int8 _y_flip;
01108 
01110   char _next_temp_file[9];
01111 
01113   CoordSys    _coord_sys;
01114 
01116   ScreenRect _viewport;
01117 
01119   ScreenRect _scissor_rect;
01120 
01122   bool _scissor_enabled;
01123 
01125   private_video::ScreenFader _fader;
01126 
01128   bool   _advanced_display;
01129 
01131   bool   _fps_display;
01132 
01134   int32  _current_debug_TexSheet;
01135 
01137   int32  _num_tex_switches;
01138 
01140   int32  _num_draw_calls;
01141 
01143   bool   _uses_lights;
01144 
01146   GLuint _light_overlay;
01147 
01149   float  _x_shake;
01150   
01152   float _y_shake;
01153 
01155   float _gamma_value;
01156 
01158   std::list<private_video::ShakeForce> _shake_forces;
01159 
01161   bool _fullscreen;
01162 
01164   int32  _width;
01165 
01167   int32  _height;
01168 
01169   // changing the video settings does not actually do anything until
01170   // you call ApplySettings(). Up til that point, store them in temp
01171   // variables so if the new settings are invalid, we can roll back.
01172 
01174   bool   _temp_fullscreen;
01175 
01177   int32  _temp_width;
01178 
01180   int32  _temp_height;
01181 
01183   GLuint _last_tex_ID;
01184 
01186   std::string _current_font;
01187 
01189   Color       _current_text_color;
01190 
01192   StillImage _default_menu_cursor;
01193 
01195   StillImage _rectangle_image;
01196 
01198   bool _text_shadow;
01199 
01201   Color _fog_color;
01202 
01204   float _fog_intensity;
01205 
01207   Color _light_color;
01208 
01210   bool  _lightning_active;
01211 
01213   int32 _lightning_current_time;
01214 
01216   int32 _lightning_end_time;
01217 
01219   std::vector <float> _lightning_data;
01220 
01222   int32 _animation_counter;
01223 
01225   int32 _current_frame_diff;
01226 
01228   std::map    <std::string, private_video::Image*>   _images;
01229 
01231   std::vector <private_video::TexSheet *>     _tex_sheets;
01232 
01234   std::map    <std::string, FontProperties *> _font_map;
01235 
01237   std::map <std::string, ParticleEffectDef *> _particle_effect_defs;
01238 
01240   std::stack  <private_video::Context>      _context_stack;
01241 
01242   //-- Private methods ------------------------------------------------------
01243 
01249   bool _BindTexture(GLuint tex_ID);
01250 
01255   int32 _ConvertXAlign(int32 xalign);
01256 
01261   int32 _ConvertYAlign(int32 yalign);
01262 
01269   GLuint _CreateBlankGLTexture(int32 width, int32 height);
01270 
01282   std::string _CreateTempFilename(const std::string &extension);
01283 
01292   private_video::TexSheet *_CreateTexSheet(int32 width, int32 height, private_video::TexSheetType type, bool is_static);
01293 
01299   bool _DeleteTexture(GLuint tex_ID);
01300 
01306   bool _DeleteImage(private_video::Image *const image);
01307 
01313   bool _DeleteImage(StillImage &id);
01314 
01320   bool _DeleteImage(AnimatedImage &id);
01321 
01326   bool _DeleteTempTextures();
01327 
01339   bool _DrawElement(const private_video::ImageElement &element, const Color *color_array);
01340 
01346   bool _DrawStillImage(const StillImage &img);
01347 
01354   bool _DrawStillImage(const StillImage &img, const Color &color);
01355 
01361   bool _DrawTextHelper(const uint16 *const uText);
01362 
01369   bool _CacheGlyphs(const uint16 *uText, FontProperties *fp);
01370 
01377   RenderedLine *_GenTexLine(uint16 *line, FontProperties *fp);
01378 
01384   Color _GetTextShadowColor(FontProperties *fp);
01385 
01393   private_video::TexSheet *_InsertImageInTexSheet(private_video::Image *image, private_video::ImageLoadInfo & load_info, bool is_static);
01394 
01400   bool _LoadImage(StillImage &id);
01401 
01407   bool _LoadImage(AnimatedImage &id);
01408 
01414   bool _LoadImageHelper(StillImage &id);
01415 
01416 
01423   bool _LoadMultiImage (std::vector <StillImage>& images, const std::string &file_name, const uint32& rows, const uint32& cols);
01424 
01431   bool _LoadRawImage(const std::string & file_name, private_video::ImageLoadInfo & load_info);
01432 
01439   bool _LoadRawImageJpeg(const std::string & file_name, private_video::ImageLoadInfo & load_info);
01440 
01447   bool _LoadRawImagePng(const std::string & file_name, private_video::ImageLoadInfo & load_info);
01448 
01454   bool _SavePng (const std::string& file_name, hoa_video::private_video::ImageLoadInfo &info) const;
01455 
01461   bool _SaveJpeg (const std::string& file_name, hoa_video::private_video::ImageLoadInfo &info) const;
01462 
01470   bool _GetImageInfoPng (const std::string& file_name, uint32 &rows, uint32& cols, uint32& bpp);
01471 
01479   bool _GetImageInfoJpeg (const std::string& file_name, uint32 &rows, uint32& cols, uint32& bpp);
01480 
01486   bool _ReloadImagesToSheet(private_video::TexSheet *tex_sheet);
01487 
01494   bool _RemoveImage(private_video::Image *image_to_remove);
01495 
01502   void _ConvertImageToGrayscale(const private_video::ImageLoadInfo& src, private_video::ImageLoadInfo &dst) const;
01503 
01504 
01509   void _RGBAToRGB (const private_video::ImageLoadInfo& src, private_video::ImageLoadInfo &dst) const;
01510 
01511 
01513 
01517   void _GetBufferFromTexture (hoa_video::private_video::ImageLoadInfo& buffer, hoa_video::private_video::TexSheet* texture) const;
01518 
01520 
01524   void _GetBufferFromImage (hoa_video::private_video::ImageLoadInfo& buffer, hoa_video::private_video::Image* img) const;
01525 
01531   bool _RemoveSheet(private_video::TexSheet *sheet_to_remove);
01532 
01539   float _RoundForce(float force);   // rounds a force value
01540 
01544   void _PopContext();
01545 
01549   void _PushContext();
01550 
01558   bool _SaveTempTextures();
01559 
01564   int32 _ScreenCoordX(float x);
01565 
01570   int32 _ScreenCoordY(float y);
01571 
01577   void  _UpdateShake(int32 frame_time);
01578 
01580   bool _ShouldSmooth();
01581 
01587   bool _DEBUG_ShowAdvancedStats();
01588 
01593   bool _DEBUG_ShowTexSheet();
01594 
01596   float _x;
01597   float _y;
01598 }; // class GameVideo
01599 
01600 }  // namespace hoa_video
01601 
01602 #endif // __VIDEO_HEADER__

Generated on Fri Jul 6 23:11:26 2007 for Hero of Allacrost by  doxygen 1.5.1