"Learn C" ? - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: "Learn C" ? (/thread-6482.html) Pages: 1 2 "Learn C" ? - Marjock - Jul 3, 2004 12:35 PM so in heaps the books and pdf docs etc. it says stuff like "we assume that you've already learned the C language". What sorts of things should I be able to do? how do I know when I've managed to "Learn C"? at the moment, I can do basic things like make a calculator, and make a coloured ball bounce off the corners of a window. I can handle arrays, what else do I need to know? I hope this makes sense for the rest of you, Mark "Learn C" ? - Muffinking - Jul 3, 2004 01:29 PM usually it basically means you have a good handle on: - Pointers - parameter passing - arrays - memory allocation/management - structures pretty much a good overall knowledge of how to do these and other things within C.. also how to implement various data structures in the C language.. like a stack.. a queue and what not.. from the sounds of it.. I think you have a decent handle on things.. Hank- "Learn C" ? - Duane - Jul 3, 2004 01:33 PM Marjock Wrote:make a coloured ball bounce off the corners of a window. I can handle arrays, what else do I need to know? I hope this makes sense for the rest of you, Mark How do yo manage that? "Learn C" ? - Marjock - Jul 3, 2004 01:47 PM hehe, chances are I know half of these things I just don't know their names ^^ How did make a ball bounce of the edges of a window? uhm, drew the ball at x,y,xx,yy Code: ```x = x + moverx y = y + movery xx = xx + moverx yy = yy + movery if(xx>400) { moverx= moverx * -1 } if(x<0) { moverx = moverx * -1 }``` and so on and soforth, I /think/ I can't remember exactly and I don't have the code on this comp, but that's the sorda thing that shoud work if you just want a basic idea. Have a look at my previous posts, that's how I found out how to do it. *shrug* -Mark "Learn C" ? - Sta7ic - Jul 3, 2004 03:06 PM "Learn C" as was said usually means understanding how to write and use functions, arguments, return values, arrays, pointers (and pointer arithmatic), what all the operators are (including <<, *=, % and so on), preprocessor commands, scope, static & global variables, structures, and memory allocation. It's about being able to read the raw, uncommented code and verbalise in shop talk what a given chunk of code does. Frequently "learning a language" also means knowing how to use the built-in functions or classes, like the cmath and stdio libraries, the Java Integer and BufferedReader classes, or the Python re module. Understanding how data structures work, such as lists, queues, stacks, trees, and maps work, is also a plus, but is not language specific and not under "learning C". Being able to learn and work with non-C libraries is a survival skill as well. -"Sta7ic" Matt "Learn C" ? - Steven - Jul 3, 2004 04:47 PM Marjock Wrote:uhm, drew the ball at x,y,xx,yy Heh, that's often the hardest part "Learn C" ? - Fenris - Jul 4, 2004 03:11 AM Sounds to me that you need to get a handle on pointers, functions and memory allocation, and you should be ready to make some really interesting mistakes. ;-) "Learn C" ? - Mazilurik - Jul 4, 2004 07:12 AM Sta7ic Wrote:[Learning C is] about being able to read the raw, uncommented code and verbalise in shop talk what a given chunk of code does. This is a crucial aspect of "knowing" a programming language. Whenever you see sample code in something that you're reading, it's worth the effort to go through it line by line and make sure that you understand exactly what the author is doing; after all, if you can't see how something works in someone else's code, the likelihood that you'd be able to do something similar in your own code is minimal. Unless you can take what you've read about a language and apply it to practical situations, you don't really "know" the language. Don't get discouraged, however, if you run across things which you don't understand; logical issues will usually sort themselves out if you work through the code a little longer, and syntax and language features which you don't understand can always be looked up. (It's also worth your time to look up features which you "sort of" understand from a practical standpoint but don't know in theory; for example, it's good to know what a dereference operator actually does, rather than just thinking of it as "that * thing.") You probably won't know every single feature of a language until you've used it for years, if you ever do, but knowing everything about a language isn't necessary to know how to use it; so long as you're willing to learn, a solid understanding of the fundamental aspects of the language (which other people have mentioned in this thread) should be enough for you to move on to more interesting areas. (Studying sample code can also help in this regard; the advantages of things such as pointers and structures aren't always evident until you see them in use.) "Learn C" ? - Carlos Camacho - Jul 4, 2004 04:50 PM I may be flamed but... if you have learned the basics of C, and want to learn more towards making games, why not consider Mark's book? "Mac Game Programming" Cheers, "Learn C" ? - Josh - Jul 4, 2004 05:34 PM MGP assumes that the reader has a good working knowledge of C++ and Carbon. I'm sure I'll get heat about this, but... IMHO, the method MGP teaches (QuickDraw) is no longer a very viable or wise method. Anymore, OpenGL is the only way to go. "Learn C" ? - Steven - Jul 4, 2004 05:55 PM If you would like something cross-platform and somewhat easy to work with, I would look at Allegro - it's a very nice library that's quite easy to work with without having to learn as much as OpenGL... "Learn C" ? - Marjock - Jul 5, 2004 02:47 AM Thanks guys, but I think I want to learn/use OpenGL. so, I'm pretty sure I know what a function is and how to use one, but I may as well make sure: so, first we would declare the function before the main loop: void mark(something = 4,somethingelse = 3); I think in the brackets I'm giving a value to those variables but only for that function, but I'm not really sure. Then I'd call it inside the main loop (hold I've just hit a problem, I think what I'm doing might be a sub routine, not a function): gosub(mark); I think that's right, but as I said that might only be for sub routines *shrug* and then I would define it (so basically defining it is saying what'll happen when I call it, right?) void mark(something = 4,somethingelse = 3) { whatever I want the function to do } now, I think that the idea of this is that, I only have to define it once, but I can then call it as many times as I want. hmmm, I'm not sure if that's right, so please correct me where I'm wrong. thanks, Mark "Learn C" ? - NCarter - Jul 5, 2004 06:01 AM Marjock Wrote:so, first we would declare the function before the main loop: void mark(something = 4,somethingelse = 3); I think in the brackets I'm giving a value to those variables but only for that function, but I'm not really sure. Uh oh... you seem to be a bit confused. For starters, the things in the brackets are the parameters of the function. If you assign a value to a parameter in a function declaration, you're actually specifying the default value for that parameter if you omit it when you call the function. This is a C++ feature; I don't believe it's supported in plain C, so you'd have to leave out the assignment and always specify the parameter. You also need to specify the type of the parameters. For example: Code: `void mark([b]int[/b] something, [b]int[/b] somethingelse);` Quote:Then I'd call it inside the main loop (hold I've just hit a problem, I think what I'm doing might be a sub routine, not a function): gosub(mark); I think that's right, but as I said that might only be for sub routines *shrug* Gosub isn't a keyword in C (it's from the Basic language). You would call the above function like this: Code: `mark(4, 3);` Here I'm explicitly specifying the parameters as I call the function. Of course, I can use any other numbers or variables instead of 4 and 3 (provided they're of a type which is compatible with int). Incidentally, there's no major difference between functions and 'subroutines' in C. Functions return a value and 'subroutines' return void (nothing), although C programmers use the word 'function' for both kinds of code. Quote:and then I would define it (so basically defining it is saying what'll happen when I call it, right?) void mark(something = 4,somethingelse = 3) { whatever I want the function to do } You're nearly right here, except you can't use the assignments inside the brackets when you define a function. This won't work at all, even in C++. If you need to create variables and assign values to them, you need to do something like this: Code: ```void mark(int something, int somethingelse) {     int anothervariable = 5;     int onemorevariable;     onemorevariable = something + somethingelse - anothervariable; }``` Quote:now, I think that the idea of this is that, I only have to define it once, but I can then call it as many times as I want. That's correct. From the above misunderstandings, I think you should read through a good book or tutorial on C before diving into anything much more complicated! "Learn C" ? - Mazilurik - Jul 5, 2004 06:42 AM (NCarter's already covered some of this (more clearly than I did), but I still think it's worth posting; besides, I didn't do all this typing for nothing... ) You've got the general idea, though I think you're missing the point; you aren't using parameter passing, which is what gives functions most of their power. For instance, parameter passing would let you define a function called SortArray() which can be used to sort any array in your program, or a function called DrawImage() to which you could pass information about the size, texture, and position of an image and have it draw to the screen. A more concrete example: If you wanted to create a function called DrawImage, you would first write what is called a function prototype; this simply states that there is a function called DrawImage which is defined elsewhere and specifies the arguments that the function takes. It's usually stored in a header file (for instance, image.h), which allows source files to easily use functions defined in other files (graphics.c could use DrawImage() if you added the line #include "image.h" to it). A function prototype is of the general format type FunctionName(type1 arg1, type2 arg2,Ö); , where "type" is the data type returned by the function ("void" if no return value), "type1" is the data type passed as the first argument, "arg1" is the name used for the first argument inside of the function (technically, a variable created to store the data passed as the first argument), etc. It's often good to have a return value just for error checking; the function can be set up to return a specific (generally non-zero) value if a given error occurs. A function prototype for DrawImage might be int DrawImage(float vSize, float hSize, float vPos, float hPos, int tex_id); . Notice, however, that all of the arguments passed to the function are associated with a specific image; thus it makes sense to group them together and pass them all to the function at once. In C, you can accomplish this by defining a structure for the image: Code: ```typedef struct {     float vSize;     float hSize;     float vPos;     float hPos;     int tex_id; } image;``` The typedef ... image part of the code makes the struct defined in between into a custom datatype which can be used in the definition of variables (e.g. image myImage; creates a variable called myImage with the format of the image structure defined above). This definition would also be placed in your header file. The function prototype then becomes int DrawImage(image* img); . The "*" next to "image" means that the variable passed to DrawImage isn't actually an image struct, but rather a pointer to one; this saves the function from having to create an internal copy of the struct and also allows the function to directly edit the data passed to it (though we probably won't need that here). (Just in case you don't know, a pointer is a variable storing the memory address of a given variable; it can be dereferenced to get the actual variable with the "*" operator (e.g. *myPointer gives you whatever variable myPointer refers to). In the case of structs, the elements of a struct* (say, the vSize element of an image struct referred to by a pointer) can be accessed with the "->" operator (e.g. img->vSize gives you the vSize value of the image struct to which the pointer img refers).) Now that we have the function prototype stated, we need to define the function itself. This definition should be placed in an image.c file which #includes image.h: Code: ```int DrawImage(image* img) //notice that there is no semicolon {     //code for drawing image to screen     //don't refer to any variables except those passed to the function     //in order to make the function self-contained     //(for instance, you can refer to img->vSize, but not to someOtherImg->vSize)     //this keeps the function from being tied to any specific variables     //and thus makes it reusable }``` You can then use the function in your code: Code: ```#include "image.h" int main(int argc, char* argv[]) {     image myImage;     //define the variables contained in the struct (by reading from a file, perhaps)     DrawImage(&myImage); //the "&" means to pass the address of the variable                          //i.e. to pass a pointer to the variable }``` However, since there's really no need to refer to myImage except as a pointer, it's usually better to simply define it as an image* to begin with: Code: ```#include "image.h" int main(void) {     myImage = (image*)malloc(sizeof(image)); //allocates a block of memory for an image     //define the variables contained in the struct     DrawImage(myImage); //already a pointer; no need for "&"     free(myImage); //when done, free memory allocated for myImage }``` Hope that helps; if you have any questions feel free to ask, and if anyone notices any errors please point them out. "Learn C" ? - Josh - Jul 5, 2004 12:35 PM Marjock Wrote:Thanks guys, but I think I want to learn/use OpenGL. That's all well and good, but first you need to learn C.