commit
f2a49d561f
59 changed files with 802 additions and 19477 deletions
@ -1 +1 @@ |
|||||||
Subproject commit 486a09a2e9a8c4ae5bb2853a9bef2b64e875f883 |
Subproject commit e5a04ab458afd52cf630cc9e35ccdc10efba6688 |
@ -1 +1 @@ |
|||||||
7bcb9e414784d5ca8a4338680421023d07ef9e53 |
a64f5f3d44b2d96c5b555dd91172e5e8d760edb2 |
@ -1,311 +0,0 @@ |
|||||||
#include "selfdrive/ui/paint.h" |
|
||||||
|
|
||||||
#include <cassert> |
|
||||||
|
|
||||||
#ifdef __APPLE__ |
|
||||||
#include <OpenGL/gl3.h> |
|
||||||
#define NANOVG_GL3_IMPLEMENTATION |
|
||||||
#define nvgCreate nvgCreateGL3 |
|
||||||
#else |
|
||||||
#include <GLES3/gl3.h> |
|
||||||
#define NANOVG_GLES3_IMPLEMENTATION |
|
||||||
#define nvgCreate nvgCreateGLES3 |
|
||||||
#endif |
|
||||||
|
|
||||||
#define NANOVG_GLES3_IMPLEMENTATION |
|
||||||
#include <nanovg_gl.h> |
|
||||||
#include <nanovg_gl_utils.h> |
|
||||||
|
|
||||||
#include "selfdrive/common/util.h" |
|
||||||
#include "selfdrive/hardware/hw.h" |
|
||||||
#include "selfdrive/ui/ui.h" |
|
||||||
|
|
||||||
static void ui_draw_text(const UIState *s, float x, float y, const char *string, float size, NVGcolor color, const char *font_name) { |
|
||||||
nvgFontFace(s->vg, font_name); |
|
||||||
nvgFontSize(s->vg, size); |
|
||||||
nvgFillColor(s->vg, color); |
|
||||||
nvgText(s->vg, x, y, string, NULL); |
|
||||||
} |
|
||||||
|
|
||||||
static void draw_chevron(UIState *s, float x, float y, float sz, NVGcolor fillColor, NVGcolor glowColor) { |
|
||||||
// glow
|
|
||||||
float g_xo = sz/5; |
|
||||||
float g_yo = sz/10; |
|
||||||
nvgBeginPath(s->vg); |
|
||||||
nvgMoveTo(s->vg, x+(sz*1.35)+g_xo, y+sz+g_yo); |
|
||||||
nvgLineTo(s->vg, x, y-g_xo); |
|
||||||
nvgLineTo(s->vg, x-(sz*1.35)-g_xo, y+sz+g_yo); |
|
||||||
nvgClosePath(s->vg); |
|
||||||
nvgFillColor(s->vg, glowColor); |
|
||||||
nvgFill(s->vg); |
|
||||||
|
|
||||||
// chevron
|
|
||||||
nvgBeginPath(s->vg); |
|
||||||
nvgMoveTo(s->vg, x+(sz*1.25), y+sz); |
|
||||||
nvgLineTo(s->vg, x, y); |
|
||||||
nvgLineTo(s->vg, x-(sz*1.25), y+sz); |
|
||||||
nvgClosePath(s->vg); |
|
||||||
nvgFillColor(s->vg, fillColor); |
|
||||||
nvgFill(s->vg); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_circle_image(const UIState *s, int center_x, int center_y, int radius, const char *image, NVGcolor color, float img_alpha) { |
|
||||||
nvgBeginPath(s->vg); |
|
||||||
nvgCircle(s->vg, center_x, center_y, radius); |
|
||||||
nvgFillColor(s->vg, color); |
|
||||||
nvgFill(s->vg); |
|
||||||
const int img_size = radius * 1.5; |
|
||||||
ui_draw_image(s, {center_x - (img_size / 2), center_y - (img_size / 2), img_size, img_size}, image, img_alpha); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_circle_image(const UIState *s, int center_x, int center_y, int radius, const char *image, bool active) { |
|
||||||
float bg_alpha = active ? 0.3f : 0.1f; |
|
||||||
float img_alpha = active ? 1.0f : 0.15f; |
|
||||||
ui_draw_circle_image(s, center_x, center_y, radius, image, nvgRGBA(0, 0, 0, (255 * bg_alpha)), img_alpha); |
|
||||||
} |
|
||||||
|
|
||||||
static void draw_lead(UIState *s, const cereal::RadarState::LeadData::Reader &lead_data, const vertex_data &vd) { |
|
||||||
// Draw lead car indicator
|
|
||||||
auto [x, y] = vd; |
|
||||||
|
|
||||||
float fillAlpha = 0; |
|
||||||
float speedBuff = 10.; |
|
||||||
float leadBuff = 40.; |
|
||||||
float d_rel = lead_data.getDRel(); |
|
||||||
float v_rel = lead_data.getVRel(); |
|
||||||
if (d_rel < leadBuff) { |
|
||||||
fillAlpha = 255*(1.0-(d_rel/leadBuff)); |
|
||||||
if (v_rel < 0) { |
|
||||||
fillAlpha += 255*(-1*(v_rel/speedBuff)); |
|
||||||
} |
|
||||||
fillAlpha = (int)(fmin(fillAlpha, 255)); |
|
||||||
} |
|
||||||
|
|
||||||
float sz = std::clamp((25 * 30) / (d_rel / 3 + 30), 15.0f, 30.0f) * 2.35; |
|
||||||
x = std::clamp(x, 0.f, s->fb_w - sz / 2); |
|
||||||
y = std::fmin(s->fb_h - sz * .6, y); |
|
||||||
draw_chevron(s, x, y, sz, nvgRGBA(201, 34, 49, fillAlpha), COLOR_YELLOW); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_line(UIState *s, const line_vertices_data &vd, NVGcolor *color, NVGpaint *paint) { |
|
||||||
if (vd.cnt == 0) return; |
|
||||||
|
|
||||||
const vertex_data *v = &vd.v[0]; |
|
||||||
nvgBeginPath(s->vg); |
|
||||||
nvgMoveTo(s->vg, v[0].x, v[0].y); |
|
||||||
for (int i = 1; i < vd.cnt; i++) { |
|
||||||
nvgLineTo(s->vg, v[i].x, v[i].y); |
|
||||||
} |
|
||||||
nvgClosePath(s->vg); |
|
||||||
if (color) { |
|
||||||
nvgFillColor(s->vg, *color); |
|
||||||
} else if (paint) { |
|
||||||
nvgFillPaint(s->vg, *paint); |
|
||||||
} |
|
||||||
nvgFill(s->vg); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_vision_lane_lines(UIState *s) { |
|
||||||
const UIScene &scene = s->scene; |
|
||||||
NVGpaint track_bg; |
|
||||||
if (!scene.end_to_end) { |
|
||||||
// paint lanelines
|
|
||||||
for (int i = 0; i < std::size(scene.lane_line_vertices); i++) { |
|
||||||
NVGcolor color = nvgRGBAf(1.0, 1.0, 1.0, scene.lane_line_probs[i]); |
|
||||||
ui_draw_line(s, scene.lane_line_vertices[i], &color, nullptr); |
|
||||||
} |
|
||||||
|
|
||||||
// paint road edges
|
|
||||||
for (int i = 0; i < std::size(scene.road_edge_vertices); i++) { |
|
||||||
NVGcolor color = nvgRGBAf(1.0, 0.0, 0.0, std::clamp<float>(1.0 - scene.road_edge_stds[i], 0.0, 1.0)); |
|
||||||
ui_draw_line(s, scene.road_edge_vertices[i], &color, nullptr); |
|
||||||
} |
|
||||||
track_bg = nvgLinearGradient(s->vg, s->fb_w, s->fb_h, s->fb_w, s->fb_h * .4, |
|
||||||
COLOR_WHITE, COLOR_WHITE_ALPHA(0)); |
|
||||||
} else { |
|
||||||
track_bg = nvgLinearGradient(s->vg, s->fb_w, s->fb_h, s->fb_w, s->fb_h * .4, |
|
||||||
COLOR_RED, COLOR_RED_ALPHA(0)); |
|
||||||
} |
|
||||||
// paint path
|
|
||||||
ui_draw_line(s, scene.track_vertices, nullptr, &track_bg); |
|
||||||
} |
|
||||||
|
|
||||||
// Draw all world space objects.
|
|
||||||
static void ui_draw_world(UIState *s) { |
|
||||||
nvgScissor(s->vg, 0, 0, s->fb_w, s->fb_h); |
|
||||||
|
|
||||||
// Draw lane edges and vision/mpc tracks
|
|
||||||
ui_draw_vision_lane_lines(s); |
|
||||||
|
|
||||||
// Draw lead indicators if openpilot is handling longitudinal
|
|
||||||
if (s->scene.longitudinal_control) { |
|
||||||
auto lead_one = (*s->sm)["radarState"].getRadarState().getLeadOne(); |
|
||||||
auto lead_two = (*s->sm)["radarState"].getRadarState().getLeadTwo(); |
|
||||||
if (lead_one.getStatus()) { |
|
||||||
draw_lead(s, lead_one, s->scene.lead_vertices[0]); |
|
||||||
} |
|
||||||
if (lead_two.getStatus() && (std::abs(lead_one.getDRel() - lead_two.getDRel()) > 3.0)) { |
|
||||||
draw_lead(s, lead_two, s->scene.lead_vertices[1]); |
|
||||||
} |
|
||||||
} |
|
||||||
nvgResetScissor(s->vg); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_vision_maxspeed(UIState *s) { |
|
||||||
const int SET_SPEED_NA = 255; |
|
||||||
float maxspeed = (*s->sm)["controlsState"].getControlsState().getVCruise(); |
|
||||||
const bool is_cruise_set = maxspeed != 0 && maxspeed != SET_SPEED_NA; |
|
||||||
if (is_cruise_set && !s->scene.is_metric) { maxspeed *= KM_TO_MILE; } |
|
||||||
|
|
||||||
const Rect rect = {bdr_s * 2, int(bdr_s * 1.5), 184, 202}; |
|
||||||
ui_fill_rect(s->vg, rect, COLOR_BLACK_ALPHA(100), 30.); |
|
||||||
ui_draw_rect(s->vg, rect, COLOR_WHITE_ALPHA(100), 10, 20.); |
|
||||||
|
|
||||||
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE); |
|
||||||
ui_draw_text(s, rect.centerX(), 118, "MAX", 26 * 2.5, COLOR_WHITE_ALPHA(is_cruise_set ? 200 : 100), "sans-regular"); |
|
||||||
if (is_cruise_set) { |
|
||||||
const std::string maxspeed_str = std::to_string((int)std::nearbyint(maxspeed)); |
|
||||||
ui_draw_text(s, rect.centerX(), 212, maxspeed_str.c_str(), 48 * 2.5, COLOR_WHITE, "sans-bold"); |
|
||||||
} else { |
|
||||||
ui_draw_text(s, rect.centerX(), 212, "N/A", 42 * 2.5, COLOR_WHITE_ALPHA(100), "sans-semibold"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_vision_speed(UIState *s) { |
|
||||||
const float speed = std::max(0.0, (*s->sm)["carState"].getCarState().getVEgo() * (s->scene.is_metric ? MS_TO_KPH : MS_TO_MPH)); |
|
||||||
const std::string speed_str = std::to_string((int)std::nearbyint(speed)); |
|
||||||
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE); |
|
||||||
ui_draw_text(s, s->fb_w/2, 210, speed_str.c_str(), 96 * 2.5, COLOR_WHITE, "sans-bold"); |
|
||||||
ui_draw_text(s, s->fb_w/2, 290, s->scene.is_metric ? "km/h" : "mph", 36 * 2.5, COLOR_WHITE_ALPHA(200), "sans-regular"); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_vision_event(UIState *s) { |
|
||||||
if (s->scene.engageable) { |
|
||||||
// draw steering wheel
|
|
||||||
const int radius = 96; |
|
||||||
const int center_x = s->fb_w - radius - bdr_s * 2; |
|
||||||
const int center_y = radius + (bdr_s * 1.5); |
|
||||||
const QColor &color = bg_colors[s->status]; |
|
||||||
NVGcolor nvg_color = nvgRGBA(color.red(), color.green(), color.blue(), color.alpha()); |
|
||||||
ui_draw_circle_image(s, center_x, center_y, radius, "wheel", nvg_color, 1.0f); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_vision_face(UIState *s) { |
|
||||||
const int radius = 96; |
|
||||||
const int center_x = radius + (bdr_s * 2); |
|
||||||
const int center_y = s->fb_h - footer_h / 2; |
|
||||||
ui_draw_circle_image(s, center_x, center_y, radius, "driver_face", s->scene.dm_active); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_vision_header(UIState *s) { |
|
||||||
NVGpaint gradient = nvgLinearGradient(s->vg, 0, header_h - (header_h / 2.5), 0, header_h, |
|
||||||
nvgRGBAf(0, 0, 0, 0.45), nvgRGBAf(0, 0, 0, 0)); |
|
||||||
ui_fill_rect(s->vg, {0, 0, s->fb_w , header_h}, gradient); |
|
||||||
ui_draw_vision_maxspeed(s); |
|
||||||
ui_draw_vision_speed(s); |
|
||||||
ui_draw_vision_event(s); |
|
||||||
} |
|
||||||
|
|
||||||
static void ui_draw_vision(UIState *s) { |
|
||||||
const UIScene *scene = &s->scene; |
|
||||||
// Draw augmented elements
|
|
||||||
if (scene->world_objects_visible) { |
|
||||||
ui_draw_world(s); |
|
||||||
} |
|
||||||
// Set Speed, Current Speed, Status/Events
|
|
||||||
ui_draw_vision_header(s); |
|
||||||
if ((*s->sm)["controlsState"].getControlsState().getAlertSize() == cereal::ControlsState::AlertSize::NONE) { |
|
||||||
ui_draw_vision_face(s); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void ui_draw(UIState *s, int w, int h) { |
|
||||||
// Update intrinsics matrix after possible wide camera toggle change
|
|
||||||
if (s->fb_w != w || s->fb_h != h) { |
|
||||||
ui_resize(s, w, h); |
|
||||||
} |
|
||||||
glEnable(GL_BLEND); |
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
||||||
nvgBeginFrame(s->vg, s->fb_w, s->fb_h, 1.0f); |
|
||||||
ui_draw_vision(s); |
|
||||||
nvgEndFrame(s->vg); |
|
||||||
glDisable(GL_BLEND); |
|
||||||
} |
|
||||||
|
|
||||||
void ui_draw_image(const UIState *s, const Rect &r, const char *name, float alpha) { |
|
||||||
nvgBeginPath(s->vg); |
|
||||||
NVGpaint imgPaint = nvgImagePattern(s->vg, r.x, r.y, r.w, r.h, 0, s->images.at(name), alpha); |
|
||||||
nvgRect(s->vg, r.x, r.y, r.w, r.h); |
|
||||||
nvgFillPaint(s->vg, imgPaint); |
|
||||||
nvgFill(s->vg); |
|
||||||
} |
|
||||||
|
|
||||||
void ui_draw_rect(NVGcontext *vg, const Rect &r, NVGcolor color, int width, float radius) { |
|
||||||
nvgBeginPath(vg); |
|
||||||
radius > 0 ? nvgRoundedRect(vg, r.x, r.y, r.w, r.h, radius) : nvgRect(vg, r.x, r.y, r.w, r.h); |
|
||||||
nvgStrokeColor(vg, color); |
|
||||||
nvgStrokeWidth(vg, width); |
|
||||||
nvgStroke(vg); |
|
||||||
} |
|
||||||
|
|
||||||
static inline void fill_rect(NVGcontext *vg, const Rect &r, const NVGcolor *color, const NVGpaint *paint, float radius) { |
|
||||||
nvgBeginPath(vg); |
|
||||||
radius > 0 ? nvgRoundedRect(vg, r.x, r.y, r.w, r.h, radius) : nvgRect(vg, r.x, r.y, r.w, r.h); |
|
||||||
if (color) nvgFillColor(vg, *color); |
|
||||||
if (paint) nvgFillPaint(vg, *paint); |
|
||||||
nvgFill(vg); |
|
||||||
} |
|
||||||
void ui_fill_rect(NVGcontext *vg, const Rect &r, const NVGcolor &color, float radius) { |
|
||||||
fill_rect(vg, r, &color, nullptr, radius); |
|
||||||
} |
|
||||||
void ui_fill_rect(NVGcontext *vg, const Rect &r, const NVGpaint &paint, float radius) { |
|
||||||
fill_rect(vg, r, nullptr, &paint, radius); |
|
||||||
} |
|
||||||
|
|
||||||
void ui_nvg_init(UIState *s) { |
|
||||||
// on EON, we enable MSAA
|
|
||||||
s->vg = Hardware::EON() ? nvgCreate(0) : nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); |
|
||||||
assert(s->vg); |
|
||||||
|
|
||||||
// init fonts
|
|
||||||
std::pair<const char *, const char *> fonts[] = { |
|
||||||
{"sans-regular", "../assets/fonts/opensans_regular.ttf"}, |
|
||||||
{"sans-semibold", "../assets/fonts/opensans_semibold.ttf"}, |
|
||||||
{"sans-bold", "../assets/fonts/opensans_bold.ttf"}, |
|
||||||
}; |
|
||||||
for (auto [name, file] : fonts) { |
|
||||||
int font_id = nvgCreateFont(s->vg, name, file); |
|
||||||
assert(font_id >= 0); |
|
||||||
} |
|
||||||
|
|
||||||
// init images
|
|
||||||
std::vector<std::pair<const char *, const char *>> images = { |
|
||||||
{"wheel", "../assets/img_chffr_wheel.png"}, |
|
||||||
{"driver_face", "../assets/img_driver_face.png"}, |
|
||||||
}; |
|
||||||
for (auto [name, file] : images) { |
|
||||||
s->images[name] = nvgCreateImage(s->vg, file, 1); |
|
||||||
assert(s->images[name] != 0); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void ui_resize(UIState *s, int width, int height) { |
|
||||||
s->fb_w = width; |
|
||||||
s->fb_h = height; |
|
||||||
|
|
||||||
auto intrinsic_matrix = s->wide_camera ? ecam_intrinsic_matrix : fcam_intrinsic_matrix; |
|
||||||
float zoom = ZOOM / intrinsic_matrix.v[0]; |
|
||||||
if (s->wide_camera) { |
|
||||||
zoom *= 0.5; |
|
||||||
} |
|
||||||
|
|
||||||
// Apply transformation such that video pixel coordinates match video
|
|
||||||
// 1) Put (0, 0) in the middle of the video
|
|
||||||
// 2) Apply same scaling as video
|
|
||||||
// 3) Put (0, 0) in top left corner of video
|
|
||||||
s->car_space_transform.reset(); |
|
||||||
s->car_space_transform.translate(width / 2, height / 2 + y_offset) |
|
||||||
.scale(zoom, zoom) |
|
||||||
.translate(-intrinsic_matrix.v[2], -intrinsic_matrix.v[5]); |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
#pragma once |
|
||||||
|
|
||||||
#include "selfdrive/ui/ui.h" |
|
||||||
|
|
||||||
void ui_draw(UIState *s, int w, int h); |
|
||||||
void ui_draw_image(const UIState *s, const Rect &r, const char *name, float alpha); |
|
||||||
void ui_draw_rect(NVGcontext *vg, const Rect &r, NVGcolor color, int width, float radius = 0); |
|
||||||
void ui_fill_rect(NVGcontext *vg, const Rect &r, const NVGpaint &paint, float radius = 0); |
|
||||||
void ui_fill_rect(NVGcontext *vg, const Rect &r, const NVGcolor &color, float radius = 0); |
|
||||||
void ui_nvg_init(UIState *s); |
|
||||||
void ui_resize(UIState *s, int width, int height); |
|
||||||
void ui_update_params(UIState *s); |
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,681 +0,0 @@ |
|||||||
//
|
|
||||||
// Copyright (c) 2013 Mikko Mononen memon@inside.org
|
|
||||||
//
|
|
||||||
// This software is provided 'as-is', without any express or implied
|
|
||||||
// warranty. In no event will the authors be held liable for any damages
|
|
||||||
// arising from the use of this software.
|
|
||||||
// Permission is granted to anyone to use this software for any purpose,
|
|
||||||
// including commercial applications, and to alter it and redistribute it
|
|
||||||
// freely, subject to the following restrictions:
|
|
||||||
// 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
// claim that you wrote the original software. If you use this software
|
|
||||||
// in a product, an acknowledgment in the product documentation would be
|
|
||||||
// appreciated but is not required.
|
|
||||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
// misrepresented as being the original software.
|
|
||||||
// 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef NANOVG_H |
|
||||||
#define NANOVG_H |
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
extern "C" { |
|
||||||
#endif |
|
||||||
|
|
||||||
#define NVG_PI 3.14159265358979323846264338327f |
|
||||||
|
|
||||||
#ifdef _MSC_VER |
|
||||||
#pragma warning(push) |
|
||||||
#pragma warning(disable: 4201) // nonstandard extension used : nameless struct/union
|
|
||||||
#endif |
|
||||||
|
|
||||||
typedef struct NVGcontext NVGcontext; |
|
||||||
|
|
||||||
struct NVGcolor { |
|
||||||
union { |
|
||||||
float rgba[4]; |
|
||||||
struct { |
|
||||||
float r,g,b,a; |
|
||||||
}; |
|
||||||
}; |
|
||||||
}; |
|
||||||
typedef struct NVGcolor NVGcolor; |
|
||||||
|
|
||||||
struct NVGpaint { |
|
||||||
float xform[6]; |
|
||||||
float extent[2]; |
|
||||||
float radius; |
|
||||||
float feather; |
|
||||||
NVGcolor innerColor; |
|
||||||
NVGcolor outerColor; |
|
||||||
int image; |
|
||||||
}; |
|
||||||
typedef struct NVGpaint NVGpaint; |
|
||||||
|
|
||||||
enum NVGwinding { |
|
||||||
NVG_CCW = 1, // Winding for solid shapes
|
|
||||||
NVG_CW = 2, // Winding for holes
|
|
||||||
}; |
|
||||||
|
|
||||||
enum NVGsolidity { |
|
||||||
NVG_SOLID = 1, // CCW
|
|
||||||
NVG_HOLE = 2, // CW
|
|
||||||
}; |
|
||||||
|
|
||||||
enum NVGlineCap { |
|
||||||
NVG_BUTT, |
|
||||||
NVG_ROUND, |
|
||||||
NVG_SQUARE, |
|
||||||
NVG_BEVEL, |
|
||||||
NVG_MITER, |
|
||||||
}; |
|
||||||
|
|
||||||
enum NVGalign { |
|
||||||
// Horizontal align
|
|
||||||
NVG_ALIGN_LEFT = 1<<0, // Default, align text horizontally to left.
|
|
||||||
NVG_ALIGN_CENTER = 1<<1, // Align text horizontally to center.
|
|
||||||
NVG_ALIGN_RIGHT = 1<<2, // Align text horizontally to right.
|
|
||||||
// Vertical align
|
|
||||||
NVG_ALIGN_TOP = 1<<3, // Align text vertically to top.
|
|
||||||
NVG_ALIGN_MIDDLE = 1<<4, // Align text vertically to middle.
|
|
||||||
NVG_ALIGN_BOTTOM = 1<<5, // Align text vertically to bottom.
|
|
||||||
NVG_ALIGN_BASELINE = 1<<6, // Default, align text vertically to baseline.
|
|
||||||
}; |
|
||||||
|
|
||||||
enum NVGblendFactor { |
|
||||||
NVG_ZERO = 1<<0, |
|
||||||
NVG_ONE = 1<<1, |
|
||||||
NVG_SRC_COLOR = 1<<2, |
|
||||||
NVG_ONE_MINUS_SRC_COLOR = 1<<3, |
|
||||||
NVG_DST_COLOR = 1<<4, |
|
||||||
NVG_ONE_MINUS_DST_COLOR = 1<<5, |
|
||||||
NVG_SRC_ALPHA = 1<<6, |
|
||||||
NVG_ONE_MINUS_SRC_ALPHA = 1<<7, |
|
||||||
NVG_DST_ALPHA = 1<<8, |
|
||||||
NVG_ONE_MINUS_DST_ALPHA = 1<<9, |
|
||||||
NVG_SRC_ALPHA_SATURATE = 1<<10, |
|
||||||
}; |
|
||||||
|
|
||||||
enum NVGcompositeOperation { |
|
||||||
NVG_SOURCE_OVER, |
|
||||||
NVG_SOURCE_IN, |
|
||||||
NVG_SOURCE_OUT, |
|
||||||
NVG_ATOP, |
|
||||||
NVG_DESTINATION_OVER, |
|
||||||
NVG_DESTINATION_IN, |
|
||||||
NVG_DESTINATION_OUT, |
|
||||||
NVG_DESTINATION_ATOP, |
|
||||||
NVG_LIGHTER, |
|
||||||
NVG_COPY, |
|
||||||
NVG_XOR, |
|
||||||
}; |
|
||||||
|
|
||||||
struct NVGcompositeOperationState { |
|
||||||
int srcRGB; |
|
||||||
int dstRGB; |
|
||||||
int srcAlpha; |
|
||||||
int dstAlpha; |
|
||||||
}; |
|
||||||
typedef struct NVGcompositeOperationState NVGcompositeOperationState; |
|
||||||
|
|
||||||
struct NVGglyphPosition { |
|
||||||
const char* str; // Position of the glyph in the input string.
|
|
||||||
float x; // The x-coordinate of the logical glyph position.
|
|
||||||
float minx, maxx; // The bounds of the glyph shape.
|
|
||||||
}; |
|
||||||
typedef struct NVGglyphPosition NVGglyphPosition; |
|
||||||
|
|
||||||
struct NVGtextRow { |
|
||||||
const char* start; // Pointer to the input text where the row starts.
|
|
||||||
const char* end; // Pointer to the input text where the row ends (one past the last character).
|
|
||||||
const char* next; // Pointer to the beginning of the next row.
|
|
||||||
float width; // Logical width of the row.
|
|
||||||
float minx, maxx; // Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending.
|
|
||||||
}; |
|
||||||
typedef struct NVGtextRow NVGtextRow; |
|
||||||
|
|
||||||
enum NVGimageFlags { |
|
||||||
NVG_IMAGE_GENERATE_MIPMAPS = 1<<0, // Generate mipmaps during creation of the image.
|
|
||||||
NVG_IMAGE_REPEATX = 1<<1, // Repeat image in X direction.
|
|
||||||
NVG_IMAGE_REPEATY = 1<<2, // Repeat image in Y direction.
|
|
||||||
NVG_IMAGE_FLIPY = 1<<3, // Flips (inverses) image in Y direction when rendered.
|
|
||||||
NVG_IMAGE_PREMULTIPLIED = 1<<4, // Image data has premultiplied alpha.
|
|
||||||
}; |
|
||||||
|
|
||||||
// Begin drawing a new frame
|
|
||||||
// Calls to nanovg drawing API should be wrapped in nvgBeginFrame() & nvgEndFrame()
|
|
||||||
// nvgBeginFrame() defines the size of the window to render to in relation currently
|
|
||||||
// set viewport (i.e. glViewport on GL backends). Device pixel ration allows to
|
|
||||||
// control the rendering on Hi-DPI devices.
|
|
||||||
// For example, GLFW returns two dimension for an opened window: window size and
|
|
||||||
// frame buffer size. In that case you would set windowWidth/Height to the window size
|
|
||||||
// devicePixelRatio to: frameBufferWidth / windowWidth.
|
|
||||||
void nvgBeginFrame(NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio); |
|
||||||
|
|
||||||
// Cancels drawing the current frame.
|
|
||||||
void nvgCancelFrame(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Ends drawing flushing remaining render state.
|
|
||||||
void nvgEndFrame(NVGcontext* ctx); |
|
||||||
|
|
||||||
//
|
|
||||||
// Composite operation
|
|
||||||
//
|
|
||||||
// The composite operations in NanoVG are modeled after HTML Canvas API, and
|
|
||||||
// the blend func is based on OpenGL (see corresponding manuals for more info).
|
|
||||||
// The colors in the blending state have premultiplied alpha.
|
|
||||||
|
|
||||||
// Sets the composite operation. The op parameter should be one of NVGcompositeOperation.
|
|
||||||
void nvgGlobalCompositeOperation(NVGcontext* ctx, int op); |
|
||||||
|
|
||||||
// Sets the composite operation with custom pixel arithmetic. The parameters should be one of NVGblendFactor.
|
|
||||||
void nvgGlobalCompositeBlendFunc(NVGcontext* ctx, int sfactor, int dfactor); |
|
||||||
|
|
||||||
// Sets the composite operation with custom pixel arithmetic for RGB and alpha components separately. The parameters should be one of NVGblendFactor.
|
|
||||||
void nvgGlobalCompositeBlendFuncSeparate(NVGcontext* ctx, int srcRGB, int dstRGB, int srcAlpha, int dstAlpha); |
|
||||||
|
|
||||||
//
|
|
||||||
// Color utils
|
|
||||||
//
|
|
||||||
// Colors in NanoVG are stored as unsigned ints in ABGR format.
|
|
||||||
|
|
||||||
// Returns a color value from red, green, blue values. Alpha will be set to 255 (1.0f).
|
|
||||||
NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b); |
|
||||||
|
|
||||||
// Returns a color value from red, green, blue values. Alpha will be set to 1.0f.
|
|
||||||
NVGcolor nvgRGBf(float r, float g, float b); |
|
||||||
|
|
||||||
|
|
||||||
// Returns a color value from red, green, blue and alpha values.
|
|
||||||
NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a); |
|
||||||
|
|
||||||
// Returns a color value from red, green, blue and alpha values.
|
|
||||||
NVGcolor nvgRGBAf(float r, float g, float b, float a); |
|
||||||
|
|
||||||
|
|
||||||
// Linearly interpolates from color c0 to c1, and returns resulting color value.
|
|
||||||
NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u); |
|
||||||
|
|
||||||
// Sets transparency of a color value.
|
|
||||||
NVGcolor nvgTransRGBA(NVGcolor c0, unsigned char a); |
|
||||||
|
|
||||||
// Sets transparency of a color value.
|
|
||||||
NVGcolor nvgTransRGBAf(NVGcolor c0, float a); |
|
||||||
|
|
||||||
// Returns color value specified by hue, saturation and lightness.
|
|
||||||
// HSL values are all in range [0..1], alpha will be set to 255.
|
|
||||||
NVGcolor nvgHSL(float h, float s, float l); |
|
||||||
|
|
||||||
// Returns color value specified by hue, saturation and lightness and alpha.
|
|
||||||
// HSL values are all in range [0..1], alpha in range [0..255]
|
|
||||||
NVGcolor nvgHSLA(float h, float s, float l, unsigned char a); |
|
||||||
|
|
||||||
//
|
|
||||||
// State Handling
|
|
||||||
//
|
|
||||||
// NanoVG contains state which represents how paths will be rendered.
|
|
||||||
// The state contains transform, fill and stroke styles, text and font styles,
|
|
||||||
// and scissor clipping.
|
|
||||||
|
|
||||||
// Pushes and saves the current render state into a state stack.
|
|
||||||
// A matching nvgRestore() must be used to restore the state.
|
|
||||||
void nvgSave(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Pops and restores current render state.
|
|
||||||
void nvgRestore(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Resets current render state to default values. Does not affect the render state stack.
|
|
||||||
void nvgReset(NVGcontext* ctx); |
|
||||||
|
|
||||||
//
|
|
||||||
// Render styles
|
|
||||||
//
|
|
||||||
// Fill and stroke render style can be either a solid color or a paint which is a gradient or a pattern.
|
|
||||||
// Solid color is simply defined as a color value, different kinds of paints can be created
|
|
||||||
// using nvgLinearGradient(), nvgBoxGradient(), nvgRadialGradient() and nvgImagePattern().
|
|
||||||
//
|
|
||||||
// Current render style can be saved and restored using nvgSave() and nvgRestore().
|
|
||||||
|
|
||||||
// Sets current stroke style to a solid color.
|
|
||||||
void nvgStrokeColor(NVGcontext* ctx, NVGcolor color); |
|
||||||
|
|
||||||
// Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
|
|
||||||
void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint); |
|
||||||
|
|
||||||
// Sets current fill style to a solid color.
|
|
||||||
void nvgFillColor(NVGcontext* ctx, NVGcolor color); |
|
||||||
|
|
||||||
// Sets current fill style to a paint, which can be a one of the gradients or a pattern.
|
|
||||||
void nvgFillPaint(NVGcontext* ctx, NVGpaint paint); |
|
||||||
|
|
||||||
// Sets the miter limit of the stroke style.
|
|
||||||
// Miter limit controls when a sharp corner is beveled.
|
|
||||||
void nvgMiterLimit(NVGcontext* ctx, float limit); |
|
||||||
|
|
||||||
// Sets the stroke width of the stroke style.
|
|
||||||
void nvgStrokeWidth(NVGcontext* ctx, float size); |
|
||||||
|
|
||||||
// Sets how the end of the line (cap) is drawn,
|
|
||||||
// Can be one of: NVG_BUTT (default), NVG_ROUND, NVG_SQUARE.
|
|
||||||
void nvgLineCap(NVGcontext* ctx, int cap); |
|
||||||
|
|
||||||
// Sets how sharp path corners are drawn.
|
|
||||||
// Can be one of NVG_MITER (default), NVG_ROUND, NVG_BEVEL.
|
|
||||||
void nvgLineJoin(NVGcontext* ctx, int join); |
|
||||||
|
|
||||||
// Sets the transparency applied to all rendered shapes.
|
|
||||||
// Already transparent paths will get proportionally more transparent as well.
|
|
||||||
void nvgGlobalAlpha(NVGcontext* ctx, float alpha); |
|
||||||
|
|
||||||
//
|
|
||||||
// Transforms
|
|
||||||
//
|
|
||||||
// The paths, gradients, patterns and scissor region are transformed by an transformation
|
|
||||||
// matrix at the time when they are passed to the API.
|
|
||||||
// The current transformation matrix is a affine matrix:
|
|
||||||
// [sx kx tx]
|
|
||||||
// [ky sy ty]
|
|
||||||
// [ 0 0 1]
|
|
||||||
// Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
|
|
||||||
// The last row is assumed to be 0,0,1 and is not stored.
|
|
||||||
//
|
|
||||||
// Apart from nvgResetTransform(), each transformation function first creates
|
|
||||||
// specific transformation matrix and pre-multiplies the current transformation by it.
|
|
||||||
//
|
|
||||||
// Current coordinate system (transformation) can be saved and restored using nvgSave() and nvgRestore().
|
|
||||||
|
|
||||||
// Resets current transform to a identity matrix.
|
|
||||||
void nvgResetTransform(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Premultiplies current coordinate system by specified matrix.
|
|
||||||
// The parameters are interpreted as matrix as follows:
|
|
||||||
// [a c e]
|
|
||||||
// [b d f]
|
|
||||||
// [0 0 1]
|
|
||||||
void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f); |
|
||||||
|
|
||||||
// Translates current coordinate system.
|
|
||||||
void nvgTranslate(NVGcontext* ctx, float x, float y); |
|
||||||
|
|
||||||
// Rotates current coordinate system. Angle is specified in radians.
|
|
||||||
void nvgRotate(NVGcontext* ctx, float angle); |
|
||||||
|
|
||||||
// Skews the current coordinate system along X axis. Angle is specified in radians.
|
|
||||||
void nvgSkewX(NVGcontext* ctx, float angle); |
|
||||||
|
|
||||||
// Skews the current coordinate system along Y axis. Angle is specified in radians.
|
|
||||||
void nvgSkewY(NVGcontext* ctx, float angle); |
|
||||||
|
|
||||||
// Scales the current coordinate system.
|
|
||||||
void nvgScale(NVGcontext* ctx, float x, float y); |
|
||||||
|
|
||||||
// Stores the top part (a-f) of the current transformation matrix in to the specified buffer.
|
|
||||||
// [a c e]
|
|
||||||
// [b d f]
|
|
||||||
// [0 0 1]
|
|
||||||
// There should be space for 6 floats in the return buffer for the values a-f.
|
|
||||||
void nvgCurrentTransform(NVGcontext* ctx, float* xform); |
|
||||||
|
|
||||||
|
|
||||||
// The following functions can be used to make calculations on 2x3 transformation matrices.
|
|
||||||
// A 2x3 matrix is represented as float[6].
|
|
||||||
|
|
||||||
// Sets the transform to identity matrix.
|
|
||||||
void nvgTransformIdentity(float* dst); |
|
||||||
|
|
||||||
// Sets the transform to translation matrix matrix.
|
|
||||||
void nvgTransformTranslate(float* dst, float tx, float ty); |
|
||||||
|
|
||||||
// Sets the transform to scale matrix.
|
|
||||||
void nvgTransformScale(float* dst, float sx, float sy); |
|
||||||
|
|
||||||
// Sets the transform to rotate matrix. Angle is specified in radians.
|
|
||||||
void nvgTransformRotate(float* dst, float a); |
|
||||||
|
|
||||||
// Sets the transform to skew-x matrix. Angle is specified in radians.
|
|
||||||
void nvgTransformSkewX(float* dst, float a); |
|
||||||
|
|
||||||
// Sets the transform to skew-y matrix. Angle is specified in radians.
|
|
||||||
void nvgTransformSkewY(float* dst, float a); |
|
||||||
|
|
||||||
// Sets the transform to the result of multiplication of two transforms, of A = A*B.
|
|
||||||
void nvgTransformMultiply(float* dst, const float* src); |
|
||||||
|
|
||||||
// Sets the transform to the result of multiplication of two transforms, of A = B*A.
|
|
||||||
void nvgTransformPremultiply(float* dst, const float* src); |
|
||||||
|
|
||||||
// Sets the destination to inverse of specified transform.
|
|
||||||
// Returns 1 if the inverse could be calculated, else 0.
|
|
||||||
int nvgTransformInverse(float* dst, const float* src); |
|
||||||
|
|
||||||
// Transform a point by given transform.
|
|
||||||
void nvgTransformPoint(float* dstx, float* dsty, const float* xform, float srcx, float srcy); |
|
||||||
|
|
||||||
// Converts degrees to radians and vice versa.
|
|
||||||
float nvgDegToRad(float deg); |
|
||||||
float nvgRadToDeg(float rad); |
|
||||||
|
|
||||||
//
|
|
||||||
// Images
|
|
||||||
//
|
|
||||||
// NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
|
|
||||||
// In addition you can upload your own image. The image loading is provided by stb_image.
|
|
||||||
// The parameter imageFlags is combination of flags defined in NVGimageFlags.
|
|
||||||
|
|
||||||
// Creates image by loading it from the disk from specified file name.
|
|
||||||
// Returns handle to the image.
|
|
||||||
int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags); |
|
||||||
|
|
||||||
// Creates image by loading it from the specified chunk of memory.
|
|
||||||
// Returns handle to the image.
|
|
||||||
int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata); |
|
||||||
|
|
||||||
// Creates image from specified image data.
|
|
||||||
// Returns handle to the image.
|
|
||||||
int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data); |
|
||||||
|
|
||||||
// Updates image data specified by image handle.
|
|
||||||
void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data); |
|
||||||
|
|
||||||
// Returns the dimensions of a created image.
|
|
||||||
void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h); |
|
||||||
|
|
||||||
// Deletes created image.
|
|
||||||
void nvgDeleteImage(NVGcontext* ctx, int image); |
|
||||||
|
|
||||||
//
|
|
||||||
// Paints
|
|
||||||
//
|
|
||||||
// NanoVG supports four types of paints: linear gradient, box gradient, radial gradient and image pattern.
|
|
||||||
// These can be used as paints for strokes and fills.
|
|
||||||
|
|
||||||
// Creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
|
|
||||||
// of the linear gradient, icol specifies the start color and ocol the end color.
|
|
||||||
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
|
|
||||||
NVGpaint nvgLinearGradient(NVGcontext* ctx, float sx, float sy, float ex, float ey, |
|
||||||
NVGcolor icol, NVGcolor ocol); |
|
||||||
|
|
||||||
// Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
|
|
||||||
// drop shadows or highlights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
|
|
||||||
// (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
|
|
||||||
// the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
|
|
||||||
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
|
|
||||||
NVGpaint nvgBoxGradient(NVGcontext* ctx, float x, float y, float w, float h, |
|
||||||
float r, float f, NVGcolor icol, NVGcolor ocol); |
|
||||||
|
|
||||||
// Creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
|
|
||||||
// the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
|
|
||||||
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
|
|
||||||
NVGpaint nvgRadialGradient(NVGcontext* ctx, float cx, float cy, float inr, float outr, |
|
||||||
NVGcolor icol, NVGcolor ocol); |
|
||||||
|
|
||||||
// Creates and returns an image patter. Parameters (ox,oy) specify the left-top location of the image pattern,
|
|
||||||
// (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render.
|
|
||||||
// The gradient is transformed by the current transform when it is passed to nvgFillPaint() or nvgStrokePaint().
|
|
||||||
NVGpaint nvgImagePattern(NVGcontext* ctx, float ox, float oy, float ex, float ey, |
|
||||||
float angle, int image, float alpha); |
|
||||||
|
|
||||||
//
|
|
||||||
// Scissoring
|
|
||||||
//
|
|
||||||
// Scissoring allows you to clip the rendering into a rectangle. This is useful for various
|
|
||||||
// user interface cases like rendering a text edit or a timeline.
|
|
||||||
|
|
||||||
// Sets the current scissor rectangle.
|
|
||||||
// The scissor rectangle is transformed by the current transform.
|
|
||||||
void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h); |
|
||||||
|
|
||||||
// Intersects current scissor rectangle with the specified rectangle.
|
|
||||||
// The scissor rectangle is transformed by the current transform.
|
|
||||||
// Note: in case the rotation of previous scissor rect differs from
|
|
||||||
// the current one, the intersection will be done between the specified
|
|
||||||
// rectangle and the previous scissor rectangle transformed in the current
|
|
||||||
// transform space. The resulting shape is always rectangle.
|
|
||||||
void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h); |
|
||||||
|
|
||||||
// Reset and disables scissoring.
|
|
||||||
void nvgResetScissor(NVGcontext* ctx); |
|
||||||
|
|
||||||
//
|
|
||||||
// Paths
|
|
||||||
//
|
|
||||||
// Drawing a new shape starts with nvgBeginPath(), it clears all the currently defined paths.
|
|
||||||
// Then you define one or more paths and sub-paths which describe the shape. The are functions
|
|
||||||
// to draw common shapes like rectangles and circles, and lower level step-by-step functions,
|
|
||||||
// which allow to define a path curve by curve.
|
|
||||||
//
|
|
||||||
// NanoVG uses even-odd fill rule to draw the shapes. Solid shapes should have counter clockwise
|
|
||||||
// winding and holes should have counter clockwise order. To specify winding of a path you can
|
|
||||||
// call nvgPathWinding(). This is useful especially for the common shapes, which are drawn CCW.
|
|
||||||
//
|
|
||||||
// Finally you can fill the path using current fill style by calling nvgFill(), and stroke it
|
|
||||||
// with current stroke style by calling nvgStroke().
|
|
||||||
//
|
|
||||||
// The curve segments and sub-paths are transformed by the current transform.
|
|
||||||
|
|
||||||
// Clears the current path and sub-paths.
|
|
||||||
void nvgBeginPath(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Starts new sub-path with specified point as first point.
|
|
||||||
void nvgMoveTo(NVGcontext* ctx, float x, float y); |
|
||||||
|
|
||||||
// Adds line segment from the last point in the path to the specified point.
|
|
||||||
void nvgLineTo(NVGcontext* ctx, float x, float y); |
|
||||||
|
|
||||||
// Adds cubic bezier segment from last point in the path via two control points to the specified point.
|
|
||||||
void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y); |
|
||||||
|
|
||||||
// Adds quadratic bezier segment from last point in the path via a control point to the specified point.
|
|
||||||
void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y); |
|
||||||
|
|
||||||
// Adds an arc segment at the corner defined by the last path point, and two specified points.
|
|
||||||
void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius); |
|
||||||
|
|
||||||
// Closes current sub-path with a line segment.
|
|
||||||
void nvgClosePath(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Sets the current sub-path winding, see NVGwinding and NVGsolidity.
|
|
||||||
void nvgPathWinding(NVGcontext* ctx, int dir); |
|
||||||
|
|
||||||
// Creates new circle arc shaped sub-path. The arc center is at cx,cy, the arc radius is r,
|
|
||||||
// and the arc is drawn from angle a0 to a1, and swept in direction dir (NVG_CCW, or NVG_CW).
|
|
||||||
// Angles are specified in radians.
|
|
||||||
void nvgArc(NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir); |
|
||||||
|
|
||||||
// Creates new rectangle shaped sub-path.
|
|
||||||
void nvgRect(NVGcontext* ctx, float x, float y, float w, float h); |
|
||||||
|
|
||||||
// Creates new rounded rectangle shaped sub-path.
|
|
||||||
void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r); |
|
||||||
|
|
||||||
// Creates new rounded rectangle shaped sub-path with varying radii for each corner.
|
|
||||||
void nvgRoundedRectVarying(NVGcontext* ctx, float x, float y, float w, float h, float radTopLeft, float radTopRight, float radBottomRight, float radBottomLeft); |
|
||||||
|
|
||||||
// Creates new ellipse shaped sub-path.
|
|
||||||
void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry); |
|
||||||
|
|
||||||
// Creates new circle shaped sub-path.
|
|
||||||
void nvgCircle(NVGcontext* ctx, float cx, float cy, float r); |
|
||||||
|
|
||||||
// Fills the current path with current fill style.
|
|
||||||
void nvgFill(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Fills the current path with current stroke style.
|
|
||||||
void nvgStroke(NVGcontext* ctx); |
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// Text
|
|
||||||
//
|
|
||||||
// NanoVG allows you to load .ttf files and use the font to render text.
|
|
||||||
//
|
|
||||||
// The appearance of the text can be defined by setting the current text style
|
|
||||||
// and by specifying the fill color. Common text and font settings such as
|
|
||||||
// font size, letter spacing and text align are supported. Font blur allows you
|
|
||||||
// to create simple text effects such as drop shadows.
|
|
||||||
//
|
|
||||||
// At render time the font face can be set based on the font handles or name.
|
|
||||||
//
|
|
||||||
// Font measure functions return values in local space, the calculations are
|
|
||||||
// carried in the same resolution as the final rendering. This is done because
|
|
||||||
// the text glyph positions are snapped to the nearest pixels sharp rendering.
|
|
||||||
//
|
|
||||||
// The local space means that values are not rotated or scale as per the current
|
|
||||||
// transformation. For example if you set font size to 12, which would mean that
|
|
||||||
// line height is 16, then regardless of the current scaling and rotation, the
|
|
||||||
// returned line height is always 16. Some measures may vary because of the scaling
|
|
||||||
// since aforementioned pixel snapping.
|
|
||||||
//
|
|
||||||
// While this may sound a little odd, the setup allows you to always render the
|
|
||||||
// same way regardless of scaling. I.e. following works regardless of scaling:
|
|
||||||
//
|
|
||||||
// const char* txt = "Text me up.";
|
|
||||||
// nvgTextBounds(vg, x,y, txt, NULL, bounds);
|
|
||||||
// nvgBeginPath(vg);
|
|
||||||
// nvgRoundedRect(vg, bounds[0],bounds[1], bounds[2]-bounds[0], bounds[3]-bounds[1]);
|
|
||||||
// nvgFill(vg);
|
|
||||||
//
|
|
||||||
// Note: currently only solid color fill is supported for text.
|
|
||||||
|
|
||||||
// Creates font by loading it from the disk from specified file name.
|
|
||||||
// Returns handle to the font.
|
|
||||||
int nvgCreateFont(NVGcontext* ctx, const char* name, const char* filename); |
|
||||||
|
|
||||||
// Creates font by loading it from the specified memory chunk.
|
|
||||||
// Returns handle to the font.
|
|
||||||
int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData); |
|
||||||
|
|
||||||
// Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
|
|
||||||
int nvgFindFont(NVGcontext* ctx, const char* name); |
|
||||||
|
|
||||||
// Adds a fallback font by handle.
|
|
||||||
int nvgAddFallbackFontId(NVGcontext* ctx, int baseFont, int fallbackFont); |
|
||||||
|
|
||||||
// Adds a fallback font by name.
|
|
||||||
int nvgAddFallbackFont(NVGcontext* ctx, const char* baseFont, const char* fallbackFont); |
|
||||||
|
|
||||||
// Sets the font size of current text style.
|
|
||||||
void nvgFontSize(NVGcontext* ctx, float size); |
|
||||||
|
|
||||||
// Sets the blur of current text style.
|
|
||||||
void nvgFontBlur(NVGcontext* ctx, float blur); |
|
||||||
|
|
||||||
// Sets the letter spacing of current text style.
|
|
||||||
void nvgTextLetterSpacing(NVGcontext* ctx, float spacing); |
|
||||||
|
|
||||||
// Sets the proportional line height of current text style. The line height is specified as multiple of font size.
|
|
||||||
void nvgTextLineHeight(NVGcontext* ctx, float lineHeight); |
|
||||||
|
|
||||||
// Sets the text align of current text style, see NVGalign for options.
|
|
||||||
void nvgTextAlign(NVGcontext* ctx, int align); |
|
||||||
|
|
||||||
// Sets the font face based on specified id of current text style.
|
|
||||||
void nvgFontFaceId(NVGcontext* ctx, int font); |
|
||||||
|
|
||||||
// Sets the font face based on specified name of current text style.
|
|
||||||
void nvgFontFace(NVGcontext* ctx, const char* font); |
|
||||||
|
|
||||||
// Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
|
|
||||||
float nvgText(NVGcontext* ctx, float x, float y, const char* string, const char* end); |
|
||||||
|
|
||||||
// Draws multi-line text string at specified location wrapped at the specified width. If end is specified only the sub-string up to the end is drawn.
|
|
||||||
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
|
|
||||||
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
|
|
||||||
void nvgTextBox(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end); |
|
||||||
|
|
||||||
// Measures the specified text string. Parameter bounds should be a pointer to float[4],
|
|
||||||
// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
|
|
||||||
// Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
|
|
||||||
// Measured values are returned in local coordinate space.
|
|
||||||
float nvgTextBounds(NVGcontext* ctx, float x, float y, const char* string, const char* end, float* bounds); |
|
||||||
|
|
||||||
// Measures the specified multi-text string. Parameter bounds should be a pointer to float[4],
|
|
||||||
// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
|
|
||||||
// Measured values are returned in local coordinate space.
|
|
||||||
void nvgTextBoxBounds(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds); |
|
||||||
|
|
||||||
// Calculates the glyph x positions of the specified text. If end is specified only the sub-string will be used.
|
|
||||||
// Measured values are returned in local coordinate space.
|
|
||||||
int nvgTextGlyphPositions(NVGcontext* ctx, float x, float y, const char* string, const char* end, NVGglyphPosition* positions, int maxPositions); |
|
||||||
|
|
||||||
// Returns the vertical metrics based on the current text style.
|
|
||||||
// Measured values are returned in local coordinate space.
|
|
||||||
void nvgTextMetrics(NVGcontext* ctx, float* ascender, float* descender, float* lineh); |
|
||||||
|
|
||||||
// Breaks the specified text into lines. If end is specified only the sub-string will be used.
|
|
||||||
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
|
|
||||||
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
|
|
||||||
int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows); |
|
||||||
|
|
||||||
//
|
|
||||||
// Internal Render API
|
|
||||||
//
|
|
||||||
enum NVGtexture { |
|
||||||
NVG_TEXTURE_ALPHA = 0x01, |
|
||||||
NVG_TEXTURE_RGBA = 0x02, |
|
||||||
}; |
|
||||||
|
|
||||||
struct NVGscissor { |
|
||||||
float xform[6]; |
|
||||||
float extent[2]; |
|
||||||
}; |
|
||||||
typedef struct NVGscissor NVGscissor; |
|
||||||
|
|
||||||
struct NVGvertex { |
|
||||||
float x,y,u,v; |
|
||||||
}; |
|
||||||
typedef struct NVGvertex NVGvertex; |
|
||||||
|
|
||||||
struct NVGpath { |
|
||||||
int first; |
|
||||||
int count; |
|
||||||
unsigned char closed; |
|
||||||
int nbevel; |
|
||||||
NVGvertex* fill; |
|
||||||
int nfill; |
|
||||||
NVGvertex* stroke; |
|
||||||
int nstroke; |
|
||||||
int winding; |
|
||||||
int convex; |
|
||||||
}; |
|
||||||
typedef struct NVGpath NVGpath; |
|
||||||
|
|
||||||
struct NVGparams { |
|
||||||
void* userPtr; |
|
||||||
int edgeAntiAlias; |
|
||||||
int (*renderCreate)(void* uptr); |
|
||||||
int (*renderCreateTexture)(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data); |
|
||||||
int (*renderDeleteTexture)(void* uptr, int image); |
|
||||||
int (*renderUpdateTexture)(void* uptr, int image, int x, int y, int w, int h, const unsigned char* data); |
|
||||||
int (*renderGetTextureSize)(void* uptr, int image, int* w, int* h); |
|
||||||
void (*renderViewport)(void* uptr, int width, int height, float devicePixelRatio); |
|
||||||
void (*renderCancel)(void* uptr); |
|
||||||
void (*renderFlush)(void* uptr, NVGcompositeOperationState compositeOperation); |
|
||||||
void (*renderFill)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths, int npaths); |
|
||||||
void (*renderStroke)(void* uptr, NVGpaint* paint, NVGscissor* scissor, float fringe, float strokeWidth, const NVGpath* paths, int npaths); |
|
||||||
void (*renderTriangles)(void* uptr, NVGpaint* paint, NVGscissor* scissor, const NVGvertex* verts, int nverts); |
|
||||||
void (*renderDelete)(void* uptr); |
|
||||||
}; |
|
||||||
typedef struct NVGparams NVGparams; |
|
||||||
|
|
||||||
// Constructor and destructor, called by the render back-end.
|
|
||||||
NVGcontext* nvgCreateInternal(NVGparams* params); |
|
||||||
void nvgDeleteInternal(NVGcontext* ctx); |
|
||||||
|
|
||||||
NVGparams* nvgInternalParams(NVGcontext* ctx); |
|
||||||
|
|
||||||
// Debug function to dump cached path data.
|
|
||||||
void nvgDebugDumpPathCache(NVGcontext* ctx); |
|
||||||
|
|
||||||
#ifdef _MSC_VER |
|
||||||
#pragma warning(pop) |
|
||||||
#endif |
|
||||||
|
|
||||||
#define NVG_NOTUSED(v) for (;;) { (void)(1 ? (void)0 : ( (void)(v) ) ); break; } |
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
} |
|
||||||
#endif |
|
||||||
|
|
||||||
#endif // NANOVG_H
|
|
File diff suppressed because it is too large
Load Diff
@ -1,143 +0,0 @@ |
|||||||
//
|
|
||||||
// Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
|
|
||||||
//
|
|
||||||
// This software is provided 'as-is', without any express or implied
|
|
||||||
// warranty. In no event will the authors be held liable for any damages
|
|
||||||
// arising from the use of this software.
|
|
||||||
// Permission is granted to anyone to use this software for any purpose,
|
|
||||||
// including commercial applications, and to alter it and redistribute it
|
|
||||||
// freely, subject to the following restrictions:
|
|
||||||
// 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
// claim that you wrote the original software. If you use this software
|
|
||||||
// in a product, an acknowledgment in the product documentation would be
|
|
||||||
// appreciated but is not required.
|
|
||||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
// misrepresented as being the original software.
|
|
||||||
// 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
//
|
|
||||||
#ifndef NANOVG_GL_UTILS_H |
|
||||||
#define NANOVG_GL_UTILS_H |
|
||||||
|
|
||||||
struct NVGLUframebuffer { |
|
||||||
NVGcontext* ctx; |
|
||||||
GLuint fbo; |
|
||||||
GLuint rbo; |
|
||||||
GLuint texture; |
|
||||||
int image; |
|
||||||
}; |
|
||||||
typedef struct NVGLUframebuffer NVGLUframebuffer; |
|
||||||
|
|
||||||
// Helper function to create GL frame buffer to render to.
|
|
||||||
void nvgluBindFramebuffer(NVGLUframebuffer* fb); |
|
||||||
NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags); |
|
||||||
void nvgluDeleteFramebuffer(NVGLUframebuffer* fb); |
|
||||||
|
|
||||||
#endif // NANOVG_GL_UTILS_H
|
|
||||||
|
|
||||||
#ifdef NANOVG_GL_IMPLEMENTATION |
|
||||||
|
|
||||||
#if defined(NANOVG_GL3) || defined(NANOVG_GLES2) || defined(NANOVG_GLES3) |
|
||||||
// FBO is core in OpenGL 3>.
|
|
||||||
# define NANOVG_FBO_VALID 1 |
|
||||||
#elif defined(NANOVG_GL2) |
|
||||||
// On OS X including glext defines FBO on GL2 too.
|
|
||||||
# ifdef __APPLE__ |
|
||||||
# include <OpenGL/glext.h> |
|
||||||
# define NANOVG_FBO_VALID 1 |
|
||||||
# endif |
|
||||||
#endif |
|
||||||
|
|
||||||
static GLint defaultFBO = -1; |
|
||||||
|
|
||||||
NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags) |
|
||||||
{ |
|
||||||
#ifdef NANOVG_FBO_VALID |
|
||||||
GLint localDefaultFBO; |
|
||||||
GLint defaultRBO; |
|
||||||
NVGLUframebuffer* fb = NULL; |
|
||||||
|
|
||||||
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &localDefaultFBO); |
|
||||||
glGetIntegerv(GL_RENDERBUFFER_BINDING, &defaultRBO); |
|
||||||
|
|
||||||
fb = (NVGLUframebuffer*)malloc(sizeof(NVGLUframebuffer)); |
|
||||||
if (fb == NULL) goto error; |
|
||||||
memset(fb, 0, sizeof(NVGLUframebuffer)); |
|
||||||
|
|
||||||
fb->image = nvgCreateImageRGBA(ctx, w, h, imageFlags | NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED, NULL); |
|
||||||
|
|
||||||
#if defined NANOVG_GL2 |
|
||||||
fb->texture = nvglImageHandleGL2(ctx, fb->image); |
|
||||||
#elif defined NANOVG_GL3 |
|
||||||
fb->texture = nvglImageHandleGL3(ctx, fb->image); |
|
||||||
#elif defined NANOVG_GLES2 |
|
||||||
fb->texture = nvglImageHandleGLES2(ctx, fb->image); |
|
||||||
#elif defined NANOVG_GLES3 |
|
||||||
fb->texture = nvglImageHandleGLES3(ctx, fb->image); |
|
||||||
#endif |
|
||||||
|
|
||||||
fb->ctx = ctx; |
|
||||||
|
|
||||||
// frame buffer object
|
|
||||||
glGenFramebuffers(1, &fb->fbo); |
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fb->fbo); |
|
||||||
|
|
||||||
// render buffer object
|
|
||||||
glGenRenderbuffers(1, &fb->rbo); |
|
||||||
glBindRenderbuffer(GL_RENDERBUFFER, fb->rbo); |
|
||||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h); |
|
||||||
|
|
||||||
// combine all
|
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->texture, 0); |
|
||||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rbo); |
|
||||||
|
|
||||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) goto error; |
|
||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, localDefaultFBO); |
|
||||||
glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO); |
|
||||||
return fb; |
|
||||||
error: |
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, localDefaultFBO); |
|
||||||
glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO); |
|
||||||
nvgluDeleteFramebuffer(fb); |
|
||||||
return NULL; |
|
||||||
#else |
|
||||||
NVG_NOTUSED(ctx); |
|
||||||
NVG_NOTUSED(w); |
|
||||||
NVG_NOTUSED(h); |
|
||||||
NVG_NOTUSED(imageFlags); |
|
||||||
return NULL; |
|
||||||
#endif |
|
||||||
} |
|
||||||
|
|
||||||
void nvgluBindFramebuffer(NVGLUframebuffer* fb) |
|
||||||
{ |
|
||||||
#ifdef NANOVG_FBO_VALID |
|
||||||
if (defaultFBO == -1) glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO); |
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : defaultFBO); |
|
||||||
#else |
|
||||||
NVG_NOTUSED(fb); |
|
||||||
#endif |
|
||||||
} |
|
||||||
|
|
||||||
void nvgluDeleteFramebuffer(NVGLUframebuffer* fb) |
|
||||||
{ |
|
||||||
#ifdef NANOVG_FBO_VALID |
|
||||||
if (fb == NULL) return; |
|
||||||
if (fb->fbo != 0) |
|
||||||
glDeleteFramebuffers(1, &fb->fbo); |
|
||||||
if (fb->rbo != 0) |
|
||||||
glDeleteRenderbuffers(1, &fb->rbo); |
|
||||||
if (fb->image >= 0) |
|
||||||
nvgDeleteImage(fb->ctx, fb->image); |
|
||||||
fb->ctx = NULL; |
|
||||||
fb->fbo = 0; |
|
||||||
fb->rbo = 0; |
|
||||||
fb->texture = 0; |
|
||||||
fb->image = -1; |
|
||||||
free(fb); |
|
||||||
#else |
|
||||||
NVG_NOTUSED(fb); |
|
||||||
#endif |
|
||||||
} |
|
||||||
|
|
||||||
#endif // NANOVG_GL_IMPLEMENTATION
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue