From 0f37d9f2df042eb8ba021dd91b898c1f07d86b58 Mon Sep 17 00:00:00 2001
From: Bananymous <bananymousosq@gmail.com>
Date: Fri, 18 Oct 2024 03:44:10 +0300
Subject: [PATCH] Add support for banan-os

---
 doomgeneric/Makefile.banan_os        |  57 +++++++++++
 doomgeneric/doomgeneric_banan_os.cpp | 138 +++++++++++++++++++++++++++
 2 files changed, 200 insertions(+)
 create mode 100644 doomgeneric/Makefile.banan_os
 create mode 100644 doomgeneric/doomgeneric_banan_os.cpp

diff --git a/doomgeneric/Makefile.banan_os b/doomgeneric/Makefile.banan_os
new file mode 100644
index 0000000..0878148
--- /dev/null
+++ b/doomgeneric/Makefile.banan_os
@@ -0,0 +1,57 @@
+################################################################
+#
+# $Id:$
+#
+# $Log:$
+#
+
+ifeq ($(V),1)
+	VB=''
+else
+	VB=@
+endif
+
+CC=$(BANAN_ARCH)-banan_os-gcc
+CXX=$(BANAN_ARCH)-banan_os-g++
+CFLAGS+=-O3 -Wall -DNORMALUNIX -DLINUX -DSNDSERV -D_DEFAULT_SOURCE
+CXXFLAGS+=$(CFLAGS) --std=c++20
+LDFLAGS+=
+LIBS+=-lgui -linput -lstdc++
+
+# subdirectory for objects
+OBJDIR=build
+OUTPUT=$(OBJDIR)/doom
+
+SRC_DOOM = dummy.o am_map.o doomdef.o doomstat.o dstrings.o d_event.o d_items.o d_iwad.o d_loop.o d_main.o d_mode.o d_net.o f_finale.o f_wipe.o g_game.o hu_lib.o hu_stuff.o info.o i_cdmus.o i_endoom.o i_joystick.o i_scale.o i_sound.o i_system.o i_timer.o memio.o m_argv.o m_bbox.o m_cheat.o m_config.o m_controls.o m_fixed.o m_menu.o m_misc.o m_random.o p_ceilng.o p_doors.o p_enemy.o p_floor.o p_inter.o p_lights.o p_map.o p_maputl.o p_mobj.o p_plats.o p_pspr.o p_saveg.o p_setup.o p_sight.o p_spec.o p_switch.o p_telept.o p_tick.o p_user.o r_bsp.o r_data.o r_draw.o r_main.o r_plane.o r_segs.o r_sky.o r_things.o sha1.o sounds.o statdump.o st_lib.o st_stuff.o s_sound.o tables.o v_video.o wi_stuff.o w_checksum.o w_file.o w_main.o w_wad.o z_zone.o w_file_stdc.o i_input.o i_video.o doomgeneric.o doomgeneric_banan_os.o
+OBJS += $(addprefix $(OBJDIR)/, $(SRC_DOOM))
+
+all:	 $(OUTPUT)
+
+clean:
+	rm -rf $(OBJDIR)
+	rm -f $(OUTPUT)
+	rm -f $(OUTPUT).gdb
+	rm -f $(OUTPUT).map
+
+$(OUTPUT):	$(OBJS)
+	@echo [Linking $@]
+	$(VB)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) \
+	-o $(OUTPUT) $(LIBS) -Wl,-Map,$(OUTPUT).map
+	@echo [Size]
+	-$(CROSS_COMPILE)size $(OUTPUT)
+
+$(OBJS): | $(OBJDIR)
+
+$(OBJDIR):
+	mkdir -p $(OBJDIR)
+
+$(OBJDIR)/%.o:	%.c
+	@echo [Compiling $<]
+	$(VB)$(CC) $(CFLAGS) -c $< -o $@
+
+$(OBJDIR)/%.o:	%.cpp
+	@echo [Compiling $<]
+	$(VB)$(CXX) $(CXXFLAGS) -c $< -o $@
+
+print:
+	@echo OBJS: $(OBJS)
\ No newline at end of file
diff --git a/doomgeneric/doomgeneric_banan_os.cpp b/doomgeneric/doomgeneric_banan_os.cpp
new file mode 100644
index 0000000..9161771
--- /dev/null
+++ b/doomgeneric/doomgeneric_banan_os.cpp
@@ -0,0 +1,139 @@
+extern "C"
+{
+#include "doomgeneric.h"
+#include "doomkeys.h"
+}
+
+#include <assert.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/banan-os.h>
+#include <sys/framebuffer.h>
+#include <sys/mman.h>
+#include <time.h>
+
+#include <LibGUI/Window.h>
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
+static BAN::UniqPtr<LibGUI::Window> s_window;
+
+static constexpr size_t s_key_queue_size = 16;
+static unsigned short s_key_queue[s_key_queue_size];
+static size_t s_key_read_index = 0;
+static size_t s_key_write_index = 0;
+
+extern "C"
+{
+
+void DG_Init()
+{
+	s_window = MUST(LibGUI::Window::create(DOOMGENERIC_RESX, DOOMGENERIC_RESY, "DOOM"_sv));
+	s_window->set_key_event_callback(
+		[](LibGUI::EventPacket::KeyEvent::event_t event)
+		{
+			unsigned short doom_key = 0;
+			switch (event.key)
+			{
+				case LibInput::Key::Enter:
+					doom_key = KEY_ENTER;
+					break;
+				case LibInput::Key::Escape:
+					doom_key = KEY_ESCAPE;
+					break;
+				case LibInput::Key::ArrowLeft:
+					doom_key = KEY_LEFTARROW;
+					break;
+				case LibInput::Key::ArrowUp:
+					doom_key = KEY_UPARROW;
+					break;
+				case LibInput::Key::ArrowRight:
+					doom_key = KEY_RIGHTARROW;
+					break;
+				case LibInput::Key::ArrowDown:
+					doom_key = KEY_DOWNARROW;
+					break;
+				case LibInput::Key::LeftCtrl:
+				case LibInput::Key::RightCtrl:
+					doom_key = KEY_FIRE;
+					break;
+				case LibInput::Key::Space:
+					doom_key = KEY_USE;
+					break;
+				case LibInput::Key::RightShift:
+					doom_key = KEY_RSHIFT;
+					break;
+				default:
+				{
+					const char* utf8 = LibInput::key_to_utf8(event.key, event.modifier);
+					if (utf8 && strlen(utf8) == 1 && isalpha(*utf8))
+						doom_key = tolower(*utf8);
+				}
+			}
+
+			if (doom_key == 0)
+				return;
+
+			s_key_queue[s_key_write_index] = doom_key | (int)event.pressed() << 8;
+			s_key_write_index = (s_key_write_index + 1) % s_key_queue_size;
+		}
+	);
+}
+
+void DG_DrawFrame()
+{
+	auto& texture = s_window->texture();
+	for (size_t y = 0; y < DOOMGENERIC_RESY; y++)
+		for (size_t x = 0; x < DOOMGENERIC_RESX; x++)
+			texture.set_pixel(x, y, 0xFF000000 | DG_ScreenBuffer[y * DOOMGENERIC_RESX + x]);
+	s_window->invalidate();
+	s_window->poll_events();
+}
+
+void DG_SleepMs(uint32_t ms)
+{
+	struct timespec ts;
+	ts.tv_sec  =  ms / 1000;
+	ts.tv_nsec = (ms % 1000) * 1000000;
+	nanosleep(&ts, NULL);
+}
+
+uint32_t DG_GetTicksMs()
+{
+	struct timespec ts;
+	clock_gettime(CLOCK_MONOTONIC, &ts);
+	return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
+}
+
+int DG_GetKey(int* pressed, unsigned char* doomKey)
+{
+	if (s_key_read_index == s_key_write_index)
+		return 0;
+
+	unsigned short key_data = s_key_queue[s_key_read_index];
+	s_key_read_index = (s_key_read_index + 1) % s_key_queue_size;
+
+	*pressed = key_data >> 8;
+	*doomKey = key_data & 0xFF;
+
+	return 1;
+}
+
+void DG_SetWindowTitle(const char* title)
+{
+	(void)title;
+}
+
+int main(int argc, char** argv)
+{
+	doomgeneric_Create(argc, argv);
+
+	for (;;)
+		doomgeneric_Tick();
+
+	return 0;
+}
+
+}
-- 
2.45.2

