From 0dd5f62c07b55e97f45aebc852f4ad038979c2a0 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Fri, 28 Feb 2025 15:16:06 +0100 Subject: [PATCH] Initial commit :) --- .gitmodules | 9 +++ CMakeLists.txt | 21 ++++++ hello.c | 165 +++++++++++++++++++++++++++++++++++++++++++ submodules/SDL | 1 + submodules/SDL_ttf | 1 + submodules/utilitiec | 1 + 6 files changed, 198 insertions(+) create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 hello.c create mode 160000 submodules/SDL create mode 160000 submodules/SDL_ttf create mode 160000 submodules/utilitiec diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c605a7b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "submodules/SDL_ttf"] + path = submodules/SDL_ttf + url = https://github.com/libsdl-org/SDL_ttf.git +[submodule "submodules/SDL"] + path = submodules/SDL + url = https://github.com/libsdl-org/SDL.git +[submodule "submodules/utilitiec"] + path = submodules/utilitiec + url = https://git.jossco.de/VegOwOtenks/utilitiec.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b6c26b6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.16) +project(hello) + +# set the output directory for built objects. +# This makes sure that the dynamic library goes into the build directory automatically. +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$") + +# This assumes the SDL source is available in vendored/SDL +add_subdirectory(submodules/SDL EXCLUDE_FROM_ALL) + +# This assumes the SDL_ttf source is available in vendored/SDL_ttf +add_subdirectory(submodules/SDL_ttf EXCLUDE_FROM_ALL) + +add_subdirectory(submodules/utilitiec EXCLUDE_FROM_ALL) + +# Create your game executable target as usual +add_executable(hello WIN32 hello.c) + +# Link to the actual SDL3 library. +target_link_libraries(hello PRIVATE SDL3_ttf::SDL3_ttf SDL3::SDL3 QuadTree allocator-interface rand m) diff --git a/hello.c b/hello.c new file mode 100644 index 0000000..7929ad7 --- /dev/null +++ b/hello.c @@ -0,0 +1,165 @@ +/* + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely. +*/ +#include +#include +#include +#include +#include +#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ +#include +#include +#include + +#include "submodules/utilitiec/src/QuadTree/QuadTree.h" +#include "submodules/utilitiec/src/rand/xoshiro256.h" + +static SDL_Window *window = NULL; +static SDL_Renderer *renderer = NULL; +static QuadTree tree; + +typedef struct Particle_s { + QuadTreeLeaf position; + double velocity; + double direction; +} Particle; + +static Particle particles[1000]; + +scalar QuadTreeLeaf_norm(QuadTreeLeaf* vector) +{ + return sqrt(pow(vector->x, 2) + pow(vector->y, 2)); +} + +scalar Particle_AngleBetweenDirectionAndOtherParticle(Particle* this, Particle* other) +{ + QuadTreeLeaf vectorFromThisToOther = {other->position.x - this->position.x, other->position.y - this->position.y}; + QuadTreeLeaf thisDirectionAsVector = {cos(this->direction), sin(this->direction)}; + scalar normProduct = QuadTreeLeaf_norm(&vectorFromThisToOther) * QuadTreeLeaf_norm(&thisDirectionAsVector); + if (normProduct == 0) return 0; + return (vectorFromThisToOther.x * thisDirectionAsVector.x + vectorFromThisToOther.y * thisDirectionAsVector.y) / normProduct; +} + +/* This function runs once at startup. */ +SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) +{ + SDL_Color color = { 255, 255, 255, SDL_ALPHA_OPAQUE }; + SDL_Surface *text; + + /* Create the window */ + if (!SDL_CreateWindowAndRenderer("Swarm Simulation", 600, 600, 0, &window, &renderer)) { + SDL_Log("Couldn't create window and renderer: %s\n", SDL_GetError()); + return SDL_APP_FAILURE; + } + + if (!TTF_Init()) { + SDL_Log("Couldn't initialise SDL_ttf: %s\n", SDL_GetError()); + return SDL_APP_FAILURE; + } + + QuadTree_Create(&tree, 600, 600, NULL); + + Xoshiro256State rand_state = { 0, 10, 10, 0 }; + + for (size_t i = 0; i < sizeof(particles) / sizeof(particles[0]); i++) { + particles[i].position.x = xoshiro256_next(&rand_state) % 600; + particles[i].position.y = xoshiro256_next(&rand_state) % 600; + particles[i].velocity = xoshiro256_next(&rand_state) % 7; + particles[i].direction = (xoshiro256_next(&rand_state) % 1000) / 999.0 * acos(0); + QuadTree_Insert(&tree, &particles[i].position); + } + + return SDL_APP_CONTINUE; +} + +/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ +SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) +{ + if (event->type == SDL_EVENT_KEY_DOWN || + event->type == SDL_EVENT_QUIT) { + return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ + } + return SDL_APP_CONTINUE; +} + +int QuadTree_DrawTree(void* context, QuadSubTree* tree, QuadTreeDimension dimension) +{ + SDL_FRect render_rect; + render_rect.x = dimension.left; + render_rect.y = dimension.top; + render_rect.h = dimension.bottom - dimension.top; + render_rect.w = dimension.right - dimension.left; + SDL_RenderRect(renderer, &render_rect); + + return EXIT_SUCCESS; +} + +void Particle_Move(Particle* self) +{ + self->position.x += 1/60.0 * self->velocity * cos(self->direction); + self->position.y += 1/60.0 * self->velocity * sin(self->direction); + + self->position.x = self->position.x > 600 ? self->position.x - 600 : self->position.x; + self->position.y = self->position.y > 600 ? self->position.y - 600 : self->position.y; +} + +void Particle_turnTo(Particle* self, Particle* target) +{ + scalar angle = Particle_AngleBetweenDirectionAndOtherParticle(self, target); + self->direction += angle; + self->direction /= 2; +} + +/* This function runs once per frame, and is the heart of the program. */ +SDL_AppResult SDL_AppIterate(void *appstate) +{ + // clear screen + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + QuadTree new_tree; + QuadTree_Create(&new_tree, 600, 600, tree.allocator); + + + + + for (size_t i = 0; i < sizeof(particles) / sizeof(particles[0]); i++) { + Particle_turnTo(particles + i, particles + 0); + + Particle_Move(particles + i); + QuadTree_Insert(&new_tree, &particles[i].position); + SDL_RenderPoint(renderer, particles[i].position.x, particles[i].position.y); + } + +SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + + SDL_RenderPoint(renderer, particles[0].position.x, particles[0].position.y); + + QuadTree_Destroy(&tree); + tree = new_tree; + //QuadTree_ForEachTree(&tree, QuadTree_DrawTree, NULL); + + // swap + SDL_RenderPresent(renderer); + + return SDL_APP_CONTINUE; +} + +/* This function runs once at shutdown. */ +void SDL_AppQuit(void *appstate, SDL_AppResult result) +{ + QuadTree_Destroy(&tree); + TTF_Quit(); + SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); +} diff --git a/submodules/SDL b/submodules/SDL new file mode 160000 index 0000000..e012573 --- /dev/null +++ b/submodules/SDL @@ -0,0 +1 @@ +Subproject commit e01257376659f75562108e4948e13135fc3108e6 diff --git a/submodules/SDL_ttf b/submodules/SDL_ttf new file mode 160000 index 0000000..d1e85b2 --- /dev/null +++ b/submodules/SDL_ttf @@ -0,0 +1 @@ +Subproject commit d1e85b29c62c72e9de27a91c9c02ef39f7ca5ab3 diff --git a/submodules/utilitiec b/submodules/utilitiec new file mode 160000 index 0000000..a569dd0 --- /dev/null +++ b/submodules/utilitiec @@ -0,0 +1 @@ +Subproject commit a569dd05a6470b4a98fbf3a0218d2d13820d16fa