Posts: 476
Threads: 25
Joined: Nov 2022
Reputation:
45
Code: (Select All)
SCREEN _NEWIMAGE(320, 200, 256)
_FULLSCREEN _SQUAREPIXELS
COLOR ,1
CLS
m = 1
d = 0
lim = 120
st = 1
c = 1
x = 0
y = 10
w = 50
h = 50
FGC$ = "12" : BGC$="8"
T$ = "C"+FGC$+" R10 E5 R20 F5 R10 BL50"
M$ = "D20 BR50 BU20 D20 BL50"
B$ = "D10 E10 U10 R30 D10 F10 U10 BL50"
F$ = "BU20 BF5 P "+BGC$+","+FGC$
ALIEN$ = T$+M$+B$+F$
ALIEN_SPRITE_CLEAR& = _NEWIMAGE(w+1, h+1, 256)
ALIEN_SPRITE& = _NEWIMAGE(w+1, h+1, 256)
' ALIEN_HW_CLEAR& = _NEWIMAGE(w+1, h+1, 32)
' ALIEN_HW& = _NEWIMAGE(w+1, h+1, 32)
_DEST ALIEN_SPRITE&
' _PALETTECOLOR 0, _RGB32(0, 0, 0)
' _PALETTECOLOR 1, _RGB32(0, 0, 64)
' _PALETTECOLOR 12, _RGB32(255, 170, 170)
' _PALETTECOLOR 8, _RGB32(170, 0, 0)
_CLEARCOLOR 0
PSET (0,10)
DRAW ALIEN$
_DEST ALIEN_SPRITE_CLEAR&
COLOR ,1
CLS
' ALIEN_HW& = _COPYIMAGE(ALIEN_SPRITE&, 33)
' ALIEN_HW_CLEAR& = _COPYIMAGE(ALIEN_SPRITE_CLEAR&, 33)
' _FREEIMAGE ALIEN_SPRITE&
' _FREEIMAGE ALIEN_SPRITE_CLEAR&
_DEST 0
' _DISPLAYORDER _HARDWARE
DO
FOR x = 0 TO _WIDTH-w STEP st
IF lim > 0 THEN _LIMIT lim
_PUTIMAGE (x, y)-(x+w, y+h), ALIEN_SPRITE&, 0, (0,0)-(w,h)
_DISPLAY
_DELAY d
_PUTIMAGE (x, y)-(x+w, y+h), ALIEN_SPRITE_CLEAR&, 0, (0,0)-(w,h)
' DRAW ALIEN$
' LINE (x, y-y)-(x + w, y + h), c, BF
' _DELAY 1
NEXT x
FOR x = _WIDTH-w TO 0 STEP -st
IF lim > 0 THEN _LIMIT lim
_PUTIMAGE (x, y)-(x+w, y+h), ALIEN_SPRITE&, 0, (0,0)-(w,h)
_DISPLAY
_DELAY d
_PUTIMAGE (x, y)-(x+w, y+h), ALIEN_SPRITE_CLEAR&, 0, (0,0)-(w,h)
' DRAW ALIEN$
' LINE (x, y-y)-(x + w, y + h), c, BF
' _DELAY 1
NEXT x
LOOP UNTIL _KEYHIT
I am trying to get this to work with hardware images. I can't seem to get it.
Could someone give me a hand?
Thanks
Posts: 3,965
Threads: 176
Joined: Apr 2022
Reputation:
219
I never worked with Hardware images the Wiki for _copyimage didn't show an example in my quick scan.
I wonder if Terry's tutorial might have something?
b = b + ...
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
Posts: 98
Threads: 1
Joined: Jun 2023
Reputation:
3
grymmjack, It looks like Space Invaders for old fogies over 20 years of age. Joking aside it looks great. Keep it up.
Posts: 476
Threads: 25
Joined: Nov 2022
Reputation:
45
(09-12-2023, 03:09 AM)GareBear Wrote: grymmjack, It looks like Space Invaders for old fogies over 20 years of age. Joking aside it looks great. Keep it up.
Thanks
Posts: 476
Threads: 25
Joined: Nov 2022
Reputation:
45
09-12-2023, 10:21 AM
(This post was last modified: 09-12-2023, 11:03 AM by grymmjack.
Edit Reason: updated a bit
)
OK I have the hardware images working I think, but I can't figure out how to make the background blue.
Code: (Select All)
OPTION _EXPLICIT
DIM AS INTEGER lim, st, x, y, w, h
DIM AS SINGLE d
DIM AS STRING FGC, BGC, T, M, B, F, ALIEN
DIM AS LONG ALIEN_SPRITE, ALIEN_32, ALIEN_HW, CANVAS, CANVAS_HW
CANVAS& = _NEWIMAGE(320, 200, 32)
SCREEN CANVAS&
CLS , _RGB32(0, 0, 255)
_FULLSCREEN _SQUAREPIXELS
d! = 0
lim% = 120
st% = 1
x% = 0
y% = 10
w% = 50
h% = 50
FGC$ = "12" : BGC$="8"
T$ = "C" + FGC$ + " R10 E5 R20 F5 R10 BL50"
M$ = "D20 BR50 BU20 D20 BL50"
B$ = "D10 E10 U10 R30 D10 F10 U10 BL50"
F$ = "BU20 BF5 P " + BGC$ + "," + FGC$
ALIEN$ = T$ + M$ + B$ + F$
ALIEN_SPRITE& = _NEWIMAGE(w% + 1, h% + 1, 256)
_DEST ALIEN_SPRITE&
PSET (0, 10)
DRAW ALIEN$
ALIEN_32& = _NEWIMAGE(w% + 1, h% + 1, 32)
_SOURCE ALIEN_SPRITE& : _DEST ALIEN_32& : _PUTIMAGE
ALIEN_HW& = _COPYIMAGE(ALIEN_32&, 33)
_DISPLAYORDER _HARDWARE
_DEST CANVAS&
DO
FOR x% = 0 TO _WIDTH - w% + 1 STEP st%
IF lim% > 0 THEN _LIMIT lim%
_PUTIMAGE (x%, y%)-(x% + w%, y% + h%), ALIEN_HW&, 0, (0,0)-(w%, h%)
CANVAS_HW& = _COPYIMAGE(CANVAS&, 33)
_DISPLAY
IF d! > 0 THEN _DELAY d!
NEXT x%
FOR x% = _WIDTH - w% + 1 TO 0 STEP -st%
IF lim% > 0 THEN _LIMIT lim%
_PUTIMAGE (x%, y%)-(x% + w%, y% + h%), ALIEN_HW&, 0, (0,0)-(w%, h%)
CANVAS_HW& = _COPYIMAGE(CANVAS&, 33)
_DISPLAY
IF d! > 0 THEN _DELAY d!
NEXT
LOOP UNTIL _KEYHIT=27
_FREEIMAGE ALIEN_SPRITE&
_FREEIMAGE ALIEN_HW&
SYSTEM
Posts: 372
Threads: 23
Joined: May 2022
Reputation:
56
Made a few changes.
Code: (Select All)
OPTION _EXPLICIT
DIM AS INTEGER lim, st, x, y, w, h
DIM AS SINGLE d
DIM AS STRING FGC, BGC, T, M, B, F, ALIEN
DIM AS LONG ALIEN_SPRITE, ALIEN_32, ALIEN_HW, CANVAS
CANVAS& = _NEWIMAGE(320, 200, 32)
SCREEN CANVAS&
_FULLSCREEN _SQUAREPIXELS
d! = 0
lim% = 120
st% = 1
x% = 0
y% = 10
w% = 50
h% = 50
FGC$ = "12": BGC$ = "8"
T$ = "C" + FGC$ + " R10 E5 R20 F5 R10 BL50"
M$ = "D20 BR50 BU20 D20 BL50"
B$ = "D10 E10 U10 R30 D10 F10 U10 BL50"
F$ = "BU20 BF5 P " + BGC$ + "," + FGC$
ALIEN$ = T$ + M$ + B$ + F$
ALIEN_SPRITE& = _NEWIMAGE(w% + 1, h% + 1, 256)
_DEST ALIEN_SPRITE&
PSET (0, 10)
DRAW ALIEN$
_CLEARCOLOR 0, ALIEN_SPRITE&
ALIEN_32& = _NEWIMAGE(w% + 1, h% + 1, 32)
_SOURCE ALIEN_SPRITE&: _DEST ALIEN_32&: _PUTIMAGE
ALIEN_HW& = _COPYIMAGE(ALIEN_32&, 33)
_DEST CANVAS&
CLS , _RGB32(0, 0, 255)
DO
FOR x% = 0 TO _WIDTH - w% + 1 STEP st%
IF lim% > 0 THEN _LIMIT lim%
_PUTIMAGE (x%, y%), ALIEN_HW&
_DISPLAY
IF d! > 0 THEN _DELAY d!
NEXT x%
FOR x% = _WIDTH - w% + 1 TO 0 STEP -st%
IF lim% > 0 THEN _LIMIT lim%
_PUTIMAGE (x%, y%), ALIEN_HW&
_DISPLAY
IF d! > 0 THEN _DELAY d!
NEXT
LOOP UNTIL _KEYHIT = 27
_FREEIMAGE ALIEN_SPRITE&
_FREEIMAGE ALIEN_HW&
SYSTEM
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
(09-12-2023, 01:45 AM)bplus Wrote: I never worked with Hardware images the Wiki for _copyimage didn't show an example in my quick scan.
I wonder if Terry's tutorial might have something?
Sorry, it doesn't. Something I need to add in the near future.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
09-12-2023, 07:11 PM
(This post was last modified: 09-12-2023, 07:34 PM by TerryRitchie.)
It's well worth learning to use hardware images. Here is a little demo I just wrote showing the power of hardware images. This program creates 8000 sprites and then let's the user switch between software and hardware mode to see the performance difference. I'm going to start creating demos for a lesson on hardware vs software images in the tutorial. It's long overdue.
Keys:
H - hardware mode
S - software mode
ESC - leave program
Note: You may need to decrease the amount of objects depending on your system's performance. Change the TOTAL = 8000 to a lower number that doesn't crash the program.
Code: (Select All) OPTION _EXPLICIT ' declare those variables son!
CONST TOTAL = 8000 ' too many circles will crash program depending on amount of RAM and VRAM
CONST SWIDTH = 1280 ' screen width
CONST SHEIGHT = 720 ' screen height
TYPE SPRITEXY ' sprite location and vector
x AS SINGLE ' horizontal coordinate
y AS SINGLE ' vertical coordinate
xdir AS SINGLE ' x vector
ydir AS SINGLE ' y vector
END TYPE
DIM SpriteSW(TOTAL) AS LONG ' software sprites
DIM SpriteHW(TOTAL) AS LONG ' hardware sprites
DIM SpriteXY(TOTAL) AS SPRITEXY ' sprite locations and vecotrs
DIM HWSprite AS LONG ' original sprite used for copying ('HARD' printed inside)
DIM SWSprite AS LONG ' original sprite used for copying ('SOFT' printed inside)
DIM Count AS INTEGER ' sprite counter
DIM Software AS INTEGER ' -1 in software mode, 0 in hardware mode
DIM a AS STRING ' key pressed by user
DIM t1 AS DOUBLE ' timer at start of frame
DIM t2 AS DOUBLE ' timer at end of frame
DIM t3 AS DOUBLE ' total time taken to draw sprites
RANDOMIZE TIMER ' seed random number generator
SCREEN _NEWIMAGE(SWIDTH, SHEIGHT, 32) ' create screen canvas
Software = -1 ' start in software mode
HWSprite = _NEWIMAGE(50, 50, 32) ' temp hardware sprite image
SWSprite = _NEWIMAGE(50, 50, 32) ' temp software sprite image (notice 32 - still a software sprite at this point)
_DEST HWSprite ' draw on temp hardware sprite image
LOCATE 2, 2: PRINT "HARD"; ' print inside circle
CIRCLE (24, 24), 24 ' draw circle
_DEST SWSprite ' draw on temp software sprite image
LOCATE 2, 2: PRINT "SOFT"; ' print inside circle
CIRCLE (24, 24), 24 ' draw circle
_DEST 0 ' draw on main screen canvas
FOR Count = 1 TO TOTAL ' cycle through sprites
SpriteSW(Count) = _COPYIMAGE(SWSprite, 32) ' copy the software sprite
SpriteHW(Count) = _COPYIMAGE(HWSprite, 33) ' copy the software sprite to a hardware sprite (notice 33)
SpriteXY(Count).x = SWIDTH / 2 - 24 ' x coordinate start (centered)
SpriteXY(Count).y = SHEIGHT / 2 - 24 ' y coordinate start (centered)
SpriteXY(Count).xdir = RND - RND ' random x vector from -1 to 1
SpriteXY(Count).ydir = RND - RND ' random y vector from -1 to 1
NEXT Count
_FREEIMAGE SWSprite ' temp sprites no longer need
_FREEIMAGE HWSprite
DO ' begin frame loop
t1 = TIMER(.001) ' get frame start time
Count = 0 ' reset sprite counter
IF Software THEN CLS ' software screens need clearing, hardware don't
DO ' begin sprite draw loop
Count = Count + 1 ' increment sprite counter
IF Software THEN ' software mode?
_PUTIMAGE (SpriteXY(Count).x, SpriteXY(Count).y), SpriteSW(Count) ' yes, draw software sprite
ELSE ' no
_PUTIMAGE (SpriteXY(Count).x, SpriteXY(Count).y), SpriteHW(Count) ' draw hardware sprite
END IF
SpriteXY(Count).x = SpriteXY(Count).x + SpriteXY(Count).xdir ' move sprite by set vectors
SpriteXY(Count).y = SpriteXY(Count).y + SpriteXY(Count).ydir
IF SpriteXY(Count).x > SWIDTH - 50 OR SpriteXY(Count).x < 1 THEN SpriteXY(Count).xdir = -SpriteXY(Count).xdir ' keep sprite on screen
IF SpriteXY(Count).y > SHEIGHT - 50 OR SpriteXY(Count).y < 32 THEN SpriteXY(Count).ydir = -SpriteXY(Count).ydir
LOOP UNTIL Count = TOTAL ' leave when all sprites drawn
t2 = TIMER(.001) ' get frame end time
t3 = t2 - t1 ' calculate total time
IF t3 > 0 THEN ' avoid /0
LOCATE 2, 28 ' position cursor
PRINT USING "#### FPS "; 1 / t3; ' print frame count
IF Software THEN ' software mode?
PRINT "software mode "; ' yes, print indicator
ELSE ' no
PRINT "hardware mode "; ' print indictaor
END IF
END IF
LOCATE 1, 2 ' print instructions
PRINT "Controlling"; TOTAL; "sprites. Press 'H' for hardware or 'S' for software - ESC to quit"
_DISPLAY ' update screen with changes
a = UCASE$(INKEY$) ' get user keypress (if any)
IF a = "S" THEN Software = -1 ' if user pressed S key set software mode
IF a = "H" THEN Software = 0: CLS ' if user pressed H set hardware mode and clear software screen
LOOP UNTIL a = CHR$(27) ' leave when user presses ESC key
FOR Count = 1 TO TOTAL ' cycle through sprites
_FREEIMAGE SpriteSW(Count) ' remove sprites from RAM
_FREEIMAGE SpriteHW(Count)
NEXT Count
SYSTEM ' return to operating system
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
^
|
Well done!
But I purposely reduced the object count from 8000 to 4000 because with 4000 the screen was getting a bit crowded which looked gross.
I got 19FPS from "software" mode and usually 1000FPS from "hardware" mode. This was on a HP laptop which originally had Windows8 on it, with Intel Sandy Bridge CPU, MESA graphics and on Slackel Linux (based on Slackware).
Code: (Select All) [~]$ inxi -G
Graphics:
Device-1: Intel 2nd Generation Core Processor Family Integrated Graphics
driver: i915 v: kernel
Device-2: Chicony HP Truevision HD driver: uvcvideo type: USB
Display: x11 server: X.Org v: 21.1.8 driver: X: loaded: intel dri: crocus
gpu: i915 resolution: 1366x768~60Hz
API: OpenGL v: 3.3 Mesa 23.1.6 renderer: Mesa Intel HD Graphics 2000 (SNB
GT1)
|