AGK includes...

Started by therevills, April 02, 2019, 10:09:15

Previous topic - Next topic

therevills

So I'm playing with AGK and trying to have a nice clean approach with files using includes... but I cant get my "colours" working when defined in another file. The code compiles, but the box doesnt have a colour assigned, if I define them in main.agc it works  ???

Any ideas?

Files:

  • main.agc
  • includes.agc
  • functions.agc
  • constants.agc
  • colors.agc

main.agc:
#include "includes.agc"

Init("Testing AGK", 1024, 768)

type BoxType
x as integer
y as integer
w as integer
h as integer
color as integer
endtype

box as BoxType
box.x = Random(0, 1000)
box.y = Random(0, 700)
box.w = Random(10, 100)
box.h = Random(10, 100)
box.color = red

do
DrawBox(box.x, box.y, box.x + box.w, box.y + box.h, box.color, box.color, box.color, box.color, TRUE)
Sync()
loop


includes.agc
#include "common/constants.agc"
#include "common/functions.agc"
#include "colors.agc"


functions.agc
function Init(name as string, width as integer, height as integer)
// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle(name)
SetWindowSize(width, height, FALSE)
SetWindowAllowResize(TRUE) // allow the user to resize the window

// set display properties
SetVirtualResolution(1024, 768) // doesn't have to match the window
SetOrientationAllowed(TRUE, TRUE, TRUE, TRUE) // allow both portrait and landscape on mobile devices
SetSyncRate(30, 0) // 30fps instead of 60 to save battery
SetScissor(0, 0, 0, 0) // use the maximum available screen space, no black borders
UseNewDefaultFonts(TRUE)
endfunction


constants.agc
#constant FALSE 0
#constant TRUE 1


colors.agc
red = MakeColor( 255, 0, 0 )
green = MakeColor( 0, 255, 0 )
blue = MakeColor( 0, 0, 255 )

Steve Elliott

#1
Use a constant and it works.

Quote
#Constant red = MakeColor( 255, 0, 0 )
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Qube

Don't use #Include, it's evil as it can add the code anywhere :o

Use #Insert which places the code exactly at that spot. <<<--- top tip of the day ;D
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

Steve Elliott

Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Amon

Dude, this better be the beginning of a diddy framework for agk...... :)
A scene manager would be the bizniz.

therevills

Thanks Steve - I thought I tried the constant.

Thanks Qube - I was going thru the doco and missed "#Insert".

So I guess the above code "fails" is because "red" is initialized somewhere after I use it...

@Amon - baby steps...

blinkok

#6
yeah. The file is added at the end of main.agc so it never gets executed.
I think #insert would fix it

one other thing. if you use

#insert "../common/constants.agc"

Then it will use a folder at the same depth as your project folder and you won't have to copy the folder into each project you create

MikeHart

If anyone wants to have a fantomengine like framework for sprites, look here


http://www.github.com/mikehart66

therevills

#8
Thanks Mike, I'll have a look :)

So an "included" file can include other files... but an "inserted" file can not insert other files  ???

And an "inserted" file must have a new line first  ??? ???

Steve Elliott

Yeah AGK certainly has it's quirks.  ???

I've only worked on small programs so that sort of stuff hasn't been an issue for me.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Derron

Without checking the documentation: so what is the benefit of having two of these commands?
Shouldn't include just mean "replace this line with the included file content" ? There is no reason to "somewhere in the code" write that you want something "added at the end" (just add the include at the end then ...).

I love "import" in BlitzMax as it avoids such mess: code is independend from the other and done. Of course in AGK this is a "scripted language problem" ... hope "Tier2" offers a different approach (imports are way better for reusability than includes as it avoids the need to know if something included includes something else which was included somewhere ... else ;-)).


bye
Ron

MikeHart

Tier2 is C++ on desktop. You link to agks header files and import its libs. On mobile it is similar.

therevills

Looks like "include" is good for functions and constants, "insert" is good for other code - basically it replaces the line where the insert with the other files contents:


if RectsOverlap(x, y, w, h, x1, y2, w2, h2)
  #insert "dostuff.agc"
endif


https://www.appgamekit.com/documentation/language/0_include.htm

https://www.appgamekit.com/documentation/language/36_insert.htm

Derron

It all sounds as if you could use "insert" for both things - add the "insert" at the end and you have the "include" behaviour.

Both commands act as kind of "placeholder" with the difference of "include" not replacing that line but "removing that line + append at the end".

Maybe they differ when it comes to compilation.


bye
Ron

Pfaber11

This is all interesting stuff . I haven't used include or insert yet but I may do in my next app. Might be better than just creating one big app. Kind of like replacing gosub and return if I understand it right.
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz