void mem_free(device_memory &mem) { if (mem.type == MEM_TEXTURE) { tex_free(mem); } else if (mem.device_pointer) { if (mem.type == MEM_DEVICE_ONLY) { util_aligned_free((void *)mem.device_pointer); } mem.device_pointer = 0; stats.mem_free(mem.device_size); mem.device_size = 0; } }
void mem_copy_to(device_memory &mem) { if (mem.type == MEM_TEXTURE) { tex_free(mem); tex_alloc(mem); } else if (mem.type == MEM_PIXELS) { assert(!"mem_copy_to not supported for pixels."); } else { if (!mem.device_pointer) { mem_alloc(mem); } /* copy is no-op */ } }
/** * Debug function for AI scripts to dump 2D array data (e.g. terrain tile weights). */ static void DumpImage(void* UNUSED(cbdata), std::wstring name, std::vector<u32> data, u32 w, u32 h, u32 max) { // TODO: this is totally not threadsafe. VfsPath filename = L"screenshots/aidump/" + name; if (data.size() != w*h) { debug_warn(L"DumpImage: data size doesn't match w*h"); return; } if (max == 0) { debug_warn(L"DumpImage: max must not be 0"); return; } const size_t bpp = 8; int flags = TEX_BOTTOM_UP|TEX_GREY; const size_t img_size = w * h * bpp/8; const size_t hdr_size = tex_hdr_size(filename); shared_ptr<u8> buf; AllocateAligned(buf, hdr_size+img_size, maxSectorSize); Tex t; if (tex_wrap(w, h, bpp, flags, buf, hdr_size, &t) < 0) return; u8* img = buf.get() + hdr_size; for (size_t i = 0; i < data.size(); ++i) img[i] = (u8)((data[i] * 255) / max); tex_write(&t, filename); tex_free(&t); }
void background_close(void) { tex_free(triangle); tex_free(background); }
/////////////////////////////////////////////////////////////////// // Load all sky textures void SkyManager::LoadSkyTextures() { for (size_t i = 0; i < ARRAY_SIZE(m_SkyTexture); ++i) { VfsPath path = VfsPath("art/textures/skies") / m_SkySet / (Path::String(s_imageNames[i])+L".dds"); CTextureProperties textureProps(path); textureProps.SetWrap(GL_CLAMP_TO_EDGE); CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps); texture->Prefetch(); m_SkyTexture[i] = texture; } glGenTextures(1, &m_SkyCubeMap); glBindTexture(GL_TEXTURE_CUBE_MAP, m_SkyCubeMap); int types[] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y }; const wchar_t* images[numTextures+1] = { L"front", L"back", L"right", L"left", L"top", L"top" }; for (size_t i = 0; i < numTextures+1; ++i) { VfsPath path = VfsPath("art/textures/skies") / m_SkySet / (Path::String(images[i])+L".dds"); shared_ptr<u8> file; size_t fileSize; g_VFS->LoadFile(path, file, fileSize); Tex tex; tex_decode(file, fileSize, &tex); tex_transform_to(&tex, (tex.flags | TEX_BOTTOM_UP | TEX_ALPHA) & ~(TEX_DXT | TEX_MIPMAPS)); u8* data = tex_get_data(&tex); if (types[i] == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y || types[i] == GL_TEXTURE_CUBE_MAP_POSITIVE_Y) { std::vector<u8> rotated(tex.dataSize); for (size_t y = 0; y < tex.h; ++y) { for (size_t x = 0; x < tex.w; ++x) { size_t invx = y, invy = tex.w-x-1; rotated[(y*tex.w + x) * 4 + 0] = data[(invy*tex.w + invx) * 4 + 0]; rotated[(y*tex.w + x) * 4 + 1] = data[(invy*tex.w + invx) * 4 + 1]; rotated[(y*tex.w + x) * 4 + 2] = data[(invy*tex.w + invx) * 4 + 2]; rotated[(y*tex.w + x) * 4 + 3] = data[(invy*tex.w + invx) * 4 + 3]; } } glTexImage2D(types[i], 0, GL_RGB, tex.w, tex.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, &rotated[0]); } else { glTexImage2D(types[i], 0, GL_RGB, tex.w, tex.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); } tex_free(&tex); } glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); }
void greed_gui_free(void) { tex_free(gui_tex); font_free(huge_font); font_free(big_font); font_free(small_font); }
int dgreed_main(int argc, const char** argv) { log_init("pview.log", LOG_LEVEL_INFO); video_init(800, 600, "PView"); rand_init(666); GuiDesc style = greed_gui_style(false); gui_init(&style); particles_init("greed_assets/", 5); TexHandle empty = tex_load("greed_assets/empty.png"); if(psystem_descs_count < 1) LOG_ERROR("No particle systems described!"); int active_backg = 0; int active_desc = 0; const char* active_desc_name = psystem_descs[active_desc].name; RectF gui_area = rectf(0.0f, 0.0f, 520.0f, 80.0f); RectF gui_area2 = rectf(0.0f, 500.0f, 280.0f, 600.0f); Vector2 button_prev_pos = vec2(10.0f, 10.0f); Vector2 button_next_pos = vec2(280.0f, 10.0f); Vector2 button_backg_pos = vec2(10.0f, 550.0f); Vector2 label_name_pos = vec2(20.0f, 60.0f); char label_text[256]; while(system_update()) { RectF src = rectf_null(); RectF dest = {0.0f, 0.0f, EDITOR_WIDTH, EDITOR_HEIGHT}; Color c = backgrounds[active_backg % ARRAY_SIZE(backgrounds)]; video_draw_rect(empty, 0, &src, &dest, c); if(mouse_down(MBTN_LEFT)) { uint x, y; mouse_pos(&x, &y); Vector2 pos = vec2((float)x, (float)y); if(!rectf_contains_point(&gui_area, &pos)) if(!rectf_contains_point(&gui_area2, &pos)) particles_spawn(active_desc_name, &pos, 0.0f); } particles_update(time_ms() / 1000.0f); sprintf(label_text, "Current psystem: %s", active_desc_name); gui_label(&label_name_pos, label_text); if(gui_button(&button_prev_pos, "Previuos")) active_desc = MAX(0, active_desc-1); if(gui_button(&button_next_pos, "Next")) active_desc = MIN(psystem_descs_count-1, active_desc+1); if(gui_button(&button_backg_pos, "Background color")) active_backg++; active_desc_name = psystem_descs[active_desc].name; particles_draw(); draw_grid(0, 12.0f); video_present(); } tex_free(empty); particles_close(); gui_close(); greed_gui_free(); video_close(); log_close(); return 0; }