Valid C

Valid C

Postby Sauer2 » 19 Apr 2010, 18:07

Since I'm a beginner with C, is this a valid header?
Attachments
misc.h
(1.31 KiB) Downloaded 821 times
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby alapshin » 19 Apr 2010, 18:21

Yes, I think it is ok.. Only coding style comments, if you want..
alapshin
 
Posts: 39
Joined: 28 Mar 2010, 12:09

Re: Valid C

Postby Sauer2 » 19 Apr 2010, 19:07

Thank you, why not?
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby alapshin » 19 Apr 2010, 20:05

Changes I may advise (it is just a coding style, so no errors from your side):
1. #ifndef Miscellaneous_H -> #ifndef MISCELLANEOUS_H (Usually all defines are written in capital letters)
2. All #define directives at the top if these are constants
3. const modifier is preferrable than #define for constants since it defines type directly, no chances to cheat for compiler.
4. Use enums for constants. For example:

enum CloudType
{
SMALL = 0,
MEDIUM,
BIG
}

struct Cloud{
CloudType Type;
int XPosition;
int YPosition;
}

I think this way increases readability of the code. "Magic" consts is not a good practice.
5. After #endif usually there is something like "//MISCELLANEOUS_H" (this helps when there are nested ifdefs)
alapshin
 
Posts: 39
Joined: 28 Mar 2010, 12:09

Re: Valid C

Postby Sauer2 » 19 Apr 2010, 20:47

Cool, thanks. Only one problem left: In Case I don't put a ';' behind the structures, the compiler gives me an error.
{l Code}: {l Select All Code}
two or more data types in declaration specifiers

EDIT: I also get
{l Code}: {l Select All Code}
expected specifier-qualifier-list before ‘CloudType’

from the enumerations.
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby alapshin » 19 Apr 2010, 20:54

Sorry, I forgot ";" after enum.. I did not try to compile this..
alapshin
 
Posts: 39
Joined: 28 Mar 2010, 12:09

Re: Valid C

Postby Sauer2 » 19 Apr 2010, 21:04

Something's still fishy...I get the last one even if I have ";" set.
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby alapshin » 19 Apr 2010, 21:11

";" after structs are also needed.
The following code was compiled well with gcc:

enum CloudType
{
S = 0,
M,
B
};

struct Cloud{
CloudType Type; /*0 is small, 1 is medium, 2 is big*/
int XPosition;
int YPosition;
};

int main()
{
return 0;
}
alapshin
 
Posts: 39
Joined: 28 Mar 2010, 12:09

Re: Valid C

Postby Sauer2 » 19 Apr 2010, 21:24

Hmm, still doesn't work, strange... Also I tried adding 'typedef' in front of struct. I always get the second message. :(

Anyway, thank you for the tipps. I'm gonna try this further tomorrow.

EDIT: I got it working, surprisingly. there was enum in front of CloudType Cloud missing.
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby Sauer2 » 27 Apr 2010, 23:30

Another Problem: I have a function loadImage that looks like this:

{l Code}: {l Select All Code}
SDL_Surface* LoadImage(char *file) {
  SDL_Surface *tmp;
  tmp = IMG_Load(file);
 
  if (!tmp) {
    fprintf(stderr, "Error: '%s' could not be opened: %s\n", file, IMG_GetError());
    exit(1);
    return NULL;
  }   
  return tmp;
}


In case I try to load an Image like this:

SDL_Surface playerImage = loadImage("player.png");

i get:
error: invalid initializer
:?
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby Sauer2 » 28 Apr 2010, 15:18

Sorry, that was just some pointer stuff i forgot. (the function gives back a pointer)

Slowly, I realize why THEY hate C... ;)
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby Sauer2 » 30 Apr 2010, 00:20

Does someone know a simple game written in C using SDL? There are some things I'd like to study from another game...
BTW: Why ain't there a question icon similar to this...
icon_question.png
icon_question.png (657 Bytes) Viewed 24176 times
?
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby Sauer2 » 01 May 2010, 21:02

