Get Started Now

Intro to C++ Programming

Starting from the fundamentals, become a C++ software engineer, step by step.

LATEST UPDATES

Screenshot from Cyberpunk 2077
Module One

Intro to C++ Programming

Starting from the basics, become a C++ software engineer, step by step

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt
Screenshot from Warhammer: Total War
Screenshot from Cyberpunk 2077
Capstone Project

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt
Screenshot from Warhammer: Total War
Screenshot from Cyberpunk 2077
Module Two

Professional C++

Learn C++ and SDL development by recreating classic retro games

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt
Screenshot from Warhammer: Total War
MOST POPULAR

Implementing an Application Loop

Step-by-step guide on creating the SDL2 application and event loops for interactive games
Abstract art representing computer programming
Ryan McCombe
Updated
Published

The first step of implementing any application is writing the foundational code that keeps the program running until we or the user decide it’s time to quit. This code is often called the application loop, main loop, or, if we’re making a game, the game loop.

Typically, every iteration of the loop involves 3 steps:

  1. Process any events, such as user input. This involves a second, nested loop often called the event loop.
  2. Ask all of our objects to update themselves, ready to be rendered to the screen. Any events that occurred in step 1 can influence this process
  3. Render a new frame to the screen, so the user can see the changes.

Managing Window Input Focus

Learn how to manage and control window input focus in SDL applications, including how to create, detect, and manipulate window focus states.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

On most platforms, multiple applications can be open at once. However, when the user is providing input on their keyboard, they typically only intend to provide that input to one of the open applications.

Users can select which application is active by, for example, clicking within its window.

Operating systems typically apply more vibrant styling to the active window. In the following example, the left window is inactive, while the right is active.

Accordingly, the user will not expect the left window to react to keyboard events, as their input is likely intended for the right window instead:

Managing Mouse Focus with SDL2

Learn how to track and respond to mouse focus events in SDL2, including handling multiple windows and customizing focus-related click behavior.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In desktop environments, users can have multiple windows open at once. On most platforms, only a single window can have input focus at a time, which is typically gained by the user clicking on the window:

Screenshot showing windows with and without focus

This is referred to as input focus, and we covered it in more detail in our earlier lesson on keyboard input:

In this lesson, we’ll introduce the related concept of mouse focus. A window has mouse focus if the user’s pointer is currently hovering over it. The window with mouse focus is not necessarily the same as the window with input focus.

Mouse State

Learn how to monitor mouse position and button states in real-time using SDL's state query functions
Abstract art representing computer programming
Ryan McCombe
Updated
Published

Previously, we introduced how SDL dispatches events when the player moves their mouse within our window, or when they click their mouse buttons when our window has input focus:

#include <SDL.h>
#include <iostream>
#include "Window.h"

void HandleButton(SDL_MouseButtonEvent& E) {
  if (E.button == SDL_BUTTON_LEFT) {
    if (E.state == SDL_PRESSED) {
      std::cout << "Left button pressed at ("
        << E.x << ", " << E.y << ")\n";
    } else if (E.state == SDL_RELEASED) {
      std::cout << "Left button released at ("
        << E.x << ", " << E.y << ")\n";
    }
  }
}

void HandleMotion(SDL_MouseMotionEvent& E) {
  std::cout << "Mouse moved to ("
    << E.x << ", " << E.y << ")\n";
}

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  Window GameWindow;
  SDL_Event E;

  while (true) {
    while (SDL_PollEvent(&E)) {
      if (E.type == SDL_MOUSEBUTTONDOWN
          || E.type == SDL_MOUSEBUTTONUP) {
        HandleButton(E.button);
      } else if (E.type == SDL_MOUSEMOTION) {
        HandleMotion(E.motion);
      }
    }
    GameWindow.Update();
  }

  SDL_Quit();
  return 0;
}
Mouse moved to (226, 4)
Left button pressed at (226, 4)
Mouse moved to (227, 4)
Mouse moved to (228, 5)
Left button released at (228, 5)

This allows us to react to our user’s mouse input, but it is not the only approach that we can use. Instead of reacting to events, we are free to query the mouse’s current position and which buttons are pressed at any time.

We covered how to track mouse motion and clicking through the event loop in our introductory lesson:

