Text Rotation Fun.bas

Rotate text about a point or about the x axis or y axis at a point (rotating colors too):

'text rotation fun.bas for SmallBASIC 0.12.11 (B+=MGA) 2017-02-02

'global variables
message = "abcdefghijklmnopqrstuvwxyz""
secWide = txtw(message) + 2
secHigh = txth(message) + 2
dim sect(secWide, secHigh) 'array to store message points
color rgb(200, 200, 200),0 : cls
rect 0, 0, secWide, secHigh, 9

? message
loadSect 0, 0 'load array
'debug checks
'for y = 0 to secHigh
' for x = 0 to secWide
' if sect(x, y) then pset x + 200, y + 200
' next
'next
'input "OK ";ok

dim plasma(5, 3)
for
i = 0 to 5
plasma(i, 0) = rnd * rnd
plasma(i, 1) = rnd * rnd
plasma(i, 2) = rnd * rnd
next

cls
cx = xmax/2 : cy = ymax/2
while 1
cls
dp 0
yaxis cx, cy/4, a, 2
dp 1
xaxis cx, 3*cy/4, a, 5
dp 2
rotate cx/2, cy, a, 1
dp 3
rotate 3*cx/2, cy, a -90, 2.5
dp 4
rotate cx, cy/2, -4*a + 90, 2
dp 5
rotate cx, 3*cy/2, 2*a + 180, 4.5
showpage
delay 10
a = a + 1
if a = 360 then a = 0
wend
pause

sub
dp(i)
color rgb(128 + 127*sin(plasma(i,0)*a), 128 + 127*sin(plasma(i, 1)*a), 128 + 127*sin(plasma(i, 2)*a))
end


sub
loadSect(xstart, ystart)
local x, y, p, black
'these are all global
black = rgb(100 ,100, 100)
for y = 0 to secHigh
for x = 0 to secWide
p = POINT(xstart + x, ystart + y)
if p < black then sect(x, y) = 1 '<== data from screen points
next
next
end

sub
rotate(cx, cy, angle, scale) 'and scale
local cax, cay, ra, cc, d, anew, ax,ay

cax = secWide/2 : cay = secHigh/2 'array center
for y = 0 to secHigh
for x = 0 to secWide
cc = sect(x,y)
if (x-cax) <> 0 and cc <> 0 then
d = ((x-cax)^2+(y-cay)^2)^.5
anew = atan((y-cay)/(x-cax))
if x-cax < 0 and y-cay < 0 then anew = anew + pi+rad(angle) '-x,-y
if x-cax < 0 and y-cay >= 0 then anew = anew + pi+rad(angle) '-x,+y
if x-cax >= 0 and y-cay < 0 then anew = anew + rad(angle) '+x,-y
if x-cax >= 0 and y-cay >= 0 then anew = anew + rad(angle) '+x,+y
ax = d*cos(anew):ay=d*sin(anew)
rect int(cx+ax*scale),int(cy+ay*scale) step scale+1, scale+1 filled
end if
next
next
end

sub
yaxis(cx, cy, angle, scale)
local cax, cay, cc, ax,ay

cax = secWide/2 : cay = secHigh/2 'array center
for y = 0 to secHigh
for x = 0 to secWide
cc = sect(x,y)
if cc <> 0 then
ax = (x - cax)*cos(rad(angle)):ay= (y - cay)
rect int(cx+ax*scale),int(cy+ay*scale) step scale+1, scale+1 filled
end if
next
next
end

sub
xaxis(cx, cy, angle, scale)
local cax, cay, cc, ax,ay

cax = secWide/2 : cay = secHigh/2 'array center
for y = 0 to secHigh
for x = 0 to secWide
cc = sect(x,y)
if cc <> 0 then
ax = (x - cax) :ay = (y - cay) * sin( rad(angle))
rect int(cx+ax*scale),int(cy+ay*scale) step scale+1, scale+1 filled
end if
next
next
end


Yeah, today I realized, "Why stop at text?", can do this with any image or part of one!

So next up is to generalize, LoadSec to return a loaded array and then render by array rotations or new scales... anywhere around any point.

