From 8dcd42c97b014ae6b397fe6375da1f7a2e4cc074 Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Thu, 11 Sep 2025 23:07:38 +0100 Subject: [PATCH] Initial commit of 2D RPG Engine. @dacav come help. --- .gitignore | 1 + CMakeLists.txt | 17 ++++++++++ Makefile | 46 +++++++++++++++++++++++++++ README.org | 39 +++++++++++++++++++++++ src/main.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Makefile create mode 100644 README.org create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e56e04 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1bf8cb4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.16) + +project(bettola VERSION 0.1) + +# Let's use C++17? +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Deps. +find_package(SDL3 REQUIRED) +find_package(GLEW REQUIRED) +find_package(OpenGL REQUIRED) + +add_executable(bettola src/main.cpp) + +target_link_libraries(bettola PRIVATE SDL3::SDL3 GLEW::glew OpenGL::GL) + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6d88c3a --- /dev/null +++ b/Makefile @@ -0,0 +1,46 @@ +SHELL:=/bin/bash +BUILD_DIR:=bin +PROJECT_NAME:=bettola +EXECUTABLE:=$(BUILD_DIR)/$(PROJECT_NAME) + +CXX_COMPILER:=clang++ +CMAKE_MAKEFILE:=$(BUILD_DIR)/Makefile + +.PHONY: all build config run clean help + +# Default. +all: build + +# Build project, if not configured, then do that first. +build: $(CMAKE_MAKEFILE) + @echo "==== Building Bettola ====" + @cmake --build $(BUILD_DIR) + +# run 'config' target if the build directory or CMake cache is missing. +$(CMAKE_MAKEFILE): + $(MAKE) config + +config: + @echo "==== Configuring Bettola with CMake ====" + @mkdir -p $(BUILD_DIR) + @cmake -S . -B$(BUILD_DIR) -DCMAKE_CXX_COMPILER=$(CXX_COMPILER) + +# Build and run. +run: all + @echo "==== Running Bettola ====" + $(EXECUTABLE) + +# Remove build dir. +clean: + @echo "==== Cleaning Bettola ====" + @rm -rf $(BUILD_DIR) + @echo "==== Project Cleaned ====" + +help: + @echo "Available commands:" + @echo " make - Build the project (default)." + @echo " make build - Build the project." + @echo " make run - Build and run the project." + @echo " make config - Force CMake to re-configure the project." + @echo " make clean - Remove all build files" + diff --git a/README.org b/README.org new file mode 100644 index 0000000..b8c2aa8 --- /dev/null +++ b/README.org @@ -0,0 +1,39 @@ +#+TITLE: Bettola Game + +A 2D RPG game and engine created with C++, SDL3, and OpenGL. + +* Dependencies +The following dependencies are requird: + - A C++ compiler (e.g., clang, g++) + - CMake (3.16 or newer) + - SDL3 development libraries + - GLEW development libraries + +** Installation (Debian) +You can install all required dependencies with the following command: +#+BEGIN_SRC bash +sudo apt update +sudo apt install build-essential clang cmake libsdl3-dev libglew-dev +#+END_SRC + +* Build Instructions +This project uses a top-level Makefile to simplify the CMake workflow. All build artifacts will be placed in the =bin/= directory. + +Run the following from the root of the project directory. + + - *Build the project:* + The executable will be located in =bin/bettola=. + #+BEGIN_SRC bash + make + #+END_SRC + + - *Build and run the project:* + #+BEGIN_SRC bash + make run + #+END_SRC + + - *Clean the project:* + This removes the =bin/= directory. + #+BEGIN_SRC bash + make clean + #+END_SRC diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c448ce5 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +const int SCREEN_WIDTH = 800; +const int SCREEN_HEIGHT = 600; + +int main(int argc, char* argv[]) { + /* Initialise SDL. */ + if(!SDL_Init(SDL_INIT_VIDEO)) { + fprintf(stderr, "Failed to init SDL! SDL ERROR: %s\n", SDL_GetError()); + return -1; + } + + /* OpenGL context attributes */ + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + + /* Create the window. */ + SDL_Window* window = SDL_CreateWindow( + "Bettola Makes No Sense!", + SCREEN_WIDTH, + SCREEN_HEIGHT, + SDL_WINDOW_OPENGL + ); + + if(!window) { + fprintf(stderr, "Failed to create window! SDL_ERROR: %s\n", SDL_GetError()); + SDL_Quit(); + return -1; + } + + /* OpenGL context. */ + SDL_GLContext gl_context = SDL_GL_CreateContext(window); + if(!gl_context) { + fprintf(stderr, "Failed to create OpenGL context! SDL_ERROR: %s\n", SDL_GetError()); + SDL_DestroyWindow(window); + SDL_Quit(); + return -1; + } + + /* GLEW init - Must be done AFTER creating the GL context. */ + glewExperimental = GL_TRUE; + GLenum glew_error = glewInit(); + if(glew_error != GLEW_OK) { + fprintf(stderr, "Failed to init GLEW! %s\n", glewGetErrorString(glew_error)); + SDL_GL_DestroyContext(gl_context); + SDL_DestroyWindow(window); + SDL_Quit(); + return -1; + } + + /* Set viewport. */ + glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT); + + /* Main game loop.. For now. */ + bool is_running = true; + SDL_Event event; + + while(is_running) { + /* Event handling. */ + while(SDL_PollEvent(&event)) { + if(event.type == SDL_EVENT_QUIT) { + is_running = false; + } + } + + /* Render that b*tch. */ + glClearColor(0.1f, 0.1f, 0.3f, 1.0f); /* Dark blue or something. */ + glClear(GL_COLOR_BUFFER_BIT); + + /* Swap the buffers. */ + SDL_GL_SwapWindow(window); + } + + /* Clean up after ourselves. */ + SDL_GL_DestroyContext(gl_context); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +}