void* fsLowLevelAPI::newFreeTypeFont(const void* data, u32 data_size) { FT_Face face0; if (FT_New_Memory_Face(s_freetype, static_cast<const FT_Byte*>(data), data_size, 0, &face0)) { return NULL; } u32 face_num = face0->num_faces; void* font_info = fsMalloc(sizeof(u32) + sizeof(FT_Face) * face_num); *static_cast<u32*>(font_info) = face_num; FT_Face* faces = reinterpret_cast<FT_Face*>(static_cast<u32*>(font_info) + 1); faces[0] = face0; for (u32 i = 1; i < face_num; i++) { if (FT_New_Memory_Face(s_freetype, static_cast<const FT_Byte*>(data), data_size, i, &faces[i])) { return NULL; } } return font_info; }
//============================================================== void *MmTextFileLoad(const char *file) //-------------------------------------------------------------- // テキストファイルを読み込む // ※データ終端に '\0' を付加します //-------------------------------------------------------------- // in: file = ファイル名 //-------------------------------------------------------------- // out: 読み込みアドレス //============================================================== { sFILE *fp; size_t len; char *p; fp = FsOpen(file); ASSERT(fp); len = FsGetSizeOrig(fp); p = (char *)fsMalloc(len + 1, "text data"); ASSERT(p); FsRead(fp, p, len); FsClose(fp); *(p + len) = '\0'; return p; }
void fsUtil::loadShader(fsID shd_id, const char* vert_file, const char* frag_file, u8 uni_num, u8 att_num, u8 tex_num) { fsID vert_id = fsID::genID(); char* vert_code = NULL; if (vert_file) { fsResMgr::loadResourceAs(vert_id, vert_file, false); fsRes res = fsResMgr::getResource(vert_id); vert_code = static_cast<char*>(fsMalloc(res.getDataSize() + 1)); fsMemHelper::memcpy(vert_code, res.getData<void>(), res.getDataSize()); vert_code[res.getDataSize()] = '\0'; fsResMgr::removeResource(vert_id); } fsID frag_id = fsID::genID(); char* frag_code = NULL; if (frag_file) { fsResMgr::loadResourceAs(frag_id, frag_file, false); fsRes res = fsResMgr::getResource(frag_id); frag_code = static_cast<char*>(fsMalloc(res.getDataSize() + 1)); fsMemHelper::memcpy(frag_code, res.getData<void>(), res.getDataSize()); frag_code[res.getDataSize()] = '\0'; fsResMgr::removeResource(frag_id); } fsDrawMgr::newShader(shd_id, vert_code, frag_code, uni_num, att_num, tex_num); if (vert_code) { fsFree(vert_code); } if (frag_code) { fsFree(frag_code); } }
void InputRecordRead(sFILE *fp) { gInput[INP_CH0].record = FALSE; gInput[INP_CH0].playback = FALSE; u_int header; FsRead(fp, &header, sizeof(u_int)); record_num = ntohl(header); record_ofs = record_num; FreeWork(record); record = (sInputRecord *)fsMalloc(sizeof(sInputRecord) * record_num, "record"); ASSERT(record); FileBuffer buffer; size_t size = FsGetSize(fp) - 4; void *zptr = fsMalloc(size, "zlib"); ASSERT(zptr); FsRead(fp, zptr, size); buffer.ptr = (u_char *)ZlibDecode(zptr, ZlibEncodeSize(zptr)); buffer.ofs = 0; Free(zptr); sInputRecord *ptr = record; for(int i = 0; i < record_ofs; i += 1) { ptr->x = (int)getIntValue(&buffer); ptr->y = (int)getIntValue(&buffer); ptr->btn_p = getIntValue(&buffer); ptr->btn_td = getIntValue(&buffer); ptr->btn_tu = getIntValue(&buffer); RecKey *recKey = ptr->recKey; for(int h = 0; h < KEY_INPUT_BUFFER * 2; h += 1) { recKey->key = getCharValue(&buffer); recKey->push = (BOOL)getCharValue(&buffer); recKey += 1; } ptr->recNum = getIntValue(&buffer); ptr += 1; } Free(buffer.ptr); }
void InputSetRecData(u_char *ptr) { gInput[INP_CH0].record = FALSE; gInput[INP_CH0].playback = FALSE; FreeWork(record); record_num = ntohl(*(u_int *)ptr); record = (sInputRecord *)fsMalloc(sizeof(sInputRecord) * record_num, "record"); memcpy(record, ptr + 4, sizeof(sInputRecord) * record_num); /* FIXME:頭の4バイトがデータサイズになっている…が、他に良い実装を思い付かず */ }
PixelCube(u16 side_length, u16 pattern_num) { m_side_length = side_length; m_sq_side_length = m_side_length * m_side_length; m_pattern_num = pattern_num; m_cube_data_size = sizeof(Cube) * m_sq_side_length * m_side_length * m_pattern_num; m_cube_data = static_cast<Cube*>(fsMalloc(m_cube_data_size)); fsMemHelper::memset(m_cube_data, 1, m_cube_data_size); setCurPattern(0); }
PixelImage(u16 width, u16 height) { m_side_length = width / DIRECTION_NUM; m_pattern_num = height / m_side_length; m_pattern_size = PIXEL_SIZE * width * m_side_length; m_image_data_line_size = PIXEL_SIZE * width; m_image_data_size = PIXEL_SIZE * width * height; m_image_data = static_cast<u8*>(fsMalloc(m_image_data_size)); m_cur_direction = DIRECTION_FRONT; setCurPattern(0); }
void fsTex::resizeImage(u16 width, u16 height) { if (m_mode != MODE_READ_WRITE) { fsThrow(ExceptionInvalidCall); } if (width == 0 || height == 0 || width > fsDrawMgr::getMaxTextureLength() || height > fsDrawMgr::getMaxTextureLength()) { fsThrow(ExceptionInvalidArgument); } m_width = width; m_height = height; u16 valid_width = fsDrawMgr::getValidTextureLength(width); u16 valid_height = fsDrawMgr::getValidTextureLength(height); if (width != valid_width || height != valid_height) { m_flag.setOn(FLAG_UV_ADJUST); m_u_param_a = static_cast<r32>(width) / valid_width; m_u_param_b = 0.0f; m_v_param_a = static_cast<r32>(height) / valid_height; m_v_param_b = 0.0f; } else { m_flag.setOff(FLAG_UV_ADJUST); m_u_param_a = 1.0f; m_u_param_b = 0.0f; m_v_param_a = 1.0f; m_v_param_b = 0.0f; } if (m_image) { fsFree(const_cast<void*>(m_image)); } m_image_size = fsDrawMgr::getTexturePixelSize(m_format.getType()) * m_width * m_height; m_image = fsMalloc(m_image_size); m_flag.setOn(fsTex::FLAG_UPLOAD); }
void fsTex::expandAndRegisterTexture_png() { u16 valid_width = fsDrawMgr::getValidTextureLength(m_width); u16 valid_height = fsDrawMgr::getValidTextureLength(m_height); u16 pixel_size = fsDrawMgr::getTexturePixelSize(m_format.getType()); u16 src_line_size = m_width * pixel_size; u16 dest_line_size = valid_width * pixel_size; void* new_image = fsMalloc(valid_width * valid_height * pixel_size); fsUtil::readPNGImage(new_image, valid_width * valid_height * pixel_size, dest_line_size, m_image, m_image_size); u8* src = static_cast<u8*>(new_image) + src_line_size - pixel_size; u8* dest = src + pixel_size; if (m_width < valid_width) { for (u32 i = 0; i < m_height; i++) { for (u32 j = m_width; j < valid_width; j++) { fsMemHelper::memcpy(dest, src, pixel_size); dest += pixel_size; } src += dest_line_size; dest += src_line_size; } } src = static_cast<u8*>(new_image) + dest_line_size * (m_height - 1); dest = src + dest_line_size; for (u32 i = m_height; i < valid_height; i++) { fsMemHelper::memcpy(dest, src, dest_line_size); dest += dest_line_size; } m_tex_obj = fsLowLevelAPI::registerTexture( // valid_width, valid_height, static_cast<fsLowLevelAPI::TextureFormat>(m_format.getType() - FORMAT_PNG_RGB), new_image); fsFree(new_image); }
bool fsLowLevelAPI::createFreeType() { s_memory = static_cast<FT_Memory>(fsMalloc(sizeof(*s_memory))); s_memory->user = NULL; s_memory->alloc = myMalloc; s_memory->free = myFree; s_memory->realloc = myRealloc; if (FT_New_Library(s_memory, &s_freetype)) { fsFree(s_memory); return false; } FT_Add_Default_Modules(s_freetype); return true; }
void fsTex::expandAndRegisterTexture_ctx() { u16 valid_width = fsDrawMgr::getValidTextureLength(m_width); u16 valid_height = fsDrawMgr::getValidTextureLength(m_height); u16 pixel_size = fsDrawMgr::getTexturePixelSize(m_format.getType()); u16 src_line_size = m_width * pixel_size; u16 dest_line_size = valid_width * pixel_size; void* new_image = fsMalloc(valid_width * valid_height * pixel_size); const u8* src = static_cast<const u8*>(m_image); u8* dest = static_cast<u8*>(new_image); for (u32 i = 0; i < m_height; i++) { fsMemHelper::memcpy(dest, src, src_line_size); src += src_line_size - pixel_size; dest += src_line_size; for (u32 j = m_width; j < valid_width; j++) { fsMemHelper::memcpy(dest, src, pixel_size); dest += pixel_size; } src += pixel_size; } src = dest - dest_line_size; for (u32 i = m_height; i < valid_height; i++) { fsMemHelper::memcpy(dest, src, dest_line_size); dest += dest_line_size; } m_tex_obj = fsLowLevelAPI::registerTexture( // valid_width, valid_height, static_cast<fsLowLevelAPI::TextureFormat>(m_format.getType()), new_image); fsFree(new_image); }
void InputRecordStart(int rec_max) { gInput[INP_CH0].record = TRUE; gInput[INP_CH0].playback = FALSE; gInput[INP_CH0].rec_datamax = FALSE; for(int i = 0; i < INPUT_WORK_NUM; i += 1) { ZEROMEMORY(gInput[i].key_press, sizeof(gInput[i].key_press)); } FreeWork(record); record_num = INPUT_RECORD_SIZE; record_ofs = 0; record_max = rec_max; /* 0:無限 */ record = (sInputRecord *)fsMalloc(sizeof(sInputRecord) * record_num, "record"); record->recNum = 0; }
void fsTex::endEditImage(u16 x, u16 y, u16 width, u16 height) { if (m_flag.isOff(fsTex::FLAG_EDIT)) { fsThrow(ExceptionInvalidCall); } if (width == 0 || height == 0 || x + width > m_width || y + height > m_height) { fsThrow(ExceptionInvalidArgument); } m_flag.setOff(fsTex::FLAG_EDIT); if (m_flag.isOn(fsTex::FLAG_UPLOAD)) { return; } u16 pixel_size = fsDrawMgr::getTexturePixelSize(m_format.getType()); u32 image_line_size = m_width * pixel_size; u32 sub_image_line_size = width * pixel_size; u32 sub_image_size = sub_image_line_size * height; u8* sub_image = static_cast<u8*>(fsMalloc(sub_image_size)); const u8* src = static_cast<const u8*>(m_image) + (m_width * y + x) * pixel_size; u8* dest = sub_image; for (s32 i = 0; i < height; i++) { fsMemHelper::memcpy(dest, src, sub_image_line_size); src += image_line_size; dest += sub_image_line_size; } fsLowLevelAPI::replaceSubTexture(m_tex_obj, x, y, width, height, // static_cast<fsLowLevelAPI::TextureFormat>(m_format.getType()), sub_image); fsFree(sub_image); }
void InputRecordWrite(sFILE *fp) { if(record && record_ofs) { FileBuffer buffer; buffer.ptr = (u_char *)fsMalloc(256, "byteValue"); buffer.ofs = 0; buffer.size = 256; sInputRecord *ptr = record; for(int i = 0; i < record_ofs; i += 1) { putIntValue(&buffer, (u_int)ptr->x); putIntValue(&buffer, (u_int)ptr->y); putIntValue(&buffer, ptr->btn_p); putIntValue(&buffer, ptr->btn_td); putIntValue(&buffer, ptr->btn_tu); RecKey *recKey = ptr->recKey; for(int h = 0; h < KEY_INPUT_BUFFER * 2; h += 1) { putCharValue(&buffer, recKey->key); putCharValue(&buffer, recKey->push); recKey += 1; } putIntValue(&buffer, ptr->recNum); ptr += 1; } u_int header; header = htonl(record_ofs); FsWrite(fp, &header, sizeof(u_int)); void *zptr = ZlibEncode(buffer.ptr, buffer.ofs); FsWrite(fp, zptr, ZlibEncodeSize(zptr)); Free(zptr); Free(buffer.ptr); } }
bool fsLowLevelAPI::openSoundDevice(u8 channel_num, u16 sample_rate, u16 snd_mix_buf_msec, SoundMixFunction snd_mix_func) { fprintf(stderr,"channel_num %d sample_rate %d snd_mix_buf_mse %d \n",channel_num, sample_rate, snd_mix_buf_msec); if (isSoundDeviceOpen()) { closeSoundDevice(); } pthread_mutex_init(&s_snd_mix_mutex, NULL); s_snd_mix_func = snd_mix_func; m_pAudioDevice = alcOpenDevice(NULL); if (m_pAudioDevice) { m_pAlcContext = alcCreateContext(m_pAudioDevice, NULL); alcMakeContextCurrent(m_pAlcContext); } ALenum err = alGetError(); if (err != AL_NO_ERROR) { return false; } alGenSources(1, &m_AudioSource); alGenBuffers(BUFFER_COUNT, m_AudioBuffers); s_channel_num = channel_num; s_sample_rate = sample_rate; s_snd_mix_buf_msec = snd_mix_buf_msec; s_snd_mix_buf_sample_num = sample_rate * snd_mix_buf_msec / 1000; s_bits_per_sample = 16; if (s_channel_num != 1 && s_channel_num != 2) { return false; } switch (s_bits_per_sample){ case 8: m_AudioFormat = (channel_num == 1) ? AL_FORMAT_MONO8 : AL_FORMAT_STEREO8; break; case 16: m_AudioFormat = (channel_num == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; break; default: return 0; } // m_AudioFormat = AL_FORMAT_STEREO16; s_snd_mix_buf_size = 2 * s_channel_num * s_snd_mix_buf_sample_num; /*for (s32 i = 0; i < 2; i++) { s_snd_mix_buf[i] = ckMalloc(s_snd_mix_buf_size); ckMemMgr::memset(s_snd_mix_buf[i], 0, s_snd_mix_buf_size); }*/ buf = fsMalloc(s_snd_mix_buf_size); s_snd_mix_buf = fsMalloc(s_snd_mix_buf_size*2); //alGenBuffers(BUFFER_COUNT, m_AudioBuffers); //alGenSources(1, &m_AudioSource); //alSourcePlay(m_AudioSource); s_is_playing = true; if (pthread_create(&s_snd_play_thread, NULL, soundPlayThread, NULL) != 0) { s_is_playing = false; closeSoundDevice(); return false; } // if (pthread_create(&s_snd_play_thread, NULL, soundPlayThread, NULL) != 0) // { // s_is_playing = false; // closeSoundDevice(); // return false; // } // s_is_snd_dev_open = true; return true; }
fsTex::fsTex(fsID tex_id, u16 width, u16 height, TexFormat format, TexMode mode, const void* image, u32 image_size) { m_id = tex_id; m_width = width; m_height = height; m_format = format; m_mode = mode; m_tex_obj = 0; m_proxy_tex = NULL; m_flag.clear(); u16 valid_width = fsDrawMgr::getValidTextureLength(width); u16 valid_height = fsDrawMgr::getValidTextureLength(height); if (width != valid_width || height != valid_height) { m_flag.setOn(FLAG_UV_ADJUST); m_u_param_a = static_cast<r32>(width) / valid_width; m_u_param_b = 0.0f; m_v_param_a = static_cast<r32>(height) / valid_height; m_v_param_b = 0.0f; } else { m_u_param_a = 1.0f; m_u_param_b = 0.0f; m_v_param_a = 1.0f; m_v_param_b = 0.0f; } switch (m_mode.getType()) { case MODE_READ_ONLY: m_image = image; m_image_size = image_size; break; case MODE_READ_WRITE: m_image_size = fsDrawMgr::getTexturePixelSize(m_format.getType()) * m_width * m_height; m_image = fsMalloc(m_image_size); break; case MODE_FRAMEBUFFER: m_image = NULL; m_image_size = 0; m_flag.setOn(FLAG_UV_ADJUST); m_v_param_a = -m_v_param_a; m_v_param_b = 1.0f; break; default: fsThrow(ExceptionInvalidArgument); } m_flag.setOn(fsTex::FLAG_UPLOAD); fsDrawMgr::instance()->m_tex_map.add(tex_id, this); }
//============================================================== void PngWrite(char *file, int w, int h, u_char *ptr) //-------------------------------------------------------------- // データ出力 // 32bit → 32bit //-------------------------------------------------------------- // in: file = ファイル名 // w, h = データサイズ // ptr = データポインタ //-------------------------------------------------------------- // out: なし //============================================================== { sFILE *fp; png_structp png_ptr; png_infop info_ptr; png_color_8 sig_bit; png_text text_ptr[1]; png_bytep *row_pointers; int k; #ifdef USE_USER_MEMORY png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL, NULL, libpng_Malloc, libpng_Free); #else png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); #endif if(!png_ptr) { return; } info_ptr = png_create_info_struct(png_ptr); if(!info_ptr) { png_destroy_write_struct(&png_ptr, png_infopp_NULL); return; } // ファイルの新規作成 //-------------------- fp = FsCreate(file); ASSERT(fp); png_set_write_fn(png_ptr, (png_voidp)fp, writeFunc, flushFunc); png_set_IHDR(png_ptr, info_ptr, w, h, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_BASE); sig_bit.red = 8; sig_bit.green = 8; sig_bit.blue = 8; sig_bit.alpha = 0; png_set_sBIT(png_ptr, info_ptr, &sig_bit); text_ptr[0].key = "Description"; text_ptr[0].text = "ktcDIB::Save() Data"; text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; png_set_text(png_ptr, info_ptr, text_ptr, 1); png_write_info(png_ptr, info_ptr); png_set_bgr(png_ptr); row_pointers = (png_bytep *)fsMalloc(sizeof(png_bytep *) * h, "png_bytep"); ASSERT(row_pointers); for(k = 0; k < h; ++k) { png_byte *p; p = (png_byte *)fsMalloc(sizeof(png_byte) * w * 3, "png_byte"); ASSERT(p); row_pointers[k] = (png_bytep)p; memcpy(p, ptr, w * 3); ptr += w * 3; } png_write_image(png_ptr, row_pointers); png_write_end(png_ptr, info_ptr); for(k = 0; k < h; ++k) { Free(row_pointers[k]); } Free(row_pointers); png_destroy_write_struct(&png_ptr, &info_ptr); FsClose(fp); }
static void* myMalloc(FT_Memory, long size) { return fsMalloc(size); }
void fsUtil::calcNormalAsTriangles(fsVec* normal, const fsPrim::PrimData* prim_data, u16 vert_num, bool is_smoothing) { if (!normal || !prim_data || vert_num == 0) { fsThrow(ExceptionInvalidArgument); } if (vert_num < 3) { for (s32 i = 0; i < vert_num; i++) { normal[i] = fsVec::Z_UNIT; } return; } u16 tri_vert_num = (vert_num / 3) * 3; for (s32 i = 0; i < tri_vert_num; i += 3) { fsVec vec1 = prim_data[i + 1].pos - prim_data[i].pos; fsVec vec2 = prim_data[i + 2].pos - prim_data[i].pos; normal[i] = normal[i + 1] = normal[i + 2] = vec1.cross(vec2).normalize(); } for (s32 i = tri_vert_num; i < vert_num; i++) { normal[i] = fsVec::Z_UNIT; } if (!is_smoothing) { return; } u32 mark_buf_size = sizeof(u16) * vert_num; u16* mark_buf = static_cast<u16*>(fsMalloc(mark_buf_size)); fsMemHelper::memset(mark_buf, 0, mark_buf_size); for (s32 i = 0; i < vert_num; i++) { if (mark_buf[i] > 0) { continue; } u16 mark = i + 1; u16 mark_num = 1; const fsVec& pos1 = prim_data[i].pos; const fsVec& n1 = normal[i]; fsVec avgn = n1; mark_buf[i] = mark; for (s32 j = i + 1; j < vert_num; j++) { const fsVec& pos2 = prim_data[j].pos; const fsVec& n2 = normal[j]; if (pos1.x == pos2.x && pos1.y == pos2.y && pos1.z == pos2.z) { r32 inner = n1.dot(n2); if (inner > 0.7071f) // cos 45 { mark_buf[j] = mark; mark_num++; if (inner < 1.0f - fsMath::EPSILON) { avgn += n2; } } } } if (mark_num > 1) { if (avgn.x == 0.0f && avgn.y == 0.0f && avgn.z == 0.0f) { continue; } avgn = avgn.normalize(); for (s32 j = i; j < vert_num; j++) { if (mark_buf[j] == mark) { normal[j] = avgn; } } } } fsFree(mark_buf); }