-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot-sdl-lib.patch
335 lines (297 loc) · 10.6 KB
/
plot-sdl-lib.patch
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
diff -Naru plot-sdl-orig/plot-sdl-lib/plot.c plot-sdl/plot-sdl-lib/plot.c
--- plot-sdl-orig/plot-sdl-lib/plot.c 2018-12-29 19:05:51.359581687 +0100
+++ plot-sdl/plot-sdl-lib/plot.c 2018-12-29 18:53:12.967517273 +0100
@@ -53,24 +53,28 @@
int plot_graph(plot_params *params)
{
- setvbuf (stdout, NULL, _IONBF, 0);
+ if (params->screen == (SDL_Window *)NULL)
+ {
+ setvbuf (stdout, NULL, _IONBF, 0);
+
+
+ if(TTF_Init() == -1)
+ {
+ fprintf(stderr, "Error SDL TTF_Init error : %s\n", TTF_GetError());
+ return EXIT_FAILURE;
+ }
+ if (SDL_Init(SDL_INIT_VIDEO)==-1){
+
+ fprintf(stderr, "Error SDL init failure : %s\n", SDL_GetError());
+ return EXIT_FAILURE;
+ }
+ }
- surfacelist surface_list = NULL;
-
- if(TTF_Init() == -1)
- {
- fprintf(stderr, "Error SDL TTF_Init error : %s\n", TTF_GetError());
- return EXIT_FAILURE;
- }
- if (SDL_Init(SDL_INIT_VIDEO)==-1){
-
- fprintf(stderr, "Error SDL init failure : %s\n", SDL_GetError());
- return EXIT_FAILURE;
- }
+ surfacelist surface_list = NULL;
splot plot;
plot.font = NULL;
- plot.screen = NULL;
+ plot.screen = params->screen;
plot.plot_surface = NULL;
plot.plot_mask_surface = NULL;
plot.caption_surface = NULL;
@@ -79,7 +83,7 @@
plot.captionY = NULL;
plot.textureX = NULL;
plot.textureY = NULL;
- plot.renderer = NULL;
+ plot.renderer = params->renderer;
//font specs
plot.font = TTF_OpenFont(params->font_text_path, params->font_text_size);
@@ -92,19 +96,22 @@
return EXIT_FAILURE;
}
- plot.screen = SDL_CreateWindow(
- params->plot_window_title,
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- params->screen_width,
- params->screen_heigth,
- SDL_WINDOW_SHOWN);
+ if (plot.screen == NULL)
+ {
+ plot.screen = SDL_CreateWindow(
+ params->plot_window_title,
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ params->screen_width,
+ params->screen_heigth,
+ SDL_WINDOW_SHOWN);
+ }
//SDL_SetWindowFullscreen(plot.screen,SDL_WINDOW_FULLSCREEN);
draw_plot(&plot,params,&surface_list);
- clean_plot(&plot,params,&surface_list);
+ clean_plot(&plot,params,&surface_list);
return EXIT_SUCCESS;
}
@@ -121,26 +128,31 @@
*/
void clean_plot(splot *plot, plot_params *params, surfacelist *surface_list)
{
- SDL_DestroyTexture(plot->textureX);
- SDL_DestroyTexture(plot->textureY);
- SDL_FreeSurface (plot->plot_mask_surface);
- SDL_FreeSurface (plot->plot_surface);
- SDL_FreeSurface (plot->caption_mask_surface);
- SDL_FreeSurface (plot->caption_surface);
- SDL_FreeSurface (plot->captionX);
- SDL_FreeSurface (plot->captionY);
+ SDL_DestroyTexture(plot->textureX);
+ SDL_DestroyTexture(plot->textureY);
+ SDL_FreeSurface (plot->plot_mask_surface);
+ SDL_FreeSurface (plot->plot_surface);
+ SDL_FreeSurface (plot->caption_mask_surface);
+ SDL_FreeSurface (plot->caption_surface);
+ SDL_FreeSurface (plot->captionX);
+ SDL_FreeSurface (plot->captionY);
params->caption_list = clear_caption(params->caption_list);
params->coordinate_list = clear_coord(params->coordinate_list);
*surface_list=clear_surface(*surface_list);
- SDL_DestroyRenderer(plot->renderer);
- SDL_DestroyWindow (plot->screen);
-
- SDL_Quit();
- TTF_CloseFont(plot->font);
- TTF_Quit();
+ if (params->screen != NULL) {
+ TTF_CloseFont(plot->font);
+ return;
+ }
+
+ SDL_DestroyRenderer(plot->renderer);
+ SDL_DestroyWindow (plot->screen);
+
+ SDL_Quit();
+ TTF_CloseFont(plot->font);
+ TTF_Quit();
}
/**
@@ -155,7 +167,10 @@
*/
void draw_plot(splot *plot, plot_params *params, surfacelist *surface_list)
{
- plot->renderer = SDL_CreateRenderer(plot->screen, 0, 0);
+ if (params->renderer == NULL)
+ {
+ plot->renderer = SDL_CreateRenderer(plot->screen, 0, 0);
+ } else plot->renderer = params->renderer;
if (plot->screen != NULL) {
@@ -166,14 +181,17 @@
plot->captionY = TTF_RenderText_Blended(plot->font, params->caption_text_y, font_color);
//------------ background-----------------------
- SDL_SetRenderDrawColor(plot->renderer,255,255,255,255);
- SDL_Rect screen;
- screen.x=0;
- screen.y=0;
- screen.w=params->screen_width;
- screen.h=params->screen_heigth;
+ if (!params->hide_backgroud)
+ {
+ SDL_SetRenderDrawColor(plot->renderer,255,255,255,255);
+ SDL_Rect screen;
+ screen.x=params->offset_x;
+ screen.y=params->offset_y;
+ screen.w=params->screen_width;
+ screen.h=params->screen_heigth;
- SDL_RenderFillRect( plot->renderer, &screen );
+ SDL_RenderFillRect( plot->renderer, &screen );
+ }
//---------------------------------------------
float plot_width=params->screen_width*0.8;
@@ -181,8 +199,8 @@
float plot_caption_heigth=params->screen_heigth*0.05;
SDL_Rect plot_position;
- plot_position.x=(params->screen_width/2)-(plot_width*0.47);
- plot_position.y=(params->screen_heigth*0.50)-(plot_heigth/2);
+ plot_position.x=((params->screen_width/2)-(plot_width*0.47)) + params->offset_x;
+ plot_position.y=((params->screen_heigth*0.50)-(plot_heigth/2)) + params->offset_y;
plot_position.w=plot_width;
plot_position.h=plot_heigth;
@@ -199,24 +217,25 @@
plot_caption_position.h=plot_caption_heigth;
SDL_Rect plot_caption_mask_position;
- plot_caption_mask_position.x=plot_caption_position.x-stroke_width;
- plot_caption_mask_position.y=plot_caption_position.y-stroke_width;
- plot_caption_mask_position.w=plot_width+stroke_width*2;
- plot_caption_mask_position.h=plot_caption_heigth+stroke_width*2;
-
- SDL_SetRenderDrawColor(plot->renderer,0, 0, 0,255);
- SDL_RenderFillRect(plot->renderer,&plot_mask_position);
-
- SDL_SetRenderDrawColor(plot->renderer,255, 255, 255,255);
- SDL_RenderFillRect(plot->renderer,&plot_position);
-
- SDL_SetRenderDrawColor(plot->renderer,0, 0, 0,255);
- SDL_RenderFillRect(plot->renderer,&plot_caption_mask_position);
-
- SDL_SetRenderDrawColor(plot->renderer,255, 255, 255,255);
- SDL_RenderFillRect(plot->renderer,&plot_caption_position);
-
-
+ if (!params->hide_caption)
+ {
+ plot_caption_mask_position.x=plot_caption_position.x-stroke_width;
+ plot_caption_mask_position.y=plot_caption_position.y-stroke_width;
+ plot_caption_mask_position.w=plot_width+stroke_width*2;
+ plot_caption_mask_position.h=plot_caption_heigth+stroke_width*2;
+
+ SDL_SetRenderDrawColor(plot->renderer,0, 0, 0,255);
+ SDL_RenderFillRect(plot->renderer,&plot_mask_position);
+
+ SDL_SetRenderDrawColor(plot->renderer,255, 255, 255,255);
+ SDL_RenderFillRect(plot->renderer,&plot_position);
+
+ SDL_SetRenderDrawColor(plot->renderer,0, 0, 0,255);
+ SDL_RenderFillRect(plot->renderer,&plot_caption_mask_position);
+
+ SDL_SetRenderDrawColor(plot->renderer,255, 255, 255,255);
+ SDL_RenderFillRect(plot->renderer,&plot_caption_position);
+ }
SDL_Rect caption_y_position;
caption_y_position.x=plot_position.x;
caption_y_position.y=plot_position.y;
@@ -282,6 +301,7 @@
caption_text.x=circle_x2+DOT_RADIUS+CAPTION_OFFSET_CIRCLE_TO_TEXT;
caption_text.y=circle_y2-caption_text.h/2;
SDL_RenderCopy(plot->renderer, texture_text, NULL, &caption_text);
+ SDL_DestroyTexture(texture_text);
*surface_list=push_back_surface(*surface_list,caption_text_surface);
@@ -299,13 +319,15 @@
}
}
- SDL_RenderPresent(plot->renderer);
+ if (params->renderer == NULL)
+ SDL_RenderPresent(plot->renderer);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_VERBOSE,APP_NAME,"plot finished");
#endif
- wait_for_sdl_event();
+ if (params->renderer == NULL)
+ wait_for_sdl_event();
}
else{
fprintf(stderr, "Error cant allocate memory for screen : %s\n", SDL_GetError());
@@ -448,6 +470,7 @@
caption_text.x=init_pos_x-caption_text.w/2;
caption_text.y=init_pos_y+5;
SDL_RenderCopy(renderer, texture_text, NULL, &caption_text);
+ SDL_DestroyTexture(texture_text);
*surface_list=push_back_surface(*surface_list,caption_text_surface);
@@ -476,6 +499,7 @@
caption_text.x=init_pos_x-caption_text.w-10;
caption_text.y=init_pos_y-caption_text.h/2;
SDL_RenderCopy(renderer, texture_text, NULL, &caption_text);
+ SDL_DestroyTexture(texture_text);
*surface_list=push_back_surface(*surface_list,caption_text_surface);
@@ -497,6 +521,7 @@
SDL_Point caption_center={plot_position_x-CAPTION_Y_LABEL_OFFSET,0};
SDL_RendererFlip flip = SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL;
SDL_RenderCopyEx(plot->renderer,plot->textureY,NULL,&text_caption_y,90,&caption_center,flip);
+ SDL_DestroyTexture(plot->textureY);
//caption x
SDL_Rect text_caption_x;
@@ -505,6 +530,7 @@
text_caption_x.x=params->screen_width/2-text_caption_x.w/2;
text_caption_x.y=plot_position_y+plot_heigth+1*regular_caption_text_heigth;
SDL_RenderCopy(plot->renderer, plot->textureX, NULL, &text_caption_x);
+ SDL_DestroyTexture(plot->textureX);
}
/**
@@ -636,4 +662,4 @@
SDL_RenderDrawPoint(renderer,x,((int)(cy - r + dy)));
}
}
-}
\ Ingen nyrad vid filslut
+}
diff -Naru plot-sdl-orig/plot-sdl-lib/plotsdl/plot.h plot-sdl/plot-sdl-lib/plotsdl/plot.h
--- plot-sdl-orig/plot-sdl-lib/plotsdl/plot.h 2018-12-29 19:05:51.359581687 +0100
+++ plot-sdl/plot-sdl-lib/plotsdl/plot.h 2018-12-29 18:55:55.086700725 +0100
@@ -66,6 +66,12 @@
float scale_y ;
float max_x ;
float max_y ;
+ SDL_Renderer *renderer ;
+ SDL_Window *screen ;
+ int offset_x ;
+ int offset_y ;
+ int hide_backgroud ;
+ int hide_caption ;
} plot_params;
@@ -214,4 +220,4 @@
*/
void fill_circle(SDL_Renderer *renderer, int cx, int cy, int radius);
-#endif
\ Ingen nyrad vid filslut
+#endif
diff -Naru plot-sdl-orig/plot-sdl-test/main.c plot-sdl/plot-sdl-test/main.c
--- plot-sdl-orig/plot-sdl-test/main.c 2018-12-29 19:05:51.359581687 +0100
+++ plot-sdl/plot-sdl-test/main.c 2018-12-29 18:57:06.834339680 +0100
@@ -87,6 +87,12 @@
params.scale_y = 10;
params.max_x = 8.5;
params.max_y = 120;
+ params.renderer = NULL; // Let the lib create the renderer
+ params.screen = NULL; // Let the lib create the screen
+ params.hide_backgroud = 0; // Do not hide the white background
+ params.hide_caption = 0; // Do not hide the caption
+ params.offset_x = 0; // Add an offset to x postion
+ params.offset_y = 0; // Add an offset to the y position
int ret = plot_graph(¶ms);
@@ -97,4 +103,4 @@
}
return EXIT_SUCCESS;
-}
\ Ingen nyrad vid filslut
+}