Browse Source

add selectOrbit() game interface

main
cinnaboot 4 years ago
parent
commit
5efd583d64
  1. 55
      src/game.cpp
  2. 2
      src/game.h
  3. 6
      src/gooey.cpp
  4. 10
      src/main.cpp

55
src/game.cpp

@ -2,6 +2,13 @@
#include "game.h" #include "game.h"
// forware declarations
void changeOrbitColor(GameOrbit* orbit, vec3* color_data);
// interface
GameOrbit* GameOrbit*
getFreeOrbit(GameState* gs) getFreeOrbit(GameState* gs)
{ {
@ -60,40 +67,46 @@ ellipse3DUpdate(EllipseParameters ep, Ellipse3D& e3d)
} }
void void
changeOrbitColor(GameOrbit* orbit, vec3* color_data) selectOrbit(GameState* gs, GameOrbit* orbit)
{
GLBuffer* color_buf;
GLMesh* gl_mesh = &orbit->ellipse_entity->meshes[0];
for (u32 i = 0; i < gl_mesh->num_vertex_attrib_buffers; i ++) {
if (utilCStrMatch(gl_mesh->vertex_attrib_buffers[i].name, "color"))
color_buf = &gl_mesh->vertex_attrib_buffers[i];
}
assert(color_buf && color_buf->data_size == orbit->e3d.vert_count * 3 * 4);
updateGLBuffer(color_buf, color_data);
}
void
updateOrbitColors(GameOrbit* last_selected_orbit, GameOrbit* orbit)
{ {
assert(orbit && orbit->ellipse_entity); assert(orbit && orbit->ellipse_entity);
// FIXME: need to allocate these somewhere other than stack // FIXME: need to allocate these somewhere other than stack
static vec3 selected_colors[DEFAULT_ORBIT_VERTICES]; static vec3 selected_colors[DEFAULT_ORBIT_VERTICES];
static vec3 default_colors[DEFAULT_ORBIT_VERTICES];
for (u32 i = 0; i < DEFAULT_ORBIT_VERTICES; i++) for (u32 i = 0; i < DEFAULT_ORBIT_VERTICES; i++)
selected_colors[i] = SELECTED_ELLIPSE_COLOR; selected_colors[i] = SELECTED_ELLIPSE_COLOR;
changeOrbitColor(orbit, selected_colors); static vec3 default_colors[DEFAULT_ORBIT_VERTICES];
// repeat for last orbit
if (last_selected_orbit != nullptr) {
for (u32 i = 0; i < DEFAULT_ORBIT_VERTICES; i++) for (u32 i = 0; i < DEFAULT_ORBIT_VERTICES; i++)
default_colors[i] = DEFAULT_ELLIPSE_COLOR; default_colors[i] = DEFAULT_ELLIPSE_COLOR;
changeOrbitColor(last_selected_orbit, default_colors); if (gs->last_selected_orbit != nullptr) {
changeOrbitColor(gs->last_selected_orbit, default_colors);
gs->last_selected_orbit->selected = false;
}
changeOrbitColor(orbit, selected_colors);
orbit->selected = true;
gs->last_selected_orbit = orbit;
}
// internal
void
changeOrbitColor(GameOrbit* orbit, vec3* color_data)
{
GLBuffer* color_buf;
GLMesh* gl_mesh = &orbit->ellipse_entity->meshes[0];
for (u32 i = 0; i < gl_mesh->num_vertex_attrib_buffers; i ++) {
if (utilCStrMatch(gl_mesh->vertex_attrib_buffers[i].name, "color"))
color_buf = &gl_mesh->vertex_attrib_buffers[i];
} }
assert(color_buf && color_buf->data_size == orbit->e3d.vert_count * 3 * 4);
updateGLBuffer(color_buf, color_data);
} }

2
src/game.h

@ -57,4 +57,4 @@ Ellipse3D ellipse3DInit(EllipseParameters ep, uint vert_count);
void ellipse3DUpdate(EllipseParameters ep, Ellipse3D& e3d); void ellipse3DUpdate(EllipseParameters ep, Ellipse3D& e3d);
void updateOrbitColors(GameOrbit* last_selected_orbit, GameOrbit* orbit); void selectOrbit(GameState* gs, GameOrbit* orbit);

6
src/gooey.cpp

@ -119,7 +119,7 @@ void drawOrbitWindow(GameState* gs)
Text("Orbit Selection"); Text("Orbit Selection");
BeginChild("orbit selection", ImVec2(G_WIDTH - 20, 100), true); BeginChild("orbit selection", ImVec2(G_WIDTH - 20, 100), true);
static u32 selected = 0; static u32 selected = UINT_MAX;
for (u32 i = 0; i < gs->num_orbits; i++) { for (u32 i = 0; i < gs->num_orbits; i++) {
GameOrbit& orbit = gs->orbits[i]; GameOrbit& orbit = gs->orbits[i];
@ -131,9 +131,7 @@ void drawOrbitWindow(GameState* gs)
if (Selectable(label, selected == i)) { if (Selectable(label, selected == i)) {
selected = i; selected = i;
orbit.selected = true; selectOrbit(gs, &orbit);
} else {
orbit.selected = false;
} }
} }
} }

10
src/main.cpp

@ -199,10 +199,7 @@ loadScene(GameState* gs, RenderState* rs)
double a = 26564.5; // semi-major axis in km double a = 26564.5; // semi-major axis in km
double e = 0.7411; // eccentricity double e = 0.7411; // eccentricity
GameOrbit* orbit_1 = loadOrbit(gs, rs, body, orbitInit(a, e), rg, "sat_01"); loadOrbit(gs, rs, body, orbitInit(a, e), rg, "sat_01");
// NOTE: set as selected orbit
updateOrbitColors(nullptr, orbit_1);
gs->last_selected_orbit = orbit_1;
double a_2 = r + 10000; double a_2 = r + 10000;
double e_2 = 0; double e_2 = 0;
@ -294,11 +291,6 @@ preFrameCallback(RenderState* rs, void* user_data = nullptr)
// TODO: update EllipseEntity per frame // TODO: update EllipseEntity per frame
//updateOrbit(gs->system, ellipse_entity); //updateOrbit(gs->system, ellipse_entity);
} }
if (orbit.selected && &orbit != gs->last_selected_orbit) {
updateOrbitColors(gs->last_selected_orbit, &orbit);
gs->last_selected_orbit = &orbit;
}
} }
} }
} }

Loading…
Cancel
Save