diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-02-05 00:41:41 -0600 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-02-05 00:41:41 -0600 |
| commit | 269e06dae5facc0f201bf6d3b2564afd65c55a1b (patch) | |
| tree | 17266bd1830653f44d8a8bdd491ad970b36c0c9f | |
init
| -rw-r--r-- | .gitignore | 3 | ||||
| -rwxr-xr-x | Makefile | 7 | ||||
| -rwxr-xr-x | main.c | 191 |
3 files changed, 201 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..36338e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +game-of-life +.clangd +suggest.md diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..c1a4587 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +LIBS="-I/home/iceyrazor/stuff/scripts/c/my_utils" + +main: main.c + clang -O3 -o game-of-life main.c ${LIBS} `sdl2-config --cflags --libs` -lm + +debug: main.c + clang -O3 -o game-of-life main.c ${LIBS} `sdl2-config --cflags --libs` -Wall -lm -fsanitize=address -g @@ -0,0 +1,191 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <time.h> + +#include <SDL2/SDL.h> +#include <SDL2/SDL_video.h> + +int startw=1900; +int starth=1100; + +void putPixelRGB(SDL_Renderer* renderer, int x, int y, unsigned char r,unsigned char g,unsigned char b){ +SDL_SetRenderDrawColor(renderer,(Uint8)r,(Uint8)g,(Uint8)b,255); +SDL_RenderDrawPoint(renderer,x,y); +} + +void putRectRGB(SDL_Renderer* renderer, int x, int y, int size, unsigned char r,unsigned char g,unsigned char b){ + +SDL_Rect tmp_rect; +tmp_rect.w = tmp_rect.h = size; +tmp_rect.x=x; +tmp_rect.y=y; +SDL_SetRenderDrawColor(renderer,(Uint8)r,(Uint8)g,(Uint8)b,255); +SDL_RenderFillRect(renderer,&tmp_rect); +} + +void iterate_game(int **grid, int** nextgrid, int cols, int rows){ + for (int i=0; i<cols; i++){ + for(int j=0;j<rows; j++){ + int state=grid[i][j]; + int sum=0; + for (int k=-1; k<2; k++){ + for (int l=-1; l<2; l++){ + int col = (i + k + cols) % cols; + int row = (j + l + rows) % rows; + sum = sum + grid[col][row]; + } + } + + sum -= grid[i][j]; + + if(state==0&sum==3){ + nextgrid[i][j]=1; + } else if((state==1) & (sum<2 | sum>3)){ + nextgrid[i][j]=0; + } else { + nextgrid[i][j]=state; + } + } + } + + for (int i=0; i<cols; i++){ + for(int j=0;j<rows; j++){ + grid[i][j]=nextgrid[i][j]; + } + } +} + +void draw(SDL_Renderer* renderer,SDL_Window* window,int cols, int rows, float mX, float mY, int boxsize, int **grid, int **nextgrid, int width, int height){ + for (int i=0; i<width; i++){ + for(int j=0;j<height; j++){ + putPixelRGB(renderer,i,j,0,0,0); + } + } + + + + + for (int i=0; i<cols; i++){ + for(int j=0;j<rows; j++){ + if(grid[i][j]==1){ + putRectRGB(renderer,i*boxsize,j*boxsize,boxsize-1,150,50,255); + } + } + } + iterate_game(grid,nextgrid,cols,rows); + + + + SDL_RenderPresent(renderer); + SDL_UpdateWindowSurface(window); +} + +void mousePressed(int **grid,int x,int y,int size,int cols, int rows, double hue){ + +} + +int main(int argc, char *argv[]) +{ + if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) + SDL_Log("SDL fails to initialize! %s\n", SDL_GetError()); + + + + SDL_Window *window = SDL_CreateWindow("SDL Tutorial", 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()); + } + + int w, h; + SDL_GetWindowSize(window, &w, &h); + printf("Window dimensions: %dx%d\n", w, h); + + int boxsize=15; + int cols=w/boxsize; + int rows=h/boxsize; + SDL_Log("cols %d rows %d\n\n\n\n",cols,rows); + int **grid = (int **)malloc(cols * sizeof(int *)); + for (int i=0; i<cols; i++){ + grid[i]=(int *)malloc(rows * sizeof(int *)); + } + + int **nextgrid = (int **)malloc(cols * sizeof(int *)); + for (int i=0; i<cols; i++){ + nextgrid[i]=(int *)malloc(rows * sizeof(int *)); + } + + srand(time(NULL)); + for (int i=0; i<cols; i++){ + for(int j=0;j<rows; j++){ + grid[i][j]=rand()%2; + nextgrid[i][j]=rand()%2; + } + } + + + draw(renderer,window,cols,rows,0,0,boxsize,grid,nextgrid,w,h); + + bool quit = false; + SDL_Event e; + bool mousedown=false; + int gmouseX=0; + int gmouseY=0; + bool ctrl=false; + 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){ + mousedown=true; + } else if(e.button.button==SDL_BUTTON_RIGHT){ + } + } else if(e.type==SDL_MOUSEBUTTONUP){ + mousedown=false; + } + + //SDL_Log("%d\n",e.key.keysym.sym); + if(e.type==SDL_KEYDOWN&e.key.keysym.sym==1073742048){ + ctrl=true; + + }else if(e.type==SDL_KEYUP&e.key.keysym.sym==1073742048){ + ctrl=false; + }else if(e.type==SDL_KEYDOWN&e.key.keysym.sym==114&ctrl==true){ + srand(time(NULL)); + for (int i=0; i<cols; i++){ + for(int j=0;j<rows; j++){ + grid[i][j]=rand()%2; + } + } + } + + + gmouseX=e.button.x; + gmouseY=e.button.y; + } + float mouseoutX=gmouseX; + float mouseoutY=gmouseY; + draw(renderer,window,cols,rows,mouseoutX,mouseoutY,boxsize,grid,nextgrid,w,h); + //SDL_Delay(10); + } + + SDL_DestroyWindow(window); + //free window and windowSurface + SDL_Quit(); + for (int i=0; i<cols; i++){ + free(grid[i]); + free(nextgrid[i]); + } + free(grid); + free(nextgrid); + + + + return 0; +} |