Setting up a C++ Development Environment

Getting our computer set up so we can create and build C++ programs. Then, creating our very first application
3D art showing a blacksmith character
Ryan McCombe
Updated
Published

In this introductory lesson, we'll create our first program in C++! We will start very simply. We'll just be outputting some text to the screen.

Things will ramp up quickly, but first, our priority is to make sure we have everything we need to start writing and running C++ programs.

If you do not have a development environment set up yet, let's start by going through some options we have.

Numbers

An introduction to the different types of numbers in C++, and how we can do basic math operations on them.
3D art showing a character using an abacus
Ryan McCombe
Updated
Published

Welcome to our exploration of numeric variables! In this lesson, we explore numeric data types and their operations in C++.

Building on our previous discussion about integers and floating-point numbers, we will expand your understanding of how numbers work in C++ and how you can manipulate them.

As a reminder, here is how we create variables. This is similar to what we saw in the previous chapter, except we're now also showing the creation of a float type. A float is a number with a decimal point:

bool isDead { false };
int Level { 5 };
float Armor { 0.2 };
int LargeNumber { 100000000 };

Booleans - true and false values

An overview of the fundamental true or false data type, how we can create them, and how we can combine them.
3D art showing a wizard character
Ryan McCombe
Updated
Published

In this lesson, we introduce the concept of booleans in C++. Booleans are one of the simplest yet most powerful data types in programming. They represent the concept of binary states — true or false. You can think of them as the answer to a yes-or-no question.

Understanding Booleans is crucial because they form the backbone of decision-making in code. They help us answer questions like: "Is this condition met?" or "Should this action be performed?"

They are the key to controlling how a program behaves and responds to different scenarios. We've already seen a glimpse of how we can create booleans:

bool isAlive { true };
bool isDead { false };

In this lesson, we'll explore more on how to create, manipulate, and use these fundamental types to direct the flow of our programs. We'll start with the basics of creating booleans through comparisons and then move on to more complex operations.

Types and Literals

Explore how C++ programs store and manage numbers in computer memory, including integer and floating-point types, memory allocation, and overflow handling.
3D art representing computer programming
Ryan McCombe
Published

Programming languages can be categorized as either "high-level" or "low-level". High-level languages (like Python) focus on making it easy for humans to write code, hiding many technical details about how computers work.

Low-level languages give programmers more direct control over the computer's hardware and memory, which can make programs faster and more efficient.

C++ is considered a relatively low-level language, which means we sometimes need to understand what's happening "under the hood" when we write our code. This is particularly true when it comes to how our programs use computer memory.

Implicit Conversions and Narrowing Casts

Going into more depth on what is happening when a variable is used as a different type
3D art showing a wizard character
Ryan McCombe
Updated
Published

In our journey so far, we have discovered how functions and variables work with specific data types. Each type, like int or float, plays a unique role. But what happens when we mix these types - for example, what happens when we use an int where a float is expected?

This scenario introduces us to the concept of implicit conversion.

It allows us to use different types of values interchangeably in some situations, without needing to manually convert them. For instance, using an int value where a float is needed. The compiler, our code's translator, handles this conversion for us.

However, there's a twist. Not all conversions are equal. Some are straightforward, like turning an int into a float. Others, like converting a string to an int, are not possible implicitly.

In this lesson, we’ll explore how implicit conversions work with variables, functions, and operators. We'll also tackle the concept of narrowing casts, a special type of conversion that needs extra attention.

Header Files

Explore how header files and linkers streamline C++ programming, learning to organize and link our code effectively
3D art showing a character in a bar
Ryan McCombe
Updated
Published

In a previous section, we saw how we could declare and define a function in two separate steps.

The declaration of a function uses its prototype. The prototype includes the function’s return type, its name, and the types of its parameters.

// A function declaration
int Add(int x, int y);

The definition includes all those things, but also includes the body of the function, where its behavior is implemented:

// A function definition
int Add(int x, int y) {
  return x + y;
}
Module One
3D art showing a progammer setting up a development environment

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Capstone Project
3D art representing computer programming

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Module Two
A computer programmer

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Get Started Now

Intro to C++ Programming

Starting from the fundamentals, become a C++ software engineer, step by step.

Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved