Jackalope / jrf (public) (License: GPLv3 or later version) (since 2019-11-21) (hash sha1)
Libriary for reading and writing resource files: vertices, indices, meshes, images, models and scene data.
Supports reading from filesystem as well as from zip files.
It uses unique binary container format for those resources.
It is used in tool for converting popular formats like wavefront .obj file, TARGA image to this container and also used in resource loader as part of graphics framework.

It requires jlib libriary (see my user repositories) and libzip.
It is possible to remove libzip dependency.
List of commits:
Subject Hash Author Date (UTC)
removed IndicesArrays, added Mesh resource and Vertices attributes is not dynamically allocated a54a61cfc285bbec4246b1affdc5fe9251c059d0 jp.larry 2019-12-06 17:04:06
new obj file error f4385161fdd1ad8038e2d0924e1d3de2a6506cc9 Your Name 2019-12-01 10:26:18
scene issues solved cef4ba84d44cbdc6690c87e553df9fbfba60059f Your Name 2019-12-01 04:19:29
changed transformation matrix to 4x4 from 3x3 5b5728dabefb471cf126bf4b524f5079a14c0735 Your Name 2019-11-30 15:16:50
added missing return 94644ed6f61207f664ac4675fa015afbe017ddea jp.larry 2019-11-29 09:03:14
scene improvements, changed shift type to unsigned, set default-value funtions, model must be path a2708b94b88eb0ede73b9aab396e7a13f13baa52 Your Name 2019-11-29 09:27:32
separate function for str reading 9127f9a18f9f4974975fa35d58f596952b6aa4e5 Your Name 2019-11-29 09:26:43
new result errors ef56e017e2e761fdf23c6a64ed33277c98569e13 Your Name 2019-11-29 09:26:12
new types for Scene e2fde0d2b66e125ba3bfd86b8f51c81ecb5ab047 Your Name 2019-11-29 00:38:27
shift scale added to Scene 974752944e73bbc2949ff399ab9d806baeeab751 Your Name 2019-11-29 00:38:06
new resource type - scene - list of models f3eea43135dad8b3e24cf6683e5abc1a5e2e57ad Your Name 2019-11-28 07:59:59
model correctly handling error returns 3df9ec0f9abed56709b8b9556a5291217ad8744d Your Name 2019-11-28 07:59:25
updated jlib 2a28ba18170dc757f7bad931a12a34ee3e7e1c46 Your Name 2019-11-27 03:44:11
replaced free with jl::deallocate ab8d6cb89f814227c280797dbe654971a85264a6 Your Name 2019-11-25 07:29:05
second initial commit fb7a7e999477c84f10b3d618f6b00347fd0a9ca7 Your Name 2019-11-23 01:38:14
initial commit 3dc064f64d245d1dddcbdf8493b0be0a321cb65d Your Name 2019-11-22 20:10:24
Commit a54a61cfc285bbec4246b1affdc5fe9251c059d0 - removed IndicesArrays, added Mesh resource and Vertices attributes is not dynamically allocated
Author: jp.larry
Author date (UTC): 2019-12-06 17:04
Committer name: jp.larry
Committer date (UTC): 2019-12-06 17:04
Parent(s): 5e0d42eda395ecac4f52af2215088adcb45e5078
Signing key:
Tree: e74f77597ddc21d465d80fc2f12fc20c5639a03f
File Lines added Lines deleted
include/jrf/indices.h 142 106
include/jrf/jrf.h 41 37
include/jrf/mesh.h 118 0
include/jrf/model.h 13 116
include/jrf/read.h 10 10
include/jrf/result.h 4 1
include/jrf/scene.h 1 0
include/jrf/vertices.h 75 93
include/jrf/write.h 1 1
src/result.cpp 1 0
File include/jrf/indices.h changed (mode: 100644) (index dd36734..7bae4b6)
6 6
7 7 namespace jrf namespace jrf
8 8 { {
9 enum IndexFormat : uint32_t { U16 = 2, U32 = 4 };
10
11 struct Indices
12 {
13 uint8_t *p_data;
14 uint64_t size;
15
16 inline void destroy()
17 {
18 jl::deallocate(&p_data);
19 }
20 };
21
22
23 struct IndicesArrays
24 {
25 uint32_t array_count;
26 IndexFormat format;
27 union
28 {
29 Indices *p_indices;
30 Indices indices;
31 } u;
32 bool is_allocated;
33
34 void destroy()
35 {
36 if (is_allocated)
9 enum IndexFormat : uint32_t { U16 = 2, U32 = 4 };
10
11 struct Indices
12 {
13 uint8_t *p_data;
14 uint64_t size;
15 IndexFormat format;
16
17 inline void destroy() {
18 jl::deallocate(&p_data);
19 }
20
21 template<typename IO = jl::io_agent>
22 [[nodiscard]] Result read(IO *p_mediator)
23 {
24 auto *p_io = jl::io_agent_p_alt_cast(p_mediator);
25 if (not p_io->read_items(&format))
26 return Result::MEDIATOR_ERROR;
27 if (not p_io->read_items(&size))
28 return Result::MEDIATOR_ERROR;
29
30 if (size == 0)
31 p_data = nullptr;
32 else
33 {
34 if (not jl::allocate_bytes(&p_data, size))
35 return Result::MEDIATOR_ERROR;
36 if (not p_io->read_bytes(p_data, size))
37 return this->destroy(), Result::MEDIATOR_ERROR;
38 }
39 return Result::SUCCESS;
40 }
41
42 template<typename IO = jl::io_agent>
43 [[nodiscard]] Result write(IO *p_mediator) const
44 {
45 auto *p_io = jl::io_agent_p_alt_cast(p_mediator);
46 if (not p_io->write_items(&format))
47 return Result::MEDIATOR_ERROR;
48 if (not p_io->write_items(&size))
49 return Result::MEDIATOR_ERROR;
50 if (not p_io->write_bytes(p_data, size))
51 return Result::MEDIATOR_ERROR;
52
53 return Result::SUCCESS;
54 }
55 };
56
57 /*
58 struct IndicesArrays
59 {
60 uint32_t array_count;
61 IndexFormat format;
62 union
63 {
64 Indices *p_indices;
65 Indices indices;
66 } u;
67 bool is_allocated;
68
69 void destroy()
70 {
71 if (is_allocated)
72 {
73 if (u.p_indices != nullptr)
37 74 { {
38 if (u.p_indices != nullptr)
39 {
40 for (uint32_t i = 0; i < array_count; ++i)
41 jl::deallocate(&u.p_indices[i].p_data);
42 jl::deallocate(&u.p_indices);
43 }
75 for (uint32_t i = 0; i < array_count; ++i)
76 jl::deallocate(&u.p_indices[i].p_data);
77 jl::deallocate(&u.p_indices);
44 78 } }
45 else u.indices.destroy();
46 }
47
48 template<typename IO = jl::io_agent>
49 [[nodiscard]] Result read(IO *p_mediator)
50 {
51 if (p_mediator->read(&format, sizeof(format)) != sizeof(format))
52 return Result::MEDIATOR_ERROR;
53 if (p_mediator->read(&array_count, sizeof (array_count)) != sizeof(array_count))
54 return Result::MEDIATOR_ERROR;
55
56 if (array_count == 0)
57 u.p_indices = nullptr;
58 else
79 }
80 else u.indices.destroy();
81 }
82
83 template<typename IO = jl::io_agent>
84 [[nodiscard]] Result read(IO *p_mediator)
85 {
86 if (p_mediator->read(&format, sizeof(format)) != sizeof(format))
87 return Result::MEDIATOR_ERROR;
88 if (p_mediator->read(&array_count, sizeof (array_count)) != sizeof(array_count))
89 return Result::MEDIATOR_ERROR;
90
91 if (array_count == 0)
92 u.p_indices = nullptr;
93 else
94 {
95 Indices *p_ind;
96 is_allocated = array_count > 1;
97 if (is_allocated)
59 98 { {
60 Indices *p_ind;
61 is_allocated = array_count > 1;
62 if (is_allocated)
63 {
64 if (not jl::allocate(&u.p_indices, array_count))
65 return Result::ALLOCATION_FAIL;
66 p_ind = u.p_indices;
67 }
68 else p_ind = &u.indices;
69
70 for (uint32_t i = 0; i < array_count; ++i)
71 {
72 p_ind[i].p_data = nullptr;
73 if (p_mediator->read(&p_ind[i].size, sizeof(Indices::size)) != sizeof(Indices::size))
74 {
75 jl::deallocate(&u.p_indices);
76 return Result::MEDIATOR_ERROR;
77 }
78 }
79
80 for (uint32_t i = 0; i < array_count; ++i)
81 {
82 Result res;
83 auto &array = p_ind[i];
84 if (not jl::allocate_bytes(&array.p_data, array.size))
85 {
86 res = Result::ALLOCATION_FAIL;
87 goto CANCEL;
88 }
89 if (p_mediator->read(array.p_data, array.size) != array.size)
90 {
91 res = Result::MEDIATOR_ERROR;
92 CANCEL: destroy();
93 return res;
94 }
95 }
99 if (not jl::allocate(&u.p_indices, array_count))
100 return Result::ALLOCATION_FAIL;
101 p_ind = u.p_indices;
96 102 } }
97 return Result::SUCCESS;
98 }
99
100 template<typename IO = jl::io_agent>
101 [[nodiscard]] Result write(IO *p_mediator) const
102 {
103 if (p_mediator->write(&format, sizeof(format)) != sizeof(format))
104 return Result::MEDIATOR_ERROR;
105 if (p_mediator->write(&array_count, sizeof (array_count)) != sizeof (array_count))
106 return Result::MEDIATOR_ERROR;
107
108 const Indices *p_ind;
109 if (is_allocated)
110 p_ind = u.p_indices;
111 103 else p_ind = &u.indices; else p_ind = &u.indices;
112 104
113 105 for (uint32_t i = 0; i < array_count; ++i) for (uint32_t i = 0; i < array_count; ++i)
114 if (p_mediator->write(&p_ind[i].size, sizeof(Indices::size)) != sizeof(Indices::size))
115 return Result::MEDIATOR_ERROR;
106 {
107 p_ind[i].p_data = nullptr;
108 if (p_mediator->read(&p_ind[i].size, sizeof(Indices::size)) != sizeof(Indices::size))
109 {
110 jl::deallocate(&u.p_indices);
111 return Result::MEDIATOR_ERROR;
112 }
113 }
116 114
117 115 for (uint32_t i = 0; i < array_count; ++i) for (uint32_t i = 0; i < array_count; ++i)
118 if (p_mediator->write(p_ind[i].p_data, p_ind[i].size) != int64_t(p_ind[i].size))
119 return Result::MEDIATOR_ERROR;
120
121 return Result::SUCCESS;
122 }
123 };
116 {
117 Result res;
118 auto &array = p_ind[i];
119 if (not jl::allocate_bytes(&array.p_data, array.size))
120 {
121 res = Result::ALLOCATION_FAIL;
122 goto CANCEL;
123 }
124 if (p_mediator->read(array.p_data, array.size) != array.size)
125 {
126 res = Result::MEDIATOR_ERROR;
127 CANCEL: destroy();
128 return res;
129 }
130 }
131 }
132 return Result::SUCCESS;
133 }
134
135 template<typename IO = jl::io_agent>
136 [[nodiscard]] Result write(IO *p_mediator) const
137 {
138 if (p_mediator->write(&format, sizeof(format)) != sizeof(format))
139 return Result::MEDIATOR_ERROR;
140 if (p_mediator->write(&array_count, sizeof (array_count)) != sizeof (array_count))
141 return Result::MEDIATOR_ERROR;
142
143 const Indices *p_ind;
144 if (is_allocated)
145 p_ind = u.p_indices;
146 else p_ind = &u.indices;
147
148 for (uint32_t i = 0; i < array_count; ++i)
149 if (p_mediator->write(&p_ind[i].size, sizeof(Indices::size)) != sizeof(Indices::size))
150 return Result::MEDIATOR_ERROR;
151
152 for (uint32_t i = 0; i < array_count; ++i)
153 if (p_mediator->write(p_ind[i].p_data, p_ind[i].size) != int64_t(p_ind[i].size))
154 return Result::MEDIATOR_ERROR;
155
156 return Result::SUCCESS;
157 }
158 };
159 */
124 160 } }
File include/jrf/jrf.h changed (mode: 100644) (index d94924c..ca76334)
4 4
5 5 namespace jrf namespace jrf
6 6 { {
7 template<ResourceType RT>
8 struct Resource { using T = void; };
7 template<ResourceType RT>
8 struct Resource { using T = void; };
9 9
10 template<>
11 struct Resource<ResourceType::IMAGE> { using T = Image; };
12 template<>
13 struct Resource<ResourceType::VERTICES> { using T = Vertices; };
14 template<>
15 struct Resource<ResourceType::INDICES> { using T = IndicesArrays; };
16 template<>
17 struct Resource<ResourceType::MODEL> { using T = Model; };
18 template<>
19 struct Resource<ResourceType::SCENE> { using T = Scene; };
10 template<>
11 struct Resource<ResourceType::IMAGE> { using T = Image; };
12 template<>
13 struct Resource<ResourceType::VERTICES> { using T = Vertices; };
14 template<>
15 struct Resource<ResourceType::INDICES> { using T = Indices; };
16 template<>
17 struct Resource<ResourceType::MESH> { using T = Mesh; };
18 template<>
19 struct Resource<ResourceType::MODEL> { using T = Model; };
20 template<>
21 struct Resource<ResourceType::SCENE> { using T = Scene; };
20 22
21 union ResourceUnion {
22 Image image;
23 Vertices vertices;
24 IndicesArrays indices_array;
25 Model model;
26 Scene scene;
27 };
23 union ResourceUnion {
24 Image image;
25 Vertices vertices;
26 Indices indices;
27 Mesh mesh;
28 Model model;
29 Scene scene;
30 };
28 31
29 static const constexpr union {
30 uint64_t integer;
31 uint8_t data[8] = {'j','r','f','0','0','0','0'};
32 } MAGIC {};
32 static const constexpr union {
33 uint64_t integer;
34 uint8_t data[8] = {'j','r','f','0','0','0','0'};
35 } MAGIC {};
33 36
34 struct EntryHeader {
35 uint64_t magic;
36 ResourceType type;
37 };
37 struct EntryHeader {
38 uint64_t magic;
39 ResourceType type;
40 };
38 41
39 void inline destroy(ResourceType type, ResourceUnion *p_ru) {
40 switch(type) {
41 case ResourceType::IMAGE: p_ru->image .destroy(); break;
42 case ResourceType::VERTICES: p_ru->vertices .destroy(); break;
43 case ResourceType::INDICES: p_ru->indices_array.destroy(); break;
44 case ResourceType::MODEL: p_ru->model .destroy(); break;
45 case ResourceType::SCENE: p_ru->scene .destroy(); break;
46 default: return;
47 }
48 }
42 void inline destroy(ResourceType type, ResourceUnion *p_ru) {
43 switch(type) {
44 case ResourceType::IMAGE: p_ru->image .destroy(); break;
45 case ResourceType::VERTICES: p_ru->vertices.destroy(); break;
46 case ResourceType::INDICES: p_ru->indices .destroy(); break;
47 case ResourceType::MESH: p_ru->mesh .destroy(); break;
48 case ResourceType::MODEL: p_ru->model .destroy(); break;
49 case ResourceType::SCENE: p_ru->scene .destroy(); break;
50 default: return;
51 }
52 }
49 53 } }
File include/jrf/mesh.h added (mode: 100644) (index 0000000..51878a1)
1 #pragma once
2
3 #include "vertices.h"
4 #include "indices.h"
5 #include <jlib/string.h>
6 #include <jlib/darray.h>
7
8 namespace jrf
9 {
10 template<typename IO = jl::io_agent>
11 [[nodiscard]] Result read_str(IO *p_io, jl::string *p_dst)
12 {
13 jl::darray<char> path_tmp;
14 path_tmp.init();
15 char sym;
16 do {
17 if (p_io->read(&sym, 1) != 1)
18 return Result::MEDIATOR_ERROR;
19 if (not path_tmp.insert(sym))
20 return Result::ALLOCATION_FAIL;
21 }
22 while(sym != '\0');
23 *p_dst = {path_tmp.begin(), path_tmp.end()};
24 return Result::SUCCESS;
25 }
26
27 template<typename T>
28 struct Data {
29 union {
30 T data;
31 jl::string path;
32 } u;
33 ResourceMode mode;
34
35 void destroy() {
36 switch(mode) {
37 case ResourceMode::DATA: u.data.destroy(); break;
38 case ResourceMode::PATH: u.path.destroy(); break;
39 case ResourceMode::NONE: break;
40 }
41 }
42
43 template<typename IO = jl::io_agent>
44 [[nodiscard]] Result read(IO *p_io)
45 {
46 if (p_io->read(&mode, sizeof(mode)) != sizeof(mode))
47 return Result::MEDIATOR_ERROR;
48 switch(mode)
49 {
50 case ResourceMode::NONE: break;
51 case ResourceMode::DATA: {
52 Result res = u.data.read(p_io);
53 if (res != Result::SUCCESS)
54 return res;
55 } break;
56 case ResourceMode::PATH: {
57 auto res = read_str(p_io, &u.path);
58 if (res != Result::SUCCESS)
59 return res;
60 }
61 }
62 return Result::SUCCESS;
63 }
64 template<typename IO = jl::io_agent>
65 [[nodiscard]] Result write(IO *p_io) const
66 {
67 if (p_io->write(&mode, sizeof(mode)) != sizeof(mode))
68 return Result::MEDIATOR_ERROR;
69 switch(mode) {
70 case ResourceMode::DATA: {
71 Result res = u.data.write(p_io);
72 if (res != Result::SUCCESS)
73 return res;
74 } break;
75 case ResourceMode::PATH: {
76 if (p_io->write(u.path.begin(), u.path.size())
77 != int64_t(u.path.size()))
78 return Result::MEDIATOR_ERROR;
79 } break;
80 default: break;
81 }
82 return Result::SUCCESS;
83 }
84 };
85
86 struct Mesh
87 {
88 Data<Vertices> vert;
89 Data<Indices> ind;
90
91 void destroy() {
92 vert.destroy();
93 ind .destroy();
94 }
95
96 template<typename IO = jl::io_agent>
97 [[nodiscard]] Result read(IO *p_mediator)
98 {
99 Result res;
100 res = vert.read(p_mediator);
101 if (res != Result::SUCCESS)
102 return res;
103 res = ind.read(p_mediator);
104 if (res != Result::SUCCESS)
105 vert.destroy();
106 return res;
107 }
108
109 template<typename IO = jl::io_agent>
110 [[nodiscard]] Result write(IO *p_mediator) const
111 {
112 Result res = vert.write(p_mediator);
113 if (res != Result::SUCCESS)
114 return res;
115 return ind.write(p_mediator);
116 }
117 };
118 }
File include/jrf/model.h changed (mode: 100644) (index a704f83..cf7285c)
1 1 #pragma once #pragma once
2 2
3 #include "vertices.h"
4 #include "indices.h"
3 #include "mesh.h"
5 4 #include "image.h" #include "image.h"
6 #include <jlib/string.h>
7 #include <jlib/darray.h>
8 #include <jlib/array.h>
5
9 6
10 7 namespace jrf namespace jrf
11 8 { {
12 template<typename IO = jl::io_agent>
13 [[nodiscard]] Result read_str(IO *p_io, jl::string *p_dst)
14 {
15 jl::darray<char> path_tmp;
16 path_tmp.init();
17 char sym;
18 do {
19 if (p_io->read(&sym, 1) != 1)
20 return Result::MEDIATOR_ERROR;
21 if (not path_tmp.insert(sym))
22 return Result::ALLOCATION_FAIL;
23 }
24 while(sym != '\0');
25 *p_dst = {path_tmp.begin(), path_tmp.end()};
26 return Result::SUCCESS;
27 }
28
29 9 struct Model struct Model
30 10 { {
31 enum Part { VERTICES, INDICES, IMAGE };
32 constexpr static const uint8_t PART_COUNT = IMAGE + 1;
33
34 template<typename T>
35 union Data {
36 void destroy(ResourceMode mode) {
37 switch(mode) {
38 case ResourceMode::DATA: data.destroy(); break;
39 case ResourceMode::PATH: path.destroy(); break;
40 case ResourceMode::NONE: break;
41 }
42 }
43
44 template<typename IO = jl::io_agent>
45 [[nodiscard]] Result read(ResourceMode mode, IO *p_io)
46 {
47 switch(mode)
48 {
49 case ResourceMode::NONE: break;
50 case ResourceMode::DATA: {
51 Result res = data.read(p_io);
52 if (res != Result::SUCCESS)
53 return res;
54 } break;
55 case ResourceMode::PATH: {
56 auto res = read_str(p_io, &path);
57 if (res != Result::SUCCESS)
58 return res;
59 }
60 }
61 return Result::SUCCESS;
62 }
63 template<typename IO = jl::io_agent>
64 [[nodiscard]] Result write(ResourceMode mode, IO *p_io) const
65 {
66 switch(mode) {
67 case ResourceMode::DATA: {
68 Result res = data.write(p_io);
69 if (res != Result::SUCCESS)
70 return res;
71 } break;
72 case ResourceMode::PATH: {
73 if (p_io->write(path.begin(), path.size())
74 != int64_t(path.size()))
75 return Result::MEDIATOR_ERROR;
76 } break;
77 default: break;
78 }
79 return Result::SUCCESS;
80 }
81
82 T data;
83 jl::string path;
84 };
85
86 Data<Vertices> u_vert;
87 Data<IndicesArrays> u_ind;
88 Data<Image> u_image;
89 jl::array<ResourceMode, PART_COUNT> modes;
11 Data<Mesh> mesh;
12 Data<Image> image;
90 13
91 14 void destroy() { void destroy() {
92 u_vert .destroy(modes[Part::VERTICES]);
93 u_ind .destroy(modes[Part::INDICES]);
94 u_image.destroy(modes[Part::IMAGE]);
15 mesh.destroy();
16 image.destroy();
95 17 } }
96 18
97 19 template<typename IO = jl::io_agent> template<typename IO = jl::io_agent>
98 20 [[nodiscard]] Result read(IO *p_mediator) [[nodiscard]] Result read(IO *p_mediator)
99 21 { {
100 modes = {};
101 decltype(modes) modes_tmp;
102 auto *p_io = jl::io_agent_p_alt_cast(p_mediator);
103 if (not p_io->read_items(&modes_tmp))
104 return Result::MEDIATOR_ERROR;
105
106 22 Result res; Result res;
107 res = u_vert.read(modes_tmp[Part::VERTICES], p_mediator);
108 if (res != Result::SUCCESS)
109 return destroy(), res;
110 modes[Part::VERTICES] = modes_tmp[Part::VERTICES];
111
112 res = u_ind.read(modes_tmp[Part::INDICES], p_mediator);
23 res = mesh.read(p_mediator);
113 24 if (res != Result::SUCCESS) if (res != Result::SUCCESS)
114 return destroy(), res;
115 modes[Part::INDICES] = modes_tmp[Part::INDICES];
25 return res;
116 26
117 res = u_image.read(modes_tmp[Part::IMAGE], p_mediator);
27 res = image.read(p_mediator);
118 28 if (res != Result::SUCCESS) if (res != Result::SUCCESS)
119 return destroy(), res;
120 modes[Part::IMAGE] = modes_tmp[Part::IMAGE];
121 return Result::SUCCESS;
29 mesh.destroy();
30 return res;
122 31 } }
123 32
124 33 template<typename IO = jl::io_agent> template<typename IO = jl::io_agent>
125 34 [[nodiscard]] Result write(IO *p_io) const [[nodiscard]] Result write(IO *p_io) const
126 35 { {
127 if (p_io->write(&modes, sizeof(modes)) != sizeof(modes))
128 return Result::MEDIATOR_ERROR;
129
130 Result res;
131 res = u_vert.write(modes[Part::VERTICES], p_io);
132 if (res != Result::SUCCESS)
133 return res;
134 res = u_ind.write(modes[Part::INDICES], p_io);
135 if (res != Result::SUCCESS)
136 return res;
137 res = u_image.write(modes[Part::IMAGE], p_io);
36 Result res = mesh.write(p_io);
138 37 if (res != Result::SUCCESS) if (res != Result::SUCCESS)
139 38 return res; return res;
140 return Result::SUCCESS;
39 return image.write(p_io);
141 40 } }
142 41 }; };
143
144
145 42 } }
File include/jrf/read.h changed (mode: 100644) (index 8f01e25..58b030a)
... ... namespace jrf::details
13 13 { {
14 14 switch(type) switch(type)
15 15 { {
16 case ResourceType::IMAGE: return p_ru->image .read(p_io);
17 case ResourceType::VERTICES: return p_ru->vertices .read(p_io);
18 case ResourceType::INDICES: return p_ru->indices_array.read(p_io);
19 case ResourceType::MODEL: return p_ru->model .read(p_io);
20 case ResourceType::SCENE: return p_ru->scene .read(p_io);
16 case ResourceType::IMAGE: return p_ru->image .read(p_io);
17 case ResourceType::VERTICES: return p_ru->vertices.read(p_io);
18 case ResourceType::INDICES: return p_ru->indices .read(p_io);
19 case ResourceType::MODEL: return p_ru->model .read(p_io);
20 case ResourceType::SCENE: return p_ru->scene .read(p_io);
21 21 default: return Result::UNKNOWN_RESOURCE_TYPE; default: return Result::UNKNOWN_RESOURCE_TYPE;
22 22 } }
23 23 } }
 
... ... namespace jrf
98 98 switch(type) switch(type)
99 99 { {
100 100 case ResourceType::IMAGE: case ResourceType::IMAGE:
101 return read<ResourceType::IMAGE> (&p_ru->image, paths...);
101 return read<ResourceType::IMAGE> (&p_ru->image, paths...);
102 102 case ResourceType::VERTICES: case ResourceType::VERTICES:
103 return read<ResourceType::VERTICES>(&p_ru->vertices, paths...);
103 return read<ResourceType::VERTICES>(&p_ru->vertices, paths...);
104 104 case ResourceType::INDICES: case ResourceType::INDICES:
105 return read<ResourceType::INDICES> (&p_ru->indices_array, paths...);
105 return read<ResourceType::INDICES> (&p_ru->indices, paths...);
106 106 case ResourceType::MODEL: case ResourceType::MODEL:
107 return read<ResourceType::MODEL> (&p_ru->model, paths...);
107 return read<ResourceType::MODEL> (&p_ru->model, paths...);
108 108 case ResourceType::SCENE: case ResourceType::SCENE:
109 return read<ResourceType::SCENE> (&p_ru->scene, paths...);
109 return read<ResourceType::SCENE> (&p_ru->scene, paths...);
110 110 default: return Result::UNKNOWN_RESOURCE_TYPE; default: return Result::UNKNOWN_RESOURCE_TYPE;
111 111 } }
112 112 } }
File include/jrf/result.h changed (mode: 100644) (index 8b19eac..7006816)
... ... namespace jrf
6 6 enum ResourceType : uint8_t { enum ResourceType : uint8_t {
7 7 VERTICES, VERTICES,
8 8 INDICES, INDICES,
9 MESH,
9 10 IMAGE, IMAGE,
10 11 MODEL, MODEL,
11 12 SCENE, OPTIONS, ENTRY, SCENE, OPTIONS, ENTRY,
 
... ... namespace jrf
15 16 constexpr static const char* RES_TYPE_STR[ResourceType::COUNT+1] = { constexpr static const char* RES_TYPE_STR[ResourceType::COUNT+1] = {
16 17 "Vertices", "Vertices",
17 18 "Indices", "Indices",
19 "Mesh",
18 20 "Image", "Image",
19 21 "Model", "Model",
20 22 "Scene", "Options", "Entry", "Scene", "Options", "Entry",
 
... ... namespace jrf
22 24 }; };
23 25
24 26 constexpr static const uint16_t RES_TYPE_STR_LENGTH[ResourceType::COUNT+1] constexpr static const uint16_t RES_TYPE_STR_LENGTH[ResourceType::COUNT+1]
25 = { 8, 7, 5, 5, 5, 7, 5, 0 };
27 = { 8, 7, 4, 5, 5, 5, 7, 5, 0 };
26 28
27 29 enum class ResourceMode : uint8_t { NONE, DATA, PATH }; enum class ResourceMode : uint8_t { NONE, DATA, PATH };
28 30
 
... ... namespace jrf
48 50 INCOMPATIBLE_RESOURCES, INCOMPATIBLE_RESOURCES,
49 51 RESOURCE_PART_DUPLICATE, RESOURCE_PART_DUPLICATE,
50 52 RESOURCE_PART_MISSING, RESOURCE_PART_MISSING,
53 RESOURCE_PART_OVERLAP,
51 54 RESOURCE_TYPE_MISMATCH, RESOURCE_TYPE_MISMATCH,
52 55 SCENE_ENTRY_MODEL_IS_NOT_PATH, SCENE_ENTRY_MODEL_IS_NOT_PATH,
53 56 FILE_WRONG_HEADER, FILE_WRONG_HEADER,
File include/jrf/scene.h changed (mode: 100644) (index f5430c8..5d2f1a0)
1 1 #pragma once #pragma once
2 2
3 3 #include "model.h" #include "model.h"
4 #include <jlib/array.h>
4 5
5 6 namespace jrf { struct Scene; struct SceneEntry; } namespace jrf { struct Scene; struct SceneEntry; }
6 7
File include/jrf/vertices.h changed (mode: 100644) (index b62b48c..16d55ea)
2 2
3 3 #include <jlib/io_agent.h> #include <jlib/io_agent.h>
4 4 #include <jlib/allocate.h> #include <jlib/allocate.h>
5 #include <jlib/array.h>
5 6
6 7 #include "result.h" #include "result.h"
7 8
8 9 namespace jrf namespace jrf
9 10 { {
10 struct Vertices
11 {
12 enum Format : uint8_t
13 {
14 F16, F32, F64, I8, I16, I32, I64, U8, U16, U32, U64
15 };
16
17 constexpr static const uint8_t FORMAT_SIZE[] = { 2, 4, 8, 1, 2, 4, 8, 1, 2, 4, 8 };
18
19 struct Attribute
20 {
21 uint8_t dimention_count;
22 Format format;
23 };
24
25 Attribute *p_attributes;
26 uint16_t attr_count;
27 uint16_t vec_size;
28 void *p_vecs;
29 uint64_t vecs_count;
30 uint64_t vecs_size;
31
32 void destroy()
33 {
34 jl::deallocate(&p_attributes);
35 jl::deallocate(&p_vecs);
36 }
37
38 template<typename IO = jl::io_agent>
39 [[nodiscard]] Result read(IO *p_mediator)
40 {
41 if (p_mediator->read(&attr_count, sizeof(attr_count)) != sizeof(attr_count))
42 return Result::MEDIATOR_ERROR;
43
44 Result res = Result::MEDIATOR_ERROR;
45
46 if (attr_count == 0)
47 p_attributes = nullptr;
48 else
49 {
50 if (not jl::allocate(&p_attributes, attr_count))
51 return Result::ALLOCATION_FAIL;
52
53 uint64_t size = sizeof (Attribute) * attr_count;
54 if (p_mediator->read(p_attributes, size) != size)
55 goto F_A;
56 }
57
58 if (p_mediator->read(&vec_size, sizeof (vec_size)) != sizeof(vec_size))
59 goto F_A;
60 if (p_mediator->read(&vecs_count, sizeof (vecs_count)) != sizeof(vecs_count))
61 goto F_A;
62 if (p_mediator->read(&vecs_size, sizeof (vecs_size)) != sizeof(vecs_size))
63 goto F_A;
64
65 if (vecs_size == 0)
66 p_vecs = nullptr;
67 else
68 {
69 if (not jl::allocate_bytes(&p_vecs, vecs_size))
70 {
71 res = Result::ALLOCATION_FAIL;
72 goto F_A;
73 }
74 if (p_mediator->read(p_vecs, vecs_size) != vecs_size)
75 goto F_A;
76 }
77
78 return Result::SUCCESS;
79
80 F_A: jl::deallocate(&p_attributes);
81 return res;
82 }
83
84 template<typename IO = jl::io_agent>
85 [[nodiscard]] Result write(IO *p_mediator) const
86 {
87 if (p_mediator->write(&attr_count, sizeof(attr_count)) != sizeof (attr_count))
88 return Result::MEDIATOR_ERROR;
89 if (p_mediator->write(p_attributes, sizeof (Attribute) * attr_count) != sizeof(Attribute) * attr_count)
90 return Result::MEDIATOR_ERROR;
91 if (p_mediator->write(&vec_size, sizeof (vec_size)) != sizeof(vec_size))
92 return Result::MEDIATOR_ERROR;
93 if (p_mediator->write(&vecs_count, sizeof (vecs_count)) != sizeof (vecs_count))
94 return Result::MEDIATOR_ERROR;
95 if (p_mediator->write(&vecs_size, sizeof (vecs_size)) != sizeof(vecs_size))
96 return Result::MEDIATOR_ERROR;
97 if (p_mediator->write(p_vecs, vecs_size) != int64_t(vecs_size))
98 return Result::MEDIATOR_ERROR;
99
100 return Result::SUCCESS;
101 }
102 };
11 struct Vertices
12 {
13 enum Format : uint8_t {
14 F16, F32, F64,
15 I8, I16, I32, I64,
16 U8, U16, U32, U64
17 };
18
19 constexpr static const uint8_t FORMAT_SIZE[] = {
20 2, 4, 8,
21 1, 2, 4, 8,
22 1, 2, 4, 8
23 };
24
25 struct Attribute
26 {
27 uint8_t dimention_count;
28 Format format;
29 };
30
31 constexpr static const uint8_t MAX_ATTR_COUNT = 8;
32
33 jl::array<Attribute, MAX_ATTR_COUNT> attributes;
34 uint16_t attr_count;
35 void *p_vecs;
36 uint64_t vecs_size;
37
38 void destroy() {
39 jl::deallocate(&p_vecs);
40 }
41
42 template<typename IO = jl::io_agent>
43 [[nodiscard]] Result read(IO *p_mediator)
44 {
45 auto *p_io = jl::io_agent_p_alt_cast(p_mediator);
46 if (not p_io->read_items(&attr_count))
47 return Result::MEDIATOR_ERROR;
48
49 if (attr_count != 0)
50 if (not p_io->read_items(&attributes))
51 return Result::MEDIATOR_ERROR;
52
53 if (not p_io->read_items(&vecs_size))
54 return Result::MEDIATOR_ERROR;
55
56 if (vecs_size == 0)
57 p_vecs = nullptr;
58 else
59 {
60 if (not jl::allocate_bytes(&p_vecs, vecs_size))
61 return Result::MEDIATOR_ERROR;
62 if (not p_io->read_bytes(p_vecs, vecs_size))
63 return this->destroy(), Result::MEDIATOR_ERROR;
64 }
65
66 return Result::SUCCESS;
67 }
68
69 template<typename IO = jl::io_agent>
70 [[nodiscard]] Result write(IO *p_mediator) const
71 {
72 auto *p_io = jl::io_agent_p_alt_cast(p_mediator);
73 if (not p_io->write_items(&attr_count))
74 return Result::MEDIATOR_ERROR;
75 if (not p_io->write_items(&attributes))
76 return Result::MEDIATOR_ERROR;
77 if (not p_io->write_items(&vecs_size))
78 return Result::MEDIATOR_ERROR;
79 if (not p_io->write_bytes(p_vecs, vecs_size))
80 return Result::MEDIATOR_ERROR;
81
82 return Result::SUCCESS;
83 }
84 };
103 85 } }
File include/jrf/write.h changed (mode: 100644) (index dfc595c..79dd4a3)
... ... namespace jrf
13 13 case ResourceType::VERTICES: case ResourceType::VERTICES:
14 14 return write<ResourceType::VERTICES>(p_io, p_ru->vertices); return write<ResourceType::VERTICES>(p_io, p_ru->vertices);
15 15 case ResourceType::INDICES: case ResourceType::INDICES:
16 return write<ResourceType::INDICES> (p_io, p_ru->indices_array);
16 return write<ResourceType::INDICES> (p_io, p_ru->indices);
17 17 case ResourceType::MODEL: case ResourceType::MODEL:
18 18 return write<ResourceType::MODEL> (p_io, p_ru->model); return write<ResourceType::MODEL> (p_io, p_ru->model);
19 19 case ResourceType::SCENE: case ResourceType::SCENE:
File src/result.cpp changed (mode: 100644) (index e872790..b53df30)
... ... const char* jrf::to_str(jrf::Result result)
26 26 STR(INCOMPATIBLE_RESOURCES) STR(INCOMPATIBLE_RESOURCES)
27 27 STR(RESOURCE_PART_DUPLICATE) STR(RESOURCE_PART_DUPLICATE)
28 28 STR(RESOURCE_PART_MISSING) STR(RESOURCE_PART_MISSING)
29 STR(RESOURCE_PART_OVERLAP)
29 30 STR(RESOURCE_TYPE_MISMATCH) STR(RESOURCE_TYPE_MISMATCH)
30 31 STR(SCENE_ENTRY_MODEL_IS_NOT_PATH) STR(SCENE_ENTRY_MODEL_IS_NOT_PATH)
31 32 STR(FILE_WRONG_HEADER) STR(FILE_WRONG_HEADER)
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/Jackalope/jrf

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/Jackalope/jrf

Clone this repository using git:
git clone git://git.rocketgit.com/user/Jackalope/jrf

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main