This may be key to finally drawing on top of SmallBASIC images the biggest hindrance to doing game stuff here.

I very, very, very much want to do an SB version of tanks that we did in SdlBasic but have to be able to draw on top of an image.

Don't quote me on this one... sdlbasic uses spritez() and bobz() to place images on 'layers' but I think with SB the 'furtherest' image has to be placed first and so on... I'm not wrong about SDL but I could be in regards to SB... I can just sense a series of experiments to either prove or disprove your hypothesis. I'm going to be "out" for most of the day and won't be much help for you. Let me know how you get on?

Have a great day.

J

Not sure, but I think you can achieve what you guys are talking about using the IMAGE command.
It would be cool to see a largish game written in SB !

Here is a simple 'parallax' type demo. It's as ugly as sin but seems to work.
Ignore the right half of the screen. I tried to use "view" and "window", to limit the viewing size to 512x432, but failed miserably.

I'm not sure if this is the type of drawing 'ontop' of other images that you are trying to do, but it was the first thing I thought of.

I am going to attempt to attach my file... crossed fingers... https://www.dropbox.com/s/3r6bayttevc5ypc/parallax.zip?dl=0

Here is your code with some fixes and test:

'
' Image test #1
'

cls
b1 = image(0, 0, xmax, ymax)
color 0, rgb(150, 250, 170):cls
circle 20, 20, 20, 1, rgb(255, 230, 100) filled
sun = image(0, 0, 42, 42)
color 15, 0 : cls
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
backscroll = -8
back1x = 0
back2x = back1x + 512
ground1x = 0
ground2x = ground1x + 512
ground3x = ground2x + 512
bushes1x = 0
bushes2x = bushes1x + 512

open "back1.png" as #a
open "back2.png" as #b
open "ground1.png" as #c
open "ground2.png" as #d
open "bushes1.png" as #e
open "bushes2.png" as #f

