Skip to content

Commit 99c93aa

Browse files
committed
fix c standard, add animation and search examples, update libs3util and extend gitignore
1 parent b112aeb commit 99c93aa

9 files changed

+523
-13
lines changed

.gitignore

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
build/
2-
SND/
3-
GFX/
4-
SND
5-
GFX
2+
SND*
3+
GFX*
64
snd.raw
75
src/config.h
86
release/
97
palette_dump*.data
108
alloc.log
9+
10+
#clion
11+
.idea
12+
cmake-build-*

CMakeLists.txt

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
cmake_minimum_required(VERSION 3.2.2)
22
project(libs3dat)
33

4+
option(PRIVATE_FILENAME "stop leaking complete path to file (should be enabled on distribution)" ON)
5+
46
add_subdirectory(libs3util)
57

68
include(CPack)
79
include(CTest)
810
include(TestBigEndian)
911

1012
set(CMAKE_C_FLAGS "-Wall")
11-
set(CMAKE_CXX_STANDARD 11)
12-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
13+
set(CMAKE_C_STANDARD 11)
14+
set(CMAKE_C_STANDARD_REQUIRED ON)
1315
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
1416
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
1517
set(CPACK_PACKAGE_NAME "libs3dat")
@@ -18,9 +20,8 @@ set(STRING_BUFFER 1024)
1820

1921
TEST_BIG_ENDIAN(IS_BE)
2022

21-
option(PRIVATE_FILENAME "stop leaking complete path to file (should be enabled on distribution)" ON)
2223

23-
configure_file(config.h.in ${PROJECT_BINARY_DIR}/config.h)
24+
configure_file(config.h.in ${PROJECT_BINARY_DIR}/config.s3dat.h)
2425

2526
include_directories(src ${PROJECT_BINARY_DIR} libs3util/src)
2627

@@ -40,11 +41,22 @@ target_link_libraries(gen_gui s3dat m)
4041
add_executable(invert invert.c)
4142
target_link_libraries(invert s3dat)
4243

44+
find_package(Qt5Widgets)
45+
46+
if(Qt5Widgets_FOUND)
47+
add_executable(search search.cpp)
48+
target_link_libraries(search s3dat Qt5::Widgets)
49+
endif()
50+
4351
find_package(glfw3)
4452
find_package(OpenGL)
4553

4654
if(glfw3_FOUND AND OpenGL_FOUND)
4755

56+
add_executable(animation animation.c)
57+
target_link_libraries(animation s3dat ${OPENGL_LIBRARIES} glfw m)
58+
target_include_directories(animation PUBLIC ${OPENGL_INCLUDE_DIRS} ${glfw3_INCLUDE_DIRS})
59+
4860
add_executable(walk walk.c)
4961
target_link_libraries(walk s3dat ${OPENGL_LIBRARIES} glfw m)
5062
target_include_directories(walk PUBLIC ${OPENGL_INCLUDE_DIRS} ${glfw3_INCLUDE_DIRS})

