mirror of
https://github.com/MichaelFisher1997/opengl-cpp.git
synced 2025-09-09 06:54:03 +00:00
init SDL
This commit is contained in:
commit
803114bad0
41
MakeFile
Normal file
41
MakeFile
Normal file
@ -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
|
BIN
build/hello
Executable file
BIN
build/hello
Executable file
Binary file not shown.
BIN
build/opengl
Executable file
BIN
build/opengl
Executable file
Binary file not shown.
199
flake.lock
generated
Normal file
199
flake.lock
generated
Normal file
@ -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
|
||||||
|
}
|
91
flake.nix
Normal file
91
flake.nix
Normal file
@ -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]
|
||||||
|
}"
|
||||||
|
'';
|
||||||
|
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
12
src/main.cpp
Normal file
12
src/main.cpp
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
9
src/main.h
Normal file
9
src/main.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// for initializing and shutdown functions
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
// for rendering images and graphics on screen
|
||||||
|
#include <SDL2/SDL_image.h>
|
||||||
|
|
||||||
|
// for using SDL_Delay() functions
|
||||||
|
#include <SDL2/SDL_timer.h>
|
||||||
|
|
120
src/sdl.cpp
Normal file
120
src/sdl.cpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#include "sdl.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
src/sdl.hpp
Normal file
36
src/sdl.hpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
};
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user