1 #include "ugraphics.h"
2 #include <ulog/ulog.h>
3 #ifdef AOS_COMP_SDL2
4 
5 #define MAX_TEXT 100
6 #define MAX_IMAGE 10
7 
8 SDL_Window *window;
9 SDL_Renderer *renderer;
10 
11 TTF_Font *font = NULL;
12 struct { char *string; SDL_Texture *texture; } text[MAX_TEXT];
13 struct { char *file; SDL_Texture *texture; } image[MAX_IMAGE];
14 
15 // Internal function prototypes
16 static int32_t graphics_generate_text(char *string);
17 static void graphics_wipe_text(void);
18 static int32_t graphics_draw_texture(SDL_Texture *texture, int32_t x, int32_t y);
19 #endif
20 
21 #define TAG "ugraphics"
22 
23 int32_t text_count = 0;
24 int32_t image_count = 0;
25 
ugraphics_init(int32_t width,int32_t height)26 int32_t ugraphics_init(int32_t width, int32_t height)
27 {
28 #ifdef AOS_COMP_SDL2
29     if (SDL_Init(SDL_INIT_VIDEO) == -1) {
30         LOGE(TAG, "SDL_Init: %s\n", SDL_GetError());
31         return -1;
32     }
33 
34     if (IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG) == -1) {
35         LOGE(TAG, "IMG_Init: %s\n", SDL_GetError());
36         return -1;
37     }
38 
39     if (SDL_CreateWindowAndRenderer(width, height, 0, &window, &renderer) == -1) {
40         LOGE(TAG, "SDL_CreateWindowAndRenderer: %s\n", SDL_GetError());
41         return -1;
42     }
43 
44     SDL_SetWindowTitle(window, "ugraphics");
45     return 0;
46 #endif
47 }
48 
ugraphics_load_font(const char * filename,int32_t size)49 int32_t ugraphics_load_font(const char *filename, int32_t size)
50 {
51 #ifdef AOS_COMP_SDL2
52     if (font) {
53         LOG("font has been opened\n");
54         return 0;
55     }
56 
57     if (TTF_Init() == -1) {
58         LOGE(TAG, "TTF_Init: %s\n", TTF_GetError());
59         return -1;
60     }
61 
62     font = TTF_OpenFont(filename, size);
63     if (!font) {
64         LOGE(TAG, "TTF_OpenFont: %s\n", TTF_GetError());
65         return -1;
66     }
67     return 0;
68 #endif
69 }
70 
ugraphics_set_font_style(int32_t style)71 void ugraphics_set_font_style(int32_t style)
72 {
73 #ifdef AOS_COMP_SDL2
74     if (font)
75         TTF_SetFontStyle(font, style);
76     else
77         LOGE(TAG, "TTF_SeetFontStyle fail\n");
78 #endif
79 }
80 
81 // Delete all generated text forcing them to be regenerated next frame
graphics_wipe_text(void)82 void graphics_wipe_text(void)
83 {
84 #ifdef AOS_COMP_SDL2
85     for (int32_t i = 0; i < text_count; i++) {
86         if (text[i].string != NULL)
87             free(text[i].string);
88         SDL_DestroyTexture(text[i].texture);
89     }
90     text_count = 0;
91 #endif
92 }
93 
ugraphics_quit(void)94 void ugraphics_quit(void)
95 {
96 #ifdef AOS_COMP_SDL2
97     if (font) {
98         TTF_CloseFont(font);
99         TTF_Quit();
100     }
101     graphics_wipe_text();
102     graphics_wipe_image();
103     if (renderer)
104         SDL_DestroyRenderer(renderer);
105     if (window)
106         SDL_DestroyWindow(window);
107     SDL_Quit();
108 #endif
109 }
110 
ugraphics_flip(void)111 void ugraphics_flip(void)
112 {
113 #ifdef AOS_COMP_SDL2
114     SDL_RenderPresent(renderer);
115     SDL_RenderClear(renderer);
116     graphics_wipe_text();
117     graphics_wipe_image();
118 #endif
119 }
120 
ugraphics_clear(void)121 int32_t ugraphics_clear(void)
122 {
123 #ifdef AOS_COMP_SDL2
124     return SDL_RenderClear(renderer);
125 #endif
126 }
127 
ugraphics_set_color(uint32_t color)128 int32_t ugraphics_set_color(uint32_t color)
129 {
130 #ifdef AOS_COMP_SDL2
131     return SDL_SetRenderDrawColor(renderer, color >> 24,
132                              color >> 16, color >> 8, color);
133 #endif
134 }
135 
graphics_generate_text(char * string)136 static int32_t graphics_generate_text(char *string)
137 {
138 #ifdef AOS_COMP_SDL2
139     SDL_Color color;
140     SDL_GetRenderDrawColor(renderer, &color.r, &color.g, &color.b, &color.a);
141     SDL_Surface *surface = TTF_RenderUTF8_Blended(font, string, color);
142     if (!surface) {
143         LOGE(TAG, "TTF_RenderUTF8_Blended: %s\n", TTF_GetError());
144         return -1;
145     }
146     SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
147     SDL_FreeSurface(surface);
148     if (!texture) {
149         LOGE(TAG, "SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
150         return -1;
151     }
152     text[text_count].string = strdup(string);
153     // text[text_count].string = string;
154     text[text_count].texture = texture;
155     text_count++;
156     return 0;
157 #endif
158 }
159 
graphics_wipe_image(void)160 void graphics_wipe_image(void)
161 {
162 #ifdef AOS_COMP_SDL2
163     for (int32_t i = 0; i < image_count; i++)
164         SDL_DestroyTexture(image[i].texture);
165 
166     image_count = 0;
167 #endif
168 }
169 
ugraphics_draw_rect(int32_t x,int32_t y,int32_t w,int32_t h)170 int32_t ugraphics_draw_rect(int32_t x, int32_t y, int32_t w, int32_t h)
171 {
172 #ifdef AOS_COMP_SDL2
173     SDL_Rect rect = { x, y, w, h };
174     return SDL_RenderDrawRect(renderer, &rect);
175 #endif
176 }
177 
ugraphics_fill_rect(int32_t x,int32_t y,int32_t w,int32_t h)178 int32_t ugraphics_fill_rect(int32_t x, int32_t y, int32_t w, int32_t h)
179 {
180 #ifdef AOS_COMP_SDL2
181     SDL_Rect rect = { x, y, w, h };
182     return SDL_RenderFillRect(renderer, &rect);
183 #endif
184 }
185 
ugraphics_draw_line(int32_t x1,int32_t y1,int32_t x2,int32_t y2)186 int32_t ugraphics_draw_line(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
187 {
188 #ifdef AOS_COMP_SDL2
189     return SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
190 #endif
191 }
192 
193 #ifdef AOS_COMP_SDL2
graphics_draw_texture(SDL_Texture * texture,int32_t x,int32_t y)194 static int32_t graphics_draw_texture(SDL_Texture *texture, int32_t x, int32_t y)
195 {
196     int32_t ret;
197     SDL_Rect drect = { x, y, 0, 0 };
198 
199     ret = SDL_QueryTexture(texture, NULL, NULL, &drect.w, &drect.h);
200     if (ret < 0)
201         return -1;
202     printf("drect.w: %d, drect.h: %d\n", drect.w, drect.h);
203     ret = SDL_RenderCopy(renderer, texture, NULL, &drect);
204     if (ret < 0)
205         return -1;
206 
207     return 0;
208 }
209 #endif
210 
ugraphics_draw_string(char * string,int32_t x,int32_t y)211 int32_t ugraphics_draw_string(char *string, int32_t x, int32_t y)
212 {
213     int32_t ret;
214 
215 #ifdef AOS_COMP_SDL2
216     if (strcmp(string, "") == 0)
217         return -1;
218     for (int32_t i = 0; i < text_count; i++) {
219         if (strcmp(string, text[i].string) != 0)
220             continue;
221         ret = graphics_draw_texture(text[i].texture, x, y);
222         if (ret < 0)
223             return -1;
224     }
225     /*If a texture for the string doesn't exist create it*/
226     ret = graphics_generate_text(string);
227     if (ret < 0)
228         return -1;
229     ret = graphics_draw_texture(text[text_count - 1].texture, x, y);
230     if (ret < 0)
231         return -1;
232 #endif
233     return 0;
234 }
235 
ugraphics_string_width(char * string)236 int32_t ugraphics_string_width(char *string)
237 {
238 #ifdef AOS_COMP_SDL2
239     if (strcmp(string, "") == 0)
240         return 0;
241     for (int32_t i = 0; i < text_count; i++) {
242         if (strcmp(string, text[i].string) != 0)
243             continue;
244         int32_t width;
245         SDL_QueryTexture(text[i].texture, NULL, NULL, &width, NULL);
246         return width;
247     }
248 #endif
249     return 0;
250 }
251 
graphics_generate_image(char * file)252 static int32_t graphics_generate_image(char *file)
253 {
254 #ifdef AOS_COMP_SDL2
255     SDL_Texture *image_texture = IMG_LoadTexture(renderer, file);
256     if (!image_texture) {
257         LOGE(TAG, "Couldn't load %s\n", file);
258         return -1;
259     }
260 
261     image[image_count].file = file;
262     image[image_count].texture = image_texture;
263     image_count++;
264 #endif
265     return 0;
266 }
267 
ugraphics_draw_image(const char * file,int32_t x,int32_t y)268 int32_t ugraphics_draw_image(const char *file,  int32_t x, int32_t y)
269 {
270     int32_t ret;
271 
272 #ifdef AOS_COMP_SDL2
273     SDL_Rect drect = { x, y, 0, 0 };
274     if (strcmp(file, "") == 0) {
275         LOG("[%s]file is null\n");
276         return -1;
277     }
278     for (int32_t i = 0; i < image_count; i++) {
279         if (strcmp(file, image[i].file) != 0)
280             continue;
281         graphics_draw_texture(image[i].texture, x, y);
282         return 0;
283     }
284     ret = graphics_generate_image(file);
285     if (ret < 0)
286         return -1;
287 
288     ret = graphics_draw_texture(image[image_count - 1].texture, x, y);
289     if (ret < 0)
290         return -1;
291 #endif
292     return 0;
293 }
294 
ugraphics_save_image(char * buffer,int32_t len,const char * path)295 int32_t ugraphics_save_image(char *buffer, int32_t len, const char *path)
296 {
297     if (!buffer) {
298         LOGE(TAG, "buffer is null\n");
299         return -1;
300     }
301 #ifdef AOS_COMP_SDL2
302     FILE *file;
303 
304     file = fopen(path, "wb");
305 
306     if (file == NULL) {
307         LOGE(TAG, "open file %s fail,\n", path);
308         return -1;
309     }
310 
311     if (fwrite(buffer, len, 1, file) < 1) {
312         LOGE(TAG, "write file buf fail\n");
313         return -1;
314     }
315     fclose(file);
316 #endif
317     return 0;
318 }
319 
320