summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2024-10-11 00:25:31 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2024-10-11 00:25:31 +0700
commitb2a911d0ca1bf5da967be4ee6921471dedcefd53 (patch)
tree429a1ad9f186e09a554e9a5fe6b8e988f1b1c0ae
amusing
-rw-r--r--.gitignore8
-rw-r--r--CMakeLists.txt32
-rw-r--r--sources/main.c96
3 files changed, 136 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7493ec2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,8 @@
+.idea/
+.vscode/
+.vs/
+cmake-build-debug/
+bin/
+build/
+out/
+CMakeSettings.json \ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..5b78677
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,32 @@
+cmake_minimum_required(VERSION 3.0)
+project(visz C)
+set(CMAKE_C_STANDARD 99)
+
+# Adding Raylib
+include(FetchContent)
+set(FETCHCONTENT_QUIET FALSE)
+set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
+set(BUILD_GAMES OFF CACHE BOOL "" FORCE) # don't build the supplied example games
+
+FetchContent_Declare(
+ raylib
+ GIT_REPOSITORY "https://github.com/raysan5/raylib.git"
+ GIT_TAG "master"
+ GIT_PROGRESS TRUE
+)
+
+FetchContent_MakeAvailable(raylib)
+
+# Adding our source files
+file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/sources/*.c") # Define PROJECT_SOURCES as a list of all source files
+set(PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/sources/") # Define PROJECT_INCLUDE to be the path to the include directory of the project
+
+# Declaring our executable
+add_executable(${PROJECT_NAME})
+target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCES})
+target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_INCLUDE})
+target_link_libraries(${PROJECT_NAME} PRIVATE raylib)
+
+# Setting ASSETS_PATH
+target_compile_definitions(${PROJECT_NAME} PUBLIC ASSETS_PATH="${CMAKE_CURRENT_SOURCE_DIR}/assets/") # Set the asset path macro to the absolute path on the dev machine
+#target_compile_definitions(${PROJECT_NAME} PUBLIC ASSETS_PATH="./assets") # Set the asset path macro in release mode to a relative path that assumes the assets folder is in the same directory as the game executable \ No newline at end of file
diff --git a/sources/main.c b/sources/main.c
new file mode 100644
index 0000000..a84e253
--- /dev/null
+++ b/sources/main.c
@@ -0,0 +1,96 @@
+#include "raylib.h"
+#include <stdlib.h>
+#include <time.h>
+
+int main(void)
+{
+ const int pixSize = 5;
+ const int padding = 2;
+ const int dim = 80;
+ const int space = pixSize + (2 * padding);
+ const int winSize = space * dim;
+
+ bool (*currState)[dim];
+ bool (*nextState)[dim];
+ currState = malloc(dim * dim * sizeof(bool));
+ nextState = malloc(dim * dim * sizeof(bool));
+
+ srand(time(NULL));
+ int prob = 30;
+ for (int i=0; i<dim; i++) {
+ for (int j=0; j<dim; j++) {
+ if (rand() % 100 < prob) {
+ currState[i][j] = true;
+ nextState[i][j] = true;
+ } else {
+ currState[i][j] = false;
+ nextState[i][j] = false;
+ }
+ }
+ }
+
+ InitWindow(winSize, winSize, "Kontol");
+ SetTargetFPS(1);
+
+ while (!WindowShouldClose())
+ {
+ BeginDrawing();
+
+ ClearBackground(BLACK);
+
+
+ for (int i=0; i<dim; i++) {
+ for (int j=0; j<dim; j++) {
+ Color pixColor = BLACK;
+ if (currState[i][j]) pixColor = WHITE;
+ DrawRectangle(i * space, j * space, pixSize, pixSize, pixColor);
+
+ int n1 = 0;
+ int n2 = 0;
+ int n3 = 0;
+ int n4 = 0;
+ int n5 = 0;
+ int n6 = 0;
+ int n7 = 0;
+ int n8 = 0;
+ if (i > 0 && j > 0) {
+ n1 = currState[i - 1][j - 1];
+ n2 = currState[i - 1][j];
+ n4 = currState[i][j - 1];
+ }
+ if (i > 0 && j < dim-1) {
+ n3 = currState[i - 1][j + 1];
+ }
+ if (i < dim-1 && j < dim-1) {
+ n5 = currState[i][j + 1];
+ n7 = currState[i + 1][j];
+ }
+ if (i < dim-1 && j > 0) {
+ n6 = currState[i + 1][j - 1];
+ }
+ if (i < dim-1 && j < dim-1) {
+ n8 = currState[i + 1][j + 1];
+ }
+
+ int total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;
+ if (total < 2) nextState[i][j] = 0;
+ if (total > 3) nextState[i][j] = 0;
+ if ((total == 2 || total == 3) && currState[i][j]) nextState[i][j] = false;
+ if (total == 3 && !currState[i][j]) nextState[i][j] = true;
+ }
+ }
+
+ bool (*temp)[dim] = currState;
+ currState = nextState;
+ nextState = temp;
+
+ EndDrawing();
+ }
+
+ free(currState);
+ free(nextState);
+
+ CloseWindow();
+
+ return 0;
+}