Skip to content

Commit e9a6f5c

Browse files
authored
Merge pull request #1 from commonlispbr/make-portable
Make StarWar game more portable at sbcl-wise / Linux system
2 parents b81ad36 + 3064baf commit e9a6f5c

13 files changed

+150
-23
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
system-index.txt
2+
release/
3+
*.tar*
4+
*.fasl

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
build:
2+
rm -rf release/
3+
mkdir -p release
4+
sbcl --load build.lisp
5+
mv starwar-linux release/
6+
cp starwar.conf release/
7+
cp background.mp3 release/
8+
mv lib/ release/
9+
cp StarWar.desktop release/
10+
sed -i 's/starwar.lisp/starwar-linux/g' release/StarWar.desktop

StarWar.desktop

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Desktop Entry]
2+
Version=1.0
3+
Type=Application
4+
Name=StarWar
5+
Comment=A survival and dominance starwar game
6+
Exec=./starwar.lisp
7+
Icon=gcstar
8+
Terminal=false
9+
StartupNotify=false

build.lisp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(ql:quickload :starwar)
2+
(in-package :starwar)
3+
(setq *compression* 1)
4+
(make-binary)

src/globals.lisp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ generateing the map. ")
8686
(defparameter planet-core-radius 3)
8787
(defparameter star-max-amount 1000)
8888

89-
(defparameter screen-rightmost (- world-rightmost screen-width))
90-
(defparameter screen-bottommost (- world-bottommost screen-height))
91-
(defparameter world-width (- world-rightmost world-leftmost))
92-
(defparameter world-height (- world-bottommost world-topmost))
93-
(defparameter margin-bottom (- screen-height 10))
94-
(defparameter margin-right (- screen-width 10))
89+
(defun load-world-limits ()
90+
(defparameter screen-rightmost (- world-rightmost screen-width))
91+
(defparameter screen-bottommost (- world-bottommost screen-height))
92+
(defparameter world-width (- world-rightmost world-leftmost))
93+
(defparameter world-height (- world-bottommost world-topmost))
94+
(defparameter margin-bottom (- screen-height 10))
95+
(defparameter margin-right (- screen-width 10)))
96+
97+
(load-world-limits)
9598

