Advertisement
Guest User

Programming guide

a guest
Sep 17th, 2016
1,600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.80 KB | None | 0 0
  1. |=----------------------------------------------------------------------------=|
  2. |=----------------------=[ Programming Guide ]=-------------------------------=|
  3. |=----------------------------------------------------------------------------=|
  4. |=-----------=[ learn programming for (more)fun and profit ]=-----------------=|
  5. |=----------------------------------------------------------------------------=|
  6.  
  7.  
  8. --[ Motivation
  9.  
  10.  
  11. Since people sometimes want to learn to code seriously instead of "just barely
  12. enough to like make game", and they ask where to start, I decided to try and
  13. whip up this little guide so I don't have to keep writing the same replies.
  14.  
  15. Learning programming has benefits for making games. If your code is ineffective,
  16. your game will run slow. If your code is unreadable, not only are you unhireable
  17. as most commercial games are written in teams, you won't be able to maintain
  18. your game yourself as when looking at code you wrote a month ago is the same as
  19. looking at a code written by someone else.
  20.  
  21. I repeat, this guide is for people who want to become skillful at programming,
  22. make it a marketable skill and maybe get a job programming(games or anything
  23. else they may want), not just slap together a game as quickly as possible
  24. regardless of the extensibility, effectivness, performance, readability
  25. and maintainability of code.
  26.  
  27. This guide is suitable for complete newcomers as well as people with some
  28. experience who want to get serious with programming.
  29.  
  30.  
  31. --[ Introduction
  32.  
  33.  
  34. First of all, programming is a craft rather than science. You absolutely can not
  35. learn it just by reading books and thinking abouy it. Programming books
  36. are supposed to be a supplement to the learning not the core of it. Write code
  37. as you go through books, when you read about a new thing, write a program
  38. using it.
  39.  
  40. It's also better to write a bunch of smaller programs (couple hundred lines
  41. at most) rather than immediately trying to write an open world RPG, if you tried
  42. to do that, it would take ages you'd be stuck rewriting your bad code over
  43. and over and in the end never getting any results.
  44.  
  45. My recommendation is in the beginning jst try to use the newest thing you learn
  46. in a simple way(i.e. you just learned how to use an array and a cycle so try
  47. to find the highest number in a sequence of numbers in array), the books I
  48. recommend provide these kinds of excercises at the end of a section. After you
  49. have basic understanding of the language try solving algorhithmic problems from
  50. high school algorhithmic competitions, their solutions are usually short(within
  51. 1K lines depending on dificulty) and they will teach you algorithmic thinking.
  52.  
  53.  
  54. --[ Getting Started
  55.  
  56.  
  57. As a first language to learn, go with C (even people who already code should
  58. learn C if they are looking to be more serious with their programming).
  59. I know you might think C is an incredibly low level language that's unusable in
  60. real projects but this is a meme and it's patently untrue, C is a high level
  61. language and it's perfectly usable for making games(I'll provide examples
  62. later).
  63.  
  64. C is in my opinion the perfect language for beginners for a couple of reasons:
  65.  
  66. a) C is simple and easy to learn.
  67.  
  68. It only has 32 keywords. It provides the most essential tools which
  69. you'll find in pretty much any other language and the vast majority
  70. of languages have syntax inspired by C. You'll find learning new
  71. languages really easy after learning C (with some exceptions like
  72. functional languages).
  73.  
  74. b) C is procedural.
  75.  
  76. It's focused on procedures and algorithms rather than some esoteric
  77. design of object hierarchies. When coding in C you will be focused on
  78. trying to solve problems with procedures(functions) which are simply
  79. implementations of algorhithms. You will learn how a big problem can
  80. often be broken down into smaller ones, each solved with a procedure
  81. of it's own.
  82.  
  83. c) C is still relatively close to the hardware.
  84.  
  85. It makes you understand how memory is handled by the OS on a more
  86. fundamental level which in turn gives you an understanding of how
  87. the higher level languages work "under the hood" and about their
  88. limitations and inefficiencies which you wouldn't know otherwise.
  89.  
  90. Experience in C will make you better in most other languages.
  91.  
  92. d) C is bare bones and has no helper wheels.
  93.  
  94. If you want to shoot yourself in the foot, C will let you do it. This
  95. gives you a lot of power but as you know, with great power comes great
  96. responsibility. If you aren't careful you will fall, but every fall is
  97. going to be a learning opportunity because it will be your fault
  98. not because of some weird behavior of obscure features of the language.
  99.  
  100.  
  101. --{ Recommended Reading
  102.  
  103.  
  104. C is best learned using the book The C Programming Language by K&R (it's the
  105. only book you need, C is very simple).
  106.  
  107. Supplement it by a great book Algorhithms + Data structures = Programs by
  108. Niclaus Wirth which will give you a very solid foundation to build on for
  109. the rest of your programming adventures.
  110.  
  111. Remember to always do the excercises from the books and don't forget to keep
  112. solving those competition problems as you learn. You can solve many with just
  113. the tools presented in the first chapter of K&R.
  114.  
  115. Here is an archive of programming contest problems from just one school and you
  116. can use google to find more:
  117. http://www.cs.umd.edu/Outreach/hsContest.shtml
  118. (at a glance I can tell at least the first three from 2016 are solvable with
  119. just the knowledge from the first chapter of K&R)
  120.  
  121. Treat them like puzzles and have fun, try to challenge yourself to make them
  122. as optimal(fast) as possible).
  123.  
  124. For formatting guidelines (where to put brackets, how to indent code etc),
  125. use this:
  126. https://www.kernel.org/doc/Documentation/CodingStyle
  127.  
  128.  
  129. --{ Linux
  130.  
  131.  
  132. I have to recommend you start programming in Linux. Linux is a very programmer
  133. friendly environment, many great programmers swear by it, and even most good
  134. universities recommend it for their students.
  135.  
  136. I switched to Linux as a highschool kid getting into programming competitions
  137. because it was much easier to work in. Contrary to popular opinion, it's not
  138. some ugly, convoluted or purely terminal based OS anymore. These days,
  139. it's a full fledged user friendly OS on par with Windows and Mac. You can get
  140. many flavors of linux, many of them as pretty and user friendly as mac or
  141. windows. In fact you can get some that specifically try to imitate the look
  142. of Windows(PCLinuxOS) or Mac(ElementaryOS) or go their own way in designing
  143. a user friendly UI.
  144.  
  145. For newcomers I recommend one of these: Ubuntu (or it's various flavors like
  146. Xubuntu), Fedora(Xfce version is my personal OS of choice), Linux Mint
  147. or the two mentioned in the paragraph above.
  148.  
  149. You don't even have to give up windows, you can install Linux under 30 minutes
  150. and you can either install alongside windows(dualbooting, you get a choice when
  151. starting pc for which os to boot) or run it in virtual machine with something
  152. like VirtualBox. At least give it a try.
  153.  
  154.  
  155. --[ Linux development tools
  156.  
  157.  
  158. For writing code I recommend starting with simple editors like Gedit(similar to
  159. notepad on windows but with various enhancements useful for programming) or
  160. Geany(more features but still very light-weight) or Vim(ard to learn but once
  161. learned, your speed will blow through the roof) for the adventurous.
  162.  
  163.  
  164. For compiling your programs you only need to use the terminal.
  165.  
  166. Compiling in linux: http://www.linfo.org/create_c1.html
  167.  
  168.  
  169. The terminal is the most useful tool in Linux and if you're not using it you're
  170. missing out. Here is a good bunch of tutorials to get into it:
  171.  
  172. http://linuxcommand.org/
  173.  
  174.  
  175. When beginning I used to have a printed cheat-sheet with terminal commands which
  176. I put somewhere I could always glance at it if I forgot something:
  177.  
  178. https://files.fosswire.com/2007/08/fwunixrefshot.png
  179.  
  180.  
  181. That's really all you need to get started and after 5 years of coding, that's
  182. still what I use for 90% of my work.
  183.  
  184.  
  185. --[ Making games in C
  186.  
  187.  
  188. Making games in C is entirely possible even for beginners and single-man/small
  189. teams. I recommend starting out with simple terminal games, either text based
  190. or using the ncurses library, but you can safely skip straight to graphics.
  191.  
  192. Once you want to make some games with real graphics, then I suggest SDL2 for
  193. creating windows and simple 2D graphics for 2D games and OpenAL for sound.
  194.  
  195. OpenGL is great for 3D or 2D(more complex than SDL but provides more features).
  196. I'm sure you can find engines for C but I would suggest first writing some games
  197. without them at least for learning purposes, the same idea of learning how it
  198. works "under the hood" applies here.
  199.  
  200. Here are some tutorials:
  201.  
  202. ncurses tutorial: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
  203. SDL2 tutorials : http://lazyfoo.net/tutorials/SDL/index.php
  204. OpenGL tutorials: http://learnopengl.com/
  205. http://lazyfoo.net/tutorials/OpenGL/index.php
  206. (remember to heavily use official library docs as well
  207.  
  208. ==WARNING==
  209. While math above arithmetic is generally not a requirement for being a good
  210. programmer, I believe writing good 3d games(yes even with engines and no amount
  211. of screaming from unitydevs will change my mind) requires understanding
  212. of linear algebra and mathematical analysis up to partial differential
  213. equations. For 2D games you should at least have good understanding of
  214. highschool math, that is, basic algebra, geometry, vectors in 2d space and trig.
  215. Mathematical analysis and linear algebra can be useful in 2D as well.
  216.  
  217.  
  218. --[ Examples
  219.  
  220.  
  221. Here are some real games written in C with source codes you can scrounge
  222. through:
  223.  
  224. Handmade Hero - a project of one programmer to make a full game in C
  225. from scratch (without even using graphics libraries
  226. like OpenGL).
  227.  
  228. It's made by a great professional programmer who worked
  229. on The Witness and he records and streams all the coding
  230. sessions with commentary.
  231.  
  232. https://handmadehero.org/
  233.  
  234. Space Nerds in Space - 3D Star Trek bridge simulator made by one dude
  235. with a couple of friends without any prebaked engine.
  236.  
  237. https://github.com/smcameron/space-nerds-in-space
  238.  
  239. Craft - Minecraft clone.
  240.  
  241. https://github.com/fogleman/Craft
  242.  
  243. Wizznic - Puzzle game in C.
  244.  
  245. https://github.com/DusteDdk/Wizznic
  246.  
  247. ID Soft Games - Many old games by ID Software are in C and now open
  248. source.
  249.  
  250. https://github.com/id-Software
  251.  
  252. There are many other you can find online. I hope you see that writing games in C
  253. is more than possible, not some herculean task. In fact there is no proof that
  254. OOP itself speeds up the process compared to procedural programming, you can
  255. write games procedurally in high level languages and make games as quickly
  256. as in OOP languages.
  257.  
  258.  
  259. --[ Where to move on from C
  260.  
  261.  
  262. From C you can go pretty much anywhere, here are some tips:
  263.  
  264.  
  265. a) OOP Languages
  266.  
  267. C++, Java, C#, D and tons more. They are popular in the industry
  268. and thus marketable so it's a good idea to learn some even it OOP isn't
  269. all that great.
  270.  
  271. It's really hard to recommend a book here cause most books you find on
  272. OOP are literal garbage (I try to be moderate, but here I just can't).
  273. While they teach you to code and you may be able to write working
  274. software, your code will suck. It's sad that it is like this but alas.
  275.  
  276. One of the best ways to code in these languages would be using some
  277. functional principles. So I think that, ironically, to really write good
  278. code in C++ you should dabble a bit in Haskell. (Never touch a book
  279. that's about general OOP theory, it's useless and the ideas are bad.)
  280.  
  281. Here are some articles by really good programmers on using functional
  282. principles in C++, one is from Carmack and one is from the post mortem
  283. of The Witness:
  284.  
  285. http://www.gamasutra.com/view/news/169296/Indepth_Functional_programming_in_C.php
  286.  
  287. https://mollyrocket.com/casey/stream_0019.html
  288.  
  289. I recommend reading the whole post mortem, this is the same guy who
  290. makes Handmade Hero:
  291.  
  292. https://mollyrocket.com/casey/index.html
  293.  
  294.  
  295. From regular books, Eckel and Stroustrup will get you started
  296. sufficiently in C++.
  297.  
  298. Decent Java books would be Java in a Nutshell or The Java Programming
  299. Language (spare yourself though, when dante wrote divine comedy he
  300. ommited one circle of hell and that would be Java).
  301.  
  302. I have no idea about C# or D books since I don't use them.
  303.  
  304. Why OOP is bad: https://www.youtube.com/watch?v=QM1iUe6IofM
  305.  
  306. Another good rant on modern programming conventions:
  307. https://www.youtube.com/watch?v=Y0V7nwaPe2E
  308.  
  309.  
  310. b) Scripting languages.
  311.  
  312. I recommend learning at least one or two scripting language like Bash
  313. (extremely useful if you work in Linux), Python, Lua, Ruby, JS or other.
  314. These languages have an advantage that they are interpreted and very
  315. high level which means you can write code in them extremely quickly,
  316. the tradeoff is much slower performance than compiled languages.
  317. This makes them useful at quickly writing short programs for small
  318. specific tasks(scripts) and prototyping. Most of these have some type
  319. of game library(often more than one) you can use to make games quickly
  320. (LOVE for lua, PyGame for Python etc).
  321.  
  322. Fun book on Ruby: http://poignant.guide/book/
  323.  
  324. Interactive Python tutorial: http://www.learnpython.org/
  325.  
  326. Another Python book: https://learnpythonthehardway.org/book/
  327. (i haven't read this one just heard it's ok)
  328.  
  329. Short and concise book on Python:
  330. http://www.greenteapress.com/thinkpython/thinkpython.pdf
  331.  
  332. This one serves great as reference: http://www.diveintopython3.net/
  333.  
  334. A quick overview of Lua: http://tylerneylon.com/a/learn-lua/
  335.  
  336. Crashcourse in Lua, this one teaches you by analyzing a piece of code:
  337. http://tylerneylon.com/a/learn-lua/
  338.  
  339. Great book on Lua: Programming in Lua
  340.  
  341. 1st edition is available online but i'd recommend
  342. using the newest
  343.  
  344. Advanced concepts in Lua: http://www.lua.org/gems/
  345.  
  346. Remember to use official reference manuals (this applies to all
  347. languages): http://www.lua.org/manual/5.1/
  348.  
  349.  
  350. c) Functional languages.
  351.  
  352. Languages like Haskell and Lisp are for the more adventurous people but
  353. they are worth learning just for what an eye opening experience they
  354. are. Programming functionally is completely different experience that
  355. will challenge everything you know about programming. Game programming
  356. in lazy languages is especially weird due to the real time nature of
  357. games.
  358.  
  359. I also believe functional programming will become way more popular in
  360. the coming years because of the trend of increasing the amount of cores
  361. in processors instead of making the cores faster. This means that
  362. programs that can work on multiple cores at once(multithreading) will be
  363. more and more desireable. It is very hard to write such code in object
  364. oriented languages like C++ but functional languages make it easier by
  365. an order of magnitude.
  366.  
  367. Learn you a Haskell is popular, fun to read tutorial but not that great
  368. from the educational standpoint, use at your own discretion:
  369. http://learnyouahaskell.com/
  370.  
  371. Good compilation of resources for learning haskell:
  372. https://github.com/bitemyapp/learnhaskell
  373.  
  374. Interactive Haskell tutorial: https://tryhaskell.org/
  375.  
  376. Lisp: http://www.gigamonkeys.com/book/
  377.  
  378.  
  379. There are tons of other procedural languages and many languages that don't fit
  380. into the previous specific boxes, mix and match between features or do something
  381. weird. You can explore and find whatever you like.
  382.  
  383. Everything is up to you now.
  384.  
  385. Best of luck with your programming and gamedev adventure!
  386.  
  387.  
  388.  
  389.  
  390.  
  391. --[ Random thoughts/tips/whatever
  392.  
  393.  
  394. --[ Why OOP sucks
  395.  
  396.  
  397. Another reason to start without OOP is that OOP gets into your head. It's an
  398. intuitive concept but it usually doesn't work as nicely as those tiny examples
  399. in books, in fact the bigger you get the worse it becomes.
  400.  
  401. Once you have it in your head however, and you don't know anything else, you
  402. start trying to fit every problem into some classes and objects even if it's
  403. not a good idea. It's not your fault, you're not dumb, it's just that that's
  404. the only tool you've been taught. OOP becomes the hammer and every problem
  405. suddenly looks like a nail.
  406.  
  407.  
  408. I know OOP seems like all the rage now, just don't think it's going to be
  409. a godsend or silver bullet that solves all your programming problems. OOP is
  410. in fact really not that great and doesn't actually provide any advantage it
  411. originally promised over procedural imperative programming (you'd be hard
  412. pressed to find any actual evidence of this).
  413.  
  414. OOP is just popular because it's easy to superficially teach in a corporate
  415. enviroment to create armies of code monkeys that write really bad code.
  416. Popularity has an advantage of tons of libraries but even that is a bit of a
  417. hidden disatvantage as most libraries are shoddily written and not as universal
  418. as you'd think. If you want to make an argument about "reinventing the wheel",
  419. then please realize that libraries are not wheels but complex pieces of code
  420. and you probably have little idea how they're written, and they might not be
  421. implemented effectively or the way that fits your problem domain.
  422.  
  423. So just be careful and don't overly rely on external libraries and if you do,
  424. make sure they are good and fit your problem(SDL2 for example is one of the best
  425. libs out there with virtually 0 overhead and provides extreme simplification
  426. and crossplatform support).
  427.  
  428.  
  429. --[ Functional Programming
  430.  
  431.  
  432. If you want a different paradigm that actually does some very interesting things
  433. then get into functional programming. It's weird, it's completely different but
  434. it provides some amazing things like referential transparency, laziness and
  435. immutable state which actually solve a lot of problems procedural programming
  436. has and OOP promised to solve but made worse(let's solve problems with mutable
  437. state by making an entire paradigm dependent on mutable state, wew lad).
  438.  
  439. The problems with FP are that it's often not as efficient as C since it's high
  440. level, however, Haskell compiler for example offers some great optimization
  441. options and with a little understanding of compilation you can rewrite the
  442. heavy parts of code a C-like performance.
  443.  
  444. There's an extra benefit to learning these since functional principles can be
  445. applied in any language.
  446.  
  447.  
  448. --[ For the l33t h4x0rZ
  449.  
  450.  
  451. Hacking: The Art of Exploitation by Jon Erickson - with all the shitty meme
  452. books on "hacking" it can be hard to identify any serious ones so I decided to
  453. throw one in here if anyone is interested. This one is good. Really fucking
  454. good. However it's also pretty terse and you should already know how to code
  455. and know C pretty well. It's worth reading even if you're not planning to make
  456. a career in security because it will teach you a lot about the inner workings
  457. of operating systems.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement