-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc.tmd
More file actions
169 lines (130 loc) · 4.7 KB
/
Copy pathc.tmd
File metadata and controls
169 lines (130 loc) · 4.7 KB
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
---
title: Tmdoc - examples for the C programming language
author: Detlef Groth
date: 2025-10-17 20:00
include-before: |
<style>
body { max-width: 1000px; font-family: Candara, sans-serif; }
pre { font-family: Consolas; monospaced; }
table { min-width: 400px; border-spacing: 5px; border-collapse: collapse; }
</style>
<center>some links on top</center>
---
`tcl include header.md`
## Introduction
Below are some C coding examples.
## Hello World
```{.shell cmd="gcc %i -o hello&&./hello > %o" chunk.ext=c ext=txt}
#include <stdio.h>
int main() {
printf("Hello C World!\n");
return(0);
}
```
## Image Creation
Here an example which does create an image using the
[Gd](https://libgd.github.io) library.
```{.shell label=gdtest cmd="gcc -L/usr/lib/x86_64-linux-gnu %i -o %b -lgd -lpng -lz -ljpeg -lfreetype -lm &&./%b %o" chunk.ext=c ext=png include=false}
/* Bring in gd library functions */
#include <gd.h>
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main(int argc, char **argv) {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen(argv[1], "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Close the files. */
fclose(pngout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
```

And here an example using the small PlutoVG vector graphics library which can be installed on Fedora like this:
```
sudo dnf install plutovg-devel
```
Here example code:
```
'``{.shell cmd="gcc -lplutovg %i -o smiley&&./smiley" chunk.ext=c ext=png include=false}
... Code ...
'``
```
On debian the library seems not to be available. Here you can then install the library like this:
```
git clone https://github.com/sammycage/plutovg.git
cd plutovg
cmake -B build .
cmake --build build
sudo cmake --install build
```
You the need to use the following chunk parameters to use the package:
```
label=smiley cmd="gcc -I/usr/local/include/plutovg %i /usr/local/lib64/libplutovg.a \
-o %b -lm &&./%b %o" chunk.ext=c ext=png include=false
```
And here the output:
```{.shell label=smiley cmd="gcc -I/usr/local/include/plutovg %i /usr/local/lib64/libplutovg.a -o %b -lm &&./%b %o" chunk.ext=c ext=png include=false}
#include <plutovg/plutovg.h>
int main(int argc, char **argv)
{
const int width = 150;
const int height = 150;
const float center_x = width / 2.f;
const float center_y = height / 2.f;
const float face_radius = 70;
const float mouth_radius = 50;
const float eye_radius = 10;
const float eye_offset_x = 25;
const float eye_offset_y = 20;
const float eye_x = center_x - eye_offset_x;
const float eye_y = center_y - eye_offset_y;
plutovg_surface_t* surface = plutovg_surface_create(width, height);
plutovg_canvas_t* canvas = plutovg_canvas_create(surface);
plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, center_x, center_y, face_radius, 0, PLUTOVG_TWO_PI, 0);
plutovg_canvas_set_rgb(canvas, 1, 0.8, 0);
plutovg_canvas_fill_preserve(canvas);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
plutovg_canvas_set_line_width(canvas, 5);
plutovg_canvas_stroke(canvas);
plutovg_canvas_restore(canvas);
plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, eye_x, eye_y, eye_radius, 0, PLUTOVG_TWO_PI, 0);
plutovg_canvas_arc(canvas, center_x + eye_offset_x, eye_y, eye_radius, 0, PLUTOVG_TWO_PI, 0);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
plutovg_canvas_fill(canvas);
plutovg_canvas_restore(canvas);
plutovg_canvas_save(canvas);
plutovg_canvas_arc(canvas, center_x, center_y, mouth_radius, 0, PLUTOVG_PI, 0);
plutovg_canvas_set_rgb(canvas, 0, 0, 0);
plutovg_canvas_set_line_width(canvas, 5);
plutovg_canvas_stroke(canvas);
plutovg_canvas_restore(canvas);
plutovg_surface_write_to_png(surface, argv[1]);
plutovg_canvas_destroy(canvas);
plutovg_surface_destroy(surface);
return 0;
}
```
