aboutsummaryrefslogtreecommitdiff
path: root/docs/markdown/GuiTutorial.md
blob: 1c19569173805b160dfc7d596664f8c9fe0ad74f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
short-description: A simple GUI tutorial
...

# Building a simple SDL2 app from scratch

This page shows from the ground up how to define and build an SDL2 gui
application using nothing but Meson. The document is written for
Windows, as it is the most complex platform, but the same basic ideas
should work on Linux and macOS as well.

The sample application is written in plain C as SDL 2 is also written
in C. If you prefer C++ instead, the conversion is fairly simple and
is left as an exercise to the reader.

This document assumes that you have already installed both Visual
Studio and Meson.

# Set up the build directory

First you need to create an empty directory for all your stuff. The
Visual Studio toolchain is a bit unusual in that it requires you to
run your builds from a specific shell. This can be found by opening
the application menu and then choosing `Visual Studio <year> -> x86_64
native tools command prompt`.

It will put you in a weird directory, so you need to go to your home
directory:

    cd \users\yourusername

Typically you'd type `cd \users\` and then press the tabulator key to
make the shell autocomplete the username. Once that is done you can
create the directory.

    mkdir sdldemo
    cd sdldemo

# Creating the sample program

Now we need to create a source file and a Meson build definition file.
We're not going to use SDL at all, but instead start with a simple
program that only prints some text. Once we have it working we can
extend it to do graphics. The source goes into a file `sdlprog.c` and
has the following contents:

```c
#include <stdio.h>

int main(int argc, char **argv) {
  printf("App is running.\n");
  return 0;
}
```

The build definition goes to a file called `meson.build` and looks like this:

```meson
project('sdldemo', 'c')

executable('sdlprog', 'sdlprog.c')
```

With this done we can start the build with the following command:

    meson build

Here `build` is the _build directory_, everything that is generated
during the build is put in that directory. When run, it should look like this.

![Configuring the sample application](images/sdltutorial_01.png)

The program is compiled with this:

    meson compile -C build

The `-C` argument tells Meson where the configured build directory is.

The program will be in the build directory and can be run like this:

    build\sdlprog

The output should look like this.

![Running the sample application](images/sdltutorial_02.png)

# Upgrading the program to use SDL

The code needed to start SDL is a bit more complicated and we're not
going to go into how it works. Merely replace the contents of
`sdlprog.c` with the following:

```c
#include "SDL.h"

int main(int argc, char *argv[])
{
    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Surface *surface;
    SDL_Event event;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
        return 3;
    }

    if (SDL_CreateWindowAndRenderer(320, 240, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
        return 3;
    }

    while (1) {
        SDL_PollEvent(&event);
        if (event.type == SDL_QUIT) {
            break;
        }
        SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    SDL_Quit();

    return 0;
}
```

Let's try to compile this by running `meson compile -C build` again.

![Building SDL app fails](images/sdltutorial_03.png)

That fails. The reason for this is that we don't actually have SDL
currently available. We need to obtain it somehow. In more technical
terms SDL2 is an _external dependency_ and obtaining it is called
_dependency resolution_.

Meson has a web service for downloading and building (if needed)
dependencies called a WrapDB. It provides SDL2 so we can use it
directly. First we need to create a `subprojects` directory because in
Meson all subprojects like these must be stored in that directory for
consistency.

    mkdir subprojects

Then we can install the dependency:

    meson wrap install sdl2

It looks like this:

![Obtaining SDL2 from WrapDB](images/sdltutorial_04.png)

As a final step we need to update our build definition file to use the
newly obtained dependency.

```meson
project('sdldemo', 'c',
        default_options: 'default_library=static')

sdl2_dep = dependency('sdl2')

executable('sdlprog', 'sdlprog.c',
           win_subsystem: 'windows',
           dependencies: sdl2_dep)
```

In addition to the dependency this has a few other changes. First we
specify that we want to build helper libraries statically. For simple
projects like these it makes things simpler. We also need to tell
Meson that the program we are building is a Windows GUI
application rather than a console application.

This is all we need to do and can now run `meson compile` again. First
the system downloads and configures SDL2:

![Running the sample application](images/sdltutorial_05.png)

A bit later the compilation exits succesfully.

![Running the sample application](images/sdltutorial_06.png)

# Program is done

Now we can run the application with

    build\sdlprog

The end result is a black SDL window.

![Running the sample application](images/sdltutorial_07.png)