Skip to content

Commit f07d680

Browse files
committed
view class rework
1 parent c6e48db commit f07d680

20 files changed

+556
-471
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ libAudio:
4545
rm bin/Audio.o bin/NoteDetector.o bin/notes.o bin/VorbisDecoder.o
4646

4747
exeopensmith:
48-
g++ $(CXXFLAGS) -I. -Iinclude opensmith/Camera.cpp opensmith/Controller.cpp opensmith/Filesystem.cpp opensmith/GameState.cpp opensmith/Hud.cpp opensmith/main.cpp opensmith/Menu.cpp opensmith/Mesh.cpp opensmith/Model.cpp opensmith/Setup.cpp opensmith/Sprite.cpp opensmith/Text2D.cpp opensmith/Tuner.cpp opensmith/util.cpp opensmith/View.cpp bin/libAudio.a bin/libPsarcReader.a bin/libRijndael.a bin/libSettings.a bin/libWem.a $(PORTAUDIO) -lz `pkg-config --static --libs glew` `pkg-config --static --libs glfw3` `pkg-config --static --libs ogg` `pkg-config --static --libs vorbis` $(OSXGL) -o bin/opensmith
48+
g++ $(CXXFLAGS) -I. -Iinclude opensmith/Camera.cpp opensmith/Controller.cpp opensmith/Filesystem.cpp opensmith/GameState.cpp opensmith/Hud.cpp opensmith/main.cpp opensmith/Menu.cpp opensmith/Mesh.cpp opensmith/Model.cpp opensmith/Setup.cpp opensmith/Sprite.cpp opensmith/Text2D.cpp opensmith/Texture.cpp opensmith/Tuner.cpp opensmith/util.cpp opensmith/View.cpp bin/libAudio.a bin/libPsarcReader.a bin/libRijndael.a bin/libSettings.a bin/libWem.a $(PORTAUDIO) -lz `pkg-config --static --libs glew` `pkg-config --static --libs glfw3` `pkg-config --static --libs ogg` `pkg-config --static --libs vorbis` $(OSXGL) -o bin/opensmith
4949

5050
exeSetup:
5151
g++ $(CXXFLAGS) -I. -Iinclude Setup/Setup.cpp bin/libAudio.a bin/libSettings.a $(PORTAUDIO) -o bin/Setup

opensmith/GameState.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "GameState.h"
2-
#include "GLFW/glfw3.h"
32
#include "Menu.h"
43
#include "Settings/Settings.h"
54
#include "View.h"

opensmith/GameState.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <GL/glew.h>
23
#include <GLFW/glfw3.h>
34
#include "View.h"
45
#include "Model.h"

opensmith/Menu.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -307,45 +307,34 @@ TuningMenu::TuningMenu()
307307
items.push_back("E Standard");
308308
items.push_back("Eb Standard");
309309
items.push_back("Drop D");
310+
items.push_back("Drop C#");
311+
items.push_back("Drop C");
310312
}
311313

312314
void TuningMenu::keyEnter()
313315
{
314-
if (selectedItem == 0)
316+
switch (selectedItem)
315317
{
318+
case 0:
316319
delete gameState;
317-
std::vector<int> tuning;
318-
tuning.push_back(40);
319-
tuning.push_back(45);
320-
tuning.push_back(50);
321-
tuning.push_back(55);
322-
tuning.push_back(59);
323-
tuning.push_back(64);
324-
gameState = new Tuner(tuning, true);
325-
}
326-
if (selectedItem == 1)
327-
{
320+
gameState = new Tuner({ 40, 45, 50, 55, 59, 64 }, true);
321+
break;
322+
case 1:
328323
delete gameState;
329-
std::vector<int> tuning;
330-
tuning.push_back(39);
331-
tuning.push_back(44);
332-
tuning.push_back(49);
333-
tuning.push_back(54);
334-
tuning.push_back(58);
335-
tuning.push_back(63);
336-
gameState = new Tuner(tuning, true);
337-
}
338-
if (selectedItem == 2)
339-
{
324+
gameState = new Tuner({ 39, 44, 49, 54, 58, 63 }, true);
325+
break;
326+
case 2:
327+
delete gameState;
328+
gameState = new Tuner({ 38, 45, 50, 55, 59, 64 }, true);
329+
break;
330+
case 3:
331+
delete gameState;
332+
gameState = new Tuner({ 37, 44, 49, 54, 58, 63 }, true);
333+
break;
334+
case 4:
340335
delete gameState;
341-
std::vector<int> tuning;
342-
tuning.push_back(38);
343-
tuning.push_back(45);
344-
tuning.push_back(50);
345-
tuning.push_back(55);
346-
tuning.push_back(59);
347-
tuning.push_back(64);
348-
gameState = new Tuner(tuning, true);
336+
gameState = new Tuner({ 36, 43, 48, 53, 57, 62 }, true);
337+
break;
349338
}
350339
}
351340

