From ca393b6b47618e48c107ad5c021d86700343648e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 21 Sep 2024 15:31:58 +0200 Subject: move gameloop poc to mwe --- gameloop/.vscode/settings.json | 54 -------------------------- gameloop/CMakeLists.txt | 28 -------------- gameloop/imgs/demo.bmp | Bin 7998698 -> 0 bytes gameloop/imgs/demo.jpg | Bin 903706 -> 0 bytes gameloop/include/eventManager.h | 4 -- gameloop/include/gameObject.h | 29 -------------- gameloop/include/loopManager.h | 25 ------------ gameloop/include/timer.h | 30 --------------- gameloop/include/window.h | 20 ---------- gameloop/src/eventManager.cpp | 0 gameloop/src/gameObject.cpp | 58 ---------------------------- gameloop/src/loopManager.cpp | 72 ----------------------------------- gameloop/src/main.cpp | 19 --------- gameloop/src/timer.cpp | 68 --------------------------------- gameloop/src/window.cpp | 60 ----------------------------- gameloop/versions/delayBased.cpp | 57 --------------------------- mwe/gameloop/CMakeLists.txt | 28 ++++++++++++++ mwe/gameloop/imgs/demo.bmp | Bin 0 -> 7998698 bytes mwe/gameloop/imgs/demo.jpg | Bin 0 -> 903706 bytes mwe/gameloop/include/eventManager.h | 4 ++ mwe/gameloop/include/gameObject.h | 29 ++++++++++++++ mwe/gameloop/include/loopManager.h | 25 ++++++++++++ mwe/gameloop/include/timer.h | 30 +++++++++++++++ mwe/gameloop/include/window.h | 20 ++++++++++ mwe/gameloop/src/eventManager.cpp | 0 mwe/gameloop/src/gameObject.cpp | 58 ++++++++++++++++++++++++++++ mwe/gameloop/src/loopManager.cpp | 72 +++++++++++++++++++++++++++++++++++ mwe/gameloop/src/main.cpp | 19 +++++++++ mwe/gameloop/src/timer.cpp | 68 +++++++++++++++++++++++++++++++++ mwe/gameloop/src/window.cpp | 60 +++++++++++++++++++++++++++++ mwe/gameloop/versions/delayBased.cpp | 57 +++++++++++++++++++++++++++ 31 files changed, 470 insertions(+), 524 deletions(-) delete mode 100644 gameloop/.vscode/settings.json delete mode 100644 gameloop/CMakeLists.txt delete mode 100644 gameloop/imgs/demo.bmp delete mode 100644 gameloop/imgs/demo.jpg delete mode 100644 gameloop/include/eventManager.h delete mode 100644 gameloop/include/gameObject.h delete mode 100644 gameloop/include/loopManager.h delete mode 100644 gameloop/include/timer.h delete mode 100644 gameloop/include/window.h delete mode 100644 gameloop/src/eventManager.cpp delete mode 100644 gameloop/src/gameObject.cpp delete mode 100644 gameloop/src/loopManager.cpp delete mode 100644 gameloop/src/main.cpp delete mode 100644 gameloop/src/timer.cpp delete mode 100644 gameloop/src/window.cpp delete mode 100644 gameloop/versions/delayBased.cpp create mode 100644 mwe/gameloop/CMakeLists.txt create mode 100644 mwe/gameloop/imgs/demo.bmp create mode 100644 mwe/gameloop/imgs/demo.jpg create mode 100644 mwe/gameloop/include/eventManager.h create mode 100644 mwe/gameloop/include/gameObject.h create mode 100644 mwe/gameloop/include/loopManager.h create mode 100644 mwe/gameloop/include/timer.h create mode 100644 mwe/gameloop/include/window.h create mode 100644 mwe/gameloop/src/eventManager.cpp create mode 100644 mwe/gameloop/src/gameObject.cpp create mode 100644 mwe/gameloop/src/loopManager.cpp create mode 100644 mwe/gameloop/src/main.cpp create mode 100644 mwe/gameloop/src/timer.cpp create mode 100644 mwe/gameloop/src/window.cpp create mode 100644 mwe/gameloop/versions/delayBased.cpp diff --git a/gameloop/.vscode/settings.json b/gameloop/.vscode/settings.json deleted file mode 100644 index 6cee9cb..0000000 --- a/gameloop/.vscode/settings.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "list": "cpp", - "map": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "initializer_list": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "cinttypes": "cpp", - "typeinfo": "cpp", - "valarray": "cpp" - } -} diff --git a/gameloop/CMakeLists.txt b/gameloop/CMakeLists.txt deleted file mode 100644 index 6bc5017..0000000 --- a/gameloop/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(gameloop) - -# Set the C++ standard (optional, but good practice) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED True) - -# Find the SDL2 package -find_package(SDL2 REQUIRED) - -# Add your executable -set(SOURCES - src/loopManager - src/window.cpp - src/main.cpp - src/eventManager.cpp - src/gameObject.cpp - src/timer.cpp -) - -add_executable(gameloop ${SOURCES}) - -# Link the SDL2 library to your project -target_link_libraries(gameloop ${SDL2_LIBRARIES}) - -# Include SDL2 header files and project headers -target_include_directories(gameloop PRIVATE ${SDL2_INCLUDE_DIRS}) -target_include_directories(gameloop PRIVATE ${CMAKE_SOURCE_DIR}/include) diff --git a/gameloop/imgs/demo.bmp b/gameloop/imgs/demo.bmp deleted file mode 100644 index 65354fe..0000000 Binary files a/gameloop/imgs/demo.bmp and /dev/null differ diff --git a/gameloop/imgs/demo.jpg b/gameloop/imgs/demo.jpg deleted file mode 100644 index f534e1b..0000000 Binary files a/gameloop/imgs/demo.jpg and /dev/null differ diff --git a/gameloop/include/eventManager.h b/gameloop/include/eventManager.h deleted file mode 100644 index 2aa0a68..0000000 --- a/gameloop/include/eventManager.h +++ /dev/null @@ -1,4 +0,0 @@ -class EventManager -{ - -}; diff --git a/gameloop/include/gameObject.h b/gameloop/include/gameObject.h deleted file mode 100644 index 0e17991..0000000 --- a/gameloop/include/gameObject.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include -class GameObject { - public: - GameObject(); - GameObject(std::string name, float x, float y, float width, float height, float velX, float velY); - std::string getName() const; - float getX() const; - float getY() const; - float getWidth() const; - float getHeight() const; - float getVelX() const; - float getVelY() const; - void setName(std::string value); - void setX(float value); - void setY(float value); - void setWidth(float value); - void setHeight(float value); - void setVelX(float value); - void setVelY(float value); - private: - std::string name = ""; - float x = 0; - float y = 0; - float width = 0; - float height = 0; - float velX = 0; - float velY = 0; -}; diff --git a/gameloop/include/loopManager.h b/gameloop/include/loopManager.h deleted file mode 100644 index 06bcd5f..0000000 --- a/gameloop/include/loopManager.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include "window.h" -#include "gameObject.h" -class LoopManager{ - public: - LoopManager(); - void setup(); - void loop(); - private: - - std::vector objectList; - void processInput(); - void update(); - void lateUpdate(); - void fixedUpdate(); - void render(); - bool gameRunning = false; - WindowManager window; - int timeScale = 1; - float accumulator = 0.0; - double currentTime; - double t = 0.0; - double dt = 0.01; -}; diff --git a/gameloop/include/timer.h b/gameloop/include/timer.h deleted file mode 100644 index a245e5c..0000000 --- a/gameloop/include/timer.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include - -class LoopTimer { -public: - static LoopTimer& getInstance(); - void start(); - void update(); - double getDeltaTime() const; - int getCurrentTime() const; - void advanceFixedUpdate(); - double getFixedDeltaTime() const; - void setFPS(int FPS); - int getFPS() const; - void enforceFrameRate(); - double getLag() const; -private: - LoopTimer(); - int FPS = 50; - double gameScale = 1; - double maximumDeltaTime = 0.25; - double deltaTime; - double frameTargetTime = FPS / 1000; - double fixedDeltaTime = 0.01; - double elapsedTime; - double elapsedFixedTime; - double time; - uint64_t lastFrameTime; -}; diff --git a/gameloop/include/window.h b/gameloop/include/window.h deleted file mode 100644 index cfde0e9..0000000 --- a/gameloop/include/window.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include -#include -#include "gameObject.h" -#include -class WindowManager{ - public: - WindowManager(); - virtual ~WindowManager(); - void render(std::vector objects); - bool initWindow(); - void destroyWindow(); - - SDL_Renderer* getRenderer(); - private: - const int SCREEN_WIDTH = 800; - const int SCREEN_HEIGHT = 600; - SDL_Window* window = NULL; - SDL_Renderer* renderer = NULL; -}; diff --git a/gameloop/src/eventManager.cpp b/gameloop/src/eventManager.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/gameloop/src/gameObject.cpp b/gameloop/src/gameObject.cpp deleted file mode 100644 index f637314..0000000 --- a/gameloop/src/gameObject.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "gameObject.h" -std::string GameObject::getName() const{ - return name; -} -float GameObject::getX() const{ - return x; -} - -float GameObject::getY() const{ - return y; -} - -float GameObject::getWidth() const{ - return width; -} - -float GameObject::getHeight() const{ - return height; -} - -float GameObject::getVelX() const{ - return velX; -} - -float GameObject::getVelY() const{ - return velY; -} -void GameObject::setName(std::string value){ - name = value; -} -void GameObject::setX(float value){ - x = value; -} - -void GameObject::setY(float value){ - y = value; -} - -void GameObject::setWidth(float value){ - width = value; -} - -void GameObject::setHeight(float value){ - height = value; -} - -void GameObject::setVelX(float value){ - velX = value; -} - -void GameObject::setVelY(float value){ - velY = value; -} - -GameObject::GameObject(std::string name, float x, float y, float width, float height, float velX, float velY) - : name(name),x(x), y(y), width(width), height(height), velX(velX), velY(velY) { - - } diff --git a/gameloop/src/loopManager.cpp b/gameloop/src/loopManager.cpp deleted file mode 100644 index f7ce886..0000000 --- a/gameloop/src/loopManager.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "loopManager.h" -#include "timer.h" -LoopManager::LoopManager(){ -} -void LoopManager::processInput(){ - SDL_Event event; - SDL_PollEvent(&event); - switch(event.type){ - case SDL_QUIT: - gameRunning = false; - break; - case SDL_KEYDOWN: - if(event.key.keysym.sym == SDLK_ESCAPE){ - gameRunning = false; - } - break; - } -} -void LoopManager::fixedUpdate(){ - fprintf(stderr,"fixed update\n"); -} -void LoopManager::loop() { - LoopTimer& timer = LoopTimer::getInstance(); - timer.start(); - - while (gameRunning) { - timer.update(); - - while (timer.getLag() >= timer.getFixedDeltaTime()) { - processInput(); - fixedUpdate(); - timer.advanceFixedUpdate(); - } - - update(); - render(); - - timer.enforceFrameRate(); - } - - window.destroyWindow(); -} - -void LoopManager::setup(){ - gameRunning = window.initWindow(); - LoopTimer::getInstance().start(); - LoopTimer::getInstance().setFPS(50); - - for(int i = 0; i < 2;i++){ - GameObject* square = new GameObject("square2",i*40,i*40,20,20,0,0); - objectList.push_back(square); - } -} -void LoopManager::render(){ - fprintf(stderr,"**********render********** \n"); - if(gameRunning){ - window.render(objectList); - } -} - -void LoopManager::update() { - fprintf(stderr,"**********normal update********** \n"); - LoopTimer& timer = LoopTimer::getInstance(); - - float delta= timer.getDeltaTime(); - - for (int i = 0; i < objectList.size(); i++) { - objectList[i]->setX(objectList[i]->getX() + 50 * delta); - objectList[i]->setY(objectList[i]->getY() + 50 * delta); - } -} - diff --git a/gameloop/src/main.cpp b/gameloop/src/main.cpp deleted file mode 100644 index 2333e63..0000000 --- a/gameloop/src/main.cpp +++ /dev/null @@ -1,19 +0,0 @@ -//Using SDL and standard IO -#include -#include -//#include "window.h" -#include "loopManager.h" -#include "timer.h" -//Screen dimension constants - - -//Starts up SDL and creates window - -int main( int argc, char* args[] ) -{ - LoopManager gameLoop; - gameLoop.setup(); - gameLoop.loop(); - - return 0; -} diff --git a/gameloop/src/timer.cpp b/gameloop/src/timer.cpp deleted file mode 100644 index 1e3045f..0000000 --- a/gameloop/src/timer.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "timer.h" - -// Constructor (private) -LoopTimer::LoopTimer(){} - -// Get the singleton instance of the timer -LoopTimer& LoopTimer::getInstance() { - static LoopTimer instance; - return instance; -} - -// Start the timer (initialize frame time) -void LoopTimer::start() { - lastFrameTime = SDL_GetTicks64(); - elapsedTime = 0; - elapsedFixedTime = 0; - deltaTime = 0; -} - -// Update the timer, calculate deltaTime -void LoopTimer::update() { - uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds - - if (deltaTime > maximumDeltaTime) { - deltaTime = maximumDeltaTime; - } - - elapsedTime += deltaTime; - lastFrameTime = currentFrameTime; -} - -double LoopTimer::getDeltaTime() const { - return deltaTime; -} -int LoopTimer::getCurrentTime() const { - return SDL_GetTicks(); -} - -void LoopTimer::advanceFixedUpdate() { - elapsedFixedTime += fixedDeltaTime; -} - -double LoopTimer::getFixedDeltaTime() const { - return fixedDeltaTime; -} - -void LoopTimer::setFPS(int FPS) { - this->FPS = FPS; - frameTargetTime = 1.0 / FPS; -} - -int LoopTimer::getFPS() const { - return FPS; -} - -void LoopTimer::enforceFrameRate() { - uint64_t currentFrameTime = SDL_GetTicks64(); - double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; - - if (frameDuration < frameTargetTime) { - uint32_t delayTime = (uint32_t)((frameTargetTime - frameDuration) * 1000.0); - SDL_Delay(delayTime); - } -} -double LoopTimer::getLag() const { - return elapsedTime - elapsedFixedTime; -} diff --git a/gameloop/src/window.cpp b/gameloop/src/window.cpp deleted file mode 100644 index f998a79..0000000 --- a/gameloop/src/window.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "window.h" -WindowManager::WindowManager(){ - if( !initWindow() ) - { - printf( "Failed to initialize!\n" ); - } -} -WindowManager::~WindowManager(){ - destroyWindow(); -} -SDL_Renderer* WindowManager::getRenderer(){ - return renderer; -} - -void WindowManager::render(std::vector objects){ - // Set the draw color to black and clear the screen - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderClear(renderer); - - // Print object position (optional for debugging) - //fprintf(stderr, "%d\n", objectList.size()); - for(int i = 0; i < objects.size();i++){ - //fprintf(stderr, "%f\n", objectList[i]->getX()); - // Create a rectangle representing the ball - SDL_Rect ball_rect = { - (int)objects[i]->getX(), - (int)objects[i]->getY(), - (int)objects[i]->getWidth(), - (int)objects[i]->getHeight(), - }; - // Set the draw color to white and render the ball - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderFillRect(renderer, &ball_rect); - } - - SDL_RenderPresent(renderer); -} - -bool WindowManager::initWindow(){ - if(SDL_Init(SDL_INIT_EVERYTHING) != 0){ - fprintf(stderr,"Error inititalising SDL.\n"); - return false; - } - window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); - if(!window){ - fprintf(stderr,"Error creating SDL Window. \n"); - return false; - } - renderer = SDL_CreateRenderer(window,-1,0); - if(!renderer){ - fprintf(stderr,"Error creating SDL renderer. \n"); - return false; - } - return true; -} -void WindowManager::destroyWindow(){ - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); -} diff --git a/gameloop/versions/delayBased.cpp b/gameloop/versions/delayBased.cpp deleted file mode 100644 index a8cad15..0000000 --- a/gameloop/versions/delayBased.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "loopManager.h" -#include "timer.h" -LoopManager::LoopManager(){ -} -void LoopManager::processInput(){ - SDL_Event event; - SDL_PollEvent(&event); - switch(event.type){ - case SDL_QUIT: - gameRunning = false; - break; - case SDL_KEYDOWN: - if(event.key.keysym.sym == SDLK_ESCAPE){ - gameRunning = false; - } - break; - } -} -void LoopManager::loop(){ - fprintf(stderr,"loop. \n"); - while(gameRunning){ - //Timer::getInstance().update(); - //deltaTime = Timer::getInstance().getDeltaTime(); - processInput(); - update(); - render(); - } - window.destroyWindow(); -} -void LoopManager::setup(){ - gameRunning = window.initWindow(); - LoopTimer::getInstance().start(); - LoopTimer::getInstance().setFPS(210); - - for(int i = 0; i < 2;i++){ - GameObject* square2 = new GameObject("square2",i*40,i*40,20,20,0,0); - objectList.push_back(square2); - } -} -void LoopManager::render(){ - if(gameRunning){ - window.render(objectList); - } -} - -void LoopManager::update() { - LoopTimer& timer = LoopTimer::getInstance(); - timer.enforceFrameRate(); - timer.update(); - float delta= timer.getDeltaTime(); - - for (int i = 0; i < objectList.size(); i++) { - objectList[i]->setX(objectList[i]->getX() + 50 * delta); - objectList[i]->setY(objectList[i]->getY() + 50 * delta); - } -} - diff --git a/mwe/gameloop/CMakeLists.txt b/mwe/gameloop/CMakeLists.txt new file mode 100644 index 0000000..6bc5017 --- /dev/null +++ b/mwe/gameloop/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.5) +project(gameloop) + +# Set the C++ standard (optional, but good practice) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# Find the SDL2 package +find_package(SDL2 REQUIRED) + +# Add your executable +set(SOURCES + src/loopManager + src/window.cpp + src/main.cpp + src/eventManager.cpp + src/gameObject.cpp + src/timer.cpp +) + +add_executable(gameloop ${SOURCES}) + +# Link the SDL2 library to your project +target_link_libraries(gameloop ${SDL2_LIBRARIES}) + +# Include SDL2 header files and project headers +target_include_directories(gameloop PRIVATE ${SDL2_INCLUDE_DIRS}) +target_include_directories(gameloop PRIVATE ${CMAKE_SOURCE_DIR}/include) diff --git a/mwe/gameloop/imgs/demo.bmp b/mwe/gameloop/imgs/demo.bmp new file mode 100644 index 0000000..65354fe Binary files /dev/null and b/mwe/gameloop/imgs/demo.bmp differ diff --git a/mwe/gameloop/imgs/demo.jpg b/mwe/gameloop/imgs/demo.jpg new file mode 100644 index 0000000..f534e1b Binary files /dev/null and b/mwe/gameloop/imgs/demo.jpg differ diff --git a/mwe/gameloop/include/eventManager.h b/mwe/gameloop/include/eventManager.h new file mode 100644 index 0000000..2aa0a68 --- /dev/null +++ b/mwe/gameloop/include/eventManager.h @@ -0,0 +1,4 @@ +class EventManager +{ + +}; diff --git a/mwe/gameloop/include/gameObject.h b/mwe/gameloop/include/gameObject.h new file mode 100644 index 0000000..0e17991 --- /dev/null +++ b/mwe/gameloop/include/gameObject.h @@ -0,0 +1,29 @@ +#pragma once +#include +class GameObject { + public: + GameObject(); + GameObject(std::string name, float x, float y, float width, float height, float velX, float velY); + std::string getName() const; + float getX() const; + float getY() const; + float getWidth() const; + float getHeight() const; + float getVelX() const; + float getVelY() const; + void setName(std::string value); + void setX(float value); + void setY(float value); + void setWidth(float value); + void setHeight(float value); + void setVelX(float value); + void setVelY(float value); + private: + std::string name = ""; + float x = 0; + float y = 0; + float width = 0; + float height = 0; + float velX = 0; + float velY = 0; +}; diff --git a/mwe/gameloop/include/loopManager.h b/mwe/gameloop/include/loopManager.h new file mode 100644 index 0000000..06bcd5f --- /dev/null +++ b/mwe/gameloop/include/loopManager.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include "window.h" +#include "gameObject.h" +class LoopManager{ + public: + LoopManager(); + void setup(); + void loop(); + private: + + std::vector objectList; + void processInput(); + void update(); + void lateUpdate(); + void fixedUpdate(); + void render(); + bool gameRunning = false; + WindowManager window; + int timeScale = 1; + float accumulator = 0.0; + double currentTime; + double t = 0.0; + double dt = 0.01; +}; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h new file mode 100644 index 0000000..a245e5c --- /dev/null +++ b/mwe/gameloop/include/timer.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +class LoopTimer { +public: + static LoopTimer& getInstance(); + void start(); + void update(); + double getDeltaTime() const; + int getCurrentTime() const; + void advanceFixedUpdate(); + double getFixedDeltaTime() const; + void setFPS(int FPS); + int getFPS() const; + void enforceFrameRate(); + double getLag() const; +private: + LoopTimer(); + int FPS = 50; + double gameScale = 1; + double maximumDeltaTime = 0.25; + double deltaTime; + double frameTargetTime = FPS / 1000; + double fixedDeltaTime = 0.01; + double elapsedTime; + double elapsedFixedTime; + double time; + uint64_t lastFrameTime; +}; diff --git a/mwe/gameloop/include/window.h b/mwe/gameloop/include/window.h new file mode 100644 index 0000000..cfde0e9 --- /dev/null +++ b/mwe/gameloop/include/window.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include +#include "gameObject.h" +#include +class WindowManager{ + public: + WindowManager(); + virtual ~WindowManager(); + void render(std::vector objects); + bool initWindow(); + void destroyWindow(); + + SDL_Renderer* getRenderer(); + private: + const int SCREEN_WIDTH = 800; + const int SCREEN_HEIGHT = 600; + SDL_Window* window = NULL; + SDL_Renderer* renderer = NULL; +}; diff --git a/mwe/gameloop/src/eventManager.cpp b/mwe/gameloop/src/eventManager.cpp new file mode 100644 index 0000000..e69de29 diff --git a/mwe/gameloop/src/gameObject.cpp b/mwe/gameloop/src/gameObject.cpp new file mode 100644 index 0000000..f637314 --- /dev/null +++ b/mwe/gameloop/src/gameObject.cpp @@ -0,0 +1,58 @@ +#include "gameObject.h" +std::string GameObject::getName() const{ + return name; +} +float GameObject::getX() const{ + return x; +} + +float GameObject::getY() const{ + return y; +} + +float GameObject::getWidth() const{ + return width; +} + +float GameObject::getHeight() const{ + return height; +} + +float GameObject::getVelX() const{ + return velX; +} + +float GameObject::getVelY() const{ + return velY; +} +void GameObject::setName(std::string value){ + name = value; +} +void GameObject::setX(float value){ + x = value; +} + +void GameObject::setY(float value){ + y = value; +} + +void GameObject::setWidth(float value){ + width = value; +} + +void GameObject::setHeight(float value){ + height = value; +} + +void GameObject::setVelX(float value){ + velX = value; +} + +void GameObject::setVelY(float value){ + velY = value; +} + +GameObject::GameObject(std::string name, float x, float y, float width, float height, float velX, float velY) + : name(name),x(x), y(y), width(width), height(height), velX(velX), velY(velY) { + + } diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp new file mode 100644 index 0000000..f7ce886 --- /dev/null +++ b/mwe/gameloop/src/loopManager.cpp @@ -0,0 +1,72 @@ +#include "loopManager.h" +#include "timer.h" +LoopManager::LoopManager(){ +} +void LoopManager::processInput(){ + SDL_Event event; + SDL_PollEvent(&event); + switch(event.type){ + case SDL_QUIT: + gameRunning = false; + break; + case SDL_KEYDOWN: + if(event.key.keysym.sym == SDLK_ESCAPE){ + gameRunning = false; + } + break; + } +} +void LoopManager::fixedUpdate(){ + fprintf(stderr,"fixed update\n"); +} +void LoopManager::loop() { + LoopTimer& timer = LoopTimer::getInstance(); + timer.start(); + + while (gameRunning) { + timer.update(); + + while (timer.getLag() >= timer.getFixedDeltaTime()) { + processInput(); + fixedUpdate(); + timer.advanceFixedUpdate(); + } + + update(); + render(); + + timer.enforceFrameRate(); + } + + window.destroyWindow(); +} + +void LoopManager::setup(){ + gameRunning = window.initWindow(); + LoopTimer::getInstance().start(); + LoopTimer::getInstance().setFPS(50); + + for(int i = 0; i < 2;i++){ + GameObject* square = new GameObject("square2",i*40,i*40,20,20,0,0); + objectList.push_back(square); + } +} +void LoopManager::render(){ + fprintf(stderr,"**********render********** \n"); + if(gameRunning){ + window.render(objectList); + } +} + +void LoopManager::update() { + fprintf(stderr,"**********normal update********** \n"); + LoopTimer& timer = LoopTimer::getInstance(); + + float delta= timer.getDeltaTime(); + + for (int i = 0; i < objectList.size(); i++) { + objectList[i]->setX(objectList[i]->getX() + 50 * delta); + objectList[i]->setY(objectList[i]->getY() + 50 * delta); + } +} + diff --git a/mwe/gameloop/src/main.cpp b/mwe/gameloop/src/main.cpp new file mode 100644 index 0000000..2333e63 --- /dev/null +++ b/mwe/gameloop/src/main.cpp @@ -0,0 +1,19 @@ +//Using SDL and standard IO +#include +#include +//#include "window.h" +#include "loopManager.h" +#include "timer.h" +//Screen dimension constants + + +//Starts up SDL and creates window + +int main( int argc, char* args[] ) +{ + LoopManager gameLoop; + gameLoop.setup(); + gameLoop.loop(); + + return 0; +} diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp new file mode 100644 index 0000000..1e3045f --- /dev/null +++ b/mwe/gameloop/src/timer.cpp @@ -0,0 +1,68 @@ +#include "timer.h" + +// Constructor (private) +LoopTimer::LoopTimer(){} + +// Get the singleton instance of the timer +LoopTimer& LoopTimer::getInstance() { + static LoopTimer instance; + return instance; +} + +// Start the timer (initialize frame time) +void LoopTimer::start() { + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; +} + +// Update the timer, calculate deltaTime +void LoopTimer::update() { + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds + + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } + + elapsedTime += deltaTime; + lastFrameTime = currentFrameTime; +} + +double LoopTimer::getDeltaTime() const { + return deltaTime; +} +int LoopTimer::getCurrentTime() const { + return SDL_GetTicks(); +} + +void LoopTimer::advanceFixedUpdate() { + elapsedFixedTime += fixedDeltaTime; +} + +double LoopTimer::getFixedDeltaTime() const { + return fixedDeltaTime; +} + +void LoopTimer::setFPS(int FPS) { + this->FPS = FPS; + frameTargetTime = 1.0 / FPS; +} + +int LoopTimer::getFPS() const { + return FPS; +} + +void LoopTimer::enforceFrameRate() { + uint64_t currentFrameTime = SDL_GetTicks64(); + double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; + + if (frameDuration < frameTargetTime) { + uint32_t delayTime = (uint32_t)((frameTargetTime - frameDuration) * 1000.0); + SDL_Delay(delayTime); + } +} +double LoopTimer::getLag() const { + return elapsedTime - elapsedFixedTime; +} diff --git a/mwe/gameloop/src/window.cpp b/mwe/gameloop/src/window.cpp new file mode 100644 index 0000000..f998a79 --- /dev/null +++ b/mwe/gameloop/src/window.cpp @@ -0,0 +1,60 @@ +#include "window.h" +WindowManager::WindowManager(){ + if( !initWindow() ) + { + printf( "Failed to initialize!\n" ); + } +} +WindowManager::~WindowManager(){ + destroyWindow(); +} +SDL_Renderer* WindowManager::getRenderer(){ + return renderer; +} + +void WindowManager::render(std::vector objects){ + // Set the draw color to black and clear the screen + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderClear(renderer); + + // Print object position (optional for debugging) + //fprintf(stderr, "%d\n", objectList.size()); + for(int i = 0; i < objects.size();i++){ + //fprintf(stderr, "%f\n", objectList[i]->getX()); + // Create a rectangle representing the ball + SDL_Rect ball_rect = { + (int)objects[i]->getX(), + (int)objects[i]->getY(), + (int)objects[i]->getWidth(), + (int)objects[i]->getHeight(), + }; + // Set the draw color to white and render the ball + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderFillRect(renderer, &ball_rect); + } + + SDL_RenderPresent(renderer); +} + +bool WindowManager::initWindow(){ + if(SDL_Init(SDL_INIT_EVERYTHING) != 0){ + fprintf(stderr,"Error inititalising SDL.\n"); + return false; + } + window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); + if(!window){ + fprintf(stderr,"Error creating SDL Window. \n"); + return false; + } + renderer = SDL_CreateRenderer(window,-1,0); + if(!renderer){ + fprintf(stderr,"Error creating SDL renderer. \n"); + return false; + } + return true; +} +void WindowManager::destroyWindow(){ + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} diff --git a/mwe/gameloop/versions/delayBased.cpp b/mwe/gameloop/versions/delayBased.cpp new file mode 100644 index 0000000..a8cad15 --- /dev/null +++ b/mwe/gameloop/versions/delayBased.cpp @@ -0,0 +1,57 @@ +#include "loopManager.h" +#include "timer.h" +LoopManager::LoopManager(){ +} +void LoopManager::processInput(){ + SDL_Event event; + SDL_PollEvent(&event); + switch(event.type){ + case SDL_QUIT: + gameRunning = false; + break; + case SDL_KEYDOWN: + if(event.key.keysym.sym == SDLK_ESCAPE){ + gameRunning = false; + } + break; + } +} +void LoopManager::loop(){ + fprintf(stderr,"loop. \n"); + while(gameRunning){ + //Timer::getInstance().update(); + //deltaTime = Timer::getInstance().getDeltaTime(); + processInput(); + update(); + render(); + } + window.destroyWindow(); +} +void LoopManager::setup(){ + gameRunning = window.initWindow(); + LoopTimer::getInstance().start(); + LoopTimer::getInstance().setFPS(210); + + for(int i = 0; i < 2;i++){ + GameObject* square2 = new GameObject("square2",i*40,i*40,20,20,0,0); + objectList.push_back(square2); + } +} +void LoopManager::render(){ + if(gameRunning){ + window.render(objectList); + } +} + +void LoopManager::update() { + LoopTimer& timer = LoopTimer::getInstance(); + timer.enforceFrameRate(); + timer.update(); + float delta= timer.getDeltaTime(); + + for (int i = 0; i < objectList.size(); i++) { + objectList[i]->setX(objectList[i]->getX() + 50 * delta); + objectList[i]->setY(objectList[i]->getY() + 50 * delta); + } +} + -- cgit v1.2.3 From 44affb6635359ef2f619562800165ccfff438ae4 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 21 Sep 2024 15:35:05 +0200 Subject: `make format` --- mwe/dynlink/exec/main.c | 1 - mwe/dynlink/lib/lib.c | 5 +-- mwe/dynlink/lib/lib.h | 1 - mwe/gameloop/CMakeLists.txt | 12 +++-- mwe/gameloop/include/eventManager.h | 5 +-- mwe/gameloop/include/gameObject.h | 52 +++++++++++----------- mwe/gameloop/include/loopManager.h | 44 +++++++++--------- mwe/gameloop/include/timer.h | 29 ++++++------ mwe/gameloop/include/window.h | 33 +++++++------- mwe/gameloop/src/gameObject.cpp | 86 ++++++++++++------------------------ mwe/gameloop/src/loopManager.cpp | 75 +++++++++++++++---------------- mwe/gameloop/src/main.cpp | 4 +- mwe/gameloop/src/timer.cpp | 72 +++++++++++++----------------- mwe/gameloop/src/window.cpp | 67 ++++++++++++++-------------- mwe/gameloop/versions/delayBased.cpp | 49 ++++++++++---------- test/dummy.cpp | 5 +-- 16 files changed, 242 insertions(+), 298 deletions(-) diff --git a/mwe/dynlink/exec/main.c b/mwe/dynlink/exec/main.c index e3f458a..2bbdc20 100644 --- a/mwe/dynlink/exec/main.c +++ b/mwe/dynlink/exec/main.c @@ -5,4 +5,3 @@ int main() { return 0; } - diff --git a/mwe/dynlink/lib/lib.c b/mwe/dynlink/lib/lib.c index 930e403..c7a78e4 100644 --- a/mwe/dynlink/lib/lib.c +++ b/mwe/dynlink/lib/lib.c @@ -1,6 +1,3 @@ #include -void library_function() { - printf("%s\n", __PRETTY_FUNCTION__); -} - +void library_function() { printf("%s\n", __PRETTY_FUNCTION__); } diff --git a/mwe/dynlink/lib/lib.h b/mwe/dynlink/lib/lib.h index 068f07d..093eadb 100644 --- a/mwe/dynlink/lib/lib.h +++ b/mwe/dynlink/lib/lib.h @@ -1,4 +1,3 @@ #pragma once void library_function(); - diff --git a/mwe/gameloop/CMakeLists.txt b/mwe/gameloop/CMakeLists.txt index 6bc5017..233ba98 100644 --- a/mwe/gameloop/CMakeLists.txt +++ b/mwe/gameloop/CMakeLists.txt @@ -8,21 +8,19 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) # Find the SDL2 package find_package(SDL2 REQUIRED) -# Add your executable -set(SOURCES - src/loopManager - src/window.cpp - src/main.cpp +add_executable(gameloop + src/loopManager.cpp + src/window.cpp + src/main.cpp src/eventManager.cpp src/gameObject.cpp src/timer.cpp ) -add_executable(gameloop ${SOURCES}) - # Link the SDL2 library to your project target_link_libraries(gameloop ${SDL2_LIBRARIES}) # Include SDL2 header files and project headers target_include_directories(gameloop PRIVATE ${SDL2_INCLUDE_DIRS}) target_include_directories(gameloop PRIVATE ${CMAKE_SOURCE_DIR}/include) + diff --git a/mwe/gameloop/include/eventManager.h b/mwe/gameloop/include/eventManager.h index 2aa0a68..69c6801 100644 --- a/mwe/gameloop/include/eventManager.h +++ b/mwe/gameloop/include/eventManager.h @@ -1,4 +1 @@ -class EventManager -{ - -}; +class EventManager {}; diff --git a/mwe/gameloop/include/gameObject.h b/mwe/gameloop/include/gameObject.h index 0e17991..69f4d52 100644 --- a/mwe/gameloop/include/gameObject.h +++ b/mwe/gameloop/include/gameObject.h @@ -1,29 +1,31 @@ #pragma once #include class GameObject { - public: - GameObject(); - GameObject(std::string name, float x, float y, float width, float height, float velX, float velY); - std::string getName() const; - float getX() const; - float getY() const; - float getWidth() const; - float getHeight() const; - float getVelX() const; - float getVelY() const; - void setName(std::string value); - void setX(float value); - void setY(float value); - void setWidth(float value); - void setHeight(float value); - void setVelX(float value); - void setVelY(float value); - private: - std::string name = ""; - float x = 0; - float y = 0; - float width = 0; - float height = 0; - float velX = 0; - float velY = 0; +public: + GameObject(); + GameObject(std::string name, float x, float y, float width, float height, + float velX, float velY); + std::string getName() const; + float getX() const; + float getY() const; + float getWidth() const; + float getHeight() const; + float getVelX() const; + float getVelY() const; + void setName(std::string value); + void setX(float value); + void setY(float value); + void setWidth(float value); + void setHeight(float value); + void setVelX(float value); + void setVelY(float value); + +private: + std::string name = ""; + float x = 0; + float y = 0; + float width = 0; + float height = 0; + float velX = 0; + float velY = 0; }; diff --git a/mwe/gameloop/include/loopManager.h b/mwe/gameloop/include/loopManager.h index 06bcd5f..e202423 100644 --- a/mwe/gameloop/include/loopManager.h +++ b/mwe/gameloop/include/loopManager.h @@ -1,25 +1,25 @@ #pragma once -#include -#include "window.h" #include "gameObject.h" -class LoopManager{ - public: - LoopManager(); - void setup(); - void loop(); - private: - - std::vector objectList; - void processInput(); - void update(); - void lateUpdate(); - void fixedUpdate(); - void render(); - bool gameRunning = false; - WindowManager window; - int timeScale = 1; - float accumulator = 0.0; - double currentTime; - double t = 0.0; - double dt = 0.01; +#include "window.h" +#include +class LoopManager { +public: + LoopManager(); + void setup(); + void loop(); + +private: + std::vector objectList; + void processInput(); + void update(); + void lateUpdate(); + void fixedUpdate(); + void render(); + bool gameRunning = false; + WindowManager window; + int timeScale = 1; + float accumulator = 0.0; + double currentTime; + double t = 0.0; + double dt = 0.01; }; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h index a245e5c..22383b2 100644 --- a/mwe/gameloop/include/timer.h +++ b/mwe/gameloop/include/timer.h @@ -4,27 +4,28 @@ class LoopTimer { public: - static LoopTimer& getInstance(); - void start(); - void update(); - double getDeltaTime() const; - int getCurrentTime() const; + static LoopTimer & getInstance(); + void start(); + void update(); + double getDeltaTime() const; + int getCurrentTime() const; void advanceFixedUpdate(); double getFixedDeltaTime() const; - void setFPS(int FPS); - int getFPS() const; - void enforceFrameRate(); + void setFPS(int FPS); + int getFPS() const; + void enforceFrameRate(); double getLag() const; + private: - LoopTimer(); + LoopTimer(); int FPS = 50; double gameScale = 1; double maximumDeltaTime = 0.25; - double deltaTime; + double deltaTime; double frameTargetTime = FPS / 1000; - double fixedDeltaTime = 0.01; - double elapsedTime; - double elapsedFixedTime; + double fixedDeltaTime = 0.01; + double elapsedTime; + double elapsedFixedTime; double time; - uint64_t lastFrameTime; + uint64_t lastFrameTime; }; diff --git a/mwe/gameloop/include/window.h b/mwe/gameloop/include/window.h index cfde0e9..6806a26 100644 --- a/mwe/gameloop/include/window.h +++ b/mwe/gameloop/include/window.h @@ -1,20 +1,21 @@ #pragma once -#include -#include #include "gameObject.h" +#include #include -class WindowManager{ - public: - WindowManager(); - virtual ~WindowManager(); - void render(std::vector objects); - bool initWindow(); - void destroyWindow(); - - SDL_Renderer* getRenderer(); - private: - const int SCREEN_WIDTH = 800; - const int SCREEN_HEIGHT = 600; - SDL_Window* window = NULL; - SDL_Renderer* renderer = NULL; +#include +class WindowManager { +public: + WindowManager(); + virtual ~WindowManager(); + void render(std::vector objects); + bool initWindow(); + void destroyWindow(); + + SDL_Renderer * getRenderer(); + +private: + const int SCREEN_WIDTH = 800; + const int SCREEN_HEIGHT = 600; + SDL_Window * window = NULL; + SDL_Renderer * renderer = NULL; }; diff --git a/mwe/gameloop/src/gameObject.cpp b/mwe/gameloop/src/gameObject.cpp index f637314..78217c4 100644 --- a/mwe/gameloop/src/gameObject.cpp +++ b/mwe/gameloop/src/gameObject.cpp @@ -1,58 +1,30 @@ #include "gameObject.h" -std::string GameObject::getName() const{ - return name; -} -float GameObject::getX() const{ - return x; -} - -float GameObject::getY() const{ - return y; -} - -float GameObject::getWidth() const{ - return width; -} - -float GameObject::getHeight() const{ - return height; -} - -float GameObject::getVelX() const{ - return velX; -} - -float GameObject::getVelY() const{ - return velY; -} -void GameObject::setName(std::string value){ - name = value; -} -void GameObject::setX(float value){ - x = value; -} - -void GameObject::setY(float value){ - y = value; -} - -void GameObject::setWidth(float value){ - width = value; -} - -void GameObject::setHeight(float value){ - height = value; -} - -void GameObject::setVelX(float value){ - velX = value; -} - -void GameObject::setVelY(float value){ - velY = value; -} - -GameObject::GameObject(std::string name, float x, float y, float width, float height, float velX, float velY) - : name(name),x(x), y(y), width(width), height(height), velX(velX), velY(velY) { - - } +std::string GameObject::getName() const { return name; } +float GameObject::getX() const { return x; } + +float GameObject::getY() const { return y; } + +float GameObject::getWidth() const { return width; } + +float GameObject::getHeight() const { return height; } + +float GameObject::getVelX() const { return velX; } + +float GameObject::getVelY() const { return velY; } +void GameObject::setName(std::string value) { name = value; } +void GameObject::setX(float value) { x = value; } + +void GameObject::setY(float value) { y = value; } + +void GameObject::setWidth(float value) { width = value; } + +void GameObject::setHeight(float value) { height = value; } + +void GameObject::setVelX(float value) { velX = value; } + +void GameObject::setVelY(float value) { velY = value; } + +GameObject::GameObject(std::string name, float x, float y, float width, + float height, float velX, float velY) + : name(name), x(x), y(y), width(width), height(height), velX(velX), + velY(velY) {} diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp index f7ce886..cb532cc 100644 --- a/mwe/gameloop/src/loopManager.cpp +++ b/mwe/gameloop/src/loopManager.cpp @@ -1,72 +1,69 @@ #include "loopManager.h" #include "timer.h" -LoopManager::LoopManager(){ -} -void LoopManager::processInput(){ +LoopManager::LoopManager() {} +void LoopManager::processInput() { SDL_Event event; SDL_PollEvent(&event); - switch(event.type){ + switch (event.type) { case SDL_QUIT: gameRunning = false; break; case SDL_KEYDOWN: - if(event.key.keysym.sym == SDLK_ESCAPE){ + if (event.key.keysym.sym == SDLK_ESCAPE) { gameRunning = false; } - break; + break; } } -void LoopManager::fixedUpdate(){ - fprintf(stderr,"fixed update\n"); -} +void LoopManager::fixedUpdate() { fprintf(stderr, "fixed update\n"); } void LoopManager::loop() { - LoopTimer& timer = LoopTimer::getInstance(); - timer.start(); + LoopTimer & timer = LoopTimer::getInstance(); + timer.start(); - while (gameRunning) { - timer.update(); + while (gameRunning) { + timer.update(); - while (timer.getLag() >= timer.getFixedDeltaTime()) { - processInput(); - fixedUpdate(); - timer.advanceFixedUpdate(); - } + while (timer.getLag() >= timer.getFixedDeltaTime()) { + processInput(); + fixedUpdate(); + timer.advanceFixedUpdate(); + } - update(); - render(); + update(); + render(); - timer.enforceFrameRate(); - } + timer.enforceFrameRate(); + } - window.destroyWindow(); + window.destroyWindow(); } -void LoopManager::setup(){ +void LoopManager::setup() { gameRunning = window.initWindow(); LoopTimer::getInstance().start(); LoopTimer::getInstance().setFPS(50); - - for(int i = 0; i < 2;i++){ - GameObject* square = new GameObject("square2",i*40,i*40,20,20,0,0); + + for (int i = 0; i < 2; i++) { + GameObject * square + = new GameObject("square2", i * 40, i * 40, 20, 20, 0, 0); objectList.push_back(square); } } -void LoopManager::render(){ - fprintf(stderr,"**********render********** \n"); - if(gameRunning){ +void LoopManager::render() { + fprintf(stderr, "**********render********** \n"); + if (gameRunning) { window.render(objectList); } } void LoopManager::update() { - fprintf(stderr,"**********normal update********** \n"); - LoopTimer& timer = LoopTimer::getInstance(); - - float delta= timer.getDeltaTime(); + fprintf(stderr, "**********normal update********** \n"); + LoopTimer & timer = LoopTimer::getInstance(); - for (int i = 0; i < objectList.size(); i++) { - objectList[i]->setX(objectList[i]->getX() + 50 * delta); - objectList[i]->setY(objectList[i]->getY() + 50 * delta); - } -} + float delta = timer.getDeltaTime(); + for (int i = 0; i < objectList.size(); i++) { + objectList[i]->setX(objectList[i]->getX() + 50 * delta); + objectList[i]->setY(objectList[i]->getY() + 50 * delta); + } +} diff --git a/mwe/gameloop/src/main.cpp b/mwe/gameloop/src/main.cpp index 2333e63..889a30a 100644 --- a/mwe/gameloop/src/main.cpp +++ b/mwe/gameloop/src/main.cpp @@ -6,11 +6,9 @@ #include "timer.h" //Screen dimension constants - //Starts up SDL and creates window -int main( int argc, char* args[] ) -{ +int main(int argc, char * args[]) { LoopManager gameLoop; gameLoop.setup(); gameLoop.loop(); diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp index 1e3045f..61e144d 100644 --- a/mwe/gameloop/src/timer.cpp +++ b/mwe/gameloop/src/timer.cpp @@ -1,68 +1,58 @@ #include "timer.h" // Constructor (private) -LoopTimer::LoopTimer(){} +LoopTimer::LoopTimer() {} // Get the singleton instance of the timer -LoopTimer& LoopTimer::getInstance() { - static LoopTimer instance; - return instance; +LoopTimer & LoopTimer::getInstance() { + static LoopTimer instance; + return instance; } // Start the timer (initialize frame time) void LoopTimer::start() { - lastFrameTime = SDL_GetTicks64(); - elapsedTime = 0; - elapsedFixedTime = 0; - deltaTime = 0; + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; } // Update the timer, calculate deltaTime void LoopTimer::update() { - uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime + = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds - if (deltaTime > maximumDeltaTime) { - deltaTime = maximumDeltaTime; - } + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } - elapsedTime += deltaTime; - lastFrameTime = currentFrameTime; + elapsedTime += deltaTime; + lastFrameTime = currentFrameTime; } -double LoopTimer::getDeltaTime() const { - return deltaTime; -} -int LoopTimer::getCurrentTime() const { - return SDL_GetTicks(); -} +double LoopTimer::getDeltaTime() const { return deltaTime; } +int LoopTimer::getCurrentTime() const { return SDL_GetTicks(); } -void LoopTimer::advanceFixedUpdate() { - elapsedFixedTime += fixedDeltaTime; -} +void LoopTimer::advanceFixedUpdate() { elapsedFixedTime += fixedDeltaTime; } -double LoopTimer::getFixedDeltaTime() const { - return fixedDeltaTime; -} +double LoopTimer::getFixedDeltaTime() const { return fixedDeltaTime; } void LoopTimer::setFPS(int FPS) { - this->FPS = FPS; - frameTargetTime = 1.0 / FPS; + this->FPS = FPS; + frameTargetTime = 1.0 / FPS; } -int LoopTimer::getFPS() const { - return FPS; -} +int LoopTimer::getFPS() const { return FPS; } void LoopTimer::enforceFrameRate() { - uint64_t currentFrameTime = SDL_GetTicks64(); - double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; + uint64_t currentFrameTime = SDL_GetTicks64(); + double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; - if (frameDuration < frameTargetTime) { - uint32_t delayTime = (uint32_t)((frameTargetTime - frameDuration) * 1000.0); - SDL_Delay(delayTime); - } -} -double LoopTimer::getLag() const { - return elapsedTime - elapsedFixedTime; + if (frameDuration < frameTargetTime) { + uint32_t delayTime + = (uint32_t) ((frameTargetTime - frameDuration) * 1000.0); + SDL_Delay(delayTime); + } } +double LoopTimer::getLag() const { return elapsedTime - elapsedFixedTime; } diff --git a/mwe/gameloop/src/window.cpp b/mwe/gameloop/src/window.cpp index f998a79..dd1d98f 100644 --- a/mwe/gameloop/src/window.cpp +++ b/mwe/gameloop/src/window.cpp @@ -1,59 +1,56 @@ #include "window.h" -WindowManager::WindowManager(){ - if( !initWindow() ) - { - printf( "Failed to initialize!\n" ); +WindowManager::WindowManager() { + if (!initWindow()) { + printf("Failed to initialize!\n"); } } -WindowManager::~WindowManager(){ - destroyWindow(); -} -SDL_Renderer* WindowManager::getRenderer(){ - return renderer; -} +WindowManager::~WindowManager() { destroyWindow(); } +SDL_Renderer * WindowManager::getRenderer() { return renderer; } -void WindowManager::render(std::vector objects){ - // Set the draw color to black and clear the screen - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderClear(renderer); +void WindowManager::render(std::vector objects) { + // Set the draw color to black and clear the screen + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderClear(renderer); - // Print object position (optional for debugging) - //fprintf(stderr, "%d\n", objectList.size()); - for(int i = 0; i < objects.size();i++){ - //fprintf(stderr, "%f\n", objectList[i]->getX()); + // Print object position (optional for debugging) + //fprintf(stderr, "%d\n", objectList.size()); + for (int i = 0; i < objects.size(); i++) { + //fprintf(stderr, "%f\n", objectList[i]->getX()); // Create a rectangle representing the ball SDL_Rect ball_rect = { - (int)objects[i]->getX(), - (int)objects[i]->getY(), - (int)objects[i]->getWidth(), - (int)objects[i]->getHeight(), + (int) objects[i]->getX(), + (int) objects[i]->getY(), + (int) objects[i]->getWidth(), + (int) objects[i]->getHeight(), }; // Set the draw color to white and render the ball SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderFillRect(renderer, &ball_rect); } - - SDL_RenderPresent(renderer); + + SDL_RenderPresent(renderer); } -bool WindowManager::initWindow(){ - if(SDL_Init(SDL_INIT_EVERYTHING) != 0){ - fprintf(stderr,"Error inititalising SDL.\n"); +bool WindowManager::initWindow() { + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { + fprintf(stderr, "Error inititalising SDL.\n"); return false; - } - window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); - if(!window){ - fprintf(stderr,"Error creating SDL Window. \n"); + } + window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, + SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (!window) { + fprintf(stderr, "Error creating SDL Window. \n"); return false; } - renderer = SDL_CreateRenderer(window,-1,0); - if(!renderer){ - fprintf(stderr,"Error creating SDL renderer. \n"); + renderer = SDL_CreateRenderer(window, -1, 0); + if (!renderer) { + fprintf(stderr, "Error creating SDL renderer. \n"); return false; } return true; } -void WindowManager::destroyWindow(){ +void WindowManager::destroyWindow() { SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); diff --git a/mwe/gameloop/versions/delayBased.cpp b/mwe/gameloop/versions/delayBased.cpp index a8cad15..253a03a 100644 --- a/mwe/gameloop/versions/delayBased.cpp +++ b/mwe/gameloop/versions/delayBased.cpp @@ -1,57 +1,56 @@ #include "loopManager.h" #include "timer.h" -LoopManager::LoopManager(){ -} -void LoopManager::processInput(){ +LoopManager::LoopManager() {} +void LoopManager::processInput() { SDL_Event event; SDL_PollEvent(&event); - switch(event.type){ + switch (event.type) { case SDL_QUIT: gameRunning = false; break; case SDL_KEYDOWN: - if(event.key.keysym.sym == SDLK_ESCAPE){ + if (event.key.keysym.sym == SDLK_ESCAPE) { gameRunning = false; } - break; + break; } } -void LoopManager::loop(){ - fprintf(stderr,"loop. \n"); - while(gameRunning){ +void LoopManager::loop() { + fprintf(stderr, "loop. \n"); + while (gameRunning) { //Timer::getInstance().update(); - //deltaTime = Timer::getInstance().getDeltaTime(); + //deltaTime = Timer::getInstance().getDeltaTime(); processInput(); update(); render(); } window.destroyWindow(); } -void LoopManager::setup(){ +void LoopManager::setup() { gameRunning = window.initWindow(); LoopTimer::getInstance().start(); LoopTimer::getInstance().setFPS(210); - - for(int i = 0; i < 2;i++){ - GameObject* square2 = new GameObject("square2",i*40,i*40,20,20,0,0); + + for (int i = 0; i < 2; i++) { + GameObject * square2 + = new GameObject("square2", i * 40, i * 40, 20, 20, 0, 0); objectList.push_back(square2); } } -void LoopManager::render(){ - if(gameRunning){ +void LoopManager::render() { + if (gameRunning) { window.render(objectList); } } void LoopManager::update() { - LoopTimer& timer = LoopTimer::getInstance(); - timer.enforceFrameRate(); - timer.update(); - float delta= timer.getDeltaTime(); + LoopTimer & timer = LoopTimer::getInstance(); + timer.enforceFrameRate(); + timer.update(); + float delta = timer.getDeltaTime(); - for (int i = 0; i < objectList.size(); i++) { - objectList[i]->setX(objectList[i]->getX() + 50 * delta); - objectList[i]->setY(objectList[i]->getY() + 50 * delta); - } + for (int i = 0; i < objectList.size(); i++) { + objectList[i]->setX(objectList[i]->getX() + 50 * delta); + objectList[i]->setY(objectList[i]->getY() + 50 * delta); + } } - diff --git a/test/dummy.cpp b/test/dummy.cpp index 08850b2..a00a9c6 100644 --- a/test/dummy.cpp +++ b/test/dummy.cpp @@ -1,6 +1,3 @@ #include -TEST(dummy, foo) { - ASSERT_TRUE(1); -} - +TEST(dummy, foo) { ASSERT_TRUE(1); } -- cgit v1.2.3 From a12702c912ae0ce96c754ffcb2ab5d901c504b65 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 21 Sep 2024 15:43:14 +0200 Subject: remove lazy.mk and update readme --- contributing.md | 4 ++-- lazy.mk | 33 --------------------------------- makefile | 2 -- mwe/dynlink/exec/makefile | 1 - mwe/dynlink/lib/makefile | 1 - readme.md | 24 ++++++++++++++++++++++-- src/makefile | 2 -- test/makefile | 7 ------- 8 files changed, 24 insertions(+), 50 deletions(-) delete mode 100644 lazy.mk delete mode 100644 mwe/dynlink/exec/makefile delete mode 100644 mwe/dynlink/lib/makefile delete mode 100644 src/makefile delete mode 100644 test/makefile diff --git a/contributing.md b/contributing.md index 40e1edd..832667d 100644 --- a/contributing.md +++ b/contributing.md @@ -16,8 +16,9 @@ # Code style -- Formatting nitty-gritty is handled by clang-format/clang-tidy - ASCII only +- Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make + format` in the root folder of this repository to format all sources files) - When using libraries of which the header include order is important, make sure to separate the include statements using a blank line (clang-format may sort include statements, but does not sort across empty lines). @@ -32,7 +33,6 @@ # Documentation - All documentation is written in U.S. English -- TODO # Libraries diff --git a/lazy.mk b/lazy.mk deleted file mode 100644 index a591fd5..0000000 --- a/lazy.mk +++ /dev/null @@ -1,33 +0,0 @@ -# NOTE: CMAKE IS THE PRIMARY BUILD SYSTEM FOR THIS PROJECT. THIS FILE IS -# PROVIDED PURELY FOR CONVENIENCE, AND SHOULD NOT BECOME AN ESSENTIAL PART OF -# THE BUILD SYSTEM! - -BUILD_DIR ?= build -TARGET ?= $(BUILD_DIR)/main - -# always generate fresh build rules when cmake is re-run -CMFLAGS += --fresh -# make cmake shut up -CMFLAGS += --log-level WARNING -CMFLAGS += -Wno-deprecated - -.PHONY: FORCE - -all: FORCE $(TARGET) - -$(BUILD_DIR)/build.ninja: CMakeLists.txt - @mkdir -p $(BUILD_DIR) - @cmake -B $(BUILD_DIR) -G Ninja $(CMFLAGS) - -$(TARGET): $(BUILD_DIR)/build.ninja FORCE - @ninja -C $(BUILD_DIR) - -clean: FORCE - $(RM) -r $(BUILD_DIR) - -# Forward any unknown targets to Ninja -ifneq ($(MAKECMDGOALS),) -%:: - @ninja -C $(BUILD_DIR) $@ -endif - diff --git a/makefile b/makefile index 3792250..737e7a8 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,3 @@ -all: $(TARGET) # TARGET is defined in lazy.mk (build/main) - .PHONY: FORCE doxygen: Doxyfile FORCE diff --git a/mwe/dynlink/exec/makefile b/mwe/dynlink/exec/makefile deleted file mode 100644 index eec7c61..0000000 --- a/mwe/dynlink/exec/makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../../lazy.mk diff --git a/mwe/dynlink/lib/makefile b/mwe/dynlink/lib/makefile deleted file mode 100644 index eec7c61..0000000 --- a/mwe/dynlink/lib/makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../../lazy.mk diff --git a/readme.md b/readme.md index 4974694..f3aab09 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,26 @@ # crêpe -systems programming in c++ minor engine +This repository contains: -please read [contributing.md](./contributing.md) +|folder|content| +|-|-| +|`lib/`|third-party libraries as git submodules| +|`mwe/`|minimal working examples and proof-of-concepts| +|`src/crepe/`|game engine source code| +|`test/`|game engine unit tests| + +## Compilation + +This repository uses CMake (the makefile in the root of this repository is for +running auxiliary tasks only). Make sure you have initialized and updated the +submodules before compiling. + +## Documentation + +API documentation is done using Doxygen. To generate the docs, run `make +doxygen`. + +## Code style + +Please read [contributing.md](./contributing.md). diff --git a/src/makefile b/src/makefile deleted file mode 100644 index 8352615..0000000 --- a/src/makefile +++ /dev/null @@ -1,2 +0,0 @@ -include ../lazy.mk - diff --git a/test/makefile b/test/makefile deleted file mode 100644 index 7aeee34..0000000 --- a/test/makefile +++ /dev/null @@ -1,7 +0,0 @@ -TARGET = $(BUILD_DIR)/test - -include ../lazy.mk - -test: $(TARGET) FORCE - $(TARGET) - -- cgit v1.2.3 From 6a3e61197ed1f6ba235192cc1b5a8f1cd24f00c1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 22 Sep 2024 19:03:20 +0200 Subject: add soloud audio POC --- .gitmodules | 6 +++ lib/sdl2 | 1 + lib/soloud/CMakeLists.txt | 97 ++++++++++++++++++++++++++++++++++++++++ lib/soloud/src | 1 + mwe/audio/bgm.ogg | Bin 0 -> 125020 bytes mwe/audio/sfx1.wav | Bin 0 -> 44144 bytes mwe/audio/sfx2.wav | Bin 0 -> 44144 bytes mwe/audio/sfx3.wav | Bin 0 -> 44144 bytes mwe/audio/soloud/CMakeLists.txt | 19 ++++++++ mwe/audio/soloud/main.cpp | 48 ++++++++++++++++++++ 10 files changed, 172 insertions(+) create mode 160000 lib/sdl2 create mode 100644 lib/soloud/CMakeLists.txt create mode 160000 lib/soloud/src create mode 100644 mwe/audio/bgm.ogg create mode 100644 mwe/audio/sfx1.wav create mode 100644 mwe/audio/sfx2.wav create mode 100644 mwe/audio/sfx3.wav create mode 100644 mwe/audio/soloud/CMakeLists.txt create mode 100644 mwe/audio/soloud/main.cpp diff --git a/.gitmodules b/.gitmodules index f8a9861..ba367bb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,3 +3,9 @@ url = https://github.com/google/googletest branch = v1.15.x shallow = true +[submodule "lib/soloud/src"] + path = lib/soloud/src + url = https://github.com/jarikomppa/soloud +[submodule "lib/sdl2"] + path = lib/sdl2 + url = https://github.com/libsdl-org/SDL diff --git a/lib/sdl2 b/lib/sdl2 new file mode 160000 index 0000000..9519b99 --- /dev/null +++ b/lib/sdl2 @@ -0,0 +1 @@ +Subproject commit 9519b9916cd29a14587af0507292f2bd31dd5752 diff --git a/lib/soloud/CMakeLists.txt b/lib/soloud/CMakeLists.txt new file mode 100644 index 0000000..8433281 --- /dev/null +++ b/lib/soloud/CMakeLists.txt @@ -0,0 +1,97 @@ +cmake_minimum_required(VERSION 3.28) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) + +add_compile_definitions(WITH_SDL2) + +add_subdirectory(../sdl2 sdl2) + +project(soloud C CXX) + +add_library(soloud STATIC + src/src/audiosource/ay/chipplayer.cpp + src/src/audiosource/ay/sndbuffer.cpp + src/src/audiosource/ay/sndchip.cpp + src/src/audiosource/ay/sndrender.cpp + src/src/audiosource/ay/soloud_ay.cpp + src/src/audiosource/monotone/soloud_monotone.cpp + src/src/audiosource/noise/soloud_noise.cpp + src/src/audiosource/openmpt/soloud_openmpt.cpp + src/src/audiosource/openmpt/soloud_openmpt_dll.c + src/src/audiosource/sfxr/soloud_sfxr.cpp + src/src/audiosource/speech/darray.cpp + src/src/audiosource/speech/klatt.cpp + src/src/audiosource/speech/resonator.cpp + src/src/audiosource/speech/soloud_speech.cpp + src/src/audiosource/speech/tts.cpp + src/src/audiosource/tedsid/sid.cpp + src/src/audiosource/tedsid/soloud_tedsid.cpp + src/src/audiosource/tedsid/ted.cpp + src/src/audiosource/vic/soloud_vic.cpp + src/src/audiosource/vizsn/soloud_vizsn.cpp + src/src/audiosource/wav/dr_impl.cpp + src/src/audiosource/wav/soloud_wav.cpp + src/src/audiosource/wav/soloud_wavstream.cpp + src/src/audiosource/wav/stb_vorbis.c + + src/src/backend/alsa/soloud_alsa.cpp + src/src/backend/coreaudio/soloud_coreaudio.cpp + src/src/backend/jack/soloud_jack.cpp + src/src/backend/miniaudio/soloud_miniaudio.cpp + src/src/backend/nosound/soloud_nosound.cpp + src/src/backend/null/soloud_null.cpp + src/src/backend/openal/soloud_openal.cpp + src/src/backend/openal/soloud_openal_dll.c + src/src/backend/opensles/soloud_opensles.cpp + src/src/backend/oss/soloud_oss.cpp + src/src/backend/portaudio/soloud_portaudio.cpp + src/src/backend/portaudio/soloud_portaudio_dll.c + src/src/backend/sdl/soloud_sdl1.cpp + src/src/backend/sdl/soloud_sdl1_dll.c + src/src/backend/sdl/soloud_sdl2.cpp + src/src/backend/sdl/soloud_sdl2_dll.c + src/src/backend/sdl2_static/soloud_sdl2_static.cpp + src/src/backend/sdl_static/soloud_sdl_static.cpp + src/src/backend/wasapi/soloud_wasapi.cpp + src/src/backend/winmm/soloud_winmm.cpp + src/src/backend/xaudio2/soloud_xaudio2.cpp + + src/src/core/soloud.cpp + src/src/core/soloud_audiosource.cpp + src/src/core/soloud_bus.cpp + src/src/core/soloud_core_3d.cpp + src/src/core/soloud_core_basicops.cpp + src/src/core/soloud_core_faderops.cpp + src/src/core/soloud_core_filterops.cpp + src/src/core/soloud_core_getters.cpp + src/src/core/soloud_core_setters.cpp + src/src/core/soloud_core_voicegroup.cpp + src/src/core/soloud_core_voiceops.cpp + src/src/core/soloud_fader.cpp + src/src/core/soloud_fft.cpp + src/src/core/soloud_fft_lut.cpp + src/src/core/soloud_file.cpp + src/src/core/soloud_filter.cpp + src/src/core/soloud_misc.cpp + src/src/core/soloud_queue.cpp + src/src/core/soloud_thread.cpp + + # src/src/filter/soloud_bassboostfilter.cpp + # src/src/filter/soloud_biquadresonantfilter.cpp + # src/src/filter/soloud_dcremovalfilter.cpp + # src/src/filter/soloud_duckfilter.cpp + # src/src/filter/soloud_echofilter.cpp + # src/src/filter/soloud_eqfilter.cpp + # src/src/filter/soloud_fftfilter.cpp + # src/src/filter/soloud_flangerfilter.cpp + # src/src/filter/soloud_freeverbfilter.cpp + # src/src/filter/soloud_lofifilter.cpp + # src/src/filter/soloud_robotizefilter.cpp + # src/src/filter/soloud_waveshaperfilter.cpp +) +target_include_directories(soloud PRIVATE src/include) +target_include_directories(soloud SYSTEM INTERFACE src/include) + +target_link_libraries(soloud PRIVATE SDL2) + diff --git a/lib/soloud/src b/lib/soloud/src new file mode 160000 index 0000000..e82fd32 --- /dev/null +++ b/lib/soloud/src @@ -0,0 +1 @@ +Subproject commit e82fd32c1f62183922f08c14c814a02b58db1873 diff --git a/mwe/audio/bgm.ogg b/mwe/audio/bgm.ogg new file mode 100644 index 0000000..d6397fb Binary files /dev/null and b/mwe/audio/bgm.ogg differ diff --git a/mwe/audio/sfx1.wav b/mwe/audio/sfx1.wav new file mode 100644 index 0000000..576377a Binary files /dev/null and b/mwe/audio/sfx1.wav differ diff --git a/mwe/audio/sfx2.wav b/mwe/audio/sfx2.wav new file mode 100644 index 0000000..908ec86 Binary files /dev/null and b/mwe/audio/sfx2.wav differ diff --git a/mwe/audio/sfx3.wav b/mwe/audio/sfx3.wav new file mode 100644 index 0000000..20b7c84 Binary files /dev/null and b/mwe/audio/sfx3.wav differ diff --git a/mwe/audio/soloud/CMakeLists.txt b/mwe/audio/soloud/CMakeLists.txt new file mode 100644 index 0000000..f2df20a --- /dev/null +++ b/mwe/audio/soloud/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.28) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) +set(CMAKE_BUILD_TYPE Debug) + +add_subdirectory(../../../lib/soloud soloud) + +project(poc C CXX) + +add_executable(main + main.cpp +) + +target_link_libraries(main + soloud +) + diff --git a/mwe/audio/soloud/main.cpp b/mwe/audio/soloud/main.cpp new file mode 100644 index 0000000..25ba003 --- /dev/null +++ b/mwe/audio/soloud/main.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +#include +#include + +using namespace std; +using namespace std::chrono_literals; + +int main() { + SoLoud::Soloud soloud; + soloud.init(); + + // load background track + SoLoud::WavStream bgm; + bgm.load("../bgm.ogg"); + // NOTE: SoLoud::Wav is poorly named as it also supports FLAC, MP3 and OGG + + // load three short samples + SoLoud::Wav sfx[3]; + sfx[0].load("../sfx1.wav"); + sfx[1].load("../sfx2.wav"); + sfx[2].load("../sfx3.wav"); + + // start the background track + SoLoud::handle bgm_handle = soloud.play(bgm); + + // play each sample sequentially + this_thread::sleep_for(500ms); + soloud.play(sfx[0]); + this_thread::sleep_for(500ms); + soloud.play(sfx[1]); + soloud.setPause(bgm_handle, true); + this_thread::sleep_for(500ms); + soloud.play(sfx[2]); + soloud.setPause(bgm_handle, false); + this_thread::sleep_for(500ms); + + // play all samples simultaniously + for (unsigned i = 0; i < 3; i++) + soloud.play(sfx[i]); + this_thread::sleep_for(1000ms); + + // stop all audio and exit + soloud.deinit(); + return 0; +} -- cgit v1.2.3 From f4560e02f703f1c6f857c8e5af63fa9fc4ca6438 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 22 Sep 2024 19:31:03 +0200 Subject: update gitmodules --- .gitmodules | 3 ++- contributing.md | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitmodules b/.gitmodules index ba367bb..bb860c0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,11 +1,12 @@ [submodule "lib/googletest"] path = lib/googletest url = https://github.com/google/googletest - branch = v1.15.x shallow = true [submodule "lib/soloud/src"] path = lib/soloud/src url = https://github.com/jarikomppa/soloud + shallow = true [submodule "lib/sdl2"] path = lib/sdl2 url = https://github.com/libsdl-org/SDL + shallow = true diff --git a/contributing.md b/contributing.md index 832667d..eec5bc0 100644 --- a/contributing.md +++ b/contributing.md @@ -38,6 +38,6 @@ - External libraries should be included as Git submodules under the `lib/` subdirectory -- When adding new submodules, make sure to manually set the `branch` and - `shallow` options in the [.gitmodules](./.gitmodules) file +- When adding new submodules, please set the `shallow` option to `true` in the + [.gitmodules](./.gitmodules) file -- cgit v1.2.3