animation.c

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#include <s3dat_ext.h>
2+
#include <GL/gl.h>
3+
#include <GLFW/glfw3.h>
4+
5+
int width = 640, height = 360;
6+
7+
s3util_exception_t* ex;
8+
9+
10+
typedef struct {
11+
s3dat_t* parent;
12+
GLuint tex_id;
13+
uint16_t width;
14+
uint16_t height;
15+
int16_t xoff;
16+
int16_t yoff;
17+
} gltex_t;
18+
19+
#define GLTEX(res) ((gltex_t*)res->data.raw)
20+
21+
void delete_texture(gltex_t* tex) {
22+
glDeleteTextures(1, &tex->tex_id);
23+
s3util_free_func(s3dat_memset(tex->parent), tex);
24+
}
25+
26+
s3dat_restype_t gl_bitmap_type = {"gltex", (void (*) (void*)) delete_texture, NULL};
27+
28+
void bitmap_to_gl_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws) {
29+
s3dat_t* handle = me->parent;
30+
S3DAT_EXHANDLER_CALL(me, res, throws, __FILE__, __func__, __LINE__);
31+
32+
if(res->type != s3dat_landscape && res->type != s3dat_settler && res->type != s3dat_torso && res->type != s3dat_shadow) return;
33+
34+
S3DAT_CHECK_TYPE(handle, res, "s3dat_bitmap_t", throws, __FILE__, __func__, __LINE__);
35+
36+
gltex_t* texhandle = s3util_alloc_func(s3dat_memset(handle), sizeof(gltex_t), throws);
37+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
38+
39+
GLuint tex_id;
40+
glGenTextures(1, &tex_id);
41+
glBindTexture(GL_TEXTURE_2D, tex_id);
42+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
43+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
44+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
45+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
46+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s3dat_width(res->res), s3dat_height(res->res), 0, GL_RGBA, GL_UNSIGNED_BYTE, s3dat_bmpdata(res->res));
47+
glBindTexture(GL_TEXTURE_2D, 0);
48+
49+
texhandle->parent = handle;
50+
texhandle->tex_id = tex_id;
51+
texhandle->width = s3dat_width(res->res);
52+
texhandle->height = s3dat_height(res->res);
53+
texhandle->xoff = *s3dat_xoff(res->res);
54+
texhandle->yoff = *s3dat_yoff(res->res);
55+
56+
res->res->type->deref(res->res->data.raw);
57+
58+
res->res->data.raw = texhandle;
59+
res->res->type = &gl_bitmap_type;
60+
}
61+
62+
void draw_texture(s3dat_ref_t* ref, double x, double y, double z, double cr, double cg, double cb, double ca, bool flip) {
63+
glColor4d(cr, cg, cb, ca);
64+
glBindTexture(GL_TEXTURE_2D, GLTEX(ref)->tex_id);
65+
66+
glPushMatrix();
67+
glTranslated(x+GLTEX(ref)->xoff, y+GLTEX(ref)->yoff, z);
68+
glBegin(GL_QUADS);
69+
glTexCoord2d(0, flip); glVertex2i(0, 0);
70+
glTexCoord2d(0, !flip); glVertex2i(0, GLTEX(ref)->height);
71+
glTexCoord2d(1, !flip); glVertex2i(GLTEX(ref)->width, GLTEX(ref)->height);
72+
glTexCoord2d(1, flip); glVertex2i(GLTEX(ref)->width, 0);
73+
glEnd();
74+
glPopMatrix();
75+
}
76+
77+
void onresize(GLFWwindow* wnd, int w, int h) {
78+
width = w;
79+
height = h;
80+
81+
glViewport(0, 0, width, height);
82+
glMatrixMode(GL_PROJECTION);
83+
glLoadIdentity();
84+
glOrtho(0, width, height, 0, -10, 1);
85+
glMatrixMode(GL_MODELVIEW);
86+
}
87+
88+
int main(int argc, char** argv) {
89+
s3dat_t* ani_file = NULL;
90+
s3dat_ref_t* ani = NULL;
91+
92+
s3dat_t* img_gfx = NULL;
93+
94+
uint32_t ani_index = 0;
95+
char* ani_file_name = "GFX/Siedler3_25.f8007e01f.dat";
96+
if(argc == 3) {
97+
ani_index = atoi(argv[2]);
98+
ani_file_name = argv[1];
99+
} else if(argc > 1) {
100+
printf("%s [file] [ani_index]\n", argv[0]);
101+
return 1;
102+
}
103+
104+
ani_file = s3dat_new_malloc();
105+
s3dat_readfile_name(ani_file, ani_file_name, &ex);
106+
s3util_catch_exception(&ex);
107+
108+
ani = s3dat_extract_animation(ani_file, ani_index, &ex);
109+
s3util_catch_exception(&ex);
110+
111+
uint32_t ani_len = s3dat_anilen(ani);
112+
113+
uint32_t file = ani_len > 0 ? s3dat_frame(ani, 0)->settler_file : 0;
114+
115+
116+
char img_name[31];
117+
snprintf(img_name, 30, "GFX/Siedler3_%.2i.f8007e01f.dat", file);
118+
img_gfx = s3dat_new_malloc();
119+
s3dat_readfile_name(img_gfx, img_name, &ex);
120+
s3util_catch_exception(&ex);
121+
122+
s3dat_extracthandler_t* exhandler = s3dat_new_exhandler(img_gfx, &ex);
123+
s3util_catch_exception(&ex);
124+
exhandler->call = bitmap_to_gl_handler;
125+
s3dat_add_extracthandler(img_gfx, exhandler);
126+
127+
s3dat_ref_t* settler_frames[ani_len];
128+
s3dat_ref_t* torso_frames[ani_len];
129+
s3dat_ref_t* shadow_frames[ani_len];
130+
131+
glfwInit();
132+
133+
GLFWwindow* wnd = glfwCreateWindow(width, height, "animation", NULL, NULL);
134+
glfwSetFramebufferSizeCallback(wnd, onresize);
135+
glfwMakeContextCurrent(wnd);
136+
glfwSwapInterval(1);
137+
138+
glEnable(GL_TEXTURE_2D);
139+
glEnable(GL_DEPTH_TEST);
140+
glEnable(GL_BLEND);
141+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
142+
143+
onresize(wnd, width, height);
144+
145+
for(uint32_t i = 0;i != ani_len;i++) {
146+
s3dat_frame_t* frame = s3dat_frame(ani, i);
147+
printf("%i:%i:%i %i:%i:%i %i:%i:%i\n", frame->settler_file, frame->settler_id, frame->settler_frame,
148+
frame->torso_file, frame->torso_id, frame->torso_frame,
149+
frame->shadow_file, frame->shadow_id, 0);
150+
151+
settler_frames[i] = s3dat_extract_settler(img_gfx, frame->settler_id, frame->settler_frame, &ex);
152+
s3util_catch_exception(&ex);
153+
154+
if(frame->torso_id != 65535) {
155+
torso_frames[i] = s3dat_extract_torso(img_gfx, frame->torso_id, frame->torso_frame, &ex);
156+
s3util_catch_exception(&ex);
157+
} else {
158+
torso_frames[i] = NULL;
159+
}
160+
161+
shadow_frames[i] = s3dat_extract_shadow(img_gfx, frame->shadow_id, frame->torso_frame, &ex);
162+
s3util_catch_exception(&ex);
163+
}
164+
165+
double time = glfwGetTime();
166+
uint32_t frame = 0;
167+
168+
glClearColor(0.8, 0.8, 0.8, 1);
169+
while(!glfwWindowShouldClose(wnd)) {
170+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
171+
glLoadIdentity();
172+
173+
draw_texture(shadow_frames[frame], 100, 100, 0, 1, 1, 0, 1, false);
174+
draw_texture(settler_frames[frame], 100, 100, 1, 1, 1, 1, 1, false);
175+
if(torso_frames[frame]) draw_texture(torso_frames[frame], 100, 100, 2, 1, 1, 0, 1, false);
176+
177+
if(glfwGetTime()>time+0.08) {
178+
time = glfwGetTime();
179+
frame++;
180+
frame %= ani_len;
181+
}
182+
183+
glfwSwapBuffers(wnd);
184+
glfwPollEvents();
185+
}
186+
187+
if(ani != NULL) s3dat_unref(ani);
188+
if(ani_file != NULL) s3dat_delete(ani_file);
189+
190+
for(uint32_t i = 0;i != ani_len;i++) {
191+
s3dat_unref(settler_frames[i]);
192+
if(torso_frames[i]) s3dat_unref(torso_frames[i]);
193+
s3dat_unref(shadow_frames[i]);
194+
}
195+
196+
if(img_gfx != NULL) s3dat_delete(img_gfx);
197+
198+
glfwTerminate();
199+
200+
}

cli.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int main() {
8181
s3dat_ref_t* strings[8];
8282
for(uint16_t l = 0;l != 8;l++) {
8383
strings[l] = s3dat_extract_string(handle, s, l, &ex);
84-
if(s3dat_catch_exception(&ex)) {
84+
if(s3util_catch_exception(&ex)) {
8585
printf("%s", s3dat_strdata(strings[l]));
8686
}
8787
if(l != 7) printf("|");

config.h.in

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef S3DAT_CONFIG_H
22
#define S3DAT_CONFIG_H
33

4-
#cmakedefine USE_ICONV
54
#cmakedefine IS_BE
65
#cmakedefine PRIVATE_FILENAME
76
#cmakedefine STRING_BUFFER @STRING_BUFFER@

0 commit comments

Comments
 (0)