example1.c
This file
Path
3rd-party/nanosvg/example/example1.c
258 lines · 6.0 KiB · C
Last changed by
Not known.
History of this fileTake it
1//2// Copyright (c) 2013 Mikko Mononen memon@inside.org3//4// This software is provided 'as-is', without any express or implied5// warranty. In no event will the authors be held liable for any damages6// arising from the use of this software.7// Permission is granted to anyone to use this software for any purpose,8// including commercial applications, and to alter it and redistribute it9// freely, subject to the following restrictions:10// 1. The origin of this software must not be misrepresented; you must not11// claim that you wrote the original software. If you use this software12// in a product, an acknowledgment in the product documentation would be13// appreciated but is not required.14// 2. Altered source versions must be plainly marked as such, and must not be15// misrepresented as being the original software.16// 3. This notice may not be removed or altered from any source distribution.17//1819#include <stdio.h>20#include <string.h>21#include <float.h>22#include <GLFW/glfw3.h>2324#define NANOSVG_IMPLEMENTATION25#include "nanosvg.h"2627NSVGimage* g_image = NULL;2829static unsigned char bgColor[4] = {205,202,200,255};30static unsigned char lineColor[4] = {0,160,192,255};3132static float distPtSeg(float x, float y, float px, float py, float qx, float qy)33{34 float pqx, pqy, dx, dy, d, t;35 pqx = qx-px;36 pqy = qy-py;37 dx = x-px;38 dy = y-py;39 d = pqx*pqx + pqy*pqy;40 t = pqx*dx + pqy*dy;41 if (d > 0) t /= d;42 if (t < 0) t = 0;43 else if (t > 1) t = 1;44 dx = px + t*pqx - x;45 dy = py + t*pqy - y;46 return dx*dx + dy*dy;47}4849static void cubicBez(float x1, float y1, float x2, float y2,50 float x3, float y3, float x4, float y4,51 float tol, int level)52{53 float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;54 float d;55 56 if (level > 12) return;5758 x12 = (x1+x2)*0.5f;59 y12 = (y1+y2)*0.5f;60 x23 = (x2+x3)*0.5f;61 y23 = (y2+y3)*0.5f;62 x34 = (x3+x4)*0.5f;63 y34 = (y3+y4)*0.5f;64 x123 = (x12+x23)*0.5f;65 y123 = (y12+y23)*0.5f;66 x234 = (x23+x34)*0.5f;67 y234 = (y23+y34)*0.5f;68 x1234 = (x123+x234)*0.5f;69 y1234 = (y123+y234)*0.5f;7071 d = distPtSeg(x1234, y1234, x1,y1, x4,y4);72 if (d > tol*tol) {73 cubicBez(x1,y1, x12,y12, x123,y123, x1234,y1234, tol, level+1); 74 cubicBez(x1234,y1234, x234,y234, x34,y34, x4,y4, tol, level+1); 75 } else {76 glVertex2f(x4, y4);77 }78}7980void drawPath(float* pts, int npts, char closed, float tol)81{82 int i;83 glBegin(GL_LINE_STRIP);84 glColor4ubv(lineColor);85 glVertex2f(pts[0], pts[1]);86 for (i = 0; i < npts-1; i += 3) {87 float* p = &pts[i*2];88 cubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7], tol, 0);89 }90 if (closed) {91 glVertex2f(pts[0], pts[1]);92 }93 glEnd();94}9596void drawControlPts(float* pts, int npts)97{98 int i;99100 // Control lines101 glColor4ubv(lineColor);102 glBegin(GL_LINES);103 for (i = 0; i < npts-1; i += 3) {104 float* p = &pts[i*2];105 glVertex2f(p[0],p[1]);106 glVertex2f(p[2],p[3]);107 glVertex2f(p[4],p[5]);108 glVertex2f(p[6],p[7]);109 }110 glEnd();111112 // Points113 glPointSize(6.0f);114 glColor4ubv(lineColor);115116 glBegin(GL_POINTS);117 glVertex2f(pts[0],pts[1]);118 for (i = 0; i < npts-1; i += 3) {119 float* p = &pts[i*2];120 glVertex2f(p[6],p[7]);121 }122 glEnd();123124 // Points125 glPointSize(3.0f);126127 glBegin(GL_POINTS);128 glColor4ubv(bgColor);129 glVertex2f(pts[0],pts[1]);130 for (i = 0; i < npts-1; i += 3) {131 float* p = &pts[i*2];132 glColor4ubv(lineColor);133 glVertex2f(p[2],p[3]);134 glVertex2f(p[4],p[5]);135 glColor4ubv(bgColor);136 glVertex2f(p[6],p[7]);137 }138 glEnd();139}140141void drawframe(GLFWwindow* window)142{143 int width = 0, height = 0;144 float view[4], cx, cy, hw, hh, aspect, px;145 NSVGshape* shape;146 NSVGpath* path;147148 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);149 glfwGetFramebufferSize(window, &width, &height);150151 glViewport(0, 0, width, height);152 glClearColor(220.0f/255.0f, 220.0f/255.0f, 220.0f/255.0f, 1.0f);153 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);154 glEnable(GL_BLEND);155 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);156 glDisable(GL_TEXTURE_2D);157 glMatrixMode(GL_PROJECTION);158 glLoadIdentity();159160 // Fit view to bounds161 cx = g_image->width*0.5f;162 cy = g_image->height*0.5f;163 hw = g_image->width*0.5f;164 hh = g_image->height*0.5f;165166 if (width/hw < height/hh) {167 aspect = (float)height / (float)width;168 view[0] = cx - hw * 1.2f;169 view[2] = cx + hw * 1.2f;170 view[1] = cy - hw * 1.2f * aspect;171 view[3] = cy + hw * 1.2f * aspect;172 } else {173 aspect = (float)width / (float)height;174 view[0] = cx - hh * 1.2f * aspect;175 view[2] = cx + hh * 1.2f * aspect;176 view[1] = cy - hh * 1.2f;177 view[3] = cy + hh * 1.2f;178 }179 // Size of one pixel.180 px = (view[2] - view[1]) / (float)width;181182 glOrtho(view[0], view[2], view[3], view[1], -1, 1);183184 glMatrixMode(GL_MODELVIEW);185 glLoadIdentity();186 glDisable(GL_DEPTH_TEST);187 glColor4ub(255,255,255,255);188 glEnable(GL_BLEND);189 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);190191 // Draw bounds192 glColor4ub(0,0,0,64);193 glBegin(GL_LINE_LOOP);194 glVertex2f(0, 0);195 glVertex2f(g_image->width, 0);196 glVertex2f(g_image->width, g_image->height);197 glVertex2f(0, g_image->height);198 glEnd();199200 for (shape = g_image->shapes; shape != NULL; shape = shape->next) {201 for (path = shape->paths; path != NULL; path = path->next) {202 drawPath(path->pts, path->npts, path->closed, px * 1.5f);203 drawControlPts(path->pts, path->npts);204 }205 }206207 glfwSwapBuffers(window);208}209210void resizecb(GLFWwindow* window, int width, int height)211{212 // Update and render213 NSVG_NOTUSED(width);214 NSVG_NOTUSED(height);215 drawframe(window);216}217218int main()219{220 GLFWwindow* window;221 const GLFWvidmode* mode;222223 if (!glfwInit())224 return -1;225226 mode = glfwGetVideoMode(glfwGetPrimaryMonitor());227 window = glfwCreateWindow(mode->width - 40, mode->height - 80, "Nano SVG", NULL, NULL);228 if (!window)229 {230 printf("Could not open window\n");231 glfwTerminate();232 return -1;233 }234235 glfwSetFramebufferSizeCallback(window, resizecb);236 glfwMakeContextCurrent(window);237 glEnable(GL_POINT_SMOOTH);238 glEnable(GL_LINE_SMOOTH);239240241 g_image = nsvgParseFromFile("../example/nano.svg", "px", 96.0f);242 if (g_image == NULL) {243 printf("Could not open SVG image.\n");244 glfwTerminate();245 return -1;246 }247248 while (!glfwWindowShouldClose(window))249 {250 drawframe(window);251 glfwPollEvents();252 }253254 nsvgDelete(g_image);255256 glfwTerminate();257 return 0;258}