Переглянути джерело

Fix issues/warnings, build with cmake

Kajetan Johannes Hammerle 1 тиждень тому
батько
коміт
f65557febd
12 змінених файлів з 366 додано та 230 видалено
  1. 6 0
      .clangd
  2. 3 0
      .gitignore
  3. 94 0
      CMakeLists.txt
  4. 0 22
      GameEngine.h
  5. 0 18
      meson.build
  6. 2 1
      src/Control.c
  7. 1 1
      src/Control.h
  8. 58 39
      src/GameEngine.c
  9. 21 0
      src/GameEngine.h
  10. 159 143
      src/Main.c
  11. 8 5
      src/Vector3D.c
  12. 14 1
      src/Vector3D.h

+ 6 - 0
.clangd

@@ -0,0 +1,6 @@
+CompileFlags:
+  Add:
+    - "-ferror-limit=0"
+    - "-std=c23"
+    - "-xc"
+  CompilationDatabase: ./build_debug/

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+build_debug/
+build_release/
+.cache/

+ 94 - 0
CMakeLists.txt

@@ -0,0 +1,94 @@
+cmake_minimum_required(VERSION 3.25)
+project(inverting_game C)
+
+set(CMAKE_C_STANDARD 23)
+
+set(SRC
+    "src/Control.c"
+    "src/GameEngine.c"
+    "src/Main.c"
+    "src/Vector3D.c"
+)
+
+set(WARNINGS
+    -Wno-attributes
+
+    -Wall
+    -Walloc-zero
+    -Walloca
+    -Wanalyzer-too-complex
+    -Warith-conversion
+    -Warray-bounds=2
+    -Warray-parameter
+    -Wattribute-alias=2
+    -Wbad-function-cast
+    -Wbidi-chars=any
+    -Wcast-align=strict
+    -Wcast-qual
+    -Wconversion
+    -Wdate-time
+    -Wdisabled-optimization
+    -Wdouble-promotion
+    -Wduplicated-branches
+    -Wduplicated-cond
+    -Wenum-compare
+    -Wenum-conversion
+    -Werror
+    -Wextra
+    -Wfloat-equal
+    -Wformat-overflow=2
+    -Wformat-signedness
+    -Wformat-truncation=2
+    -Wformat=2
+    -Wframe-larger-than=8388608
+    -Wimplicit-fallthrough=5
+    -Winfinite-recursion
+    -Winit-self
+    -Winvalid-pch
+    -Wjump-misses-init
+    -Wlarger-than=1073741824
+    -Wlogical-op
+    -Wmissing-braces
+    -Wmissing-declarations
+    -Wmissing-include-dirs
+    -Wmissing-prototypes
+    -Wmultichar
+    -Wnarrowing
+    -Wnested-externs
+    -Wnormalized=nfkc
+    -Wnull-dereference
+    -Wold-style-definition
+    -Woverlength-strings
+    -Wredundant-decls
+    -Wshadow
+    -Wshift-overflow=2
+    -Wsign-conversion
+    -Wstack-protector
+    -Wstack-usage=8388608
+    -Wstrict-overflow=2
+    -Wstrict-prototypes
+    -Wstringop-overflow=4
+    -Wswitch-enum
+    -Wtrampolines
+    -Wtrivial-auto-var-init
+    -Wundef
+    -Wunreachable-code
+    -Wunused-const-variable=2
+    -Wuse-after-free=3
+    -Wvla
+    -Wwrite-strings
+    -pedantic
+    -pedantic-errors
+)
+
+add_executable(inverting_game ${SRC})
+target_compile_options(inverting_game PRIVATE
+    ${WARNINGS}
+    -fdiagnostics-color=always
+)
+target_link_libraries(inverting_game PRIVATE
+    m
+    GLEW
+    glfw
+    GL
+)

+ 0 - 22
GameEngine.h

@@ -1,22 +0,0 @@
-#ifndef GAMEENGINE_H
-#define GAMEENGINE_H
-
-#include <GL/glew.h>
-#include <GLFW/glfw3.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-static const long NANOS_PER_TICK = 50000000;
-static const int MAX_TICKS_PER_FRAME = 5;
-
-typedef void (*InitFunction)(int);
-typedef void (*TickFunction)(int);
-typedef void (*RenderTickFunction)(int, float);
-typedef void (*WindowResize)(int, int);
-typedef void (*MouseMove)(float, float);
-
-int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFunction renderTick,
-              WindowResize windowResize, MouseMove move);
-GLuint getProgram();
-
-#endif

+ 0 - 18
meson.build

@@ -1,18 +0,0 @@
-project('inverting game', 'c')
-
-sources = [
-    'Control.c', 
-    'GameEngine.c', 
-    'Main.c', 
-    'Vector3D.c']
-
-glewDep = dependency('glew')
-glfwDep = dependency('glfw3')
-
-cc = meson.get_compiler('c')
-mathDep = cc.find_library('m', required : true)
-
-executable('inverting_game', 
-    sources: sources,
-    dependencies : [glewDep, glfwDep, mathDep],
-    cpp_args: ['-Wall', '-Wextra', '-pedantic', '-Werror'])

