From 6b50afc6cdd8d3555901b02ce12f15b5bac32aa8 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Tue, 4 Jul 2006 00:40:14 +0000 Subject: [PATCH] - added color ligts SVN-Revision: 3865 --- data/levels/test/light.stl | 23 +++++++++++++++++++++-- src/object/spotlight.cpp | 13 ++++++++++++- src/object/spotlight.hpp | 3 +++ src/sprite/sprite.cpp | 33 ++++++++++++++++++++++++++++++++- src/sprite/sprite.hpp | 16 +++++++++++++++- src/video/drawing_context.cpp | 12 ++++++++---- src/video/drawing_context.hpp | 34 +++++++++++++++++++++------------- src/video/surface.cpp | 14 +++++++++++++- src/video/surface.hpp | 4 +++- 9 files changed, 128 insertions(+), 24 deletions(-) diff --git a/data/levels/test/light.stl b/data/levels/test/light.stl index f41d431c2..e82b54015 100644 --- a/data/levels/test/light.stl +++ b/data/levels/test/light.stl @@ -70,7 +70,26 @@ (reset-points ) (objects - (spotlight (x 100) (y 300)) - (spotlight (x 500) (y 200)) + (spotlight (x 100) (y 300) + (red 1.0) + (green 0.0) + (blue 0.0) + (alpha 1.0) + (angle 60) + ) + (spotlight (x 500) (y 300) + (red 0.0) + (green 0.0) + (blue 1.0) + (alpha 1.0) + (angle 0)) + + (spotlight (x 300) (y 100) + (red 0.0) + (green 1.0) + (blue 0.0) + (alpha 1.0) + (angle -60)) + ) ) diff --git a/src/object/spotlight.cpp b/src/object/spotlight.cpp index d171525eb..6a29688d6 100644 --- a/src/object/spotlight.cpp +++ b/src/object/spotlight.cpp @@ -28,9 +28,18 @@ #include "sector.hpp" Spotlight::Spotlight(const lisp::Lisp& lisp) + : angle(0.0f), + color(1.0f, 1.0f, 1.0f) { lisp.get("x", position.x); lisp.get("y", position.y); + + lisp.get("angle", angle); + + lisp.get("red", color.red); + lisp.get("green", color.green); + lisp.get("blue", color.blue); + lisp.get("alpha", color.alpha); center = sprite_manager->create("images/objects/spotlight/spotlight_center.sprite"); base = sprite_manager->create("images/objects/spotlight/spotlight_base.sprite"); @@ -38,7 +47,7 @@ Spotlight::Spotlight(const lisp::Lisp& lisp) lightcone = sprite_manager->create("images/objects/spotlight/lightcone.sprite"); light = sprite_manager->create("images/objects/spotlight/light.sprite"); - angle = 0.0f; + } Spotlight::~Spotlight() @@ -62,6 +71,8 @@ Spotlight::draw(DrawingContext& context) context.push_target(); context.set_target(DrawingContext::LIGHTMAP); + light->set_color(color); + light->set_blend(Blend(GL_SRC_ALPHA, GL_ONE)); light->set_angle(angle); light->draw(context, position, 0); diff --git a/src/object/spotlight.hpp b/src/object/spotlight.hpp index e8d76e16a..2cfabb42a 100644 --- a/src/object/spotlight.hpp +++ b/src/object/spotlight.hpp @@ -23,6 +23,7 @@ #include "game_object.hpp" #include "math/vector.hpp" #include "lisp/lisp.hpp" +#include "video/color.hpp" class Sprite; @@ -43,6 +44,8 @@ private: Sprite* lights; Sprite* light; Sprite* lightcone; + + Color color; }; #endif diff --git a/src/sprite/sprite.cpp b/src/sprite/sprite.cpp index 00c624713..0c011ef14 100644 --- a/src/sprite/sprite.cpp +++ b/src/sprite/sprite.cpp @@ -32,7 +32,11 @@ #include "timer.hpp" Sprite::Sprite(SpriteData& newdata) - : data(newdata), frame(0), animation_loops(-1), angle(0.0f) + : data(newdata), + frame(0), + animation_loops(-1), + angle(0.0f), + color(1.0f, 1.0f, 1.0f, 1.0f) { action = data.get_action("normal"); if(!action) @@ -44,6 +48,7 @@ Sprite::Sprite(const Sprite& other) : data(other.data), frame(other.frame), animation_loops(other.animation_loops), angle(0.0f), + color(1.0f, 1.0f, 1.0f, 1.0f), action(other.action) { last_ticks = real_time; @@ -108,6 +113,8 @@ Sprite::draw(DrawingContext& context, const Vector& pos, int layer) context.draw_surface(action->surfaces[(int)frame], pos - Vector(action->x_offset, action->y_offset), angle, + color, + blend, layer + action->z_order); } @@ -194,4 +201,28 @@ Sprite::get_angle() const return angle; } +void +Sprite::set_color(const Color& c) +{ + color = c; +} + +Color +Sprite::get_color() const +{ + return color; +} + +void +Sprite::set_blend(const Blend& b) +{ + blend = b; +} + +Blend +Sprite::get_blend() const +{ + return blend; +} + /* EOF */ diff --git a/src/sprite/sprite.hpp b/src/sprite/sprite.hpp index b97b65cde..a43df0a16 100644 --- a/src/sprite/sprite.hpp +++ b/src/sprite/sprite.hpp @@ -27,9 +27,13 @@ #include "math/vector.hpp" #include "math/rect.hpp" #include "sprite_data.hpp" +#include "video/color.hpp" +#include "video/drawing_context.hpp" class Surface; class DrawingContext; +class Color; +class Blend; class Sprite { @@ -92,6 +96,14 @@ public: /** Get the angle of the sprite rotation in degree */ float get_angle() const; + void set_color(const Color& color); + + Color get_color() const; + + void set_blend(const Blend& blend); + + Blend get_blend() const; + /** Get current frame */ int get_frame() const { return (int)frame; } @@ -115,7 +127,9 @@ private: int animation_loops; float last_ticks; float angle; - + Color color; + Blend blend; + SpriteData::Action* action; }; diff --git a/src/video/drawing_context.cpp b/src/video/drawing_context.cpp index 281f98123..1214c20a6 100644 --- a/src/video/drawing_context.cpp +++ b/src/video/drawing_context.cpp @@ -70,8 +70,9 @@ DrawingContext::~DrawingContext() } void -DrawingContext::draw_surface(const Surface* surface, const Vector& position, float angle, - int layer) +DrawingContext::draw_surface(const Surface* surface, const Vector& position, + float angle, const Color& color, const Blend& blend, + int layer) { assert(surface != 0); @@ -89,6 +90,9 @@ DrawingContext::draw_surface(const Surface* surface, const Vector& position, flo request.drawing_effect = transform.drawing_effect; request.alpha = transform.alpha; request.angle = angle; + request.color = color; + request.blend = blend; + request.request_data = const_cast (surface); requests->push_back(request); @@ -98,7 +102,7 @@ void DrawingContext::draw_surface(const Surface* surface, const Vector& position, int layer) { - draw_surface(surface, position, 0.0f, layer); + draw_surface(surface, position, 0.0f, Color(1.0f, 1.0f, 1.0f), Blend(), layer); } void @@ -392,7 +396,7 @@ DrawingContext::handle_drawing_requests(DrawingRequests& requests) if (i->angle == 0.0f) surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect); else - surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->drawing_effect); + surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect); break; } case SURFACE_PART: diff --git a/src/video/drawing_context.hpp b/src/video/drawing_context.hpp index 4ed280688..a8c7e5680 100644 --- a/src/video/drawing_context.hpp +++ b/src/video/drawing_context.hpp @@ -52,6 +52,21 @@ enum { LAYER_GUI = 500 }; +class Blend +{ +public: + GLenum sfactor; + GLenum dfactor; + + Blend() + : sfactor(GL_SRC_ALPHA), dfactor(GL_ONE_MINUS_SRC_ALPHA) + {} + + Blend(GLenum s, GLenum d) + : sfactor(s), dfactor(d) + {} +}; + /** * This class provides functions for drawing things on screen. It also * maintains a stack of transforms that are applied to graphics. @@ -66,7 +81,8 @@ public: void draw_surface(const Surface* surface, const Vector& position, int layer); /// Adds a drawing request for a surface into the request list. - void draw_surface(const Surface* surface, const Vector& position, float angle, + void draw_surface(const Surface* surface, const Vector& position, + float angle, const Color& color, const Blend& blend, int layer); /// Adds a drawing request for part of a surface. void draw_surface_part(const Surface* surface, const Vector& source, @@ -138,16 +154,6 @@ private: /// the currently active transform Transform transform; - class Blend - { - public: - GLenum sfactor; - GLenum dfactor; - - Blend() - : sfactor(GL_SRC_ALPHA), dfactor(GL_ONE_MINUS_SRC_ALPHA) - {} - }; std::vector blend_stack; Blend blend_mode; @@ -191,11 +197,13 @@ private: float alpha; Blend blend; float angle; - + Color color; + void* request_data; DrawingRequest() - : angle(0.0f) + : angle(0.0f), + color(1.0f, 1.0f, 1.0f, 1.0f) {} bool operator<(const DrawingRequest& other) const diff --git a/src/video/surface.cpp b/src/video/surface.cpp index 799372b27..ff89f8c6f 100644 --- a/src/video/surface.cpp +++ b/src/video/surface.cpp @@ -32,6 +32,8 @@ #include "gameconfig.hpp" #include "physfs/physfs_sdl.hpp" #include "video/surface.hpp" +#include "video/drawing_context.hpp" +#include "video/color.hpp" #include "image_texture.hpp" #include "texture_manager.hpp" @@ -134,6 +136,8 @@ static inline void intern_draw2(float left, float top, float right, float bottom float uv_left, float uv_top, float uv_right, float uv_bottom, float angle, + const Color& color, + const Blend& blend, DrawingEffect effect) { if(effect & HORIZONTAL_FLIP) @@ -154,6 +158,8 @@ static inline void intern_draw2(float left, float top, float right, float bottom top -= center_y; bottom -= center_y; + glBlendFunc(blend.sfactor, blend.dfactor); + glColor4f(color.red, color.green, color.blue, color.alpha); glBegin(GL_QUADS); glTexCoord2f(uv_left, uv_top); glVertex2f(left*ca - top*sa + center_x, @@ -171,10 +177,14 @@ static inline void intern_draw2(float left, float top, float right, float bottom glVertex2f(left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y); glEnd(); + + // FIXME: find a better way to restore the blend mode + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } void -Surface::draw(float x, float y, float alpha, float angle, DrawingEffect effect) const +Surface::draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const { glColor4f(1.0f, 1.0f, 1.0f, alpha); glBindTexture(GL_TEXTURE_2D, texture->get_handle()); @@ -183,6 +193,8 @@ Surface::draw(float x, float y, float alpha, float angle, DrawingEffect effect) x + width, y + height, uv_left, uv_top, uv_right, uv_bottom, angle, + color, + blend, effect); } diff --git a/src/video/surface.hpp b/src/video/surface.hpp index 8961606ed..0945253ed 100644 --- a/src/video/surface.hpp +++ b/src/video/surface.hpp @@ -22,6 +22,8 @@ #include +class Color; +class Blend; class ImageTexture; /// bitset for drawing effects @@ -51,7 +53,7 @@ private: float uv_right; float uv_bottom; - void draw(float x, float y, float alpha, float angle, DrawingEffect effect) const; + void draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const; void draw(float x, float y, float alpha, DrawingEffect effect) const; void draw_part(float src_x, float src_y, float dst_x, float dst_y, float width, float height, -- 2.11.0