Tag: SDL

Other ways to time code

Other ways to time code

Stop Watch photo from Pixabay
Image by Free-Photos from Pixabay

As well as the hr_time code that I used in discussion of sdldemo SDL itself provides some other ways to time. If you are happy with millisecond level accuracy of timing, just call this function which returns an unsigned 32-bit (i.e. 0- 4 billion) giving the number of milliseconds since the SDL library was initialised.

SDL_GetTicks()

The link goes to the Wiki page on libsdl.orgĀ  for SDL_GetTicks().

As always, call it twice, once to get the start value and once to get the stop then subtract start from stop to get the elapsed time in milliseconds..

For some operations this can be easier to use. A frame is 16.666666 milliseconds so if your code runs in less than 16 milliseconds, it’s not going to cause problems. In my original Windows code, it ran is a fraction of a millisecond. Compiled C code really is fast and helped because the GPU is doing most of the donkey work!

There’s also a higher accuracy timer. This I guess is just a wrapper round the system call I call in hr_time.

Uint64 SDL_GetPerformanceCounter(void)
Which I haven’t used yet but which I guess returns a processor clock count. To measure time you need to call it twice (start and stop values as before) then divide by the frequency value returned from a call to

Uint64 SDL_GetPerformanceFrequency(void)

My computer returns a clock frequency of 3,500,000,000 clocks per second but the actual value is irrelevant just divide ((stop – start)/ frequency) to get the elapsed time in seconds.

SDL on Linux

SDL on Linux

In my forthcoming ebook, which is the Linux equivalent of the first one I use Visual Studio Code (VSC) as the IDE to develop along with the Microsoft C/C++ extension. I’m using the SDL2 library for fast graphics and Clang as the compiler.

Thankfully Microsoft have documented most of the process of using Clang with VSC, albeit on a Mac. I’m using Ubuntu but it’s mostly the same.

Before I could configure SDL I had to add it and I never realised about apt-cache on Ubuntu (and Debian). The command

apt- cache search libsdl2

Outputs this, showing what's available.
libsdl2-2.0-0 - Simple DirectMedia Layer
libsdl2-dev - Simple DirectMedia Layer development files
libsdl2-doc - Reference manual for libsdl2
libsdl2-gfx-1.0-0 - drawing and graphical effects extension for SDL2
libsdl2-gfx-dev - development files for SDL2_gfx
libsdl2-gfx-doc - documentation files for SDL2_gfx
libsdl2-image-2.0-0 - Image loading library for Simple DirectMedia Layer 2, libraries
libsdl2-image-dev - Image loading library for Simple DirectMedia Layer 2, development files
libsdl2-mixer-2.0-0 - Mixer library for Simple DirectMedia Layer 2, libraries
libsdl2-mixer-dev - Mixer library for Simple DirectMedia Layer 2, development files
libsdl2-net-2.0-0 - Network library for Simple DirectMedia Layer 2, libraries
libsdl2-net-dev - Network library for Simple DirectMedia Layer 2, development files
libsdl2-ttf-2.0-0 - TrueType Font library for Simple DirectMedia Layer 2, libraries
libsdl2-ttf-dev - TrueType Font library for Simple DirectMedia Layer 2, development files

So a quick

sudo apt-get install libsdl2-dev

Installed 73 MB of files including all the header files. I used the files app to search for SDL files and it found a folder SDL2 in /usr/include.

And all I needed in my program was

[perl]

#include <SDL2/SDL.h>

[/perl]

And I had to add “/usr/include/SDL2/” into the includePath section of c_cpp_properties.json and “-lSDL2” into the args section of tasks.json. These two JSON files are included in C/C++ projects in VSC.

At that point I could compile and run my first SDL program on Ubuntu. It throws 10,000 random sized coloured rectangles onto the screen

The first SDL demo program