diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-27 14:00:57 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-27 14:00:57 +0200 |
commit | a78f7bbfcdabad9550afe22f615b973b92cb074f (patch) | |
tree | 318e888219965634e3b81be9b74a0522af2136e0 /mwe | |
parent | 86a7bd1aee2ee96079e00e2696a624fd6b642fa0 (diff) | |
parent | f4560e02f703f1c6f857c8e5af63fa9fc4ca6438 (diff) |
fixed merge conflict
Diffstat (limited to 'mwe')
26 files changed, 521 insertions, 8 deletions
diff --git a/mwe/audio/bgm.ogg b/mwe/audio/bgm.ogg Binary files differnew file mode 100644 index 0000000..d6397fb --- /dev/null +++ b/mwe/audio/bgm.ogg diff --git a/mwe/audio/sfx1.wav b/mwe/audio/sfx1.wav Binary files differnew file mode 100644 index 0000000..576377a --- /dev/null +++ b/mwe/audio/sfx1.wav diff --git a/mwe/audio/sfx2.wav b/mwe/audio/sfx2.wav Binary files differnew file mode 100644 index 0000000..908ec86 --- /dev/null +++ b/mwe/audio/sfx2.wav diff --git a/mwe/audio/sfx3.wav b/mwe/audio/sfx3.wav Binary files differnew file mode 100644 index 0000000..20b7c84 --- /dev/null +++ b/mwe/audio/sfx3.wav 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 <soloud.h> +#include <soloud_wav.h> +#include <soloud_wavstream.h> + +#include <chrono> +#include <thread> + +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; +} 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/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/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 <stdio.h> -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/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/mwe/gameloop/CMakeLists.txt b/mwe/gameloop/CMakeLists.txt new file mode 100644 index 0000000..233ba98 --- /dev/null +++ b/mwe/gameloop/CMakeLists.txt @@ -0,0 +1,26 @@ +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_executable(gameloop + src/loopManager.cpp + src/window.cpp + src/main.cpp + src/eventManager.cpp + src/gameObject.cpp + src/timer.cpp +) + +# 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 Binary files differnew file mode 100644 index 0000000..65354fe --- /dev/null +++ b/mwe/gameloop/imgs/demo.bmp diff --git a/mwe/gameloop/imgs/demo.jpg b/mwe/gameloop/imgs/demo.jpg Binary files differnew file mode 100644 index 0000000..f534e1b --- /dev/null +++ b/mwe/gameloop/imgs/demo.jpg diff --git a/mwe/gameloop/include/eventManager.h b/mwe/gameloop/include/eventManager.h new file mode 100644 index 0000000..69c6801 --- /dev/null +++ b/mwe/gameloop/include/eventManager.h @@ -0,0 +1 @@ +class EventManager {}; diff --git a/mwe/gameloop/include/gameObject.h b/mwe/gameloop/include/gameObject.h new file mode 100644 index 0000000..fc7d026 --- /dev/null +++ b/mwe/gameloop/include/gameObject.h @@ -0,0 +1,30 @@ +#pragma once +#include <iostream> +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); + int direction; + 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..e202423 --- /dev/null +++ b/mwe/gameloop/include/loopManager.h @@ -0,0 +1,25 @@ +#pragma once +#include "gameObject.h" +#include "window.h" +#include <SDL2/SDL.h> +class LoopManager { +public: + LoopManager(); + void setup(); + void loop(); + +private: + std::vector<GameObject *> 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..ca1e5f3 --- /dev/null +++ b/mwe/gameloop/include/timer.h @@ -0,0 +1,32 @@ +#pragma once + +#include <SDL2/SDL.h> + +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; + double getGameScale(); + void setGameScale(double); + 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..6806a26 --- /dev/null +++ b/mwe/gameloop/include/window.h @@ -0,0 +1,21 @@ +#pragma once +#include "gameObject.h" +#include <SDL2/SDL.h> +#include <iostream> +#include <vector> +class WindowManager { +public: + WindowManager(); + virtual ~WindowManager(); + void render(std::vector<GameObject *> 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 --- /dev/null +++ b/mwe/gameloop/src/eventManager.cpp diff --git a/mwe/gameloop/src/gameObject.cpp b/mwe/gameloop/src/gameObject.cpp new file mode 100644 index 0000000..78217c4 --- /dev/null +++ b/mwe/gameloop/src/gameObject.cpp @@ -0,0 +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) {} diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp new file mode 100644 index 0000000..dde3cef --- /dev/null +++ b/mwe/gameloop/src/loopManager.cpp @@ -0,0 +1,92 @@ +#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; + }else if(event.key.keysym.sym == SDLK_i){ + LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() + 0.1); + }else if(event.key.keysym.sym == SDLK_k){ + LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() - 0.1); + } + + 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(500); + + for(int i = 1; i < 3;i++){ + GameObject* square = new GameObject("square2",i*60,i*60,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++) { + GameObject* obj = objectList[i]; + + // Move the object based on its direction + if (obj->direction == 1) { + obj->setX(obj->getX() + 50 * delta); + } else { + obj->setX(obj->getX() - 50 * delta); + } + + + if (obj->getX() > 500) { + obj->setX(500); + obj->direction = 0; // Switch direction to left + } else if (obj->getX() < 50) { + obj->setX(50); // Clamp the position to the boundary + obj->direction = 1; // Switch direction to right + } + } +} + + diff --git a/mwe/gameloop/src/main.cpp b/mwe/gameloop/src/main.cpp new file mode 100644 index 0000000..9f407c4 --- /dev/null +++ b/mwe/gameloop/src/main.cpp @@ -0,0 +1,15 @@ +//Using SDL and standard IO +#include <SDL2/SDL.h> +#include <stdio.h> +//#include "window.h" +#include "loopManager.h" +#include "timer.h" + + +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..03fb9eb --- /dev/null +++ b/mwe/gameloop/src/timer.cpp @@ -0,0 +1,68 @@ +#include "timer.h" +LoopTimer::LoopTimer(){} + +LoopTimer& LoopTimer::getInstance() { + static LoopTimer instance; + return instance; +} + +void LoopTimer::start() { + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; +} + +void LoopTimer::update() { + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; + + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } + deltaTime *= gameScale; + 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::setGameScale(double value){ + gameScale = value; +}; +double LoopTimer::getGameScale(){ + return gameScale; +} +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..dd1d98f --- /dev/null +++ b/mwe/gameloop/src/window.cpp @@ -0,0 +1,57 @@ +#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<GameObject *> 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..253a03a --- /dev/null +++ b/mwe/gameloop/versions/delayBased.cpp @@ -0,0 +1,56 @@ +#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); + } +} |