Skip to content

Commit 1b3e643

Browse files
committed
rewrite walk.c to use new own extract_handler and caching,
enable parallel building in Makefile
1 parent f83bae0 commit 1b3e643

File tree

4 files changed

+94
-67
lines changed

4 files changed

+94
-67
lines changed

Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ CMAKE_OPTS=-DCMAKE_BUILD_TYPE=Debug -D PRIVATE_FILENAME=OFF
33
all:
44
@mkdir -p build
55
@cd build; cmake ${CMAKE_OPTS} .. > /dev/null
6-
@make -C build --quiet
6+
@make -C build --quiet -j
77
%:
88
@mkdir -p build
99
@cd build; cmake ${CMAKE_OPTS} .. > /dev/null
10-
@make -C build --quiet $@
10+
@make -C build --quiet $@ -j
1111

12-
.PHONY: release
12+
.PHONY: release install-release clean-release
1313

1414
release:
1515
@mkdir -p release
1616
@cd release; cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr .. > /dev/null
17-
@make -C release --quiet
17+
@make -C release --quiet -j
1818

1919
install-release: release
20-
@make -C release --quiet install
20+
@make -C release --quiet install -j
2121

2222
clean-release:
2323
@rm -rf release

src/s3dat.h

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <stddef.h>
55
#include <stdbool.h>
6+
#include <string.h>
67
#include <stdint.h>
78
#include <stdio.h>
89

@@ -23,6 +24,8 @@
2324
#define S3DAT_IOSET_LINUX_MMF_FD 0x603
2425
#define S3DAT_IOSET_WIN32_MMF_HANDLE 0x604
2526

27+
#define S3DAT_EXCEPTION_WRONG_RESTYPE 0x109
28+
2629
#define S3DAT_EXHANDLER_CALL(me, res, throws, file, func, line) \
2730
do { \
2831
me->before->call(me->before, res, throws); \
@@ -35,6 +38,13 @@
3538
return; \
3639
}
3740

41+
#define S3DAT_CHECK_TYPE(handle, res, type, throws, file, func, line) \
42+
if(strncmp(res->restype->name, type, strlen(type)) != 0) { \
43+
s3dat_throw(handle, throws, S3DAT_EXCEPTION_WRONG_RESTYPE, file, func, line); \
44+
return; \
45+
}
46+
47+
3848

3949
typedef struct s3dat_exception_t s3dat_exception_t;
4050

src/s3dat_internal.h

-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "config.h"
44

55
#include <stdio.h>
6-
#include <string.h>
76
#include <sys/stat.h>
87
#include <sys/types.h>
98
#include <unistd.h>
@@ -52,13 +51,6 @@
5251
#define S3DAT_INTERNAL_OUT_OF_MEMORY(handle, throws) \
5352
s3dat_throw(handle, throws, S3DAT_EXCEPTION_OUT_OF_MEMORY, NULL, NULL, 0)
5453

55-
56-
#define S3DAT_CHECK_TYPE(handle, res, type, throws, file, func, line) \
57-
if(strncmp(res->restype->name, type, strlen(type)) != 0) { \
58-
s3dat_throw(handle, throws, S3DAT_EXCEPTION_WRONG_RESTYPE, file, func, line); \
59-
return; \
60-
}
61-
6254
typedef struct s3dat_internal_stack_t s3dat_internal_stack_t;
6355
typedef struct s3dat_internal_attribute_t s3dat_internal_attribute_t;
6456

walk.c

+79-54
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,54 @@
1111
int width = 640, height = 360;
1212

1313

