this repo has no description
1#include <ctype.h>
2#include <fcntl.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/mman.h>
8#include <sys/stat.h>
9#include <unistd.h>
10
11#include "util.h"
12
13void arg_check(int argc, char *argv[]) {
14 if (argc < 2) {
15 printf("u9map <level number>\n");
16 exit(1);
17 }
18 printf("loading level %s\n", argv[1]);
19}
20
21long mmap_file(const char filename[], void **map) {
22 int fd = open(filename, O_RDONLY);
23 struct stat stat;
24 fstat(fd, &stat);
25 const int offset = 0;
26 *map = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, offset);
27 printf("mmap_file %p\n", *map);
28 close(fd);
29 return stat.st_size;
30}
31
32int read_file(const char *filename, uint32_t *file_size, char **buffer,
33 uint32_t *bytes_read) {
34 FILE *fp = fopen(filename, "rb");
35
36 // file size
37 fseek(fp, 0, SEEK_END);
38 *file_size = ftell(fp);
39 fseek(fp, 0, SEEK_SET);
40
41 // allocate
42 char *buf = (char *)malloc(*file_size + 1);
43
44 // read
45 *bytes_read = fread(buf, 1, *file_size, fp);
46 buf[*bytes_read] = 0;
47 *buffer = buf;
48
49 fclose(fp);
50 return 0;
51}
52
53char *path_merge(char *path, char *level) {
54 char *env_path = getenv("U9MAP_PATH");
55 if (!env_path) {
56 printf(
57 "please set environment variable U9MAP_PATH to the ultima9 folder\n");
58 exit(1);
59 }
60 int env_path_len = strlen(env_path);
61 int path_len = strlen(path);
62 int level_len = strlen(level);
63
64 char *buf = malloc(env_path_len + path_len + level_len + 1);
65 sprintf(buf, "%s/%s%s", env_path, path, level);
66 if (fopen(buf, "r") == NULL) {
67 // switch case
68 int latch = 0;
69 for (int idx = strlen(buf); idx >= 0; idx--) {
70 if (isalpha(buf[idx])) {
71 latch = 1;
72 buf[idx] = toupper(buf[idx]);
73 } else {
74 if (latch == 1)
75 break;
76 }
77 }
78 }
79 return buf;
80}
81
82long timeval_usec(struct timeval *t) {
83 return t->tv_sec * 1000000 + t->tv_usec;
84}
85
86float stopwatch_elapsed_sec(struct stopwatch_t *stopwatch) {
87 gettimeofday(&stopwatch->t_now, NULL);
88 return (timeval_usec(&stopwatch->t_now) - timeval_usec(&stopwatch->t_then)) /
89 1000000.0;
90}
91
92void stopwatch_init(struct stopwatch_t *stopwatch) {
93 stopwatch->frames = 0;
94 gettimeofday(&stopwatch->t_then, NULL);
95}