|
|
Help Burt Learn Lite-C
#166634
11/09/07 04:02
11/09/07 04:02
|
Joined: Aug 2002
Posts: 2,692 California, USA
bupaje
OP
Expert
|
OP
Expert
Joined: Aug 2002
Posts: 2,692
California, USA
|
Or I'll whine until you get fed up.  On the plus side I won't post in the Lite-C forums until my questions are less stupid. Can anyone look at this and tell me why I can't get the value for sMessage to print? Be patient with me as I haven't even looked at programming in ages and last that I did was BASIC. This is a nonsense program , just trying to understand the structure and such. #include <acknex.h> #include <default.c> /* Declare Variables */ var vGlobal_Wind_Speed; var vLocal_Wind_1; var vObj1=1; STRING* sMessage; function global_wind() { // Generate Global Wind Values vGlobal_Wind_Speed = 50; } function local_wind() { // Generate Local Wind Values //get global wind and adjust by local conditions vLocal_Wind_1 = (vGlobal_Wind_Speed / 2); sMessage = "Windy!"; return sMessage; } function display_results() { PANEL* pDisplay = { digits (35, 10, "Global Wind Speed: = %0.f", "Arial#16b", 1, vGlobal_Wind_Speed); digits (35, 30, "Local Wind 1: = %0.f", "Arial#16b", 1, vLocal_Wind_1); digits (35, 70, "Object 1: = %s", "Arial#16b", 1, sMessage); flags = VISIBLE; } } function main() { var temp_counter=0; // Initialize Display video_mode = 6; screen_color.blue = 150; // Check if wind engine is enabled while (temp_counter<1) { global_wind(); local_wind(); display_results(); temp_counter += 1; } }
|
|
|
Re: Help Burt Learn Lite-C
[Re: bupaje]
#166635
11/09/07 11:53
11/09/07 11:53
|
Joined: Jul 2002
Posts: 4,436 Germany, Luebeck
Xarthor
Expert
|
Expert
Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
|
As I'm only used to c-script this may be false, but I would move the PANEL* pDisplay out of the function and instead just set the flag of that panel to visible inside the function: Code:
PANEL* pDisplay = { digits (35, 10, "Global Wind Speed: = %0.f", "Arial#16b", 1, vGlobal_Wind_Speed); digits (35, 30, "Local Wind 1: = %0.f", "Arial#16b", 1, vLocal_Wind_1); digits (35, 70, "Object 1: = %s", "Arial#16b", 1, sMessage); }
function display_results() { set(pDisplay,VISIBLE); }
|
|
|
Re: Help Burt Learn Lite-C
[Re: Xarthor]
#166636
11/09/07 16:39
11/09/07 16:39
|
Joined: Oct 2004
Posts: 1,655
testDummy
Serious User
|
Serious User
Joined: Oct 2004
Posts: 1,655
|
Due to time constraints, product age, developmental history, etc. I haven't yet made a switch to Lite-C. *Use the [ code ] [ /code ] tags to maintain formatting in code. Code:
STRING* sMessage; // pointer points to NULL or garbage sMessage = "Windy!"; // Wrong? Must allocate mem, etc. sMessage = str_create("Windy!"); // Right?
|
|
|
Re: Help Burt Learn Lite-C
[Re: testDummy]
#166637
11/09/07 18:09
11/09/07 18:09
|
Joined: Aug 2002
Posts: 2,692 California, USA
bupaje
OP
Expert
|
OP
Expert
Joined: Aug 2002
Posts: 2,692
California, USA
|
Thanks. Still no good and there is somethings I'm not getting so please bear with me. 1. I still get a [null] for sMessage when I try to print. I'm guessing I misunderstand. I want to declare a string variable called sMessage, assign it a value in a function and return that to the print routine. Not sure where am going wrong. 2. Want to understand how to structure the program. I wanted to put the print routine and so on in seperate functions so as to break the program into discrete modules so I can better understand, then in main() simply call each module/function in order. This is how I remember it from basic anyway. Code:
declare variables
functions
main routine start initialize function1() function2() function3() output end
Once I can visualize the way to structure and sort out the pointers and memory stuff I didn't formerly have to deal with, think I'll be ok. Thanks for any help. Sorry for the noob questions - I'm not usually such a blockhead. Code:
#include <acknex.h> #include <default.c>
var vGlobal_Wind_Speed; var vLocal_Wind_1; var vObj1=1;
STRING* sMessage; sMessage = str_create("Windy!");
function global_wind() { // generate global wind values vGlobal_Wind_Speed = 50;
}
function local_wind() { //get global wind and adjust by local conditions
vLocal_Wind_1 = (vGlobal_Wind_Speed / 2); }
PANEL* pDisplay = { digits (35, 10, "Global Wind Speed: = %0.f", "Arial#16b", 1, vGlobal_Wind_Speed); digits (35, 30, "Local Wind 1: = %0.f", "Arial#16b", 1, vLocal_Wind_1); digits (35, 70, "Object 1: = %s", "Arial#16b", 1, sMessage); }
function display_results() { set(pDisplay,VISIBLE); }
function main() { var temp_counter=0; // initialize display video_mode = 6; screen_color.blue = 150;
// while wind engine is enabled while (temp_counter<1) {
global_wind(); local_wind(); display_results(); temp_counter += 1; }
}
|
|
|
Re: Help Burt Learn Lite-C
[Re: bupaje]
#166638
11/09/07 20:12
11/09/07 20:12
|
Joined: Jul 2004
Posts: 1,205 Greece
LarryLaffer
Serious User
|
Serious User
Joined: Jul 2004
Posts: 1,205
Greece
|
Hello bupaje, In your last post, replace this: Code:
STRING* sMessage; sMessage = str_create("Windy!");
with this: Code:
STRING* sMessage = "Windy!";
to get it working.. you see, when you type things outside of any function, all you are allowed to do is declare global variables, strings and such... so str_create("Windy!"); is not allowed since str_create is a name of a function, and you can only call other functions if you are in a function yourself (like in main() ). You can also make the example you had in your first post working, if you replace this: Code:
function local_wind() { // Generate Local Wind Values
//get global wind and adjust by local conditions
vLocal_Wind_1 = (vGlobal_Wind_Speed / 2); sMessage = "Windy!"; return sMessage; }
with this: Code:
function local_wind() { // Generate Local Wind Values
//get global wind and adjust by local conditions
vLocal_Wind_1 = (vGlobal_Wind_Speed / 2); str_cpy(sMessage,"Windy!"); }
You cannot edit strings, like you do with variables using '=', so you'll have to use the function str_cpy instead. There's also no need to 'return' anything since you've made sMessage a global string. Quote:
2. Want to understand how to structure the program. I wanted to put the print routine and so on in seperate functions so as to break the program into discrete modules so I can better understand, then in main() simply call each module/function in order. This is how I remember it from basic anyway.
My advice is that you forget everything you've learnt in basic, and start with a fresh mind by studying the Lite-C tutorial. c64 basic was a batch language, meaning there were no functions or anything so of course your program structure would be different. what you describe looks more like qbasic or turbo basic which was procedural, but still more dated than lite-c in lots of areas.
You'll learn program structure by doing the tutorial and mimicking other examples, but in general c-script/lite-c programs tend to not use the main() function as much because of the Scheduler List feature and the use of wait(1), so you won't have to worry too much about creating while loops inside main() and trying to display an output at the end of it
Best of luck in this, Aris
|
|
|
Re: Help Burt Learn Lite-C
[Re: bupaje]
#166641
11/11/07 14:58
11/11/07 14:58
|
Joined: Jul 2004
Posts: 1,205 Greece
LarryLaffer
Serious User
|
Serious User
Joined: Jul 2004
Posts: 1,205
Greece
|
I guess that what you want to look into is the wait() function. If you say you've made wind speed to mechanically climb up, then you've already figured out how to increment the wind value in a while loop Code:
wind=100; while (wind<150) { wind+=1; }
but since you don't get to see every iteration of the while loop slowly, wind goes instantly from 100 to 150 in the screen. If you have the program wait for one tenth of a second though after every iteration, the number will gradually go up as u want it Code:
wind=100; while (wind<150) { wind+=1; wait(-0.1); }
Copy paste and run the program below and try to figure out how everything works: Code:
#include <acknex.h> #include <default.c>
var vGlobal_Wind_Speed;
STRING* sMessage = "";
PANEL* pDisplay = { digits (35, 10, "Global Wind Speed: = %0.f", "Arial#16b", 1, vGlobal_Wind_Speed); digits (35, 70, "Object 1: = %s", "Arial#16b", 1, sMessage); flags=VISIBLE; }
#define LOW_WIND 100 #define HIGH_WIND 150
#define TENTH_OF_SECOND 0.1
function main() { // initialize display video_mode = 6; screen_color.blue = 150;
while (1) { for (vGlobal_Wind_Speed=LOW_WIND; vGlobal_Wind_Speed<HIGH_WIND; vGlobal_Wind_Speed++) wait(-TENTH_OF_SECOND); str_cpy(sMessage,"Reached High Wind"); for (vGlobal_Wind_Speed=HIGH_WIND; vGlobal_Wind_Speed>LOW_WIND; vGlobal_Wind_Speed--) wait(-TENTH_OF_SECOND); str_cpy(sMessage,"Reached Low Wind"); } }
Some new things I've included: While(1): This while loop will go on forever, because its expression (1) is taken as being always true. You could have also written another always true statement, like (10>5) for example, but while(1) is more customary. #define: Some variables, i declared with #define instead of var, and typed them in all caps. These are numbers that aren't really variable, but always stay the same throughout the whole program, so it's better coding to use define for all those. The same way, it would be preferred to use define to declare pi, as #define PI 3.14 than use var pi=3.14, although it would have worked all the same. I could have also used the numbers 100 and 150 themselves and not use defines or vars at all, but avoid hardcoding numbers as much as you can. If you decide later that high wind is 170 instead of 150, but you've already used the number 150 many times in your program, it would be harder to replace them all one by one. for(...): For loops are cleaner than while loops and sometimes preferred, in cases such as yours. The above could have been written with while loops, but i would have used more lines for it. The shorter you make your program, the quicker you, some one else and the compiler will read it good luck, Aris
|
|
|
Re: Help Burt Learn Lite-C
[Re: LarryLaffer]
#166642
11/11/07 18:17
11/11/07 18:17
|
Joined: Aug 2002
Posts: 2,692 California, USA
bupaje
OP
Expert
|
OP
Expert
Joined: Aug 2002
Posts: 2,692
California, USA
|
Aris, thanks for the very detailed and helpful response. I was confused about defines yesterday and now see they are constants. I thought they were like VB enums - is there an equivalent in Lite-C by the way? A way to group related variables or defines as eventually I was thinking I'd like to incorporate a scale like the Beaufort Scale http://en.wikipedia.org/wiki/Beaufort_scale In a related question is it possible to group several strings to be able to access them via dot notation? I was trying to put all the "wind!" messages in a struct but guess I misunderstand its purpose.
|
|
|
|