a janky init
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
bin/**
|
||||
**/.clangd
|
||||
.clangd
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -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.
|
||||
3
README.md
Normal file
3
README.md
Normal file
@@ -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
|
||||
55
lib/global_objects.c
Normal file
55
lib/global_objects.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef global_objects
|
||||
#define global_objects
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#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<f64>{
|
||||
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
|
||||
61
lib/init.c
Executable file
61
lib/init.c
Executable file
@@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include "init.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
|
||||
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;
|
||||
}
|
||||
13
lib/init.h
Executable file
13
lib/init.h
Executable file
@@ -0,0 +1,13 @@
|
||||
#ifndef inithell
|
||||
#define inithell
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct{
|
||||
int width;
|
||||
int height;
|
||||
bool mousedown;
|
||||
OBJECT *obj;
|
||||
} STUFFS;
|
||||
#endif
|
||||
28
make
Executable file
28
make
Executable file
@@ -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
|
||||
55
src/1-1-walker.c
Executable file
55
src/1-1-walker.c
Executable file
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
|
||||
#define PROJECT_NAME "vecotrs-main"
|
||||
|
||||
typedef struct {
|
||||
float x;
|
||||
float y;
|
||||
} OBJECT;
|
||||
|
||||
#include <init.h>
|
||||
|
||||
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 <init.c>
|
||||
56
src/1-2-vec-math.c
Executable file
56
src/1-2-vec-math.c
Executable file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include "global_objects.c"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
|
||||
#define PROJECT_NAME "vecotrs-main"
|
||||
|
||||
typedef struct {
|
||||
Point pos;
|
||||
Point vel;
|
||||
} OBJECT;
|
||||
|
||||
#include <init.h>
|
||||
|
||||
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 <init.c>
|
||||
65
src/1-3-rand-vec.c
Executable file
65
src/1-3-rand-vec.c
Executable file
@@ -0,0 +1,65 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include "global_objects.c"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
|
||||
#define PROJECT_NAME "vecotrs-main"
|
||||
|
||||
typedef struct {
|
||||
Point pos;
|
||||
} OBJECT;
|
||||
|
||||
#include <init.h>
|
||||
|
||||
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 <init.c>
|
||||
Reference in New Issue
Block a user