DEV Community

The Struggling Dev
The Struggling Dev

Posted on

(Game)Dev with Emacs - Because it's not Already Hard Enough Without it

Introduction

This is another tale from down the rabbit hole. See Writing an Emacs Major Mode - Because Why Not? (DEV), for my first post on Emacs. As I wrote in my post on Character Design (DEV), I'm playing around with writing a game. And to make it even more fun I've decided to use Emacs for it.

Emacs Setup and Syntax Highlighting & Code Complete

I'm using Doom Emacs. I'm refraining from reproducing the installation guide here and just point to it for anyone who's interested: Doom Emacs #Install (GitHub). After the installation we activate support for C++ and LSP (auto-complete and stuff) by removing the comments from the init.el file (just press SPC+f+P on the Doom dashboard and choose init.el). Search for "lsp" and "(cc" and remove the leading ;;.

;;lsp
;;(cc +lsp)
Enter fullscreen mode Exit fullscreen mode

Additionally, we add the following line to the end of the init.el.

(setenv "LSP_USE_PLISTS" "1")
Enter fullscreen mode Exit fullscreen mode

This line improves LSP performance significantly, see LSP Mode - Performance for more performance tips.

If you've already used LSP mode in Emacs without plists active, you have to delete the LSP package first - otherwise you'll get errors when activating LSP mode the next time.

According to the documentation you should be able to just delete the package by using package-delete, but in my case I couldn't find any lsp packages listed. If you run in the same problem, and are on MacOS, here's how to delete the packages manually.

# cd into your .emacs folder and search for lsp stuff
find ./ -name "lsp*"
# and delete it - after checking the results. In my case, removing the following did the trick.
rm -rf .//.local/straight/build-29.1/lsp*
rm -rf .//.local/straight/repos/lsp*

After these changes we close Emacs and run doom sync. Back in Emacs we can run M-x lsp-doctor to check whether we're all good.

Doctor Who? Doctor LSP!

The native compilation part is intriguing, but I haven't had time to check this out more closely and after switching to plists performance was fine.

If we now open a projectile project (a directory with a .git folder should automatically be identified as a project), we have syntax highlighting and code completion.

Code completion in Emacs

Now that we have some of the creature comforts for developing in place, let's compile an application.

Compiling an Application

While we're in a project we can start compilation with SPC+p+c. The issue is, we have to provide the compile command ourselves. For my project I'll call CMake. Typing this every time we want to compile is annoying, luckily Emacs remembers our last command. But what if we have multiple components we want to compile, which we should've because tests 😉.

For this case I've started using bash files to compile and run my projects.

#!/bin/bash
cmd=$1
if [ "$cmd" = "build" ]; then
  cd build && cmake --build .
elif [ "$cmd" = "run" ]; then
  cd build && cmake --build . && ./MyGame
elif [ "$cmd" = "configure" ]; then
  cd build && cmake ../src
elif [ "$cmd" = "test" ]; then
  cd build && cmake --build . && ctest
else
  cd build && cmake --build . && ./MyGame
fi
Enter fullscreen mode Exit fullscreen mode

With this file in our project root, we can just SPC+p+c and ./run.sh for most cases when we want to compile and run the application. Easy as pie, or is it?

Should you Ditch your IDE?

I'm not deep enough down the rabbit hole to really provide a profound answer to this question yet, sorry. But, I'll do it anyway - as someone still on the journey might describe the destination he hasn't yet seen: NOOOO. Thanks for reading.

But let me elaborate. Like with everything in life there are trade offs.

What I Like

It's a stupid argument, (Doom) Emacs just looks and feels "cool" 🤓. It has that "hacker"-aesthetic.

But on to some functional arguments.

  • Highly optimized for keyboard input. Emacs does have mouse support, but I rarely use the touchpad for anything with Emacs.
  • It's relatively light-weight and easy on resources.
  • It's highly customizable
  • Org mode. Org mode is just great for taking notes and organizing tasks. I might write a post on it one day. If you're interested, check out Org Mode in the mean time.
  • I enjoy just having one editor for everything - kinda like an integrated development environment 🤔. Sure you can code and take notes in CLion or VS Code as well, but can you also browse?🤪 Emacs has you covered with the Emacs Web Wowser (eww). It's basic, it's ugly and I've used it only a few times for quick searches, but it's there.

Stuff that's Missing or That I Don't Like

  • I've previously used CLion for C++ development, and there's a few things I miss. For one, I'm not a C++ developer by trade, so I mostly suck at it. CLion is great that way as it supports you with tips and best practice hints. Maybe, likely, I can add linters to Emacs as well.
  • There are some vim commands that are not mapped in EVIL mode.
  • Debugging seems to be possible, didn't get around trying it out.
  • The basics are much harder than with any IDE. Not only do you have to do something extra to get code completion, you (might) have to do even more to make it fast.

For all its power, Emacs makes you earn it. It's not comfy - it might be after you've configured everything you need. If you ever get there:

The Struggles

Emacs - Emacs is suffering. It makes you swear, it makes you despair, ... and it makes you celebrate if/when it finally works. Emacs is interesting, powerful and highly customizable. And with that comes the big downside. Although you most likely can do everything with Emacs, it takes time, research, configuration, trial & error and nerves.

The biggest challenge when writing this post, was what I should leave out. For example reproducing the Doom Emacs installation guide seemed to be a waste of time and space. If something's missing, don't hesitate to ask/point it out in the comments.

Thanks for reading and keep on struggling.

Top comments (0)