Ooops
January 26, 2021, 04:34:22 AM

Author Topic: [bb] Background or Texture creator by n8r2k [ 1+ years ago ]  (Read 574 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Background or Texture creator
Author : n8r2k
Posted : 1+ years ago

Description : I accidentally discovered the green one when i was messing around with a matrix of binary numbers. Then i decided to use plot instead of text. Then i messed around with the colors and found some neat stuff. Now is on <a href="http://n8r2k.deviousbytes.com/" target="_blank">http://n8r2k.deviousbytes.com/[/url] .

Code :
Code: BlitzBasic
  1. Graphics 800,600,16,2
  2. SeedRnd MilliSecs()
  3. cols = 800
  4. rows = 600
  5. Dim num(Cols)
  6.  
  7. Goto blue ;change this to what u want
  8.  
  9.  
  10. .grass
  11. For x = 0 To cols Step 1
  12.         For y = 0 To rows Step 1
  13.         Color 0,Rand(0,200),0
  14.                 Plot x,y
  15. Next
  16. Next
  17. SaveBuffer(FrontBuffer(),"grass.png")
  18. Goto close
  19.  
  20. .foil
  21. For x = 0 To cols Step 1
  22.         For y = 0 To rows Step 1
  23.         Color Rand(0,200),Rand(0,200),Rand(0,200)
  24.                 Plot x,y
  25. Next
  26. Next
  27. SaveBuffer(FrontBuffer(),"foil.png")
  28. Goto close
  29.  
  30. .blue
  31. For x = 0 To cols Step 1
  32.         For y = 0 To rows Step 1
  33.         Color Rand(0,200),Rand(0,200),255
  34.                 Plot x,y
  35. Next
  36. Next
  37. SaveBuffer(FrontBuffer(),"blue.png")
  38. Goto close
  39.  
  40. .red
  41. For x = 0 To cols Step 1
  42.         For y = 0 To rows Step 1
  43.         Color Rand(0,200),0,0
  44.                 Plot x,y
  45. Next
  46. Next
  47. SaveBuffer(FrontBuffer(),"red.png")
  48. Goto close
  49.  
  50. .close
  51. End


Comments :


n8r2k(Posted 1+ years ago)

 *Note, this only works with debug on if you want to see the pic not sure y


Erroneouss(Posted 1+ years ago)

 SaveBuffer(FrontBuffer(),"red.png") DOES NOT WORK. Neither does the other ones.  SaveBuffer only saves to ".bmp"


Kevin_(Posted 1+ years ago)

 n8r2k....Why don't you try and design your programs using functions?They make it easier for others to understand your code as well as isolating parts of the program that just do specific tasks. A few comments here and there would also be nice to tell people what it is actually doing.Oh, and you'll get more 'street-cred' if you get rid of those nasty GOTO's.


WarpZone(Posted 1+ years ago)

 Hey, I think I know why he didn't want to use functions. :)  I tried to convert it from gotos to Functions, and I got the syntax right, but nothing was happening. :P  Maybe n8r2k also hit this stumbling block?Well, I looked it up in the command index, and it turns out, variables declared outside of a function are not availible inside the function.  So you have to pass the variables to the function, by listing your variables inside the little brackets, seperated by commas.
Code: [Select]
Graphics 800,600,16,2
SeedRnd MilliSecs()
Global cols# = 800 rows# = 600
Dim num(Cols)

Foil(cols#,rows#) ;change this to what u want

Function Grass(cols#,rows#)
For x = 0 To cols Step 1
For y = 0 To rows# Step 1
Color 0,Rand(0,200),0
Plot x,y
Next
Next
SaveBuffer(FrontBuffer(),"grass.bmp")
End Function

Function Foil(cols#,rows#)
For x = 0 To cols Step 1
For y = 0 To rows# Step 1
Color Rand(0,255),Rand(0,256),Rand(0,256)
Plot x,y
Next
Next
SaveBuffer(FrontBuffer(),"foil.bmp")
End Function

Function Blue(cols#,rows#)
For x = 0 To cols# Step 1
For y = 0 To rows# Step 1
Color Rand(0,200),Rand(0,200),255
Plot x,y
Next
Next
SaveBuffer(FrontBuffer(),"blue.bmp")
End Function

Function Red(cols#,rows#)
For x = 0 To cols# Step 1
For y = 0 To rows# Step 1
Color Rand(0,200),0,0
Plot x,y
Next
Next
SaveBuffer(FrontBuffer(),"red.bmp")
End Function

End
So here's your original program, n8r2k, using functions instead of gotos.  Overall, good job! :)  This was a good learning experience for me, and I might be able to make something really cool out of your simple program.My next step will be to condense all those functions into one single function, and just pass it an extra variable to determine which colors to use.  :)  But I wanted you to see it in this form, n8r2k, so that you could see how functions work.  It's really not that difficult, and it will make things much smoother for you down the road as a programmer if you get the hang of using functions now. :)Or to put it another way...thx d00d this wuz cool u maek mor kthxbi :) !!!!!!11!1


WarpZone(Posted 1+ years ago)

 Here's your program with just one function.  I explained the changes I made with comments as much as I could.  Look at how much smaller the program is! :)I also realized that my version of Foil was too colorful, so I renamed it ColorTV and made up a new Foil effect for you.  :)  I think you will like it, it uses ALL greyscale values by only generating a random number once, and then re-using the value for all 2 colors.
Code: [Select]
Graphics 800,600,16,2
SeedRnd MilliSecs()
cols# = 800 rows# = 600 ;changed the vars to var#s just so we're clear these are NUMBERS and not STRINGS.  It's good ettiquite. ;)
NoiseColor$ = "ColorTV"   ;This is the part you change.  Must be set to either "Grass" , "Foil" , "Blue" , "Red" , or new ColorTV! :)
Dim num(Cols)

Noise(cols#,rows#,NoiseColor$)

Function Noise(cols#,rows#,NoiseColor$)
For x = 0 To cols Step 1
For y = 0 To rows# Step 1

Select NoiseColor$ ;here I used a SELECT/CASE/DEFAULT/END SELECT command here rather than mucking about with all the if statements it would take.  Look up "case" in the Blitz command reference for more info. ;)
Case "Grass"  Color 0,Rnd(0,256),0
Case "Foil"  GreyColor# = Rnd(0,255) Color GreyColor# , GreyColor# , GreyColor#
Case "ColorTV"  Color Rnd(0,255),Rnd(0,256),Rnd(0,256)
Case "Blue"  Color 0,0,Rnd(0,256)
Case "Red"  Color Rnd(0,255),0,0
End Select
Plot x,y
Next
Next

SaveBuffer(FrontBuffer(), NoiseColor$ + ".bmp")  ;Here we see how easy it is to turn a string variable into a filename.  Just literally add the file extention to the string.  (I wasn't actually sure this would work until I tried it. :P )
End Function


End
It's interesting to note that if you put in a bad value for the NoiseColor$ string, it will use all white.  I wonder why that is. :PHmmm... I think next, I'll kick it up a notch by letting the user specify image dimmensions, min and max RGB values, and a filename while the program is running. ;)


WarpZone(Posted 1+ years ago)

 Whew. :)  Debugging this thing was a real pain. :)  But I finally got it to run properly.  Well, okay, I got it to parse. :P  There is still a major bug because the print statements cause the "written" zone of the screen to include the whole screen, not just area where plot statements have occured. :PI'll work on correcting this bug tomorrow.  (Probably by using a different buffer other than the front buffer to write my data to. ;) What were you thinking?  j/k, I know you probabaly wanted to see the WIP.)Feel free to enter this code and try it out.  You can generate some really nice effects by specifying different min and max values.  
Code: [Select]
Graphics 800,600,16,2
SeedRnd MilliSecs()

;No need to declare values here!  We'll be obtaining them from the user with INPUT statements!

cols# =Input$("Image Height?")
rows# =Input$("Image Width?")
Dim num(Cols)
Filename$=Input$("What should we call this image?")
Greyscale$=Input$("Is the image Greyscale? (y/n)")

If Greyscale$ = "y"
MinR# = Input$("Minimum Brightness? (0-255)")
MaxR# = Input$("Maximum Brightness? (0-255)")
Noise(cols#,rows#,Filename$,Greyscale$,MinR#,MaxR#,0,0,0,0)

Else If Greyscale$ = "n"
MinR# = Input$("Minimum Red? (0-255)")
MaxR# = Input$("Maximum Red? (0-255)")
MinB# = Input$("Minimum Blue? (0-255)")
MaxB# = Input$("Maximum Blue? (0-255)")
MinG# = Input$("Minimum Green? (0-255)")
MaxG# = Input$("Maximum Green? (0-255)")
Noise(cols#,rows#,Filename$,Greyscale$,MinR#,MaxR#,MinB#,MaxB#,MinG#,MaxG#)
End If

End




Function Noise(cols#,rows#,Filename$,Greyscale$,MinR#,MaxR#,MinB#,MaxB#,MinG#,MaxG#)

For x = 0 To cols Step 1
For y = 0 To rows# Step 1

Select Greyscale$
Case "y"  GreyColor# = Rnd(MinR#,MaxR#) Color GreyColor# , GreyColor# , GreyColor#
Case "n"  Color Rnd(MinR#,MaxR#),Rnd(MinG#,MaxG#),Rnd(MinB#,MaxB#)
End Select
Plot x,y
Next
Next

SaveBuffer(FrontBuffer(), Filename$ + ".bmp")
End Function
See what effects you can achieve by combining different colors.  Here are some of my favorite combos:CREAM:Greyscale = y, Minimum = 128, Maximum = 255ASPHALT:Greyscale = y, Minimum = 16, Maximum = 32OCEAN:Greyscale = n, MinR = 0 , Max R = 0, MinB=128, MaxB=255, MinG=0, MaxG=128QUILT:Greyscale = n, MinR = 128 , Max R = 255, MinB=128, MaxB=255, MinG=128, MaxG=255I feel like I'm learning more about Blitz Basic coding than ever before! :D  Thanks for posting, n8r2k, you've given me something nice and simple to build upon! :D


WarpZone(Posted 1+ years ago)

 Okay, here we go! :)  I had to look up a few commands in order to figure it out (I've never messed with buffers before) but here you go:
Code: [Select]
Graphics 800,600,16,2
SeedRnd MilliSecs()

;No need to declare variables here! We'll be obtaining them from the user with INPUT statements!

cols# =Input$("Image Height?")
rows# =Input$("Image Width?")
Dim num(Cols#)
Filename$=Input$("What should we call this image?")
Greyscale$=Input$("Is the image Greyscale? (y/n)")

If Greyscale$ = "y"
MinR# = Input$("Minimum Brightness? (0-255)")
MaxR# = Input$("Maximum Brightness? (0-255)")
Noise(cols#,rows#,Filename$,Greyscale$,MinR#,MaxR#,0,0,0,0)

Else If Greyscale$ = "n"
MinR# = Input$("Minimum Red? (0-255)")
MaxR# = Input$("Maximum Red? (0-255)")
MinB# = Input$("Minimum Blue? (0-255)")
MaxB# = Input$("Maximum Blue? (0-255)")
MinG# = Input$("Minimum Green? (0-255)")
MaxG# = Input$("Maximum Green? (0-255)")
Noise(cols#,rows#,Filename$,Greyscale$,MinR#,MaxR#,MinB#,MaxB#,MinG#,MaxG#)
End If

End

Function Noise(cols#,rows#,Filename$,Greyscale$,MinR#,MaxR#,MinB#,MaxB#,MinG#,MaxG#)

MyTexture=CreateImage(cols#,rows#) ;MyTexture is a variable name.  CreateImage creates a new image within texture memory, just like a texture in a 3D game.  At first, it's a blank image, but...
SetBuffer ImageBuffer(MyTexture) ; By setting our new image as a buffer, we can draw to it instead of directly to the screen! :D

For x = 0 To cols Step 1
For y = 0 To rows# Step 1

Select Greyscale$
Case "y"  GreyColor# = Rnd(MinR#,MaxR#) Color GreyColor# , GreyColor# , GreyColor#
Case "n"  Color Rnd(MinR#,MaxR#),Rnd(MinG#,MaxG#),Rnd(MinB#,MaxB#)
End Select

Plot x,y
Next
Next

SaveBuffer  (ImageBuffer(MyTexture), Filename$ + ".bmp") ; Now we just save our buffer instead of FrontBuffer!  Easy! :D
End Function
One issue is that you can't see the image being drawn on the screen anymore, though it outputs to the directory just fine.  We could address this by drawing the MyTexture buffer to the screen a few times while the program is running.  I think for my next trick, I'll try to add a GUI, so you can just adjust sliders or whatnot to alter the variables, preview your image, and save it to disk.  (You'll be able to create multiple images by running the program once, and you won't have to type in all those numbers. :P )By the way, I declare that all my code on this page is Public Domain. ;)


WarpZone(Posted 1+ years ago)

 By the way...I wasn't sure what the "Dim num(Cols#)" line was for, so I commented it out.  The program still works exactly the same.  Can anybody shed some light on this?  Maybe it's another holdover from whatever language taught n8r2k to use GOTO statements? :P  (LOL j/k n8r2k, keep up the good work! :)  u'r getting better and better at what u do! I've seen some of ur other programs!  dismantling ur simple BASIC-style progs and turning them into functions is teaching me teh 1337 Blitz skillzorz! so thx, m8!)


n8r2k(Posted 1+ years ago)

 I am just now reading these after a long break from programming, and i have to say, i am happy to give u somthing to do warpzone, i think. I dont remember why i used gotos, maybe cause i learned to code on an apple and it felt more natural? any way i have learned to use functions somewhat more efficiantly. I didn't know save buffer only saves a .bmp, it worked fine for me. Not sure whats up with that. If any one ever reads this anyway. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal