Sunday, July 14, 2024

Learning SDL Part II

 In Learning SDL Part I I talked about the 2DLight example from https://glusoft.com/sdl2-tutorials. It might be a bit too advanced for beginner. The examples have external dependencies, and might not easy to be understood without basic concept of SDL. Code might have not been maintained after years. And may due to build tools update or package update, code cannot be built out-of-box. So I decide to start from simple one: https://lazyfoo.net/tutorials/SDL

The first example is "01_hello_SDL". Since it is a simple one, I'm going to try to build it with NMake. A simple Makefile for x64 would be like this:

# Compiler
CC=cl
# Include directory for SDL headers
SDL_INCLUDE=C:\path\to\SDL\include\SDL2
# Library directory for SDL library files
SDL_LIB=C:\path\to\SDL\lib\x64
# Compiler flags, appending /Zi if want to debug
CFLAGS=/I$(SDL_INCLUDE) /Dmain=SDL_main
# Linker flags
LFLAGS=/link /LIBPATH:$(SDL_LIB) SDL2.lib SDL2main.lib SDL2_image.lib Shell32.lib /SUBSYSTEM:CONSOLE

# Target executable name
TARGET=01_hello_SDL.exe
# Source files
SOURCES=01_hello_SDL.cpp

# Rule to make the target
$(TARGET): $(SOURCES)
    $(CC) $(CFLAGS) /Fe:$(TARGET) $(SOURCES) $(LFLAGS)

# Clean target
clean:
    del $(TARGET) *.obj

Note: 1) have to specify /SUBSYSTEM:CONSOLE, otherwise may get entry point not defined error as Win32 app will look for WinMain as entry point.

2)  Have to link to SDL2main.lib and Shell32.lib. SDL2main.lib provides SDL entry function. The Shell32.lib is needed, otherwise, will get error 'LNK2019: unresolved external symbol __imp_CommandLineToArgvW referenced in function main_getcmdline'

3) For some projects need more image format support such as png, would need link to more lib, such as 06_extension_libraries_and_loading_other_image_formats, would need libpng16-16.dll and zlib1.dll which is a dependency of the png dll. If this zlib1 is missing, will only see error complaining "Failed loading libpng16-16.dll" which might be misleading.

4) For projects which needs font, would need link to SDL2_ttf.lib, and needs libfreetype-6.dll at runtime

5) For projects which needs audio, would need link to SDL2_mixer.lib

Run this to setup x64 env: "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat", then run 'NMake' to build. Can copy the x64 SDL dll to C:\Windows\System32 folder, then won't need to copy them to working folder every time.

6) For projects using OpenGL, OpenGL very likely is already installed on the system. May check whether opengl.dll and glu.dll under \windows\system32. To link the lib, add these two libs:

opengl32.lib glu32.lib

7) For OpenGL Extension Wrangler library, can be downloaded from https://glew.sourceforge.net/

0 Comments:

Post a Comment