November 28, 2020, 11:03:25 AM

Author Topic: [bb] Cartoon Speech Bubbles by Andy_A [ 1+ years ago ]  (Read 512 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Cartoon Speech Bubbles by Andy_A [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Cartoon Speech Bubbles
Author : Andy_A
Posted : 1+ years ago

Description : There's enough parameter's available to make a speech bubble for every occasion.

Code :
Code: BlitzBasic
  1. ;     Title: Cartoon Speech Bubbles
  2. ;Programmer: Andy Amaya
  3. ;      Date: 2011.06.21
  4.  
  5. ;       FillTriangle by Tom Toad
  6. ;http://www.blitzmax.com/codearcs/codearcs.php?code=1200
  7.  
  8. AppTitle "Cartoon Speech Bubbles"
  9. Global sw, sh
  10. sw = 800: sh = 600
  11. Graphics sw, sh, 32, 2
  12. SetBuffer BackBuffer()
  13.  
  14. Const black% = $00
  15. Const darkblue%= $80
  16. Const red%   = $FF0000
  17. Const green% = $8000
  18. Const palegreen%=$A0FFA0
  19. Const gray1% = $D0D0D0
  20. Const yellow%= $FFFFC0
  21. Const white% = $FFFFFF
  22. Const skyblue%=$20A0FF
  23.  
  24. setClsColor skyblue
  25.  
  26. While MouseHit(1) = 0
  27.         angle# = (angle# + 20.0)
  28.         If angle > 360.0 Then angle = angle - 360.0
  29.         Cls
  30.         colr = (colr + 1) Mod 4
  31.         Select colr
  32.                 Case 0: fill = white: outline = black
  33.                 Case 1: fill = gray1: outline = black
  34.                 Case 2: fill = yellow: outline = darkblue
  35.                 Case 3: fill = palegreen: outline = green
  36.         End Select
  37.        
  38.         sBubble(400, 300, 300, 150, Rand(30,200), angle, Rand(2,6), outline, fill)
  39.        
  40.         setColor red
  41.         Text 400,260,"Speech bubbles can be",True
  42.         Text 400,275, "small or large,", True
  43.         Text 400,290,"and can be just about any color!",True
  44.  
  45.         setColor outline
  46.         Text 400,335,"R-click for more bubbles",True
  47.         Text 400,350,"L-click to exit",True
  48.         Flip
  49.         WaitMouse()
  50. Wend
  51.  
  52. Function sBubble(x%, y%, ew%, eh%, tail%, angle#=0., pen%, outline%=0, fill=$FFFFFF)
  53. ;=====================================================================================
  54. ;       sBubble - Speech Bubble function
  55. ;=====================================================================================
  56. ; Parameters:
  57. ; x, y are the center coords of the ellipse used to construct the speech bubble
  58. ; ew, eh are the width and height of the ellipse, respectively
  59. ; tail = the length of the 'tail' coming from speech bubble its variable so you can
  60. ;        position the speech bubble far or near to the character
  61. ; angle = angle of tail coming from speech bubble    NOTE: 0 degrees is due East
  62. ; pen = thickness of outline surrounding the speech bubble
  63. ; outline = is the color of the outline in Hex (i.e. $000000 = black)
  64. ; fill    = is the fill color of the bubble in Hex (i.e. $FFFFFF = white)
  65. ;
  66. ; NB: This function requires: setColor(); fatLine(), and fillTriangle() functions.
  67. ;=====================================================================================
  68.         Local p2%, rx%, ry%, ltSideX%, ltSideY%, rtSideX%, rtSideY%, tailX%, tailY%
  69.        
  70.         If pen < 2 Then pen = 2
  71.         p2% = pen Shl 1
  72.         rx = ew/2
  73.         ry = eh/2
  74.        
  75.         ;draw the outline oval
  76.         setColor outline
  77.         Oval x-rx-pen, y-ry-pen, ew+p2, eh+p2, True
  78.         ;calc the points used to draw the tail outline
  79.         ltSideX = Cos(angle + 5.)*(rx-5) + x
  80.         ltSideY = Sin(angle + 5.)*(ry-5) + y
  81.        
  82.         rtSideX = Cos(angle - 5.)*(rx-5) + x
  83.         rtSideY = Sin(angle - 5.)*(ry-5) + y
  84.        
  85.         tailX = Cos(angle)*(rx+tail)+x
  86.         tailY = Sin(angle)*(ry+tail)+y
  87.  
  88.         ;draw the tail outline
  89.         fatLine(ltSideX, ltSideY, tailX, tailY, p2)
  90.         fatLine(rtSideX, rtSideY, tailX, tailY, p2)
  91.        
  92.         ;fill the tail and the main ellipse
  93.         setColor fill
  94.         fillTriangle(ltSideX, ltSideY, tailX, tailY, rtSideX, rtSideY)
  95.         Oval x-rx,y-ry,ew,eh,True
  96. End Function
  97.  
  98. Function setColor(hexValue%)
  99.         Color (hexValue Shr 16) And 255,(hexValue Shr 8) And 255, hexValue And 255
  100. End Function
  101.  
  102. Function setClsColor(hexValue%)
  103.         ClsColor (hexValue Shr 16) And 255,(hexValue Shr 8) And 255, hexValue And 255
  104. End Function
  105.  
  106. Function rgb2hex%(r%, g%, b%)
  107.         Return (r Shl 16) Or (g Shl 8) Or b
  108. End Function
  109.  
  110. Function reticle(x,y,colr)
  111.         Local r%, g%, b%
  112.         r = ColorRed
  113.         g = ColorGreen
  114.         b = ColorBlue
  115.         setColor(colr)
  116.         Line(x-5,y,x+5,y)
  117.         Line(x,y-5,x,y+5)
  118.         Color r,g,b
  119. End Function
  120.  
  121. ;FillTriangle by Tom Toad
  122. ;http://www.blitzmax.com/codearcs/codearcs.php?code=1200
  123. Function fillTriangle(x1#,y1#,x2#,y2#,x3#,y3#)
  124.         Local slope1#,slope2#,slope3#,x#,y#,length#
  125.  
  126.         ;make sure the triangle coordinates are ordered so that x1 < x2 < x3
  127.         If x2 < x1 Then x = x2: y = y2: x2 = x1: y2 = y1: x1 = x: y1 = y
  128.         If x3 < x1 Then x = x3: y = y3: x3 = x1: y3 = y1: x1 = x: y1 = y
  129.         If x3 < x2 Then x = x3: y = y3: x3 = x2: y3 = y2: x2 = x: y2 = y
  130.        
  131.         If x1 <> x3 Then slope1 = (y3-y1)/(x3-x1)
  132.         length = x2 - x1
  133.         ;draw the first half of the triangle
  134.         If length <> 0 Then
  135.                 slope2 = (y2-y1)/(x2-x1)
  136.                 For x = 0 To length
  137.                         Line x+x1,x*slope1+y1,x+x1,x*slope2+y1
  138.                 Next
  139.         End If
  140.  
  141.         y = length*slope1+y1
  142.         length = x3-x2
  143.         ;draw the second half
  144.         If length <> 0 Then
  145.                 slope3 = (y3-y2)/(x3-x2)
  146.                 For x = 0 To length
  147.                         Line x+x2,x*slope1+y,x+x2,x*slope3+y2
  148.                 Next
  149.         End If
  150. End Function
  151.  
  152. Function fatLine(x1%, y1%, x2%, y2%, penSize%)
  153.         If penSize% < 1 Then Return False
  154.         If penSize% = 1 Then Line(x1%, y1%, x2%, y2%): Return
  155.         ; penSize% is thickness to draw line
  156.         offset% = penSize% / 2 ;offset needed to place ovals correctly
  157.  
  158.         ; Calculate deltax and deltay for initialization
  159.         deltax% = Abs(x2% - x1%)
  160.         deltay% = Abs(y2% - y1%)
  161.  
  162.         ; Initialize all vars based on which is the independent variable
  163.         If deltax% >= deltay% Then
  164.                 ; x is the independent variable
  165.                 numovals% = deltax% + 1
  166.                 d% = (2 * deltay%) - deltax%
  167.                 dinc1% = deltay% Shl 1
  168.                 dinc2% = (deltay% - deltax%) Shl 1
  169.                 xinc1% = 1
  170.                 xinc2% = 1
  171.                 yinc1% = 0
  172.                 yinc2% = 1
  173.         Else
  174.                 ; y is the independent variable
  175.                 numovals% = deltay% + 1
  176.                 d% = (2 * deltax%) - deltay%
  177.                 dinc1% = deltax% Shl 1
  178.                 dinc2% = (deltax% - deltay%) Shl 1
  179.                 xinc1% = 0
  180.                 xinc2% = 1
  181.                 yinc1% = 1
  182.                 yinc2% = 1
  183.         End If
  184.         ; Make sure x and y move in the right directions
  185.         If x1% > x2% Then
  186.                 xinc1% = - xinc1%
  187.                 xinc2% = - xinc2%
  188.         End If
  189.         If y1% > y2% Then
  190.                 yinc1% = - yinc1%
  191.                 yinc2% = - yinc2%
  192.         End If
  193.         ; Start drawing at x%, y%
  194.         x% = x1% - offset%
  195.         y% = y1% - offset%
  196.         ; Draw the filled ovals
  197.         For i% = 1 To numovals%
  198.                 Oval x%, y%, penSize%, penSize%, True
  199.                 If d% < 0 Then
  200.                         d% = d% + dinc1%
  201.                         x% = x% + xinc1%
  202.                         y% = y% + yinc1%
  203.         Else
  204.                         d% = d% + dinc2%
  205.                         x% = x% + xinc2%
  206.                         y% = y% + yinc2%
  207.                 End If
  208.         Next
  209. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal