<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kaustuv Pokharel</title>
    <description>The latest articles on Forem by Kaustuv Pokharel (@kastuv).</description>
    <link>https://forem.com/kastuv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1241213%2Fe1d5cf53-0046-4f02-b5e4-e2cd0665ed93.jpeg</url>
      <title>Forem: Kaustuv Pokharel</title>
      <link>https://forem.com/kastuv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kastuv"/>
    <language>en</language>
    <item>
      <title>(Transmission Control Protocol)TCP and TCP Header: Networking</title>
      <dc:creator>Kaustuv Pokharel</dc:creator>
      <pubDate>Mon, 14 Jul 2025 17:52:41 +0000</pubDate>
      <link>https://forem.com/kastuv/transmission-control-protocoltcp-and-tcp-header-networking-45k3</link>
      <guid>https://forem.com/kastuv/transmission-control-protocoltcp-and-tcp-header-networking-45k3</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnn8pfm8ngge21lanwlnu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnn8pfm8ngge21lanwlnu.jpg" alt=" " width="800" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Excellent observation — you're noticing those numbers on &lt;strong&gt;top and side&lt;/strong&gt; of the TCP header diagram.&lt;/p&gt;

&lt;p&gt;Let’s break that part down&lt;/p&gt;




&lt;h2&gt;
  
  
  What Do &lt;code&gt;0 1 2 3&lt;/code&gt; and &lt;code&gt;0123456789...&lt;/code&gt; Mean in the Header Diagram?
&lt;/h2&gt;

&lt;p&gt;This is a &lt;strong&gt;bit-level layout&lt;/strong&gt; of the TCP header.&lt;/p&gt;

&lt;h3&gt;
  
  
  Top Row: &lt;code&gt;0 1 2 3&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This row represents &lt;strong&gt;32-bit word boundaries&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each group (&lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt;, etc.) stands for &lt;strong&gt;4 bytes (32 bits)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It's showing how the entire header is organized as &lt;strong&gt;a series of 32-bit blocks&lt;/strong&gt;, which is how computers store and process them efficiently.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why 32 bits?&lt;/strong&gt;&lt;br&gt;
CPUs are optimized to process 32-bit or 64-bit words at once. TCP headers are aligned to 32-bit boundaries to boost speed.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Second Row: &lt;code&gt;0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;These are &lt;strong&gt;bit positions&lt;/strong&gt; inside each 32-bit word (horizontal axis, left to right).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each tick = 1 bit.&lt;/li&gt;
&lt;li&gt;After 15, it wraps around again with 0,1... for the next 16 bits.&lt;/li&gt;
&lt;li&gt;So from left to right, you’re seeing &lt;strong&gt;bit 0 to 31&lt;/strong&gt; of that 32-bit chunk.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Left Side: &lt;code&gt;0 1 2 3&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;These numbers on the left represent &lt;strong&gt;row numbers&lt;/strong&gt; → how many &lt;strong&gt;32-bit rows&lt;/strong&gt; down you are in the header.&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Row &lt;code&gt;0&lt;/code&gt; = first 32 bits (4 bytes)&lt;/li&gt;
&lt;li&gt;Row &lt;code&gt;1&lt;/code&gt; = next 32 bits&lt;/li&gt;
&lt;li&gt;…&lt;/li&gt;
&lt;li&gt;Row &lt;code&gt;3&lt;/code&gt; = last block in the minimal 20-byte TCP header&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Reminder from the Image:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“One tick mark represents &lt;strong&gt;one bit position&lt;/strong&gt;.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So when you see a field that spans 16 tick marks — that means it's &lt;strong&gt;16 bits = 2 bytes&lt;/strong&gt; wide.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Read It
&lt;/h2&gt;

