Initial commit :)
This commit is contained in:
commit
0dd5f62c07
6 changed files with 198 additions and 0 deletions
9
.gitmodules
vendored
Normal file
9
.gitmodules
vendored
Normal file
|
@ -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
|
21
CMakeLists.txt
Normal file
21
CMakeLists.txt
Normal file
|
@ -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}/$<CONFIGURATION>")
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
|
||||
|
||||
# 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)
|
165
hello.c
Normal file
165
hello.c
Normal file
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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 <SDL3/SDL_rect.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <SDL3/SDL_surface.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#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);
|
||||
}
|
1
submodules/SDL
Submodule
1
submodules/SDL
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit e01257376659f75562108e4948e13135fc3108e6
|
1
submodules/SDL_ttf
Submodule
1
submodules/SDL_ttf
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit d1e85b29c62c72e9de27a91c9c02ef39f7ca5ab3
|
1
submodules/utilitiec
Submodule
1
submodules/utilitiec
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit a569dd05a6470b4a98fbf3a0218d2d13820d16fa
|
Loading…
Reference in a new issue