+ 2 - 1
Control.c → src/Control.c

@@ -1,4 +1,5 @@
 #include "Control.h"
+
 #include <GLFW/glfw3.h>
 
 typedef struct Key {
@@ -75,4 +76,4 @@ int mouseIsDown(int button) {
 
 int mouseIsJustDown(int button) {
     return buttons[button].downTime == 1 || buttons[button].fastClick > 0;
-}
+}

+ 1 - 1
Control.h → src/Control.h

@@ -16,4 +16,4 @@ void mouseRelease(int button);
 int mouseIsDown(int button);
 int mouseIsJustDown(int button);
 
-#endif
+#endif

+ 58 - 39
GameEngine.c → src/GameEngine.c

@@ -1,4 +1,9 @@
 #include "GameEngine.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
 #include "Control.h"
 
 static GLuint program;
@@ -8,14 +13,18 @@ static WindowResize windowResize;
 static MouseMove mouseMove;
 static int activeFocus = 0;
 
+static bool hasOldMousePosition = false;
 static double oldMouseX = 0;
 static double oldMouseY = 0;
 
-static void flushOutput() {
+static void flushOutput(void) {
     fflush(stdout);
 }
 
-static void onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods) {
+static void onKeyEvent(
+    GLFWwindow* w, int key, int scancode, int action, int mods) {
+    (void)scancode;
+    (void)mods;
     if(action == GLFW_RELEASE) {
         if(key == GLFW_KEY_ESCAPE) {
             activeFocus = 0;
@@ -29,6 +38,7 @@ static void onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mod
 }
 
 static void onMouseClick(GLFWwindow* w, int button, int action, int mods) {
+    (void)mods;
     if(action == GLFW_PRESS) {
         if(!activeFocus) {
             glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@@ -44,12 +54,14 @@ static void onMouseClick(GLFWwindow* w, int button, int action, int mods) {
 }
 
 static void onMouseMove(GLFWwindow* w, double x, double y) {
+    (void)w;
     if(activeFocus) {
-        if(oldMouseX == 0 && oldMouseY == 0) {
+        if(!hasOldMousePosition) {
             oldMouseX = x;
             oldMouseY = y;
+            hasOldMousePosition = true;
         } else {
-            mouseMove(x - oldMouseX, y - oldMouseY);
+            mouseMove((float)(x - oldMouseX), (float)(y - oldMouseY));
             oldMouseX = x;
             oldMouseY = y;
         }
@@ -57,15 +69,16 @@ static void onMouseMove(GLFWwindow* w, double x, double y) {
 }
 
 static void onWindowResize(GLFWwindow* w, int width, int height) {
+    (void)w;
     glViewport(0, 0, width, height);
     windowResize(width, height);
 }
 
-static GLchar* readFile(char* name) {
+static GLchar* readFile(const char* name) {
     FILE* file = fopen(name, "r");
     if(file != NULL) {
-        int size = 128;
-        int index = 0;
+        size_t size = 128;
+        size_t index = 0;
         GLchar* content = malloc(sizeof(GLchar) * size);
 
         while(1) {
@@ -77,7 +90,7 @@ static GLchar* readFile(char* name) {
                 size *= 2;
                 content = realloc(content, size);
             }
-            content[index] = c;
+            content[index] = (char)c;
             index++;
         }
 
@@ -94,7 +107,7 @@ static GLchar* readFile(char* name) {
     return NULL;
 }
 
-int checkShaderErrors(char* name, GLuint shader) {
+static int checkShaderErrors(const char* name, GLuint shader) {
     int returnValue = 0;
 
     printf("compiling %s shader ...\n", name);
@@ -123,13 +136,13 @@ int checkShaderErrors(char* name, GLuint shader) {
     return returnValue;
 }
 
-static GLuint compileProgram(const GLchar* vertex, const GLchar* fragment) {
+static bool compileProgram(const GLchar* vertex, const GLchar* fragment) {
     vShader = glCreateShader(GL_VERTEX_SHADER);
     glShaderSource(vShader, 1, &vertex, NULL);
     glCompileShader(vShader);
 
     if(checkShaderErrors("vertex", vShader)) {
-        return 0;
+        return true;
     }
 
     fShader = glCreateShader(GL_FRAGMENT_SHADER);
@@ -137,10 +150,10 @@ static GLuint compileProgram(const GLchar* vertex, const GLchar* fragment) {
     glCompileShader(fShader);
 
     if(checkShaderErrors("fragment", fShader)) {
-        return 0;
+        return true;
     }
 
-    GLuint program = glCreateProgram();
+    program = glCreateProgram();
     glAttachShader(program, vShader);
     glAttachShader(program, fShader);
     glLinkProgram(program);
@@ -149,7 +162,7 @@ static GLuint compileProgram(const GLchar* vertex, const GLchar* fragment) {
     GLenum error = glGetError();
     if(error) {
         fprintf(stderr, "error: %u\n", glGetError());
-        return 0;
+        return true;
     } else {
         printf("no error occured ...\n");
     }
@@ -166,31 +179,30 @@ static GLuint compileProgram(const GLchar* vertex, const GLchar* fragment) {
         glGetProgramInfoLog(program, bufferSize, &charsUsed, buffer);
         buffer[bufferSize - 1] = '\0';
         fprintf(stderr, "%s\n", buffer);
-        return 0;
+        return true;
     }
 
     glUseProgram(program);
-    return program;
+    return false;
 }
 
-static GLuint createProgram() {
-    GLchar* vertex = readFile("shader/vertex.vs");
+static bool createProgram(void) {
+    GLchar* vertex = readFile("./shader/vertex.vs");
     if(vertex == NULL) {
         fprintf(stderr, "cannot read vertex.vs\n");
-        return 0;
+        return true;
     }
-    GLchar* fragment = readFile("shader/fragment.fs");
+    GLchar* fragment = readFile("./shader/fragment.fs");
     if(fragment == NULL) {
         fprintf(stderr, "cannot read fragment.fs\n");
         free(vertex);
-        return 0;
+        return true;
     }
-
-    GLuint program = compileProgram(vertex, fragment);
+    bool r = compileProgram(vertex, fragment);
     flushOutput();
     free(vertex);
     free(fragment);
-    return program;
+    return r;
 }
 
 static void onTerm(GLFWwindow* window) {
@@ -198,8 +210,10 @@ static void onTerm(GLFWwindow* window) {
     glfwTerminate();
 }
 
-int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFunction renderTick,
-              WindowResize inWindowResize, MouseMove move) {
+int startGame(
+    const char* name, InitFunction init, TickFunction tick,
+    RenderTickFunction renderTick, WindowResize inWindowResize,
+    MouseMove move) {
     if(!glfwInit()) {
         fprintf(stderr, "could not initialize GLFW");
         return 1;
@@ -219,7 +233,6 @@ int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFuncti
     }
 
     glfwSetKeyCallback(window, onKeyEvent);
-    glfwSetFramebufferSizeCallback(window, onWindowResize);
     glfwSetMouseButtonCallback(window, onMouseClick);
     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
     activeFocus = 1;
@@ -232,13 +245,19 @@ int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFuncti
 
     GLenum err = glewInit();
     if(GLEW_OK != err) {
-        fprintf(stderr, "Could not initialize GLEW: %s\n", glewGetErrorString(err));
-        return 1;
+        const char* error = (const char*)glewGetErrorString(err);
+        if(err != GLEW_ERROR_NO_GLX_DISPLAY ||
+           strcmp(error, "Unknown error") != 0) {
+            fprintf(stderr, "Could not initialize GLEW: %s\n", error);
+            return 1;
+        }
+        fprintf(stderr, "Ignoring glewInit Wayland error\n");
     }
     printf("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
+    // accesses OpenGL and must have it initialized
+    glfwSetFramebufferSizeCallback(window, onWindowResize);
 
-    program = createProgram();
-    if(program == 0) {
+    if(createProgram()) {
         onTerm(window);
         return 1;
     }
@@ -252,14 +271,14 @@ int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFuncti
     // printf("%lu\n", glfwGetTimerValue());
     // printf("%lu\n", glfwGetTimerValue());
 
-    long newTime = glfwGetTimerValue();
-    long oldTime = newTime;
-    long lag = 0;
+    int64_t newTime = (int64_t)glfwGetTimerValue();
+    int64_t oldTime = newTime;
+    int64_t lag = 0;
     while(!glfwWindowShouldClose(window)) {
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
         oldTime = newTime;
-        newTime = glfwGetTimerValue();
+        newTime = (int64_t)glfwGetTimerValue();
         lag += newTime - oldTime;
 
         int ticksPerFrame = 0;
@@ -267,7 +286,7 @@ int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFuncti
             lag -= NANOS_PER_TICK;
 
             controlTick();
-            tick(program);
+            tick();
             ticksPerFrame++;
 
             if(ticksPerFrame >= MAX_TICKS_PER_FRAME) {
@@ -280,7 +299,7 @@ int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFuncti
             }
         }
 
-        renderTick(program, (float)lag / NANOS_PER_TICK);
+        renderTick(program, (float)lag / (float)NANOS_PER_TICK);
 
         glfwSwapBuffers(window);
         glfwPollEvents();
@@ -293,6 +312,6 @@ int startGame(char* name, InitFunction init, TickFunction tick, RenderTickFuncti
     return 0;
 }
 
-GLuint getProgram() {
+GLuint getProgram(void) {
     return program;
-}
+}

+ 21 - 0
src/GameEngine.h

@@ -0,0 +1,21 @@
+#ifndef GAMEENGINE_H
+#define GAMEENGINE_H
+
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+[[maybe_unused]] constexpr int64_t NANOS_PER_TICK = 50'000'000;
+[[maybe_unused]] constexpr int64_t MAX_TICKS_PER_FRAME = 5;
+
+typedef void (*InitFunction)(GLuint);
+typedef void (*TickFunction)(void);
+typedef void (*RenderTickFunction)(GLuint, float);
+typedef void (*WindowResize)(int, int);
+typedef void (*MouseMove)(float, float);
+
+int startGame(
+    const char* name, InitFunction init, TickFunction tick,
+    RenderTickFunction renderTick, WindowResize windowResize, MouseMove move);
+GLuint getProgram(void);
+
+#endif

+ 159 - 143
Main.c → src/Main.c

@@ -1,16 +1,19 @@
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
 #include "Control.h"
 #include "GameEngine.h"
 #include "Vector3D.h"
-#include <math.h>
 
-long tickCounter = 0;
-long renderTickCounter = 0;
+int64_t tickCounter = 0;
+int64_t renderTickCounter = 0;
 
-long tickSum = 0;
-long renderTickSum = 0;
+int64_t tickSum = 0;
+int64_t renderTickSum = 0;
 
-long tickTime = -1;
-long renderTickTime = -1;
+int64_t tickTime = -1;
+int64_t renderTickTime = -1;
 
 typedef struct GameField {
     int id;
@@ -20,16 +23,16 @@ typedef struct GameField {
     float a;
 } GameField;
 
-int amountGameFields = -1;
+size_t amountGameFields = 0;
 GameField* gameFields = NULL;
 
-GLuint vbo = -1;
-GLuint vba = -1;
+GLuint vbo = 0;
+GLuint vba = 0;
 
-int fieldSize = 0;
-int quality = 0;
-int vertices = 0;
-GLsizeiptr size = 0;
+size_t fieldSize = 0;
+size_t quality = 0;
+size_t vertices = 0;
+size_t size = 0;
 GLfloat* data = NULL;
 
 Vector3D oldCamera;
@@ -52,7 +55,7 @@ Vector3D down;
 int selectedIndex = -1;
 int selectedLayer = -1;
 
-GameField* getGameField(int layer, int index) {
+static GameField* getGameField(size_t layer, size_t index) {
     if(layer == 0) {
         return &gameFields[0];
     } else if(layer == 1 + fieldSize) {
@@ -66,11 +69,11 @@ GameField* getGameField(int layer, int index) {
     return &gameFields[index];
 }
 
-float interpolate(float lag, float old, float new) {
+static float interpolate(float lag, float old, float new) {
     return old + lag * (new - old);
 }
 
-void colorField(GameField* field) {
+static void colorField(GameField* field) {
     if(field->id) {
         field->r = 1.0f;
         field->g = 0.0f;
@@ -84,14 +87,14 @@ void colorField(GameField* field) {
     }
 }
 
-void generateSphere() {
-    int triangles = fieldSize * quality;
-    int layers = (2 + fieldSize) * quality;
+static void generateSphere(void) {
+    size_t triangles = fieldSize * quality;
+    size_t layers = (2 + fieldSize) * quality;
 
     if(gameFields == NULL) {
         amountGameFields = 2 + fieldSize * fieldSize;
         gameFields = malloc(sizeof(GameField) * amountGameFields);
-        for(int i = 0; i < amountGameFields; i++) {
+        for(size_t i = 0; i < amountGameFields; i++) {
             gameFields[i].id = (rand() < RAND_MAX / 2) ? 1 : 0;
             colorField(&gameFields[i]);
         }
@@ -103,15 +106,15 @@ void generateSphere() {
         data = malloc(size);
     }
 
-    for(int l = 0; l < layers; l++) {
-        float high1 = cos((M_PI * l) / layers);
-        float high2 = cos((M_PI * (l + 1)) / layers);
+    for(size_t l = 0; l < layers; l++) {
+        float high1 = cosf((float)(M_PI * (double)l) / (float)layers);
+        float high2 = cosf((float)(M_PI * (double)(l + 1)) / (float)layers);
 
-        float texHigh1 = (l % quality) * (1.0f / quality);
-        float texHigh2 = texHigh1 + (1.0f / quality);
+        float texHigh1 = (float)(l % quality) * (1.0f / (float)quality);
+        float texHigh2 = texHigh1 + (1.0f / (float)quality);
 
-        float r1 = sqrt(1 - high1 * high1);
-        float r2 = sqrt(1 - high2 * high2);
+        float r1 = sqrtf(1.0f - high1 * high1);
+        float r2 = sqrtf(1.0f - high2 * high2);
 
         int flag = (l / quality) == 0;
         if(flag) {
@@ -120,14 +123,15 @@ void generateSphere() {
         }
         flag |= (l / quality) >= fieldSize + 1;
 
-        for(int i = 0; i < triangles; i++) {
-            float first = 2 * M_PI * i / triangles;
-            float second = 2 * M_PI * (i + 1) / triangles;
+        for(size_t i = 0; i < triangles; i++) {
+            float first = (float)(2 * M_PI * (double)i) / (float)triangles;
+            float second =
+                (float)(2 * M_PI * (double)(i + 1)) / (float)triangles;
 
-            int off = 27 * 2 * i + l * triangles * 27 * 2;
+            size_t off = 27 * 2 * i + l * triangles * 27 * 2;
 
-            float texWidth1 = (i % quality) * (1.0f / quality);
-            float texWidth2 = texWidth1 + (1.0f / quality);
+            float texWidth1 = (float)(i % quality) * (1.0f / (float)quality);
+            float texWidth2 = texWidth1 + (1.0f / (float)quality);
 
             float r = 0.0f;
             float g = 0.0f;
@@ -142,24 +146,24 @@ void generateSphere() {
                 a = field->a;
             }
 
-            for(int j = 0; j < 54; j += 9) {
+            for(size_t j = 0; j < 54; j += 9) {
                 data[3 + off + j] = r;
                 data[4 + off + j] = g;
                 data[5 + off + j] = b;
                 data[6 + off + j] = a;
             }
 
-            data[0 + off] = r2 * cos(first);
+            data[0 + off] = r2 * cosf(first);
             data[1 + off] = high2;
-            data[2 + off] = r2 * sin(first);
+            data[2 + off] = r2 * sinf(first);
 
-            data[9 + off] = r1 * cos(first);
+            data[9 + off] = r1 * cosf(first);
             data[10 + off] = high1;
-            data[11 + off] = r1 * sin(first);
+            data[11 + off] = r1 * sinf(first);
 
-            data[18 + off] = r1 * cos(second);
+            data[18 + off] = r1 * cosf(second);
             data[19 + off] = high1;
-            data[20 + off] = r1 * sin(second);
+            data[20 + off] = r1 * sinf(second);
 
             if(flag) {
                 data[7 + off] = texHigh2 * 0.5f;
@@ -181,17 +185,17 @@ void generateSphere() {
                 data[26 + off] = texHigh1;
             }
 
-            data[27 + off] = r2 * cos(first);
+            data[27 + off] = r2 * cosf(first);
             data[28 + off] = high2;
-            data[29 + off] = r2 * sin(first);
+            data[29 + off] = r2 * sinf(first);
 
-            data[36 + off] = r1 * cos(second);
+            data[36 + off] = r1 * cosf(second);
             data[37 + off] = high1;
-            data[38 + off] = r1 * sin(second);
+            data[38 + off] = r1 * sinf(second);
 
-            data[45 + off] = r2 * cos(second);
+            data[45 + off] = r2 * cosf(second);
             data[46 + off] = high2;
-            data[47 + off] = r2 * sin(second);
+            data[47 + off] = r2 * sinf(second);
 
             if(flag) {
                 data[34 + off] = texHigh2 * 0.5f;
@@ -215,45 +219,45 @@ void generateSphere() {
         }
     }
 
-    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
+    glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)size, data, GL_STATIC_DRAW);
 }
 
-void updateProjection(float aspect, int program) {
+static void updateProjection(float aspect, GLuint program) {
     float fovY = 60.0f;
     float nearClip = 0.1f;
     float farClip = 1000;
 
-    float q = 1.0f / (float)tan((0.5f * fovY) * M_PI / 180.0f);
+    float q = 1.0f / tanf((0.5f * fovY) * (float)M_PI / 180.0f);
     float a = q / aspect;
     float b = (nearClip + farClip) / (nearClip - farClip);
     float c = (2.0f * nearClip * farClip) / (nearClip - farClip);
 
-    GLfloat data[16];
-    data[0] = a;
-    data[1] = 0.0f;
-    data[2] = 0.0f;
-    data[3] = 0.0f;
+    GLfloat mData[16];
+    mData[0] = a;
+    mData[1] = 0.0f;
+    mData[2] = 0.0f;
+    mData[3] = 0.0f;
 
-    data[4] = 0.0f;
-    data[5] = q;
-    data[6] = 0.0f;
-    data[7] = 0.0f;
+    mData[4] = 0.0f;
+    mData[5] = q;
+    mData[6] = 0.0f;
+    mData[7] = 0.0f;
 
-    data[8] = 0.0f;
-    data[9] = 0.0f;
-    data[10] = b;
-    data[11] = -1.0f;
+    mData[8] = 0.0f;
+    mData[9] = 0.0f;
+    mData[10] = b;
+    mData[11] = -1.0f;
 
-    data[12] = 0.0f;
-    data[13] = 0.0f;
-    data[14] = c;
-    data[15] = 0;
+    mData[12] = 0.0f;
+    mData[13] = 0.0f;
+    mData[14] = c;
+    mData[15] = 0;
 
     GLint loc = glGetUniformLocation(program, "projMatrix");
-    glUniformMatrix4fv(loc, 1, 0, data);
+    glUniformMatrix4fv(loc, 1, 0, mData);
 }
 
-float chooseSmallerPositive(float a, float b) {
+static float chooseSmallerPositive(float a, float b) {
     if(a < 0) {
         return b;
     } else if(b < 0 || b > a) {
@@ -262,13 +266,17 @@ float chooseSmallerPositive(float a, float b) {
     return b;
 }
 
-void updateView(int program, float lag) {
+static void updateView(GLuint program, float lag) {
     // front
-    vectorSetAngles(&front, interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
+    vectorSetAngles(
+        &front, interpolate(lag, oldLengthAngle, lengthAngle),
+        interpolate(lag, oldWidthAngle, widthAngle));
 
     // calculate selected tile
-    float a = camera.x * camera.x + camera.y * camera.y + camera.z * camera.z - 1;
-    float b = 2 * (camera.x * front.x + camera.y * front.y + camera.z * front.z);
+    float a =
+        camera.x * camera.x + camera.y * camera.y + camera.z * camera.z - 1;
+    float b =
+        2 * (camera.x * front.x + camera.y * front.y + camera.z * front.z);
     float c = front.x * front.x + front.y * front.y + front.z * front.z;
 
     float p = b / c;
@@ -276,21 +284,22 @@ void updateView(int program, float lag) {
 
     float det = p * p * 0.25f - q;
     if(det >= 0) {
-        float k = sqrt(det);
+        float k = sqrtf(det);
         k = chooseSmallerPositive(-0.5f * p + k, -0.5f * p - k);
         if(k >= 0) {
             Vector3D v;
             vectorSetTo(&v, &camera);
             vectorAddMul(&v, &front, k);
 
-            selectedLayer = (asin(-v.y) + M_PI_2) * M_1_PI * (2 + fieldSize);
-            if(selectedLayer == 2 + fieldSize) {
-                selectedLayer = fieldSize + 1;
+            selectedLayer = (int)((asin((double)-v.y) + M_PI_2) * M_1_PI *
+                                  (double)(2 + fieldSize));
+            if(selectedLayer == (int)(2 + fieldSize)) {
+                selectedLayer = (int)(fieldSize + 1);
             }
 
-            float tan = atan2(v.z, v.x) * M_1_PI * 0.5;
+            float tan = atan2f(v.z, v.x) * (float)M_1_PI * 0.5f;
             tan += (tan < 0);
-            selectedIndex = tan * fieldSize;
+            selectedIndex = (int)(tan * (float)fieldSize);
         }
     }
 
@@ -313,39 +322,41 @@ void updateView(int program, float lag) {
     // down
     vectorSetToInverse(&down, &up);
 
-    GLfloat data[16];
-    data[0] = right.x;
-    data[1] = up.x;
-    data[2] = back.x;
-    data[3] = 0.0f;
+    GLfloat mData[16];
+    mData[0] = right.x;
+    mData[1] = up.x;
+    mData[2] = back.x;
+    mData[3] = 0.0f;
 
-    data[4] = right.y;
-    data[5] = up.y;
-    data[6] = back.y;
-    data[7] = 0.0f;
+    mData[4] = right.y;
+    mData[5] = up.y;
+    mData[6] = back.y;
+    mData[7] = 0.0f;
 
-    data[8] = right.z;
-    data[9] = up.z;
-    data[10] = back.z;
-    data[11] = 0.0f;
+    mData[8] = right.z;
+    mData[9] = up.z;
+    mData[10] = back.z;
+    mData[11] = 0.0f;
 
     Vector3D interCam;
     vectorSetTo(&interCam, &oldCamera);
     vectorAddMul(&interCam, &camera, lag);
     vectorAddMul(&interCam, &oldCamera, -lag);
 
-    data[12] = vectorDotInverse(&right, &interCam);
-    data[13] = vectorDotInverse(&up, &interCam);
-    data[14] = vectorDotInverse(&back, &interCam);
-    data[15] = 1.0f;
+    mData[12] = vectorDotInverse(&right, &interCam);
+    mData[13] = vectorDotInverse(&up, &interCam);
+    mData[14] = vectorDotInverse(&back, &interCam);
+    mData[15] = 1.0f;
 
-    // printf("%f %f %f %f\n", data[0], data[4], data[8], data[12]);
-    // printf("%f %f %f %f\n", data[1], data[5], data[9], data[13]);
-    // printf("%f %f %f %f\n", data[2], data[6], data[10], data[14]);
-    // printf("%f %f %f %f\n", data[3], data[7], data[11], data[15]);
+    // printf("%f %f %f %f\n", mData[0], mData[4], mData[8], mData[12]);
+    // printf("%f %f %f %f\n", mData[1], mData[5], mData[9], mData[13]);
+    // printf("%f %f %f %f\n", mData[2], mData[6], mData[10], mData[14]);
+    // printf("%f %f %f %f\n", mData[3], mData[7], mData[11], mData[15]);
 
     // front
-    vectorSetAngles(&front, interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
+    vectorSetAngles(
+        &front, interpolate(lag, oldLengthAngle, lengthAngle),
+        interpolate(lag, oldWidthAngle, widthAngle));
     front.y = 0;
     vectorNormalize(&front);
 
@@ -367,10 +378,10 @@ void updateView(int program, float lag) {
     vectorSetToInverse(&down, &up);
 
     GLint loc = glGetUniformLocation(program, "viewMatrix");
-    glUniformMatrix4fv(loc, 1, 0, data);
+    glUniformMatrix4fv(loc, 1, 0, mData);
 }
 
-void init(int program) {
+static void init(GLuint program) {
     printf("Init\n");
 
     glGenBuffers(1, &vbo);
@@ -397,7 +408,7 @@ void init(int program) {
     updateProjection(4.0f / 3.0f, program);
 }
 
-void invertField(int layer, int index) {
+static void invertField(size_t layer, size_t index) {
     GameField* field = getGameField(layer, index);
     if(field != NULL) {
         field->id = 1 - field->id;
@@ -405,17 +416,17 @@ void invertField(int layer, int index) {
     }
 }
 
-void invertAt(int layer, int index) {
+static void invertAt(size_t layer, size_t index) {
     if(layer == 0) {
         invertField(layer, index);
         layer++;
-        for(int i = 0; i < fieldSize; i++) {
+        for(size_t i = 0; i < fieldSize; i++) {
             invertField(layer, i);
         }
     } else if(layer == fieldSize + 1) {
         invertField(layer, index);
         layer--;
-        for(int i = 0; i < fieldSize; i++) {
+        for(size_t i = 0; i < fieldSize; i++) {
             invertField(layer, i);
         }
     } else {
@@ -427,11 +438,11 @@ void invertAt(int layer, int index) {
     }
 }
 
-void tick(int program) {
+static void tick(void) {
     if(tickTime == -1) {
-        tickTime = glfwGetTimerValue();
+        tickTime = (int64_t)glfwGetTimerValue();
     } else {
-        long time = glfwGetTimerValue();
+        int64_t time = (int64_t)glfwGetTimerValue();
         tickSum += time - tickTime;
         tickTime = time;
 
@@ -462,8 +473,9 @@ void tick(int program) {
         vectorAddMul(&camera, &down, factor);
     }
 
-    if(mouseIsJustDown(GLFW_MOUSE_BUTTON_1) && selectedLayer != -1 && selectedIndex != -1) {
-        invertAt(selectedLayer, selectedIndex);
+    if(mouseIsJustDown(GLFW_MOUSE_BUTTON_1) && selectedLayer != -1 &&
+       selectedIndex != -1) {
+        invertAt((size_t)selectedLayer, (size_t)selectedIndex);
         generateSphere();
         selectedLayer = -1;
         selectedIndex = -1;
@@ -472,9 +484,9 @@ void tick(int program) {
     widthAngle -= mouseY * 0.1f;
     lengthAngle -= mouseX * 0.1f;
 
-    if(widthAngle >= 89.5) {
+    if(widthAngle >= 89.5f) {
         widthAngle = 89.5f;
-    } else if(widthAngle <= -89.5) {
+    } else if(widthAngle <= -89.5f) {
         widthAngle = -89.5f;
     }
 
@@ -482,11 +494,11 @@ void tick(int program) {
     mouseY = 0.0f;
 }
 
-void renderTick(int program, float lag) {
+static void renderTick(GLuint program, float lag) {
     if(renderTickTime == -1) {
-        renderTickTime = glfwGetTimerValue();
+        renderTickTime = (int64_t)glfwGetTimerValue();
     } else {
-        long time = glfwGetTimerValue();
+        int64_t time = (int64_t)glfwGetTimerValue();
         renderTickSum += time - renderTickTime;
         renderTickTime = time;
 
@@ -497,25 +509,25 @@ void renderTick(int program, float lag) {
 
     glBindBuffer(GL_ARRAY_BUFFER, vbo);
     glBindVertexArray(vba);
-    glDrawArrays(GL_TRIANGLES, 0, vertices);
+    glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices);
 }
 
-void onWindowResize(int width, int height) {
-    updateProjection((float)width / height, getProgram());
+static void onWindowResize(int width, int height) {
+    updateProjection((float)width / (float)height, getProgram());
 }
 
-void onMouseMove(float x, float y) {
+static void onMouseMove(float x, float y) {
     mouseX += x;
     mouseY += y;
 }
 
-int readPositiveInt(int from, int to, char* message, int error) {
+static int readPositiveInt(int from, int to, char* message, int error) {
     fputs(message, stdout);
     fflush(stdout);
 
-    int size = 16;
-    int index = 0;
-    char* buffer = malloc(sizeof(char) * size);
+    size_t bufferSize = 16;
+    size_t index = 0;
+    char* buffer = malloc(sizeof(char) * bufferSize);
 
     while(1) {
         int c = fgetc(stdin);
@@ -524,7 +536,7 @@ int readPositiveInt(int from, int to, char* message, int error) {
             return error;
         } else if(c == '\n') {
             int number = 0;
-            int i = 0;
+            size_t i = 0;
             while(i < index) {
                 int n = buffer[i] - '0';
                 if(n < 0 || n > 9) {
@@ -548,34 +560,36 @@ int readPositiveInt(int from, int to, char* message, int error) {
             continue;
         }
 
-        if(index >= size) {
-            size *= 2;
-            buffer = realloc(buffer, sizeof(char) * size);
+        if(index >= bufferSize) {
+            bufferSize *= 2;
+            buffer = realloc(buffer, sizeof(char) * bufferSize);
         }
-        buffer[index] = c;
+        buffer[index] = (char)c;
         index++;
     }
 }
 
-int main() {
-    /*fieldSize = readPositiveInt(5, 20, "Select a game size from 5 to 20:\n", 0);
-    if(fieldSize == 0)
+int main(void) {
+    (void)readPositiveInt;
+    /*fieldSize = readPositiveInt(5, 20, "Select a game size from 5 to 20:\n",
+    0); if(fieldSize == 0)
     {
         printf("Cannot read from stdin\n");
         return EXIT_SUCCESS;
     }
-    quality = readPositiveInt(1, 9, "Select a rendering quality from 1 to 9:\n", 0);
-    if(quality == 0)
+    quality = readPositiveInt(1, 9, "Select a rendering quality from 1 to 9:\n",
+    0); if(quality == 0)
     {
         printf("Cannot read from stdin\n");
         return EXIT_SUCCESS;
-    }*/
+    }/*/
     fieldSize = 5;
     quality = 8;
 
-    if(startGame("Test Game", init, tick, renderTick, onWindowResize, onMouseMove)) {
+    if(startGame(
+           "Test Game", init, tick, renderTick, onWindowResize, onMouseMove)) {
         fprintf(stderr, "Exited with error\n");
-        // return EXIT_FAILURE;
+        return EXIT_FAILURE;
     }
 
     if(data != NULL) {
@@ -590,11 +604,13 @@ int main() {
 
     printf("_______________TPS_______________\n");
     printf("%ld  %ld  %ld\n", tickCounter, tickSum, tickTime);
-    printf("%lf\n", (double)tickSum / tickCounter);
-    printf("%lf\n", 1000000000.0 * tickCounter / tickSum);
+    printf("%lf\n", (double)tickSum / (double)tickCounter);
+    printf("%lf\n", 1000000000.0 * (double)tickCounter / (double)tickSum);
     printf("_______________FPS_______________\n");
     printf("%ld  %ld  %ld\n", renderTickCounter, renderTickSum, renderTickTime);
-    printf("%lf\n", (double)renderTickSum / renderTickCounter);
-    printf("%lf\n", 1000000000.0 * renderTickCounter / renderTickSum);
+    printf("%lf\n", (double)renderTickSum / (double)renderTickCounter);
+    printf(
+        "%lf\n",
+        1000000000.0 * (double)renderTickCounter / (double)renderTickSum);
     return EXIT_SUCCESS;
-}
+}

+ 8 - 5
Vector3D.c → src/Vector3D.c

@@ -1,4 +1,5 @@
 #include "Vector3D.h"
+
 #include <math.h>
 
 void vectorSet(Vector3D* v, float x, float y, float z) {
@@ -26,8 +27,8 @@ void vectorSetMul(Vector3D* v1, Vector3D* v2, float f) {
 }
 
 void vectorSetAngles(Vector3D* v, float lengthAngle, float widthAngle) {
-    lengthAngle = lengthAngle * M_PI / 180.0f;
-    widthAngle = widthAngle * M_PI / 180.0f;
+    lengthAngle = lengthAngle * (float)M_PI / 180.0f;
+    widthAngle = widthAngle * (float)M_PI / 180.0f;
     v->x = cosf(widthAngle) * sinf(lengthAngle);
     v->z = cosf(widthAngle) * cosf(lengthAngle);
     v->y = sinf(widthAngle);
@@ -62,7 +63,9 @@ void vectorCross(Vector3D* v, float x, float y, float z) {
 }
 
 void vectorCrossWith(Vector3D* v1, Vector3D* v2) {
-    vectorSet(v1, v1->y * v2->z - v1->z * v2->y, v1->z * v2->x - v1->x * v2->z, v1->x * v2->y - v1->y * v2->x);
+    vectorSet(
+        v1, v1->y * v2->z - v1->z * v2->y, v1->z * v2->x - v1->x * v2->z,
+        v1->x * v2->y - v1->y * v2->x);
 }
 
 void vectorNormalize(Vector3D* v) {
@@ -73,7 +76,7 @@ void vectorNormalize(Vector3D* v) {
 }
 
 float vectorLength(Vector3D* v) {
-    return sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
+    return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
 }
 
 float vectorDot(Vector3D* v1, Vector3D* v2) {
@@ -82,4 +85,4 @@ float vectorDot(Vector3D* v1, Vector3D* v2) {
 
 float vectorDotInverse(Vector3D* v1, Vector3D* v2) {
     return v1->x * (-v2->x) + v1->y * (-v2->y) + v1->z * (-v2->z);
-}
+}

+ 14 - 1
Vector3D.h → src/Vector3D.h

@@ -1,6 +1,19 @@
 #ifndef VECTOR3D_H
 #define VECTOR3D_H
 
+#ifndef M_PI
+#define M_PI 3.14159265358979323846
+#endif
+#ifndef M_PI_2
+#define M_PI_2 1.57079632679489661923
+#endif
+#ifndef M_PI_4
+#define M_PI_4 0.78539816339744830962
+#endif
+#ifndef M_1_PI
+#define M_1_PI 0.31830988618379067154
+#endif
+
 typedef struct Vector3D {
     float x;
     float y;
@@ -27,4 +40,4 @@ float vectorLength(Vector3D* v);
 float vectorDot(Vector3D* v1, Vector3D* v2);
 float vectorDotInverse(Vector3D* v1, Vector3D* v2);
 
-#endif
+#endif