the ugly shellscript powering https://oppi.li
1<!DOCTYPE html>
2<html lang="en">
3 <head>
4 <link rel="stylesheet" href="/style.css">
5 <link rel="stylesheet" href="/syntax.css">
6 <meta charset="UTF-8">
7 <meta name="viewport" content="initial-scale=1">
8 <meta content="#ffffff" name="theme-color">
9 <meta name="HandheldFriendly" content="true">
10 <meta property="og:title" content="SDL2 Devlog">
11 <meta property="og:type" content="website">
12 <meta property="og:description" content="a static site {for, by, about} me ">
13 <meta property="og:url" content="https://oppi.li/posts/SDL2_devlog">
14 <link rel="icon" type="image/x-icon" href="/favicon.png">
15 <title>SDL2 Devlog · oppi.li</title>
16 <body>
17 <div class="posts">
18 <div class="post">
19 <a href="/" class="post-end-link">Home</a>
20 <span>/</span>
21 <a href="/posts" class="post-end-link">Posts</a>
22 <span>/</span>
23 <a class="post-end-link">SDL2 Devlog</a>
24 <a class="stats post-end-link" href="https://tangled.sh/@oppi.li/site/raw/main/posts/SDL2_devlog.md
25">View Raw</a>
26 <div class="separator"></div>
27 <div class="date">
28 11/04 — 2021
29 <div class="stats">
30 <span class="stats-number">
31 124.28
32 </span>
33 <span class="stats-unit">cm</span>
34  
35 <span class="stats-number">
36 10.0
37 </span>
38 <span class="stats-unit">min</span>
39 </div>
40 </div>
41 <h1>
42 SDL2 Devlog
43 </h1>
44 <div class="post-text">
45 <p>I have been working on an editor for the <a
46href="https://git.peppe.rs/graphics/obi/about">One Bit Image</a> file
47format in Rust and SDL2. This entry in my blog follows my progress on
48the editor. The days are listed in reverse chronological order, begin
49from the bottom, if this is your first time on this page.</p>
50<h3 id="day-20">Day 20</h3>
51<p>More <code>lisp</code> stuff! I added a new brush, for rectangular
52selections. While selection doesn’t do much on its own, the selected
53area can be passed onto a <code>lisp</code> procedure, for example, a
54procedure to draw horizontal black and white lines:</p>
55<figure>
56<video src="https://u.peppe.rs/frU.mp4" controls=""><a
57href="https://u.peppe.rs/frU.mp4">Day 20</a></video>
58<figcaption aria-hidden="true">Day 20</figcaption>
59</figure>
60<h3 id="day-19">Day 19</h3>
61<p>Attempted <a href="https://peppe.rs/art/conduit.png">some isometric
62art</a> within the editor. The angles displayed alongside the line brush
63are handly, however, having only a rectangular grid did not help. I
64implemented an isometric grid today. Isometric grids in pixel art differ
65in that the tangent of the isometric angle is exactly 0.5! For every
66pixel down, you go exactly two pixels sideways. The math works out
67really well in the drawing procedures too, dealing with floating points
68is a pain.</p>
69<figure>
70<img src="https://u.peppe.rs/1Kb.png" alt="Day 19" />
71<figcaption aria-hidden="true">Day 19</figcaption>
72</figure>
73<h3 id="day-18">Day 18</h3>
74<p>I added basic support for guides, they can be added and activated
75from the <code>lisp</code> REPL. Another long standing improvement I
76wanted to make was reworking the pixmap drawing procedure. The old
77procedure draws a square for each pixel in the pixmap, coloured
78according to its value in the pixmap. Naturally, this means, for an
79<strong>NxN</strong> pixmap, there are <strong>N²</strong> calls to SDL!
80I reworked this procedure to compress each line of the pixmap using RLE
81(run length encoding), and call out to SDL for each run in the line.
82This drastically improved drawing speeds on larger grids. The following
83is a comparison between the two procedures, the leftmost picture is the
84rendered image, the middle picture is the optimized drawing procedure
85(draws each run instead of pixel), and the right most picture is the
86primitive drawing procedure (draws each pixel):</p>
87<figure>
88<img src="https://u.peppe.rs/U4B.png" alt="Day 18" />
89<figcaption aria-hidden="true">Day 18</figcaption>
90</figure>
91<h3 id="day-17">Day 17</h3>
92<p>I decided to give the text-only statusline a touch up, by adding a
93active color and dither level preview. Aligning the “widget” to the
94right of statusline involved a lot more than I thought, so I created a
95ghetto CSS-like rectangle placement system to position containers inside
96containers:</p>
97<div class="sourceCode" id="cb1"><pre
98class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">// roughly something like this</span></span>
99<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> statusline <span class="op">=</span> </span>
100<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="pp">Container::</span>new(<span class="pp">Offset::</span>Left(<span class="dv">0</span>)<span class="op">,</span> <span class="pp">Offset::</span>Bottom(<span class="dv">40</span>))</span>
101<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>width(<span class="pp">Size::</span>Max)</span>
102<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>height(<span class="pp">Size::</span>Absolute(<span class="dv">20</span>))<span class="op">;</span></span>
103<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> </span>
104<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> <span class="kw">mut</span> primary <span class="op">=</span> <span class="pp">Container::</span>uninit()</span>
105<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>width(<span class="pp">Size::</span>Absolute(<span class="dv">16</span>))</span>
106<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span>height(<span class="pp">Size::</span>Absolute(<span class="dv">16</span>))<span class="op">;</span></span>
107<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> </span>
108<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>container<span class="op">.</span>place(</span>
109<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> <span class="op">&</span><span class="kw">mut</span> padding_box<span class="op">,</span></span>
110<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> <span class="pp">HorAlign::</span>Right<span class="op">,</span></span>
111<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> <span class="pp">VertAlign::</span>Center</span>
112<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span></code></pre></div>
113<p>The result (brush preview on the bottom right):</p>
114<figure>
115<video src="https://u.peppe.rs/OtU.mp4" controls=""><a
116href="https://u.peppe.rs/OtU.mp4">Day 17</a></video>
117<figcaption aria-hidden="true">Day 17</figcaption>
118</figure>
119<h3 id="day-16">Day 16</h3>
120<p>The embedded lisp is coming along nicely, users can load a custom
121<code>rc.lisp</code>, which is evaluated on startup. To disable to grid
122on start, for example:</p>
123<div class="sourceCode" id="cb2"><pre
124class="sourceCode scheme"><code class="sourceCode scheme"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">;;; rc.lisp</span></span>
125<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>(toggle-grid)</span></code></pre></div>
126<p>Some aliases to switch between brushes:</p>
127<div class="sourceCode" id="cb3"><pre
128class="sourceCode scheme"><code class="sourceCode scheme"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">;;; rc.lisp</span></span>
129<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>(<span class="ex">define</span><span class="fu"> </span>(brush kind)</span>
130<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> (<span class="kw">cond</span></span>
131<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind 'f) (brush-fill))</span>
132<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind 'c) (brush-circle))</span>
133<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind 'l) (brush-line))</span>
134<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> ((<span class="kw">eq?</span> kind 'l+) (brush-line-extend))</span>
135<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> (<span class="kw">else</span> (brush-circle))))</span></code></pre></div>
136<p>The following script draws a straight line along a given axis, at a
137given distance from the canvas boundary:</p>
138<figure>
139<video src="https://u.peppe.rs/b3i.mp4" controls=""><a
140href="https://u.peppe.rs/b3i.mp4">Day 16</a></video>
141<figcaption aria-hidden="true">Day 16</figcaption>
142</figure>
143<h3 id="day-15">Day 15</h3>
144<p>I began writing a standard library for the lisp, in lisp. It includes
145basic list operations: <code>car</code>, <code>cdr</code>,
146<code>null?</code>, <code>list</code>, higher order functions:
147<code>map</code>, <code>filter</code>, <code>fold</code>:</p>
148<div class="sourceCode" id="cb4"><pre
149class="sourceCode lisp"><code class="sourceCode commonlisp"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>(define (member? item ls)</span>
150<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> (fold <span class="dv">#f</span></span>
151<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> (<span class="kw">lambda</span> (acc x) (<span class="kw">or</span> acc (eq? item x)))</span>
152<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> ls))</span></code></pre></div>
153<h3 id="day-14">Day 14</h3>
154<p>I attempted a <a href="https://peppe.rs/art/ramen_noodles.png">small
155art piece</a> using the editor, while it was largely usable, I felt a
156certain lack of feedback. The brushes just didn’t relay as much info as
157I’d have liked, for example, the approximate points of the line or the
158angle made by the line against the x-axis. Unfortunately, the existing
159infrastructure around brushes and line drawing didn’t easily allow for
160this either. I went ahead and reimplemented brushes, and added a new
161flood fill brush too:</p>
162<figure>
163<video src="https://u.peppe.rs/8q.mp4" controls=""><a
164href="https://u.peppe.rs/8q.mp4">Day 14</a></video>
165<figcaption aria-hidden="true">Day 14</figcaption>
166</figure>
167<h3 id="day-13">Day 13</h3>
168<p>I added a few more forms to the <code>lisp</code> evaluator. It
169handles recursion, definitions, variable mutation and more. The prelude
170contains 20 subroutines so far, including comparision and logic
171operators. The REPL interface on the SDL side requires some UX tweaks;
172environment based completion, readline motions sound doable.</p>
173<figure>
174<video src="https://u.peppe.rs/u3.mp4" controls=""><a
175href="https://u.peppe.rs/u3.mp4">Day 13</a></video>
176<figcaption aria-hidden="true">Day 13</figcaption>
177</figure>
178<h3 id="day-12">Day 12</h3>
179<p>I lifted most of <a
180href="https://github.com/murarth/ketos">murarth/ketos</a> into the
181editor. <code>ketos</code>’s implementation of <code>lisp</code> is too
182vast for my use case. For example, the editor does not need data types
183to handle raw strings or byte strings. I have got a basic evaluator
184running inside the SDL2 context (notice the <code>lisp</code> REPL at
185the bottom of the window). Over the following days, I intend to create a
186set of prelude functions to manipulate the pixmap. Users can implement
187their own brushes, dithering patterns, keybinds and more
188(hopefully).</p>
189<figure>
190<video src="https://u.peppe.rs/y0.mp4" controls=""><a
191href="https://u.peppe.rs/y0.mp4">Day 12</a></video>
192<figcaption aria-hidden="true">Day 12</figcaption>
193</figure>
194<h3 id="day-11">Day 11</h3>
195<p>I intend to supplement the editor with scripting language and an
196inbuilt REPL for the same. I began by implementing a text box widget
197from scratch, with history and readline like editing:</p>
198<figure>
199<video src="https://u.peppe.rs/Mh.mp4" controls=""><a
200href="https://u.peppe.rs/Mh.mp4">Day 11</a></video>
201<figcaption aria-hidden="true">Day 11</figcaption>
202</figure>
203<h3 id="day-10">Day 10</h3>
204<p>I started reading up on dithering methods and half-toning, I wanted
205to create a dithering brush that would automatically produce popular
206dithering patterns. The method that caught my eye (and also the one used
207most often in pixel art), was Bayer’s ordered dithering. When applied to
208a black and white image, each pixel, based on its intensity, is mapped
209to a 4x4 grid of pixels. A completely empty (completely black) 4x4 grid
210represents zero intensity, and a filled 4x4 grid represents full
211intensity. Bayer’s ordered dithering can produce 15 steps of intensity
212between zero and full (by switching on exactly 1 pixel more at each
213level), thus, being able to draw 17 “shades” from white to black.
214Creating a dithering brush from here was fairly trivial. Our pixmap is
215supposed to represent the final dithered image, it must be divided into
2164x4 grids. Each grid is colored based on the intensity of the brush
217passing over it:</p>
218<figure>
219<img src="https://u.peppe.rs/Mn.png" alt="Day 10" />
220<figcaption aria-hidden="true">Day 10</figcaption>
221</figure>
222<h3 id="day-9">Day 9</h3>
223<p>I started working towards an interface. I like the idea of a largely
224read-only HUD, i. e., an interface that simply describes the state of
225the application. Changes to this state are initiated via keybinds or
226text commands. I am proud of the symmetry indicator; <code>-</code> for
227horizontal symmetry, <code>|</code> for vertical symmetry,
228<code>+</code> for radial symmetry.</p>
229<figure>
230<img src="https://u.peppe.rs/hx.png" alt="Day 9" />
231<figcaption aria-hidden="true">Day 9</figcaption>
232</figure>
233<h3 id="day-8">Day 8</h3>
234<p>One of my favourite features of GIMP was symmetric editing. I added
235some coordinate geometry primitives to my pixmap abstraction, allowing
236for mirroring and reflecting figures about lines or points. The result
237was an ergonomic function that applies symmetry to any painting
238operation, (undo/redo works as expected):</p>
239<div class="sourceCode" id="cb5"><pre
240class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> line <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>pixmap<span class="op">.</span>get_line(start<span class="op">,</span> end)<span class="op">;</span></span>
241<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> sym_line <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>symmetry<span class="op">.</span>apply(<span class="op">&</span>line)<span class="op">;</span></span>
242<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> point on line<span class="op">.</span>extend(sym_line) <span class="op">{</span></span>
243<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="co">// draw to window</span></span>
244<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
245<figure>
246<video src="https://u.peppe.rs/B1.mp4" controls=""><a
247href="https://u.peppe.rs/B1.mp4">Day 8</a></video>
248<figcaption aria-hidden="true">Day 8</figcaption>
249</figure>
250<h3 id="day-7">Day 7</h3>
251<p>Bresenham saves the day again! This time, I implemented his line
252drawing algorithm, to, well, draw lines. Each point on the line is then
253“buffed” based on the active brush size. Today’s changes fit in very
254well with the undo system and the brush size feature. Creating the right
255abstractions, one at a time :)</p>
256<figure>
257<video src="https://u.peppe.rs/xt.mp4" controls=""><a
258href="https://u.peppe.rs/xt.mp4">Day 7</a></video>
259<figcaption aria-hidden="true">Day 7</figcaption>
260</figure>
261<h3 id="day-6">Day 6</h3>
262<p>I extended Bresenham’s algorithm to draw not just circle outlines,
263but also generate their fills. Unlike Bresenham’s algorithm, this
264variant generates points for two quadrants at once, these points are
265mirrored over the dividing axis to generate the other two quadrants.</p>
266<figure>
267<img src="https://u.peppe.rs/f3.png" alt="Day 6" />
268<figcaption aria-hidden="true">Day 6</figcaption>
269</figure>
270<h3 id="day-5">Day 5</h3>
271<p>I discovered and implemented Bresenham’s algorithm for efficient
272circle drawing. The algorithm allowed for sized circular brushes,
273something I really liked from GIMP. Very convenient that the Wikipedia
274page for Bresenham’s algorithm also includes a section about optimizing
275for integer based arithmetic. I managed to abstract out another giant
276component of the application, the pixmap. Any image is just a grid of
277pixels (a pixmap), where the pixel’s value is decided by the application
278(1-bit in my case). I could potentially extend the application to a
27924-bit image editor!</p>
280<figure>
281<video src="https://u.peppe.rs/Kh.mp4" controls=""><a
282href="https://u.peppe.rs/Kh.mp4">Day 5</a></video>
283<figcaption aria-hidden="true">Day 5</figcaption>
284</figure>
285<h3 id="day-4">Day 4</h3>
286<p>I created a generic “undo stack” data structure that allows for
287infinite “undos” and “redos”. Every modification operation to the grid
288is persisted to the application state. A couple of keybinds allow the
289user to revert and re-apply these operations! I expect abstracting this
290component will come in handy down the line.</p>
291<figure>
292<video src="https://u.peppe.rs/w5.mp4" controls=""><a
293href="https://u.peppe.rs/w5.mp4">Day 4</a></video>
294<figcaption aria-hidden="true">Day 4</figcaption>
295</figure>
296<h3 id="day-3">Day 3</h3>
297<p>I implemented the bare minimum required to call the program an
298“editor”. The application displays a grid, tracks mouse events, paints
299white to the canvas on left click, and black to the canvas on right
300click. I created a make-shift MVC architecture à la Elm in Rust.</p>
301<figure>
302<video src="https://u.peppe.rs/GF.mp4" controls=""><a
303href="https://u.peppe.rs/GF.mp4">Day 3</a></video>
304<figcaption aria-hidden="true">Day 3</figcaption>
305</figure>
306<h3 id="day-2">Day 2</h3>
307<p>I started figuring out event handling today. Implemented a couple of
308keybinds to zoom in/out of the drawing area. Conversions of SDL2
309coordinates (measured in signed 32 bit integers) to my internal “drawing
310area” coordinates (measured in unsigned 32 bit integers) is very
311annoying. Hopefully the unchecked conversions won’t haunt me later.</p>
312<figure>
313<video src="https://u.peppe.rs/L4.mp4" controls=""><a
314href="https://u.peppe.rs/L4.mp4">Day 2</a></video>
315<figcaption aria-hidden="true">Day 2</figcaption>
316</figure>
317<h3 id="day-1">Day 1</h3>
318<p>Getting started with Rust and SDL2 is very straightforward. The
319<code>rust-sdl2</code> library contains some detailed examples that
320allowed me to get all the way to drawing a grid from a
321<code>Vec<bool></code>:</p>
322<figure>
323<img src="https://u.peppe.rs/Ma.png" alt="Day 1" />
324<figcaption aria-hidden="true">Day 1</figcaption>
325</figure>
326
327 </div>
328
329 <div class="intro">
330 Hi.
331 <div class="hot-links">
332 <a href="/index.xml" class="feed-button">Subscribe</a>
333 </div>
334 <p>I'm Akshay, programmer, pixel-artist & programming-language enthusiast.</p>
335 <p>I am currently building <a href="https://tangled.sh">tangled.sh</a> — a new social-enabled code-collaboration platform.</p>
336 <p>Reach out at oppili@libera.chat.</p>
337 </div>
338
339 <a href="/" class="post-end-link">Home</a>
340 <span>/</span>
341 <a href="/posts" class="post-end-link">Posts</a>
342 <span>/</span>
343 <a class="post-end-link">SDL2 Devlog</a>
344 <a class="stats post-end-link" href="https://tangled.sh/@oppi.li/site/raw/main/posts/SDL2_devlog.md
345">View Raw</a>
346 </div>
347 </div>
348 </body>
349</html>