i = image(#a)
j = image(#b)
k = image(#c)
l = image(#d)
m = image(#e)
n = image(#f)

dir = 1 : opacity = 100
while inkey$ <> chr$(27)
'opacity = opacity + dir '<<<<<<<<<<<<<<<<<<<<<< comment this line on/off
if opacity > 100 then opacity = 100 : dir = dir * -1
if opacity < 0 then opacity = 0 : dir = dir * -1

i.show(back1x,0, 1, opacity)
j.show(back2x,0, 2, opacity)
k.show(ground1x,408, 3, opacity)
l.show(ground2x,408, 4, opacity)
m.show(bushes1x,328, 5, opacity)
n.show(bushes2x,328, 6, opacity)


back1x = back1x + (backscroll/8)
if back1x < -511 then back1x = 0
back2x = back1x + 511

bushes1x = bushes1x + (backscroll/2)
if bushes1x < -511 then bushes1x = 0
bushes2x = bushes1x + 512

ground1x = ground1x + backscroll
if ground1x < -511 then ground1x = 0
ground2x = ground1x + 512


circle 70, 190, 20, 1, 7 filled '<<<< see how you cant draw on top of image!!!
sun.show( 50, 170, 8, opacity)
'rect 512, 0, xmax, ymax, 0 filled '<<<< wont black out!!!
b1.show(512, 0, 11, 100) 'well at least we can black construction part

wend
close



I was able to get picture framed in 512 wide view by using a blackout image b1.

But see how nothing drawn on screen shows up unless opacity is very low!

BTW I was able to get SdlBasic version of Tanks Battle! going in QB64:
http://www.qb64.net/forum/index.php?topic=14726.0

or here with .exe
http://qb64.thejoyfulprogrammer.com/showthread.php?tid=1227&pid=5859&rndtime=15177853701244805113#pid5859

My mistake. The "sun" is not actually a sun. I was experimenting with a black rectangle to cover the right side of the screen. No success. I then figured I would place a shape (red disc) on the left side just to check to see if the way I coded it was correct or not. The disc did not display but must have forgotten to remove it from the listing prior to publishing. So... no sun... lol

Did this example demonstrate 'drawing one object on top of another' properly or should I be looking at something else?

ps: By the way... nice tanks!

J

Here is code you, Johnno, had from the zip file:

'
' Image test #1
'
cls
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
backscroll = -1
back1x = 0
back2x = 512
ground1x = 0
ground2x = 512
bushes1x = 0
bushes2x = 512

open "back1.png" as #a
open "back2.png" as #b
open "ground1.png" as #c
open "ground2.png" as #d
open "bushes1.png" as #e
open "bushes2.png" as #f
i = image(#a)
j = image(#b)
k = image(#c)
l = image(#d)
m = image(#e)
n = image(#f)

while inkey$ <> chr$(27)
i.show(back1x,0)
j.show(back2x,0)
k.show(ground1x,408)
l.show(ground2x,408)
m.show(bushes1x,328)
n.show(bushes2x,328)

back1x = back1x + (backscroll/3)
if back1x < -512 then back1x = 0
back2x = back2x + (backscroll/3)
if back2x < 0 then back2x = 512
bushes1x = bushes1x + (backscroll/2)
if bushes1x < -512 then bushes1x = 0
bushes2x = bushes2x + (backscroll/2)
if bushes2x < 0 then bushes2x = 512
ground1x = ground1x + backscroll
if ground1x < -512 then ground1x = 0
ground2x = ground2x + backscroll
if ground2x < 0 then ground2x = 512
wend

close #a
close #b
close #c
close #d
close #e
close #f



You, Johnno, had no suns or rectangles or opacity. I put these in to show you can t draw stuff over images!

Ok... I see the confusion...

I had added the disc to test 'overdraw' but didn't post it... because it did't work... I'm going to blame it on a caffeine deficiency... lol

How about a nice game of Pong?

Text and graphics drawing are typically baked into a "surface" before being sent over to SDL (or NDK with android) for display.

Images and form fields are drawn separately over the top of this "surface". I can make another update to allow special handling of text segments to also be handled in this way (to appear over the image). The easiest way to make this work is via an escape code. I'd prefer not to add any new built-in command, unless there's something from one of the other BASICs that would be good to emulate.

This is only Basic I know of where you can't draw or text over an image.

Doesn't an image go to a screen memory location, same as drawing or text and the last change made is what is seen when displayed?

Even if everything is layered, just let text and graphics occupy the last or top layer?

Even if you "bake" in a different spot (and QB64 does have that too) you can still draw or text on top of image before or after image is "screened".

I suppose one maybe able to 'cheat' a little. If in SB, you cannot draw text on an image, then a work around might be to convert the text to an image. Not sure how that will work with your rotate subs... if QB64 and SDLBasic can draw an image and copies it into memory, then it stands to reason, that SB can too... The one downside I can see from this method is that there may not be any transparency... Oh well. It was just a thought anyway...

Have a great day.

J

The difference is, the text and graphics get drawn into a blob whereas images/forms are can be modified/moved etc so exist as 'addressable' elements. That's how it is now, but I'm always open to suggestions for improvements.

Cheers,
Chris

".. whereas images/forms are can be modified/moved etc so exist as 'addressable' elements. "

So, how are images modified? say with some text on top? ;-))

I think we can take an image, load it to array, draw stuff in same array like a virtual screen, and then screen the array? But might be too slow. That is the idea I got when rotating text.

I think your idea of modifying a loaded image should work reasonably well. Still it would be better for SB to provide this somehow. I'm testing a few ideas.

Images can be updated by calling the show() command as outlined here:

https://smallbasic.sourceforge.io/?q=node/617

Here is a poorly conceived example:

open "http://img2.wikia.nocookie.net/__cb20150113215904/farmville/images/9/92/..." as #1
i = image(#1)
x_left = xmax/4
y_top = ymax/4
r =xmax/4
y_step = 10
n_points =20
n = 0
while 1
n= (n+1) mod n_points
x = x_left + r * sin(n* pi / n_points)
y = y_top + (n * y_step)
i.show(x,y)
delay 100
wend

Hi Chris, Johnno and all the lurkers,

I am sure you know the difference between moving an image around and modifying and displaying it (in real time or nearly so). BTW that code example did not error out when it couldn't find the image, just left me staring at black screen. I tried my own .png, it worked fine moving a rigid image tile around.

I think there might be a way through with arrays. Shian might have left enough clues that I might be able to work something out???

This is extremely important to me because at least half my passion for Basic programming comes from being able to create dynamic images/drawings in real time, so much better than still art or statues, it's dynamic and it's almost in real time rendering and it's often mathematical or quite useful in displaying math ideas!

At moment I have converted my 100 line SmallBASIC interpreter into a real standalone one with QB64 at 6 times the code length and not quite there yet with all the functionality the 100 liner SB code had! I can't believe the difference in passion I have for this project and the disinterest of other QB64 coders! Oh well! Walter played a real stinky game with people at TJP so I am withdrawing from there until the stink dissipates. Retrogameprogramming does not allow giant screen shots, exe's... and requires membership anyway for attachments or to see screen shots, QB.net seems to have a rickety server that goes down every time more than 2 or 3 people are on it (but SmallBASIC here has same problem or does it depend who is on-line? it shouldn't be effected I would think but that doesn't seem to be the case) but if interested it's at QB64.net under Novelty Interpreters. I don't think you have to be a member to download from there??? Nothing is perfect and I seem to be ranting about the fact... yikes! The point is, I want to stay with BRUN project for awhile "while the force is with me".

Checking out my array ideas means drawing lines, circles, text.... to an array serving as a Virtual screen and then showing the array with IMAGE commands. This seems an extraordinary complicated way to get around problem with interpreted code but I don't know under the hood stuff, don't think I really want to either... which explains why I am a BASIC coder!

If I did know under the hood stuff, I might know why all the last in text and drawing "primitives" can't be placed on top of a reserved z index layer saved exclusively for real time drawing and texting.

\_ ? _/
   |
  /  \

And BTW I am extremely impressed you can grab images off Internet (when it works) and display them and manipulate them through code!!!!!!

And I wonder what else might be done in this fashion with SmallBASIC, like using Internet for dictionary or Encyclopedia with AI.

There you go Johnno, really useful stuff, not just playing games and drawing pictures for the refrigerator door! ;-))

As for internet-grabber a small demo:

open "chess-pgnfile.pgn" for output as #2
for i=1 to 20
open "http://moodle.usm.md/endgame/getpgn.php?pid="+i as #1
tload #1,p
?p:?#2,p
close #1
next
close #2
end

cheers!

Thanks, you are getting like a whole book of paragraph pages here. I'll have to check out rest of site, if I can.

Oh what language is that from Moldova State University? looking at Google map, north of Black Sea.
Oh that's where Belarus is! (no not at Moldova) and Romania and Bulgaria and Black Sea...

jsalai may I ask where you are from? I am from Ohio, USA, the flyover heartland of USA close to major city Cleveland on shore of the Great Lake, Lake Erie.

Well, all I'm interested at Moldova State University is this Chess-studies site by Mihail Croitor...

I'm somewhere of nowhere :(
Changed my Country very often not moving an inch from place... My roots are in the most developed and richest part of the Europe - now the poorest one... as well not moving an inch from place... I'm from Odzaci, a small city in Vojvodina, part of former Austro-Hungarian Empire, later part of Hungary, then various forms of Yugoslavia, now Serbia...
I speak Serbian, Hungarian, and German fluently (as once WAS normal for this part of the World), read and understand English and Russian, am not bad in Latin... I'm 69...
Programed on ZX-Spectrum, Honeywell DPS-6, PCs, mobiles; used Basic, COBOL, various Assemblers, scripting languages; SB selected as the most usable on mobiles....

So that's me :)

Best!

Thanks jsalai, nice to meet you or know a little more about you.

If ZX Spectrum fan, have you run across Paul Dunn's work? He is developing his own Spectrum thingy system mostly for himself but has a dedicated follower or two. Clever guy with coding IMHO but what do I know of clever guys? I think Chris is quite clever too! :D and you are not to shabby either :)