commit 803114bad08126ef0f1b12d805e7ac58e6a1fb55 Author: MichaelFisher1997 Date: Sun Jan 5 01:30:18 2025 +0000 init SDL diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/MakeFile b/MakeFile new file mode 100644 index 0000000..f2b7558 --- /dev/null +++ b/MakeFile @@ -0,0 +1,41 @@ +# A simple Makefile for compiling small SDL projects + +# set the compiler +CC := clang + +# set the compiler flags +CFLAGS := `sdl2-config --libs --cflags` -ggdb3 -O0 --std=c99 -Wall -lSDL2_image -lm +# add header files here +HDRS := + +# add source files here +SRCS := #file-name.c + +# generate names of object files +OBJS := $(SRCS:.c=.o) + +# name of executable +EXEC := #name your executable file + +# default recipe +all: $(EXEC) + +showfont: showfont.c Makefile + $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) + +glfont: glfont.c Makefile + $(CC) -o $@ $@.c $(CFLAGS) $(LIBS) + +# recipe for building the final executable +$(EXEC): $(OBJS) $(HDRS) Makefile + $(CC) -o $@ $(OBJS) $(CFLAGS) + +# recipe for building object files +#$(OBJS): $(@:.o=.c) $(HDRS) Makefile +# $(CC) -o $@ $(@:.o=.c) -c $(CFLAGS) + +# recipe to clean the workspace +clean: + rm -f $(EXEC) $(OBJS) + +.PHONY: all clean diff --git a/build/hello b/build/hello new file mode 100755 index 0000000..a54469e Binary files /dev/null and b/build/hello differ diff --git a/build/opengl b/build/opengl new file mode 100755 index 0000000..ee5c45e Binary files /dev/null and b/build/opengl differ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..0382004 --- /dev/null +++ b/flake.lock @@ -0,0 +1,199 @@ +{ + "nodes": { + "devshell": { + "inputs": { + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1735644329, + "narHash": "sha256-tO3HrHriyLvipc4xr+Ewtdlo7wM1OjXNjlWRgmM7peY=", + "owner": "numtide", + "repo": "devshell", + "rev": "f7795ede5b02664b57035b3b757876703e2c3eac", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1733328505, + "narHash": "sha256-NeCCThCEP3eCl2l/+27kNNK7QrwZB1IJCrXfrbv5oqU=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1722073938, + "narHash": "sha256-OpX0StkL8vpXyWOGUD6G+MA26wAXK6SpT94kLJXo6B4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e36e9f57337d0ff0cf77aceb58af4c805472bfae", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1735915915, + "narHash": "sha256-Q4HuFAvoKAIiTRZTUxJ0ZXeTC7lLfC9/dggGHNXNlCw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a27871180d30ebee8aa6b11bf7fef8a52f024733", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1708161998, + "narHash": "sha256-6KnemmUorCvlcAvGziFosAVkrlWZGIc6UNT9GUYr0jQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "84d981bae8b5e783b3b548de505b22880559515f", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devshell": "devshell", + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs_2", + "zig": "zig" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "zig": { + "inputs": { + "flake-compat": "flake-compat_2", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_3" + }, + "locked": { + "lastModified": 1735906289, + "narHash": "sha256-bOjbgbuJvRHtHOFAJ9U+zEajk0N1Z1zG4MNtmsLt55Q=", + "owner": "mitchellh", + "repo": "zig-overlay", + "rev": "7ccbc34001590a16cb94e8be428020099493b190", + "type": "github" + }, + "original": { + "owner": "mitchellh", + "repo": "zig-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4749318 --- /dev/null +++ b/flake.nix @@ -0,0 +1,91 @@ +{ + description = "Zig Devshell"; + + inputs.devshell.url = "github:numtide/devshell"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.zig.url = "github:mitchellh/zig-overlay"; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + inputs.flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + + outputs = { + flake-utils, + devshell, + nixpkgs, + zig, + ... + }: + flake-utils.lib.eachDefaultSystem (system: { + devShells.default = let + pkgs = import nixpkgs { + inherit system; + + overlays = [devshell.overlays.default zig.overlays.default]; + }; + in + pkgs.mkShell { + packages = with pkgs; [ + zigpkgs.default + zls + ]; + + buildInputs = with pkgs; [ + clang-tools + cmake + codespell + conan + cppcheck + doxygen + gtest + lcov + vcpkg + vcpkg-tool + glfw + glfw-wayland + glfw3 + glm + assimp + # libdecor + mesa + alsa-lib + dbus + fontconfig + glew + libGL + libpulseaudio + libxkbcommon + makeWrapper + SDL2 + SDL2_image + patchelf + speechd + udev + # xwayland + xorg.libX11 + xorg.libXcursor + xorg.libXext + xorg.libXfixes + xorg.libXi + xorg.libXinerama + xorg.libXrandr + xorg.libXrender + vulkan-loader + vulkan-tools + vulkan-headers + ]; + # DISPLAY=":1"; + + # shellHook = '' export LD_LIBRARY_PATH=${pkgs.wayland}/lib:$LD_LIBRARY_PATH ''; + shellHook = '' + export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${ + with pkgs; + pkgs.lib.makeLibraryPath [ libGL xorg.libX11 xorg.libXi libxkbcommon mesa xorg.libXrender xorg.libXinerama glfw-wayland] + }" + ''; + + }; + }); + } diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..f958fe7 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,12 @@ +#include "sdl.hpp" + +int main(int argc, char* argv[]) { + // Create an instance of our SdlWindow class + SdlWindow window("My SDL2 Window", 800, 600); + + // Run the main loop + window.run(); + + return 0; +} + diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..174de40 --- /dev/null +++ b/src/main.h @@ -0,0 +1,9 @@ +// for initializing and shutdown functions +#include + +// for rendering images and graphics on screen +#include + +// for using SDL_Delay() functions +#include + diff --git a/src/sdl.cpp b/src/sdl.cpp new file mode 100644 index 0000000..7071c81 --- /dev/null +++ b/src/sdl.cpp @@ -0,0 +1,120 @@ +#include "sdl.hpp" +#include + +SdlWindow::SdlWindow(const char* title, int width, int height) + : m_window(nullptr), + m_renderer(nullptr), + m_isRunning(false), + m_isFullscreen(false), + m_width(width), + m_height(height) +{ + // 1. Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + std::cerr << "Failed to init SDL2: " << SDL_GetError() << std::endl; + return; + } + + // 2. Create the window + m_window = SDL_CreateWindow( + title, + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + width, + height, + SDL_WINDOW_SHOWN + ); + if (!m_window) { + std::cerr << "Failed to create window: " << SDL_GetError() << std::endl; + return; + } + + // 3. Create the renderer + m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED); + if (!m_renderer) { + std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl; + return; + } + + // 4. If everything succeeded, mark the application as running + m_isRunning = true; +} + +SdlWindow::~SdlWindow() { + // Cleanup + if (m_renderer) { + SDL_DestroyRenderer(m_renderer); + m_renderer = nullptr; + } + if (m_window) { + SDL_DestroyWindow(m_window); + m_window = nullptr; + } + SDL_Quit(); +} + +void SdlWindow::run() { + while (m_isRunning) { + processEvents(); + update(); + render(); + } +} + +void SdlWindow::processEvents() { +SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + m_isRunning = false; + } + // Handle other events (keyboard, mouse, etc.) here if needed + else if (event.type == SDL_KEYDOWN) { + // Check if the key pressed is Enter/Return + if (event.key.keysym.sym == SDLK_RETURN) { + std::cout << "input detected" << std::endl; + } + if (event.key.keysym.sym == SDLK_SPACE) { + std::cout << "Full screen togled!" << std::endl; + setFullscreen(!m_isFullscreen); + } + if (event.key.keysym.sym == SDLK_ESCAPE) { + std::cout << "Bye!" << std::endl; + m_isRunning = false; //exit application + } + } + } +} + +void SdlWindow::update() { + // Update game/application logic here +} + +void SdlWindow::render() { + // Set background color to red + SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 255); + SDL_RenderClear(m_renderer); + + // You can draw shapes, textures, etc. here + + SDL_RenderPresent(m_renderer); +} + +void SdlWindow::change_res(int newWidth, int newHeight) +{ + // Just call SDL_SetWindowSize + if (m_window) { + SDL_SetWindowSize(m_window, newWidth, newHeight); + } +} + +void SdlWindow::setFullscreen(bool fullscreen) { + if (m_window) { + m_isFullscreen = fullscreen; + if (m_isFullscreen) { + SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP); + } else { + SDL_SetWindowFullscreen(m_window, 0); // return to windowed + change_res(m_width, m_height); + } + } +} diff --git a/src/sdl.hpp b/src/sdl.hpp new file mode 100644 index 0000000..5f9a3bd --- /dev/null +++ b/src/sdl.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include + +// Forward declaration of classes and structs if needed +// class SomethingElse; + +class SdlWindow { +public: + // Constructor + SdlWindow(const char* title, int width, int height); + + // Destructor + ~SdlWindow(); + + // Run the main loop + void run(); + + void change_res(int newWidth, int newHeight); + void setFullscreen(bool fullscreen); + +private: + // Private data members + SDL_Window* m_window; + SDL_Renderer* m_renderer; + bool m_isRunning; + bool m_isFullscreen; + int m_width; + int m_height; + + // Private methods + void processEvents(); + void update(); + void render(); +}; +