Skip to content

Commit f9b552c

Browse files
committed
Fix build on Windows.
1 parent 6721997 commit f9b552c

13 files changed

Lines changed: 75 additions & 57 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function(tiny_engine_configure_target TARGET_NAME)
4646

4747
# More warnings.
4848
if(MSVC)
49-
target_compile_options(${TARGET_NAME} PUBLIC /W3 /WX)
49+
target_compile_options(${TARGET_NAME} PUBLIC /W3 /WX /wd4996) # disable "unsafe function" warning
5050
else()
5151
target_compile_options(${TARGET_NAME} PUBLIC -Wall -Wextra -Werror -Wconversion -Wno-unused-but-set-parameter)
5252
endif()

res/engine/shader/model.frag.glsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ void main(void) {
88
// Normals may be unnormalized after the rasterization (when they are interpolated).
99
vec3 frag_normal_unit = normalize(frag_normal);
1010

11+
vec4 out_color = color;
12+
1113
// TODO: dummy usage
12-
if (frag_normal.x < -5.0f) {
13-
discard;
14+
if (frag_normal.x < -5.0 || frag_uv.x < 0.5) {
15+
out_color.r += 0.5;
1416
}
1517

16-
gl_FragColor = color;
18+
gl_FragColor = out_color;
1719
}

res/engine/shader/model.vert.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ uniform mat4 world_mat;
1111
uniform mat3 normal_mat;
1212

1313
void main(void) {
14-
vec4 world_pos = world_mat * vec4(pos, 1.0F);
14+
vec4 world_pos = world_mat * vec4(pos, 1.0);
1515
gl_Position = view_proj_mat * world_pos;
1616

1717
frag_pos = world_pos.xyz;

src/engine_lib/include/math_funcs.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
*/
1111
void
1212
math_make_rotation_mat(vec3 rotation_deg, mat4 out) {
13-
vec3 z = GLM_VEC3_ZERO;
14-
vec3 y = GLM_VEC3_ZERO;
15-
vec3 x = GLM_VEC3_ZERO;
16-
z[2] = 1.0f;
17-
y[1] = 1.0f;
18-
x[0] = 1.0f;
13+
vec3 z;
14+
vec3 y;
15+
vec3 x;
16+
glm_vec3_make((vec3){0.0f, 0.0f, 1.0f}, z);
17+
glm_vec3_make((vec3){0.0f, 1.0f, 0.0f}, y);
18+
glm_vec3_make((vec3){1.0f, 0.0f, 0.0f}, x);
1919

2020
mat4 z_rot;
2121
glm_rotate_make(z_rot, glm_rad(rotation_deg[2]), z);

src/engine_lib/src/game/model.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ model_create(const char* path_to_geo) {
8383
if (path_to_geo == NULL) {
8484
model->path_to_geo = NULL;
8585
} else {
86-
const unsigned long path_len = strlen(path_to_geo);
86+
const size_t path_len = strlen(path_to_geo);
8787
model->path_to_geo = malloc(sizeof(char) * (path_len + 1));
8888
memcpy(model->path_to_geo, path_to_geo, path_len);
8989
model->path_to_geo[path_len] = 0;
@@ -185,7 +185,7 @@ model_set_custom_vert_shader(te_model* model, const char* vert_relative_path) {
185185

186186
free(model->custom_vert_relative_path);
187187

188-
const unsigned long len = strlen(vert_relative_path);
188+
const size_t len = strlen(vert_relative_path);
189189
model->custom_vert_relative_path = malloc(sizeof(char) * (len + 1));
190190
memcpy(model->custom_vert_relative_path, vert_relative_path, sizeof(char) * len);
191191
model->custom_vert_relative_path[len] = 0;
@@ -199,7 +199,7 @@ model_set_custom_frag_shader(te_model* model, const char* frag_relative_path) {
199199

200200
free(model->custom_frag_relative_path);
201201

202-
const unsigned long len = strlen(frag_relative_path);
202+
const size_t len = strlen(frag_relative_path);
203203
model->custom_frag_relative_path = malloc(sizeof(char) * (len + 1));
204204
memcpy(model->custom_frag_relative_path, frag_relative_path, sizeof(char) * len);
205205
model->custom_frag_relative_path[len] = 0;

src/engine_lib/src/io/filesystem.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#if defined(WIN32)
88
#define NOMINMAX
99
#include <windows.h>
10+
#include <direct.h>
1011
#define mkdir(dir, mode) _mkdir(dir)
1112
#elif __linux__
1213
#include <sys/stat.h>
@@ -16,19 +17,14 @@
1617

1718
void
1819
filesystem_ensure_dirs_exist(const char* file_path) {
19-
const char pathSeparator =
20-
#if defined(WIN32)
21-
'\\';
22-
#else
23-
'/';
24-
#endif
20+
const char pathSeparator = '/'; // we convert \\ to / on windows
2521

2622
char* dir_path = malloc(strlen(file_path) + 1);
2723

2824
char* next_sep = strchr(file_path, pathSeparator);
2925
while (next_sep != NULL) {
30-
long dir_path_len = next_sep - file_path;
31-
memcpy(dir_path, file_path, (unsigned long)dir_path_len);
26+
const size_t dir_path_len = next_sep - file_path;
27+
memcpy(dir_path, file_path, dir_path_len);
3228
dir_path[dir_path_len] = 0;
3329

3430
if (!filesystem_does_path_exists(dir_path)) {

src/engine_lib/src/io/log.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <time.h>
88
#include "io/filesystem.h"
99
#include "io/paths.h"
10+
#include "misc/error.h"
1011

1112
static unsigned int error_count_logged = 0;
1213
static unsigned int warn_count_logged = 0;
@@ -40,8 +41,8 @@ prv_log(enum te_log_category category, const char* message, char* filepath, int
4041
}
4142

4243
// Find filename start in the filepath.
43-
unsigned long filename_start = 0;
44-
for (unsigned long i = strlen(filepath) - 1; i > 0; i--) {
44+
size_t filename_start = 0;
45+
for (size_t i = strlen(filepath) - 1; i > 0; i--) {
4546
if (filepath[i] == '/' || filepath[i] == '\\') {
4647
filename_start = i + 1;
4748
break;
@@ -57,6 +58,10 @@ prv_log(enum te_log_category category, const char* message, char* filepath, int
5758
const char* path_to_log_file = paths_get_log_file();
5859
filesystem_ensure_dirs_exist(path_to_log_file);
5960
FILE* log_file = fopen(path_to_log_file, "a");
61+
if (log_file == NULL) {
62+
printf("failed to open log file");
63+
abort();
64+
}
6065

6166
fprintf(log_file, "%s %s\n", log_prefix, message);
6267
#if defined(DEBUG)
@@ -74,9 +79,9 @@ prv_log_fmt(enum te_log_category category, char* filepath, int line, const char*
7479
va_list args_copy;
7580
va_copy(args_copy, args);
7681

77-
unsigned long size = (unsigned long)vsnprintf(NULL, 0, fmt, args);
78-
char* message = malloc(size + 1ul);
79-
memset(message, 0, size + 1ul);
82+
int size = vsnprintf(NULL, 0, fmt, args);
83+
char* message = malloc(size + 1);
84+
memset(message, 0, size + 1l);
8085

8186
vsprintf(message, fmt, args_copy);
8287

src/engine_lib/src/io/paths.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include "misc/error.h"
88
#include "misc/globals.h"
99

10+
#if defined(WIN32)
11+
#define NOMINMAX
12+
#include <Windows.h>
13+
#include <KnownFolders.h>
14+
#include <Shlobj.h>
15+
#endif
16+
1017
static char cached_path_to_config_dir[2048] = {0};
1118
static char cached_path_to_log_file[2048] = {0};
1219

@@ -15,26 +22,26 @@ paths_get_config_dir(void) {
1522
if (cached_path_to_config_dir[0] == 0) {
1623
#if defined(WIN32)
1724
PWSTR path_tmp = NULL;
18-
const HRESULT result = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &path_tmp);
25+
const HRESULT result = SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path_tmp);
1926
if (result != S_OK) {
2027
CoTaskMemFree(path_tmp);
2128
show_error_and_abort("failed to query config dir path");
2229
}
2330

2431
// Copy path and replace slashes.
2532
char path_buff[256] = {0};
26-
const unsigned long path_len = strlen(path_tmp);
33+
const size_t path_len = wcslen(path_tmp);
2734
if (path_len > 256) {
2835
show_error_and_abort("path to AppData folder is too long");
2936
}
30-
for (unsigned long i = 0; i < path_len; i++) {
37+
for (size_t i = 0; i < path_len; i++) {
3138
if (path_tmp[i] == '\\') {
3239
path_buff[i] = '/';
3340
} else {
34-
path_buff[i] = path_tmp[i];
41+
path_buff[i] = (char)path_tmp[i];
3542
}
3643
}
37-
CoTaskMemFree(pPathTmp);
44+
CoTaskMemFree(path_tmp);
3845

3946
sprintf(cached_path_to_config_dir, "%s/tiny_engine/%s/config/", &path_buff[0],
4047
globals_get_app_name());
@@ -65,26 +72,26 @@ paths_get_log_file(void) {
6572
if (cached_path_to_log_file[0] == 0) {
6673
#if defined(WIN32)
6774
PWSTR path_tmp = NULL;
68-
const HRESULT result = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &path_tmp);
75+
const HRESULT result = SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path_tmp);
6976
if (result != S_OK) {
7077
CoTaskMemFree(path_tmp);
7178
show_error_and_abort("failed to query config dir path");
7279
}
7380

7481
// Copy path and replace slashes.
7582
char path_buff[256] = {0};
76-
const unsigned long path_len = strlen(path_tmp);
83+
const size_t path_len = wcslen(path_tmp);
7784
if (path_len > 256) {
7885
show_error_and_abort("path to AppData folder is too long");
7986
}
80-
for (unsigned long i = 0; i < path_len; i++) {
87+
for (size_t i = 0; i < path_len; i++) {
8188
if (path_tmp[i] == '\\') {
8289
path_buff[i] = '/';
8390
} else {
84-
path_buff[i] = path_tmp[i];
91+
path_buff[i] = (char)path_tmp[i];
8592
}
8693
}
87-
CoTaskMemFree(pPathTmp);
94+
CoTaskMemFree(path_tmp);
8895

8996
sprintf(cached_path_to_log_file, "%s/tiny_engine/%s/log.txt", &path_buff[0], globals_get_app_name());
9097
#elif __linux__
@@ -111,7 +118,7 @@ paths_get_log_file(void) {
111118

112119
char*
113120
paths_prepend_res_to_path(const char* relative_path) {
114-
const unsigned long len = strlen(relative_path);
121+
const size_t len = strlen(relative_path);
115122

116123
char* new_path = malloc(sizeof(char) * (len + 4 + 1));
117124

src/engine_lib/src/misc/error.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ prv_error_create(const char* message, char* file, int line) {
1818
char location_info[512] = {0};
1919
snprintf(&location_info[0], 512, " (%s:%d)", file, line);
2020

21-
const unsigned long message_len = strlen(message);
22-
const unsigned long location_info_len = strlen(location_info);
23-
const unsigned long full_message_len = message_len + location_info_len + 1ul;
21+
const size_t message_len = strlen(message);
22+
const size_t location_info_len = strlen(location_info);
23+
const size_t full_message_len = message_len + location_info_len + 1;
2424

2525
// Init error.
2626
te_error* err = malloc(sizeof(te_error));

src/engine_lib/src/misc/globals.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ globals_get_app_name(void) {
3434
#endif
3535

3636
// Find last slash in the path.
37-
unsigned long path_len = strlen(buffer);
38-
unsigned long last_slash_pos = path_len;
39-
for (unsigned long i = path_len - 1; i > 0; i--) {
37+
const size_t path_len = strlen(buffer);
38+
size_t last_slash_pos = path_len;
39+
for (size_t i = path_len - 1; i > 0; i--) {
4040
if (buffer[i] == '/' || buffer[i] == '\\') {
4141
last_slash_pos = i;
4242
break;
@@ -47,8 +47,14 @@ globals_get_app_name(void) {
4747
}
4848

4949
// Save app name.
50-
for (unsigned long src = last_slash_pos + 1, dst = 0;
51-
src < path_len && dst < (unsigned long)max_app_name_len; src++, dst++) {
50+
for (size_t src = last_slash_pos + 1, dst = 0;
51+
src < path_len && dst < max_app_name_len; src++, dst++) {
52+
#if defined(WIN32)
53+
if (buffer[src] == '.') {
54+
// don't copy ".exe"
55+
break;
56+
}
57+
#endif
5258
cached_app_name[dst] = buffer[src];
5359
}
5460
}

0 commit comments

Comments
 (0)