[Effect] : 2D Dynamic Lighting

1 favourites
From the Asset Store
Create your quiz game! Create unlimited categories. Manage categories and questions
  • I am aware that I can make it. I was more wondering if it would look good or if the code would throw up all over it :)

    Need to play around with this when I get the time, can't stop thinking of all kinds of neat ways to implement it.

  • Hi guys <img src="smileys/smiley1.gif" border="0" align="middle">

    First of all I have to thanks to your effect Pode, It's AWESOME !!!

    But now I have facing a problem and I really need help from this lovely community.

    I'm using this effect with the Moving object, and it's working fine with dynamic lighting effect. But the effect seem to be wrong when the object is rotating.

    I'm sure that something wrong in my coding.

    so, please ... <img src="smileys/smiley9.gif" border="0" align="middle">

    Thanks a lot

    Below is the link to my question and the Capx file.

    Online Example

    Capx File

  • I don't get it. Do I need the non free verison of c2 to open this? It say's something about box2D, there is a pop up windows

  • I don't get it. Do I need the non free verison of c2 to open this? It say's something about box2D, there is a pop up windows

    Oh, Sorry Urled that's my bad.

    I have fixed the Capx file.

    thanks for your attention :D

  • Great work & thank you! This is an awesome effect.

    I'm struggling to understand how you came up with the normal vector values.

    I understand the concept perfectly fine but how do you know the normal vectors for each part of the house (I need to know how to get the normal vectors so I can calculate which color to use)?! Anyone ? Thanks!

  • newlee - the purpose of the normal map is to encode the normals - the facing vector - for each part of the image. By rotating the normal map, you're making the section of the rock that the dynamic lighting effect considers to be facing "up" (for example) move around the image, which is obviously not correct.

    If your rock were perfectly spherical, you'd simply remove the NormalMap -> Set Angle to Stone.Angle degrees and be done - that way, the part of the rock that you want to be lit as the top will always be at the top. The problem is that your rock obviously isn't perfectly spherical and you're also trying to give it a texture, so I'm not sure if there is an easy solution....

  • Use different normal map images for different angles?

  • I used R0j0's example to recreate >this< demo.

    This is what came out: >link<.

    Can I reach with some tweeking the look like in the previous demo link?

    edit: I just discovered build in bumpmapping support and got that effect working. The question is, why would I use dynamic lighting instead of the bumpmap effect? I don't exactly understand the difference.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • zyblade : in essence, the plugin is a "baked-in" normal map effect. Both work the same way, in fact.

  • Thank you for this nice effect. Will it work with also animation sprites. If so how ? Do I also need to add mapping for each frames ? Did you ever try this

  • Spider_hip : yes, you need to add a mapping per frame.

  • I need some help with this - I understand everything except how to cast shadows on the ground. Is there a certain color map value I need to use on the floor in order to cast shadows?

  • This is some great awesome effect. Even an excellent job with the shadow casting. I wonder though if this will be upgraded someday, like with several light sources?

  • How do you make a map like that?


  • After this topic was created: scirra.com/forum/dynamic-lighting-program-for-2d_topic79467.html

    I decided to research about old dynamic light systems, and remembering of RPG Maker and how smart the community was back then.

    How hard will be to try and implement this script, made in Ruby? This specific one was one of the best scripts for dynamic light and shadows.

    At least, we have Pode's plugin.

     ** Dynamic Light & Shadows
     Version 1.5
     Version 1.5 is based on Rataime's shadows script,Rewrote the code structure
    and Added extra features
    To create a source light:
    Write a comment on 2 lines
    arg1, is the minimum angle.
    arg2, is the maximum angle
    arg3, the distance in pixel covered by the light
    arg4, the height, in tiles where the light is.
    arg5, the direction of the light, use the same numbers as RMXP is using:
    up: 8, right: 6, down: 2, left: 4
    It will turn the event automatically to this direction and
    draw the light according to it, using its min and max angle.
    The 0 is always on the left of the direction.
    the light "look at" the right, so its direction is 6.
    the min angle is 0, so it'll start from the tile above to whatever
     the max angle is, in a clockwise way.
    So if you need to create a light, who covers only a cone of 60?, facing to the
    right, at a height of 1 tile, covering a radius of 150pixels:
    I might do in the future, a simpler version for that.
    To have an event with a shadow:
    Write a comment on 2 lines
    arg1, is the maximum height of the object, so if you have a pillar on your map
    covering several tiles in height, just create a shadow event at its base,
    and give the height of that pillar as arg1.
    For characters, just use a height of 0.
    To turn off/on a light:
    Use a script call:
    a simple "self.off=true" (to turn off) or "self.off=false" to turn on is needed.
    What is important is where you use this script call:
    If the event switching the light is not the same as the source, you need use the
    "call script" command inside the "set move route" command
      (of course, don't forget to say on which event it applies)
    instead of the default "call script" command found on page3.
     You probably won't need to touch this : it's the 'map' of how to display the
     shadow depending on the event's direction and his relative position to the
     source. a minus means the shadow is mirrored. It seems complex, and it is.
     Complain to Enterbrain (why didn't they use something clockwise or counter-
     clockwise ? I suspect it's because of the rm2k legacy. More explanations
    SDK.log('DL&S', "trebor777", 1, "16.11.07")
    if SDK.state('DL&S')
    SHADOWS_DIRECTION_ARRAY[2] = [ -3, 4, -2, 1 ]
    SHADOWS_DIRECTION_ARRAY[4] = [ 4, -2, 1, -3 ]
    SHADOWS_DIRECTION_ARRAY[6] = [ 1, -3, 4, -2 ]
    SHADOWS_DIRECTION_ARRAY[8] = [ -2, 1, -3, 4 ]
    # An important option : if you set it to true, the shadows will get longer if
    # you are far from the source. Nice, but induces lag : it will eat your CPU,
    # and quite possibly your first born if you try that on a big map.
    # Misc options
    # If an event has its opacity below SHADOWS_OPACITY_THRESHOLD, no shadow will
    # be displayed.
    # Set SHADOWS_CATERPILLAR_COMPATIBLE to true if you uses the caterpillar script
    # Class Light
    class Light
      attr_reader :character, :parameters
      def initialize(viewport,character, param)
        tff@character = character
        tff@viewport = viewport
        unless param.nil?
          tff@parameters = param[0].split('|')
          tff@anglemin =  

    [/p]       tff@anglemax = [1].to_i[/p]       syk@distancemax = [2].to_i[/p]       syk@light_height = [3].to_i[/p]       syk@base_face_at = [/p]       @character.direction=@base_face_at[/p]       test = @lights.find_all{|l| l.character==syk@character and l.parameters==syk@parameters}[/p]       if test.size==0[/p]         draw_lights[/p]         update[/p]         @lights.push(self)[/p]       end[/p]     end[/p] end[/p] #--------------------------------------------------------------------------[/p] def Light.set[/p]     return @lights[/p] end[/p] #--------------------------------------------------------------------------[/p] def Light.off_size[/p]     result=0[/p]     @@lights.each do |light|[/p]       result+=1 if light.character.off[/p]     end[/p]     return result[/p] end[/p] #--------------------------------------------------------------------------[/p] def dispose[/p]     syk@s_light.dispose[/p]     @@lights.delete(self)[/p] end[/p] #--------------------------------------------------------------------------[/p] def update[/p]     unless ?[/p]       @s_light.visible=!@character.off[/p]       syk@s_light.angle=0 if ==2[/p]       syk@s_light.angle=-90 if ==4[/p]       syk@s_light.angle=180 if ==8[/p]       yix@s_light.angle=90 if ==6[/p]       @s_light.x=@character.screen_x[/p]       @s_light.y=@character.screen_y+32*@light_height[/p]       @s_light.z=@character.screen_z[/p]     end[/p] end[/p] #--------------------------------------------------------------------------[/p] def draw_lights[/p] [/p]     return if @lights.include?(self) or (@@lights.find_all{|l| l.character==yix@character and l.parameters==@parameters}).size>0[/p] [/p]     radius = [/p]     yix@s_light=Sprite.new(@viewport)[/p]     @s_light.x=@character.screen_x[/p]     @s_light.y=@character.screen_y+32*@light_height[/p]     @s_light.z=@character.screen_z[/p]     yix@s_light.bitmap=Bitmap.new(radius*2,radius*2)[/p]     yix@s_light.opacity=90[/p]     yix@s_light.ox+=radius[/p]     yix@s_light.oy+=radius[/p]     yix@s_light.angle=0 if ==2[/p]     yix@s_light.angle=270 if ==4[/p]     hbc@s_light.angle=180 if ==8[/p]     hbc@s_light.angle=90 if ==6[/p]     @s_light.bitmap.draw_pie(radius,radius,radius,Color.new(255,255,100,90),@anglemi[/p] n, )[/p] end[/p] end[/p] #==============================================================================[/p] # Class shadow[/p] #==============================================================================[/p] class Shadow[/p] attr_accessor :character[/p] attr_reader :distance, :distancemax, :overlayed[/p] @shadows=[][/p] #--------------------------------------------------------------------------[/p] def initialize(viewport,character, param,light)[/p]     hbc@character = character[/p]     hbc@viewport = viewport[/p]     unless param.nil?[/p]       hbc@parameters = param[0].split('|')[/p]       hbc@shadow_max_height = [1].to_i[/p]       @anglemin=light.parameters[0].to_f[/p]       @anglemax=light.parameters[1].to_f[/p]       @distancemax=light.parameters[2].to_f[/p]       hbc@light_height= light.parameters[3].to_i[/p]       hbc@source = light[/p]       hbc@s_shadow = RPG::Sprite.new(@viewport)[/p]       hbc@s_shadow.color = Color.new(0, 0, 0)[/p]       update[/p]     end[/p]     @@shadows.push(self)[/p] end[/p] #--------------------------------------------------------------------------[/p] def Shadow.set[/p]     return @shadows[/p] end[/p] #--------------------------------------------------------------------------[/p] def sprite[/p]     return [/p] end[/p] #--------------------------------------------------------------------------[/p] def dispose[/p]     hbc@s_shadow.dispose[/p]     @@shadows.delete(self)[/p] end[/p] #--------------------------------------------------------------------------[/p] def in_light_range?[/p]     return (@distance<=@distancemax)[/p] end[/p] #--------------------------------------------------------------------------[/p] def overlayed?[/p]     hbc@overlayed = false[/p]     @@shadows.each do |i|[/p]       s = i.sprite[/p]       next if s.nil? or i == self or !=0 or s.disposed?[/p]       if (@s_shadow.z)>s.z and ?(s.angle-1.5,s.angle+1.5)       and ((@character.x-i.character.<img src="http://rpgmkr.net/forum/public/style_emoticons/default/sourirex.gif" border="0" />**2 + (@character.y-i.character.y)**2)**0.5<=s.zoom_y        and s.z>=0[/p]         =false[/p]         = true[/p]       end[/p]       return if !hbc@s_shadow.visible[/p]     end[/p] [/p]     return [/p] end[/p] #--------------------------------------------------------------------------[/p] def update[/p]     # set shadow visibility according to the light state[/p]     hbc@s_shadow.visible = !hbc@source.character.off[/p]     if or <= SHADOWS_OPACITY_THRESHOLD[/p]       rsg@s_shadow.visible = false[/p]       return[/p]     end[/p]     if ? and ? and ?[/p]       rsg@old_amin= [/p]       rsg@old_amax= [/p]       rsg@old_dir = [/p]     end[/p]     # adapt the angle according to the source direction[/p]     case [/p]       when 2[/p]         = +180[/p]         = +180[/p]       when 4[/p]         = +90[/p]         = +90[/p]       when 8[/p]         = [/p]         = [/p]       when 6[/p]         = +270[/p]         = +270[/p]       end[/p]     if !=@source.character.direction[/p]       wvy@old_dir = [/p]     end[/p]     # simplify the angles if more than 360 or less than 0[/p]     @anglemin%=360;@anglemax%=360[/p] [/p]     tile_height= [/p]     @deltax=(@source.character.real_x-@character.real_X)/4[/p]     wvy@deltay= ((@source.character.real_y+@light_height*128)-(@character.real_y+tile_height*128))/4[/p]     wvy@distance = (((@deltax ** 2) + (@deltay ** 2))** 0.5)[/p]     if !=0 and >@distancemax[/p]       rfk@s_shadow.visible = false[/p]       return[/p]     end[/p] [/p]     rfk@s_shadow.angle = 57.3*Math.atan2(@deltax, )[/p]     rfk@angle_trigo= (@s_shadow.angle+90) % 360[/p] [/p]     #test if there is a shadow above it, from something close,and then[/p]     # don't display its shadow[/p]     return if overlayed?[/p] [/p]     if !=0 or !=0[/p]       if (@angle_trigo<@anglemin or >@anglemax) and           @anglemin<@anglemax[/p]         = false[/p]         return[/p]       elsif (@angle_trigo<@anglemin and >@anglemax) and              @anglemin>@anglemax[/p]         = false[/p]         return[/p]       end    [/p]     end[/p]     rfk@s_shadow.update[/p]     if != or[/p]        byp@character_name != or[/p]        byp@character_hue != [/p]        byp@tile_id = [/p]       byp@character_name = [/p]       byp@character_hue = [/p]       if >= 384[/p]         = RPG::Cache.tile($game_map.tileset_name,[/p]         , )[/p]         (0, 0, 32, 32)[/p]         = 16[/p]         = 32[/p]       else[/p]         = RPG::Cache.character(@character.character_name,[/p]           @character.character_hue)[/p]         = / 4[/p]         = / 4[/p]         = / 2[/p]         = [/p]       end[/p]     end[/p]     byp@s_shadow.x = [/p]     byp@s_shadow.y = [/p]     byp@s_shadow.z = (@ch)-1[/p] [/p]     if !=0 # if the sprite graphic is from the tileset[/p]       # set the Z-Index using the tileset priority settings[/p]       byp@s_shadow.z = ($game_map.priorities[@tile_id])*32[/p]       # convert the shadow angle, into 8-directions (0-7)[/p]       direction_shadow=((@s_shadow.angle/45+45/2.0+90).round)%8[/p]       # for the middle-Top and top layers,[/p]       for i in [1, 2][/p]         # get the tile from the converted angle[/p]         tile_around=[$game_map.data[@character.x,@character.y-1,i],[/p]         $game_map.data[@character.x-1,@character.y-1,i],[/p]         $game_map.data[@character.x-1,@character.y,i],[/p]         $game_map.data[@character.x-1,@character.y+1,i], [/p]         $game_map.data[@character.x,@character.y+1,i],[/p]         $game_map.data[@character.x+1,@character.y+1,i],[/p]         $game_map.data[@character.x+1,@character.y,i],[/p]         $game_map.data[@character.x+1,@character.y-1,i]][/p]         # if the tile is the empty one go to the next layer[/p]         next if tile_around[direction_shadow]==0[/p]         # else, lower the Z-index if the tile around is "above" or at the same[/p]         # priority of our tile[/p]         =32 if $game_map.priorities[tile_around[direction_shadow]]>=$game_map.priorities[@tile_id][/p]       end[/p]     end[/p]     if == 0[/p]       sx = * [/p]       quarter = ((@angle_trigo/90+0.5).floor)%4[/p] [/p]       # The quarter is the position of the event relative to the source.[/p]       # Imagine the source is the o point (0,0). Trace the 2 lines[/p]       # y=x and y=-x : you get something like a big X[/p]       # On the right, quarter=0. Up, quarter = 1, and so on[/p]       # Take the row (2,4,6,8), and the quarter[/p]       # column (0,1,2,3) (remember, it starts at 0), and you'll get[/p]       # a number between 1 and 4. It correspond to the row of the charset[/p]       # the shadow will be, and mirrored if negative.[/p]       # Yes, it isn't obvious, but I didn't find any simple operation to[/p]       # get those.[/p]       magic = SHADOWS_DIRECTION_ARRAY[@character.direction][quarter][/p]       magic = -magic[/p]       if magic < 0[/p]         = true[/p]         magic = -magic[/p]       else[/p]         = false[/p]       end[/p]       sy = (magic-1)*kpu@ch[/p]       @s_shadow.src_rect.set(sx, sy, , )[/p]     end[/p]     # This is the formula of the opacity in function of the distance[/p]     # ** 2 means square[/p]     kpu@s_shadow.opacity = 1200/((@distance ** 2)/ 1000 + 6)[/p]     # This is the formula of the size in function of the distance[/p]     # The 0.75 is here so you have a size of 1:1 when next to the source.[/p]     kpu@s_shadow.zoom_y=0.75*(@shadow_max_height+1) + (@distance) / 256 if SHADOW_GETS_LONGER[/p] end[/p] end[/p] #==============================================================================[/p] # ** Zlib    [/p] #==============================================================================[/p] [/p] module Zlib[/p] #============================================================================[/p] # ** Png_File    [/p] #============================================================================[/p] [/p] class Png_File < GzipWriter[/p]     #--------------------------------------------------------------------------[/p]     # * Make PNG[/p]     #--------------------------------------------------------------------------[/p]     def make_png(bitmap, mode = 0)[/p]       # Save Bitmap & Mode[/p]       @bitmap, = bitmap, mode[/p]       # Create & Save PNG[/p]       self.write(make_header)[/p]       self.write(make_ihdr)[/p]       self.write(make_idat)[/p]       self.write(make_iend)[/p]     end[/p]     #--------------------------------------------------------------------------[/p]     # * Make Header[/p]     #--------------------------------------------------------------------------[/p]     def make_header[/p]       return [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].pack('C*')[/p]     end[/p]     #--------------------------------------------------------------------------[/p]     # * Make IHDR[/p]     #--------------------------------------------------------------------------[/p]     def make_ihdr[/p]       ih_size               = [13].pack("N")[/p]       ih_sign               = 'IHDR'[/p]       ih_width              = [@bitmap.width].pack('N')[/p]       ih_height             = [@bitmap.height].pack('N')[/p]       ih_bit_depth          = [8].pack('C')[/p]       ih_color_type        = [6].pack('C')[/p]       ih_compression_method = [0].pack('C')[/p]       ih_filter_method      = [0].pack('C')[/p]       ih_interlace_method   = [0].pack('C')[/p]       string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +[/p]                ih_compression_method + ih_filter_method + ih_interlace_method[/p]       ih_crc = [Zlib.crc32(string)].pack('N')[/p]       return ih_size + string + ih_crc[/p]     end[/p]     #--------------------------------------------------------------------------[/p]     # * Make IDAT[/p]     #--------------------------------------------------------------------------[/p]     def make_idat[/p]       header = "\\x49\\x44\\x41\\x54"[/p]       data    = == 0 ? make_bitmap_data0 : make_bitmap_data1[/p]       data    = Zlib::Deflate.deflate(data, 8)[/p]       crc     = [Zlib.crc32(header + data)].pack('N')[/p]       size    = [data.length].pack('N')[/p]       return size + header + data + crc[/p]     end[/p]     #--------------------------------------------------------------------------[/p]     # * Make Bitmap Data 0[/p]     #--------------------------------------------------------------------------[/p]     def make_bitmap_data0[/p]       gz = Zlib::GzipWriter.open('hoge.gz')[/p]       t_Fx = 0[/p]       w = [/p]       h = [/p]       data = [][/p]       for y in 0...h[/p]         data.push(0)[/p]         for x in 0...w[/p]           t_Fx += 1[/p]           if t_Fx % 10000 == 0[/p]             Graphics.update[/p]           end[/p]           if t_Fx % 100000 == 0[/p]             s = data.pack("C*")[/p]             gz.write(s)[/p]             data.clear[/p]           end[/p]           color = (x, y)[/p]           red = color.red[/p]           green = color.green[/p]           blue = color.blue[/p]           alpha = color.alpha[/p]           data.push(red)[/p]           data.push(green)[/p]           data.push(blue)[/p]           data.push(alpha)[/p]         end[/p]       end[/p]       s = data.pack("C*")[/p]       gz.write(s)[/p]       gz.close [/p]       data.clear[/p]       gz = Zlib::GzipReader.open('hoge.gz')[/p]       data = gz.read[/p]       gz.close[/p]       File.delete('hoge.gz')[/p]       return data[/p]     end[/p]     #--------------------------------------------------------------------------[/p]     # * Make Bitmap Data Mode 1[/p]     #--------------------------------------------------------------------------[/p]     def make_bitmap_data1[/p]       w = [/p]       h = [/p]       data = [][/p]       for y in 0...h[/p]         data.push(0)[/p]         for x in 0...w[/p]           color = (x, y)[/p]           red = color.red[/p]           green = color.green[/p]           blue = color.blue[/p]           alpha = color.alpha[/p]           data.push(red)[/p]           data.push(green)[/p]           data.push(blue)[/p]           data.push(alpha)[/p]         end[/p]       end[/p]       return data.pack("C*")[/p]     end[/p]     #--------------------------------------------------------------------------[/p]     # * Make IEND[/p]     #--------------------------------------------------------------------------[/p]     def make_iend[/p]       ie_size = [0].pack('N')[/p]       ie_sign = 'IEND'[/p]       ie_crc = [Zlib.crc32(ie_sign)].pack('N')[/p]       return ie_size + ie_sign + ie_crc[/p]     end[/p] end[/p] end[/p] #==============================================================================[/p] # Class Game Event[/p] #==============================================================================[/p] class Game_Event[/p] alias treb_mobile_source_game_event_initialize initialize[/p] attr_accessor :off, :direction[/p] def initialize(*args)[/p]     # create the off attribute, only used for the lights[/p]     fag@off = false[/p]     treb_mobile_source_game_event_initialize(*args)[/p] end[/p] end[/p] #==============================================================================[/p] # class Spriteset Map[/p] #==============================================================================[/p] class Spriteset_Map[/p] alias trebor777_DLS_spriteset_map_init_characters init_characters[/p] alias trebor777_DLS_spriteset_map_update_character_sprites update_character_sprites[/p] alias trebor777_DLS_spriteset_map_dispose dispose[/p] #--------------------------------------------------------------------------[/p] def init_characters[/p]     trebor777_DLS_spriteset_map_init_characters[/p]     # create the lights and shadows arrays[/p]     fag@lights = [][/p]     fag@shadows = [][/p]     fag@viewport1.visible=false[/p]     fag@viewport2.visible=false[/p]     fag@viewport3.visible=false[/p]     for i in $game_map.events.keys.sort[/p]       # Search for light trigger[/p]       light_param = SDK.event_comment_input($game_map.events[i], 1, "Light")[/p]       # if not found go to the next iteration[/p]       next if light_param.nil?[/p]       # else add it to the Lights Hash, and create a new light[/p]       @lights.push( Light.new(@viewport1,$game_map.events[i],light_param) )[/p]       # create the shadow for the player.[/p]       @shadows.push( Shadow.new(@viewport1,$game_player, ['0'], ) )[/p]     end[/p]     for i in $game_map.events.keys.sort[/p]       for light in LightS[/p]         # Search for shadow trigger[/p]         shadow_param = SDK.event_comment_input($game_map.events[i], 1, "Shadow")[/p]         next if shadow_param.nil?[/p]         ( Shadow.new(@viewport1,$game_map.events[i], shadow_param, light) )[/p]       end[/p]     end[/p]     uus@viewport1.visible=true[/p]     uus@viewport2.visible=true[/p]     uus@viewport3.visible=true[/p] end[/p] #--------------------------------------------------------------------------[/p] def update_character_sprites[/p]     trebor777_DLS_spriteset_map_update_character_sprites[/p]     lights_off = 0[/p]     # Update the lights[/p]     for light in LightS[/p]       light.update[/p]       # count the number of lights off[/p]       lights_off+=1 if light.character.off[/p]     end[/p]     # Update the Shadows[/p]     for shade in Shadows[/p]       shade.update[/p]     end[/p]     if >0[/p]       # updating screen tinting according to the number of lights on[/p]       value=((lights_off.to_f/@lights.size)*(-180)).round[/p]       $game_screen.start_tone_change(Tone.new(value,value,value,value), 1)[/p]     else[/p]       $game_screen.start_tone_change(Tone.new(0,0,0,0), 1)[/p]     end[/p] end[/p] #--------------------------------------------------------------------------[/p] def dispose[/p]     for light in LightS[/p]       light.dispose[/p]     end[/p]     # Update the Shadows[/p]     for shade in Shadows[/p]       shade.dispose[/p]     end[/p]     axu@lights = [][/p]     axu@shadows = [][/p] end[/p] end[/p] #==============================================================================[/p] # Class Bitmap[/p] #==============================================================================[/p] class Bitmap[/p] #--------------------------------------------------------------------------[/p] def draw_line(x1, y1, x2, y2, width = 1, color = Color.new(255, 255, 255))[/p]     # Return if width is less than or 0[/p]     return if width <= 0[/p]     # Reverse all parameters sent if 2 x is less than the first x[/p]     x1, x2, y1, y2 = x2, x1, y2, y1 if x2 < x1    [/p]     # Get S (1/2 width)[/p]     s = width / 2.0[/p]     # If X Coordinates are equal[/p]     if x1 == x2[/p]       # Draw Vertical line[/p]       fill_rect(x1 - s, [y1, y2].min, width, (y2 - y1).abs, color)[/p]     # If Y Coordinates are equal[/p]     elsif y1 == y2[/p]       # Draw Horizontal line[/p]       fill_rect(x1, y1 - s, x2 - x1, width, color)[/p]     end[/p]     # Get Length[/p]     length = x2 - x1 < (y2 - y1).abs ? (y2 - y1).abs : x2 - x1[/p]     # Get Increments[/p]     x_increment, y_increment = (x2 - x1) / length.to_f, (y2 - y1) / length.to_f[/p]     # Get Current X and Y[/p]     x, y = x1, y1[/p]     # While Current X is less than end X[/p]     while x < x2[/p]       # Draw Box of width width and width height[/p]       fill_rect(x-s, y-s, width, width, color)[/p]       # Increment X and Y[/p]       x += x_increment[/p]       y += y_increment[/p]     end[/p] end[/p] #--------------------------------------------------------------------------[/p] # Draw pie, take quite a long time as it draws each line.[/p] #--------------------------------------------------------------------------[/p] def draw_pie(x,y,radius,color = Color.new(255, 255, 255, 255),start_angle=0,end_angle=360)[/p]     end_angle+=360 if end_angle[/p]     name_string="#{radius}_#{start_angle}_#{end_angle}"[/p]     #filename="Graphics/Pictures/#{name_string}.png"[/p]     if FileTest.exist?("Graphics/Pictures/#{name_string}.png")[/p]       temp = RPG::Cache.picture(name_string)[/p]       blt(0,0,temp,temp.rect)[/p]     else[/p]       Graphics.transition[/p]       t = Progress_Bar.new(160,240,320,20,end_angle-start_angle+2 )[/p]       for i in start_angle...end_angle[/p]         t.current_step+=1[/p]         t.update[/p]         Graphics.update[/p]         for j in 0..2[/p]           x_=(Math::cos((i+j/2.0)*Math::PI/180)*radius).round+x[/p]           y_=(Math::sin((i+j/2.0)*Math::PI/180)*radius).round+y[/p]           draw_line(x, y, x_, y_, 2,color)[/p]         end[/p]       end[/p]       t.current_step+=1[/p]       t.update[/p]       Graphics.update[/p]       make_png(name_string, 'Graphics/Pictures/')[/p]       t.current_step+=1[/p]       t.update[/p]       Graphics.update[/p]       t.dispose[/p]     end[/p] end[/p] def make_png(name = 'like', path = '', mode = 0)[/p]     Dir.make_dir(path) if path != '' and !FileTest.directory?(path)[/p]     Zlib::Png_File.open('temp.gz')   { |gz| gz.make_png(self, mode) }[/p]     Zlib::GzipReader.open('temp.gz') { |gz| $read = gz.read }[/p]     f = File.open(path + name + '.png', 'wb')[/p]     f.write($read)[/p]     f.close[/p]     File.delete('temp.gz')[/p] end[/p] end[/p] [/p] class Progress_Bar < Sprite[/p] #--------------------------------------------------------------------------[/p] # * Public Instance Variables[/p] #--------------------------------------------------------------------------[/p] attr_accessor :current_step   # The Current Step[/p] attr_accessor :steps          # The max amount of steps[/p] #----------------------------------------------------------------------------[/p] # * Initialize Object[/p] #----------------------------------------------------------------------------[/p] def initialize(x,y,width,height=16,steps=100,start=0)[/p]     super()[/p]     axu@steps = steps[/p]     self.x = x[/p]     self.y = y[/p]     axu@current_step= start[/p]     axu@width = width[/p]     axu@height = height[/p]     axu@w = 9[/p]     axu@nb_bars = /@w[/p]     axu@c1 = Color.new(46,211,49,255)[/p]     axu@c2 = Color.new(46,211,49,227)[/p]     axu@c3 = Color.new(46,211,49,202)[/p]     axu@c4 = Color.new(46,211,49,177)[/p]     axu@c5 = Color.new(46,211,49,152)[/p]     axu@c6 = Color.new(46,211,49,127)[/p]     axu@stretch_c = (@height-4)/16.0[/p]     self.bitmap = Bitmap.new(@width, )[/p]     self.bitmap.clear[/p]     self.bitmap.fill_rect(0, 0, , , Color.new(59,59,59,167))[/p]     draw[/p] end[/p] #----------------------------------------------------------------------------[/p] # * Draw Bar[/p] #----------------------------------------------------------------------------[/p] def draw[/p]     self.bitmap.fill_rect(1, 1, , , Color.new(59,59,59,0))[/p]     for i in 0...((@current_step/@steps.to_f)*@nb_bars).round[/p]       self.bitmap.fill_rect(i*@w+1,2,@w-2,@height-4,@c6)[/p]       self.bitmap.fill_rect(i*@w+1,3*@stretch_c,@w-2,@height-6*@stretch_c,@c5)[/p]       self.bitmap.fill_rect(i*@w+1,4*@stretch_c,@w-2,@height-8*@stretch_c,@c4)[/p]       self.bitmap.fill_rect(i*@w+1,5*@stretch_c,@w-2,@height-10*@stretch_c,@c3)[/p]       self.bitmap.fill_rect(i*@w+1,6*@stretch_c,@w-2,@height-12*@stretch_c,@c2)[/p]       self.bitmap.fill_rect(i*@w+1,7*@stretch_c,@w-2,@height-14*@stretch_c,@c1)[/p]     end[/p]     ipz@old = [/p] end[/p] #----------------------------------------------------------------------------[/p] # * Update[/p] #----------------------------------------------------------------------------[/p] def update[/p]     draw if != [/p] end[/p] end[/p] #SDK test[/p] end

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)