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
Download the last version on the github repository of SD2
If you want to have the last version of SDL2 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
cd SDL
git checkout SDL2
For this project we will use cmake to handle de makefile fo that we need to create config file for it a CmakeList.txt:
cmake_minimum_required(VERSION 3.10)
project(StaticProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SDL2_USE_STATIC_LIBS ON)
find_package(SDL2 REQUIRED)
message(STATUS "SDL2 include directories: ${SDL2_INCLUDE_DIRS}")
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.cpp)
message(STATUS "SDL2 libraries path: ${SDL2_DIR}")
target_link_libraries(${PROJECT_NAME}
${SDL2_DIR}/libSDL2.a
"-framework Cocoa"
"-framework IOKit"
"-framework CoreFoundation"
"-framework CoreVideo"
"-framework CoreAudio"
"-framework AudioToolbox"
"-framework CoreHaptics"
"-framework GameController"
"-framework CoreServices"
"-framework Metal"
"-framework Foundation"
"-framework QuartzCore"
"-framework ForceFeedback"
"-framework Carbon"
)
To explain more thiis config we create a project named StaticProject, we use the prrevious build of SDL2 by requiring the user to set the path of the package. Then this path allows to find the SDL2_INCLUDE_DIRS wiith all the headers. We display a message to be sure if the path is correct. In this project we simply have one fiile main.cpp a simple SDL App displaying a green sqaure. The more complex step step when static linking is when linking the dependencies of libSDL2.a for that we have different base frameworks in MacOs.
This list fo framework can also change I am on Mac Somona.
After creating this will we need an example of main.cpp to execute :
#include <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("Hello SDL", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr) {
std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr) {
std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
bool running = true;
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_Rect greenSquare = { 270, 190, 100, 100 };
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderFillRect(renderer, &greenSquare);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}