Ok, I have another question for someone familar with C. For the "Falling" game, i want to make own c files and header files for clouds and birds. But what about the variables like the lists and the surfaces I want to put there? Do they need to be global variables or is there a special C trick? If they need to be globals, can I make them read- and write-only from the file they are from?
I'd like to make some kind of Getter/Setter system.


Also: Is it enough to include SDL, etc. in the main file?

Thanks and best regards

Sauer2
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby qubodup » 01 May 2010, 23:43

It might be simpler, if you had the code on-line in a repository so that people can take a look at how you structure your code currently. I would recommend it over tarballs.

I can only recommend http://github.com . If you can figure out how to install git on your system, it will be easy to use, as the basic 2-3 commands are shown so that you can start a repository there.
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Re: Valid C

Postby Sauer2 » 02 May 2010, 12:12

Done, the repository's name is 'falling'.
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby qubodup » 02 May 2010, 12:41

Sauer2 {l Wrote}:Done, the repository's name is 'falling'.

................. link?
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Re: Valid C

Postby Sauer2 » 02 May 2010, 12:48

User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby qubodup » 02 May 2010, 14:13

Sauer2 {l Wrote}:http://github.com/Sauer2/falling

Well, this compiles for me on arch linux 64bit without problems.
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Re: Valid C

Postby Sauer2 » 02 May 2010, 14:25

...because I didn't change many things yet (loading images was optimized by converting them to display format). :p
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby Sauer2 » 02 May 2010, 23:17

Hm, I have a problem here. I restructured the code and put the cloud blitting procedure into 'clouds.c', also I did this with the global pointer to the cloud images.

Now the clouds are not blitted anymore. :( Someone knows why?
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby dusted » 02 May 2010, 23:27

I'm using this method instead of global variables, it works well across multiple files, and I'm fairly sure the function call is optimized out, basically, I have a struct of stuff that I find convinient to have available globally.
I've not checked the assembly output from using this method, but I suspect that there are very few cycles eaten by the pointer dereferencing.

Here's an example:

globalstuff.h:
{l Code}: {l Select All Code}
struct global_s
{
  //global things..
  int somethingGlobal; //A global int
};
typedef struct global_s global_t;

//Function returns pointer to the global struct.
inline global_t getGlobal();


globalstuff.c:
{l Code}: {l Select All Code}
#include "globalstuff.h"

//declare the struct
global_t theActualStruct;

inline global_t getGlobal()
{
  //Return the pointer to the struct (notice the & )
  return( &theActualStruct );
}


Really simple, now for usage, in ANY file that I need to access the global variables:

whateverfile.c
{l Code}: {l Select All Code}
#include "global.h" //Include the header

void whatEverFunctionNeedsAccessToGlobal()
{
  getGlobal()->somethingGlobal=10; //access the struct member
  printf("The value of the int somethingGlobal is %i\n", getGlobal()->somethingGlobal);
}
User avatar
dusted
 
Posts: 83
Joined: 27 Feb 2010, 04:35

Re: Valid C

Postby Sauer2 » 03 May 2010, 00:01

No offense, but isn't that just global variables/structures (still without any function structure around) in their own file with some kind of getter/setter giving their pointer?

Edit: BTW, I know, I'm tired and stuff, but the = instead of == wasn't the source of the problem. :)
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby Sauer2 » 03 May 2010, 11:15

Yay, 'merging clouds' is working. But I think, there better not be 3 Layers of it, performance and stuff..
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: Valid C

Postby dusted » 03 May 2010, 21:48

Yes, that is exactly what it is :) But with the ability to have the same "instance" of the data shared between multiple files, without the oddities that comes with using the extern keyword.
User avatar
dusted
 
Posts: 83
Joined: 27 Feb 2010, 04:35

Re: Valid C

Postby Sauer2 » 04 May 2010, 00:38

That was what i planned in case b. Thank you, though. ;)
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Who is online

Users browsing this forum: No registered users and 1 guest