diff --git a/build/build.sh b/build/build.sh index bb270e8..0f9b31c 100755 --- a/build/build.sh +++ b/build/build.sh @@ -1,3 +1,7 @@ SCRIPT_REL_DIR=$(dirname "${BASH_SOURCE[0]}") -$SCRIPT_REL_DIR/build_.sh debug osx arm64 -# $SCRIPT_REL_DIR/build_.sh debug wasm intel \ No newline at end of file +$SCRIPT_REL_DIR/build_.sh debug raspi arm64 +# $SCRIPT_REL_DIR/build_.sh debug wasm intel + +# pushd "run_tree/raspi/arm64/debug" +# clang -o lumenarium /home/pi/dev/Lumenarium/src_v2/platform/raspi/lumenarium_first_raspi.c -lm +# popd \ No newline at end of file diff --git a/build/build_.sh b/build/build_.sh index 626c882..eb65f7a 100755 --- a/build/build_.sh +++ b/build/build_.sh @@ -145,8 +145,8 @@ CompilerFlags_wasm+=" -Wl,--export-all" # CompilerFlags_linux="" -CompilerFlags_raspi="--target=arm-linux-gnueabihf" #target -CompilerFlags_raspi+="" +CompilerFlags_raspi="" # "--target=arm-rpi-linux-gnueabihf" # "--target=arm-linux-gnueabihf" #target +CompilerFlags_raspi+="-lm" # link with local system math libraries CompilerFlags_DEBUG_win32="" @@ -158,7 +158,10 @@ CompilerFlags_DEBUG_win32+=" -DDEBUG" # CompilerFlags_DEBUG="-O0" CompilerFlags_DEBUG+=" -g" # CompilerFlags_DEBUG+=" -DDEBUG" # -CompilerFlags_DEBUG+=" -fsanitize=address" #address sanitizer +if [ "${PLATFORM}" != "raspi" ] +then + CompilerFlags_DEBUG+=" -fsanitize=address" #address sanitizer +fi CompilerFlags_PROD=" -O3" @@ -358,4 +361,4 @@ else fi echo "Finished..." -popdir \ No newline at end of file +popdir diff --git a/run_tree/raspi/arm64/debug/lumenarium b/run_tree/raspi/arm64/debug/lumenarium new file mode 100755 index 0000000..df9fc5a Binary files /dev/null and b/run_tree/raspi/arm64/debug/lumenarium differ diff --git a/src_v2/core/lumenarium_core_time.h b/src_v2/core/lumenarium_core_time.h index 5a47360..27e57d0 100644 --- a/src_v2/core/lumenarium_core_time.h +++ b/src_v2/core/lumenarium_core_time.h @@ -1,6 +1,11 @@ #if !defined(LUMENARIUM_CORE_TIME_H) #define LUMENARIUM_CORE_TIME_H +// Ticks is a container that each os will use differently +// the value of tick should never be accessed outside of +// lumenarium_core_time.h or the various os_time implementations. +// Instead, treat it as an opaque struct and use the values returned +// from the functions defined below. typedef struct { s64 value; } Ticks; internal Ticks get_ticks_elapsed(Ticks start, Ticks end); diff --git a/src_v2/lumenarium_first.c b/src_v2/lumenarium_first.c index 3441f9d..1561c32 100644 --- a/src_v2/lumenarium_first.c +++ b/src_v2/lumenarium_first.c @@ -14,8 +14,8 @@ lumenarium_init(Editor_Desc* ed_desc) { App_State* state = 0; - permanent = bump_allocator_create_reserve(GB(2)); - global_scratch_ = bump_allocator_create_reserve(GB(4)); + permanent = bump_allocator_create_reserve(GB(1)); + global_scratch_ = bump_allocator_create_reserve(GB(1)); run_tests(); diff --git a/src_v2/lumenarium_tests.cpp b/src_v2/lumenarium_tests.cpp index da86383..747611c 100644 --- a/src_v2/lumenarium_tests.cpp +++ b/src_v2/lumenarium_tests.cpp @@ -192,6 +192,7 @@ run_tests() assert(run_tree_path_nullterm.len > 0); assert(os_pwd_set(run_tree_path_nullterm)); + // testing file io File_Handle f = os_file_open(lit_str("text.txt"), FileAccess_Read | FileAccess_Write, FileCreate_OpenExisting); File_Info i = os_file_get_info(f, scratch.a); @@ -203,7 +204,7 @@ run_tests() Data d1 = { s.str, s.len }; bool r = os_file_write_all(f, d1); assert(r); - + #if 0 // TODO(PS): these were causing startup problems but you weren't focusing on // threads/ When you build something multithreaded come back here and diff --git a/src_v2/platform/linux/lumenarium_linux_file.h b/src_v2/platform/linux/lumenarium_linux_file.h index c18a954..a44f521 100644 --- a/src_v2/platform/linux/lumenarium_linux_file.h +++ b/src_v2/platform/linux/lumenarium_linux_file.h @@ -4,48 +4,190 @@ File_Async_Job_System os_file_jobs_init() { - return (File_Async_Job_System){}; + open_files_init(); + File_Async_Job_System result = file_async_jobs_init(os_file_async_work_on_job); + return result; } File_Handle os_file_open(String path, File_Access_Flags flags_access, File_Create_Flags flags_create) { - invalid_code_path; - return (File_Handle){}; + File_Handle result = {}; + + s32 flags = 0; + if(has_flag_exact(flags_access, (FileAccess_Read | FileAccess_Write))) + { + add_flag(flags, O_RDWR); + } + else + { + if (has_flag(flags_access, FileAccess_Read)) { + add_flag(flags, O_RDONLY); + } + else if (has_flag(flags_access, FileAccess_Write)) + { + add_flag(flags, O_WRONLY); + } + else + { + return result; + } + } + + switch (flags_create) + { + case FileCreate_New: { add_flag(flags, O_CREAT | O_EXCL ); } break; + case FileCreate_CreateAlways: { add_flag(flags, O_CREAT); } break; + case FileCreate_OpenExisting: { /* add_flag(flags, O_); */ } break; + case FileCreate_OpenAlways: { /* add_flag(flags, O_); */ } break; + invalid_default_case; + } + + s32 file_handle = open((char*)path.str, flags); + if (file_handle >= 0) + { + result = open_files_put_handle(file_handle, path); + } + else + { + s32 errsv = errno; + printf("Error: os_file_open - %d\n", errsv); + printf("\tAttempting to open: %.*s\n", str_varg(path)); + printf("\tFlags: %u %u\n", flags_access, flags_create); + } + return result; } void os_file_close(File_Handle file_handle) { - invalid_code_path; + s32 os_handle = open_files_get_handle(file_handle); + if (close(os_handle) != -1) + { + open_files_rem_file(file_handle); + } + else + { + s32 errsv = errno; + printf("Error: os_file_close - %d\n", errsv); + } } File_Info os_file_get_info(File_Handle file_handle, Allocator* allocator) { - invalid_code_path; - return (File_Info){}; + File_Info info = {}; + s32 os_handle = open_files_get_handle(file_handle); + if (os_handle != -1) + { + String path = open_files_get_path(file_handle); + + struct stat os_info = {}; + if (fstat(os_handle, &os_info) != -1) + { + info.path = string_copy(path, allocator); + info.path_abs = allocator_alloc_string(allocator, PATH_MAX); + if (realpath((char*)path.str, (char*)info.path_abs.str) != 0) + { + info.path_abs.len = c_str_len((char*)info.path_abs.str); + } + else + { + s32 errsv = errno; + printf("Error - os_file_get_info - %d - realpath\n", errsv); + } + info.size = (u64)os_info.st_size; + if (S_ISDIR(os_info.st_mode)) + { + add_flag(info.flags, FileFlag_IsDir); + } + else if (!S_ISREG(os_info.st_mode)) + { + printf("Error - os_file_get_info - stat-ing a handle that is not a directory or a file\n"); + } + } + else + { + s32 errsv = errno; + printf("Error: os_file_get_info - %d\n", errsv); + } + } + return info; } Data os_file_read_all(File_Handle file_handle, Allocator* allocator) { - invalid_code_path; - return (Data){}; + Data result = {}; + s32 os_handle = open_files_get_handle(file_handle); + if (os_handle == -1) return result; + + // get file size + s32 offset = lseek(os_handle, 0, SEEK_END); + if (offset == -1) + { + s32 errsv = errno; + printf("Error: os_file_read_all:lseek - %d\n", errsv); + return result; + } + lseek(os_handle, 0, SEEK_SET); + + result.base = allocator_alloc(allocator, offset + 1); + result.size = offset + 1; + + s32 bytes_read = read(os_handle, result.base, result.size); + if (bytes_read == (result.size - 1)) + { + result.base[bytes_read] = 0; // null term + } + else if (bytes_read >= 0) + { + printf("Error: os_file_read:read - whole file not read.\n"); + } + else if (bytes_read == -1) + { + s32 errsv = errno; + printf("Error: os_file_read_all:read - %d\n", errsv); + } + + return result; +} + +bool +os_file_write_(s32 os_handle, Data file_data) +{ + s32 size_written = write(os_handle, file_data.base, file_data.size); + if (size_written > 0 && size_written != file_data.size) + { + printf("Error: os_file_write_all:write - whole file not written\n"); + return true; + } + else if (size_written < 0) + { + linux_err_print("write"); + return false; + } + + return true; } bool os_file_write_all(File_Handle file_handle, Data file_data) { - invalid_code_path; - return false; + s32 os_handle = open_files_get_handle(file_handle); + if (os_handle == -1) return false; + + lseek(os_handle, 0, SEEK_SET); + return os_file_write_(os_handle, file_data); } bool os_file_write(File_Handle file_handle, Data file_data) { - invalid_code_path; - return false; + s32 os_handle = open_files_get_handle(file_handle); + if (os_handle == -1) return false; + + return os_file_write_(os_handle, file_data); } diff --git a/src_v2/platform/linux/lumenarium_linux_network.h b/src_v2/platform/linux/lumenarium_linux_network.h index 9472aab..a4eee9e 100644 --- a/src_v2/platform/linux/lumenarium_linux_network.h +++ b/src_v2/platform/linux/lumenarium_linux_network.h @@ -4,8 +4,19 @@ Socket_Handle os_socket_create(s32 domain, s32 type, s32 protocol) { - invalid_code_path; - return (Socket_Handle){0}; + Socket_Handle result = {}; + OS_SOCKET_TYPE sock = socket(domain, type, protocol); + if (sock == -1) { + perror("Error: os_socket_create\n"); + return result; + } + + result = open_sockets_put(sock); + if (result.value == 0) + { + fprintf(stderr, "Error: os_socket_create = not enough room in open_sockets\n"); + } + return result; } bool @@ -53,14 +64,31 @@ os_socket_send() s32 os_socket_send_to(Socket_Handle handle, u32 addr, u32 port, Data data, s32 flags) { - invalid_code_path; - return 0; + OS_SOCKET_TYPE sock = open_sockets_get(handle); + + struct sockaddr_in dst = { + .sin_family = AF_INET, + .sin_port = hton_u16(port), + .sin_addr.s_addr = hton_u32(addr), + }; + struct sockaddr* dst_ptr = (struct sockaddr*)&dst; + s32 len_sent = sendto(sock, data.base, data.size, flags, dst_ptr, sizeof(struct sockaddr_in)); + if (len_sent == -1) + { + perror("Error: os_socket_send_to\n"); + return 0; + } + return len_sent; } s32 os_socket_set_opt(Socket_Handle handle, int level, int option_name, u8* option_value, s32 option_len) { - invalid_code_path; + OS_SOCKET_TYPE sock = open_sockets_get(handle); + s32 err = setsockopt(sock, level, option_name, (void*)option_value, (socklen_t)option_len); + if (err) { + fprintf(stderr, "Error: setsockopt - %d\n\targs: %d %d %.*s\n", err, level, option_name, option_len, (char*)option_value); + } return 0; } diff --git a/src_v2/platform/linux/lumenarium_linux_time.h b/src_v2/platform/linux/lumenarium_linux_time.h index 3843702..7acecd3 100644 --- a/src_v2/platform/linux/lumenarium_linux_time.h +++ b/src_v2/platform/linux/lumenarium_linux_time.h @@ -1,18 +1,26 @@ #ifndef LUMENARIUM_LINUX_TIME_H #define LUMENARIUM_LINUX_TIME_H 1 +#define NANOS_PER_SECOND 1000000000 Ticks os_get_ticks() { - invalid_code_path; - return (Ticks){}; + struct timespec ts = {}; + s32 r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); + assert(r == 0); + s64 nanos = (s64)ts.tv_sec * NANOS_PER_SECOND; + nanos += (s64)ts.tv_nsec; + + Ticks result = { + .value = nanos + }; + return result; } r64 os_get_ticks_per_second() { - invalid_code_path; - return 0; + return NANOS_PER_SECOND; } #endif // LUMENARIUM_LINUX_TIME_H \ No newline at end of file diff --git a/src_v2/platform/raspi/lumenarium_first_raspi.c b/src_v2/platform/raspi/lumenarium_first_raspi.c index e1e744f..dbbe2ac 100644 --- a/src_v2/platform/raspi/lumenarium_first_raspi.c +++ b/src_v2/platform/raspi/lumenarium_first_raspi.c @@ -18,6 +18,11 @@ #include #include +#include +#include +#include +#include +#include #define linux_err_print(sub_proc) linux_err_print_((char*)__FUNCTION__, (char*)(sub_proc), errno) void @@ -38,11 +43,36 @@ linux_err_print_(char* proc, char* sub_proc, s32 errsv) #include "../linux/lumenarium_linux_time.h" #include "../linux/lumenarium_linux_network.h" + int main (int arg_count, char** args) { - // temp - global_scratch_ = bump_allocator_create_reserve(MB(256)); - run_tests(); + // INIT APPLICATION + Editor_Desc ed_desc = { + }; + App_State* state = lumenarium_init(&ed_desc); + bool running = true; + r64 target_seconds_per_frame = 1.0 / 30.0; + Ticks ticks_start = os_get_ticks(); + while (has_flag(state->flags, AppState_IsRunning)) + { + lumenarium_frame_prepare(state); + + lumenarium_frame(state); + lumenarium_env_validate(); + + Ticks ticks_end = os_get_ticks(); + r64 seconds_elapsed = get_seconds_elapsed(ticks_start, ticks_end, os_get_ticks_per_second()); + while (seconds_elapsed < target_seconds_per_frame) + { + u32 sleep_time = (u32)(1000.0f * (target_seconds_per_frame - seconds_elapsed)); + usleep(sleep_time); + ticks_end = os_get_ticks(); + seconds_elapsed = get_seconds_elapsed(ticks_start, ticks_end, os_get_ticks_per_second()); + } + ticks_start = ticks_end; + } + + lumenarium_cleanup(state); return 0; } \ No newline at end of file