[MSX-C] Q&A official thread

Page 23/57
16 | 17 | 18 | 19 | 20 | 21 | 22 | | 24 | 25 | 26 | 27 | 28

By Sylvester

Hero (555)

Sylvester's picture

16-09-2015, 23:20

@JaviLM: you said you wrote a file for each function, but how do you link them with L80? because my L80 command now became too long Sad and if I read it correct in the dos2 manual, the max command length is 127 chars.

By Manuel

Ascended (18794)

Manuel's picture

16-09-2015, 23:22

JaviLM: I stand corrected on adding values to pointers Smile Learned something again...

By Sylvester

Hero (555)

Sylvester's picture

17-09-2015, 00:40

And an other small question about the scope of variables with multiple files. In my main.c I include a func.h which declares a public variable (int count = 0Wink and a function. The function updates the public variable and when I print the value (in the function) I see it gets updated each time i call the function.
But when I use the variable in my main.c, the value is alway 0 (as I initialized the var), and not the same as in the function that updates it ??? How can I declare a public var that can be used in both files?

By hit9918

Prophet (2923)

hit9918's picture

17-09-2015, 00:41

About stack allocation one would need an example to talk.
"not forgetting to deallocate it",
so, when the game map is loaded, one does not forget to deallocate it.
But then crash. Because when maploader() did return, the map has died.

One can do one fat malloc() and then write a function mymalloc()
that does just slide within that malloc with an incrementing pointer.
a nice function which includes check whether one does overrun the underlying malloc.
Then you can stuff dozens things in there and have only one free() to think about.

e.g. I load 8 charsets and a map and a sprite list etc etc, dozen such things with incrementing pointer
and while the game runs they are just always there, no malloc/free needed.

By hit9918

Prophet (2923)

hit9918's picture

17-09-2015, 00:44

try
extern int count
in the .h file.
and then maybe one of the .c files must say
int count

By anonymous

incognito ergo sum (116)

anonymous's picture

17-09-2015, 01:00

Sylvester wrote:

@JaviLM: you said you wrote a file for each function, but how do you link them with L80? because my L80 command now became too long Sad and if I read it correct in the dos2 manual, the max command length is 127 chars.

Good question! As we're finding these problems we have to come up with solutions and we learn more. :-)

The answer in this case is LIB80. We compile all our functions and join all the .REL files into a small library file (MYLIB.REL or however you want to call it), and then you replace all those entries in the L80 command line with a single "MYLIB/S".

I'll write about LIB80 in a few more days.

By anonymous

incognito ergo sum (116)

anonymous's picture

17-09-2015, 03:44

Sylvester wrote:

And an other small question about the scope of variables with multiple files. In my main.c I include a func.h which declares a public variable (int count = 0Wink and a function. The function updates the public variable and when I print the value (in the function) I see it gets updated each time i call the function.
But when I use the variable in my main.c, the value is alway 0 (as I initialized the var), and not the same as in the function that updates it ??? How can I declare a public var that can be used in both files?

hit9918's answer is correct. First, make sure that you declare the variable outside of any function so it becomes a global. After the #includes is fine. Second, in any other file where you need to use this variable, you declare it as extern, so the compiler will know that it is defined in another file. See this example:

F1.H:

#include <stdio.h>

VOID	f1();

F1.C:

#include "f1.h"
#include "f2.h"

int	gblVar;

VOID	f1(string)
char	*string;
{
	printf("%d: I'm f1() and %s called me.\n", gblVar++, string);
	f2("f1()");
}

F2.H:

#include <stdio.h>

VOID	f2();

F2.C:

#include "f2.h"
#include "f3.h"

extern	gblVar;

VOID	f2(string)
char	*string;
{
	printf("%d: I'm f2() and %s called me.\n", gblVar++, string);
	f3("f2()");
}

F3.H:

#include <stdio.h>

VOID	f3();

F3.C:

#include "f3.h"

extern	gblVar;

VOID	f3(string)
char	*string;
{
	printf("%d: I'm f3() and %s called me.\n", gblVar++, string);
}

MAIN.C:

#include <stdio.h>
#include "f1.h"

extern	gblVar;

char	main(argc, argv)
int	argc;
char	*argv;
{
	gblVar = 1;

	printf("%d: This is main().\n", gblVar++);
	f1("main()");
}

And you build everything with this script:

MAKE.BAT:

cf f1
cf f2
cf f3
cf main
cg -k f1
cg -k f2
cg -k f3
cg -k main
m80 =f1/z
m80 =f2/z
m80 =f3/z
m80 =main/z
l80 a:\msxc12\ck,f1,f2,f3,main,a:\msxc12\clib/s,a:\msxc12\crun/s,a:\msxc12\cend,main/n/y/e:xmain

Note that the variable gblVar only exists in F1.C. All other files that use it declare it as extern. Also, in each file you only include the .h files of the functions that you actually use.

Running this gives:

Remember that you don't need to compile every file every single time. After you've finished implementing a function you just compile it once and leave the .REL file. You only need to run the CF/CG/M80 steps for a file when you change something in it.

By anonymous

incognito ergo sum (116)

anonymous's picture

17-09-2015, 04:11

And this is a modified MAKE.BAT script to create a library with LIB80 and avoid having to add so much stuff to the L80 command:

You can even move the first half of the script to a standalone file (MAKELIB.BAT), so the MAKE.BAT script only compiles the main program, saving lots of time.

By anonymous

incognito ergo sum (116)

anonymous's picture

17-09-2015, 04:12

Note the line lib80 mylib=f1,f2,f3 and the parameter mylib/s to L80.

By AxelStone

Prophet (3108)

AxelStone's picture

17-09-2015, 07:46

I repeat, we are learning a lot in this topic Smile . Here goes anothre question:

Question: How can I chain exectutions of several programs? Supose we have a game that first execute the opening, after you execute the main menu and finally you execute the game. In BASIC is very simple:

10 ' Opening engine
...
10000 RUN"title.bas
10 ' Title engine
...
10000 RUN"game.bas
10 'Game engine
...
10000 RUN"title.bas (execute when game over)

Or simply suposse an RPG game and you enter into a Store, so you need to load it. In other words, how can I load new executables during gameplay?

Page 23/57
16 | 17 | 18 | 19 | 20 | 21 | 22 | | 24 | 25 | 26 | 27 | 28