Skip to content

Commit

Permalink
moved gl stuff from game object to gl_renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Reineke committed Jul 11, 2010
1 parent a2bfaf9 commit 56a4367
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 31 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ LD = g++
CFLAGS = `sdl-config --cflags` -g
LDFLAGS = `sdl-config --libs` -lSDL_image -lGL -lGLU
RM = /bin/rm -f
OBJS = main.o object.o extra.o game_object.o physics.o obj_shape.o control.o sphere_shape.o gl_renderer.o
OBJS = main.o object.o extra.o game_object.o physics.o obj_shape.o \
control.o sphere_shape.o gl_renderer.o gl_drawable.o

all: norbit test_obj

Expand Down
24 changes: 9 additions & 15 deletions game_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "object.h"
#include "shadow.h"
#include "game_object.h"
#include <GL/gl.h>
#include <assert.h>


Expand All @@ -13,24 +12,10 @@ simulation_world * game_object::world= NULL;
using namespace std;


void game_object::draw()
{
glPushMatrix();
glTranslatef(-posx(),-posy(),-posz());
glRotatef(body->aConfigurations[body->SourceConfigurationIndex].Orientation*180.0/PI, 0.0f,0.0f,1.0f);
glRotatef(90,1.0f,0.0f,0.0f);
glScalef(0.2f, 0.2f, 0.2f);
object_shape->draw();
glPopMatrix();
}




void game_object::set_shape(shape *s)
{
object_shape = s;
}

void game_object::set_position(float x, float y, float z)
{
Expand All @@ -55,6 +40,15 @@ float game_object::posz(){
return 0.0f;
}

/**
* Return orientation in degrees
*/
float game_object::orientation()
{
return body->aConfigurations[body->SourceConfigurationIndex].Orientation*180.0/PI;
}


void game_object::set_simulation_world(simulation_world *w){ assert(w); world = w; }

void game_object::set_rigid_body(rigid_body * b){
Expand Down
7 changes: 3 additions & 4 deletions game_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ class shape
class game_object
{
public:
void draw() ;
void set_shape(shape *shape);
void set_rigid_body(rigid_body*);
void set_position(float x, float y, float z);

static void set_simulation_world(simulation_world *w);
void apply_force(vector_2 const & F, vector_2 const & Pl);
void apply_force_G(vector_2 const & F, vector_2 const & Pl);
void apply_force(vector_2 const & F, vector_2 const & Pl); // TODO: Make it fit for 3D
void apply_force_G(vector_2 const & F, vector_2 const & Pl); // TODO: Make it fit for 3D
float posx();
float posy();
float posz();
float orientation(); // TODO: Make it fit for 3D
float mass();

private:
Expand Down
26 changes: 26 additions & 0 deletions gl_drawable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* gl_viewable.cpp
*
* Created on: Jul 11, 2010
* Author: peter
*/

#include "gl_drawable.h"

gl_drawable::gl_drawable(game_object *go, shape *sh)
{
this->go = go;
this->sh = sh;
}

void gl_drawable::draw()
{
glPushMatrix();
glTranslatef(-go->posx(),-go->posy(),-go->posz());
glRotatef(go->orientation(), 0.0f,0.0f,1.0f);
glRotatef(90,1.0f,0.0f,0.0f);
glScalef(0.2f, 0.2f, 0.2f);
sh->draw();
glPopMatrix();
}

22 changes: 22 additions & 0 deletions gl_drawable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* gl_drawable.h
*
* Created on: Jul 11, 2010
* Author: peter
*/

#ifndef GL_DRAWABLE_H_
#define GL_DRAWABLE_H_

#include "game_object.h"

class gl_drawable {
public:
gl_drawable(game_object *go, shape *sh);
void draw();
private:
game_object *go;
shape *sh;
};

#endif /* GL_DRAWABLE_H_ */
12 changes: 4 additions & 8 deletions gl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ extern "C" {
}

gl_renderer::gl_renderer() {

drawables = new list<gl_drawable*>();
}

void gl_renderer::add_ship(game_object *go)
{
go->set_shape(ship_shape);
drawables->push_front(new gl_drawable(go, ship_shape));
}

void gl_renderer::add_star(game_object *go)
{
go->set_shape(star_shape);
drawables->push_front(new gl_drawable(go, star_shape));
}

void gl_renderer::init()
Expand Down Expand Up @@ -181,10 +181,6 @@ int gl_renderer::render() {
interval = FrameTiming();
/* apply control movement */

for(list<Controller*>::const_iterator it = game_controllers.begin(); it !=game_controllers.end(); ++it)
{
(*it)->apply();
}

/* ----- Blitting on the screen --------------- */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
Expand All @@ -208,7 +204,7 @@ int gl_renderer::render() {
UpdateLight(&light2, GL_LIGHT2, 0.1f);

/* ----- Objects ----- */
for(list<game_object*>::const_iterator it = game_objects.begin(); it != game_objects.end(); ++it)
for(list<gl_drawable*>::const_iterator it = drawables->begin(); it != drawables->end(); ++it)
{
(*it)->draw();
}
Expand Down
3 changes: 3 additions & 0 deletions gl_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extern "C" {
}

#include "game_object.h"
#include "gl_drawable.h"
#include <list>

class gl_renderer {
public:
Expand All @@ -38,6 +40,7 @@ class gl_renderer {
GLfloat angle;
int quit;
shape *ship_shape, *star_shape;
list<gl_drawable*> *drawables;
};

#endif /* GL_RENDERER_H_ */
6 changes: 4 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

#include <iostream>
#include <list>
#include <string>

#include "gl_renderer.h"
Expand Down Expand Up @@ -55,7 +54,10 @@ int main(int argc, char **argv)
int running = 1;
do {
running = renderer->render();

for(list<Controller*>::const_iterator it = game_controllers.begin(); it !=game_controllers.end(); ++it)
{
(*it)->apply();
}
static real LastTime = 0;
real Time = LastTime + 0.0001f;
// printf("time %0.4f %0.4f\n", Time, LastTime);
Expand Down
4 changes: 3 additions & 1 deletion main.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef MAIN_H_
#define MAIN_H_

#include <list>

extern list<game_object*> game_objects;
extern list<Controller*> game_controllers;
//extern list<Controller*> game_controllers;
extern game_object *ship1, *ship2;
extern simulation_world *world;
extern SpaceShipController *control1, *control2;
Expand Down

0 comments on commit 56a4367

Please sign in to comment.