gcsx_graphics.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* GCSx
  2. ** GRAPHICS.H
  3. **
  4. ** Basic graphics primitives/support via SDL
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_GRAPHICS_H_
  24. #define __GCSx_GRAPHICS_H_
  25. // Startup/exit
  26. void initVideo();
  27. // -1 for "best" bpp; gl mode disallows any direct access to screen surface
  28. void selectVideoMode(int x, int y, int bpp, int fullscreen, int resizable, int gl = 0) throw_Video;
  29. void exitVideo();
  30. void prepareFont();
  31. void closeFont();
  32. // GUI colors, in RGB and packed formats
  33. enum {
  34. COLOR_FILL = 0,
  35. COLOR_LIGHT1,
  36. COLOR_LIGHT2,
  37. COLOR_DARK1,
  38. COLOR_DARK2,
  39. COLOR_BUTTONFILL1,
  40. COLOR_BUTTONFILL2,
  41. COLOR_BUTTONDOWN1,
  42. COLOR_BUTTONDOWN2,
  43. COLOR_TEXT,
  44. COLOR_TITLEACTIVE1,
  45. COLOR_TITLEACTIVE2,
  46. COLOR_TITLEINACTIVE1,
  47. COLOR_TITLEINACTIVE2,
  48. COLOR_TEXTBOX,
  49. COLOR_CURSOR,
  50. COLOR_SELECTION1,
  51. COLOR_SELECTION2,
  52. COLOR_TILECURSOR,
  53. COLOR_TILESELECTION,
  54. COLOR_TILECURSORBORDER1,
  55. COLOR_TILECURSORBORDER2,
  56. COLOR_SCROLLTRACK,
  57. COLOR_LINEBORDER,
  58. COLOR_BKFILL,
  59. COLOR_GRID,
  60. COLOR_FRONTDESKTOP,
  61. COLOR_EDITORDESKTOP,
  62. COLOR_TRANSPARENT1,
  63. COLOR_TRANSPARENT2,
  64. COLOR_TOOLTIP,
  65. COLOR_GLYPHWIDTH,
  66. COLOR_POPUPFILL1,
  67. COLOR_POPUPFILL2,
  68. COLOR_POPUPTEXT,
  69. COLOR_CONSOLEPROMPT,
  70. COLOR_CONSOLEINPUT,
  71. COLOR_COUNT
  72. };
  73. extern SDL_Color guiRGB[COLOR_COUNT];
  74. extern Uint32 guiPacked[COLOR_COUNT];
  75. // For gui- removes first \t and returns an underline position/length code
  76. // Places shortcut key (lowercase) in shortcut if not NULL
  77. int convertGuiText(std::string* text, char* shortcut, int font = FONT_STANDARD);
  78. // Draws the underline, call after drawText
  79. void drawTextUnderline(int underlineCode, Uint32 color, int x, int y, SDL_Surface* surface, int font = FONT_STANDARD);
  80. // Radio buttons and checkboxes
  81. void drawCheckbox(int isRadio, int isChecked, int isDisabled, int x, int y, int size, SDL_Surface* surface);
  82. // Info
  83. int fontWidth(const std::string& text, int font = FONT_STANDARD);
  84. int fontHeight(int font = FONT_STANDARD);
  85. int fontAscent(int font = FONT_STANDARD);
  86. extern int screenWidth;
  87. extern int screenHeight;
  88. int screenBits();
  89. int screenFullscreen();
  90. int screenResizable();
  91. int screenGL();
  92. int guiOk(); // True if GUI is usable- used by exceptions
  93. // Rectangle operations
  94. // Intersection of two rects; returns false and sets a.w to 0 if empty
  95. // intersection; result in a
  96. int intersectRects(Rect& a, const Rect& b);
  97. // Bounding box around two rects; result in a
  98. // Special case returns a untouched if b.w is zero, returns b in a if a.w is zero
  99. // If both .w are zero, false is returned with a unchanged
  100. // True is returned otherwise
  101. int boundRects(Rect& a, const Rect& b);
  102. // Take x1/x2/y1/y2 and create a rectangle; swaps as needed
  103. Rect createRect(int x1, int y1, int x2, int y2);
  104. // Used in GL mode only- only needed if you're doing your own stuff
  105. // However, if you use one, you must use the other
  106. void preGLScreenFlip();
  107. // (then do your drawing, then optionally call screenFlip, then optionally do additional drawing)
  108. void postGLScreenFlip();
  109. // (calling postGLScreenFlip has no effect if you didn't use preGLScreenFlip)
  110. // Standard graphics primitives/functions
  111. void screenFlip(const Rect* area = NULL); // area.w 0 for mouse-only update
  112. void setScreenGLAlpha(int alpha); // Only works in GL mode- alpha blends screen; 0-256
  113. void drawPixel(int x, int y, SDL_Color color, SDL_Surface* surface);
  114. void drawPixel(int x, int y, Uint32 data, SDL_Surface* surface);
  115. // Returns alpha 0 black if outside clip area
  116. Uint32 getPixel(int x, int y, SDL_Surface* surface);
  117. // (to allow common SDL routins with our Rect structure)
  118. // (doesn't support NULL rect mode!)
  119. int SDL_FillRect(SDL_Surface *dst, Rect *dstrect, Uint32 color);
  120. void SDL_SetClipRect(SDL_Surface *surface, Rect *rect);
  121. void SDL_SetClipRect(SDL_Surface *surface, const Rect *rect);
  122. void blit(int sX, int sY, SDL_Surface* src, int dX, int dY, SDL_Surface* dest, int width = -1, int height = -1);
  123. // (returns width)
  124. int drawText(const std::string& text, SDL_Color fg, int x, int y, SDL_Surface* surface, int font = FONT_STANDARD);
  125. void drawRect(int x, int y, int w, int h, Uint32 color, SDL_Surface* surface);
  126. void drawGradient(int x, int y, int w, int h, SDL_Color color1, SDL_Color color2, SDL_Surface* surface);
  127. void drawBox(int x, int y, int w, int h, Uint32 color, SDL_Surface* surface);
  128. void drawLine(int x1, int y1, int x2, int y2, Uint32 color, SDL_Surface* surface);
  129. // (x1 can be > or < x2)
  130. void drawHLine(int x1, int x2, int y1, Uint32 color, SDL_Surface* surface);
  131. void drawVLine(int y1, int y2, int x1, Uint32 color, SDL_Surface* surface);
  132. // (dotted lines automatically line up wherever drawn)
  133. void drawDottedHLine(int x1, int x2, int y, Uint32 color, SDL_Surface* surface);
  134. void drawDottedVLine(int y1, int y2, int x, Uint32 color, SDL_Surface* surface);
  135. void drawGuiBox(int x, int y, int w, int h, int thickness, SDL_Surface* surface);
  136. // (inverted version does not prefill with solid color!)
  137. void drawGuiBoxInvert(int x, int y, int w, int h, int thickness, SDL_Surface* surface);
  138. void drawFocusBox(int x, int y, int w, int h, SDL_Surface* surface);
  139. void drawSelectRect(int x, int y, int w, int h, Uint32 color, SDL_Surface* surface, int alpha = 128);
  140. // (unused)
  141. // void drawSelectGradient(int x, int y, int w, int h, SDL_Color color1, SDL_Color color2, SDL_Surface* surface, int alpha = 128);
  142. // phase ranges from 0 to 3; this is used to draw selection rectangles in a
  143. // style similar to "walking ants"; the black portion of rectangle is drawn
  144. // with 0 alpha also, so be careful if alpha blitting
  145. enum { NUM_PHASES = 8 };
  146. void drawAntsHLine(int x1, int x2, int y, int phase, SDL_Surface* surface);
  147. void drawAntsVLine(int y1, int y2, int x, int phase, SDL_Surface* surface);
  148. void drawAntsBox(int x1, int y1, int w, int h, int phase, SDL_Surface* surface);
  149. // Creating surfaces
  150. // (software surface, same settings as screen, alpha blending disabled by default)
  151. SDL_Surface* createSurface(int width, int height);
  152. // (software surface, 32bpp, RGBA order, alpha blending disabled by default)
  153. SDL_Surface* createSurface32(int width, int height);
  154. // (software surface, 8bpp, for creating masks and such, not intended for actual video)
  155. SDL_Surface* createSurface8(int width, int height);
  156. // (performs a full alpha-overlay designed for graphics editing; unoptimized;
  157. // assumes both surfaces are from createSurface32)
  158. void alphaBlit32(int sX, int sY, const SDL_Surface* src, int dX, int dY, SDL_Surface* dest, int width = -1, int height = -1);
  159. // For graphics display in editor (fairly unoptimized) assumes src is createSurface32
  160. // rgba are on specified scales; asserts w=h if using rotate90
  161. void alphaBlitFx(int sX, int sY, const SDL_Surface* src, int dX, int dY, SDL_Surface* dest, int width = -1, int height = -1, int mirror = 0, int flip = 0, int rotate90 = 0, int red = 0xFF, int green = 0xFF, int blue = 0xFF, int rgbScale = 0xFF, int alpha = 0xFF, int alphaScale = 0xFF);
  162. // (performs a blit from one surface to another, masked by a third; 0 alpha on third
  163. // means don't copy anything, anything else means copy exactly; target surface,
  164. // mask surface, and source surface can be the same IF the x/y are the same)
  165. // (if clear is true, fills any spot where copy was performed with the clear color)
  166. void maskBlit32(int sX, int sY, SDL_Surface* src, int dX, int dY, SDL_Surface* dest, int mX, int mY, const SDL_Surface* mask, int width = -1, int height = -1, int clear = 0, Uint32 clearColor = 0);
  167. void maskAlphaBlit32(int sX, int sY, SDL_Surface* src, int dX, int dY, SDL_Surface* dest, int mX, int mY, const SDL_Surface* mask, int alphaPercent = 100, int width = -1, int height = -1, int clear = 0, Uint32 clearColor = 0);
  168. // For use specifically with createSurface32 surfaces only
  169. inline Uint32 mapColor32(int r, int g, int b, int a) {
  170. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  171. return (r << 24) | (g << 16) | (b << 8) | a;
  172. #else
  173. return (a << 24) | (b << 16) | (g << 8) | r;
  174. #endif
  175. }
  176. inline void splitColor32(Uint32 color, int& r, int& g, int& b, int& a) {
  177. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  178. a = color & 255;
  179. b = (color >> 8) & 255;
  180. g = (color >> 16) & 255;
  181. r = (color >> 24) & 255;
  182. #else
  183. r = color & 255;
  184. g = (color >> 8) & 255;
  185. b = (color >> 16) & 255;
  186. a = (color >> 24) & 255;
  187. #endif
  188. }
  189. // Flood fill
  190. // Only works on surface32s
  191. // Returns affected area
  192. // Relies on using color on *dest as a boundary for what's already been filled.
  193. // This means dest must be empty (or not use color) OR, dest can be the same as
  194. // src IF tolerance is zero. If you use dest/src the same and tolerance is not
  195. // zero, the fill will stop if the filled area already contains color; if you use
  196. // a separate dest that already contains the color, it will prohibit filling.
  197. Rect floodFill32(SDL_Surface* src, SDL_Surface* dest, int x, int y, Uint32 color, int tolerance);
  198. // Non-contiguous fill- no special exceptions needed
  199. Rect floodFillNonContiguous32(SDL_Surface* src, SDL_Surface* dest, int x, int y, Uint32 color, int tolerance);
  200. // Lower level functions that may be of use (returns NULL if createText called with empty string)
  201. SDL_Surface* createText(const std::string& text, SDL_Color fg, int alpha = 1, int font = FONT_STANDARD);
  202. SDL_Surface* getScreen();
  203. Uint32 convertRGB(SDL_Color color, int alpha = -1);
  204. // (called whenever palette/mode changes)
  205. void convertGuiItems();
  206. enum {
  207. // Mouse pointers
  208. MOUSE_NORMAL = 0,
  209. MOUSE_VERT,
  210. MOUSE_HORIZ,
  211. MOUSE_BEAM,
  212. MOUSE_DIAGDOWN,
  213. MOUSE_DIAGUP,
  214. MOUSE_PIXEL,
  215. MOUSE_ADD,
  216. MOUSE_SUBTRACT,
  217. MOUSE_FOURDIRECTION,
  218. MOUSE_COUNT,
  219. };
  220. void selectMouse(int type);
  221. int mouseTooltipYOffset(); // Pixels to offset tooltip by for current mouse
  222. // low level, called by gui; always call unblit before blit if both called
  223. void unblitMouse(); // only call once before screenupdate
  224. void blitMouse(); // only call once before screenupdate
  225. #endif