What is Homebrew?
Homebrew is a free and open-source software package management system, siimply you will be able to install most of the software and librairies with one command line. If you don't have homebrew installed you can install it with one command line on the official website Homebrew
After brew is installed you can install git with:
brew install git
After downloading the project you will need to generate the project to compile, and for that CMake make this step easy. To install it with homebrew, same thing:
brew install cmake
If you want to have the last version of SDL3 you can pull the master with git:
In a terminal, create a directory where you want to download SDL, with mkdir then :
git clone https://github.com/libsdl-org/SDL.git
mkdir build
cd build
ccmake ..
press c (for configure)
You'll need to set the line CMAKE_BUILD_TYPE to Release.
press c (for configure)
press g (for Generate)
make -j6 (-j indicate the number of core you want to use to make the project, I have 6 core on my machine).
You can install the library on your system with : sudo make install If the make is successful you should have :
For this project we will use cmake to handle de makefile fo that we need to create config file for it a CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(SDL3Project)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
find_package(SDL3 REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} SDL3::SDL3)
After you have saved the CmakeLists.txt inside a new directory for example SDL3Project.
We need to create a sample project in c++ for that use this simple example diplaying a green rectangle.
Inside a main.cpp file copy the following code, the file need to be in the same directory of the CMakeLists.txt file
#include <SDL3/SDL.h>
#include <iostream>
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window* win = SDL_CreateWindow("SDL3 Project",640, 480, 0);
if (win == nullptr) {
std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Renderer* ren = SDL_CreateRenderer(win, NULL);
if (ren == nullptr) {
std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
SDL_Event e;
bool quit = false;
// Define a rectangle
SDL_FRect greenSquare {270, 190, 100, 100};
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
quit = true;
}
}
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); // Set render draw color to black
SDL_RenderClear(ren); // Clear the renderer
SDL_SetRenderDrawColor(ren, 0, 255, 0, 255); // Set render draw color to green
SDL_RenderFillRect(ren, &greenSquare); // Render the rectangle
SDL_RenderPresent(ren); // Render the screen
}
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
You can build and test you project! You can do the same thing as before:
mkdir build
cd build
ccmake ..
press c (for configure)
You'll need to set the line CMAKE_BUILD_TYPE to Release and set SDL3_DIR
to the path of the folder where you build the SDL3 library.
press c (for configure)
press g (for Generate)
make
You need to have somehting like this in the terminal during the cmake configure :