;;; -*- lexical-binding: t -*- ;; ;; ----------------------------------------------------------------------------- ;; this file: https://dataswamp.org/~incal/bad-el/src/bad-sdl2/bad-sdl2.el ;; ----------------------------------------------------------------------------- (require 'cl-lib) (cl-pushnew "." load-path :test #'string=) (cl-pushnew ".." load-path :test #'string=) (require 'luki-lisp) (-> 'bad-ecs) (let ((dm "bad-sdl2.so")) (load dm nil t) (declare-function draw_init dm) (declare-function draw_quit dm) (declare-function draw_frame dm)) (defun bad-grid-string () (cl-loop for s across-ref ecs-grid concat (concat s "\n") into str finally return str)) (defun bad-grid-string-set-to-buffer () (cl-loop with lines = (--- ecs-grid) with buf-str = (take lines (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n")) for b in buf-str for s across-ref ecs-grid do (setf s b))) ;; (bad-grid-string-set-to-buffer) ecs-grid (defun bad-grid-string-hash () (benchmark (secure-hash 'sha256 (or (bad-grid-string) "")))) (defun hz () (let ((current-time-list nil)) (cdr (time-convert nil nil)))) (defun time-now () (let ((current-time-list nil)) (1st (time-convert nil nil)))) (defun sdl2-fps () (ecs-init) (about-emacs) (draw_init) (cl-loop with fps = 60 with hz = (hz) with ticks = (/ hz fps) with frames = (** 2 12) with beg = (time-now) with hline = (make-string 80 46) with slp = 0 with tslp = 0 for lne = 0 for f from 0 to frames for next = (+ beg (* (1+ f) ticks)) do (ecs-grid-clear) (bad-grid-string-set-to-buffer) (setf (aref ecs-grid lne) (@f "GNU Emacs %d.%d" emacs-major-version emacs-minor-version)) (setf (aref ecs-grid (++ lne)) "dynamic module SDL2") (setf (aref ecs-grid (++ lne)) hline) (setf (aref ecs-grid (++ lne)) (@f "frame %d / %d (%d%%)" f frames (round (* 100 (// f frames))))) (setf (aref ecs-grid (++ lne)) hline) (setf (aref ecs-grid (++ lne)) (@f "fps (set to) %d" fps)) (setf (aref ecs-grid (++ lne)) (@f "fps (actual) %.3f" (// (* hz f) (- (time-now) beg)))) (setf (aref ecs-grid (++ lne)) hline) (setf (aref ecs-grid (++ lne)) (@f "surplus time / frame: %.3f seconds" (// tslp (1+ f)))) (draw_frame) (setf slp (// (- next (time-now)) hz)) (incf tslp slp) (sleep-for slp) finally (sleep-for 4) (draw_quit))) (defun sdl2-solar () (cl-loop with emacs = (selected-frame) with run = t with fps = 60 with hz = (hz) with ticks = (/ hz fps) with frames = (** 2 12) with beg = (time-now) initially (ecs-solar-init) (draw_init) ;; BEG ugly hack to maintain keys ;; (set-frame-parameter nil 'alpha-background 0.0) (setq mode-line-format nil) (discard-input) (select-frame-set-input-focus emacs) ;; END for f from 0 to frames for next = (+ beg (* (1+ f) ticks)) while run do (pcase (& (input-pending-p) (read-event)) ((or ?q 13) (setf run nil)) ((or ?d ?l) (ecs-solar-camera-move-right)) ((or ?a ?j) (ecs-solar-camera-move-left)) ((or ?r ?p) (ecs-solar-camera-move-up)) ((or ?f ?\;) (ecs-solar-camera-move-down)) ((or ?w ?i) (ecs-solar-camera-move-forward)) ((or ?s ?k) (ecs-solar-camera-move-backward))) (ecs-solar-update-and-render) (draw_frame) (sit-for (// (- next (time-now)) hz)) finally ;; (set-frame-parameter nil 'alpha-background 1.0) (draw_quit))) (<- 'bad-sdl2)