From 82f4a3b8acfd5677762fd8cb00922fd32c102fef Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Sun, 8 Feb 2026 04:07:36 -0600 Subject: [PATCH] a janky init --- .gitignore | 3 ++ LICENSE | 21 ++++++++++++++ README.md | 3 ++ lib/global_objects.c | 55 +++++++++++++++++++++++++++++++++++++ lib/init.c | 61 +++++++++++++++++++++++++++++++++++++++++ lib/init.h | 13 +++++++++ make | 28 +++++++++++++++++++ src/1-1-walker.c | 55 +++++++++++++++++++++++++++++++++++++ src/1-2-vec-math.c | 56 ++++++++++++++++++++++++++++++++++++++ src/1-3-rand-vec.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 360 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 lib/global_objects.c create mode 100755 lib/init.c create mode 100755 lib/init.h create mode 100755 make create mode 100755 src/1-1-walker.c create mode 100755 src/1-2-vec-math.c create mode 100755 src/1-3-rand-vec.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6685dd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin/** +**/.clangd +.clangd diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e4e5954 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 iceyrazor + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8658cb3 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# The Nature Of Code Vectors + +Me following [The Nature Of Code](https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZV4yEcW3uDwOgGXKUUsPOM) vectors 1.1 with SDL2 diff --git a/lib/global_objects.c b/lib/global_objects.c new file mode 100644 index 0000000..e6f8de5 --- /dev/null +++ b/lib/global_objects.c @@ -0,0 +1,55 @@ +#ifndef global_objects +#define global_objects + +#include + +#define flaot float + +typedef struct { + float x; + float y; +} Point; + +void point_add(Point *A, Point *B){ + A->x = A->x + B->x; + A->y = A->y + B->y; +} + +void point_sub(Point *A, Point *B){ + A->x = A->x - B->x; + A->y = A->y - B->y; +} + +void point_mul(Point *A, Point *B){ + A->x = A->x * B->x; + A->y = A->y * B->y; +} + +/* +fn set_mag(x: &f64,y: &f64, mag: &f64) -> Vec{ + let getmag: f64 = magnitude(*x, *y); + let rx = (*x / getmag) * mag; + let ry = (*y / getmag) * mag; + + return vec![rx,ry] +} + +fn magnitude(x: f64,y: f64) -> f64 { + ((x).powi(2) + (y).powi(2)).sqrt() +} +*/ + +float magnitude(flaot x, flaot y) { + return sqrt((pow(x,2) + pow(y,2))); +} + +void point_set_mag(Point *point, float mag){ + flaot getmag = magnitude(point->x, point->y); + flaot rx = (point->x / getmag) * mag; + flaot ry = (point->y / getmag) * mag; + + point->x = rx; + point->y = ry; +} + +#endif diff --git a/lib/init.c b/lib/init.c new file mode 100755 index 0000000..6c4f314 --- /dev/null +++ b/lib/init.c @@ -0,0 +1,61 @@ +#include +#include +#include "init.h" + +#include +#include + +int main(int argc, char *argv[]) +{ + if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) + SDL_Log("SDL fails to initialize! %s\n", SDL_GetError()); + + int startw=400; + int starth=400; + + SDL_Window *window = SDL_CreateWindow(PROJECT_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, startw, starth, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); + if (!window) { + printf("Failed to create window: %s\n", SDL_GetError()); + } + SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_SOFTWARE); + if (!renderer) { + printf("Failed to create renderer: %s\n", SDL_GetError()); + } + + + OBJECT obj = init_stuffs(startw,starth); + + STUFFS stuff = { + startw, + starth, + false, + &obj, + }; + + bool quit = false; + SDL_Event e; + while (!quit) { + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + quit = true; + } + else if(e.type == SDL_MOUSEBUTTONDOWN){ + if(e.button.button==SDL_BUTTON_LEFT){ + stuff.mousedown=true; + } else if(e.button.button==SDL_BUTTON_RIGHT){ + } + } else if(e.type==SDL_MOUSEBUTTONUP){ + stuff.mousedown=false; + } + } + draw(renderer,window,&stuff); + SDL_Delay(30); + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_QuitSubSystem(SDL_INIT_VIDEO); + SDL_Quit(); + + return 0; +} diff --git a/lib/init.h b/lib/init.h new file mode 100755 index 0000000..e957476 --- /dev/null +++ b/lib/init.h @@ -0,0 +1,13 @@ +#ifndef inithell +#define inithell + +#include +#include + +typedef struct{ + int width; + int height; + bool mousedown; + OBJECT *obj; +} STUFFS; +#endif diff --git a/make b/make new file mode 100755 index 0000000..72f08f9 --- /dev/null +++ b/make @@ -0,0 +1,28 @@ +#!/bin/sh + +DEBUG="" +LIB="-Ilib/" + +while getopts "d" opt; do + case "$opt" in + d) DEBUG="-Wall -fsanitize=address -g" + ;; + esac +done + +shift $((OPTIND-1)) +[ "${1:-}" = "--" ] && shift + +echo making shit +if [[ "$1" ]]; then + file="src/$1" + clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG +else + for file in src/*; do + if [ -f "$file" ]; then + if [[ "$file" != ".clangd" ]]; then + clang -o bin/$(basename $file | sed 's/.c$//') $file $LIB `sdl2-config --cflags --libs` -lm $DEBUG + fi + fi + done +fi diff --git a/src/1-1-walker.c b/src/1-1-walker.c new file mode 100755 index 0000000..5a905f0 --- /dev/null +++ b/src/1-1-walker.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#include +#include + +#define PROJECT_NAME "vecotrs-main" + +typedef struct { + float x; + float y; +} OBJECT; + +#include + +void constructor(int w, int h, OBJECT *obj){ + obj->x = (float)w/2; + obj->y = (float)h/2; +} + +OBJECT init_stuffs(int w, int h){ + OBJECT obj; + constructor(w,h,&obj); + + return obj; +} + +void update(OBJECT *obj){ + obj->x = obj->x + (rand() % 2 - 0.5); + obj->y = obj->y + (rand() % 2 - 0.5); +} + +void show(SDL_Renderer* renderer,OBJECT *obj){ + SDL_SetRenderDrawColor(renderer,150,50,255,255); + SDL_RenderDrawPoint(renderer,obj->x,obj->y); +} + +void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ + //SDL_SetRenderDrawColor(renderer,0,0,0,255); + //SDL_RenderClear(renderer); + + update(stuff->obj); + show(renderer,stuff->obj); + + SDL_RenderPresent(renderer); + //SDL_UpdateWindowSurface(window); +} + +void mousePressed(STUFFS *stuff){ + +} + +#include diff --git a/src/1-2-vec-math.c b/src/1-2-vec-math.c new file mode 100755 index 0000000..d6f38b1 --- /dev/null +++ b/src/1-2-vec-math.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include "global_objects.c" + +#include +#include + +#define PROJECT_NAME "vecotrs-main" + +typedef struct { + Point pos; + Point vel; +} OBJECT; + +#include + +void constructor(int w, int h, OBJECT *obj){ + obj->pos.x = (float)w/2; + obj->pos.y = (float)h/2; + obj->vel.x = 1; + obj->vel.y = -1; +} + +OBJECT init_stuffs(int w, int h){ + OBJECT obj; + constructor(w,h,&obj); + + return obj; +} + +void update(OBJECT *obj){ + point_add(&obj->pos, &obj->vel); +} + +void show(SDL_Renderer* renderer,OBJECT *obj){ + SDL_SetRenderDrawColor(renderer,150,50,255,255); + SDL_RenderFillRect(renderer,&(SDL_Rect){.x = obj->pos.x - 5, .y=obj->pos.y - 5, .w = 10, .h = 10}); +} + +void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ + SDL_SetRenderDrawColor(renderer,0,0,0,255); + SDL_RenderClear(renderer); + + update(stuff->obj); + show(renderer,stuff->obj); + + SDL_RenderPresent(renderer); +} + +void mousePressed(STUFFS *stuff){ + +} + +#include diff --git a/src/1-3-rand-vec.c b/src/1-3-rand-vec.c new file mode 100755 index 0000000..9c5f44f --- /dev/null +++ b/src/1-3-rand-vec.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include "global_objects.c" + +#include +#include + +#define PROJECT_NAME "vecotrs-main" + +typedef struct { + Point pos; +} OBJECT; + +#include + +float RandomFloat(float min, float max){ + return ((max - min) * ((float)rand() / RAND_MAX)) + min; +} + +void constructor(int w, int h, OBJECT *obj){ + obj->pos.x = (float)w/2; + obj->pos.y = (float)h/2; +} + +OBJECT init_stuffs(int w, int h){ + OBJECT obj; + constructor(w,h,&obj); + + return obj; +} + +void update(STUFFS *stuff){ + Point rand_point = {RandomFloat(-1, 1),RandomFloat(-1, 1)}; + point_set_mag(&rand_point, 1); + + float random_mul = RandomFloat(50, 100); + point_mul(&rand_point,&(Point){.x = random_mul, .y = random_mul}); + + stuff->obj->pos.x = rand_point.x + (float)stuff->width / 2;; + stuff->obj->pos.y = rand_point.y + (float)stuff->height / 2; +} + +void show(SDL_Renderer* renderer,STUFFS *stuff){ + SDL_SetRenderDrawColor(renderer,150,50,255,255); + //SDL_RenderFillRect(renderer,&(SDL_Rect){.x = obj->pos.x - 5, .y=obj->pos.y - 5, .w = 10, .h = 10}); + SDL_RenderDrawLine(renderer,stuff->width/2,stuff->height/2,stuff->obj->pos.x,stuff->obj->pos.y); +} + +void draw(SDL_Renderer* renderer,SDL_Window* window, STUFFS *stuff){ + //SDL_SetRenderDrawColor(renderer,0,0,0,255); + //SDL_RenderClear(renderer); + + update(stuff); + show(renderer,stuff); + + SDL_RenderPresent(renderer); +} + +void mousePressed(STUFFS *stuff){ + +} + +#include