1 #ifndef _library__framebuffer__hpp__included__
2 #define _library__framebuffer__hpp__included__
10 #include "framebuffer-pixfmt.hpp"
14 template<bool X
> struct elem
{};
15 template<> struct elem
<false> { typedef uint32_t t
; };
16 template<> struct elem
<true> { typedef uint64_t t
; };
18 extern unsigned default_shift_r
;
19 extern unsigned default_shift_g
;
20 extern unsigned default_shift_b
;
23 * Pixel format auxillary palette.
28 typedef typename elem
<X
>::t element_t
;
29 uint8_t rshift
; //Red shift.
30 uint8_t gshift
; //Green shift.
31 uint8_t bshift
; //Blue shift.
32 std::vector
<element_t
> pcache
; //Palette cache.
36 * Game framebuffer information.
41 * Pixel format of framebuffer.
45 * The physical memory backing the framebuffer.
49 * Physical width of framebuffer.
53 * Physical height of framebuffer.
57 * Physical stride of framebuffer (in bytes).
61 * Visible width of framebuffer.
65 * Visible height of framebuffer.
69 * Visible stride of framebuffer (in bytes).
73 * Visible X offset of framebuffer.
77 * Visible Y offset of framebuffer.
82 template<bool X
> struct framebuffer
;
87 * This framebuffer is in system format, and can either be backed either by temporary buffer or memory buffer.
89 * Any copying only preserves the visible part.
94 * Create a new framebuffer backed by temporary buffer.
96 * The resulting framebuffer is read-only.
98 * Parameter info: The framebuffer info.
100 raw(const info
& finfo
) throw(std::bad_alloc
);
102 * Create a new framebuffer backed by memory buffer.
104 * The resulting framebuffer can be written to.
106 raw() throw(std::bad_alloc
);
108 * Copy a framebuffer.
110 * The resulting copy is writable.
112 * Parameter f: The framebuffer.
114 raw(const raw
& f
) throw(std::bad_alloc
);
116 * Assign a framebuffer.
118 * Parameter f: The framebuffer.
119 * Throws std::runtime_error: The target framebuffer is not writable.
121 raw
& operator=(const raw
& f
) throw(std::bad_alloc
, std::runtime_error
);
123 * Load contents of framebuffer.
125 * parameter data: The data to load.
126 * throws std::runtime_error: The target framebuffer is not writable.
128 void load(const std::vector
<char>& data
) throw(std::bad_alloc
, std::runtime_error
);
130 * Save contents of framebuffer.
132 * parameter data: The vector to write the data to (in format compatible with load()).
134 void save(std::vector
<char>& data
) throw(std::bad_alloc
);
136 * Save contents of framebuffer as a PNG.
138 * parameter file: The filename to save to.
139 * throws std::runtime_error: Can't save the PNG.
141 void save_png(const std::string
& file
) throw(std::bad_alloc
, std::runtime_error
);
145 * Returns: The width.
147 size_t get_width() const throw();
151 * Returns: The height.
153 size_t get_height() const throw();
157 size_t get_stride() const throw();
159 * Get starting address.
161 unsigned char* get_start() const throw();
165 pixfmt
* get_format() const throw();
171 bool user_memory
; //True if allocated in user memory, false if aliases framebuffer.
172 char* addr
; //Address of framebuffer start.
173 pixfmt
* fmt
; //Format of framebuffer.
174 size_t width
; //Width of framebuffer.
175 size_t height
; //Height of framebuffer.
176 size_t stride
; //Stride in pixels.
177 size_t allocated
; //Amount of memory allocated (only meaningful if user_memory=true).
178 template<bool X
> friend class fb
;
185 * Rendered framebuffer.
187 * This framebuffer is in RGB32/RGB64 format, and is always backed by memory buffer.
192 typedef typename elem
<X
>::t element_t
;
194 * Creates framebuffer. The framebuffer dimensions are initially 0x0.
204 * Sets the backing memory for framebuffer. The specified memory is not freed if framebuffer is reallocated or
207 * parameter _memory: The memory buffer.
208 * parameter _width: Width of framebuffer.
209 * parameter _height: Height of framebuffer.
210 * parameter _pitch: Distance in bytes between successive scanlines (in pixels).
212 * Note: The _pitch should be multiple of 4, and _memory aligned to 16 bytes.
214 void set(element_t
* _memory
, size_t _width
, size_t _height
, size_t _pitch
) throw();
217 * Sets the size of the framebuffer. The memory is freed if framebuffer is reallocated or destroyed.
219 * The pitch of resulting framebuffer is the smallest possible.
221 * parameter _width: Width of framebuffer.
222 * parameter _height: Height of framebuffer.
223 * parameter upside_down: If true, image is upside down in memory.
224 * throws std::bad_alloc: Not enough memory.
226 void reallocate(size_t _width
, size_t _height
, bool upside_down
= false) throw(std::bad_alloc
);
231 * parameter _originx: X coordinate for origin.
232 * parameter _originy: Y coordinate for origin.
234 void set_origin(size_t _originx
, size_t _originy
) throw();
238 * Returns: The X origin.
240 size_t get_origin_x() const throw();
244 * Returns: The Y origin.
246 size_t get_origin_y() const throw();
248 * Paints raw framebuffer into framebuffer. The upper-left of image will be at origin. Scales the image by given
249 * factors. If the image does not fit with specified scale factors, it is clipped.
251 * parameter scr The framebuffer to paint.
252 * parameter hscale Horizontal scale factor.
253 * parameter vscale Vertical scale factor.
255 void copy_from(raw
& scr
, size_t hscale
, size_t vscale
) throw();
258 * Get pointer into specified row.
260 * parameter row: Number of row (must be less than height).
261 * Returns: Pointer into row data.
263 element_t
* rowptr(size_t row
) throw();
265 * Get pointer into specified row.
267 * parameter row: Number of row (must be less than height).
268 * Returns: Pointer into row data.
270 const element_t
* rowptr(size_t row
) const throw();
272 * Set palette. Also converts the image data.
274 * parameter r Shift for red component
275 * parameter g Shift for green component
276 * parameter b Shift for blue component
278 void set_palette(uint32_t r
, uint32_t g
, uint32_t b
) throw(std::bad_alloc
);
280 * Get stride of image.
282 * Returns: The stride.
284 size_t get_stride() const throw() { return stride
; }
286 * Get width of image.
288 * Returns: The width.
290 size_t get_width() const throw() { return width
; }
292 * Get height of image.
294 * Returns: The height.
296 size_t get_height() const throw() { return height
; }
298 * Get last blit width
300 * Returns: The width.
302 size_t get_last_blit_width() const throw() { return last_blit_w
; }
304 * Get last blit height
306 * Returns: The height.
308 size_t get_last_blit_height() const throw() { return last_blit_h
; }
310 * Get R palette offset.
312 uint8_t get_palette_r() const throw();
314 * Get G palette offset.
316 uint8_t get_palette_g() const throw();
318 * Get B palette offset.
320 uint8_t get_palette_b() const throw();
323 fb
& operator=(const fb
& f
);
324 size_t width
; //Width of framebuffer.
325 size_t height
; //Height of framebuffer.
326 size_t stride
; //Stride in pixels.
327 size_t offset_x
; //X offset.
328 size_t offset_y
; //Y offset.
329 size_t last_blit_w
; //Width of last blit.
330 size_t last_blit_h
; //Height of last blit.
331 element_t
* mem
; //The memory of framebuffer.
332 pixfmt
* current_fmt
; //Current format of framebuffer.
333 auxpalette
<X
> auxpal
; //Aux palette.
334 bool user_mem
; //True if internal memory is used.
335 bool upside_down
; //Upside down flag.
336 uint8_t active_rshift
; //Red shift.
337 uint8_t active_gshift
; //Green shift.
338 uint8_t active_bshift
; //Blue shift.
345 * Base class for objects to render.
356 virtual ~object() throw();
358 * Kill object function. If it returns true, kill the request. Default is to return false.
360 virtual bool kill_request(void* obj
) throw();
362 * Return true if myobj and killobj are equal and not NULL.
364 bool kill_request_ifeq(void* myobj
, void* killobj
);
368 * parameter scr: The screen to draw it on.
370 virtual void operator()(struct fb
<false>& scr
) throw() = 0;
371 virtual void operator()(struct fb
<true>& scr
) throw() = 0;
375 virtual void clone(struct queue
& q
) const throw(std::bad_alloc
) = 0;
379 * Premultiplied color.
392 operator bool() const throw() { return (origa
!= 0); }
393 bool operator!() const throw() { return (origa
== 0); }
405 color(int64_t color
) throw()
413 orig
= color
& 0xFFFFFF;
414 origa
= 256 - ((color
>> 24) & 0xFF);;
417 invHI
= 256 * static_cast<uint32_t>(inv
);
418 set_palette(default_shift_r
, default_shift_g
, default_shift_b
, false);
419 set_palette(default_shift_r
<< 1, default_shift_g
<< 1, default_shift_b
<< 1, true);
420 //std::cerr << "Color " << color << " -> hi=" << hi << " lo=" << lo << " inv=" << inv << std::endl;
422 color(const std::string
& color
) throw(std::bad_alloc
, std::runtime_error
);
423 static std::string
stringify(int64_t number
);
424 void set_palette(unsigned rshift
, unsigned gshift
, unsigned bshift
, bool X
) throw();
425 template<bool X
> void set_palette(struct fb
<X
>& s
) throw()
427 set_palette(s
.active_rshift
, s
.active_gshift
, s
.active_bshift
, X
);
429 uint32_t blend(uint32_t color
) throw()
432 a
= color
& 0xFF00FF;
433 b
= (color
& 0xFF00FF00) >> 8;
434 return (((a
* inv
+ hi
) >> 8) & 0xFF00FF) | ((b
* inv
+ lo
) & 0xFF00FF00);
436 void apply(uint32_t& x
) throw()
440 uint64_t blend(uint64_t color
) throw()
443 a
= color
& 0xFFFF0000FFFFULL
;
444 b
= (color
& 0xFFFF0000FFFF0000ULL
) >> 16;
445 return (((a
* invHI
+ hiHI
) >> 16) & 0xFFFF0000FFFFULL
) | ((b
* invHI
+ loHI
) &
446 0xFFFF0000FFFF0000ULL
);
448 void apply(uint64_t& x
) throw()
452 int64_t asnumber() const throw()
457 return orig
| ((uint32_t)(256 - origa
) << 24);
461 int64_t color_rotate_hue(int64_t color
, int count
, int steps
);
462 int64_t color_adjust_saturation(int64_t color
, double adjust
);
463 int64_t color_adjust_lightness(int64_t color
, double adjust
);
470 basecolor(const std::string
& name
, int64_t value
);
478 color_mod(const std::string
& name
, std::function
<void(int64_t&)> fn
);
482 * Bitmap font (8x16).
491 bool wide
; //If set, 16 wide instead of 8.
492 uint32_t* data
; //Glyph data. Bitpacked with element padding between rows.
493 size_t offset
; //Glyph offset.
497 * Bitmap font layout.
501 size_t x
; //X position.
502 size_t y
; //Y position.
503 const glyph
* dglyph
; //The glyph itself.
508 font() throw(std::bad_alloc
);
510 * Load a .hex format font.
512 * Parameter data: The font data.
513 * Parameter size: The font data size in bytes.
514 * Throws std::runtime_error: Bad font data.
516 void load_hex(const char* data
, size_t size
) throw(std::bad_alloc
, std::runtime_error
);
520 * Parameter glyph: Number of glyph to locate.
521 * Returns: Glyph parameters.
523 const glyph
& get_glyph(uint32_t glyph
) throw();
525 * Get metrics of string.
527 * Parameter string: The string to get metrics of.
528 * Returns: A pair. First element is width of string, the second is height of string.
530 std::pair
<size_t, size_t> get_metrics(const std::string
& string
) throw();
534 * Parameter string: The string to get layout of.
535 * Returns: String layout.
537 std::vector
<layout
> dolayout(const std::string
& string
) throw(std::bad_alloc
);
539 * Get set of all glyph numbers.
541 std::set
<uint32_t> get_glyphs_set();
545 const glyph
& get_bad_glyph() throw();
547 * Render string to framebuffer.
549 * Parameter framebuffer: The framebuffer to render on.
550 * Parameter x: The x-coordinate to start from.
551 * Parameter y: The y-coordinate to start from.
552 * Parameter text: The text to render.
553 * Parameter fg: The foreground color.
554 * Parameter bg: The background color.
555 * Parameter hdbl: If set, double width horizontally.
556 * Parameter vdbl: If set, double height vertically.
558 template<bool X
> void render(struct fb
<X
>& scr
, int32_t x
, int32_t y
, const std::string
& text
,
559 color fg
, color bg
, bool hdbl
, bool vdbl
) throw();
562 uint32_t bad_glyph_data
[4];
563 std::map
<uint32_t, glyph
> glyphs
;
565 std::vector
<uint32_t> memory
;
566 void load_hex_glyph(const char* data
, size_t size
) throw(std::bad_alloc
, std::runtime_error
);
570 #define RENDER_PAGE_SIZE 65500
573 * Queue of render operations.
578 * Applies all objects in the queue in order.
580 * parameter scr: The screen to apply queue to.
582 template<bool X
> void run(struct fb
<X
>& scr
) throw();
585 * Frees all objects in the queue without applying them.
587 void clear() throw();
589 * Call kill_request on all objects in queue.
591 void kill_request(void* obj
) throw();
593 * Get memory from internal allocator.
595 void* alloc(size_t block
) throw(std::bad_alloc
);
598 * Call object constructor on internal memory.
600 template<class T
, typename
... U
> void create_add(U
... args
)
602 add(*new(alloc(sizeof(T
))) T(args
...));
605 * Copy objects from another render queue.
607 void copy_from(queue
& q
) throw(std::bad_alloc
);
611 template<typename T
> void clone_helper(const T
* obj
)
616 * Get number of objects.
618 size_t get_object_count()
621 struct node
* n
= queue_head
;
637 void add(struct object
& obj
) throw(std::bad_alloc
);
638 struct node
{ struct object
* obj
; struct node
* next
; bool killed
; };
639 struct page
{ char content
[RENDER_PAGE_SIZE
]; };
640 struct node
* queue_head
;
641 struct node
* queue_tail
;
642 size_t memory_allocated
;
644 std::map
<size_t, page
> memory
;
648 * Drop every fourth byte of specified buffer.
650 * Parameter dest: Destination buffer, should be 16-byte aligned.
651 * Parameter src: Source buffer, should be 16-byte aligned.
652 * Parameter units: Number of 4 byte units to copy. Must be multiple of 4.
654 void copy_drop4(uint8_t* dest
, const uint32_t* src
, size_t units
);
657 * Drop every fourth byte of specified buffer and swap first and third.
659 * Parameter dest: Destination buffer, should be 16-byte aligned.
660 * Parameter src: Source buffer, should be 16-byte aligned.
661 * Parameter units: Number of 4 byte units to copy. Must be multiple of 4.
663 void copy_drop4s(uint8_t* dest
, const uint32_t* src
, size_t units
);
666 * Swap every first and third byte out of four
668 * Parameter dest: Destination buffer, should be 16-byte aligned.
669 * Parameter src: Source buffer, should be 16-byte aligned.
670 * Parameter units: Number of 4 byte units to copy. Must be multiple of 4.
672 void copy_swap4(uint8_t* dest
, const uint32_t* src
, size_t units
);
675 * Drop every fourth word of specified buffer.
677 * Parameter dest: Destination buffer, should be 16-byte aligned.
678 * Parameter src: Source buffer, should be 16-byte aligned.
679 * Parameter units: Number of 8 byte units to copy. Must be multiple of 2.
681 void copy_drop4(uint16_t* dest
, const uint64_t* src
, size_t units
);
684 * Drop every fourth byte of specified buffer and swap first and third.
686 * Parameter dest: Destination buffer, should be 16-byte aligned.
687 * Parameter src: Source buffer, should be 16-byte aligned.
688 * Parameter units: Number of 8 byte units to copy. Must be multiple of 2.
690 void copy_drop4s(uint16_t* dest
, const uint64_t* src
, size_t units
);
693 * Swap every first and third byte out of four
695 * Parameter dest: Destination buffer, should be 16-byte aligned.
696 * Parameter src: Source buffer, should be 16-byte aligned.
697 * Parameter units: Number of 8 byte units to copy. Must be multiple of 2.
699 void copy_swap4(uint16_t* dest
, const uint64_t* src
, size_t units
);