9699
(defun generate-bg-stars (n)
97100
"generate N bg stars and return list"
@@ -104,7 +107,7 @@ generateing the map. ")
104107
"read one file and load all the parameters"
105108
;; here, we MUST make sure that we are in the RIGHT package (that is the
106109
;; starwar package. or the parameters will not be set!
107-
(with-open-file (s filename)
110+
(with-open-file (s (portable-pathname filename))
108111
(do ((form (read s) (read s nil 'eof)))
109112
((eq form 'eof) nil)
110113
(if (not (= (length form) 2))
@@ -196,10 +199,4 @@ INIT-AMOUNT is how many planets one player own at the beginning of game"
196199

197200
;; load all the configure file
198201
(load-file-set-parameters "starwar.conf")
199-
200-
(setq screen-rightmost (- world-rightmost screen-width))
201-
(setq screen-bottommost (- world-bottommost screen-height))
202-
(setq world-width (- world-rightmost world-leftmost))
203-
(setq world-height (- world-bottommost world-topmost))
204-
(setq margin-bottom (- screen-height 10))
205-
(setq margin-right (- screen-width 10)))
202+
(load-world-limits))

src/make-binary.lisp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,61 @@
33

44
(in-package :starwar)
55

6+
(defparameter *compression* 9)
7+
(defparameter *libprefix* "lib/")
8+
(defvar *libraries* nil)
9+
10+
(defun libpath (prefix)
11+
(merge-pathnames prefix (uiop/os:getcwd)))
12+
13+
(defun get-libraries-names (&key (loaded-only t))
14+
(remove-duplicates
15+
(mapcar #'cffi:foreign-library-pathname
16+
(cffi:list-foreign-libraries :loaded-only loaded-only))))
17+
18+
(defun get-libraries (&optional (from "/lib/"))
19+
(loop for l in (get-libraries-names)
20+
for lfrom = (merge-pathnames l from)
21+
for lpath = (probe-file lfrom)
22+
when lpath
23+
collect lfrom))
24+
25+
(defun dump-libraries (&optional (to *libprefix*))
26+
(format t "~%LIBPATH: ~a~%" (libpath to))
27+
(ensure-directories-exist (libpath to))
28+
(let ((libs (get-libraries)))
29+
(setq *libraries* (get-libraries-names))
30+
(loop for lib in (get-libraries)
31+
do (sb-ext:run-program "/bin/cp"
32+
(list "-v"
33+
(namestring lib)
34+
(namestring (libpath to)))
35+
:output *standard-output*))))
36+
37+
(defun load-library (library-name)
38+
(cffi:load-foreign-library library-name))
39+
40+
(defun import-libraries (&optional (libpath *libprefix*))
41+
(pushnew libpath
42+
cffi:*foreign-library-directories*
43+
:test #'equal)
44+
(loop for l in *libraries*
45+
do (load-library l)))
46+
47+
(defun close-libraries ()
48+
(loop for library in (cffi:list-foreign-libraries :loaded-only t)
49+
do (cffi:close-foreign-library library)))
50+
51+
(defun main-wrapper ()
52+
(import-libraries)
53+
(starwar:main))
54+
55+
656
(defun make-binary ()
57+
(dump-libraries) ;; put all libraries into /lib
58+
(close-libraries) ;; close currently open libraries
759
(sb-ext:save-lisp-and-die #+unix "starwar-linux"
860
#+win32 "starwar-win32.exe"
9-
:toplevel #'starwar:main
10-
:executable t))
61+
:toplevel #'main-wrapper
62+
:executable t
63+
:compression *compression*))

src/packages.lisp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
(defpackage :starwar
33
(:use :cl :starwar-lib)
44
(:documentation "this is the star war game!")
5-
(:export :main :run :make-binary))
5+
(:export :main :run :make-binary :fullscreen))

src/path.lisp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(in-package :starwar)
2+
3+
(defun portable-pathname (pathname &optional (system 'starwar))
4+
"PORTABLE-PATHNAME consider two possible dirnames: local and system-wide."
5+
(if (probe-file pathname)
6+
pathname
7+
(asdf:system-relative-pathname system pathname)))

src/starwar.lisp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,20 @@
7777
(setq speed star-speed-max))
7878
(setq star-speed speed))
7979
(setq *news* (format nil "Increase speed to ~ax" star-speed)))
80+
8081
(defun decrease-star-speed ()
8182
(let ((speed (- star-speed 0.5)))
8283
(if (< speed star-speed-min)
8384
(setq speed star-speed-min))
8485
(setq star-speed speed))
8586
(setq *news* (format nil "Decrease speed to ~ax" star-speed)))
8687

88+
(defun toggle-fullscreen ()
89+
(if fullscreen
90+
(sdl:resize-window screen-width screen-height :sw t :resizable t)
91+
(sdl:resize-window screen-width screen-height :sw t :fullscreen t))
92+
(setf fullscreen (not fullscreen)))
93+
8794
(defun handle-key (key)
8895
(case key
8996
(:sdl-key-escape
@@ -94,6 +101,8 @@
94101
(set-game-running *paused*))
95102
(:sdl-key-r
96103
(clear-global-vars))
104+
(:sdl-key-f11
105+
(toggle-fullscreen))
97106
(:sdl-key-minus
98107
(decrease-star-speed))
99108
(:sdl-key-equals
@@ -103,6 +112,8 @@
103112
(clear-global-vars)
104113
(set-game-running t)))))
105114

115+
116+
106117
;; draw the information line by line
107118
(defun draw-information (&rest infos)
108119
(let ((x 10)
@@ -119,6 +130,7 @@
119130
(:up (decf *screen-pos-y* scroll-speed))
120131
(:down (incf *screen-pos-y* scroll-speed))
121132
(otherwise (error "unknown direction!"))))
133+
122134
(defun fix-screen-pos-overflow ()
123135
(when (> *screen-pos-x* screen-rightmost)
124136
(setq *screen-pos-x* screen-rightmost))
@@ -128,6 +140,7 @@
128140
(setq *screen-pos-y* screen-bottommost))
129141
(when (< *screen-pos-y* screen-topmost)
130142
(setq *screen-pos-y* screen-topmost)))
143+
131144
(defun move-screen-on-worldmap ()
132145
(let ((x (sdl:mouse-x)) (y (sdl:mouse-y)))
133146
(when (or (<= x margin-left)
@@ -264,12 +277,13 @@ the outter rect. the rect is filled by VALUE/FULL-VALUE"
264277
(format t "fullscreen: ~a~%" fullscreen)
265278
(sdl:window screen-width screen-height
266279
:fullscreen fullscreen
280+
:resizable t
267281
:title-caption "Star War"
268282
:icon-caption "Star War")
269283
(set-game-running t)
270284
;; music background
271285
(sdl-mixer:open-audio)
272-
(let ((music (sdl-mixer:load-music "background.mp3")))
286+
(let ((music (sdl-mixer:load-music (portable-pathname "background.mp3"))))
273287
(sdl-mixer:play-music music :loop t)
274288
(sdl:with-events ()
275289
(:quit-event () (sdl-mixer:Halt-Music)
@@ -284,6 +298,12 @@ the outter rect. the rect is filled by VALUE/FULL-VALUE"
284298
(handle-mouse-button button x y t))
285299
(:mouse-button-up-event (:button button :x x :y y)
286300
(handle-mouse-button button x y nil))
301+
302+
(:video-resize-event (:w w :h h)
303+
(setq screen-width w)
304+
(setq screen-height h)
305+
(sdl:resize-window w h)
306+
(load-world-limits))
287307
(:idle ()
288308
(unless *game-over*
289309
(sdl:clear-display bg-color)

starwar-lib.asd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(defsystem starwar-lib
77
:name "starwar-lib"
88
:author "Peter Xu"
9-
:version "0.0.1"
9+
:version "0.1.0"
1010
:licence "MIT"
1111
:description "Some basic functions related to game"
1212
:pathname "src/lib/"

starwar.asd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
(defsystem starwar
77
:name "starwar"
88
:author "xzpeter"
9-
:version "0.0.1"
9+
:version "0.2.0"
1010
:license "MIT"
1111
:description "A very simple starwar game."
1212
:depends-on (:lispbuilder-sdl
1313
:lispbuilder-sdl-ttf
1414
:lispbuilder-sdl-gfx
1515
:lispbuilder-sdl-mixer
16+
:cffi
1617
:starwar-lib)
1718
:pathname "src"
1819
:components ((:file "packages")
20+
(:file "path")
1921
(:file "make-binary")
20-
(:file "globals" :depends-on ("packages"))
22+
(:file "globals" :depends-on ("packages" "path"))
2123
(:file "hittable-circle" :depends-on ("packages"))
2224
(:file "classes" :depends-on ("packages"
2325
"hittable-circle"))
@@ -34,6 +36,7 @@
3436
"classes"
3537
"globals"))
3638
(:file "starwar" :depends-on ("packages"
39+
"path"
3740
"globals"
3841
"hittable-circle"
3942
"star"

starwar.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
;; configuration file for starwar game
22

33
(fullscreen nil)
4-
(screen-width 1200)
5-
(screen-height 800)
4+
(screen-width 800)
5+
(screen-height 600)
66

77
(star-speed-max 3.0)
88
(star-speed-min 0.5)

starwar.lisp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sbcl --script
2+
3+
4+
;; load quicklisp setup
5+
(let ((sbclrc "~/.sbclrc"))
6+
(when (probe-file sbclrc)
7+
(load sbclrc)))
8+
9+
(eval-when (:execute)
10+
(pushnew (truename (sb-unix:posix-getcwd/))
11+
ql:*local-project-directories*)
12+
(ql:register-local-projects)
13+
(ql:quickload :starwar))
14+
15+
(defun main ()
16+
(defparameter starwar:fullscreen t)
17+
(starwar:main))
18+
19+
(eval-when (:execute)
20+
(main))

0 commit comments

Comments
 (0)