Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Lightning with some light points

This thread is locked; no one can reply to it. rss feed Print
Lightning with some light points
Brunocogo
Member #16,393
June 2016

Hey guys, i'm studying allegro library, and i'm trying to create a game. i want to put in my game, a light effect, and i got, but i also want a light points arround the map like this:
https://www.youtube.com/watch?v=_YVTOaUKO0Q

but when i come to the light point, happens it:

https://postimg.org/image/op1hpw6dd/

i'm doing that with blends, but i dont know how to proceed, anyone can help me? AHAH

part of code:

darkarea = al_create_bitmap(SCREEN_W, SCREEN_H);
al_set_target_bitmap(darkarea);

al_set_blender(ALLEGRO_ADD , ALLEGRO_ONE , ALLEGRO_ZERO);
al_clear_to_color(al_map_rgba_f(0, 0, 0,0.9));

al_draw_bitmap(light2, 1000-camera_x, 1000-camera_y , 0); // light point
al_draw_bitmap(light, 275, 170 , 0); //light that follow player

al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_set_target_backbuffer(display);

al_draw_bitmap(darkarea, 0, 0, NULL);

al_destroy_bitmap(darkarea);

bamccaig
Member #7,536
July 2006
avatar

I'm completely clueless about any of the GL language stuff and shaders and the like, but the obvious thing occurs to me: are you drawing things in an appropriate order? I'm not sure how your lighting is being handled, but I could imagine that the bitmap that is not lighted was drawn over top of the rest.

Mark Oates
Member #1,146
March 2001
avatar

I would do it like this:

#SelectExpand
1#include <iostream> 2#include <vector> 3 4 5class LightMask 6{ 7private: 8 class point2d 9 { 10 public: 11 float x; 12 float y; 13 point2d(float x, float y) : x(x), y(y) {} 14 }; 15 16 ALLEGRO_BITMAP *surface; 17 ALLEGRO_BITMAP *light_bitmap; 18 std::vector<point2d> points; 19 20 void update_surface() 21 { 22 ALLEGRO_STATE previous_state; 23 al_store_state(&previous_state, ALLEGRO_STATE_TARGET_BITMAP | ALLEGRO_STATE_BLENDER); 24 al_set_target_bitmap(surface); 25 al_clear_to_color(al_map_rgb(0, 0, 0)); 26 al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE); 27 28 for (auto &point : points) 29 { 30 float half_w = al_get_bitmap_width(light_bitmap) * 0.5; 31 float half_h = al_get_bitmap_height(light_bitmap) * 0.5; 32 al_draw_bitmap(light_bitmap, point.x - half_w, point.y - half_h, 0); 33 } 34 35 al_restore_state(&previous_state); 36 } 37 38public: 39 LightMask(int width, int height) 40 : surface(al_create_bitmap(width, height)) 41 , light_bitmap(al_load_bitmap("data/bitmaps/gradient.jpg")) 42 { 43 if (!light_bitmap) std::cout << "There was an error loading the light bitmap" << std::endl; 44 } 45 46 ~LightMask() 47 { 48 al_destroy_bitmap(surface); 49 al_destroy_bitmap(light_bitmap); 50 } 51 52 void clear_points() 53 { 54 points.clear(); 55 } 56 57 void add_point(float x, float y) 58 { 59 points.push_back(point2d(x, y)); 60 } 61 62 void draw() 63 { 64 update_surface(); 65 66 ALLEGRO_STATE previous_state; 67 al_store_state(&previous_state, ALLEGRO_STATE_BLENDER); 68 al_set_blender(ALLEGRO_ADD, ALLEGRO_DST_COLOR, ALLEGRO_ZERO); 69 70 al_draw_bitmap(surface, 0, 0, 0); 71 72 al_restore_state(&previous_state); 73 } 74};

This object has a very simple API:

LightMask(int w, int h) - create a new lightmask.
void add_point(int x, int y) - adds a light point to your mask.
void clear_points() - removes all the light points from the mask.
void draw() - draws the mask.

Here is the light mask being drawn on a green background. It has two static points, and another point that follows the mouse (click for gif):

{"name":"610432","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/6\/4685ce30f58324f61f7186f933af907d.gif","w":656,"h":392,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/6\/4685ce30f58324f61f7186f933af907d"}610432

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Brunocogo
Member #16,393
June 2016

can you send me the gradient.jpg please?

Mark Oates
Member #1,146
March 2001
avatar

You bet! It's actually attached in my previous post. :) (paper-clip icon)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Brunocogo
Member #16,393
June 2016

thanxx man, its work, my problem was the picture. i replaced my picture by your and worked perfectly =D, sorry for my english, i'm a brazilian student and i'm still improving my english AHAHAH ;D;D

Go to: