Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git

raster.h (1897B) · raw


      1 /// @file render/raster.h
      2 /// @brief Drawing primitives of the software renderer.
      3 ///
      4 /// Everything here uses integer arithmetic only, so the same call produces the
      5 /// same pixels on every compiler and CPU.
      6 #pragma once
      7 
      8 #include "core/types.h"
      9 #include "render/frame_buffer.h"
     10 
     11 namespace kumo {
     12 
     13 /// @brief Number of fractional bits in a Point coordinate.
     14 constexpr int kSubpixelBits = 4;
     15 
     16 /// @brief One pixel, in the units of a Point coordinate.
     17 constexpr int32_t kSubpixelOne = 1 << kSubpixelBits;
     18 
     19 /// @brief A position in fixed point: 4 fractional bits, so 1/16 pixel steps.
     20 ///
     21 /// Pixel (px, py) occupies the square from (px, py) to (px + 1, py + 1) and is
     22 /// sampled at its center. Keep coordinates within about +-32000 pixels.
     23 struct Point {
     24   int32_t x;
     25   int32_t y;
     26 };
     27 
     28 /// @brief The Point at the top left corner of pixel (x, y).
     29 constexpr Point pixel_point(int x, int y) {
     30   return {x * kSubpixelOne, y * kSubpixelOne};
     31 }
     32 
     33 /// @brief Sets every pixel to @p color.
     34 void clear(FrameBuffer& framebuffer, Color color);
     35 
     36 /// @brief Fills the rectangle at (x, y) of size w by h, clipped to the buffer.
     37 void fill_rect(FrameBuffer& framebuffer, int x, int y, int w, int h,
     38                Color color);
     39 
     40 /// @brief Draws a one pixel wide line from (x0, y0) to (x1, y1), both ends
     41 ///        included, clipped to the buffer.
     42 void draw_line(FrameBuffer& framebuffer, int x0, int y0, int x1, int y1,
     43                Color color);
     44 
     45 /// @brief Fills the triangle @p a, @p b, @p c, whichever way it winds.
     46 ///
     47 /// A pixel belongs to the triangle if its center is inside. A center exactly
     48 /// on an edge belongs to it only if that is a top or a left edge (the
     49 /// "top-left rule"), so two triangles that share an edge never draw a pixel
     50 /// twice and never leave a gap.
     51 void fill_triangle(FrameBuffer& framebuffer, Point a, Point b, Point c,
     52                    Color color);
     53 
     54 }  // namespace kumo