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
Module Two

Professional C++

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

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt
Screenshot from Warhammer: Total War
Screenshot from Cyberpunk 2077
Coming Soon

SDL and C++ Development

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

Introduction to SDL_Image

Learn to load, manipulate, and save various image formats using SDL_Image.
3D art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we’ll start using the SDL_Image extension we installed earlier. We’ll cover 3 main topics:

  • Initializing and closing SDL_Image
  • Using the IMG_Load() function to load and render a wide variety of image types, rather than being restricted to the basic bitmap (.bmp) format.
  • Using surface blending modes to use transparency and other techniques when blitting
  • Creating image files from our surfaces using IMG_SaveJPG() and IMG_SavePNG()

We’ll be building upon the basic application loop and surface-blitting concepts we covered earlier in the course:

Rendering Text with SDL_ttf

Learn to render and manipulate text in SDL2 applications using the official SDL_ttf extension
3D art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we’ll see how we can render text within our programs. We’ll use the official SDL_ttf extension we installed earlier in the course.

We’ll build upon the concepts we introduced in the previous chapters. Our main.cpp looks like below.

The key thing to note is that we have created a Text class, and instantiated an object from it called TextExample. This object is being asked to Render() onto the window surface every frame:

Text Performance, Fitting and Wrapping

Explore advanced techniques for optimizing and controlling text rendering when using SDL_ttf
3D art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we’ll build on our text rendering capabilities with some new tools:

  • The ability to adjust the rasterizing process to optimize performance when appropriate
  • Dynamically choosing a font size to fit a target pixel size for our rasterized surface
  • Calculating how much text we can fit within a designated space
  • Rendering multi-line text areas using word wrapping, and controlling the alignment of the lines

Our main.cpp is below. Compared to the last lesson, we’ve added a GetWidth() method to the Window class.

Loading and Displaying Images

Learn how to load, display, and optimize image rendering in your applications
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we’ll see how we can load images into SDL, and then display them in our window.

We’ll build upon the concepts we introduced in the previous chapters. Our main.cpp looks like below.

For this lesson, the key thing to note is that we have an Image object called Example, which is being asked to Render() onto the window surface every frame:

// main.cpp
#include <SDL.h>
#include "Image.h" 

class Window {/*...*/}; int main(int argc, char** argv){ SDL_Init(SDL_INIT_VIDEO); Window GameWindow; Image Example; SDL_Event Event; bool shouldQuit{false}; while (!shouldQuit) { while (SDL_PollEvent(&Event)) { GameUI.HandleEvent(Event); if (Event.type == SDL_QUIT) { shouldQuit = true; } } GameWindow.Render(); Example.Render(GameWindow.GetSurface()); GameWindow.Update(); } SDL_Quit(); return 0; }

We’ll build out this Image class across the next two lessons. Our starting point is simply this:

Cropping and Positioning Images

Learn to precisely control image display using source and destination rectangles.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we'll dive into image positioning and cropping techniques. Here's what we'll cover:

  • Source and Destination Rectangles: Learn how to control which parts of an image are rendered and where they appear on screen.
  • Cropping: Discover how to display only specific portions of an image.
  • Positioning: Learn how to place images precisely where we want them on our destination surface.
  • Clipping: Understand what happens when images don't fit entirely on screen and how to detect it.

As a starting point, we’ll be building upon the basic application loop and surface-blitting concepts we covered in previous lessons:

Image Scaling and Aspect Ratios

Learn techniques for scaling images and working with aspect ratios
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we'll learn how to scale our images up and down during the blitting process. Here's what we'll cover:

  • The SDL_BlitScaled() function, and how it differs from SDL_BlitSurface().
  • What an aspect ratio is, why it matters, and how to calculate it.
  • Using aspect ratios to prevent images being stretched and deformed during scaling.

We’ll be building upon the basic application loop and surface-blitting concepts we covered earlier in the course:

Building a Modular UI System

Learn how to create a flexible and extensible UI system using C++ and SDL, focusing on component hierarchy and polymorphism.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In the previous section, we introduced the application loop and event loop, which sets the foundation of our program. As our program and interactions get more complex, we need to build additional systems on top of this foundation to help manage the complexity.

