diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-10-07 23:26:38 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-10-07 23:26:38 +0300 |
commit | e41af6679dae3f4c69b46962a6bf1a1f81bee600 (patch) | |
tree | b8d48e6707c4114408b270853ba8c6114d5d5a21 |
kind of working but not really
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | README.md | 0 | ||||
-rwxr-xr-x | build.sh | 46 | ||||
-rw-r--r-- | src/display.c | 37 | ||||
-rw-r--r-- | src/display.h | 19 | ||||
-rw-r--r-- | src/main.c | 105 |
6 files changed, 209 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbbd0b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/
\ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..95ea230 --- /dev/null +++ b/build.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +cd ${0%/*} # go to project root + +FLAGS="-Wall -Wextra -g -pedantic" +FLAGS_DISPLAY="-lraylib -lm -lpthread -lGL -ldl -lrt -lX11" +SRC="src" +BIN="bin" +OBJ="obj" +RUN=0 +VALGRIND="" + +function __run__ { + RUN=1 +} + +function __valgrind__ { + VALGRIND=valgrind + RUN=1 +} + +function __clean__ { + rm -rf $BIN + rm -rf $OBJ + kill $( ps -q $$ -o pgid= ) # exit +} + +set -xe + +if ! { [[ $# -eq 0 ]]; } 2> /dev/null +then + __$1__ +fi + + +mkdir -p $BIN +mkdir -p $OBJ + +gcc -c -o $OBJ/main.o $SRC/main.c +gcc -c -o $OBJ/display.o $SRC/display.c +gcc -o $BIN/main $OBJ/main.o $OBJ/display.o $FLAGS $FLAGS_DISPLAY + +if ! { [[ $RUN -eq 0 ]]; } 2> /dev/null +then + $BIN/main +fi diff --git a/src/display.c b/src/display.c new file mode 100644 index 0000000..57acf86 --- /dev/null +++ b/src/display.c @@ -0,0 +1,37 @@ +#include <raylib.h> +#include "display.h" + +static dspl_createinfo info; + +void dspl_create(dspl_createinfo createinfo) +{ + info = createinfo; +} +void dspl_start() +{ + InitWindow(info.width, info.height, info.name); + SetTargetFPS(info.fps); + + while(!WindowShouldClose()) + { + BeginDrawing(); + ClearBackground(RAYWHITE); + info.update_func(); + EndDrawing(); + } +} + +void dspl_destroy() +{ + CloseWindow(); +} + +void dspl_draw_circle(int cx, int cy, int r) +{ + DrawCircle((info.width/2) + cx, cy, 5, RED); +} + +void dspl_draw_line(int x1, int y1, int x2, int y2, int c) +{ + DrawLine((info.width/2) + x1, y1, (info.width/2) + x2, y2 , (c == 0) ? BLUE : RED); +} diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..088feb7 --- /dev/null +++ b/src/display.h @@ -0,0 +1,19 @@ +#ifndef DISPLAY_H +#define DISPLAY_H + +typedef struct dspl_createinfo { + int width; + int height; + char *name; + int fps; + void (*update_func)(); +} dspl_createinfo; + +void dspl_create(dspl_createinfo info); +void dspl_start(); +void dspl_destroy(); + +void dspl_draw_line(int x1, int y1, int x2, int y2, int c); +void dspl_draw_circle(int cx, int cy, int r); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..53b4edf --- /dev/null +++ b/src/main.c @@ -0,0 +1,105 @@ +#include <stdio.h> +#include <math.h> +#include "display.h" + +#define W 1600 +#define H 900 +#define FPS 60 +#define RFPS 1/600 +#define PI 3.141592654 + +typedef struct{ + float x; + float y; +} vec; + + +const float s = (PI/180 * 10); // rad +const int l = 250; // mm +const int m = 25; // kg +// const vec g = {0.0f, -9.81f}; // m/s^2 +const float g = -9.81f; + +// vec p; +// vec v; +// vec a; +float t; +float v; +float a; + +// vec rotate_grav(float angle, int d) // d = 0-clock, 1-anti +// { +// // vec rg = {0, g.y*m*sinf(angle)}; +// vec rg = {0, g.y}; +// if(d == 0) { +// rg.x = (rg.y*sinf(angle)); +// rg.y = -(rg.y*cosf(angle)); +// } else if(d == 1) { +// rg.x = -(rg.y*sinf(angle)); +// rg.y = (rg.y*cosf(angle)); +// } + +// printf("grav transform: %f %f %f\n", rg.x, rg.y, sqrt((rg.x*rg.x) + (rg.y*rg.y))); +// return rg; +// } + +void update() +{ + + // float sinamp = p.x / l; + // vec rg = rotate_grav((PI/2) - asinf(sinamp), (p.x > 0) ? 0 : 1); + + // printf("amp: %f\n", asinf(sinamp)*180/PI); + // printf("lenght: %f\n", sqrt((p.x*p.x) + (p.y*p.y))); + + // p.x = sinf(t) * l; + // p.y = cosf(t) * l; + + // a.x += rg.x; + // a.y += rg.y; + // v.x += a.x * RFPS; + // v.y += a.y * RFPS; + // p.x += v.x * RFPS; + // p.y += v.y * RFPS; + + // dspl_draw_line(p.x, p.y, v.x + p.x, v.y + p.y, 0); + // dspl_draw_line(p.x, p.y, a.x + p.x, a.y + p.y, 1); + // dspl_draw_line(p.x, p.y, 0, 0, 0); + // dspl_draw_circle(p.x, p.y, 5); + + a += g * cosf(t) * m; + printf("%f %f\n", sinf(t), a); + v += a * RFPS; + t += v * RFPS; + + + int px = sinf(t) * l; + int py = cosf(t) * l; + dspl_draw_line(px, py, 0, 0, 0); + dspl_draw_circle(px, py, 5); + +} + +int main(void) +{ + // p.x = sinf(t) * l; + // p.y = cosf(t) * l; + // v.x = 0; v.y = 0; + // a.x = 0; a.y = 0; + + t = s; + v = 0; + a = 0; + + dspl_createinfo info = {0}; + info.width = W; + info.height = H; + info.name = "Pendulum"; + info.fps = FPS; + info.update_func = &update; + + dspl_create(info); + dspl_start(); + dspl_destroy(); + return 0; +} |