Common Lisp SDL2 tutorial
SDL2 is a great library, but the tutorials on it are not very many.
Common Lisp is a great language, but articles on it too little.
I think that is enough to write this series.
Why I chose alsalisp? Well, on taste and color as they say.
However, the reasons are:
In fact, this series adaptation of tutorials from Lazy Foo:
lazyfoo.net/tutorials/SDL/index.php
In this article I'm not going to explain in details how to put all the necessary software, but
if anyone is really interested, please write
in the comments.
To install SDL2, use the tips Fu lazyfoo.net/tutorials/SDL/01_hello_SDL/index.php.
As for Lisp, I use the implementation www.sbcl.org and www.quicklisp.org.
At this point the efficiency was tested only in her.
Then I'm using the library cl-sdl2: github.com/lispgames/cl-sdl2.
It just so happened that support a certain functionality from SDL2, for example
surfaces, it is not fully implemented. But it does not matter, I append
functionality as you progress through the tutorials. Therefore, do not
put version of quicklisp'Oh, and just clone the master branch into the
It is assumed that you know the minimal syntax of Lisp and don't get scared from the abundance of parentheses.
As the primary means of interacting with a Lisp environments I will use slime.
You have not yet tried spacemacs? Then we go to you!
Those who do not need lyrics, and the correct code: github.com/TatriX/cl-sdl2-tutorial
To begin with, that will simply force SDL2 to show us a window filled with white.
Obviously, we need Sam library cl-sdl2. Connect it:
the
Will not complicate your task and create a window of fixed size.
To do this, declare a global (and moreover special) variables for the width and height of our window, because magic numbers are evil.
the
Now let's create a function that will open the window pour
the all white color have to wait some time and close.
Simple we will consider our main function and add her optional
a named parameter, so we can adjust the time through which
the window will close. By default two seconds of our eyes.
the
Then we need to initialize sdl2, saying library what
subsystems we want to use. For this, cl-sdl2, we provide a convenient macro:
the
We want to use only a subsystem of the graphics output, so
give the symbol :video. You ask: "how was I supposed to guess
what you need to transfer exactly that :a video?" Answer: cl-sdl2 converts
SDL_ constants to the appropriate characters. For example,
we can open the documentation for SDL_Init method, and see
available flags are: wiki.libsdl.org/SDL_Init#Remarks
To get the character cut off from the constant's name and SDL_INIT_
convert the flag into lower case.
The beauty with macro is that they are mandatory
release all allocated resources, freeing us from having to
to follow it yourself.
Great, then let's create a window:
the
This is another with a macro. This time the first element of the list
macro argument is a symbol window, through which in the body
parameters :title, :w, :h I think is quite obvious and do not require
explanation.
With the window clear, but now we want to fill in the resulting window, white
color. One possible implementation would be to use
"surfaces", they are the same surfaces. In fact, the surface is a structure
containing pixels of a certain area used in software
rendering. For example, we can obtain the surface of our window:
the
and fill it with white color:
the
The first argument is the rectangle that we want to fill. If it is not
to transfer, we will fill the entire area. Why poor #fff
to write such a complicated way? It's all in the variety of formats
pixels, screens and the like. And since SDL2 library
cross-platform, to apply all necessary transformations
used various functions, such as map-rgb in this case.
Pour a window filled in, but not enough. Now we need to politely ask the library to update our window:
the
Given that the entire so long described process will take place in fractions of a second,
and we still want to enjoy the result, ask sdl to wait a bit:
(sdl2:delay delay)
And most importantly:
the
That's all. Left to start our creation, and hope that we will not fly in the debugger:
the
In any case,
Honestly, I was planning in one article to tell you about the first three of the tutorial, but it is already a lot of text entered.
For the impatient, again the link to the code tutorials (at the moment of writing of article 16 pieces):
github.com/TatriX/cl-sdl2-tutorial
I hope it was interesting and informative.
Article based on information from habrahabr.ru
Common Lisp is a great language, but articles on it too little.
I think that is enough to write this series.
Why I chose alsalisp? Well, on taste and color as they say.
However, the reasons are:
In fact, this series adaptation of tutorials from Lazy Foo:
lazyfoo.net/tutorials/SDL/index.php
In this article I'm not going to explain in details how to put all the necessary software, but
if anyone is really interested, please write
in the comments.
To install SDL2, use the tips Fu lazyfoo.net/tutorials/SDL/01_hello_SDL/index.php.
As for Lisp, I use the implementation www.sbcl.org and www.quicklisp.org.
At this point the efficiency was tested only in her.
Then I'm using the library cl-sdl2: github.com/lispgames/cl-sdl2.
It just so happened that support a certain functionality from SDL2, for example
surfaces, it is not fully implemented. But it does not matter, I append
functionality as you progress through the tutorials. Therefore, do not
put version of quicklisp'Oh, and just clone the master branch into the
~/quicklisp/local-projects
.It is assumed that you know the minimal syntax of Lisp and don't get scared from the abundance of parentheses.
As the primary means of interacting with a Lisp environments I will use slime.
You have not yet tried spacemacs? Then we go to you!
Those who do not need lyrics, and the correct code: github.com/TatriX/cl-sdl2-tutorial
White screen
To begin with, that will simply force SDL2 to show us a window filled with white.
Obviously, we need Sam library cl-sdl2. Connect it:
the
CL-USER > (ql:quickload :sdl2)
Will not complicate your task and create a window of fixed size.
To do this, declare a global (and moreover special) variables for the width and height of our window, because magic numbers are evil.
the
(defparameter *screen-width* 640)
(defparameter *screen-height* 480)
Now let's create a function that will open the window pour
the all white color have to wait some time and close.
Simple we will consider our main function and add her optional
a named parameter, so we can adjust the time through which
the window will close. By default two seconds of our eyes.
the
(defun main (&key (delay 2000))
Then we need to initialize sdl2, saying library what
subsystems we want to use. For this, cl-sdl2, we provide a convenient macro:
the
(sdl2:with-init (:video)
We want to use only a subsystem of the graphics output, so
give the symbol :video. You ask: "how was I supposed to guess
what you need to transfer exactly that :a video?" Answer: cl-sdl2 converts
SDL_ constants to the appropriate characters. For example,
we can open the documentation for SDL_Init method, and see
available flags are: wiki.libsdl.org/SDL_Init#Remarks
To get the character cut off from the constant's name and SDL_INIT_
convert the flag into lower case.
The beauty with macro is that they are mandatory
release all allocated resources, freeing us from having to
to follow it yourself.
Great, then let's create a window:
the
(sdl2:with-window (window :title "SDL2 Window" :w *screen-width* :h *screen-height*)
This is another with a macro. This time the first element of the list
macro argument is a symbol window, through which in the body
parameters :title, :w, :h I think is quite obvious and do not require
explanation.
With the window clear, but now we want to fill in the resulting window, white
color. One possible implementation would be to use
"surfaces", they are the same surfaces. In fact, the surface is a structure
containing pixels of a certain area used in software
rendering. For example, we can obtain the surface of our window:
the
(let ((screen-surface (sdl2:get-window-surface window)))
and fill it with white color:
the
(sdl2:fill-rect screen-surface
nil
(sdl2:map-rgb (sdl2:surface-format screen-surface) 255 255 255))
The first argument is the rectangle that we want to fill. If it is not
to transfer, we will fill the entire area. Why poor #fff
to write such a complicated way? It's all in the variety of formats
pixels, screens and the like. And since SDL2 library
cross-platform, to apply all necessary transformations
used various functions, such as map-rgb in this case.
Pour a window filled in, but not enough. Now we need to politely ask the library to update our window:
the
(sdl2:update-window window)
Given that the entire so long described process will take place in fractions of a second,
and we still want to enjoy the result, ask sdl to wait a bit:
(sdl2:delay delay)
And most importantly:
the
)))
That's all. Left to start our creation, and hope that we will not fly in the debugger:
the
CL-USER > (main)
In any case,
source complete
(defparameter *screen-width* 640)
(defparameter *screen-height* 480)
(defun main (&key (delay 2000))
(sdl2:with-init (:video)
(sdl2:with-window (window :title "SDL2 Window" :w *screen-width* :h *screen-height*)
(let ((screen-surface (sdl2:get-window-surface window)))
(sdl2:fill-rect screen-surface
nil
(sdl2:map-rgb (sdl2:surface-format screen-surface) 255 255 255))
(sdl2:update-window window)
(sdl2:delay delay)))))
Honestly, I was planning in one article to tell you about the first three of the tutorial, but it is already a lot of text entered.
For the impatient, again the link to the code tutorials (at the moment of writing of article 16 pieces):
github.com/TatriX/cl-sdl2-tutorial
I hope it was interesting and informative.