14+
typedef struct {
15+
s3dat_t* parent;
16+
int tex_id;
17+
uint16_t width;
18+
uint16_t height;
19+
int16_t xoff;
20+
int16_t yoff;
21+
} gltex_t;
22+
23+
void delete_texture(gltex_t* tex) {
24+
glDeleteTextures(1, &tex->tex_id);
25+
tex->parent->free_func(tex->parent->mem_arg, tex);
26+
}
27+
28+
s3dat_restype_t gl_bitmap_type = {"gltex", (void (*) (void*)) delete_texture};
29+
30+
void bitmap_to_gl_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3dat_exception_t** throws) {
31+
s3dat_t* handle = me->parent;
32+
S3DAT_EXHANDLER_CALL(me, res, throws, __FILE__, __func__, __LINE__);
33+
34+
S3DAT_CHECK_TYPE(handle, res, "s3dat_bitmap_t", throws, __FILE__, __func__, __LINE__);
35+
36+
s3dat_bitmap_t* bitmap = res->resdata;
37+
38+
int tex_id;
39+
glGenTextures(1, &tex_id);
40+
glBindTexture(GL_TEXTURE_2D, tex_id);
41+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
42+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
43+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
44+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
45+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->width, bitmap->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->data);
46+
glBindTexture(GL_TEXTURE_2D, 0);
47+
48+
gltex_t* texhandle = handle->alloc_func(handle->mem_arg, sizeof(gltex_t));
49+
texhandle->parent = handle;
50+
texhandle->tex_id = tex_id;
51+
texhandle->width = bitmap->width;
52+
texhandle->height = bitmap->height;
53+
texhandle->xoff = bitmap->xoff;
54+
texhandle->yoff = bitmap->yoff;
55+
56+
s3dat_delete_bitmap(bitmap);
57+
58+
res->resdata = texhandle;
59+
res->restype = &gl_bitmap_type;
60+
}
61+
1462
void onresize(GLFWwindow* wnd, int w, int h) {
1563
width = w;
1664
height = h;
@@ -22,20 +70,6 @@ void onresize(GLFWwindow* wnd, int w, int h) {
2270
glMatrixMode(GL_MODELVIEW);
2371
}
2472

25-
void bitmaps_to_textures(int c, s3dat_bitmap_t** bitmaps, int* ida) {
26-
glGenTextures(c, ida);
27-
28-
for(int i = 0;i != c;i++) {
29-
glBindTexture(GL_TEXTURE_2D, ida[i]);
30-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
31-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
32-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
33-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
34-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmaps[i]->width, bitmaps[i]->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmaps[i]->data);
35-
}
36-
glBindTexture(GL_TEXTURE_2D, 0);
37-
}
38-
3973
int main() {
4074
s3dat_exception_t* ex = NULL;
4175

@@ -62,35 +96,32 @@ int main() {
6296

6397
onresize(wnd, width, height);
6498

65-
s3dat_bitmap_t* grass_bitmap;
66-
s3dat_bitmap_t* settler_bitmaps[72];
67-
s3dat_bitmap_t* torso_bitmaps[72];
99+
s3dat_extracthandler_t* exhandler1 = s3dat_new_exhandler(dat00), *exhandler2 = s3dat_new_exhandler(dat10);
100+
exhandler1->call = bitmap_to_gl_handler;
101+
exhandler2->call = bitmap_to_gl_handler;
102+
103+
s3dat_add_extracthandler(dat00, exhandler1);
104+
s3dat_add_extracthandler(dat10, exhandler2);
105+
106+
s3dat_add_cache(dat00);
107+
s3dat_add_cache(dat10);
68108

69-
grass_bitmap = s3dat_extract_landscape(dat00, 0, &ex);
109+
gltex_t* grass_tex = (gltex_t*) s3dat_extract_landscape(dat00, 0, &ex);
70110
s3dat_catch_exception(&ex, dat00);
71111

72-
int grass_tex;
73-
int settler_texs[72];
74-
int torso_texs[72];
112+
gltex_t* settler_texs[72];
113+
gltex_t* torso_texs[72];
75114

76115
int ex_s = 0;
77116

78117
for(int i = 0;i != 72;i++) {
79-
settler_bitmaps[i] = s3dat_extract_settler(dat10, ex_s, i, &ex);
118+
settler_texs[i] = (gltex_t*) s3dat_extract_settler(dat10, ex_s, i, &ex);
80119
s3dat_catch_exception(&ex, dat10);
81120

82-
torso_bitmaps[i] = s3dat_extract_torso(dat10, ex_s, i, &ex);
121+
torso_texs[i] = (gltex_t*) s3dat_extract_torso(dat10, ex_s, i, &ex);
83122
s3dat_catch_exception(&ex, dat10);
84123
}
85124

86-
bitmaps_to_textures(72, settler_bitmaps, settler_texs);
87-
bitmaps_to_textures(72, torso_bitmaps, torso_texs);
88-
bitmaps_to_textures(1, &grass_bitmap, &grass_tex);
89-
90-
//s3dat_delete_pixdatas(settler_bitmaps, 72);
91-
//s3dat_delete_pixdatas(torso_bitmaps, 72);
92-
//s3dat_delete_pixdata(grass_bitmap);
93-
94125
int settler_frame = 0xc;
95126

96127
double time = glfwGetTime();
@@ -103,43 +134,43 @@ int main() {
103134
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104135
glLoadIdentity();
105136

106-
int rows = floor(height / grass_bitmap->height)+1;
107-
int columns = floor(width / grass_bitmap->width)+1;
137+
int rows = floor(height / grass_tex->height)+1;
138+
int columns = floor(width / grass_tex->width)+1;
108139
for(int row = 0;row != rows;row++) {
109140
for(int column = 0;column != columns;column++) {
110-
glBindTexture(GL_TEXTURE_2D, grass_tex);
141+
glBindTexture(GL_TEXTURE_2D, grass_tex->tex_id);
111142
glPushMatrix();
112-
glTranslatef(column*grass_bitmap->width, row*grass_bitmap->height, 0);
143+
glTranslatef(column*grass_tex->width, row*grass_tex->height, 0);
113144
glBegin(GL_QUADS);
114145
glTexCoord2d(0, 0); glVertex2i(0, 0);
115-
glTexCoord2d(0, 1); glVertex2i(0, grass_bitmap->height);
116-
glTexCoord2d(1, 1); glVertex2i(grass_bitmap->width, grass_bitmap->height);
117-
glTexCoord2d(1, 0); glVertex2i(grass_bitmap->width, 0);
146+
glTexCoord2d(0, 1); glVertex2i(0, grass_tex->height);
147+
glTexCoord2d(1, 1); glVertex2i(grass_tex->width, grass_tex->height);
148+
glTexCoord2d(1, 0); glVertex2i(grass_tex->width, 0);
118149
glEnd();
119150
glPopMatrix();
120151
glBindTexture(GL_TEXTURE_2D, 0);
121152
}
122153
}
123154

124-
glBindTexture(GL_TEXTURE_2D, settler_texs[settler_frame]);
155+
glBindTexture(GL_TEXTURE_2D, settler_texs[settler_frame]->tex_id);
125156
glPushMatrix();
126-
glTranslatef(590-move_value+settler_bitmaps[settler_frame]->xoff-settler_bitmaps[0xc]->xoff, 100+settler_bitmaps[settler_frame]->yoff-settler_bitmaps[0xc]->yoff, 1);
157+
glTranslatef(590-move_value+settler_texs[settler_frame]->xoff-settler_texs[0xc]->xoff, 100+settler_texs[settler_frame]->yoff-settler_texs[0xc]->yoff, 1);
127158
glBegin(GL_QUADS);
128159
glTexCoord2d(0, 0); glVertex2i(0, 0);
129-
glTexCoord2d(0, 1); glVertex2i(0, settler_bitmaps[settler_frame]->height);
130-
glTexCoord2d(1, 1); glVertex2i(settler_bitmaps[settler_frame]->width, settler_bitmaps[settler_frame]->height);
131-
glTexCoord2d(1, 0); glVertex2i(settler_bitmaps[settler_frame]->width, 0);
160+
glTexCoord2d(0, 1); glVertex2i(0, settler_texs[settler_frame]->height);
161+
glTexCoord2d(1, 1); glVertex2i(settler_texs[settler_frame]->width, settler_texs[settler_frame]->height);
162+
glTexCoord2d(1, 0); glVertex2i(settler_texs[settler_frame]->width, 0);
132163
glEnd();
133164
glPopMatrix();
134165
glPushMatrix();
135166
glColor3f(1, 0, 0);
136-
glTranslatef(590-move_value+settler_bitmaps[settler_frame]->xoff-settler_bitmaps[0xc]->xoff, 100+settler_bitmaps[settler_frame]->yoff-settler_bitmaps[0xc]->yoff, 2);
137-
glBindTexture(GL_TEXTURE_2D, torso_texs[settler_frame]);
167+
glTranslatef(590-move_value+settler_texs[settler_frame]->xoff-settler_texs[0xc]->xoff, 100+settler_texs[settler_frame]->yoff-settler_texs[0xc]->yoff, 2);
168+
glBindTexture(GL_TEXTURE_2D, torso_texs[settler_frame]->tex_id);
138169
glBegin(GL_QUADS);
139170
glTexCoord2d(0, 0); glVertex2i(0, 0);
140-
glTexCoord2d(0, 1); glVertex2i(0, settler_bitmaps[settler_frame]->height);
141-
glTexCoord2d(1, 1); glVertex2i(settler_bitmaps[settler_frame]->width, settler_bitmaps[settler_frame]->height);
142-
glTexCoord2d(1, 0); glVertex2i(settler_bitmaps[settler_frame]->width, 0);
171+
glTexCoord2d(0, 1); glVertex2i(0, settler_texs[settler_frame]->height);
172+
glTexCoord2d(1, 1); glVertex2i(settler_texs[settler_frame]->width, settler_texs[settler_frame]->height);
173+
glTexCoord2d(1, 0); glVertex2i(settler_texs[settler_frame]->width, 0);
143174
glEnd();
144175
glPopMatrix();
145176
glBindTexture(GL_TEXTURE_2D, 0);
@@ -170,15 +201,9 @@ int main() {
170201
move_value += move_factor;
171202
}
172203

173-
glDeleteTextures(1, &grass_tex);
174-
glDeleteTextures(72, settler_texs);
175-
glDeleteTextures(72, torso_texs);
176204
glfwHideWindow(wnd);
177205
glfwDestroyWindow(wnd);
178206
glfwTerminate();
179-
s3dat_delete_bitmap_array(settler_bitmaps, 72);
180-
s3dat_delete_bitmap_array(torso_bitmaps, 72);
181-
s3dat_delete_bitmap(grass_bitmap);
182207
s3dat_delete(dat00);
183208
s3dat_delete(dat10);
184209
}

0 commit comments

Comments
 (0)