Skip to content

Commit

Permalink
merged with brainbot code, ported across changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilbefast committed Jan 19, 2013
1 parent 62c7ad8 commit 2888022
Show file tree
Hide file tree
Showing 31 changed files with 356 additions and 134 deletions.
24 changes: 16 additions & 8 deletions Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "io/FontManager.hpp" // font subsystem (singleton)
#include "io/MeshManager.hpp" // 3D mesh subsystem (singleton)

#define WINDOW_DEFAULT_FLAGS SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN
#ifdef SDL2
#define WINDOW_DEFAULT_FLAGS SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN
#else //SDL1.2
#define WINDOW_DEFAULT_FLAGS SDL_OPENGL
#endif

#ifdef __ANDROID__
#define WINDOW_FLAGS WINDOW_DEFAULT_FLAGS|SDL_WINDOW_BORDERLESS
Expand Down Expand Up @@ -163,7 +167,7 @@ int Application::shutdown()
SDL_GL_MakeCurrent(NULL, NULL);
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
#else // SDL1.6
#else // SDL1.2
SDL_FreeSurface(screen);
#endif // SDL2

Expand Down Expand Up @@ -208,11 +212,11 @@ int Application::startSDL()
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, GL_V_MINOR);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

#else // SDL1.6
#else // SDL1.2

screen = SDL_SetVideoMode(WINDOW_DEFAULT_W, WINDOW_DEFAULT_H,
WINDOW_C_DEPTH, SDL_OPENGL);
ASSERT_SDL(screen, "Creating SDL1.6 application screen-surface");
ASSERT_SDL(screen, "Creating SDL1.2 application screen-surface");
global::viewport = fV2(screen->w, screen->h);
SDL_WM_SetCaption(APP_NAME, NULL);

Expand All @@ -228,9 +232,11 @@ int Application::startSDL()

