Skip to content

Commit 191b9d5

Browse files
committed
Added INTRO-cmake.md
1 parent 7d2a1c5 commit 191b9d5

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

docs/INTRO-cmake.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
# Introduction to SDL with CMake
3+
4+
The easiest way to use SDL is to include it as a subproject in your project.
5+
6+
We'll start by creating a simple project to build and run [hello.c](hello.c)
7+
8+
Create the file CMakeLists.txt
9+
```cmake
10+
cmake_minimum_required(VERSION 3.16)
11+
project(hello)
12+
13+
# set the output directory for built objects.
14+
# This makes sure that the dynamic library goes into the build directory automatically.
15+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
16+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
17+
18+
# This assumes the SDL source is available in vendored/SDL
19+
add_subdirectory(vendored/SDL EXCLUDE_FROM_ALL)
20+
21+
# Create your game executable target as usual
22+
add_executable(hello WIN32 hello.c)
23+
24+
# Link to the actual SDL3 library.
25+
target_link_libraries(hello PRIVATE SDL3::SDL3)
26+
```
27+
28+
Build:
29+
```sh
30+
cmake .
31+
cmake --build .
32+
```
33+
34+
Run:
35+
- On Windows the executable is in the Debug directory:
36+
```sh
37+
./Debug/hello
38+
```
39+
- On other platforms the executable is in the current directory:
40+
```sh
41+
./hello
42+
```
43+
44+
A more complete example is available at:
45+
46+
https://github.com/Ravbug/sdl3-sample
47+
48+
Additional information and troubleshooting is available in [README-cmake.md](README-cmake.md)

docs/hello.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely.
11+
*/
12+
#include <SDL3/SDL.h>
13+
#include <SDL3/SDL_main.h>
14+
15+
int main(int argc, char *argv[])
16+
{
17+
SDL_Window *window = NULL;
18+
SDL_Renderer *renderer = NULL;
19+
const char *message = "Hello World!";
20+
int w = 0, h = 0;
21+
float x, y;
22+
bool done = false;
23+
24+
/* Create the window */
25+
if (!SDL_CreateWindowAndRenderer("Hello World", 0, 0, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
26+
SDL_Log("Couldn't create window and renderer: %s\n", SDL_GetError());
27+
return 1;
28+
}
29+
30+
while (!done) {
31+
SDL_Event event;
32+
33+
/* Handle events */
34+
while (SDL_PollEvent(&event)) {
35+
if (event.type == SDL_EVENT_KEY_DOWN ||
36+
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
37+
event.type == SDL_EVENT_QUIT) {
38+
done = true;
39+
}
40+
}
41+
42+
/* Center the message */
43+
SDL_GetWindowSize(window, &w, &h);
44+
x = (float)(w - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(message)) / 2;
45+
y = (float)(h - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
46+
47+
/* Draw the message */
48+
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
49+
SDL_RenderClear(renderer);
50+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
51+
SDL_RenderDebugText(renderer, x, y, message);
52+
SDL_RenderPresent(renderer);
53+
}
54+
SDL_Quit();
55+
56+
return 0;
57+
}
58+

0 commit comments

Comments
 (0)