I'm going to be porting my unfinished CC22 entry to C for a "normal" release, and I'm fairly new to C. This would be my first game in C, too. Expect this as a help thread/ progress thread Razz

So... how does one do labels and gotos in C, or do you need to 'include' a C file when you want to use it as a label? (See EXAMPLE)


Code:
EXAMPLE
label.c has the contents of 'Lbl A'

//main.c
...
#include "label.c" //Goto (and theoretically Return) A
SM84CE wrote:
I'm going to be porting my unfinished CC22 entry to C for a "normal" release, and I'm fairly new to C. This would be my first game in C, too. Expect this as a help thread/ progress thread Razz

So... how does one do labels and gotos in C, or do you need to 'include' a C file when you want to use it as a label? (See EXAMPLE)


Code:
EXAMPLE
label.c has the contents of 'Lbl A'

//main.c
...
#include "label.c" //Goto (and theoretically Return) A


This page explains labels in C well. Notice that it actually warns you against using the goto statement.

If you want to return values then you could use functions.
Here's an example function. (This example has no practical use, it simply exists to show functions. If I wanted to do something similar to this I'd just use a macro or constant instead.)

Code:

int returnFive(); //Function prototype, just declaring this exists.

void main() { //This function is run when the program is run
//stuff...
printf("%d",returnFive()); //Prints the result of 'returnFive()' to console
//...more stuff
}

int returnFive() {
return 5;
}


Notice how I never said you should do any of this because everything I might do could be wrong/improper. Even when I said you could use functions I used the word could, making it your choice. Evil or Very Mad
Never use labels or goto unless you have a very very good reason to. Please look at all the useful examples that come with the toolchain download.
Yeah, C provides richer primitives than goto and labels, which are nearly always preferred - especially in a first C program Smile
What you want to do is almost certainly not even legal in C: you cannot use goto to jump into a different function. (While C technically allows splitting a function across multiple files, I've never seen that done.) There is technically a way around that restriction, but it is much more complicated to use and is only used for implementing advanced techniques like closures and coroutines. C's function and looping constructs are far easier and more pleasant to use.

More required reading for using goto.
So based off of this, I assume it's better to use different functions in different files and call them as necessary?

DrDnar wrote:

lol Smile Very Happy
Just use functions in the same file.
So I keep getting this error:

Code:

C:\Users\Sanjivan\Documents\GitHub\FusionSim\src\main.c
C:\USERS\SANJIVAN\DOCUMENTS\GITHUB\FUSIONSIM\SRC\MAIN_LOOP.C    (2,9) : ERROR (217) Cannot open include file "stdbool.h"
P1: Internal Error(0xFFC2A5)
        Please contact Technical Support
 File: C:\USERS\SANJIVAN\DOCUMENTS\GITHUB\FUSIONSIM\SRC\MAIN_LOOP.C
 Relative Line: 2
make: *** [obj/main.src] Error -1


I cloned the repo to my other laptop, in hopes of the SDK not being wacky there. No luck. People in SAX also suggested that I move the folder to a shorter path, which I did on my old laptop (to C;/SM CE C/FusionSim/), and ran `make` again. Still same error. I'm hoping someone could shed light on this, the error popped up after I declared a boolean in main_loop.c I can provide code if needed, but the "line 2" is "#includes <stdbool.h>" and the boolean in=s declared as such:

Code:
bool Reheat;
...
Reheat = false;


EDIT: SDK V8.5 and 8.3 give the same error

GitHub https://github.com/SanjivanM/FusionSim/tree/master/src
Ok, fixed those errors, it was simply me not knowing Razz

Now, any suggestions for possible upgrades? I have in mind: Increasing max flow rate and max fuel. I might reduce the initial max down from 1,000,000 mL to something else...
I'll also make a "fuel" store, where you can buy specific amounts of fuel.
OK, So I've picked this up, and ran into a problem. How do I declare an int array as an extern? I've tried int[] SaveList; but that throws a "defaults to int" error.

The array itself is declared as such: int[] SaveList[30];
In the other .h and .c files, I've declared it as int[] SaveList;

Should I be including the size with the declarations as well?

Progress report coming soon!

And I might make the Git Repo private, to build more suspense...
***SM84CE laughs maniacally
Use "int SaveList[30];" in the .c file which defines the array, and "extern int SaveList[];" or "extern int SaveList[30];" (you can include the size, indeed) in the .h file which declares it. No need to use more than one extern definition if all files include the .h file. .
Do not use multiple "int SaveList[];" or worse, "int SaveList[30];" in other .c and .h files: these would cause unwanted different instances of variables with the same name, a condition which isn't much fun to debug when accidentally created.
Better yet, don't use globals.
I keep getting this error

Code:
C CE SDK Version 8.5
C:\Users\Sanjivan\Documents\GitHub\FusionSim\src\main_loop.c
C:\Users\Sanjivan\Documents\GitHub\FusionSim\src\play.c
flat assembler  version g.ice6x-ez80
C:\CEdev\include\fasmg-ez80\ld.fasmg [760] macro ? [637] macro handle [5] macro parse [303] obj\main_loop.src [1925]:
        XREF _FuelB:ROM
macro ? [6] macro XREF [3] macro ? [1]:
        . def
Processed: . := global._FuelB
Error: symbol '_FuelB' is undefined or out of scope.
make: *** [bin/FUSION.bin] Error 2
Press any key to continue . . .

when trying to declare two integers as extern variables (created in the file that handles the fuel selection, then needed in the main loop to properly get the fuel sprites when loaded)

Declaring code in play.c

Code:
int FuelA = 0;
int FuelB = 0;


play.h

Code:
#ifndef PLAY_h
#define PLAY_h

#include <graphx.h>

void play();
int fuelSelect();
extern int Efficiency;
extern gfx_sprite_t * ASpr;
extern gfx_sprite_t * BSpr;
extern int FuelA;
extern int FuelB;

extern char *ContStr;
//extern int contEff; // 50 = sphere, 100 = torus
extern int key;

#endif


main_loop.h

Code:
#ifndef MAIN_LOOP_h
#define MAIN_LOOP_h

#include <graphx.h>

void main_loop();
extern int Efficiency;
extern gfx_sprite_t * ASpr;
extern gfx_sprite_t * BSpr;
extern int FuelA;
extern int FuelB;
extern char *ContStr;
//extern int contEff; //from 'play.h'
extern int key;
extern int SaveList[30]; //declare SaveList as a global

#endif


play.c does contain the line

Code:
#include "play.h";


And Mateo, if you have any other suggestion that doesn't involve telling me to not use globals, I'd appreciate that Razz
Don't use globals. Pass arguments to functions.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement