lsnes rr0-β2
[lsnes.git] / render.hpp
blob6396355744e29070f6bc465bfdd722200d2401dc
1 #ifndef _render__hpp__included__
2 #define _render__hpp__included__
4 #include <cstdint>
5 #include <string>
6 #include <list>
7 #include <vector>
8 #include <stdexcept>
10 /**
11 * Low color (32768 colors) screen from buffer.
13 struct lcscreen
15 /**
16 * Create new screen from bsnes output data.
18 * parameter mem The output buffer from bsnes.
19 * parameter hires True if in hires mode (512-wide lines instead of 256-wide).
20 * parameter interlace True if in interlace mode.
21 * parameter overscan True if overscan is enabled.
22 * parameter region True if PAL, false if NTSC.
24 lcscreen(const uint16_t* mem, bool hires, bool interlace, bool overscan, bool region) throw();
26 /**
27 * Create new memory-backed screen. The resulting screen can be written to.
29 lcscreen() throw();
30 /**
31 * Create new screen with specified contents and size.
33 * parameter mem: Memory to use as frame data. 1 element per pixel. Left-to-Right, top-to-bottom order.
34 * parameter _width: Width of the screen to create.
35 * parameter _height: Height of the screen to create.
37 lcscreen(const uint16_t* mem, uint32_t _width, uint32_t _height) throw();
38 /**
39 * Copy the screen.
41 * The assigned copy is always writable.
43 * parameter ls: The source screen.
44 * throws std::bad_alloc: Not enough memory.
46 lcscreen(const lcscreen& ls) throw(std::bad_alloc);
47 /**
48 * Assign the screen.
50 * parameter ls: The source screen.
51 * returns: Reference to target screen.
52 * throws std::bad_alloc: Not enough memory.
53 * throws std::runtime_error: The target screen is not writable.
55 lcscreen& operator=(const lcscreen& ls) throw(std::bad_alloc, std::runtime_error);
56 /**
57 * Load contents of screen.
59 * parameter data: The data to load.
60 * throws std::bad_alloc: Not enough memory.
61 * throws std::runtime_error: The target screen is not writable.
63 void load(const std::vector<char>& data) throw(std::bad_alloc, std::runtime_error);
64 /**
65 * Save contents of screen.
67 * parameter data: The vector to write the data to (in format compatible with load()).
68 * throws std::bad_alloc: Not enough memory.
70 void save(std::vector<char>& data) throw(std::bad_alloc);
71 /**
72 * Save contents of screen as a PNG.
74 * parameter file: The filename to save to.
75 * throws std::bad_alloc: Not enough memory.
76 * throws std::runtime_error: Can't save the PNG.
78 void save_png(const std::string& file) throw(std::bad_alloc, std::runtime_error);
80 /**
81 * Destructor.
83 ~lcscreen();
85 /**
86 * True if memory is allocated by new[] and should be freed by the destructor., false otherwise. Also signals
87 * writablity.
89 bool user_memory;
91 /**
92 * Memory, 1 element per pixel in left-to-right, top-to-bottom order, 15 low bits of each element used.
94 const uint16_t* memory;
96 /**
97 * Number of elements (not bytes) between two successive scanlines.
99 uint32_t pitch;
102 * Width of image.
104 uint32_t width;
107 * Height of image.
109 uint32_t height;
112 * Image allocated size (only valid for user_memory=true).
114 size_t allocated;
118 * Truecolor modifiable screen.
120 struct screen
123 * Creates screen. The screen dimensions are initially 0x0.
125 screen() throw();
128 * Destructor.
130 ~screen() throw();
133 * Sets the backing memory for screen. The specified memory is not freed if screen is reallocated or destroyed.
135 * parameter _memory: The memory buffer.
136 * parameter _width: Width of screen.
137 * parameter _height: Height of screen.
138 * parameter _originx: X coordinate for origin.
139 * parameter _originy: Y coordinate for origin.
140 * parameter _pitch: Distance in bytes between successive scanlines.
142 void set(uint32_t* _memory, uint32_t _width, uint32_t _height, uint32_t _originx, uint32_t _originy,
143 uint32_t _pitch) throw();
146 * Sets the size of the screen. The memory is freed if screen is reallocated or destroyed.
148 * parameter _width: Width of screen.
149 * parameter _height: Height of screen.
150 * parameter _originx: X coordinate for origin.
151 * parameter _originy: Y coordinate for origin.
152 * parameter upside_down: If true, image is upside down in memory.
153 * throws std::bad_alloc: Not enough memory.
155 void reallocate(uint32_t _width, uint32_t _height, uint32_t _originx, uint32_t _originy,
156 bool upside_down = false) throw(std::bad_alloc);
159 * Paints low-color screen into screen. The upper-left of image will be at origin. Scales the image by given factors.
160 * If the image does not fit with specified scale factors, it is clipped.
162 * parameter scr The screen to paint.
163 * parameter hscale Horizontal scale factor.
164 * parameter vscale Vertical scale factor.
166 void copy_from(lcscreen& scr, uint32_t hscale, uint32_t vscale) throw();
169 * Get pointer into specified row.
171 * parameter row: Number of row (must be less than height).
173 uint32_t* rowptr(uint32_t row) throw();
176 * Backing memory for this screen.
178 uint32_t* memory;
181 * True if memory is given by user and must not be freed.
183 bool user_memory;
186 * Width of screen.
188 uint32_t width;
191 * Height of screen.
193 uint32_t height;
196 * Distance between lines in bytes.
198 size_t pitch;
201 * True if image is upside down in memory.
203 bool flipped;
206 * X-coordinate of origin.
208 uint32_t originx;
211 * Y-coordinate of origin.
213 uint32_t originy;
216 * Palette.
218 uint32_t palette[32768];
221 * Sets the palette shifts, converting the existing image.
223 * parameter rshift Shift for red component.
224 * parameter gshift Shift for green component.
225 * parameter bshift Shift for blue component.
227 void set_palette(uint32_t rshift, uint32_t gshift, uint32_t bshift) throw();
230 * Returns color value with specified (r,g,b) values (scale 0-255).
232 * parameter r: Red component.
233 * parameter g: Green component.
234 * parameter b: Blue component.
235 * returns: color element value.
237 uint32_t make_color(uint8_t r, uint8_t g, uint8_t b) throw();
240 * Current red component shift.
242 uint32_t active_rshift;
245 * Current green component shift.
247 uint32_t active_gshift;
250 * Current blue component shift.
252 uint32_t active_bshift;
253 private:
254 screen(const screen&);
255 screen& operator=(const screen&);
259 * Base class for objects to render.
261 struct render_object
264 * Destructor.
266 virtual ~render_object() throw();
269 * Draw the object.
271 * parameter scr: The screen to draw it on.
273 virtual void operator()(struct screen& scr) throw() = 0;
277 * Queue of render operations.
279 struct render_queue
282 * Adds new object to render queue. The object must be allocated by new.
284 * parameter obj: The object to add
285 * throws std::bad_alloc: Not enough memory.
287 void add(struct render_object& obj) throw(std::bad_alloc);
290 * Applies all objects in the queue in order, freeing them in progress.
292 * parameter scr: The screen to apply queue to.
294 void run(struct screen& scr) throw();
297 * Frees all objects in the queue without applying them.
299 void clear() throw();
302 * Destructor.
304 ~render_queue() throw();
305 private:
306 std::list<struct render_object*> q;
310 * Read font data for glyph.
312 * parameter codepoint: Code point of glyph.
313 * parameter x: X position to render into.
314 * parameter y: Y position to render into.
315 * parameter orig_x: X position at start of row.
316 * parameter next_x: X position for next glyph is written here.
317 * parameter next_y: Y position for next glyph is written here.
318 * returns: Two components: First is width of character, second is its offset in font data (0 if blank glyph).
320 std::pair<uint32_t, size_t> find_glyph(uint32_t codepoint, int32_t x, int32_t y, int32_t orig_x,
321 int32_t& next_x, int32_t& next_y) throw();
324 * Render text into screen.
326 * parameter _x: The x position to render to (relative to origin).
327 * parameter _y: The y position to render to (relative to origin).
328 * parameter _text: The text to render (UTF-8).
329 * parameter _fg: Foreground color.
330 * parameter _fgalpha: Foreground alpha (0-256).
331 * parameter _bg: Background color.
332 * parameter _bgalpha: Background alpha (0-256).
333 * throws std::bad_alloc: Not enough memory.
335 void render_text(struct screen& scr, int32_t _x, int32_t _y, const std::string& _text, uint32_t _fg = 0xFFFFFFFFU,
336 uint16_t _fgalpha = 255, uint32_t _bg = 0, uint16_t _bgalpha = 0) throw(std::bad_alloc);
338 #endif