Author Topic: A Different Galaxy Maker  (Read 4203 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
A Different Galaxy Maker
« on: December 15, 2019, 10:23:57 pm »
Sir Crow made a Galaxy Generator back in September which inspired me to make my own today. His is still better in my opinion, but mine is just different. Mine also makes a spiral galaxy but they are made at random angles and random stars. I also added a way to save it at any time (even during the making of it) to a .bmp file. One thing I learned about the code I've been using to make .bmp pictures is that since I use the REN command to rename temp.bmp, your picture file cannot have spaces. I tried to change the nm$ to delete all the spaces between the characters but I couldn't figure it out. I know there's a way to remove the spaces before and after characters in a string, but I'm not sure about in between. You might want to put this one in its own directory so the pictures can easily be found. I might just remove the REN command and the temp.bmp code and put the picture in its own string and put it back on the screen after you read the Save screen and give it a name. Then it can go back on the screen and save it under just the name you want. I'll look into that, but for now, here is the working version.

If you want to see Sir Crow's version, you can see it on this post:
https://www.qb64.org/forum/index.php?topic=1684.msg109277#msg109277

Here is mine:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. start:
  3. c1 = 50: c2 = 10: c3 = 50
  4. s = RND * 10000
  5.     _LIMIT 500
  6.     clr = INT(RND * 300) + 1
  7.     IF clr = 1 THEN c1 = c1 + 25
  8.     IF c1 > 255 THEN c1 = 50
  9.     IF clr = 2 THEN c3 = c3 + 50
  10.     IF c3 > 255 THEN c3 = 0
  11.     IF clr > 298 THEN c3 = c1: c2 = c1
  12.     IF c3 > 255 THEN c3 = 5
  13.  
  14.     st = (RND * 5) + 1
  15.     dd = (RND * 300) - 300
  16.     dd2 = (RND * 300) - 300
  17.     IF dd2 < dd THEN st = -st
  18.     FOR d = dd TO dd2 STEP st
  19.         s = s + st
  20.         x = COS(s * 3.141592 / 180) * d
  21.         y = SIN(s * 3.151492 / 180) * d
  22.         PSET (x + 400, y + 300), _RGB32(c1, c2, c3)
  23.     NEXT d
  24.     IF c > 255 THEN c = 50
  25.     a$ = INKEY$
  26.     IF a$ = " " THEN GOTO start:
  27.     IF a$ = CHR$(27) THEN END
  28.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  29.     p = p + .02
  30.     pe = INT(p)
  31.     p$ = STR$(pe)
  32.     _TITLE "Percent Full: " + p$ + "   Press S to save as .bmp file anytime or Space Bar for new one."
  33.     IF p > 100 THEN p = 0: GOSUB more:
  34. more:
  35. _TITLE "Press Space Bar to make a new one or Press S to save as .bmp file."
  36. again:
  37. ag$ = INKEY$
  38. IF ag$ = " " THEN GOTO start:
  39. IF ag$ = "s" OR ag$ = "S" THEN GOTO saving:
  40. GOTO again:
  41. '---------------------------------------------------------------------------
  42. saving:
  43. 'Saving
  44. 'This section first saves your picture as temp.bmp and then
  45. 'asks you a name for your picture and then renames temp.bmp to your name.
  46. _TITLE "Saving Picture"
  47. 'Now we call up the SUB to save the image to BMP.
  48. SaveImage 0, "temp.bmp"
  49. _DELAY .25
  50. PRINT "                                  Saving"
  51. PRINT "                     Your bmp file will be saved in the"
  52. PRINT "                     same directory as this program is."
  53. PRINT "                     It can be used with almost any"
  54. PRINT "                     other graphics program or website."
  55. PRINT "                     It is saved using:"
  56. PRINT "                     width: 800  height: 600 pixels."
  57. PRINT "                     Type a name to save your picture"
  58. PRINT "                     and press the Enter key. Do not"
  59. PRINT "                     add .bmp at the end, the program"
  60. PRINT "                     will do it automatically."
  61. PRINT "                     Also do not add spaces to your name."
  62. PRINT "                     If you accidentally add a space, your"
  63. PRINT "                     file will be called temp.bmp. You must"
  64. PRINT "                     rename that in Windows or it will be lost"
  65. PRINT "                     next time you save a galaxy picture."
  66. PRINT "                     Also do not use the name temp"
  67. PRINT "                     because the program uses that name"
  68. PRINT "                     and it would be erased the next time"
  69. PRINT "                     you save a picture."
  70. PRINT "                     Example: MyPic"
  71. PRINT "                     Quit and Enter key ends program."
  72. INPUT "                     ->"; nm$
  73.  
  74.  
  75. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  76. nm$ = nm$ + ".bmp"
  77. 'Checking to see if the file already exists on your computer.
  78. theFileExists = _FILEEXISTS(nm$)
  79. IF theFileExists = -1 THEN
  80.     PRINT
  81.     PRINT "                File Already Exists"
  82.     PRINT "                Saving will delete your old"
  83.     PRINT "                bmp picture."
  84.     PRINT "                Would you like to still do it?"
  85.     PRINT "                (Y/N)."
  86.     PRINT "               Esc goes to start screen."
  87.     llloop:
  88.     _LIMIT 500
  89.     ag2$ = INKEY$
  90.     IF ag2$ = CHR$(27) THEN GOTO start:
  91.     IF ag2$ = "" THEN GOTO llloop:
  92.     IF ag2$ = "y" OR ag$ = "Y" THEN
  93.         SHELL _HIDE "DEL " + nm$
  94.         GOTO saving2:
  95.     END IF
  96.     GOTO llloop:
  97. saving2:
  98. SHELL _HIDE "REN " + "temp.bmp" + " " + nm$
  99. FOR snd = 100 TO 700 STEP 100
  100.     SOUND snd, 2
  101. NEXT snd
  102. nm$ = ""
  103. GOTO start:
  104.  
  105.  
  106. 'This section saves the .bmp picture file.
  107. SUB SaveImage (image AS LONG, filename AS STRING)
  108.     bytesperpixel& = _PIXELSIZE(image&)
  109.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  110.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  111.     x& = _WIDTH(image&)
  112.     y& = _HEIGHT(image&)
  113.     b$ = "BM????QB64????" + MKL$(40) + MKL$(x&) + MKL$(y&) + MKI$(1) + MKI$(bpp&) + MKL$(0) + "????" + STRING$(16, 0) 'partial BMP header info(???? to be filled later)
  114.     IF bytesperpixel& = 1 THEN
  115.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  116.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  117.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  118.         NEXT
  119.     END IF
  120.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  121.     lastsource& = _SOURCE
  122.     _SOURCE image&
  123.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  124.     FOR py& = y& - 1 TO 0 STEP -1 ' read image pixel color data
  125.         r$ = ""
  126.         FOR px& = 0 TO x& - 1
  127.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  128.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  129.         NEXT px&
  130.         d$ = d$ + r$ + padder$
  131.     NEXT py&
  132.     _SOURCE lastsource&
  133.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  134.     b$ = b$ + d$ ' total file data bytes to create file
  135.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  136.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  137.     f& = FREEFILE
  138.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  139.     OPEN filename$ + ext$ FOR BINARY AS #f&
  140.     PUT #f&, , b$
  141.     CLOSE #f&
  142.  

Offline Qbee

  • Newbie
  • Posts: 27
    • View Profile
Re: A Different Galaxy Maker
« Reply #1 on: December 15, 2019, 10:50:34 pm »
Works fine, @SierraKen !
With GIMP I can set the backgroundcolor to transparent and stretch the image as needed.
Thanks for sharing.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A Different Galaxy Maker
« Reply #2 on: December 15, 2019, 11:05:25 pm »
Quote
I tried to change the nm$ to delete all the spaces between the characters but I couldn't figure it out.

Here is a function that will strip outside spaces from a string and replace all inner spaces with an "_" which is what might be better for filenames that can't have spaces.
Code: QB64: [Select]
  1.     INPUT "Enter a test string with spaces (just enter to quit) "; test$
  2.     PRINT "fillSpace changed '"; test$; "' to '"; fillSpace$(test$); "'."
  3.     PRINT
  4. LOOP UNTIL test$ = ""
  5.  
  6. FUNCTION fillSpace$ (s$)
  7.     test$ = _TRIM$(s$)
  8.     IF test$ = "" THEN
  9.         fillSpace$ = "_"
  10.     ELSE
  11.         FOR i = 1 TO LEN(test$)
  12.             IF MID$(test$, i, 1) = " " THEN b$ = b$ + "_" ELSE b$ = b$ + MID$(test$, i, 1)
  13.         NEXT
  14.         fillSpace$ = b$
  15.     END IF
  16.  

Marked as best answer by SierraKen on December 15, 2019, 06:11:10 pm

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Different Galaxy Maker
« Reply #3 on: December 15, 2019, 11:08:52 pm »
Thanks QBee... well I just made an even better one...
I actually figured out how to save a .bmp picture without first making a temp.bmp! And just like I said, I used the _PUTIMAGE command to place the picture back to the screen after the Save info screen. Then it saves the screen as the .bmp in your choice of names (EVEN with spaces!) and without having to save any temp file. I've been wanting to do this for months. I know you guys told me how to do it a long time ago, but I never really pushed myself until tonight.

Code: QB64: [Select]
  1. start:
  2. galaxy& = _NEWIMAGE(800, 600, 32)
  3. SCREEN galaxy&
  4. _DEST galaxy&
  5. _SOURCE galaxy&
  6. c1 = 50: c2 = 10: c3 = 50
  7. s = RND * 10000
  8.     _LIMIT 500
  9.     clr = INT(RND * 300) + 1
  10.     IF clr = 1 THEN c1 = c1 + 25
  11.     IF c1 > 255 THEN c1 = 50
  12.     IF clr = 2 THEN c3 = c3 + 50
  13.     IF c3 > 255 THEN c3 = 0
  14.     IF clr > 298 THEN c3 = c1: c2 = c1
  15.     IF c3 > 255 THEN c3 = 5
  16.  
  17.     st = (RND * 5) + 1
  18.     dd = (RND * 300) - 300
  19.     dd2 = (RND * 300) - 300
  20.     IF dd2 < dd THEN st = -st
  21.     FOR d = dd TO dd2 STEP st
  22.         s = s + st
  23.         x = COS(s * 3.141592 / 180) * d
  24.         y = SIN(s * 3.151492 / 180) * d
  25.         PSET (x + 400, y + 300), _RGB32(c1, c2, c3)
  26.     NEXT d
  27.     IF c > 255 THEN c = 50
  28.     a$ = INKEY$
  29.     IF a$ = " " THEN GOTO start:
  30.     IF a$ = CHR$(27) THEN END
  31.     IF a$ = "s" OR a$ = "S" THEN GOTO saving:
  32.     p = p + .02
  33.     pe = INT(p)
  34.     p$ = STR$(pe)
  35.     _TITLE "Percent Full: " + p$ + "   Press S to save as .bmp file anytime or Space Bar for new one."
  36.     IF p > 100 THEN p = 0: GOSUB more:
  37. more:
  38. _TITLE "Press Space Bar to make a new one or Press S to save as .bmp file."
  39. again:
  40. ag$ = INKEY$
  41. IF ag$ = " " THEN GOTO start:
  42. IF ag$ = "s" OR ag$ = "S" THEN GOTO saving:
  43. GOTO again:
  44. '---------------------------------------------------------------------------
  45. saving:
  46. galaxy2& = _COPYIMAGE(galaxy&)
  47.  
  48. 'Saving
  49. _TITLE "Saving Picture"
  50. _DELAY .25
  51. PRINT "                                  Saving"
  52. PRINT "                     Your bmp file will be saved in the"
  53. PRINT "                     same directory as this program is."
  54. PRINT "                     It can be used with almost any"
  55. PRINT "                     other graphics program or website."
  56. PRINT "                     It is saved using:"
  57. PRINT "                     width: 800  height: 600 pixels."
  58. PRINT "                     Type a name to save your picture"
  59. PRINT "                     and press the Enter key. Do not"
  60. PRINT "                     add .bmp at the end, the program"
  61. PRINT "                     will do it automatically."
  62. PRINT "                     Example: MyPic"
  63. PRINT "                     Quit and Enter key ends program."
  64. INPUT "                     ->"; nm$
  65. IF nm$ = "Quit" OR nm$ = "quit" OR nm$ = "QUIT" THEN END
  66. nm$ = nm$ + ".bmp"
  67. 'Checking to see if the file already exists on your computer.
  68. theFileExists = _FILEEXISTS(nm$)
  69. IF theFileExists = -1 THEN
  70.     PRINT
  71.     PRINT "                File Already Exists"
  72.     PRINT "                Saving will delete your old"
  73.     PRINT "                bmp picture."
  74.     PRINT "                Would you like to still do it?"
  75.     PRINT "                (Y/N)."
  76.     PRINT "               Esc goes to start screen."
  77.     llloop:
  78.     _LIMIT 500
  79.     ag2$ = INKEY$
  80.     IF ag2$ = CHR$(27) THEN GOTO start:
  81.     IF ag2$ = "" THEN GOTO llloop:
  82.     IF ag2$ = "y" OR ag$ = "Y" THEN
  83.         SHELL _HIDE "DEL " + nm$
  84.         GOTO saving2:
  85.     END IF
  86.     GOTO llloop:
  87. saving2:
  88. SCREEN galaxy2&
  89. SaveImage 0, nm$
  90. FOR snd = 100 TO 800 STEP 50
  91.     SOUND snd, 1
  92. NEXT snd
  93. nm$ = ""
  94. galaxy& = 0
  95. galaxy2& = 0
  96. GOTO start:
  97.  
  98.  
  99. 'This section saves the .bmp picture file.
  100. SUB SaveImage (image AS LONG, filename AS STRING)
  101.     bytesperpixel& = _PIXELSIZE(image&)
  102.     IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END
  103.     IF bytesperpixel& = 1 THEN bpp& = 8 ELSE bpp& = 24
  104.     x& = _WIDTH(image&)
  105.     y& = _HEIGHT(image&)
  106.     b$ = "BM????QB64????" + MKL$(40) + MKL$(x&) + MKL$(y&) + MKI$(1) + MKI$(bpp&) + MKL$(0) + "????" + STRING$(16, 0) 'partial BMP header info(???? to be filled later)
  107.     IF bytesperpixel& = 1 THEN
  108.         FOR c& = 0 TO 255 ' read BGR color settings from JPG image + 1 byte spacer(CHR$(0))
  109.             cv& = _PALETTECOLOR(c&, image&) ' color attribute to read.
  110.             b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte
  111.         NEXT
  112.     END IF
  113.     MID$(b$, 11, 4) = MKL$(LEN(b$)) ' image pixel data offset(BMP header)
  114.     lastsource& = _SOURCE
  115.     _SOURCE image&
  116.     IF ((x& * 3) MOD 4) THEN padder$ = STRING$(4 - ((x& * 3) MOD 4), 0)
  117.     FOR py& = y& - 1 TO 0 STEP -1 ' read image pixel color data
  118.         r$ = ""
  119.         FOR px& = 0 TO x& - 1
  120.             c& = POINT(px&, py&) 'POINT 32 bit values are large LONG values
  121.             IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3)
  122.         NEXT px&
  123.         d$ = d$ + r$ + padder$
  124.     NEXT py&
  125.     _SOURCE lastsource&
  126.     MID$(b$, 35, 4) = MKL$(LEN(d$)) ' image size(BMP header)
  127.     b$ = b$ + d$ ' total file data bytes to create file
  128.     MID$(b$, 3, 4) = MKL$(LEN(b$)) ' size of data file(BMP header)
  129.     IF LCASE$(RIGHT$(filename$, 4)) <> ".bmp" THEN ext$ = ".bmp"
  130.     f& = FREEFILE
  131.     OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file
  132.     OPEN filename$ + ext$ FOR BINARY AS #f&
  133.     PUT #f&, , b$
  134.     CLOSE #f&
  135.  

« Last Edit: December 15, 2019, 11:16:23 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Different Galaxy Maker
« Reply #4 on: December 15, 2019, 11:10:53 pm »
Thanks B+, but it wasn't needed this time. I'll keep your code though in case I need it down the way. You can read why I didn't need it anymore above.