SDL is Simple DirectMedia Layer.It is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.It can be used to make animations and video games.
- It basically provides a set of APIs to interact with various devices like graphics hardware, audio, keyboard, mouse, etc.
- It is written in C programming language and works with C++ and various other languages like c# and python.
Installation on Linux ( For OS which uses the apt package manager eg : Ubuntu ):
- Run command sudo apt-get update on your terminal.
- Run command sudo apt-get install clang on your terminal.
- Run command sudo apt-get install libsdl2-2.0-0 libsdl2-dbg libsdl2-dev libsdl2-image-2.0-0 libsdl2-image-dbg libsdl2-image-dev on your terminal.
we need to make a Makefile.So open a text editor of your choice and start writing the code below.
# A simple Makefile for compiling small SDL projects # set the compiler CC := clang # set the compiler flags CFLAGS := `sdl2-config --libs --cflags` -ggdb3 -O0 --std=c99 -Wall -lSDL2_image -lm # add header files here HDRS := # add source files here SRCS := #file-name.c # generate names of object files OBJS := $(SRCS:.c=.o) # name of executable EXEC := #name your executable file # default recipe all: $(EXEC) showfont: showfont.c Makefile $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) glfont: glfont.c Makefile $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) # recipe for building the final executable $(EXEC): $(OBJS) $(HDRS) Makefile $(CC) -o $@ $(OBJS) $(CFLAGS) # recipe for building object files #$(OBJS): $(@:.o=.c) $(HDRS) Makefile # $(CC) -o $@ $(@:.o=.c) -c $(CFLAGS) # recipe to clean the workspace clean: rm -f $(EXEC) $(OBJS) .PHONY: all clean
That will create a empty window on your screen.
We will write a simple program to explain rendering and I/O handling:
That will render a image on the window which can be controlled via your keyboard up, down, left, right.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- ctype.h(<cctype>) library in C/C++ with Examples
- How to print range of basic data types without any library function and constant in C?
- Unordered Sets in C++ Standard Template Library
- Advanced C++ with boost library
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- How to add "graphics.h" C/C++ library to gcc compiler in Linux
- The C++ Standard Template Library (STL)
- List in C++ Standard Template Library (STL)
- Deque in C++ Standard Template Library (STL)
- Map in C++ Standard Template Library (STL)
- Wide char and library functions in C++
- C Library math.h functions
- isgraph() C library function
- difftime() C library function
- wcstof function in C library
- wprintf() and wscanf in C Library
- snprintf() in C library
- boost::split in C++ library
- <iterator> library in C++ STL
- Factorial of Large Number Using boost multiprecision Library
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : roshanr2001