01 one shape, one color

This commit is contained in:
noname
2026-01-01 23:56:07 +01:00
parent 2dbc773716
commit f4ec986cc6
5 changed files with 179 additions and 1 deletions

1
.gitignore vendored
View File

@@ -8,6 +8,7 @@
auto-save-list
tramp
.\#*
shapes
# Org-mode
.org-id-locations

BIN
01-one-shape/.game Executable file

Binary file not shown.

22
01-one-shape/Makefile Normal file
View File

@@ -0,0 +1,22 @@
PROG=shapes
CC=gcc
STD=c2x
SRC_FILES=main.c
OUT_DIR=./
CFLAGS=-std=$(STD) -ggdb
EXPERIMENTAL_FLAGS=-pg -Ofast
LIBS=-lraylib -lGL -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lm -lpthread -ldl -lrt -lm
shapes:
$(CC) $(SRC_FILES) -o $(OUT_DIR)$@ $(CFLAGS) $(EXPERIMENTAL_FLAGS) $(LIBS)
run: shapes
$(OUT_DIR)$(PROG)
clean:
rm $(OUT_DIR)$(PROG)
.PHONY: shapes run clean

153
01-one-shape/main.c Normal file
View File

@@ -0,0 +1,153 @@
#include <raylib.h>
#include <raymath.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
/*
Jedan oblik, jedna boja. Ovo je program koji mozda generise takvu sliku, mozda ne.
Radi na sansu, preko `chance` promenljive se kontrolise da li ce sansa da svi
budu isti oblici i boja, a verovatno nece biti.
Svake sekunde se dodaje novi, svake sekunde postoji sansa da resenje ovog zadatka
propadne.
Sada kada razmislim, ovo bas i ne radi tako, ali u nekom sirem smislu radi. Samo
sanse nisu to sto se misli da jesu. Bice neki bugfix.
Dostupna je i 2D kamera. Tako da sto duze traje, ako se dovoljno odzumira greske
se i ne vide iako ce se kad tad pojaviti.
Nisam se setio necega kreativnijeg
pod ovim ogranicenjima.
EN:
One shape, one color.
This is a program that might generate such an image, maybe not.
It works based on chance, with the chance variable controlling whether everything will be the same shape and color, though it's unlikely.
Every second, a new object is added, and each second there's a chance that the solution to this task will fail.
Now that I think about it, this doesn't quite work as expected, but in a broader sense, it does. It's just that the chances aren't quite what one would think. There will be a bugfix.
2D camera is also available. So the longer it lasts, if it's zoomed out enough mistakes
won't be seen even though they will eventually show up.
I couldn't come up with something more creative under these constraints.
*/
typedef struct Circle {
int radius;
int x;
int y;
} Circle;
int main(int argc, char *argv[]) {
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "one color one shape");
SetTargetFPS(60);
Rectangle rects[100] = {0};
Circle circles[100] = {0};
double previous_time = GetTime();
double current_time = 0.0;
double update_draw_time = 0.0;
double wait_time = 0.0;
float delta_time = 0.0f;
float time_counter = 0.0f;
int target_fps = 60;
Camera2D camera = {0};
camera.target = (Vector2){ 0.0f, 0.0f };
camera.offset = (Vector2){250.0f, 250.0f};
camera.rotation = 0.0f;
camera.zoom = 1.0f;
Vector2 prevMousePos = GetMousePosition();
srand(time(NULL));
int rand_int = rand() % 101 + 1;
int chance = 10;
if(argc == 2) {
chance = atoi(argv[1]);
}
int current_objects=0;
while(!WindowShouldClose()) {
float mouseDelta = GetMouseWheelMove();
float newZoom = camera.zoom + mouseDelta * 0.01f;
if (newZoom <= 0)
newZoom = 0.01f;
camera.zoom = newZoom;
// Kod za 2D kameru od:
//https://gist.github.com/JeffM2501/3c7da5c2b7e078e254d673f91489c78f
// arhivirano:
Vector2 thisPos = GetMousePosition();
Vector2 delta = Vector2Subtract(prevMousePos, thisPos);
prevMousePos = thisPos;
if (IsMouseButtonDown(0))
camera.target = GetScreenToWorld2D(Vector2Add(camera.offset, delta),camera);
time_counter += delta_time;
camera.zoom = expf(logf(camera.zoom) + ((float)GetMouseWheelMove()*0.1f));
rand_int = rand() % 101 + 1;
if(time_counter >= 1.0f) {
rand_int = rand() % 100 + 1;
if(rand_int < chance) {
rects[current_objects] =(Rectangle){current_objects*30, 0, 30, 30};
} else {
circles[current_objects] = (Circle){.radius=30, .x=current_objects*30, 0};
}
time_counter = 0.0f;
current_objects++;
}
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
for(int i = 0; i <current_objects; i++) {
for(int j = 0; j < 10; j++) {
if(rects[i].x!=0) {
DrawRectangle(i*30, i*50, 30, 30,MAROON);
DrawRectangle(i*30, -i*50, 30, 30,MAROON);
DrawRectangle(-i*30, -i*50, 30, 30,MAROON);
DrawRectangle(-i*30, i*50, 30, 30,MAROON);
} else if(circles[i].radius !=0) {
DrawCircle(i*30, i*50, 30, GREEN);
DrawCircle(-i*30, i*50, 30, GREEN);
DrawCircle(-i*30, -i*50, 30, GREEN);
DrawCircle(i*30, -i*50, 30, GREEN);
}
}
}
EndMode2D();
EndDrawing();
current_time = GetTime();
update_draw_time = current_time - previous_time;
if (target_fps > 0) {
wait_time = (1.0f/(float)target_fps) - update_draw_time;
if (wait_time > 0.0) {
WaitTime((float)wait_time);
current_time = GetTime();
delta_time = (float)(current_time - previous_time);
}
}
else {
delta_time = (float)update_draw_time;
}
previous_time = current_time;
}
}

View File

@@ -1,3 +1,5 @@
# genuary
genuary 2026
# 01 one shape, one color