summaryrefslogtreecommitdiff
path: root/rotord/src/nodes_drawing.h
blob: 05135d6ff613eac21ba85fec7f333ad5beb9009a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef ROTOR_NODES_DRAWING
#define ROTOR_NODES_DRAWING

#include "rotor.h"
#include <cairo.h>

namespace Rotor {
	class Draw: public Image_node {
		public:
			Draw(){image=nullptr;};
			Draw(map<string,string> &settings) {
				image=nullptr;
				base_settings(settings);
				if(CAIRO_HAS_IMAGE_SURFACE==1) {
					cerr<<"cairo has image surface"<<endl;
				}
			};
			~Draw(){ if (image) delete image;};
			Draw* clone(map<string,string> &_settings) { return new Draw(_settings);};
			Image *output(const Frame_spec &frame){
				if (image_inputs.size()) {
					if (image_inputs[0]->connection){
						//copy incoming image **writable
						if (image) delete image;
						image=(((Image_node*)image_inputs[0]->connection)->get_output(frame))->clone();                 
	                }
	                else {
	                	if (!image) image =new Image();
	                	image->setup(frame.w,frame.h);
	                }
			    }
			    else {
			    	if (!image) image =new Image();
			    	image->setup(frame.w,frame.h); //do this twice or use a goto	
			    }		    
			    //draw onto new or input image
			    image->convert32(); //pad frame out to 32 bits for cairo
			    //turns out cairo doesn't draw at all if the stride definition is wrong
			    //crashes like this though
			    cairo_surface_t * cs = cairo_image_surface_create_for_data (image->RGBdata,
                                                         CAIRO_FORMAT_RGB24,
                                                         image->w,
                                                         image->h,
                                                         image->w*4);
			    cairo_t * cr = cairo_create (cs);
				cairo_rectangle(cr, 0,0, image->w/2, image->h/2);
				cairo_set_source_rgb(cr, 0,1.0,0); //cairo colour is 0.0->1.0
				cairo_fill(cr);

				cairo_text_extents_t te;
				cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
				cairo_select_font_face (cr, "Georgia",
				                  CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
				cairo_set_font_size (cr, 20);
				cairo_text_extents (cr, "hello world!", &te);
				cairo_move_to (cr, 20,20);
				cairo_show_text (cr, "hello world!");
				cairo_fill(cr);

				image->convert24(); //convert frame back to 24 bits
				return image;
			}
		private:
			Image *image; //is an image generator
	};
}

#endif