diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/algolib.c | 108 | ||||
| -rw-r--r-- | lib/gobjects.c | 51 | ||||
| -rw-r--r-- | lib/init.c | 88 | ||||
| -rw-r--r-- | lib/init.h | 19 | ||||
| -rw-r--r-- | lib/vectorlib.c | 72 |
5 files changed, 338 insertions, 0 deletions
diff --git a/lib/algolib.c b/lib/algolib.c new file mode 100644 index 0000000..43a1f2e --- /dev/null +++ b/lib/algolib.c @@ -0,0 +1,108 @@ +#ifndef algolib +#define algolib + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <vectorlib.c> +#include <gobjects.c> +#include <SDL2/SDL.h> +#include <SDL2/SDL_ttf.h> + +#define flaot float + +//Binary Search Tree + +typedef struct BS_Node { + int value; + struct BS_Node *left; + struct BS_Node *right; + Point pos; + TEXT *text; +} BS_Node; + +void BSAddNode(BS_Node **self, BS_Node **node){ + if ((*node)->value < (*self)->value){ + if((*self)->left == NULL){ + (*self)->left = (*node); + } else { + BSAddNode(&(*self)->left,node); + } + } else if ((*node)->value > (*self)->value){ + if((*self)->right == NULL){ + (*self)->right = (*node); + } else { + BSAddNode(&(*self)->right,node); + } + } else { + free(*node); + } +} + +void BSAddValue(BS_Node **root, int value){ + BS_Node *node = (BS_Node*)malloc(sizeof(BS_Node)); + node->value=value; + node->left = NULL; + node->right = NULL; + + if ((*root) == NULL){ + (*root) = node; + } else { + BSAddNode(root, &node); + } +} + +void BSPrintTree(BS_Node **node){ + if((*node)->left != NULL){ + BSPrintTree(&(*node)->left); + } + printf("%d\n",(*node)->value); + if((*node)->right!= NULL){ + BSPrintTree(&(*node)->right); + } +} + +BS_Node *BSSearchTree(BS_Node **node, int val){ + if((*node)->value == val) { + return *node; + } else if (val < (*node)->value && (*node)->left != NULL){ + return BSSearchTree(&(*node)->left,val); + } else if (val > (*node)->value && (*node)->right!= NULL){ + return BSSearchTree(&(*node)->right,val); + } + return NULL; +} + +void BSFreeTree(BS_Node **node){ + if((*node)->left != NULL){ + BSFreeTree(&(*node)->left); + } + if((*node)->right != NULL){ + BSFreeTree(&(*node)->right); + } + free(*node); +} + + +//Breadth-First Search + +typedef struct BFS_Node{ + char *value; + struct BFS_Node **edges; + bool searched; + struct BFS_Node *parent; +} BFS_Node; + +typedef struct BFS_Graph{ + BFS_Node **nodes; + BFS_Node **graph; //make hashmap +} BFS_Graph; + +BFS_Node *BFS_Constructor(char *value){ + BFS_Node *node=(BFS_Node*)malloc(sizeof(BFS_Node)); + node->value=value; + node->edges=(BFS_Node**)malloc(sizeof(BFS_Node*)*2048); + return node; +} + +#endif diff --git a/lib/gobjects.c b/lib/gobjects.c new file mode 100644 index 0000000..0817181 --- /dev/null +++ b/lib/gobjects.c @@ -0,0 +1,51 @@ +#ifndef gobjects +#define gobjects + +#include <stdlib.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_ttf.h> + +#define flaot float + +typedef struct TEXT { + SDL_Rect textRect; + SDL_Texture *texture; +} TEXT; + +typedef struct GS{ + int width; + int height; + SDL_Renderer *renderer; + TTF_Font *font; +} GS; + +TEXT *text_constructor(GS *stuff, char *text_buf){ + TEXT *text=(TEXT*)malloc(sizeof(TEXT)); + SDL_Color color={ + 244,222,205 + }; + int w, h; + TTF_SizeUTF8(stuff->font,text_buf,&w,&h); + SDL_Rect textRect={ + 0,0,w,h + }; + textRect.x=(stuff->width/2)-(textRect.w/2); + textRect.y=(stuff->height/2)-(textRect.h/2); + SDL_Surface *textSurface = TTF_RenderUTF8_Solid_Wrapped(stuff->font, text_buf, color, textRect.w); + if(!textSurface){ + fprintf(stderr,"ERROR WITH TEXT SURFACE\n"); + exit(EXIT_FAILURE); + } + SDL_Texture *texture = SDL_CreateTextureFromSurface(stuff->renderer, textSurface); + SDL_FreeSurface(textSurface); + if(!texture){ + fprintf(stderr,"ERROR WITH TEXTURE\n"); + exit(EXIT_FAILURE); + } + + text->texture=texture; + text->textRect=textRect; + return text; +} + +#endif diff --git a/lib/init.c b/lib/init.c new file mode 100644 index 0000000..9321038 --- /dev/null +++ b/lib/init.c @@ -0,0 +1,88 @@ +#include <stdio.h> +#include <stdbool.h> +#include <time.h> +#include <init.h> + +#include <SDL2/SDL.h> +#include <SDL2/SDL_video.h> +#include <SDL2/SDL_ttf.h> + +int main(int argc, char *argv[]) +{ + if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) + SDL_Log("SDL fails to initialize! %s\n", SDL_GetError()); + + if (TTF_Init() < 0) + SDL_Log("SDL fails to initialize ttf! %s\n", SDL_GetError()); + + int startw=600; + int starth=600; + + 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()); + } + + srand(time(NULL)); + OBJECT_INER obj_storage[obj_n]; + + STUFFS stuff = { + startw, + starth, + false, + 0, + 0, + NULL, + NULL, + (OBJECT){}, + }; + + stuff.renderer = renderer; + + stuff.font = TTF_OpenFont("/usr/share/fonts/TTF/JetBrainsMono-Regular.ttf" , 10); + + init_stuffs(startw,starth,obj_storage,&stuff); + + + + for (int i = 0; i < obj_n; i++) + stuff.obj.objs[i] = &obj_storage[i]; + + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + + 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; + } + stuff.mx=e.button.x; + stuff.my=e.button.y; + } + draw(window,&stuff); + SDL_Delay(10); + } + + on_end(&stuff); + TTF_CloseFont(stuff.font); + 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 100644 index 0000000..5337013 --- /dev/null +++ b/lib/init.h @@ -0,0 +1,19 @@ +#ifndef inithell +#define inithell + +#include <stdio.h> +#include <stdbool.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_ttf.h> + +typedef struct{ + int width; + int height; + bool mousedown; + int mx; + int my; + TTF_Font *font; + SDL_Renderer *renderer; + OBJECT obj; +} STUFFS; +#endif diff --git a/lib/vectorlib.c b/lib/vectorlib.c new file mode 100644 index 0000000..c059742 --- /dev/null +++ b/lib/vectorlib.c @@ -0,0 +1,72 @@ +#ifndef vector_lib +#define vector_lib + +#include <math.h> +#include <stdlib.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; +} + +void point_div(Point *A, Point *B){ + A->x = A->x / B->x; + A->y = A->y / B->y; +} + +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; +} + +void point_limit(Point *point, float mag){ + flaot getmag = magnitude(point->x, point->y); + flaot rx, ry; + if (getmag > mag){ + rx = (point->x / getmag) * mag; + ry = (point->y / getmag) * mag; + } else { + rx = point->x; + ry = point->y; + } + + point->x = rx; + point->y = ry; +} + +float RandomFloat(float min, float max){ + return ((max - min) * ((float)rand() / (double)RAND_MAX)) + min; +} + +float constrain(float val, float min, float max){ + if(val > max) val=max; + else if(val < min) val=min; + return val; +} + +#endif
\ No newline at end of file |
