Skip to content

Commit ff1746f

Browse files
refactor: delete buffers on stop
1 parent 3729004 commit ff1746f

4 files changed

Lines changed: 23 additions & 23 deletions

File tree

FreeFrame/Components/Shapes/Shape.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using System.Xml;
5-
using OpenTK.Graphics.OpenGL4;
1+
using OpenTK.Graphics.OpenGL4;
62
using OpenTK.Windowing.Desktop;
73

84
namespace FreeFrame.Components.Shapes
@@ -92,7 +88,13 @@ public void Draw()
9288
GL.BindVertexArray(_vertexArrayObject);
9389
GL.DrawElements(PrimitiveType.Triangles, _indexCount, DrawElementsType.UnsignedInt, 0);
9490
}
95-
91+
public void DeleteObjects()
92+
{
93+
GL.DeleteBuffer(_vertexBufferObject);
94+
GL.DeleteBuffer(_indexBufferObject);
95+
GL.DeleteVertexArray(_vertexArrayObject);
96+
_shader.Delete();
97+
}
9698
/// <summary>
9799
/// Should update the size and the position to the new Window size
98100
/// </summary>

FreeFrame/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static void Main()
1414
{
1515
NativeWindowSettings nativeWindowSettings = new()
1616
{
17-
Size = new Vector2i(600, 600),
17+
Size = new Vector2i(800, 600),
1818
Title = "FreeFrame"
1919
};
2020
using Window window = new(GameWindowSettings.Default, nativeWindowSettings); // Create window context (GLFW, OpenGL)

FreeFrame/Shader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public Shader(string uriVertexShader, string uriFragementShader)
4747
}
4848
public int GetUniformLocation(string uniform) => GL.GetUniformLocation(_program, uniform);
4949
public void Use() => GL.UseProgram(_program);
50-
~Shader() => GL.DeleteProgram(_program);
50+
public void Delete() => GL.DeleteProgram(_program);
51+
~Shader() => Delete();
5152
}
5253
}

FreeFrame/Window.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ protected override void OnRenderFrame(FrameEventArgs e)
7474

7575
if (_shapes != null)
7676
{
77-
7877
_shapes[0].UpdateProperties();
7978
_shapes[0].Draw();
8079
}
@@ -91,12 +90,14 @@ protected override void OnUnload()
9190
{
9291
base.OnUnload();
9392

93+
Console.WriteLine("Program stops");
94+
9495
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
9596
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
9697
GL.BindVertexArray(0);
9798
GL.UseProgram(0);
9899

99-
// TODO: Delete the buffer in the Shape class
100+
_shapes.ForEach(shape => shape.DeleteObjects());
100101
}
101102
public void ShowUI()
102103
{
@@ -106,6 +107,7 @@ public void ShowUI()
106107
ImGui.GetStyle().TabRounding = 0.0f;
107108

108109

110+
// Parameters side
109111
ImGui.Begin("Parameters", ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar);
110112
ImGui.SetWindowSize(new System.Numerics.Vector2(200, ClientSize.Y / 2));
111113
ImGui.SetWindowPos(new System.Numerics.Vector2(ClientSize.X - ImGui.GetWindowWidth(), 0));
@@ -125,10 +127,9 @@ public void ShowUI()
125127
ImGui.Text("Color");
126128
ImGui.Spacing();
127129
ImGui.ColorEdit4("Color", ref _ioColor);
128-
129130
ImGui.End();
130131

131-
132+
// Tree view side
132133
ImGui.Begin("Tree view", ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar);
133134
ImGui.SetWindowSize(new System.Numerics.Vector2(200, ClientSize.Y / 2));
134135
ImGui.SetWindowPos(new System.Numerics.Vector2(ClientSize.X - ImGui.GetWindowWidth(), ClientSize.Y / 2));
@@ -140,24 +141,23 @@ public void ShowUI()
140141
ImGui.Selectable("Rectangle");
141142
ImGui.End();
142143

143-
144+
// Animation side
144145
ImGui.Begin("Animation", ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar);
145146
ImGui.SetWindowSize(new System.Numerics.Vector2(ClientSize.X / 2, 200));
146147
ImGui.SetWindowPos(new System.Numerics.Vector2(0, ClientSize.Y - ImGui.GetWindowHeight()));
147148
ImGui.Text("Animation");
148149
ImGui.End();
149150

150-
151+
// Timeline side
151152
ImGui.Begin("Timeline", ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar);
152153
ImGui.SetWindowSize(new System.Numerics.Vector2(ClientSize.X / 2 - 200, 200));
153154
ImGui.SetWindowPos(new System.Numerics.Vector2(ClientSize.X / 2, ClientSize.Y - ImGui.GetWindowHeight()));
154155
ImGui.Text("Timeline");
155156
ImGui.Spacing();
156157
ImGui.SliderInt("(seconds)", ref _ioTimeline, 0, 60);
157-
158158
ImGui.End();
159159

160-
160+
// Navbar side
161161
ImGui.Begin("NavBar", ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.MenuBar);
162162
ImGui.SetWindowSize(new System.Numerics.Vector2(ClientSize.X - 200, 0));
163163
ImGui.SetWindowPos(new System.Numerics.Vector2(0, 0));
@@ -167,9 +167,7 @@ public void ShowUI()
167167
if (ImGui.BeginMenu("File"))
168168
{
169169
if (ImGui.MenuItem("Open..", "Ctrl+O"))
170-
{
171170
_dialogFilePicker = true;
172-
}
173171
if (ImGui.BeginMenu("Save"))
174172
{
175173
if (ImGui.MenuItem("Save as PNG", "Ctrl+S"))
@@ -199,10 +197,10 @@ public void ShowUI()
199197
ImGui.EndPopup();
200198
}
201199

200+
// File picker dialog
202201
if (_dialogFilePicker)
203202
ImGui.OpenPopup("open-file");
204-
205-
if (ImGui.BeginPopupModal("open-file"))
203+
if (ImGui.BeginPopupModal("open-file")) // ImGuiWindowFlags.AlwaysAutoResize
206204
{
207205
var picker = FilePicker.GetFilePicker(this, Path.Combine(Environment.CurrentDirectory, "Content/Atlases"), ".svg");
208206
if (picker.Draw())
@@ -211,18 +209,17 @@ public void ShowUI()
211209
_shapes.ForEach(shape => shape.GenerateObjects());
212210
FilePicker.RemoveFilePicker(this);
213211
if (compatibilityFlag)
214-
{
215212
_dialogCompatibility = true;
216-
}
217213

218214
}
219215
_dialogFilePicker = false;
220216
ImGui.EndPopup();
221217
}
222218

219+
// Compatibility alert
223220
if (_dialogCompatibility)
224221
ImGui.OpenPopup("Compatibility Problem");
225-
if (ImGui.BeginPopupModal("Compatibility Problem"))
222+
if (ImGui.BeginPopupModal("Compatibility Problem")) // ImGuiWindowFlags.AlwaysAutoResize
226223
{
227224
ImGui.Text("Some SVG elements are not compatible. Go to the list of compatible SVG elements");
228225
ImGui.Separator();

0 commit comments

Comments
 (0)