Bläddra i källkod

Improve scaling and randomness

Kajetan Johannes Hammerle 1 månad sedan
förälder
incheckning
c09e94d81b
1 ändrade filer med 58 tillägg och 36 borttagningar
  1. 58 36
      Main.c

+ 58 - 36
Main.c

@@ -1,5 +1,3 @@
-#define _XOPEN_SOURCE_EXTENDED
-
 #include <locale.h>
 #include <ncurses.h>
 #include <stdio.h>
@@ -21,14 +19,16 @@ static void (*rotateFunction)(void);
 static uint32_t seed = 0;
 
 static void initRandom(void) {
-    struct timespec time;
-    timespec_get(&time, TIME_UTC);
-    seed = (uint32_t)time.tv_nsec;
+    while(seed == 0) {
+        struct timespec time;
+        timespec_get(&time, TIME_UTC);
+        seed = (uint32_t)time.tv_nsec;
+    }
 }
 
 static int nextRandom(int max) {
-    seed = seed * 27697u + 13711u;
-    int32_t random = ((int32_t)(seed >> 1)) % max;
+    seed = seed * 0x915f77f5 + 1;
+    int32_t random = ((int32_t)(seed >> 16)) % max;
     return random;
 }
 
@@ -46,22 +46,41 @@ static void initGamefield(int w, int h) {
 }
 
 static void printGamefield(void) {
+    clear();
     static const char* MAP[] = {"\u2592", "\u2593", " "};
     static const char* wall = "\u2588";
 
-    for(int x = 0; x < width * 2 + 4; x++) {
-        mvaddstr(0, x, wall);
-        mvaddstr(height + 1, x, wall);
+    int maxX = 0;
+    int maxY = 0;
+    getmaxyx(stdscr, maxY, maxX);
+    int factorX = maxX / (width + 2);
+    int factorY = maxY / (height + 2);
+    while(factorY > 1 && factorY * 2 > factorX) {
+        factorY--;
+    }
+    factorX = factorY * 2;
+
+    for(int x = 0; x < (width + 2) * factorX; x++) {
+        for(int y = 0; y < factorY; y++) {
+            mvaddstr(y, x, wall);
+            mvaddstr(y + (height + 1) * factorY, x, wall);
+        }
     }
     for(int y = 0; y < height; y++) {
-        mvaddstr(y + 1, 0, wall);
-        addstr(wall);
-        for(int x = 0; x < width; x++) {
-            addstr(MAP[fields[y][x]]);
-            addstr(MAP[fields[y][x]]);
+        for(int wy = y * factorY; wy < (y + 1) * factorY; wy++) {
+            mvaddstr(wy + factorY, 0, wall);
+            for(int x = 1; x < factorX; x++) {
+                addstr(wall);
+            }
+            for(int x = 0; x < width; x++) {
+                for(int w = 0; w < factorX; w++) {
+                    addstr(MAP[fields[y][x]]);
+                }
+            }
+            for(int x = 0; x < factorX; x++) {
+                addstr(wall);
+            }
         }
-        addstr(wall);
-        addstr(wall);
     }
 }
 
@@ -141,7 +160,9 @@ static void rotate(void) {
 static void spawnObject(int x, int y) {
     objectX = x;
     objectY = y;
-    switch(nextRandom(7)) {
+    int u = nextRandom(8);
+    objects[u]++;
+    switch(u) {
         // .X.
         // XXX
         // ...
@@ -184,25 +205,10 @@ static void spawnObject(int x, int y) {
             movingObject[7] = y + 2;
             rotateFunction = rotate33;
             break;
-        // .X..
-        // .X..
-        // .X..
-        // .X..
-        case 3:
-            movingObject[0] = x + 0;
-            movingObject[1] = y + 0;
-            movingObject[2] = x + 0;
-            movingObject[3] = y + 1;
-            movingObject[4] = x + 0;
-            movingObject[5] = y + 2;
-            movingObject[6] = x + 0;
-            movingObject[7] = y + 3;
-            rotateFunction = rotate41;
-            break;
         // X..
         // XX.
         // .X.
-        case 4:
+        case 3:
             movingObject[0] = x + 0;
             movingObject[1] = y + 0;
             movingObject[2] = x + 0;
@@ -216,7 +222,7 @@ static void spawnObject(int x, int y) {
         // .X.
         // XX.
         // X..
-        case 5:
+        case 4:
             movingObject[0] = x + 1;
             movingObject[1] = y + 0;
             movingObject[2] = x + 0;
@@ -229,7 +235,7 @@ static void spawnObject(int x, int y) {
             break;
         // XX
         // XX
-        case 6:
+        case 5:
             movingObject[0] = x + 0;
             movingObject[1] = y + 0;
             movingObject[2] = x + 1;
@@ -240,6 +246,22 @@ static void spawnObject(int x, int y) {
             movingObject[7] = y + 1;
             rotateFunction = rotate22;
             break;
+        // .X..
+        // .X..
+        // .X..
+        // .X..
+        case 6:
+        case 7:
+            movingObject[0] = x + 0;
+            movingObject[1] = y + 0;
+            movingObject[2] = x + 0;
+            movingObject[3] = y + 1;
+            movingObject[4] = x + 0;
+            movingObject[5] = y + 2;
+            movingObject[6] = x + 0;
+            movingObject[7] = y + 3;
+            rotateFunction = rotate41;
+            break;
     }
 
     for(int i = 0; i < 8; i += 2) {