|
| 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 | +} |
0 commit comments