In this lesson, we'll dive deeper into managing complex user interfaces by implementing a modular UI system. We'll cover key topics such as:

  1. Creating a UI manager to handle events and rendering
  2. Implementing nested components for better organization
  3. Utilizing inheritance to create reusable UI elements
  4. Using polymorphism for flexible component management

By the end of this lesson, you'll have a solid foundation for building scalable and maintainable UI systems.

Building Interactive Buttons

Explore techniques for building UI components that respond to user input
Abstract art representing computer programming
Ryan McCombe
Updated
Published

Welcome to this lesson on building interactive UI components using SDL in C++. In this tutorial, we'll explore how to create dynamic buttons that respond to user input.

We'll cover topics such as event handling, creating custom button classes, and implementing hover and click behaviors.

Detecting and Managing Errors

Discover techniques for detecting and responding to SDL runtime errors
Abstract art representing computer programming
Ryan McCombe
Published

As with any code, interacting with SDL can sometimes result in errors. For example, let’s imagine we’re trying to create a window that uses Metal, which is Apple’s API for creating high-performance graphics. To do this, we pass the SDL_WINDOW_METAL flag to SDL_CreateWindow():

#include <SDL.h>

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* Window = SDL_CreateWindow(
    "Hello World",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    1024, 768,
    SDL_WINDOW_METAL
  );
  
  return 0;
}

If this program is run on a platform that doesn’t support Metal (such as a Windows machine), window creation will fail, and it might not be obvious why that is. In this lesson, we’ll introduce the ways we can detect and cover from errors coming from SDL.

Custom User Events

Discover how to use the SDL2 event system to handle custom, game-specific interactions
Abstract art representing computer programming
Ryan McCombe
Published

Previously, we’ve seen how SDL implements an event queue, which we can interact with using functions like SDL_PollEvent(). As standard, SDL will push events onto this queue to report actions like the user moving their mouse (SDL_MOUSEMOTION) or requesting the application close (SDL_QUIT).

However, we can also use SDL’s event system to manage custom events, that are specific to the game we’re making. For example, if we were making a first-person shooter, we could use this system to report when player fires their weapon or reloads.

In this lesson, we'll learn how to register and use custom events to create these game-specific behaviors.

We'll also look at how to organise our code around a custom event system, and see practical examples of how custom events can be used to manage game state and handle complex user interactions.

Handling Mouse Input

Learn how to detect and handle mouse input events in SDL, including mouse motion, button clicks, and window entry/exit.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we will cover in detail how we can detect and react to the two main forms of mouse input - the user moving their cursor, and the user clicking their mouse buttons.

When these forms of input are detected, an SDL_Event is pushed onto the event queue. We can capture these events through our event loop, and handle them as needed.

This lesson builds on our earlier work, where we have a Window class that initializes SDL and creates a window, and an application loop set up in our main function:

#include <SDL.h>

class Window {/*...*/}; int main(int argc, char** argv) { Window GameWindow; SDL_Event Event; while(true) { while(SDL_PollEvent(&Event)) { // Detect and handle input events // ... } GameWindow.RenderFrame(); } SDL_Quit(); return 0; }

Our SDL_PollEvent(&Event) statement will update our Event object with any mouse action that the user performs. We’ll then detect and react to those actions within the body of the loop.

Handling Keyboard Input

Learn how to detect and respond to keyboard input events in your SDL-based applications. This lesson covers key events, key codes, and modifier keys.
Abstract art representing computer programming
Ryan McCombe
Updated
Published

In this lesson, we will cover in detail how we can detect and react to the user providing input to our application using their keyboard. Similar to other forms of input, when SDL detects keyboard interaction, it pushes events into the event queue. We can capture these events through our event loop, and handle them as needed.

This lesson builds on our earlier work, where we have a Window class that initializes SDL and creates a window, and an application loop set up in our main function:

#include <SDL.h>

class Window {/*...*/}; int main(int argc, char** argv) { Window GameWindow; SDL_Event Event; while(true) { while(SDL_PollEvent(&Event)) { // Detect and handle input events // ... } GameWindow.RenderFrame(); } SDL_Quit(); return 0; }

Our SDL_PollEvent(&Event) statement will update our Event object with any keyboard action that the user performs. We’ll then detect and react to those actions within the body of the loop.

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:

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:

  • 57 Lessons
  • Over 200 Quiz Questions
  • 95% 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:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Coming Soon
sdl2-promo.jpg

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Under Construction
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