void update_infos() { //base y position of the text int base_y = 0; //height of a line of text with the current font. int line_height = TTF_FontLineSkip(font); //buffer to hold the current textto be drawn char buf[TEXT_BUF_SIZE]; //retrieve navdata one by one int bat = jakopter_com_read_int(com_in, 0); //and format it for textual display snprintf(buf, TEXT_BUF_SIZE, "Battery : %d%%", bat); buf[TEXT_BUF_SIZE-1] = '\0'; //finally, print it onto a texture using SDL_ttf graphs[0].tex = video_make_text(buf, &graphs[0].pos.w, &graphs[0].pos.h); //and set its draw position accordingly graphs[0].pos.x = 0; graphs[0].pos.y = base_y; //go to a new line base_y += line_height; int alt = jakopter_com_read_int(com_in, 4); snprintf(buf, TEXT_BUF_SIZE, "Altitude : %d", alt); buf[TEXT_BUF_SIZE-1] = '\0'; graphs[1].tex = video_make_text(buf, &graphs[1].pos.w, &graphs[1].pos.h); graphs[1].pos.x = 0; graphs[1].pos.y = base_y; //update pitch, roll, yaw and speed pitch = jakopter_com_read_float(com_in, 8); roll = jakopter_com_read_float(com_in, 12); yaw = jakopter_com_read_float(com_in, 16); speed = jakopter_com_read_float(com_in, 20); }
/** * \brief Create the SDL object associated with a graphical element. * With SDL, we can't create or destroy texture from a different thread than the rendering one. * \returns 0 if it's ok, -1 otherwise */ int load_element() { pthread_mutex_lock(&mutex_graphics_list); if (icon_registry == NULL) { pthread_mutex_unlock(&mutex_graphics_list); return 0; } if (load_icon == NULL) load_icon = icon_registry; //find the first that isn't loaded yet while (load_icon->next != NULL && load_icon->string == NULL) load_icon = load_icon->next; //nothing found if (load_icon->string == NULL) { pthread_mutex_unlock(&mutex_graphics_list); return 0; } if (load_icon->graphic == NULL) { fprintf(stderr, "[~][display] Graphic element isn't initialized\n"); pthread_mutex_unlock(&mutex_graphics_list); return -1; } load_icon->graphic->tex = NULL; char* string = load_icon->string; if (load_icon->type == VIDEO_TEXT) { graphics_t* graph = load_icon->graphic; graph->tex = video_make_text(string, &graph->pos.w, &graph->pos.h); if (graph->pos.x >= current_width-(graph->pos.w) || graph->pos.y >= current_height-(graph->pos.h)) { fprintf(stderr, "[~][display] Position of \"%s\" doesn't fit the bounds of the window\n", string); pthread_mutex_unlock(&mutex_graphics_list); display_graphic_remove(load_icon->id); return -1; } } else if (load_icon->type == VIDEO_ICON) { graphics_t* graph = load_icon->graphic; int width = 0; int height = 0; SDL_Texture *icon = video_import_png(string, &width, &height); if (icon == NULL) { fprintf(stderr, "[~][display] Couldn't import the image %s\n", string); pthread_mutex_unlock(&mutex_graphics_list); display_graphic_remove(load_icon->id); return -1; } graph->tex = icon; if (graph->pos.w == 0 || graph->pos.h == 0) { graph->pos.w = width; graph->pos.h = height; } if (graph->pos.x >= current_width-(graph->pos.w) || graph->pos.y >= current_height-(graph->pos.h)) { fprintf(stderr, "[~][display] Position of %s doesn't fit the bounds of the window\n", string); pthread_mutex_unlock(&mutex_graphics_list); display_graphic_remove(load_icon->id); return -1; } } //String used for load, indicates now the item is loaded load_icon->string = NULL; free(string); pthread_mutex_unlock(&mutex_graphics_list); return 0; }