1 #pragma once
2 
3 /** Class for loading and handling images.
4 */
5 
6 #include "resources.h"
7 
8 using std::string;
9 
10 #if defined(__ANDROID__)
11 #include <SDL_image.h>
12 #elif defined(__DragonFly__)
13 #include <SDL2/SDL_image.h>
14 #elif defined(__MORPHOS__)
15 #include <SDL/SDL_image.h>
16 #else
17 #include <sdl_image.h>
18 #endif
19 
20 const int font_index_period_c = 26;
21 const int font_index_apostrophe_c = 27;
22 const int font_index_exclamation_c = 28;
23 const int font_index_question_c = 29;
24 const int font_index_comma_c = 30;
25 const int font_index_dash_c = 31;
26 const int n_font_chars_c = 32;
27 
28 namespace Gigalomania {
29 	class Image : public TrackedObject {
30 		unsigned char *data;
31 		bool need_to_free_data;
32 		SDL_Surface *surface;
33 #if SDL_MAJOR_VERSION == 1
34 		static SDL_Surface *dest_surf;
35 #else
36 		SDL_Texture *texture;
37 		static SDL_Renderer *sdlRenderer;
38 #endif
39 		float scale_x, scale_y;
40 		int offset_x, offset_y;
41 
42 		Image();
43 
44 		void free();
45 
46 	public:
47 		virtual ~Image();
getClass()48 		virtual const char *getClass() const { return "CLASS_IMAGE"; }
49 
50 		void draw(int x, int y) const;
51 		void draw(int x, int y, int sw, int sh) const;
52 		void draw(int x, int y, float scale_w, float scale_h) const;
53 		void drawWithAlpha(int x, int y, unsigned char alpha) const;
54 		int getWidth() const;
55 		int getHeight() const;
getScaledWidth()56 		int getScaledWidth() const {
57 			return (int)(this->getWidth() / scale_x);
58 		}
getScaledHeight()59 		int getScaledHeight() const {
60 			return (int)(this->getHeight() / scale_y);
61 		}
62 		bool convertToDisplayFormat();
63 		bool copyPalette(const Image *image);
getScaleX()64 		float getScaleX() const {
65 			return scale_x;
66 		}
getScaleY()67 		float getScaleY() const {
68 			return scale_y;
69 		}
70 		void setScale(float scale_x,float scale_y);
71 		bool scaleTo(int n_w);
72 		void scale(float sx,float sy);
setOffset(int offset_x,int offset_y)73 		void setOffset(int offset_x, int offset_y) {
74 			this->offset_x = offset_x;
75 			this->offset_y = offset_y;
76 		}
77 		void remap(unsigned char sr,unsigned char sg,unsigned char sb,unsigned char rr,unsigned char rg,unsigned char rb);
78 		void reshadeRGB(int from, bool to_r, bool to_g, bool to_b);
79 		void brighten(float sr, float sg, float sb);
80 		void fadeAlpha(bool x_dir, bool fwd);
81 #if SDL_MAJOR_VERSION == 1
82 		void fillRect(int x, int y, int w, int h, unsigned char r, unsigned char g, unsigned char b);
83 #endif
84 		//void flipX();
85 		Image *copy(int x,int y,int w,int h) const;
copy()86 		Image *copy() const {
87 			// need to used scaled width/height, to counteract scaling in copy()
88 			return this->copy(0, 0, getScaledWidth(), getScaledHeight());
89 		}
90 		//void print(const char *inStr) const;
91 		bool isPaletted() const;
92 		int getNColors() const;
93 		unsigned char getPixelIndex(int x,int y) const;
94 		bool setPixelIndex(int x,int y,unsigned char c);
95 		bool setColor(int index,unsigned char r,unsigned char g,unsigned char b);
96 		bool createAlphaForColor(bool mask, unsigned char mr, unsigned char mg, unsigned char mb, unsigned char ar, unsigned char ag, unsigned char ab, unsigned char alpha);
97 		void scaleAlpha(float scale);
98 		bool convertToHiColor(bool alpha);
99 		void smooth();
100 
101 		static Image * loadImage(const char *filename);
loadImage(string filename)102 		static Image * loadImage(string filename) {
103 			return loadImage(filename.c_str());
104 		}
105 		static Image * createBlankImage(int width,int height, int bpp);
106 		enum NOISEMODE_t {
107 			NOISEMODE_PERLIN = 0,
108 			NOISEMODE_SMOKE = 1,
109 			NOISEMODE_PATCHY = 2,
110 			NOISEMODE_MARBLE = 3,
111 			NOISEMODE_WOOD = 4,
112 			NOISEMODE_CLOUDS = 5
113 		};
114 		static Image * createNoise(int w,int h,float scale_u,float scale_v,const unsigned char filter_max[3],const unsigned char filter_min[3],NOISEMODE_t noisemode,int n_iterations);
115 		static Image * createRadial(int w,int h,float alpha_scale);
116 		static Image * createRadial(int w,int h,float alpha_scale, Uint8 r, Uint8 g, Uint8 b);
117 
118 		enum Justify {
119 			JUSTIFY_LEFT = 0,
120 			JUSTIFY_CENTRE = 1,
121 			JUSTIFY_RIGHT = 2,
122 		};
123 		static void writeNumbers(int x,int y,Image *images[10],int number,Justify justify);
124 		static void write(int x,int y,Image *images[n_font_chars_c],const char *text,Justify justify);
125 		static void writeMixedCase(int x,int y,Image *large[n_font_chars_c],Image *little[n_font_chars_c],Image *numbers[10],const char *text,Justify justify);
126 
127 		// SDL specific
128 #if SDL_MAJOR_VERSION == 1
129 		static void setGraphicsOutput(SDL_Surface *dest_surf);
130 #else
131 		static void setGraphicsOutput(SDL_Renderer *sdlRenderer);
132 #endif
133 	};
134 }
135