&lt;p&gt;Let’s pick an example: &lt;code&gt;Source Port&lt;/code&gt; is in &lt;strong&gt;row 0&lt;/strong&gt;, &lt;strong&gt;bit 0 to 15&lt;/strong&gt; (16 bits).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;+&lt;/code&gt;&lt;/strong&gt; signs mark 32-bit (4-byte) boundaries.&lt;/li&gt;
&lt;li&gt;Each field is aligned within those 32-bit words.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Data Offset&lt;/code&gt; is just 4 bits, which is why it sits at bit position &lt;code&gt;0-3&lt;/code&gt; on row 4.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Real Example (Row by Row)
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Row&lt;/th&gt;
&lt;th&gt;Bit Range&lt;/th&gt;
&lt;th&gt;Fields&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0–15&lt;/td&gt;
&lt;td&gt;Source Port (16 bits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;16–31&lt;/td&gt;
&lt;td&gt;Destination Port (16 bits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0–31&lt;/td&gt;
&lt;td&gt;Sequence Number (32 bits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0–31&lt;/td&gt;
&lt;td&gt;Acknowledgment Number (32 bits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;0–3&lt;/td&gt;
&lt;td&gt;Data Offset (4 bits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;4–6&lt;/td&gt;
&lt;td&gt;Reserved (3 bits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;NS flag&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;8–15&lt;/td&gt;
&lt;td&gt;CWR, ECE, URG, ACK, PSH, RST, SYN, FIN&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;16–31&lt;/td&gt;
&lt;td&gt;Window Size (16 bits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;0–15&lt;/td&gt;
&lt;td&gt;Checksum&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;16–31&lt;/td&gt;
&lt;td&gt;Urgent Pointer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5+&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Options + Padding (if present)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;...&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Payload data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Why This Layout?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compact&lt;/strong&gt;: Every bit is carefully packed — no wasted space.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient&lt;/strong&gt;: 32-bit words are perfect for CPU alignment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extendable&lt;/strong&gt;: Options + reserved bits make TCP forward-compatible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Perfect, let’s now go &lt;strong&gt;field-by-field&lt;/strong&gt; in the TCP header, but this time from the &lt;strong&gt;creator’s perspective&lt;/strong&gt;, &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Background: Why TCP Needs a Header at All&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;TCP is designed to provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reliable delivery&lt;/strong&gt; over &lt;strong&gt;unreliable IP&lt;/strong&gt; (which can drop, reorder, duplicate packets)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ordered byte stream&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error checking&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flow &amp;amp; congestion control&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus, every TCP segment must carry enough metadata to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Track what’s being sent/received&lt;/li&gt;
&lt;li&gt;Allow reordering, retransmission, and confirmation&lt;/li&gt;
&lt;li&gt;Handle connection establishment and teardown&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. &lt;strong&gt;Source Port (16 bits)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To identify &lt;strong&gt;which application on the sender's side&lt;/strong&gt; is sending the data.&lt;/p&gt;

&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;Receiver wouldn’t know where to route the response — especially in systems with multiple open connections.&lt;/p&gt;

&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Think of it like the &lt;strong&gt;return address&lt;/strong&gt; on a letter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Web browser opens a page (source port = 49500), connects to google.com:443.&lt;/li&gt;
&lt;li&gt;Server uses 443 as &lt;strong&gt;destination&lt;/strong&gt;, and 49500 as &lt;strong&gt;reply&lt;/strong&gt; source.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. &lt;strong&gt;Destination Port (16 bits)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To indicate &lt;strong&gt;which application/service&lt;/strong&gt; should receive the data on the destination machine.&lt;/p&gt;

&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;The OS wouldn’t know whether to pass data to a web server, email server, or FTP daemon.&lt;/p&gt;

&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Like writing “To: Sales Department” on a company envelope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Connecting to a web server uses port 80 (HTTP), or 443 (HTTPS).&lt;/p&gt;




&lt;h2&gt;
  
  
  3. &lt;strong&gt;Sequence Number (32 bits)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To ensure &lt;strong&gt;in-order delivery&lt;/strong&gt; and &lt;strong&gt;detect packet loss/duplication&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You could receive packets out of order.&lt;/li&gt;
&lt;li&gt;Lost packets would be hard to detect.&lt;/li&gt;
&lt;li&gt;Duplicated packets would corrupt data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Page numbers in a book — imagine receiving 2, 3, 1, 4.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;You send 3000 bytes: packet 1 (0–999), packet 2 (1000–1999), packet 3 (2000–2999).&lt;br&gt;
If packet 2 is lost, the receiver knows to ask again.&lt;/p&gt;


&lt;h2&gt;
  
  
  4. &lt;strong&gt;Acknowledgment Number (32 bits)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To let sender know &lt;strong&gt;what data has been successfully received&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;The sender would have no clue if data arrived or was lost — no way to retransmit reliably.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;A “Received” stamp on a document.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;If the receiver got all data up to byte 5000, it sends &lt;code&gt;ACK = 5001&lt;/code&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  5. &lt;strong&gt;Data Offset (4 bits)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To tell where the &lt;strong&gt;TCP header ends&lt;/strong&gt; and the actual &lt;strong&gt;data begins&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;You couldn’t parse the packet — where are the flags? Where’s the data? It’d be ambiguous if options were used.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Table of contents telling where Chapter 1 starts.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Header = 20 bytes → Offset = 5 (because 5 × 4 = 20).&lt;/p&gt;


&lt;h2&gt;
  
  
  6. &lt;strong&gt;Reserved (3 bits)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To &lt;strong&gt;future-proof&lt;/strong&gt; TCP for new features.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;Adding new control fields would require breaking compatibility.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Empty parking spots reserved for future use.&lt;/p&gt;


&lt;h2&gt;
  
  
  7. &lt;strong&gt;Control Flags (9 bits)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Each bit represents a &lt;strong&gt;control instruction&lt;/strong&gt;. Let's break them down:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flag&lt;/th&gt;
&lt;th&gt;Full Form&lt;/th&gt;
&lt;th&gt;Why It Exists&lt;/th&gt;
&lt;th&gt;What Breaks Without It&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;URG&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Urgent&lt;/td&gt;
&lt;td&gt;Urgent data like keyboard interrupt (legacy)&lt;/td&gt;
&lt;td&gt;Can’t prioritize emergency info&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ACK&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Acknowledgment&lt;/td&gt;
&lt;td&gt;Used in almost every packet after the first SYN&lt;/td&gt;
&lt;td&gt;Can’t confirm delivery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;PSH&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Push&lt;/td&gt;
&lt;td&gt;Tells receiver to pass data to app immediately&lt;/td&gt;
&lt;td&gt;Data might be buffered&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;RST&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Reset&lt;/td&gt;
&lt;td&gt;Kill connection immediately&lt;/td&gt;
&lt;td&gt;Can’t handle bad or spoofed connections&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SYN&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Synchronize&lt;/td&gt;
&lt;td&gt;Establish connection (step 1 of 3-way handshake)&lt;/td&gt;
&lt;td&gt;Connection setup is impossible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;FIN&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Finish&lt;/td&gt;
&lt;td&gt;Gracefully close a connection&lt;/td&gt;
&lt;td&gt;Can’t signal end of transmission&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ECE/CWR/NS&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;ECN bits for congestion&lt;/td&gt;
&lt;td&gt;Helps manage congestion in modern networks&lt;/td&gt;
&lt;td&gt;No congestion signaling&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;SYN: Initiates connection&lt;/li&gt;
&lt;li&gt;SYN+ACK: Response with agreement&lt;/li&gt;
&lt;li&gt;FIN: Closes connection&lt;/li&gt;
&lt;li&gt;RST: Force stop (e.g., invalid packet received)&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  8. &lt;strong&gt;Window Size (16 bits)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To perform &lt;strong&gt;flow control&lt;/strong&gt;: telling sender &lt;strong&gt;how much data&lt;/strong&gt; can be accepted.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;The sender might &lt;strong&gt;flood the receiver’s buffer&lt;/strong&gt;, causing overflow or dropped packets.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Your inbox says, “I can only receive 5 emails right now.”&lt;/p&gt;
&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Window = 65535 → receiver can accept 65535 bytes.&lt;br&gt;
When full, it tells sender to stop.&lt;/p&gt;


&lt;h2&gt;
  
  
  9. &lt;strong&gt;Checksum (16 bits)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To detect &lt;strong&gt;corruption&lt;/strong&gt; of header or data in transit.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;Corrupt packets would go unnoticed, causing bad data, crashes, or security flaws.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Seal on a package — if broken, don’t trust it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;You send “hello” but noise changes it to “h3llo”. Checksum mismatch alerts the receiver.&lt;/p&gt;


&lt;h2&gt;
  
  
  10. &lt;strong&gt;Urgent Pointer (16 bits)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To &lt;strong&gt;point to urgent data&lt;/strong&gt; when the URG flag is set — legacy for telnet/terminal systems.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;Legacy apps couldn’t prioritize user interrupts.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;“Skip to this part immediately!” note.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Used to send Ctrl+C from terminal user.&lt;/p&gt;


&lt;h2&gt;
  
  
  11. &lt;strong&gt;Options (Variable)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;To allow &lt;strong&gt;extension&lt;/strong&gt; of TCP capabilities (MSS, window scaling, timestamps, SACK).&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;TCP would be stuck with static features and couldn’t adapt to evolving networks (e.g., gigabit speeds, large windows).&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Extra paper in a contract for terms.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;MSS Option: “Don’t send more than 1460 bytes”&lt;/li&gt;
&lt;li&gt;Timestamp Option: Improve RTT measurement&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  12. &lt;strong&gt;Padding&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;Ensures TCP header ends on a &lt;strong&gt;32-bit boundary&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;Misalignment could slow down CPU processing and break parsing.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;Adding blank pages to align chapters in a book.&lt;/p&gt;


&lt;h2&gt;
  
  
  13. &lt;strong&gt;Data (Payload)&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Why it was created:
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;actual message/data&lt;/strong&gt; being delivered to the application.&lt;/p&gt;
&lt;h3&gt;
  
  
  If not included:
&lt;/h3&gt;

&lt;p&gt;You’d be sending metadata but no meaningful content.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy:
&lt;/h3&gt;

&lt;p&gt;The gift inside the wrapping.&lt;/p&gt;


&lt;h2&gt;
  
  
  Stage 1: &lt;strong&gt;Connection Establishment&lt;/strong&gt; (TCP 3-Way Handshake)
&lt;/h2&gt;

&lt;p&gt;Before data is sent, TCP must &lt;strong&gt;establish a connection&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Imagine:
&lt;/h3&gt;

&lt;p&gt;You (Client) want to connect to a web server (Server) on port 80.&lt;/p&gt;


&lt;h3&gt;
  
  
  Step 1: Client sends &lt;strong&gt;SYN&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Port: 53123         → your random port
Destination Port: 80       → HTTP server
Sequence Number: 1000      → start of byte stream
ACK Number: 0              → not used yet
Flags: SYN=1               → I want to start a connection
Window Size: 65535         → I can accept this much data
Checksum: valid
Options: MSS = 1460
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;🔧 What’s happening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;SYN&lt;/code&gt; flag is set to start connection&lt;/li&gt;
&lt;li&gt;Sequence number says “my byte stream starts at 1000”&lt;/li&gt;
&lt;li&gt;Options tell the server the &lt;strong&gt;Maximum Segment Size&lt;/strong&gt; (MSS)&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  Step 2: Server responds with &lt;strong&gt;SYN + ACK&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Port: 80            → Server replying from port 80
Destination Port: 53123    → Back to your app
Sequence Number: 8000      → Server’s stream starts here
ACK Number: 1001           → Acknowledging your SYN (seq+1)
Flags: SYN=1, ACK=1
Window Size: 65535
Checksum: valid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Server:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Acknowledges your SYN by setting &lt;code&gt;ACK = 1001&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sends its own &lt;code&gt;SYN&lt;/code&gt; with sequence number 8000&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  Step 3: Client sends &lt;strong&gt;ACK&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Port: 53123
Destination Port: 80
Sequence Number: 1001      → Next byte I’ll send
ACK Number: 8001           → I received your SYN
Flags: ACK=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now connection is &lt;strong&gt;established&lt;/strong&gt;!&lt;/p&gt;


&lt;h2&gt;
  
  
  Stage 2: &lt;strong&gt;Data Transfer Begins&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s say you’re sending this HTTP GET request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s 60 bytes long.&lt;/p&gt;

&lt;h3&gt;
  
  
  Client sends data packet:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Port: 53123
Destination Port: 80
Sequence Number: 1001
ACK Number: 8001
Flags: ACK=1, PSH=1
Window Size: 65535
Checksum: valid
Data: 60 bytes of GET request
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key insights:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Sequence Number&lt;/code&gt; = 1001 (your byte stream continues)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PSH&lt;/code&gt; = 1 (push data to app immediately)&lt;/li&gt;
&lt;li&gt;Server will now &lt;strong&gt;acknowledge this&lt;/strong&gt; by sending &lt;code&gt;ACK = 1061&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Server responds with data
&lt;/h3&gt;

&lt;p&gt;Let’s say the server sends back HTML (e.g., 1000 bytes)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Port: 80
Destination Port: 53123
Sequence Number: 8001
ACK Number: 1061
Flags: ACK=1, PSH=1
Data: 1000 bytes of HTML
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ACK=1061&lt;/code&gt;: acknowledges your GET&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Sequence=8001&lt;/code&gt;: server sends its byte stream&lt;/li&gt;
&lt;li&gt;Your client now acknowledges with &lt;code&gt;ACK=9001&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Stage 3: &lt;strong&gt;Connection Termination&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Once the server is done sending, it will &lt;strong&gt;gracefully close&lt;/strong&gt; the connection using the &lt;code&gt;FIN&lt;/code&gt; flag.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Server sends FIN
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Port: 80
Destination Port: 53123
Sequence Number: 9001
ACK Number: 1061
Flags: FIN=1, ACK=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Client ACKs it
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Flags: ACK=1
ACK Number: 9002
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Client sends its own FIN
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Flags: FIN=1, ACK=1
Sequence Number: 1061
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Server ACKs that, and connection is fully closed
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Flags: ACK=1
ACK Number: 1062
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Summary of Header Fields Across Phases
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Handshake&lt;/th&gt;
&lt;th&gt;Data Transfer&lt;/th&gt;
&lt;th&gt;Termination&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Source/Dest Port&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sequence Number&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Acknowledgment&lt;/td&gt;
&lt;td&gt;YES (after first packet)&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data Offset&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flags&lt;/td&gt;
&lt;td&gt;SYN, ACK, FIN&lt;/td&gt;
&lt;td&gt;ACK, PSH, URG&lt;/td&gt;
&lt;td&gt;FIN, ACK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Window&lt;/td&gt;
&lt;td&gt;YES (flow control)&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checksum&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Options&lt;/td&gt;
&lt;td&gt;SYN packets&lt;/td&gt;
&lt;td&gt;Sometimes&lt;/td&gt;
&lt;td&gt;Rarely&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Padding&lt;/td&gt;
&lt;td&gt;Only if options used&lt;/td&gt;
&lt;td&gt;–&lt;/td&gt;
&lt;td&gt;–&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;YES&lt;/td&gt;
&lt;td&gt;Sometimes (e.g., FIN can carry data)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Flow Visualization (Simplified)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client                             Server
  | SYN (seq=x) -----------------&amp;gt; |
  | &amp;lt;-------------- SYN+ACK (seq=y, ack=x+1)
  | ACK (ack=y+1) --------------&amp;gt; |

  | GET Request ----------------&amp;gt; |
  | &amp;lt;-------------- HTML Response
  | ACK ------------------------&amp;gt; |

  | &amp;lt;------------ FIN (I'm done)
  | ACK ------------------------&amp;gt; |
  | FIN ------------------------&amp;gt; |
  | &amp;lt;------------ ACK (closed)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What If We Remove a Field?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Removed Consequence&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Sequence Number&lt;/td&gt;
&lt;td&gt;No ordering, retransmission, reliability breaks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ACK Number&lt;/td&gt;
&lt;td&gt;Sender doesn’t know what was received&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Window Size&lt;/td&gt;
&lt;td&gt;Receiver might be overwhelmed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checksum&lt;/td&gt;
&lt;td&gt;Corrupt packets go undetected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flags&lt;/td&gt;
&lt;td&gt;Can’t start or stop connection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Offset&lt;/td&gt;
&lt;td&gt;Can’t know where data begins&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ports&lt;/td&gt;
&lt;td&gt;Can’t route data to app&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;This header isn't just a data structure — it's &lt;strong&gt;a conversation manager&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It allows two devices to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start talking&lt;/li&gt;
&lt;li&gt;Exchange data responsibly&lt;/li&gt;
&lt;li&gt;Detect losses&lt;/li&gt;
&lt;li&gt;Avoid overload&lt;/li&gt;
&lt;li&gt;End the talk gracefully&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TCP header is what turns the &lt;strong&gt;chaos of the internet&lt;/strong&gt; into something structured and reliable.&lt;/p&gt;

</description>
      <category>tcp</category>
      <category>networking</category>
      <category>network</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>How to Structure Projects in C++ or Qt: CMake and Directories</title>
      <dc:creator>Kaustuv Pokharel</dc:creator>
      <pubDate>Mon, 14 Jul 2025 16:54:29 +0000</pubDate>
      <link>https://forem.com/kastuv/how-to-structure-projects-in-c-or-qt-cmake-and-directories-5bbm</link>
      <guid>https://forem.com/kastuv/how-to-structure-projects-in-c-or-qt-cmake-and-directories-5bbm</guid>
      <description>&lt;p&gt;&lt;a href="https://youtube.com/playlist?list=PLeYV3WjZWmHzBttvbFuUhAFhrU5Q4Z3Y7" rel="noopener noreferrer"&gt;📺 Tutorial Playlist&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Qt6 projects are built with CMake, and CMake plays a crucial role in managing and structuring C++ projects.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is CMake?
&lt;/h2&gt;

&lt;p&gt;CMake is a build system manager; it tells tools like Make, Ninja, or Visual Studio how to compile our C++ code, what libraries to use, where to find headers, or how to organize the output.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: CMake is not a compiler. It is just a build system generator. It creates the actual build files (like Makefile, Ninja files, Visual Studio solutions) that tell the compiler like g++, clang++, etc., how to build our C++ project.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  One important term that keeps coming up when working with CMake is "target".
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a target?
&lt;/h3&gt;

&lt;p&gt;A target is the thing you're building.&lt;/p&gt;

&lt;h4&gt;
  
  
  What it Builds:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;add_executable()&lt;/code&gt;: A complete program with main()&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;add_library()&lt;/code&gt;: A reusable module (static/shared)&lt;/li&gt;
&lt;li&gt;Imported target: From system or library (like Qt6::Core)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;add_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;MyApp main.cpp&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;MyApp&lt;/code&gt; is a target name.&lt;/li&gt;
&lt;li&gt;It's an executable target.&lt;/li&gt;
&lt;li&gt;It will build into something like &lt;code&gt;MyApp.exe&lt;/code&gt; or &lt;code&gt;./MyApp&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why Targets Have Different Names in Folders?
&lt;/h2&gt;

&lt;p&gt;Because each folder has its own role, so its target is named after what it's doing.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
├── src/
│   ├── Core/
│   ├── UI/
│   └── Network/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each has:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="c1"&gt;# src/Core/CMakeLists.txt&lt;/span&gt;
&lt;span class="nb"&gt;add_library&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;CoreLib STATIC CoreEngine.cpp&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# src/UI/CMakeLists.txt&lt;/span&gt;
&lt;span class="nb"&gt;add_library&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;UILib STATIC MainWindow.cpp&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;add_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;MyApp main.cpp&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;MyApp CoreLib UILib&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CoreLib&lt;/code&gt; – reusable backend logic&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UILib&lt;/code&gt; – reusable UI components&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MyApp&lt;/code&gt; – the final app that uses them&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why So Many target_* Commands?
&lt;/h2&gt;

&lt;p&gt;Because you don't want to change things globally.&lt;br&gt;
Instead, you do everything per-target, so your build is modular and clean.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;target_include_directories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;CoreLib PRIVATE include/&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_compile_definitions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;UILib PUBLIC USE_QT&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Happens When You Run CMake?
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 1:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cmake &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-B&lt;/span&gt; build/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Step 2:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cmake &lt;span class="nt"&gt;--build&lt;/span&gt; build/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  What this does:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CMakeLists.txt&lt;/code&gt; is read.&lt;/li&gt;
&lt;li&gt;It finds what to build (targets).&lt;/li&gt;
&lt;li&gt;It finds what libraries and dependencies are needed.&lt;/li&gt;
&lt;li&gt;It generates a Makefile or &lt;code&gt;build.ninja&lt;/code&gt; or &lt;code&gt;.vcxproj&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You then build using make or ninja or an IDE.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  CMake Vocabulary You Must Know
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;cmake_minimum_required(VERSION &amp;lt;min_version&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Sets the minimum version of CMake required to build the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;cmake_minimum_required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;VERSION 3.16&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;project(&amp;lt;project_name&amp;gt; VERSION &amp;lt;version&amp;gt; LANGUAGES &amp;lt;lang1&amp;gt; &amp;lt;lang2&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Defines the project name, version, and language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;myApp VERSION 1.0 LANGUAGES CXX&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;set(&amp;lt;VAR_NAME&amp;gt; &amp;lt;value&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Assigns a value to a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;CMAKE_CXX_STANDARD 20&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;SOURCES main.cpp utils.cpp&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the variable as &lt;code&gt;${CMAKE_CXX_STANDARD}&lt;/code&gt; and &lt;code&gt;${SOURCES}&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;add_executable(&amp;lt;target_name&amp;gt; &amp;lt;source_files...&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Declares an executable to build.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;add_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;myApp main.cpp utils.cpp&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;add_library(&amp;lt;target_name&amp;gt; STATIC|SHARED &amp;lt;source_files...&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Creates a library (static or shared).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;add_executable&lt;/code&gt; → builds &lt;code&gt;.exe&lt;/code&gt; or &lt;code&gt;.app&lt;/code&gt; (for apps with &lt;code&gt;main()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;add_library&lt;/code&gt; → builds &lt;code&gt;.a&lt;/code&gt; or &lt;code&gt;.so&lt;/code&gt; (for reusable code or shared modules)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;target_link_libraries(&amp;lt;target_name&amp;gt; &amp;lt;libraries…&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Links one target to other targets, frameworks, or libraries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;myApp Qt6::Core Qt6::Quick customLib&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;target_include_directories(&amp;lt;target&amp;gt; PUBLIC|PRIVATE &amp;lt;dirs...&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Adds header paths for a target.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;target_include_directories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;customLib PUBLIC &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;CMAKE_CURRENT_SOURCE_DIR&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;find_package(&amp;lt;PackageName&amp;gt; [REQUIRED] [COMPONENTS ...])&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Finds system-installed packages like Qt, Boost, or OpenCV.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;find_package&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;Qt6 REQUIRED COMPONENTS Core Quick Widgets&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;find_path(&amp;lt;VAR&amp;gt; &amp;lt;header&amp;gt; [PATHS path1 path2 ...])&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Finds a directory containing a header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;find_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"path here"&lt;/span&gt; customLib.h&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;find_library(&amp;lt;VAR&amp;gt; &amp;lt;Library_name&amp;gt; [PATHS path1 path2 ...])&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Finds the path to a compiled library (.a, .so, .lib)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;find_library&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;MYLIB_LIBRARY myLib&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;add_subdirectory(&amp;lt;folder&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Adds a subfolder that contains its own &lt;code&gt;CMakeLists.txt&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;add_subdirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;src&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;add_subdirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;tests&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;install(TARGETS &amp;lt;target&amp;gt; DESTINATION &amp;lt;folder&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;install(FILES &amp;lt;file&amp;gt; DESTINATION &amp;lt;folder&amp;gt;)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Specifies install paths for targets or files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;install&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;TARGETS myApp DESTINATION bin&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;set_target_properties(&amp;lt;target&amp;gt; PROPERTIES &amp;lt;key&amp;gt; &amp;lt;value&amp;gt; ...)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Sets advanced properties like bundle info or output name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;set_target_properties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;myApp PROPERTIES MACOSX_BUNDLE TRUE&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;message&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Prints custom messages to the console during CMake configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;STATUS &lt;span class="s2"&gt;"Hello CMAKE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Modular Build System (Separation of Concerns)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What it means:
&lt;/h3&gt;

&lt;p&gt;Split your project into independent parts (modules). Each part has its own job.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;Imagine a project with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;UI/&lt;/code&gt; — Handles how things look (buttons, windows)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Network/&lt;/code&gt; — Handles internet communication&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Utils/&lt;/code&gt; — Handles helper functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each folder has its own &lt;code&gt;CMakeLists.txt&lt;/code&gt;, so they can be built separately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
├── UI/
│   ├── Window.qml
│   └── CMakeLists.txt
├── Network/
│   ├── NetManager.cpp
│   └── CMakeLists.txt
├── main.cpp
└── CMakeLists.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Why it helps:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Makes each module easy to understand and update&lt;/li&gt;
&lt;li&gt;You can focus on one part at a time&lt;/li&gt;
&lt;li&gt;Faster Builds with Better Parallelism&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Faster Builds with Better Parallelism
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What it means:
&lt;/h3&gt;

&lt;p&gt;When you change a file, only its module is rebuilt — not the whole project.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;You change &lt;code&gt;UI/Button.qml&lt;/code&gt;. Only the UI part needs to rebuild. The rest (like Network) is untouched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="c1"&gt;# In UI/CMakeLists.txt&lt;/span&gt;
&lt;span class="nb"&gt;add_library&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;UI STATIC
    Button.qml
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why it helps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Saves time when compiling&lt;/li&gt;
&lt;li&gt;Especially important in big projects with 100+ files&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Clean Project Structure
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What it means:
&lt;/h3&gt;

&lt;p&gt;Keeps your project organized.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Bad:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
├── main.cpp
├── NetManager.cpp
├── Window.qml
├── AppUtils.cpp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Good:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
├── UI/
├── Network/
├── Utils/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why it helps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Easy to find things&lt;/li&gt;
&lt;li&gt;Looks professional&lt;/li&gt;
&lt;li&gt;Encourages teamwork (different people work on different folders)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Scoped Dependencies and Linking
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What it means:
&lt;/h3&gt;

&lt;p&gt;Only include what's necessary in each module.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;The Network module needs Qt's network support. The UI module needs QtQuick.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Network/CMakeLists.txt&lt;/span&gt;
&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;Network Qt6::Network&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# UI/CMakeLists.txt&lt;/span&gt;
&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;UI Qt6::Quick&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why it helps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Smaller binary&lt;/li&gt;
&lt;li&gt;Faster to compile&lt;/li&gt;
&lt;li&gt;You avoid bloating the entire app with unused stuff&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Testing and CI/CD Ease
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What it means:
&lt;/h3&gt;

&lt;p&gt;You can write and run tests for each module.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Utils/
├── MathHelper.cpp
├── CMakeLists.txt
└── tests/
    ├── test_MathHelper.cpp
    └── CMakeLists.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why it helps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Test only what you need&lt;/li&gt;
&lt;li&gt;Catch bugs early&lt;/li&gt;
&lt;li&gt;Works great with GitHub Actions or GitLab CI&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Supports Plugins and Scalability
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What it means:
&lt;/h3&gt;

&lt;p&gt;Big apps like QGroundControl support plugins. Each plugin is a subdirectory.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Plugins/
├── GPSPlugin/
│   ├── GPS.cpp
│   └── CMakeLists.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the main &lt;code&gt;CMakeLists.txt&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;add_subdirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;Plugins/GPSPlugin&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why it helps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add or remove features easily&lt;/li&gt;
&lt;li&gt;Users can build only what they need&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Project structure example:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MyApp/
├── CMakeLists.txt        # Root
├── main.cpp
├── UI/
│   └── CMakeLists.txt
├── Network/
│   └── CMakeLists.txt
└── Utils/
    └── CMakeLists.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Root &lt;code&gt;CMakeLists.txt&lt;/code&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;cmake_minimum_required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;VERSION 3.16&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;project&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;MyApp&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;add_subdirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;UI&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;add_subdirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;Network&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;add_subdirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;Utils&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;add_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;MyApp main.cpp&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;MyApp
    UI
    Network
    Utils
    Qt6::Core
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  UI/CMakeLists.txt:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;add_library&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;UI STATIC
    MainWindow.qml
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;target_link_libraries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;UI Qt6::Quick&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Structuring your C++ or Qt project with CMake properly from the beginning will save you hours in debugging, rebuilding, and scaling. With clear module boundaries, proper target scopes, and clean linking, your project becomes professional, maintainable, and ready for collaboration or production.&lt;/p&gt;

</description>
      <category>qt</category>
      <category>qml</category>
      <category>cpp</category>
      <category>cmake</category>
    </item>
    <item>
      <title>VULKAN- SETUP Summary</title>
      <dc:creator>Kaustuv Pokharel</dc:creator>
      <pubDate>Sat, 25 Jan 2025 03:23:26 +0000</pubDate>
      <link>https://forem.com/kastuv/vulkan-setup-summary-2ea4</link>
      <guid>https://forem.com/kastuv/vulkan-setup-summary-2ea4</guid>
      <description>&lt;h3&gt;
  
  
  Deep Dive into the Code Architecture for REVISING IN THE FUTURE, for personal use.
&lt;/h3&gt;

&lt;p&gt;This code is designed to &lt;strong&gt;encapsulate the Vulkan setup process into a manageable and reusable structure&lt;/strong&gt;, following object-oriented programming principles. This organization makes it easier to read, maintain, and expand the code as your Vulkan application grows in complexity. Here's a &lt;strong&gt;step-by-step breakdown&lt;/strong&gt; of the class &lt;code&gt;HelloTriangleApplication&lt;/code&gt;, its functions, and why each function exists.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Class Structure&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloTriangleApplication&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;initWindow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;initVulkan&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;mainLoop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the &lt;strong&gt;main driver class&lt;/strong&gt; for the Vulkan application. It encapsulates the entire lifecycle of your program:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;initWindow()&lt;/code&gt;&lt;/strong&gt;: Initializes the GLFW window for displaying Vulkan's output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;initVulkan()&lt;/code&gt;&lt;/strong&gt;: Sets up all Vulkan resources, such as the instance, physical device, logical device, and queues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;mainLoop()&lt;/code&gt;&lt;/strong&gt;: The rendering loop, where you keep the application running and process user input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;cleanup()&lt;/code&gt;&lt;/strong&gt;: Cleans up all resources when the application exits.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This structure ensures that each part of the program has a clear, focused purpose.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why use functions to break things up?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Code clarity&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instead of dumping all Vulkan setup code in &lt;code&gt;main()&lt;/code&gt;, breaking it into functions like &lt;code&gt;initVulkan&lt;/code&gt; and &lt;code&gt;cleanup&lt;/code&gt; makes it more readable and easier to understand.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Maintainability&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you need to modify the Vulkan setup or rendering loop, you can do so in an isolated function without breaking the rest of the code.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Reusability&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you create another Vulkan project in the future, you can reuse some of these functions, like &lt;code&gt;createInstance&lt;/code&gt; or &lt;code&gt;createLogicalDevice&lt;/code&gt;, with little modification.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;







&lt;h2&gt;
  
  
  &lt;strong&gt;Public Method: &lt;code&gt;run()&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;initWindow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;initVulkan&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;mainLoop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;run&lt;/code&gt; function is the &lt;strong&gt;entry point&lt;/strong&gt; for the Vulkan application. It acts as the &lt;strong&gt;master controller&lt;/strong&gt;, calling other functions in a logical sequence.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;initWindow()&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sets up the GLFW window for Vulkan rendering.&lt;/li&gt;
&lt;li&gt;Ensures Vulkan can create a surface to draw on.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;initVulkan()&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handles the complex Vulkan initialization process:

&lt;ul&gt;
&lt;li&gt;Connect to Vulkan (&lt;code&gt;createInstance&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Choose a GPU (&lt;code&gt;pickPhysicalDevice&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Set up an interface to communicate with the GPU (&lt;code&gt;createLogicalDevice&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;mainLoop()&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keeps the application running until the user closes the window.&lt;/li&gt;
&lt;li&gt;Processes input and, eventually, issues rendering commands.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;cleanup()&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Frees up memory and destroys Vulkan objects to prevent memory leaks.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;







&lt;h2&gt;
  
  
  &lt;strong&gt;Private Member Functions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;These functions handle specific tasks within the Vulkan lifecycle. Let’s break them down &lt;strong&gt;one by one&lt;/strong&gt;, discussing why they exist, how they’re implemented, and how they fit into the overall flow.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;1. &lt;code&gt;initWindow()&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;initWindow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;glfwInit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;glfwWindowHint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;GLFW_CLIENT_API&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GLFW_NO_API&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;glfwWindowHint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;GLFW_RESIZABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GLFW_FALSE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;glfwCreateWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WIDTH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HEIGHT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Vulkan"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  &lt;strong&gt;Purpose:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This function initializes the &lt;strong&gt;GLFW windowing system&lt;/strong&gt; and creates a window that Vulkan can use for rendering. Vulkan itself doesn’t handle window creation—it relies on external libraries like GLFW.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Steps:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;glfwInit()&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initializes the GLFW library, which manages the window.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API)&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tells GLFW not to create an OpenGL context, as Vulkan doesn’t need it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE)&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Makes the window non-resizable to simplify Vulkan’s handling of swap chains.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;glfwCreateWindow(...)&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates the actual window.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Why is it a separate function?&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Window initialization is independent of Vulkan initialization.&lt;/li&gt;
&lt;li&gt;It keeps the &lt;code&gt;run()&lt;/code&gt; function clean and focused.&lt;/li&gt;
&lt;/ul&gt;







&lt;h3&gt;
  
  
  &lt;strong&gt;2. &lt;code&gt;initVulkan()&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;initVulkan&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;createInstance&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;pickPhysicalDevice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;createLogicalDevice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  &lt;strong&gt;Purpose:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This function orchestrates all the Vulkan setup steps. It calls helper functions (&lt;code&gt;createInstance&lt;/code&gt;, &lt;code&gt;pickPhysicalDevice&lt;/code&gt;, etc.) to initialize Vulkan in a modular way.&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Why is it structured like this?&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Vulkan initialization is complex and involves multiple steps.&lt;/li&gt;
&lt;li&gt;Each step has a specific purpose:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;createInstance&lt;/code&gt;: Connects to Vulkan.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pickPhysicalDevice&lt;/code&gt;: Chooses a GPU.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;createLogicalDevice&lt;/code&gt;: Sets up communication with the GPU.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;







&lt;h3&gt;
  
  
  &lt;strong&gt;3. &lt;code&gt;createInstance()&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createInstance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enableValidationLayers&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;checkValidationLayerSupport&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"validation layers requested, but not available!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;VkApplicationInfo&lt;/span&gt; &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_APPLICATION_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pApplicationName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello Triangle"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;applicationVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_MAKE_VERSION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pEngineName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"No Engine"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;engineVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_MAKE_VERSION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apiVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_API_VERSION_1_0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;VkInstanceCreateInfo&lt;/span&gt; &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pApplicationInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;glfwExtensionCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;glfwExtensions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;glfwExtensions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;glfwGetRequiredInstanceExtensions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;glfwExtensionCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;requiredExtensions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;glfwExtensionCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;requiredExtensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emplace_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;glfwExtensions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enabledExtensionCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requiredExtensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ppEnabledExtensionNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requiredExtensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vkCreateInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;VK_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"failed to create instance!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  &lt;strong&gt;Purpose:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This function creates a Vulkan &lt;strong&gt;instance&lt;/strong&gt;, which is the foundation for all Vulkan operations.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Details:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Validation Layers&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debugging tools to catch errors during development.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;checkValidationLayerSupport()&lt;/code&gt; ensures the requested validation layers are available.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;VkApplicationInfo&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provides basic information about the app (name, version, etc.).&lt;/li&gt;
&lt;li&gt;Helps Vulkan optimize for specific apps.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Extensions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extensions allow Vulkan to interface with the operating system for windowing.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Why is it a separate function?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Creating an instance is a distinct step in Vulkan’s setup process, so it deserves its own function for clarity and modularity.&lt;/p&gt;







&lt;h3&gt;
  
  
  &lt;strong&gt;4. &lt;code&gt;pickPhysicalDevice()&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;pickPhysicalDevice&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;deviceCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;vkEnumeratePhysicalDevices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"failed to find GPUs with Vulkan support!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;VkPhysicalDevice&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;vkEnumeratePhysicalDevices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isDeviceSuitable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;physicalDevice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;physicalDevice&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;VK_NULL_HANDLE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"failed to find a suitable GPU!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  &lt;strong&gt;Purpose:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This function selects a Vulkan-compatible GPU to use for rendering.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Details:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;vkEnumeratePhysicalDevices&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieves a list of Vulkan-compatible GPUs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;isDeviceSuitable()&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks if a GPU supports the required features (e.g., graphics queues).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Why is it a separate function?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;GPU selection is a key part of Vulkan initialization, and isolating it makes the code easier to read and debug.&lt;/p&gt;







&lt;h3&gt;
  
  
  &lt;strong&gt;5. &lt;code&gt;createLogicalDevice()&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createLogicalDevice&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QueueFamilyIndices&lt;/span&gt; &lt;span class="n"&gt;indices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findQueueFamilies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;physicalDevice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;VkDeviceQueueCreateInfo&lt;/span&gt; &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queueFamilyIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;indices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;graphicsFamily&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queueCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;queuePriority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pQueuePriorities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;queuePriority&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;VkPhysicalDeviceFeatures&lt;/span&gt; &lt;span class="n"&gt;deviceFeatures&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;

    &lt;span class="n"&gt;VkDeviceCreateInfo&lt;/span&gt; &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pQueueCreateInfos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queueCreateInfoCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pEnabledFeatures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;deviceFeatures&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enabledExtensionCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vkCreateDevice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;physicalDevice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;VK_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"failed to create logical device!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;vkGetDeviceQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;graphicsFamily&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;graphicsQueue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  &lt;strong&gt;Purpose:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This function creates a &lt;strong&gt;logical device&lt;/strong&gt; to communicate with the GPU.&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Key Details:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Queues&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;GPUs support multiple &lt;strong&gt;queues&lt;/strong&gt; for different tasks (graphics, compute, etc.).&lt;/li&gt;
&lt;li&gt;This function sets up a queue for graphics commands.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Why is it a separate function?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Logical device creation is distinct from physical device selection, so separating it improves modularity and readability.&lt;/p&gt;




</description>
    </item>
    <item>
      <title>Introduction: VULKAN API</title>
      <dc:creator>Kaustuv Pokharel</dc:creator>
      <pubDate>Thu, 19 Dec 2024 02:23:19 +0000</pubDate>
      <link>https://forem.com/kastuv/introduction-vulkan-api-1jp8</link>
      <guid>https://forem.com/kastuv/introduction-vulkan-api-1jp8</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;1. Vulkan applications start by creating a Vulkan Instance that defines the Vulkan application.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A Vulkan instance is the foundation of a Vulkan application. It represents the connection between your application and the Vulkan library. The instance provides access to the Vulkan API and allows you to configure global settings like enabling extensions or validation layers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to Create a Vulkan Instance&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Specify application and engine information (optional but good practice).&lt;/li&gt;
&lt;li&gt;List desired extensions and validation layers.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;vkCreateInstance&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vulkan/vulkan.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="c1"&gt;// Main function&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;VkInstance&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Application Info (optional)&lt;/span&gt;
    &lt;span class="n"&gt;VkApplicationInfo&lt;/span&gt; &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_APPLICATION_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pApplicationName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Vulkan App"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;applicationVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_MAKE_VERSION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pEngineName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"No Engine"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;engineVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_MAKE_VERSION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apiVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_API_VERSION_1_3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Instance Create Info&lt;/span&gt;
    &lt;span class="n"&gt;VkInstanceCreateInfo&lt;/span&gt; &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pApplicationInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;appInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Create Vulkan Instance&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vkCreateInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;VK_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cerr&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Failed to create Vulkan instance!"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Vulkan instance created successfully!"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Cleanup&lt;/span&gt;
    &lt;span class="n"&gt;vkDestroyInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;2. Enumerate Physical Devices to pick an adequate GPU.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Physical devices represent actual GPUs connected to your machine. Vulkan allows you to enumerate them and pick the most suitable one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Enumerate all physical devices using &lt;code&gt;vkEnumeratePhysicalDevices&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Evaluate their properties and features.&lt;/li&gt;
&lt;li&gt;Choose a device that meets your needs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key Properties to Check&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Device type (e.g., discrete GPU, integrated GPU).&lt;/li&gt;
&lt;li&gt;Queue families (e.g., graphics, compute).&lt;/li&gt;
&lt;li&gt;Supported extensions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;pickPhysicalDevice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;VkInstance&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;VkPhysicalDevice&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;physicalDevice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;deviceCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;vkEnumeratePhysicalDevices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to find GPUs with Vulkan support!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;VkPhysicalDevice&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;vkEnumeratePhysicalDevices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;deviceCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;VkPhysicalDeviceProperties&lt;/span&gt; &lt;span class="n"&gt;deviceProperties&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;vkGetPhysicalDeviceProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;deviceProperties&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Found GPU: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;deviceProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deviceName&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// Check for desired features here (e.g., graphics queue support).&lt;/span&gt;
        &lt;span class="n"&gt;physicalDevice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to find a suitable GPU!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;3. Create Logical Device to interface with the chosen Physical Device.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A logical device is an abstraction over the physical device that allows you to interface with it. When creating a logical device, you also request specific features and queues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define required queue families.&lt;/li&gt;
&lt;li&gt;Specify desired extensions.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;vkCreateDevice&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createLogicalDevice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;VkPhysicalDevice&lt;/span&gt; &lt;span class="n"&gt;physicalDevice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;VkDevice&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;logicalDevice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Define the queue we need (e.g., graphics queue)&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;queuePriority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;VkDeviceQueueCreateInfo&lt;/span&gt; &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queueFamilyIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Assuming index 0 is valid for graphics&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queueCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pQueuePriorities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;queuePriority&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Specify device features (if any)&lt;/span&gt;
    &lt;span class="n"&gt;VkPhysicalDeviceFeatures&lt;/span&gt; &lt;span class="n"&gt;deviceFeatures&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;

    &lt;span class="c1"&gt;// Create the logical device&lt;/span&gt;
    &lt;span class="n"&gt;VkDeviceCreateInfo&lt;/span&gt; &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queueCreateInfoCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pQueueCreateInfos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;queueCreateInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pEnabledFeatures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;deviceFeatures&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vkCreateDevice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;physicalDevice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;logicalDevice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;VK_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to create logical device!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Logical device created successfully!"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;4. Multiple types of queue (queue families) on a Physical Device can be assigned to a Logical Device. Each one processes different types of command.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Queue families on a physical device process different types of commands (e.g., rendering, compute, transfer). Each queue family has capabilities such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Graphics commands.&lt;/li&gt;
&lt;li&gt;Compute operations.&lt;/li&gt;
&lt;li&gt;Data transfers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Finding Queue Families&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Query queue family properties with &lt;code&gt;vkGetPhysicalDeviceQueueFamilyProperties&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check if a family supports required operations (e.g., graphics).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;findQueueFamilies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;VkPhysicalDevice&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;queueFamilyCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;vkGetPhysicalDeviceQueueFamilyProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;queueFamilyCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;VkQueueFamilyProperties&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;queueFamilies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queueFamilyCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;vkGetPhysicalDeviceQueueFamilyProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;queueFamilyCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;queueFamilies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;queueFamilies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queueFamilies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;queueFlags&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VK_QUEUE_GRAPHICS_BIT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Found a graphics queue&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to find a suitable queue family!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;5. Extensions can be applied, such as extensions to enable displaying to a window.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Extensions enhance Vulkan's functionality, such as enabling window surface creation. Extensions are specified during instance or logical device creation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example with GLFW&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;glfwExtensionCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;glfwExtensions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;glfwGetRequiredInstanceExtensions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;glfwExtensionCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;VkInstanceCreateInfo&lt;/span&gt; &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enabledExtensionCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;glfwExtensionCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ppEnabledExtensionNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;glfwExtensions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;6. Validation Layers are optional and allow us to validate our code at run-time.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Validation layers help debug Vulkan applications. They catch errors during development but can be disabled in release builds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example to Enable Validation Layers&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;validationLayers&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"VK_LAYER_KHRONOS_validation"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;VkInstanceCreateInfo&lt;/span&gt; &lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enabledLayerCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;createInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ppEnabledLayerNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;validationLayers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vulkan</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Solid Principle Tricks</title>
      <dc:creator>Kaustuv Pokharel</dc:creator>
      <pubDate>Thu, 28 Nov 2024 09:56:21 +0000</pubDate>
      <link>https://forem.com/kastuv/solid-principle-tricks-5enh</link>
      <guid>https://forem.com/kastuv/solid-principle-tricks-5enh</guid>
      <description>&lt;p&gt;Applying the &lt;strong&gt;SOLID principles&lt;/strong&gt; can indeed feel overwhelming, especially since the implementation patterns may appear repetitive or hard to differentiate. The key lies in understanding the &lt;strong&gt;core intent&lt;/strong&gt; behind each principle and applying them &lt;strong&gt;pragmatically&lt;/strong&gt; rather than mechanically. Here's a guide to help you consistently adhere to the principles when designing classes in C++:&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;1. Single Responsibility Principle (SRP)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Thumb Rule:&lt;/strong&gt; Each class should have &lt;strong&gt;only one reason to change&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Trick:&lt;/strong&gt; Use &lt;strong&gt;verbs&lt;/strong&gt; to define class responsibilities. If a class name contains "and" or does too many things, split it.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Correct:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;OrderProcessor&lt;/code&gt; processes orders.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;InvoiceGenerator&lt;/code&gt; generates invoices.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;❌ Wrong:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;OrderAndInvoiceProcessor&lt;/code&gt; both processes orders and generates invoices.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Open/Closed Principle (OCP)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Thumb Rule:&lt;/strong&gt; Classes should be &lt;strong&gt;open for extension&lt;/strong&gt; but &lt;strong&gt;closed for modification&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Trick:&lt;/strong&gt; When adding new features, prefer &lt;strong&gt;inheritance&lt;/strong&gt;, &lt;strong&gt;interfaces&lt;/strong&gt;, or &lt;strong&gt;polymorphism&lt;/strong&gt; over modifying existing code. Avoid directly adding logic to old classes.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Correct:

&lt;ul&gt;
&lt;li&gt;Add new functionality by creating a subclass or implementing an interface.
&lt;/li&gt;
&lt;li&gt;Use abstract base classes for extensibility.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;❌ Wrong:

&lt;ul&gt;
&lt;li&gt;Modify existing classes to handle new cases.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Litmus Test:&lt;/strong&gt; Ask yourself, "Can I add new functionality without touching this class?"  &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Liskov Substitution Principle (LSP)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Thumb Rule:&lt;/strong&gt; Subclasses should be &lt;strong&gt;substitutable&lt;/strong&gt; for their base classes.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Trick:&lt;/strong&gt; Ensure that overriding methods don’t change the behavior expected from the base class. Use the &lt;strong&gt;"is-a"&lt;/strong&gt; test: If a subclass is not truly a type of its base class, rethink the inheritance.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Correct:

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;Rectangle&lt;/code&gt; and &lt;code&gt;Square&lt;/code&gt; both override &lt;code&gt;Shape&lt;/code&gt;’s &lt;code&gt;getArea()&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;Bird&lt;/code&gt; can fly unless there's a specific reason to separate flightless birds.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;❌ Wrong:

&lt;ul&gt;
&lt;li&gt;Violating contracts of the base class. Example: A &lt;code&gt;Bird&lt;/code&gt; subclass like &lt;code&gt;Penguin&lt;/code&gt; that fails to implement the flying behavior expected from &lt;code&gt;Bird&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Litmus Test:&lt;/strong&gt; Ask, "Can this subclass replace its parent without breaking functionality?"  &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;4. Interface Segregation Principle (ISP)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Thumb Rule:&lt;/strong&gt; &lt;strong&gt;Smaller is better&lt;/strong&gt;. Clients should not depend on interfaces they don’t use.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Trick:&lt;/strong&gt; Look at the &lt;strong&gt;clients&lt;/strong&gt; of the interface. If different clients need different subsets of methods, split the interface.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Correct:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Printer&lt;/code&gt;, &lt;code&gt;Scanner&lt;/code&gt;, and &lt;code&gt;Fax&lt;/code&gt; interfaces instead of one &lt;code&gt;Machine&lt;/code&gt; interface.
&lt;/li&gt;
&lt;li&gt;Multiple inheritance for interfaces if needed.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;❌ Wrong:

&lt;ul&gt;
&lt;li&gt;A single monolithic interface (&lt;code&gt;Machine&lt;/code&gt;) with &lt;code&gt;print()&lt;/code&gt;, &lt;code&gt;scan()&lt;/code&gt;, and &lt;code&gt;fax()&lt;/code&gt; methods that unrelated clients must implement.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Litmus Test:&lt;/strong&gt; Ask, "Does this class implement unused methods from an interface?"&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;5. Dependency Inversion Principle (DIP)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Thumb Rule:&lt;/strong&gt; High-level classes should &lt;strong&gt;not depend&lt;/strong&gt; on low-level classes but on abstractions.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Trick:&lt;/strong&gt; Always code against &lt;strong&gt;interfaces or abstract classes&lt;/strong&gt;, not concrete implementations. Use &lt;strong&gt;dependency injection&lt;/strong&gt; (constructor injection or setter injection).  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Correct:

&lt;ul&gt;
&lt;li&gt;Depend on a &lt;code&gt;Logger&lt;/code&gt; interface, with &lt;code&gt;FileLogger&lt;/code&gt; or &lt;code&gt;ConsoleLogger&lt;/code&gt; passed at runtime.
&lt;/li&gt;
&lt;li&gt;Use an abstract &lt;code&gt;Database&lt;/code&gt; class with &lt;code&gt;MySQLDatabase&lt;/code&gt; or &lt;code&gt;PostgresDatabase&lt;/code&gt; as concrete implementations.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;❌ Wrong:

&lt;ul&gt;
&lt;li&gt;Hardcoding dependencies inside a class (&lt;code&gt;FileLogger logger;&lt;/code&gt; instead of &lt;code&gt;Logger* logger;&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Litmus Test:&lt;/strong&gt; Ask, "Can I easily replace the dependency without modifying this class?"&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;General Guidelines for Ensuring SOLID Principles&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Start with Abstractions&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When designing a class, ask yourself, "What does this class do?" and "What abstractions or contracts does it need to fulfill?"
&lt;/li&gt;
&lt;li&gt;Example: Use abstract base classes or interfaces to define behaviors (&lt;code&gt;Drawable&lt;/code&gt;, &lt;code&gt;Logger&lt;/code&gt;, &lt;code&gt;Database&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Keep Classes Small and Focused&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Each class should represent a &lt;strong&gt;single, well-defined concept&lt;/strong&gt; or functionality. If a class feels too large, break it down into smaller, cohesive components.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. &lt;strong&gt;Avoid Tight Coupling&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Use dependency injection (pass dependencies through constructors or setters).
&lt;/li&gt;
&lt;li&gt;Prefer composition over inheritance if it better represents the relationship.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Design for Change&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Anticipate potential extensions or changes. Use polymorphism or templates in C++ for behaviors that might change in the future.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  5. &lt;strong&gt;Test with Real Use Cases&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;While designing, imagine how the classes will be used in client code. Adjust the design based on how intuitive and reusable it feels.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Practical Thumb Rules&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Design for Interfaces:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start by designing interfaces (&lt;code&gt;AbstractLogger&lt;/code&gt;, &lt;code&gt;Database&lt;/code&gt;) and implement concrete classes (&lt;code&gt;FileLogger&lt;/code&gt;, &lt;code&gt;PostgresDatabase&lt;/code&gt;) later.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Favor Composition Over Inheritance:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When possible, prefer using member objects (&lt;code&gt;Has-A&lt;/code&gt; relationship) rather than subclassing (&lt;code&gt;Is-A&lt;/code&gt; relationship). This often naturally adheres to SOLID principles.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Review Class Names:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class name should clearly describe its purpose. If it’s vague or contains multiple responsibilities, it’s time to refactor.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use Patterns Smartly:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design patterns like Factory, Adapter, and Dependency Injection naturally enforce SOLID principles when used correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Iterative Design:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your first design may not be SOLID. Refactor over time as requirements evolve.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Red Flags to Watch Out For&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fat classes:&lt;/strong&gt; Classes with many unrelated methods (violates SRP and ISP).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concrete dependencies:&lt;/strong&gt; Classes hardcode specific types instead of using abstractions (violates DIP).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rigid inheritance hierarchies:&lt;/strong&gt; Subclasses break base class contracts (violates LSP).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global variables:&lt;/strong&gt; They lead to tight coupling and are harder to test or replace.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hardcoded logic:&lt;/strong&gt; Makes extending functionality difficult (violates OCP).&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;By keeping these rules and tricks in mind, you'll find it easier to design classes that naturally adhere to the SOLID principles, making your code more maintainable, flexible, and testable.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Rocket Game C++/QT/QML</title>
      <dc:creator>Kaustuv Pokharel</dc:creator>
      <pubDate>Thu, 18 Apr 2024 20:35:43 +0000</pubDate>
      <link>https://forem.com/kastuv/rocket-game-cqtqml-38id</link>
      <guid>https://forem.com/kastuv/rocket-game-cqtqml-38id</guid>
      <description>&lt;p&gt;It's been a year since I started writing code. I'm currently a second-year software engineering student, and my love for learning C++ knows no bounds. I'm always eager to dive into new challenges and explore the vast world of programming. Among the many tools in my arsenal, the QT framework stands out as my favorite for crafting beautiful GUI applications.&lt;/p&gt;

&lt;p&gt;After dabbling in desktop apps, Android apps, iOS apps, and even whipping up a website or two, I've now set my sights on creating a game. And why keep all the excitement to myself? I've decided to document my journey here, hoping to share insights and help fellow learners along the way.&lt;/p&gt;

&lt;p&gt;I will not waste your time on my story now. So I hop onto QT Creator to start my app. I name my app "Rocketify" and keep on the destination folder. I chose my MacOs kit of the current release. I chose the "QTQuick Compact" to make my application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import QtQuick
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Rocketify")

    Rectangle
    {
        id: rectangle;
        height: 100;
        width: 100;
        color: "red";

        x: (parent.width-width)/2
        y: (parent.height-height)/2;
        //focus here is important in a sense that it allows the system to notice the keys events when we press one
        focus: true

        Keys.onPressed:
        {
            if(event.key === Qt.Key_Left)
            {
                rectangle.x -= 10;
            }
            else if(event.key === Qt.Key_Right)
            {
                rectangle.x += 10;
            }
        }
    }

        // Item {
        //         focus: true
        //         Keys.onPressed: {
        //             // This captures key events at the window level if needed
        //         }
        //     }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voilà! Here's a sleek rectangle ready to take flight in our game. With every press of the left or right arrow keys, it gracefully glides across the screen. x,y in the code are the initial position where I am keeping my rectangle at the center of the window. Let the games begin!&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>cpp</category>
      <category>qt</category>
      <category>qml</category>
    </item>
    <item>
      <title>Memory and Variables</title>
      <dc:creator>Kaustuv Pokharel</dc:creator>
      <pubDate>Tue, 16 Jan 2024 21:47:57 +0000</pubDate>
      <link>https://forem.com/kastuv/memory-and-variables-2b36</link>
      <guid>https://forem.com/kastuv/memory-and-variables-2b36</guid>
      <description>&lt;p&gt;We know what memory is when we think about it. But do we really know what it is. Now I am reading a book called &lt;strong&gt;"Memory as a programming concept in C and C++"&lt;/strong&gt; where it teaches me about how memory is working or how system manages memory for C++.&lt;br&gt;
Memory is a fundamental aspect of computer systems, crucial for storing and managing data during program execution. While the concept of memory may seem straightforward, delving into the intricacies of how it works and how variables interact with it can deepen our understanding.&lt;/p&gt;

&lt;p&gt;In the realm of programming, variables act as data containers, holding information assigned to them. For instance, consider the declaration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;

int main() {
    // Variable declaration and assignment
    int a = 10;

    // Display the value and memory address of the variable
    std::cout &amp;lt;&amp;lt; "Value of 'a': " &amp;lt;&amp;lt; a &amp;lt;&amp;lt; std::endl;
    std::cout &amp;lt;&amp;lt; "Memory address of 'a': " &amp;lt;&amp;lt; &amp;amp;a &amp;lt;&amp;lt; std::endl;

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in C++. It seems straightforward, but when we examine the final machine code or load module after compilation, the notion of a variable appears to vanish. During program execution, there's no explicit trace of a variable.&lt;/p&gt;

&lt;p&gt;A load module, a term encountered during the reading, is a memory section containing the compiled machine-level code derived from the source code. This code, in the form of executable files (.exe), dynamic link libraries (.dll), or shared objects (.so), is crucial for the execution of computer programs. Load modules encapsulate the compiled code, enabling easy distribution and execution across different systems.&lt;/p&gt;

&lt;p&gt;Let's unravel how variables and memory intertwine. Memory, in its essence, can only store binary code. Variables, akin to buckets in memory, are memory segments that store data. However, these buckets can only store binary information. Consequently, anything assigned to a variable is converted into binary within the load module.&lt;/p&gt;

&lt;p&gt;To understand how more data can be accommodated in memory, consider each variable as having a specific size. For instance, a char data type typically occupies 1 byte, while an int might take 2 bytes on a 16-bit processor, 4 bytes on a 32-bit processor, and 8 bytes on a 64-bit processor. It's akin to having different-sized buckets – if you try to pour more water (data) than the bucket can hold, it will overflow. The existence of various data types (bucket sizes) such as double, float, and long double helps manage this, preventing overflow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;

int main() {
    // Memory overflow example
    short smallBucket = 30000;  // 2 bytes
    char overflow = 'A';       // 1 byte

    // Attempting to overflow the small bucket
    smallBucket = smallBucket + overflow;

    // Display the result (may lead to unexpected behavior due to overflow)
    std::cout &amp;lt;&amp;lt; "Result of overflow: " &amp;lt;&amp;lt; smallBucket &amp;lt;&amp;lt; std::endl;

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's an approximate breakdown of what might happen:&lt;/p&gt;

&lt;p&gt;The value of overflow ('A') is promoted to its ASCII value (65 in decimal).&lt;br&gt;
The sum of smallBucket and the ASCII value is calculated.&lt;br&gt;
The result is stored back in smallBucket.&lt;br&gt;
The unexpected behavior arises from the fact that adding the ASCII value of 'A' (65) to smallBucket might exceed the representable range for a short variable (which is typically -32768 to 32767). This leads to an overflow and undefined behaviour.&lt;/p&gt;

&lt;p&gt;If you want to see the actual machine code, you can use a disassembler on the compiled executable. Keep in mind that the specific machine code will vary based on your compiler, platform, and compiler optimization settings.&lt;/p&gt;

&lt;p&gt;When a program runs, the computer searches the memory segment for the designated bucket and then examines the binary content stored within. Only after this process does the system proceed to analyze the rest of the instructions. It's a sequential journey – finding the bucket (memory location), inspecting the binary contents (data), and then executing subsequent instructions.&lt;/p&gt;

&lt;p&gt;In essence, the interplay between variables, memory, and the load module is a crucial aspect of understanding how a program operates at the machine level. This knowledge empowers programmers to write efficient, error-free code that maximizes the potential of the underlying hardware.&lt;/p&gt;

</description>
      <category>memory</category>
      <category>cpp</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
