July 19, 2019, 01:32:59 AM

Author Topic: AGK includes...  (Read 541 times)

Offline therevills

  • Hero Member
  • *****
  • Posts: 552
AGK includes...
« on: April 02, 2019, 10:09:15 AM »
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:
Code: [Select]
#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
Code: [Select]
#include "common/constants.agc"
#include "common/functions.agc"
#include "colors.agc"

functions.agc
Code: [Select]
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
Code: [Select]
#constant FALSE 0
#constant TRUE 1

colors.agc
Code: [Select]
red = MakeColor( 255, 0, 0 )
green = MakeColor( 0, 255, 0 )
blue = MakeColor( 0, 0, 255 )

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 1839
Re: AGK includes...
« Reply #1 on: April 02, 2019, 10:34:28 AM »
Use a constant and it works.

Quote
#Constant red = MakeColor( 255, 0, 0 )
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.1, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
Raspbian Buster, pi4 4Gb RAM,1.5Ghz

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 1961
Re: AGK includes...
« Reply #2 on: April 02, 2019, 02:41:21 PM »
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
Until the next time...

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 1839
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.1, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
Raspbian Buster, pi4 4Gb RAM,1.5Ghz

Offline Amon.

  • Full Member
  • ***
  • Posts: 114
  • What? There's no ceiling outside?
    • Amon.Pro
Re: AGK includes...
« Reply #4 on: April 02, 2019, 08:46:08 PM »
Dude, this better be the beginning of a diddy framework for agk...... :)
A scene manager would be the bizniz.
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Offline therevills

  • Hero Member
  • *****
  • Posts: 552
Re: AGK includes...
« Reply #5 on: April 02, 2019, 10:03:53 PM »
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...

Offline blinkok

  • Full Member
  • ***
  • Posts: 103
Re: AGK includes...
« Reply #6 on: April 02, 2019, 10:12:05 PM »
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

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 588
  • Cerberus-X Dev Team
    • Cerberus X
Re: AGK includes...
« Reply #7 on: April 03, 2019, 06:06:44 AM »
If anyone wants to have a fantomengine like framework for sprites, look here


http://www.github.com/mikehart66

Offline therevills

  • Hero Member
  • *****
  • Posts: 552
Re: AGK includes...
« Reply #8 on: April 03, 2019, 09:13:10 AM »
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  ??? ???

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 1839
Re: AGK includes...
« Reply #9 on: April 03, 2019, 09:21:43 AM »
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.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.1, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
Raspbian Buster, pi4 4Gb RAM,1.5Ghz

Offline Derron

  • Hero Member
  • *****
  • Posts: 2208
Re: AGK includes...
« Reply #10 on: April 03, 2019, 09:42:14 AM »
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

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 588
  • Cerberus-X Dev Team
    • Cerberus X
Re: AGK includes...
« Reply #11 on: April 03, 2019, 09:46:11 AM »
Tier2 is C++ on desktop. You link to agks header files and import its libs. On mobile it is similar.

Offline therevills

  • Hero Member
  • *****
  • Posts: 552
Re: AGK includes...
« Reply #12 on: April 03, 2019, 10:03:56 AM »
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:

Code: [Select]
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

Offline Derron

  • Hero Member
  • *****
  • Posts: 2208
Re: AGK includes...
« Reply #13 on: April 03, 2019, 10:58:08 AM »
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: AGK includes...
« Reply #14 on: April 23, 2019, 08:10:45 PM »
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.