Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: asivitz/mld-game
base: 7f3eb67cf8
...
head fork: asivitz/mld-game
compare: 00912acdc8
  • 5 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
10 engine.rb
View
@@ -8,6 +8,14 @@ def key_map
def key_pressed code
key_map[code] = true
+
+ if code == 73 # up arrow
+ $one.jump
+ elsif code == 57
+ $one.fire
+ elsif code == 16 # letter q
+ $running = false
+ end
end
def key_released code
@@ -42,7 +50,7 @@ def begin_contact other
def end_contact other
end
- def jump_platform
+ def is_jump_platform
false
end
end
2  ext/Physics.cpp
View
@@ -238,7 +238,7 @@ Object Physics::addGrenade(vec2 pos, float size)
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(pos.x, pos.y);
- //bodyDef.fixedRotation = true;
+ bodyDef.fixedRotation = true;
//bodyDef.bullet = true;
b2Body* body = boxworld->CreateBody(&bodyDef);
114 ext/Platform.cpp
View
@@ -15,12 +15,35 @@
using namespace Rice;
using namespace std;
+void checkGLError()
+{
+ for (int e = glGetError(); e != GL_NO_ERROR; e = glGetError())
+ {
+ cout << "******************GLERROR****************";
+ switch (e)
+ {
+ case GL_INVALID_ENUM: cout << "OpenGL: Invalid enum" << endl; break;
+ case GL_INVALID_VALUE: cout << "OpenGL: Invalid value" << endl; break;
+ case GL_INVALID_OPERATION: cout << "OpenGL: Invalid operation" << endl; break;
+ case GL_STACK_OVERFLOW: cout << "OpenGL: Stack overflow" << endl; break;
+ case GL_STACK_UNDERFLOW: cout << "OpenGL: Stack underflow" << endl; break;
+ case GL_OUT_OF_MEMORY: cout << "OpenGL: Out of memory" << endl; break;
+ default: cout << "OpenGL: Unknown Error" << endl;
+ }
+ }
+}
+
+
Platform::Platform(Object self) : Rice::Director(self)
{
window = new sf::Window(sf::VideoMode(600, 600), "OpenGL");
window->setVerticalSyncEnabled(true);
+ window->setKeyRepeatEnabled(false);
renderer = new Renderer();
+
+ lightFramebuffer = lightImage = 0;
+ createLightFramebuffer();
}
Platform::~Platform()
@@ -42,6 +65,43 @@ void Platform::setPhysics(Physics * phys)
cout << "Couldn't create shader" << endl;
}
+const int lightWidth = 600;
+const int lightHeight = 600;
+
+void Platform::createLightFramebuffer()
+{
+ if (!lightFramebuffer)
+ {
+ // [EAGLContext setCurrentContext:context];
+
+ // Create default framebuffer object.
+ glGenFramebuffers(1, &lightFramebuffer);
+ glBindFramebuffer(GL_FRAMEBUFFER, lightFramebuffer);
+
+ glGenTextures(1, &lightImage);
+ glBindTexture(GL_TEXTURE_2D, lightImage);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, lightWidth, lightHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, lightImage, 0);
+
+ if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+ cout << "Failed to make complete framebuffer object " << glCheckFramebufferStatus(GL_FRAMEBUFFER) << endl;
+
+ checkGLError();
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ }
+}
+
+void Platform::switchToLightFramebuffer()
+{
+ glBindFramebuffer(GL_FRAMEBUFFER, lightFramebuffer);
+ glViewport(0, 0, lightWidth, lightHeight);
+}
+
void Platform::draw()
{
sf::Event event;
@@ -60,8 +120,40 @@ void Platform::draw()
}
}
- bool draw_debug = true;
+ switchToLightFramebuffer();
+ glViewport(0, 0, 600, 600);
+ renderer->drawLights();
+ glBindFramebuffer(GL_FRAMEBUFFER,0);
+ //lightMap->display();
+
+ //lightMap->getTexture().copyToImage().saveToFile("out.png");
+
+
+ bool draw_debug = false;
+ window->setActive(true);
+ glViewport(0, 0, 600, 600);
renderer->draw();
+
+ const float lightModel[]=
+ {60, 0, 0, 0,
+ 0, -60, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1};
+ //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glBlendFunc(GL_DST_COLOR, GL_ZERO);
+
+ {
+ ShaderProgram * prog = renderer->program;
+ glEnable(GL_BLEND);
+ glBindTexture(GL_TEXTURE_2D, lightImage);
+ glUniform1i(prog->locationOfTex(), 0);
+ //glUniform4f(prog->locationOfUniform("color"), 1.0, 1.0, 1.0, 1.0);
+ //glUniformMatrix4fv(prog->locationOfUniform("viewMat"), 1, GL_FALSE, renderer->viewMatrix);
+ glUniformMatrix4fv(prog->locationOfModelMat(), 1, GL_FALSE, lightModel);
+ glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, 0);
+ glDisable(GL_BLEND);
+ }
+
if (draw_debug)
{
ShaderProgram * prog = physics->m_debugDraw->shaderProgram;
@@ -102,21 +194,18 @@ void Platform::addDrawCommand(int texid, Array a)
}
}
-void checkGLError()
+void Platform::addLightCommand(int texid, Array a)
{
- for (int e = glGetError(); e != GL_NO_ERROR; e = glGetError())
+ if (a.size() == 16)
{
- cout << "******************GLERROR****************";
- switch (e)
+ DrawCommand * command = new DrawCommand();
+ VALUE * carr = a.to_c_array();
+ for (int i = 0; i < 16; i++)
{
- case GL_INVALID_ENUM: cout << "OpenGL: Invalid enum" << endl; break;
- case GL_INVALID_VALUE: cout << "OpenGL: Invalid value" << endl; break;
- case GL_INVALID_OPERATION: cout << "OpenGL: Invalid operation" << endl; break;
- case GL_STACK_OVERFLOW: cout << "OpenGL: Stack overflow" << endl; break;
- case GL_STACK_UNDERFLOW: cout << "OpenGL: Stack underflow" << endl; break;
- case GL_OUT_OF_MEMORY: cout << "OpenGL: Out of memory" << endl; break;
- default: cout << "OpenGL: Unknown Error" << endl;
+ command->m[i] = (float)NUM2DBL(carr[i]);
}
+ command->texId = texid;
+ renderer->addLightCommand(command);
}
}
@@ -166,6 +255,7 @@ void Init_engine()
define_class<Platform>("Platform")
.define_constructor(Constructor<Platform, Rice::Object>())
.define_method("addDrawCommand", &Platform::addDrawCommand)
+ .define_method("addLightCommand", &Platform::addLightCommand)
.define_method("setViewMatrix", &Platform::setViewMatrix)
.define_method("physics=", &Platform::setPhysics)
.define_method("isWindowOpen", &Platform::isWindowOpen)
9 ext/Platform.h
View
@@ -6,6 +6,9 @@
#include "rice/Array.hpp"
#include "rice/Director.hpp"
#include <SFML/Window.hpp>
+//#include <SFML/RenderTexture.hpp>
+#include <SFML/Graphics.hpp>
+
using namespace Rice;
@@ -16,6 +19,9 @@ class Platform : public Rice::Director
sf::Window * window;
+ GLuint lightFramebuffer;
+ GLuint lightImage;
+
public:
Platform(Object self);
~Platform();
@@ -23,10 +29,13 @@ class Platform : public Rice::Director
void draw();
Object getPhysics();
void addDrawCommand(int texid, Array a);
+ void addLightCommand(int texid, Array a);
void setViewMatrix(Array a);
bool isWindowOpen();
int loadImage(string fileName);
void setPhysics(Physics * phys);
+ void switchToLightFramebuffer();
+ void createLightFramebuffer();
};
#endif
41 ext/Renderer.cpp
View
@@ -72,7 +72,13 @@ void Renderer::setupSquareDrawing()
void Renderer::addCommand(DrawCommand * command)
{
- commands.push(command);
+ drawCommands.push(command);
+}
+
+void Renderer::addLightCommand(DrawCommand * command)
+{
+ lightCommands.push(command);
+ //cout << "Adding light command. size: " << lightCommands.size() << endl;
}
void Renderer::commitViewMatrix()
@@ -80,17 +86,17 @@ void Renderer::commitViewMatrix()
glUniformMatrix4fv(program->locationOfUniform("viewMat"), 1, GL_FALSE, viewMatrix);
}
-void Renderer::executeCommands()
+void Renderer::executeCommands(queue<DrawCommand *> * commands)
{
- while (!commands.empty())
+ while (!commands->empty())
{
- DrawCommand * command = commands.front();
+ DrawCommand * command = commands->front();
glUniformMatrix4fv(program->locationOfModelMat(), 1, GL_FALSE, command->m);
glBindTexture(GL_TEXTURE_2D, command->texId);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, 0);
delete command;
- commands.pop();
+ commands->pop();
}
}
@@ -112,6 +118,29 @@ void Renderer::draw()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
- executeCommands();
+ executeCommands(&drawCommands);
+ glDisable(GL_BLEND);
+}
+
+void Renderer::drawLights()
+{
+ glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
+ glClear( GL_COLOR_BUFFER_BIT );
+
+ glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
+
+ program->setAsActive();
+ glUniformMatrix4fv(program->locationOfUniform("viewMat"), 1, GL_FALSE, viewMatrix);
+ glUniform4f(program->locationOfUniform("color"), 1.0, 1.0, 1.0, 1.0);
+
+ glVertexAttribPointer(program->indexForAttribute("position"), 2, GL_FLOAT, 0, 0, 0);
+ glEnableVertexAttribArray(program->indexForAttribute("position"));
+
+
+ //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glBlendFunc(GL_ONE, GL_ONE);
+ glEnable(GL_BLEND);
+ executeCommands(&lightCommands);
glDisable(GL_BLEND);
}
9 ext/Renderer.h
View
@@ -16,21 +16,24 @@ struct DrawCommand
class Renderer
{
- queue<DrawCommand *> commands;
- ShaderProgram * program;
+ queue<DrawCommand *> drawCommands;
+ queue<DrawCommand *> lightCommands;
GLuint vertexBuffer;
GLuint indexBuffer;
GLuint texCoordBuffer;
public:
+ ShaderProgram * program;
Renderer();
~Renderer();
void addCommand(DrawCommand * command);
- void executeCommands();
+ void addLightCommand(DrawCommand * command);
+ void executeCommands(queue<DrawCommand *> * commands);
void setupSquareDrawing();
void commitViewMatrix();
void draw();
+ void drawLights();
float viewMatrix[16];
};
BIN  images/light.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 main.rb
View
@@ -8,31 +8,44 @@
# maps physics body ids to game objects
$body_map = {}
+$drawables = []
+$tex_map = {}
+
+def load_image name
+ texid = $tex_map[name]
+ if not texid
+ texid = $platform.loadImage name
+ $tex_map[name] = texid
+ end
+ texid
+end
MAX_VEL = 10.0
MOVE_IMP = 4.0
-JUMP_IMP = 110.0
+JUMP_IMP = 140.0
class Player < WorldObj
attr_reader :body
attr_accessor :num_standing_on_solid
def initialize pos
- @texid = $platform.loadImage "2player\ shooter/images/redGuyLeft.png"
+ @texid = load_image "2player\ shooter/images/redGuyLeft.png"
#@texid = $platform.loadImage "images/triangle.png"
@body = $physics.addPlayer(pos, 2.0)
$body_map[@body.id] = self
@num_standing_on_solid = 0
@jump_time = Time.now
+ @fire_time = Time.now
+ @last_faced_direction = 1
end
def begin_contact other
- if other.jump_platform
+ if other.is_jump_platform
@num_standing_on_solid += 1
end
end
def end_contact other
- if other.jump_platform
+ if other.is_jump_platform
@num_standing_on_solid -= 1
end
end
@@ -40,8 +53,8 @@ def end_contact other
def mat
m = Matrix.identity(4)
pos = @body.pos
- m = m.translate(pos.x,pos.y,0)
- m = m.scale(2,4,1)
+ m = m.translate(pos[0],pos[1],0)
+ m = m.scale(-2 * @last_faced_direction,4,1)
end
def draw
@@ -49,12 +62,14 @@ def draw
end
def move_right
+ @last_faced_direction = 1
if @body.vel.x < MAX_VEL
@body.push([MOVE_IMP,0.0])
end
end
def move_left
+ @last_faced_direction = -1
if @body.vel.x > -MAX_VEL
@body.push([-MOVE_IMP,0.0])
end
@@ -67,7 +82,7 @@ def jump
end
end
- def jump_platform
+ def is_jump_platform
true
end
@@ -78,6 +93,20 @@ def update time
vel.x *= 1.0 - 1.0 * time #* factor
$one.body.vel = vel
end
+
+ def fire
+ if (Time.now - @fire_time).to_f > 0.1
+ pos = vec(*@body.pos)
+ dir = vec(@last_faced_direction, 0.4).normalize
+ pos += dir
+ gren = Grenade.new pos.to_a
+ $drawables << gren
+ dir *= 100
+ gren.body.push dir.to_a
+
+ @fire_time = Time.now
+ end
+ end
end
def process_input key_map
@@ -86,14 +115,6 @@ def process_input key_map
elsif key_map[72] # right arrow
$one.move_right
end
-
- if key_map[73] # up arrow
- $one.jump
- end
-
- if key_map[16] # letter q
- $running = false
- end
end
class Floor < WorldObj
@@ -105,7 +126,7 @@ def initialize pos
$body_map[@body.id] = self
end
- def jump_platform
+ def is_jump_platform
true # player can jump off of this
end
end
@@ -114,10 +135,21 @@ class Grenade < WorldObj
attr_reader :body
def initialize pos
- #@texid = $platform.loadImage "2player\ shooter/images/redGuyLeft.png"
+ @texid = load_image "images/triangle.png"
+ @light_texid = load_image "images/light.png"
@body = $physics.addGrenade(pos,1.0)
$body_map[@body.id] = self
end
+
+ def draw
+ m = Matrix.identity(4)
+ pos = @body.pos
+ m = m.translate(pos[0],pos[1],0)
+ mat = m.scale(3,3,1)
+ $platform.addDrawCommand(@texid, mat.flatten)
+ light_mat = m.scale(25,25,1)
+ $platform.addLightCommand(@light_texid, light_mat.flatten)
+ end
end
class Boundary < WorldObj
@@ -129,7 +161,7 @@ def initialize
$body_map[@body.id] = self
end
- def jump_platform
+ def is_jump_platform
true # player can jump off of this
end
end
@@ -163,6 +195,7 @@ def jump_platform
end
$one.draw
$platform.draw
+ $drawables.each { |d| d.draw }
process_input $platform.key_map
28 matrix_graphics.rb
View
@@ -1,10 +1,10 @@
require 'matrix'
-def vec2(x,y)
- Matrix.column_vector([x,y])
+def vec(x,y)
+ Vector.elements([x,y])
end
-class Array
+class Vector
def x
self[0]
end
@@ -20,6 +20,28 @@ def x= other
def y= other
self[1] = other
end
+
+ def normalize
+ return self / self.r
+ end
+end
+
+class Array
+ def x
+ self[0]
+ end
+
+ def x=(val)
+ self[0] = val
+ end
+
+ def y
+ self[1]
+ end
+
+ def y=(val)
+ self[1] = val
+ end
end
class Matrix

No commit comments for this range

Something went wrong with that request. Please try again.