Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

94 linhas
3.3 KiB

  1. ## C Coding Style Conventions
  2. Here it is a list with some of the code conventions used by raylib:
  3. Code element | Convention | Example
  4. --- | :---: | ---
  5. Defines | ALL_CAPS | `#define PLATFORM_DESKTOP`
  6. Macros | ALL_CAPS | `#define MIN(a,b) (((a)<(b))?(a):(b))`
  7. Variables | lowerCase | `int screenWidth = 0;`, `float targetFrameTime = 0.016f;`
  8. Local variables | lowerCase | `Vector2 playerPosition = { 0 };`
  9. Global variables | lowerCase | `bool windowReady = false;`
  10. Constants | lowerCase | `const int maxValue = 8;`
  11. Pointers | MyType *pointer | `Texture2D *array = NULL;`
  12. float values | always x.xf | `float gravity = 10.0f`
  13. Operators | value1*value2 | `int product = value*6;`
  14. Operators | value1/value2 | `int division = value/4;`
  15. Operators | value1 + value2 | `int sum = value + 10;`
  16. Operators | value1 - value2 | `int res = value - 5;`
  17. Enum | TitleCase | `enum TextureFormat`
  18. Enum members | ALL_CAPS | `PIXELFORMAT_UNCOMPRESSED_R8G8B8`
  19. Struct | TitleCase | `struct Texture2D`, `struct Material`
  20. Struct members | lowerCase | `texture.width`, `color.r`
  21. Functions | TitleCase | `InitWindow()`, `LoadImageFromMemory()`
  22. Functions params | lowerCase | `width`, `height`
  23. Ternary Operator | (condition)? result1 : result2 | `printf("Value is 0: %s", (value == 0)? "yes" : "no");`
  24. Some other conventions to follow:
  25. - **ALWAYS** initialize all defined variables.
  26. - **Do not use TABS**, use 4 spaces instead.
  27. - Avoid trailing spaces, please, avoid them
  28. - Control flow statements always are followed **by a space**:
  29. ```c
  30. if (condition) value = 0;
  31. while (!WindowShouldClose())
  32. {
  33. }
  34. for (int i = 0; i < NUM_VALUES; i++) printf("%i", i);
  35. // Be careful with the switch formatting!
  36. switch (value)
  37. {
  38. case 0:
  39. {
  40. } break;
  41. case 2: break;
  42. default: break;
  43. }
  44. ```
  45. - All conditions checks are **always between parenthesis** but not boolean values:
  46. ```c
  47. if ((value > 1) && (value < 50) && valueActive)
  48. {
  49. }
  50. ```
  51. - When dealing with braces or curly brackets, open-close them in aligned mode:
  52. ```c
  53. void SomeFunction()
  54. {
  55. // TODO: Do something here!
  56. }
  57. ```
  58. **If proposing new functions, please try to use a clear naming for function-name and functions-parameters, in case of doubt, open an issue for discussion.**
  59. ## Files and Directories Naming Conventions
  60. - Directories will be named using `snake_case`: `resources/models`, `resources/fonts`
  61. - Files will be named using `snake_case`: `main_title.png`, `cubicmap.png`, `sound.wav`
  62. _NOTE: Avoid any space or special character in the files/dir naming!_
  63. ## Games/Examples Directories Organization Conventions
  64. - Data files should be organized by context and usage in the game, think about the loading requirements for data and put all the resources that need to be loaded at the same time together.
  65. - Use descriptive names for the files, it would be perfect if just reading the name of the file, it was possible to know what is that file and where fits in the game.
  66. - Here it is an example, note that some resources require to be loaded all at once while other require to be loaded only at initialization (gui, font).
  67. ```
  68. resources/audio/fx/long_jump.wav
  69. resources/audio/music/main_theme.ogg
  70. resources/screens/logo/logo.png
  71. resources/screens/title/title.png
  72. resources/screens/gameplay/background.png
  73. resources/characters/player.png
  74. resources/characters/enemy_slime.png
  75. resources/common/font_arial.ttf
  76. resources/common/gui.png
  77. ```