mirror of
https://github.com/dpethes/rerogue.git
synced 2025-06-07 18:58:32 +02:00
model viewer: plug memleaks
This commit is contained in:
parent
d5df461d58
commit
701e357590
@ -66,9 +66,9 @@ type
|
|||||||
_materials: TMaterialArray;
|
_materials: TMaterialArray;
|
||||||
_objects: TRenderObjectList;
|
_objects: TRenderObjectList;
|
||||||
_current_robject: TRenderObject;
|
_current_robject: TRenderObject;
|
||||||
|
_model_loaded: boolean;
|
||||||
|
|
||||||
_hmt: THmtFile;
|
procedure DeallocObjects;
|
||||||
_hmt_loaded: boolean;
|
|
||||||
procedure HmtRead(stream: TMemoryStream);
|
procedure HmtRead(stream: TMemoryStream);
|
||||||
procedure HobRead(stream: TMemoryStream);
|
procedure HobRead(stream: TMemoryStream);
|
||||||
procedure HobTransform(const hobject: THobObject);
|
procedure HobTransform(const hobject: THobObject);
|
||||||
@ -77,7 +77,7 @@ type
|
|||||||
constructor Create;
|
constructor Create;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
procedure Load(hob, hmt: TMemoryStream);
|
procedure Load(hob, hmt: TMemoryStream);
|
||||||
procedure InitGL;
|
procedure InitGL(hmt: THmtFile);
|
||||||
procedure DrawGL(var opts: TRenderOpts);
|
procedure DrawGL(var opts: TRenderOpts);
|
||||||
procedure ExportObj(const obj_name: string; const png_textures: boolean);
|
procedure ExportObj(const obj_name: string; const png_textures: boolean);
|
||||||
end;
|
end;
|
||||||
@ -111,7 +111,6 @@ var
|
|||||||
current_block_vertices: TVertexList;
|
current_block_vertices: TVertexList;
|
||||||
triangle: TTriangle;
|
triangle: TTriangle;
|
||||||
fg_idx: integer;
|
fg_idx: integer;
|
||||||
tris: TTriangleList;
|
|
||||||
|
|
||||||
function InitVertex(face: THobFace; offset: integer): TTriangle;
|
function InitVertex(face: THobFace; offset: integer): TTriangle;
|
||||||
var
|
var
|
||||||
@ -160,15 +159,12 @@ begin
|
|||||||
robjpart.vertices.PushBack(v);
|
robjpart.vertices.PushBack(v);
|
||||||
current_block_vertices.PushBack(v);
|
current_block_vertices.PushBack(v);
|
||||||
end;
|
end;
|
||||||
tris := TTriangleList.Create;
|
|
||||||
for i := 0 to fg.face_count - 1 do begin
|
for i := 0 to fg.face_count - 1 do begin
|
||||||
triangle := InitVertex(fg.faces[i], 0);
|
triangle := InitVertex(fg.faces[i], 0);
|
||||||
tris.PushBack(triangle);
|
|
||||||
robjpart.triangles.PushBack(triangle);
|
robjpart.triangles.PushBack(triangle);
|
||||||
|
|
||||||
if fg.faces[i].ftype <> 3 then begin
|
if fg.faces[i].ftype <> 3 then begin
|
||||||
triangle := InitVertex(fg.faces[i], 2);
|
triangle := InitVertex(fg.faces[i], 2);
|
||||||
tris.PushBack(triangle);
|
|
||||||
robjpart.triangles.PushBack(triangle);
|
robjpart.triangles.PushBack(triangle);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -188,27 +184,31 @@ var
|
|||||||
hob: THobFile;
|
hob: THobFile;
|
||||||
begin
|
begin
|
||||||
hob := ParseHobFile(stream);
|
hob := ParseHobFile(stream);
|
||||||
_objects.Clear;
|
|
||||||
if hob.obj_count = 0 then exit;
|
if hob.obj_count = 0 then exit;
|
||||||
|
|
||||||
for i := 0 to hob.obj_count-1 do
|
for i := 0 to hob.obj_count-1 do
|
||||||
HobTransform(hob.objects[i]);
|
HobTransform(hob.objects[i]);
|
||||||
|
|
||||||
|
DeallocHob(hob);
|
||||||
|
|
||||||
//WriteLn('vertices: ', _vertices.Size);
|
//WriteLn('vertices: ', _vertices.Size);
|
||||||
//WriteLn('faces (triangulated): ', _triangles.Count);
|
//WriteLn('faces (triangulated): ', _triangles.Count);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
procedure TModel.HmtRead(stream: TMemoryStream);
|
procedure TModel.HmtRead(stream: TMemoryStream);
|
||||||
|
var
|
||||||
|
hmt: THmtFile;
|
||||||
|
|
||||||
procedure SetTexByName (var mat: TMaterial; const name: string);
|
procedure SetTexByName (var mat: TMaterial; const name: string);
|
||||||
var
|
var
|
||||||
i: integer;
|
i: integer;
|
||||||
tex: THmtTexture;
|
tex: THmtTexture;
|
||||||
begin
|
begin
|
||||||
mat.has_texture := false;
|
mat.has_texture := false;
|
||||||
for i := 0 to _hmt.texture_count - 1 do
|
for i := 0 to hmt.texture_count - 1 do
|
||||||
if _hmt.textures[i].name_string = name then begin
|
if hmt.textures[i].name_string = name then begin
|
||||||
tex := _hmt.textures[i];
|
tex := hmt.textures[i];
|
||||||
|
|
||||||
mat.bpp := 24;
|
mat.bpp := 24;
|
||||||
if tex.image.type_ = 3 then
|
if tex.image.type_ = 3 then
|
||||||
@ -229,16 +229,32 @@ var
|
|||||||
i: integer;
|
i: integer;
|
||||||
name: string;
|
name: string;
|
||||||
begin
|
begin
|
||||||
_hmt := ParseHmtFile(stream);
|
hmt := ParseHmtFile(stream);
|
||||||
SetLength(_materials, _hmt.material_count);
|
SetLength(_materials, hmt.material_count);
|
||||||
for i := 0 to _hmt.material_count - 1 do begin
|
for i := 0 to hmt.material_count - 1 do begin
|
||||||
name := _hmt.materials[i].name_string; //preserve for obj/mtl export
|
name := hmt.materials[i].name_string; //preserve for obj/mtl export
|
||||||
_materials[i].name := name;
|
_materials[i].name := name;
|
||||||
writeln('material: ', name);
|
writeln('material: ', name);
|
||||||
SetTexByName(_materials[i], name);
|
SetTexByName(_materials[i], name);
|
||||||
end;
|
end;
|
||||||
|
InitGL(hmt);
|
||||||
|
DeallocHmt(hmt);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TModel.DeallocObjects;
|
||||||
|
var
|
||||||
|
i, k: integer;
|
||||||
|
begin
|
||||||
|
if _objects.Size = 0 then
|
||||||
|
exit;
|
||||||
|
for i := 0 to _objects.Size - 1 do begin
|
||||||
|
for k := 0 to _objects[i].parts.Size - 1 do begin
|
||||||
|
_objects[i].parts[k].vertices.Free;
|
||||||
|
_objects[i].parts[k].triangles.Free;
|
||||||
|
end;
|
||||||
|
_objects[i].parts.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
constructor TModel.Create;
|
constructor TModel.Create;
|
||||||
begin
|
begin
|
||||||
@ -248,20 +264,23 @@ end;
|
|||||||
destructor TModel.Destroy;
|
destructor TModel.Destroy;
|
||||||
begin
|
begin
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
|
DeallocObjects;
|
||||||
_materials := nil;
|
_materials := nil;
|
||||||
_objects.Free;
|
_objects.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TModel.Load(hob, hmt: TMemoryStream);
|
procedure TModel.Load(hob, hmt: TMemoryStream);
|
||||||
begin
|
begin
|
||||||
if _hmt_loaded then
|
if _model_loaded then begin
|
||||||
DeallocHmt(_hmt);
|
DeallocObjects;
|
||||||
|
_objects.Clear;
|
||||||
|
end;
|
||||||
|
|
||||||
WriteLn('Loading mesh file');
|
WriteLn('Loading mesh file');
|
||||||
HobRead(hob);
|
HobRead(hob);
|
||||||
WriteLn('Loading material file');
|
WriteLn('Loading material file');
|
||||||
HmtRead(hmt);
|
HmtRead(hmt);
|
||||||
_hmt_loaded := true;
|
_model_loaded := true;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure pnm_save(const fname: string; const p: pbyte; const w, h: integer);
|
procedure pnm_save(const fname: string; const p: pbyte; const w, h: integer);
|
||||||
@ -290,7 +309,7 @@ Begin
|
|||||||
CloseFile (f);
|
CloseFile (f);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TModel.InitGL;
|
procedure TModel.InitGL(hmt: THmtFile);
|
||||||
|
|
||||||
procedure GenTexture(var mat: TMaterial);
|
procedure GenTexture(var mat: TMaterial);
|
||||||
begin
|
begin
|
||||||
@ -321,9 +340,7 @@ procedure TModel.InitGL;
|
|||||||
var
|
var
|
||||||
i: integer;
|
i: integer;
|
||||||
begin
|
begin
|
||||||
if not _hmt_loaded then
|
for i := 0 to hmt.material_count - 1 do begin
|
||||||
exit;
|
|
||||||
for i := 0 to _hmt.material_count - 1 do begin
|
|
||||||
if _materials[i].has_texture then
|
if _materials[i].has_texture then
|
||||||
GenTexture(_materials[i]);
|
GenTexture(_materials[i]);
|
||||||
end;
|
end;
|
||||||
@ -337,7 +354,7 @@ procedure TModel.DrawGL(var opts: TRenderOpts);
|
|||||||
mat: TMaterial;
|
mat: TMaterial;
|
||||||
k: Integer;
|
k: Integer;
|
||||||
begin
|
begin
|
||||||
if _hmt_loaded then begin
|
if _model_loaded then begin
|
||||||
mat := _materials[tri.material_index];
|
mat := _materials[tri.material_index];
|
||||||
if mat.has_texture then begin
|
if mat.has_texture then begin
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
@ -152,6 +152,9 @@
|
|||||||
</Checks>
|
</Checks>
|
||||||
<VerifyObjMethodCallValidity Value="True"/>
|
<VerifyObjMethodCallValidity Value="True"/>
|
||||||
</CodeGeneration>
|
</CodeGeneration>
|
||||||
|
<Other>
|
||||||
|
<CustomOptions Value="-dDebug"/>
|
||||||
|
</Other>
|
||||||
</CompilerOptions>
|
</CompilerOptions>
|
||||||
<Debugging>
|
<Debugging>
|
||||||
<Exceptions Count="2">
|
<Exceptions Count="2">
|
||||||
|
@ -262,7 +262,6 @@ begin
|
|||||||
|
|
||||||
g_model := TModel.Create;
|
g_model := TModel.Create;
|
||||||
g_model.Load(hob, hmt);
|
g_model.Load(hob, hmt);
|
||||||
g_model.InitGL;
|
|
||||||
|
|
||||||
hob.Free;
|
hob.Free;
|
||||||
hmt.Free;
|
hmt.Free;
|
||||||
@ -598,13 +597,14 @@ begin
|
|||||||
//WindowScreenshot( surface^.w, surface^.h );
|
//WindowScreenshot( surface^.w, surface^.h );
|
||||||
end;
|
end;
|
||||||
|
|
||||||
WindowFree;
|
{$ifdef DEBUG}
|
||||||
SDL_Quit;
|
|
||||||
|
|
||||||
if g_model <> nil then
|
if g_model <> nil then
|
||||||
g_model.Free;
|
g_model.Free;
|
||||||
|
WindowFree;
|
||||||
|
SDL_Quit;
|
||||||
g_filelist.Free;
|
g_filelist.Free;
|
||||||
g_rs_files.Free;
|
g_rs_files.Free;
|
||||||
rsdata.Free;
|
rsdata.Free;
|
||||||
|
{$endif}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -65,6 +65,7 @@ type
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function ParseHobFile(f: TMemoryStream): THobFile;
|
function ParseHobFile(f: TMemoryStream): THobFile;
|
||||||
|
procedure DeallocHob(var h: THobFile);
|
||||||
|
|
||||||
//**************************************************************************************************
|
//**************************************************************************************************
|
||||||
implementation
|
implementation
|
||||||
@ -348,5 +349,23 @@ begin
|
|||||||
result := hob;
|
result := hob;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure DeallocHob(var h: THobFile);
|
||||||
|
var
|
||||||
|
o: THobObject;
|
||||||
|
fg: THobFaceGroup;
|
||||||
|
i, k: Integer;
|
||||||
|
begin
|
||||||
|
for k := 0 to Length(h.objects) - 1 do begin
|
||||||
|
o := h.objects[k];
|
||||||
|
for i := 0 to Length(o.object_parts) - 1 do begin
|
||||||
|
fg := o.object_parts[i];
|
||||||
|
fg.vertices := nil;
|
||||||
|
fg.faces := nil;
|
||||||
|
end;
|
||||||
|
o.object_parts := nil;
|
||||||
|
end;
|
||||||
|
h.objects := nil;
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user