int Application::startGL()
{
#ifdef USE_GLEW
// Start the GL extension wrangler (GLEW)
GLenum result = glewInit();
ASSERT_AUX(result == GLEW_OK, "Starting GLEW", glewGetErrorString(result));
#endif // USE_GLEW

// Define viewport
glViewport(0, 0, global::viewport.x, global::viewport.y);
Expand Down Expand Up @@ -265,7 +271,7 @@ void Application::draw()
// Flip the buffers to update the screen
#ifdef SDL2
SDL_GL_SwapWindow(window);
#else // SDL1.6
#else // SDL1.2
glFlush();
SDL_GL_SwapBuffers();
#endif // SDL2
Expand Down Expand Up @@ -354,14 +360,16 @@ int Application::treatEvents()
#if USE_MOUSE
// mouse events
case SDL_MOUSEBUTTONDOWN:
if(event.button.button == SDL_BUTTON_RIGHT)
return Application::BACK;
clicking = true;
if(event.button.button == SDL_BUTTON_LEFT)
clicking = true;
scene->getState()->trigger(event.button.button, true);

break;

case SDL_MOUSEBUTTONUP:
if(event.button.button == SDL_BUTTON_LEFT)
clicking = false;
scene->getState()->trigger(event.button.button, false);
break;

case SDL_MOUSEMOTION:
Expand Down
2 changes: 2 additions & 0 deletions gameplay/things/Thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class Thing
bool isColliding(Thing* other, iV2* side = NULL);
bool isOutside(fRect* bounds, iV2* side = NULL);
bool isLeaving(fRect* bounds, iV2* side = NULL);


// overridden
virtual void die();
virtual void draw();
Expand Down
2 changes: 1 addition & 1 deletion gameplay/things/elements/ColliderElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool ColliderElement::doesContain(fV2 point) const
// accessors
fRect ColliderElement::getOffsetBox() const
{
return hitbox + owner->getPosition();
return (hitbox + owner->getPosition());
}

iV2 ColliderElement::boundarySide(fRect* bounds) const
Expand Down
4 changes: 2 additions & 2 deletions global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

namespace global
{
extern iV2 viewport;
extern fV2 scale;
extern iV2 viewport;
extern fV2 scale;
};


Expand Down
4 changes: 2 additions & 2 deletions graphics/2D/GraphicIncarnation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class GraphicIncarnation
{
/// FLAGS
public:
static const char CENTER_X = 0b00000001;
static const char CENTER_Y = 0b00000010;
static const char CENTER_X = 1; // 0b00000001;
static const char CENTER_Y = 2; // 0b00000010;

/// ATTRIBUTES
protected:
Expand Down
28 changes: 7 additions & 21 deletions graphics/3D/Mesh3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ n_groups(1)
{
}

int Mesh3D::load_obj(const char* filename)
int Mesh3D::load(const char* filename)
{
// open the file
ifstream in(filename, ios::in);
Expand All @@ -96,7 +96,7 @@ int Mesh3D::load_obj(const char* filename)
string line;
while (getline(in, line))
{
// group
// group / object
if(line[0] == 'g' || line[0] == 'o')
{
add_group();
Expand Down Expand Up @@ -144,7 +144,7 @@ int Mesh3D::load_obj(const char* filename)
else if (key == TAG_SMOOTH_SHADING)
s >> current_group->smooth;
else
cout << line << ": unrecognised\n";
cout << line << ": unrecognised OBJ line.\n";
}
current_group->last_face = faces.size()-1;

Expand Down Expand Up @@ -290,9 +290,9 @@ void Mesh3D::finalise()
normal_list_t(normals).swap(normals);
tex_coord_list_t(texture_coordinates).swap(texture_coordinates);

ofstream f;
f.open ("arrogance_mesh.obj");
f << (*this);
//ofstream f;
//f.open ("arrogance_mesh.obj");
//f << (*this);
}

void Mesh3D::unitise()
Expand Down Expand Up @@ -322,9 +322,8 @@ void Mesh3D::draw()
do
{
// activate the current group's material
bool bink;
if(current_group->material)
bink= current_group->material->activate();
current_group->material->activate();

if(current_group->smooth)
glShadeModel(GL_SMOOTH);
Expand All @@ -339,19 +338,6 @@ void Mesh3D::draw()
{
// cache the current triangle
face_t const& face = faces[face_i];

static bool first = true;
if(first && bink)
{
cout << face.uv_i << endl;

for(size_t v_i = 0; v_i < 3; v_i++)
cout << texture_coordinates[face.uv_i[v_i]] << endl;

first = false;
}


for(size_t v_i = 0; v_i < 3; v_i++)
{
// vertex texture coordinate (optional)
Expand Down
2 changes: 1 addition & 1 deletion graphics/3D/Mesh3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Mesh3D
public:
// creation, destruction
Mesh3D();
int load_obj(const char* filename);
int load(const char* filename);
~Mesh3D();
// draw
void draw();
Expand Down
6 changes: 6 additions & 0 deletions graphics/3D/Shader.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifdef USE_GLS
//!-----------------------------------------------------------------------------

#include "Shader.hpp"

#include "../../io/file.hpp"
Expand Down Expand Up @@ -130,3 +133,6 @@ int Shader::load_aux(const char* file_path)
delete[] source;
}

//!-----------------------------------------------------------------------------
#endif //USE_GLS

13 changes: 6 additions & 7 deletions graphics/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//! PRIVATE SUBROUTINES

// This function is adapted from NeHe: it replaces gluPerspective.
void glPerspective(GLdouble fov, GLdouble aspect, GLdouble near, GLdouble far)
void glPerspective(GLdouble fov, GLdouble aspect, GLdouble _near, GLdouble _far)
{
GLdouble height = tan(fov / 360 * PI) * near,
width = height * aspect;
glFrustum(-width, width, height, -height, near, far);
GLdouble height = tan(fov / 360 * PI) * _near;
GLdouble width = height * aspect;
glFrustum(-width, width, -height, height, _near, _far);
}

// The public line-drawing functions are just adaptors for this one
Expand Down Expand Up @@ -94,13 +94,12 @@ void draw::use3D()

// Set up lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT0);

// Set up camera frustrum
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glPerspective(VIEW_FIELD, (float)global::viewport.x/global::viewport.y, NEAR, FAR);
glPerspective(GL_VIEW_FIELD, global::viewport.x/global::viewport.y, GL_Z_NEAR, GL_Z_FAR);

// Clean the slate
glMatrixMode(GL_MODELVIEW);
Expand Down
6 changes: 3 additions & 3 deletions graphics/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#define CIRCLE_BASE_SEGMENTS 6

#define NEAR 0.5
#define FAR 10000
#define VIEW_FIELD 60
#define GL_Z_NEAR 0.5
#define GL_Z_FAR 10000
#define GL_VIEW_FIELD 60

namespace draw
{
Expand Down
78 changes: 61 additions & 17 deletions io/MeshManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "../math/V3.hpp"

#include "file.hpp"
#include "tinyxml/tinyxml.h"

using namespace std;

Expand All @@ -45,7 +46,7 @@ MeshManager* MeshManager::getInstance()

MeshManager::MeshManager() :
ResourceManager(),
mesh()
meshes()
{
}

Expand All @@ -59,12 +60,8 @@ int MeshManager::load()
{
// load meshes and materials

//! FIXME
ASSERT(mesh.load_obj(GET_ASSET("BerkeleyDragon.obj")) == EXIT_SUCCESS, "Loading mesh");

//! TODO
//ASSERT(load_xml(GET_ASSET("3Dobjects.xml")) == EXIT_SUCCESS,
// "Loading 3D assets based on '3Dobjects.xml'");
ASSERT(load_xml(GET_ASSET("objects3D.xml")) == EXIT_SUCCESS,
"Loading 3D assets based on 'objects3D.xml'");

// all clear!
log(LOG_INFO, "MeshManager::load() - Okay");
Expand All @@ -73,14 +70,9 @@ int MeshManager::load()

int MeshManager::unload()
{
// Clean up meshes and materials

//! FIXME


//! TODO
// destroy meshes

// Clean up the meshes
for(MeshI i = meshes.begin(); i != meshes.end(); i++)
delete (*i).second;

// all clear!
log(LOG_INFO, "MeshManager::unload() - Okay");
Expand All @@ -89,12 +81,64 @@ int MeshManager::unload()

int MeshManager::parse_root(void* root_handle)
{
//! TODO
// load meshes
ASSERT(parse_list(root_handle, "mesh_list") == EXIT_SUCCESS,
"MeshManager parsing list of meshes");


return EXIT_SUCCESS;
}

int MeshManager::parse_element(void* element)
{
//! TODO
// cast element to TinyXML element
TiXmlElement* txml_element = (TiXmlElement*)element;

// texture element
if(!strcmp(txml_element->Value(), "mesh"))
{
// get the name of the texture and deduce its filename
const char* name = txml_element->Attribute("name");
if(!name)
WARN_RTN("MeshManager::load_xml", "malformed mesh tag", EXIT_FAILURE);

// load the texture
string filename = io::name_to_path(name, MESH_FILETYPE);
load_mesh(filename.c_str(), name);

// continue to the next
txml_element = txml_element->NextSiblingElement();
}
// all clear!
return EXIT_SUCCESS;
}

/// 3D MESHES

int MeshManager::load_mesh(const char* source_file, const char* name)
{
// load image file
Mesh3D* new_mesh = new Mesh3D();
ASSERT(new_mesh->load(source_file) == EXIT_SUCCESS, source_file);

// save under requested name
str_id hash = numerise(name);
meshes[hash] = new_mesh;

// All clear !
return EXIT_SUCCESS;
}

Mesh3D* MeshManager::get_mesh(const char* name)
{
// search for the resource
str_id hash = numerise(name);
MeshI i = meshes.find(hash);
// make sure that it is found
if(i == meshes.end())
WARN_RTN("MeshManager::get_mesh invalid identifier", name, NULL);

// return the texture we recovered
return (*i).second;
}

Loading

0 comments on commit 2888022

Please sign in to comment.