opensmith/Mesh.cpp

Lines changed: 97 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,108 @@ MeshSet::~MeshSet()
1212
for (auto m : meshes) delete m;
1313
}
1414

15+
int MeshSet::get(std::string name)
16+
{
17+
auto id = names.find(name);
18+
if (id != names.end())
19+
return id->second;
20+
else
21+
throw std::runtime_error("Model not loaded.\n");
22+
}
23+
24+
int MeshSet::add(std::string name)
25+
{
26+
int id = meshes.size();
27+
names[name] = id;
28+
return id;
29+
}
30+
1531
int MeshSet::loadMesh(const char* fileName)
1632
{
17-
size_t id = meshes.size();
33+
int id = add(fileName);
1834
loadOBJ(fileName);
1935
return id;
2036
}
2137

22-
int MeshSet::generateTiledQuads(size_t tileCount)
38+
int MeshSet::loadOBJ(const char* fileName)
2339
{
24-
size_t id = meshes.size();
40+
std::ifstream file(fileName, std::ifstream::in | std::ifstream::binary);
41+
if (!file.is_open())
42+
{
43+
printf("OBJ file could not be opened\n");
44+
return 0;
45+
}
46+
47+
std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
48+
std::vector<glm::vec3> temp_vertices;
49+
std::vector<glm::vec2> temp_uvs;
50+
std::vector<glm::vec3> temp_normals;
51+
52+
while (!file.eof())
53+
{
54+
char line[128];
55+
file.getline(line, 128);
56+
57+
glm::vec3 vertex;
58+
if (sscanf(line, "v %f %f %f\n", &vertex.x, &vertex.y, &vertex.z) == 3)
59+
temp_vertices.push_back(vertex);
60+
61+
glm::vec2 uv;
62+
if (sscanf(line, "vt %f %f\n", &uv.x, &uv.y) == 2)
63+
{
64+
uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
65+
temp_uvs.push_back(uv);
66+
}
67+
68+
glm::vec3 normal;
69+
if (sscanf(line, "vn %f %f %f\n", &normal.x, &normal.y, &normal.z) == 3)
70+
temp_normals.push_back(normal);
71+
72+
std::string vertex1, vertex2, vertex3;
73+
unsigned int vIndex[3], uvIndex[3], nIndex[3];
74+
if (sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d\n", &vIndex[0], &uvIndex[0], &nIndex[0], &vIndex[1], &uvIndex[1], &nIndex[1], &vIndex[2], &uvIndex[2], &nIndex[2]) == 9)
75+
{
76+
vertexIndices.push_back(vIndex[0]);
77+
vertexIndices.push_back(vIndex[1]);
78+
vertexIndices.push_back(vIndex[2]);
79+
uvIndices.push_back(uvIndex[0]);
80+
uvIndices.push_back(uvIndex[1]);
81+
uvIndices.push_back(uvIndex[2]);
82+
normalIndices.push_back(nIndex[0]);
83+
normalIndices.push_back(nIndex[1]);
84+
normalIndices.push_back(nIndex[2]);
85+
}
86+
}
87+
88+
Mesh* M = new Mesh;
89+
90+
// where this mesh data starts in the global pool
91+
M->vertexOffset = vertices.size();
92+
93+
for (unsigned int i = 0; i < vertexIndices.size(); i++)
94+
{
95+
size_t vertexIndex = vertexIndices[i];
96+
size_t uvIndex = uvIndices[i];
97+
size_t normalIndex = normalIndices[i];
98+
99+
glm::vec3 vertex = temp_vertices[vertexIndex - 1];
100+
glm::vec2 uv = temp_uvs[uvIndex - 1];
101+
glm::vec3 normal = temp_normals[normalIndex - 1];
102+
103+
vertices.push_back(vertex);
104+
uvs.push_back(uv);
105+
normals.push_back(normal);
106+
}
107+
108+
// this mesh data vertex count
109+
M->vertexCount = vertices.size() - M->vertexOffset;
110+
meshes.push_back(M);
111+
return 0;
112+
}
113+
114+
int MeshSet::generateTiledQuads(std::string name, size_t tileCount)
115+
{
116+
int id = add(name);
25117
size_t currentOffset = vertices.size();
26118

27119
glm::vec3 quadVertices[6] = {
@@ -71,9 +163,9 @@ int MeshSet::generateTiledQuads(size_t tileCount)
71163
return id;
72164
}
73165

74-
int MeshSet::generateSlideMeshes(std::vector<int> deltas)
166+
int MeshSet::generateSlideMeshes(std::string name, std::vector<int>& deltas)
75167
{
76-
size_t id = meshes.size();
168+
int id = add(name);
77169
size_t currentOffset = vertices.size();
78170

79171
glm::vec3 quadNormals[6] = {
@@ -143,80 +235,4 @@ void MeshSet::draw(size_t id)
143235
glDrawArrays(GL_TRIANGLES, meshes[id]->vertexOffset, meshes[id]->vertexCount);
144236
}
145237

146-
int MeshSet::loadOBJ(const char* fileName)
147-
{
148-
std::ifstream file(fileName, std::ifstream::in | std::ifstream::binary);
149-
if (!file.is_open())
150-
{
151-
printf("OBJ file could not be opened\n");
152-
return 0;
153-
}
154-
155-
std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
156-
std::vector<glm::vec3> temp_vertices;
157-
std::vector<glm::vec2> temp_uvs;
158-
std::vector<glm::vec3> temp_normals;
159-
160-
while (!file.eof())
161-
{
162-
char line[128];
163-
file.getline(line, 128);
164-
165-
glm::vec3 vertex;
166-
if (sscanf(line, "v %f %f %f\n", &vertex.x, &vertex.y, &vertex.z) == 3)
167-
temp_vertices.push_back(vertex);
168-
169-
glm::vec2 uv;
170-
if (sscanf(line, "vt %f %f\n", &uv.x, &uv.y) == 2)
171-
{
172-
uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
173-
temp_uvs.push_back(uv);
174-
}
175-
176-
glm::vec3 normal;
177-
if (sscanf(line, "vn %f %f %f\n", &normal.x, &normal.y, &normal.z) == 3)
178-
temp_normals.push_back(normal);
179-
180-
std::string vertex1, vertex2, vertex3;
181-
unsigned int vIndex[3], uvIndex[3], nIndex[3];
182-
if (sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d\n", &vIndex[0], &uvIndex[0], &nIndex[0], &vIndex[1], &uvIndex[1], &nIndex[1], &vIndex[2], &uvIndex[2], &nIndex[2]) == 9)
183-
{
184-
vertexIndices.push_back(vIndex[0]);
185-
vertexIndices.push_back(vIndex[1]);
186-
vertexIndices.push_back(vIndex[2]);
187-
uvIndices.push_back(uvIndex[0]);
188-
uvIndices.push_back(uvIndex[1]);
189-
uvIndices.push_back(uvIndex[2]);
190-
normalIndices.push_back(nIndex[0]);
191-
normalIndices.push_back(nIndex[1]);
192-
normalIndices.push_back(nIndex[2]);
193-
}
194-
}
195-
196-
Mesh* M = new Mesh;
197-
198-
// where this mesh data starts in the global pool
199-
M->vertexOffset = vertices.size();
200-
201-
for (unsigned int i = 0; i < vertexIndices.size(); i++)
202-
{
203-
size_t vertexIndex = vertexIndices[i];
204-
size_t uvIndex = uvIndices[i];
205-
size_t normalIndex = normalIndices[i];
206-
207-
glm::vec3 vertex = temp_vertices[vertexIndex - 1];
208-
glm::vec2 uv = temp_uvs[uvIndex - 1];
209-
glm::vec3 normal = temp_normals[normalIndex - 1];
210-
211-
vertices.push_back(vertex);
212-
uvs.push_back(uv);
213-
normals.push_back(normal);
214-
}
215-
216-
// this mesh data vertex count
217-
M->vertexCount = vertices.size() - M->vertexOffset;
218-
meshes.push_back(M);
219-
return 0;
220-
}
221-
222238

opensmith/Mesh.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <vector>
3+
#include <map>
34
#include <glm/glm.hpp>
45

56
struct Mesh
@@ -13,15 +14,18 @@ class MeshSet
1314
public:
1415
MeshSet();
1516
~MeshSet();
17+
int get(std::string name);
18+
int add(std::string name);
1619
int loadMesh(const char* fileName);
17-
int generateTiledQuads(size_t count);
18-
int generateSlideMeshes(std::vector<int> deltas);
20+
int generateTiledQuads(std::string name, size_t count);
21+
int generateSlideMeshes(std::string name, std::vector<int>& deltas);
1922
void draw(size_t id);
2023
void* getVertices() { return vertices.data(); }
2124
void* getUVs() { return uvs.data(); }
2225
size_t getSize() { return vertices.size(); }
2326
private:
2427
int loadOBJ(const char* fileName);
28+
std::map<std::string, int> names;
2529
std::vector<Mesh*> meshes;
2630
std::vector<glm::vec3> vertices;
2731
std::vector<glm::vec2> uvs;

0 commit comments

Comments
 (0)