Author Topic: Newbie question about 8-bit screen in new QB64  (Read 4331 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 venex

  • Newbie
  • Posts: 7
    • View Profile
Newbie question about 8-bit screen in new QB64
« on: January 20, 2021, 04:50:38 pm »
I have a simple program, which works fine with QB64 SDL in Windows 10:
Code: QB64: [Select]
  1. i = _LOADIMAGE("sample.png")
  2.  

The sample.png is an 8-bit 320x200 image.

The code compiles with the latest QB64 without errors, but when I try to run the executable I'm getting the following runtime error:
Quote
Line: 3 (in main module)
Illegal function call
Continue?

The same thing happens when the first line of the program is
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(320, 200, 256)
  2.  

but when I change it to
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(320, 200, 32)
  2.  

it works and displays the image.

Problem is that many tutorials, examples and games I found online use SCREEN 13 and other 8-bit modes. How can I run them with the latest QB64? Many times simply changing to 32-bit mode does not help, because a program manipulates the color palette with _COPYPALETTE, or uses LOCATE to display text, which also seems not to work in 32-bit screen modes.
« Last Edit: January 21, 2021, 01:00:00 pm by venex »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Newbie question about 8-bit screen in new QB64
« Reply #1 on: January 20, 2021, 05:44:46 pm »
Are you looking for modern interpretations? QB64 can run SCREEN 13.

If so some small sample?

Maybe this?
http://www.qb64.org/wiki/A_Small_Game_Tutorial
« Last Edit: January 20, 2021, 05:51:47 pm by bplus »

Offline venex

  • Newbie
  • Posts: 7
    • View Profile
Re: Newbie question about 8-bit screen in new QB64
« Reply #2 on: January 20, 2021, 06:04:43 pm »
Thanks for the tutorial link. However, the problem I have is not with drawing, but with loading images. One example of a game which has this problem is Barbarian 1987 from http://barbarian.1987.free.fr/download/download.htm written in QB64 SDL. You can compile it with QB64 v1.4, but it will crash at runtime, because it has problems loading 8-bit image assets. This does not happen when the game is compiled with QB64 SDL. I would like to know if it’s a bug with _PUTIMAGE or maybe something else.
« Last Edit: January 20, 2021, 06:29:13 pm by venex »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Newbie question about 8-bit screen in new QB64
« Reply #3 on: January 20, 2021, 06:36:03 pm »
Hi @venex.  Here's a thread that may shed some light on what's happening:
https://www.qb64.org/forum/index.php?topic=1252.0

- Dav

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Newbie question about 8-bit screen in new QB64
« Reply #4 on: January 20, 2021, 07:34:39 pm »
edit
« Last Edit: January 20, 2021, 07:39:57 pm by bplus »

FellippeHeitor

  • Guest
Re: Newbie question about 8-bit screen in new QB64
« Reply #5 on: January 20, 2021, 08:20:59 pm »
8bit mode image loading has been disabled for a while now. You should always check that loaded image handles are <= -1 first (-1 signals the image failed to load):

Code: QB64: [Select]
  1. i = _LOADIMAGE("sample.png")
  2. IF i <= -1 THEN _PUTIMAGE , i ELSE PRINT "Load failed."

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Newbie question about 8-bit screen in new QB64
« Reply #6 on: January 20, 2021, 09:03:03 pm »
_LOADIMAGE in the GL version (from 1.0 onwards), auto-converts images to 32-bits.  If you want to use it with 256 color screens (8-bit), then you’ll need to first load them as 32-bits, and then convert/dither them back down to 256 colors.  RhoSigma’s libraries can handle the task, as can routines in my SaveImage libraries, along with examples by Petr, here on the forum, along with several others that I’m probably just not thinking of at the moment.

Here’s a link to one such discussion for you: https://www.qb64.org/forum/index.php?topic=2701.msg119104#msg119104
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Marked as best answer by venex on January 21, 2021, 08:08:07 am

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Newbie question about 8-bit screen in new QB64
« Reply #7 on: January 21, 2021, 10:24:06 am »
Hi. This is function for DIRECT inserting 8 bit image to 8 bit screen. This screen palette is always changed so, as it is used in image!

Code: QB64: [Select]
  1. 'LOADIMAGE256 experimental
  2.  
  3. '1] Load image as 32 bit image
  4. '2] find how much colors image contains. If 256 and less, continue. If more than 256, return -1 as error message. (can be solved with  RhoSigma's ditherring which is a part of SaveImage)
  5. '3] create 8 bit image and color palette
  6.  
  7. '4] BIG ASK IS: If is this image inserted to 8 bit screen, copy image palette to this screen? OR try to do the same palette for both images?
  8.  
  9.  
  10. SCREEN _NEWIMAGE(1700, 800, 256)
  11. img8 = LOADIMAGE256("anim1.png")
  12. _PUTIMAGE (0, 0), img8
  13.  
  14.  
  15.  
  16.  
  17. FUNCTION LOADIMAGE256 (img$)
  18.     DEFLNG A-Z
  19.     image = _LOADIMAGE(img$, 32)
  20.     DIM m AS _MEM, clr8(255) AS _UNSIGNED LONG, Clr32 AS _UNSIGNED LONG, test AS LONG, s AS LONG
  21.     FOR s = 0 TO 255
  22.         clr8(s) = 99999
  23.     NEXT s
  24.     m = _MEMIMAGE(image)
  25.     DO UNTIL p& = m.SIZE
  26.         _MEMGET m, m.OFFSET + p&, Clr32~&
  27.         test = 0
  28.         'this block prevent for writing the same color more than 1x to palette array
  29.         DO UNTIL test = 255
  30.             IF clr8(test) = Clr32~& THEN GOTO NextColor
  31.             IF clr8(test) = 99999 THEN EXIT DO
  32.             test = test + 1
  33.         LOOP
  34.         'if is empty place in palette, save this color as next palette color
  35.         IF test > 255 THEN PRINT "Image contains more than 256 colors, can not be directly copyed as 8 bit image": END
  36.         clr8(test) = Clr32
  37.         'color is saved as palette for 8 bit image
  38.         NextColor: p& = p& + 4
  39.     LOOP
  40.     image8 = _NEWIMAGE(_WIDTH(image), _HEIGHT(image), 256)
  41.     'set palette
  42.     N = _MEMIMAGE(image8)
  43.     FOR palett = 0 TO 255
  44.         _PALETTECOLOR palett, clr8(palett), image8
  45.     NEXT
  46.     'create 8 bit mask (set colors 0 to 255 to 8 bit image)
  47.     FOR C = 255 TO 0 STEP -1
  48.         clr~& = clr8(C)
  49.         R& = 0
  50.         R8& = 0
  51.         DO UNTIL R& = m.SIZE
  52.             _MEMGET m, m.OFFSET + R&, Clr32
  53.             IF Clr32 = clr~& THEN _MEMPUT N, N.OFFSET + R8&, C
  54.             R& = R& + 4
  55.             R8& = R8& + 1
  56.         LOOP
  57.     NEXT C
  58.     LOADIMAGE256 = _COPYIMAGE(image8, 256)
  59.     _MEMFREE m
  60.     _MEMFREE N
  61.     _FREEIMAGE image
  62.     _FREEIMAGE image8
  63.  

Of course, you can work with multiple 8-bit images with different palettes at the same time. You just place them on a 32-bit screen. You can make changes using 8-bit colors on 8-bit images in the background. To do this, you need knowledge of the _SOURCE, _DEST and _PALETTECOLOR statements.

Offline venex

  • Newbie
  • Posts: 7
    • View Profile
Re: Newbie question about 8-bit screen in new QB64
« Reply #8 on: January 21, 2021, 12:58:55 pm »
Thank you very much guys for your help!

I read the materials you linked and also had a quick look at the QB64 source code of internal/c/parts/video/image/src.c and I think I understand a little bit better now how it works.

@Petr your code rocks! I changed my initial program from:
Code: QB64: [Select]
  1. i = _LOADIMAGE("sample.png")
  2.  

into:
Code: QB64: [Select]
  1. i = LOADIMAGE256("sample.png")
  2.  

and now everything works like a charm :) Thank you!
« Last Edit: January 21, 2021, 01:46:18 pm by venex »