Skip to content

Commit

Permalink
Somewhat modernize the code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderstephan committed Dec 25, 2023
1 parent 165c7cd commit a27eddb
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 328 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2021 Alexander Stephan alexanderstephan.xyz
Copyright (c) 2023 Alexander Stephan alexanderstephan.xyz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
CC = go
TARGET = gobra
bindir = /usr/local/bin
SOURCE_DIRS := $(shell find . -type d)
vpath %.go $(SOURCE_DIRS)

$(TARGET): *.go
$(TARGET): cmd/main.go
# Fix for ncurses install: https://github.com/rthornton128/goncurses/issues/56
export CGO_CFLAGS_ALLOW=".*"
export CGO_LDFLAGS_ALLOW=".*"
$(CC) get -x github.com/alexanderstephan/goncurses
$(CC) get -x github.com/rthornton128/goncurses
$(CC) get -x github.com/hajimehoshi/oto
$(CC) build -x
$(CC) build -o $(TARGET) -x $<
export CGO_CFLAGS_ALLOW=
export CGO_LDFLAGS_ALLOW=

.PHONY: all
all: $(TARGET)

.PHONY: install
install: all
mv $(TARGET) $(DESTDIR)$(bindir)/$(TARGET)

.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(bindir)/$(TARGET)

.PHONY: clean
clean:
rm -f $(TARGET)
31 changes: 31 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"flag"
"fmt"
"gobra/internal/gameplay"
"os"
"os/signal"
"syscall"
)

func main() {
var cfg gameplay.Config

flag.BoolVar(&cfg.Vim, "v", false, "Enable vim bindings")
flag.BoolVar(&cfg.DebugInfo, "d", false, "Print debug info")
flag.BoolVar(&cfg.NoBounds, "n", false, "Free boundaries")
flag.BoolVar(&cfg.Sound, "s", false, "Enable sound")

flag.Parse()

// Setup signal handler.
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
gameplay.Run = false // TODO: Cancel context
}()
fmt.Println(cfg.Vim)
gameplay.Start(&cfg)
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module gobra

go 1.17
go 1.21

require (
github.com/alexanderstephan/goncurses v0.0.0-20200808213228-94cbc32628dd
github.com/hajimehoshi/oto/v2 v2.4.1
github.com/rthornton128/goncurses v0.0.0-20231014161942-82671379df88
)

require (
github.com/ebitengine/purego v0.4.0 // indirect
github.com/rthornton128/goncurses v0.0.0-20210908011339-931b33a34c71 // indirect
golang.org/x/sys v0.7.0 // indirect
)
25 changes: 2 additions & 23 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
github.com/alexanderstephan/goncurses v0.0.0-20200808213228-94cbc32628dd h1:CL10HSMSUDGBD1Iz/KzqEc6UX13+z7d1EXgf7PrClNY=
github.com/alexanderstephan/goncurses v0.0.0-20200808213228-94cbc32628dd/go.mod h1:ceMh1H2Mi5wk1+YVlxmlVb6zTrn0YEZt9F1QfeAInA0=
github.com/ebitengine/purego v0.3.0 h1:BDv9pD98k6AuGNQf3IF41dDppGBOe0F4AofvhFtBXF4=
github.com/ebitengine/purego v0.3.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/ebitengine/purego v0.4.0 h1:RQVuMIxQPQ5iCGEJvjQ17YOK+1tMKjVau2FUMvXH4HE=
github.com/ebitengine/purego v0.4.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/hajimehoshi/oto/v2 v2.0.2 h1:kbMCXhKAZMVyhEEyIqEqmSf4xjfk3HPAf1tMcyKCsN0=
github.com/hajimehoshi/oto/v2 v2.0.2/go.mod h1:rUKQmwMkqmRxe+IAof9+tuYA2ofm8cAWXFmSfzDN8vQ=
github.com/hajimehoshi/oto/v2 v2.3.1 h1:qrLKpNus2UfD674oxckKjNJmesp9hMh7u7QCrStB3Rc=
github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
github.com/hajimehoshi/oto/v2 v2.4.0 h1:2A8QvGJZ7nXwcfIIthaqWdzDn9Ul/er6oASiKcsfiLg=
github.com/hajimehoshi/oto/v2 v2.4.0/go.mod h1:74bRBgfJaEDpP3NyVyHIYBJE4DgzJ2IP5l/st5qcJog=
github.com/hajimehoshi/oto/v2 v2.4.1 h1:iTfZSulqdmQ5Hh4tVyVzNnK3aA4SgjbDapSM0YH3Lc4=
github.com/hajimehoshi/oto/v2 v2.4.1/go.mod h1:guyF8uIgSrchrKewS1E6Xyx7joUbKOi4g9W7vpcYBSc=
github.com/rthornton128/goncurses v0.0.0-20210908011339-931b33a34c71 h1:1lA/ljJAwqsNo7HHUG7M4XY9dkp6aYnnW5Tglh7bEgA=
github.com/rthornton128/goncurses v0.0.0-20210908011339-931b33a34c71/go.mod h1:AHlKFomPTwmO7H2vL8d7VNrQNQmhMi/DBhDnHRhjbCo=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/rthornton128/goncurses v0.0.0-20231014161942-82671379df88 h1:bNvZ7P4l4rvg1O3qNDRJ4FNgKASMoG+V7OIpu4SIkgA=
github.com/rthornton128/goncurses v0.0.0-20231014161942-82671379df88/go.mod h1:AHlKFomPTwmO7H2vL8d7VNrQNQmhMi/DBhDnHRhjbCo=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Binary file removed gobra
Binary file not shown.
92 changes: 0 additions & 92 deletions gobra.go

This file was deleted.

13 changes: 9 additions & 4 deletions controls.go → internal/gameplay/controls.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main
package gameplay

import (
gc "github.com/alexanderstephan/goncurses"
"fmt"

gc "github.com/rthornton128/goncurses"
)

// Controls
Expand All @@ -12,9 +14,10 @@ var (
keyRight byte
)

// HandleKeys handles keyboard input for controlling the snake and performing other actions.
func HandleKeys(input *gc.Window, stdscr *gc.Window, myFood *Food) bool {
// Get input from a dedicated window, otherwise stdscr would be blocked
// Define input handlers with interrupt condition
// Define input handlers with interrupt condition.
switch input.GetChar() {
case gc.Key(keyUp):
if d != South {
Expand Down Expand Up @@ -43,13 +46,15 @@ func HandleKeys(input *gc.Window, stdscr *gc.Window, myFood *Food) bool {
}

func initKeybindings(isVim bool) {
// Remap to vim like bindings
// Remap to vim like bindings.
if isVim {
fmt.Println(" is Vim")
keyLeft = 'h'
keyDown = 'j'
keyUp = 'k'
keyRight = 'l'
} else {
fmt.Println(" is not Vim")
keyLeft = 'a'
keyUp = 'w'
keyDown = 's'
Expand Down
Loading

0 comments on commit a27eddb

Please sign in to comment.